diff --git a/DiscordProxyStart/Servers/WinStartManager.cs b/DiscordProxyStart/Servers/WinStartManager.cs index ae1b25f..728a933 100644 --- a/DiscordProxyStart/Servers/WinStartManager.cs +++ b/DiscordProxyStart/Servers/WinStartManager.cs @@ -21,15 +21,23 @@ public static void Start() var setupPath = SetupPathHelper.GetInstallPath("Discord"); if (File.Exists(iniPath)) { - var path = IniFileHelper.GetIniValue(iniPath, "Config", "Path"); - if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) + var customPath = IniFileHelper.GetIniValue(iniPath, "Config", "Path"); + if (!string.IsNullOrWhiteSpace(customPath)) { - setupPath = path; + setupPath = customPath; } } - if (string.IsNullOrEmpty(setupPath) || !Directory.Exists(setupPath)) + if (string.IsNullOrEmpty(setupPath)) { + MsgBox.Show("Error", "没有找到Discord安装目录#1", MsgBoxButtons.OK); + Debug.WriteLine("没有找到安装目录"); + return; + } + + if (!Directory.Exists(setupPath)) + { + //安装目录不存在,使用另外的方式检查Discord.exe var exePath = SetupPathHelper.GetDiscordExePath("Discord"); if (File.Exists(exePath)) @@ -38,7 +46,7 @@ public static void Start() } else { - MsgBox.Show("Error", "没有找到Discord安装目录", MsgBoxButtons.OK); + MsgBox.Show("Error", "没有找到Discord安装目录#2", MsgBoxButtons.OK); Debug.WriteLine("没有找到安装目录"); return; } @@ -109,19 +117,28 @@ private static void NormalStart(string setupPath) try { Debug.WriteLine("正在准备启动..."); - var appPath = GetAppPath(setupPath); + var appPaths = GetAppPath(setupPath); var proxy = GetProxy(); var copyResult = CopyVersionDll(setupPath); //TODO 自动gost转换socks代理? + var mainExeFileName = "Discord.exe"; + var appPath = appPaths.FirstOrDefault(); + if (File.Exists(Path.Combine(appPath, "DiscordCanary.exe"))) + { + mainExeFileName = "DiscordCanary.exe"; + } - if (!string.IsNullOrEmpty(proxy) && copyResult && Directory.Exists(appPath.FirstOrDefault())) + + if (!string.IsNullOrEmpty(proxy) && copyResult && Directory.Exists(appPath)) { Debug.WriteLine("启动进程..."); + + var process = new Process(); process.StartInfo.FileName = updatePath; - process.StartInfo.Arguments = $"--processStart Discord.exe --a=--proxy-server={proxy}"; - process.StartInfo.WorkingDirectory = appPath.FirstOrDefault(); + process.StartInfo.Arguments = $"--processStart {mainExeFileName} --a=--proxy-server={proxy}"; + process.StartInfo.WorkingDirectory = appPath; process.Start(); //TODO 等待升级窗口出现并销毁 diff --git a/DiscordProxyStart/Utils/IniFileHelper.cs b/DiscordProxyStart/Utils/IniFileHelper.cs index 70b99d4..f00b6b6 100644 --- a/DiscordProxyStart/Utils/IniFileHelper.cs +++ b/DiscordProxyStart/Utils/IniFileHelper.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace DiscordProxyStart.Utils @@ -13,41 +14,48 @@ internal class IniFileHelper { public static string GetIniValue(string filePath, string sectionName, string keyName) { - try { - using (StreamReader reader = new StreamReader(filePath)) + string fileContent = File.ReadAllText(filePath, Encoding.UTF8); + + fileContent = Regex.Replace(fileContent, @"\r\n?|\n", "\n"); + + using (StringReader reader = new StringReader(fileContent)) { string line; + bool isInSection = false; + while ((line = reader.ReadLine()) != null) { - // 判断是否为指定的 Section - if (line.Trim().StartsWith($"[{sectionName}]")) + line = line.Trim(); + + if (!isInSection) { - // 读取 Key 对应的值 - while ((line = reader.ReadLine()) != null && line.Trim().StartsWith(keyName)) + if (line.StartsWith($"[{sectionName}]")) { - string[] parts = line.Split('=', 2); - if (parts.Length == 2) - { - // 返回值 - string value = parts[1].Trim(); - return value; - } + isInSection = true; + } + } + else + { + if (line.StartsWith("[")) + { + break; + } + if (line.StartsWith(keyName)) + { + string[] parts = line.Split(new[] { '=' }, 2); + return parts.Length == 2 ? parts[1].Trim() : string.Empty; } } } } - } catch (Exception ex) { - - + } - - // 如果未找到则返回空字符串 return string.Empty; } @@ -79,10 +87,7 @@ public static void SetIniValue(string filePath, string sectionName, string keyNa if (!keyExists) { - List newLines = new List(); - newLines.AddRange(lines.Take(i + 1)); - newLines.Add($"{keyName}={value}"); - newLines.AddRange(lines.Skip(i + 1)); + List newLines = [.. lines.Take(i + 1), $"{keyName}={value}", .. lines.Skip(i + 1)]; lines = newLines.ToArray(); } @@ -92,10 +97,7 @@ public static void SetIniValue(string filePath, string sectionName, string keyNa if (!sectionExists) { - List newLines = new List(); - newLines.AddRange(lines); - newLines.Add($"[{sectionName}]"); - newLines.Add($"{keyName}={value}"); + List newLines = [.. lines, $"[{sectionName}]", $"{keyName}={value}"]; lines = newLines.ToArray(); }