From 5cfb6e50ae4bdddcef614bd24d8984ad619c38f2 Mon Sep 17 00:00:00 2001 From: Kyle Date: Wed, 3 May 2023 23:38:30 +0800 Subject: [PATCH] Fix global/zh server detection --- src/Program.cs | 137 +++++++++++++++++++++------------ src/Properties/AssemblyInfo.cs | 4 +- src/Properties/app.manifest | 73 ++++++++++++++++++ src/StarRailRpc.csproj | 5 ++ 4 files changed, 168 insertions(+), 51 deletions(-) create mode 100644 src/Properties/app.manifest diff --git a/src/Program.cs b/src/Program.cs index 8b1c7d5..c0ce65a 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,8 +1,11 @@ using DiscordRPC; using System; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Management; using System.Runtime.InteropServices; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -46,10 +49,8 @@ static void Main() Debug.Print($"InLoop"); - var hndlZh = FindWindow("UnityWndClass", "崩坏:星穹铁道"); - var hndlEn = FindWindow("UnityWndClass", "Honkai: Star Rail"); - if (hndlZh == IntPtr.Zero && - hndlEn == IntPtr.Zero) + var handle = FindWindow("UnityWndClass", "崩坏:星穹铁道"); // "Honkai: Star Rail" + if (handle == IntPtr.Zero) { Debug.Print($"Not found game process."); playing = false; @@ -66,69 +67,37 @@ static void Main() try { - var procEn = Process.GetProcesses().FirstOrDefault(x => x.MainWindowHandle == hndlEn); - var procZh = Process.GetProcesses().FirstOrDefault(x => x.MainWindowHandle == hndlZh); + var process = Process.GetProcesses().First(x => x.MainWindowHandle == handle); - Debug.Print($"Check process with {hndlZh} | {procZh?.ProcessName} || {hndlEn} | {procEn?.ProcessName}"); + var isGlobal = CheckGlobal(process); - if (procZh != null) + Debug.Print($"Check process with {handle} | {process.ProcessName}"); + + if (!isGlobal) { if (!playing) { playing = true; - - clientZh.SetPresence(new RichPresence - { - Assets = new Assets - { - LargeImageKey = "logo", - LargeImageText = "崩坏:星穹铁道", - }, - Timestamps = Timestamps.Now, - }); - - Debug.Print($"Set RichPresence to {procZh?.ProcessName}"); + clientZh.UpdateRpc("logo", "崩坏:星穹铁道"); + Debug.Print($"Set RichPresence to {process.ProcessName}"); } else { - Debug.Print($"Keep RichPresence to {procZh?.ProcessName}"); + Debug.Print($"Keep RichPresence to {process.ProcessName}"); } } - else if (procEn != null) + else { if (!playing) { playing = true; - - clientEn.SetPresence(new RichPresence - { - Assets = new Assets - { - LargeImageKey = "logo", - LargeImageText = "Honkai: Star Rail", - }, - Timestamps = Timestamps.Now, - }); - - Debug.Print($"Set RichPresence to {procEn?.ProcessName}"); + clientEn.UpdateRpc("logo", "Honkai: Star Rail"); + Debug.Print($"Set RichPresence to {process.ProcessName}"); } else { - Debug.Print($"Keep RichPresence to {procEn?.ProcessName}"); - } - } - else - { - playing = false; - if (clientEn.CurrentPresence != null) - { - clientEn.ClearPresence(); - } - if (clientZh.CurrentPresence != null) - { - clientZh.ClearPresence(); + Debug.Print($"Keep RichPresence to {process.ProcessName}"); } - Debug.Print($"Clear RichPresence"); } } catch (Exception e) @@ -192,6 +161,76 @@ static void Main() Application.Run(); } - [DllImport("user32.dll", EntryPoint = "FindWindow")] + private static void UpdateRpc(this DiscordRpcClient client, string key, string text) + => client.SetPresence(new RichPresence + { + Assets = new Assets + { + LargeImageKey = key, + LargeImageText = text, + }, + Timestamps = Timestamps.Now, + }); + + private static bool CheckGlobal(Process process) + { + var path = GetPathOfWindow(process); + var file = Path.Combine(Path.GetDirectoryName(path)!, "config.ini"); + + if (!File.Exists(file)) + throw new FileNotFoundException("Config not found"); + + var value = GetIniSectionValue(file, "General", "cps", "gw_PC"); + + return value is not ("gw_PC" or "bilibili_PC"); + } + + private static string GetIniSectionValue(string file, string section, string key, string defaultVal = null) + { + var stringBuilder = new StringBuilder(1024); + GetPrivateProfileString(section, key, defaultVal, stringBuilder, 1024, file); + return stringBuilder.ToString(); + } + + [DllImport("kernel32.dll")] + private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); + + [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + + private static string GetPathOfWindow(Process process) + { + try + { + return process.MainModule!.FileName; + } + catch (Exception e) + { + Debug.Print(e.ToString()); + } + + try + { + using var searcher = new ManagementObjectSearcher("SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process"); + using var collection = searcher.Get(); + var results = collection.Cast(); + + foreach (var item in results) + { + var id = (int)(uint)item["ProcessId"]; + var path = (string)item["ExecutablePath"]; + + if (id == process.Id) + { + return path; + } + } + } + catch (Exception e) + { + Debug.Print(e.ToString()); + } + + throw new InvalidOperationException("Failed to get path of handle"); + } } diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 5b4fc56..f4fd70f 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")] diff --git a/src/Properties/app.manifest b/src/Properties/app.manifest new file mode 100644 index 0000000..a269fff --- /dev/null +++ b/src/Properties/app.manifest @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/StarRailRpc.csproj b/src/StarRailRpc.csproj index 4a5e689..d036e2e 100644 --- a/src/StarRailRpc.csproj +++ b/src/StarRailRpc.csproj @@ -62,6 +62,9 @@ false + + Properties\app.manifest + ..\packages\DiscordRichPresence.1.1.3.18\lib\net45\DiscordRPC.dll @@ -71,6 +74,7 @@ + @@ -96,6 +100,7 @@ True + SettingsSingleFileGenerator Settings.Designer.cs