Skip to content

Commit

Permalink
修正金丝雀支持
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Apr 24, 2024
1 parent 9bdff50 commit 381a5cc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
35 changes: 26 additions & 9 deletions DiscordProxyStart/Servers/WinStartManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 等待升级窗口出现并销毁
Expand Down
54 changes: 28 additions & 26 deletions DiscordProxyStart/Utils/IniFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Check warning on line 28 in DiscordProxyStart/Utils/IniFileHelper.cs

View workflow job for this annotation

GitHub Actions / Dotnet Build

Converting null literal or possible null value to non-nullable type.
{
// 判断是否为指定的 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)

Check warning on line 54 in DiscordProxyStart/Utils/IniFileHelper.cs

View workflow job for this annotation

GitHub Actions / Dotnet Build

The variable 'ex' is declared but never used
{



}


// 如果未找到则返回空字符串
return string.Empty;
}

Expand Down Expand Up @@ -79,10 +87,7 @@ public static void SetIniValue(string filePath, string sectionName, string keyNa

if (!keyExists)
{
List<string> newLines = new List<string>();
newLines.AddRange(lines.Take(i + 1));
newLines.Add($"{keyName}={value}");
newLines.AddRange(lines.Skip(i + 1));
List<string> newLines = [.. lines.Take(i + 1), $"{keyName}={value}", .. lines.Skip(i + 1)];
lines = newLines.ToArray();
}

Expand All @@ -92,10 +97,7 @@ public static void SetIniValue(string filePath, string sectionName, string keyNa

if (!sectionExists)
{
List<string> newLines = new List<string>();
newLines.AddRange(lines);
newLines.Add($"[{sectionName}]");
newLines.Add($"{keyName}={value}");
List<string> newLines = [.. lines, $"[{sectionName}]", $"{keyName}={value}"];
lines = newLines.ToArray();
}

Expand Down

0 comments on commit 381a5cc

Please sign in to comment.