Skip to content

Commit

Permalink
[+] 添加更新代理
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Sep 28, 2024
1 parent 64087e6 commit ef1d23d
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 48 deletions.
89 changes: 57 additions & 32 deletions RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 31 additions & 10 deletions RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using Newtonsoft.Json;
using RevokeMsgPatcher.Model;
using RevokeMsgPatcher.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Newtonsoft.Json;
using RevokeMsgPatcher.Model;
using RevokeMsgPatcher.Modifier;
using RevokeMsgPatcher.Utils;

namespace RevokeMsgPatcher.Forms
{
Expand All @@ -26,6 +22,28 @@ public FormLiteLoaderQQNT()
InitializeComponent();
InitializeDataGridView();
txtQQNTPath.Text = FindInstallPath();

InitCboProxyList();
}

private void InitCboProxyList()
{
// 添加代理 URL 到下拉菜单
foreach (var proxy in ProxySpeedTester.ProxyUrls)
{
cboGithubProxy.Items.Add(proxy);
}

// 异步测试代理速度并设置默认选项
Task.Run(async () =>
{
var fastestProxy = await ProxySpeedTester.GetFastestProxyAsync();
Debug.WriteLine(fastestProxy);
if (!string.IsNullOrEmpty(fastestProxy))
{
cboGithubProxy.Invoke(new Action(() => cboGithubProxy.SelectedItem = fastestProxy));
}
});
}


Expand Down Expand Up @@ -150,7 +168,8 @@ private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
}

data[e.RowIndex].Row.Cells["StatusColumn"].Value = "正在更新";
Task.Run(() => data[e.RowIndex].CheckAndUpdate());
var proxyUrl = cboGithubProxy.Text;
Task.Run(() => data[e.RowIndex].CheckAndUpdate(proxyUrl));
}
else if (e.ColumnIndex == dataGridView1.Columns["NameColumn"].Index || e.ColumnIndex == dataGridView1.Columns["AuthorColumn"].Index)
{
Expand All @@ -162,9 +181,10 @@ private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

private void btnCheckUpdateAll_Click(object sender, EventArgs e)
{
var proxyUrl = cboGithubProxy.Text;
foreach (var item in data)
{
Task.Run(() => item.CheckAndUpdate());
Task.Run(() => item.CheckAndUpdate(proxyUrl));
}
}

Expand All @@ -176,6 +196,7 @@ private void btnRestore_Click(object sender, EventArgs e)
MessageBox.Show("请选择正确的QQNT安装路径!");
return;
}

try
{
string appPath = GetAppPath(installPath);
Expand Down
17 changes: 11 additions & 6 deletions RevokeMsgPatcher/Model/LiteLoaderRowData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ public void GetLocalVersionAndUpdateStatus()
}
}

public async Task<string> GetRemoteVersion()
public async Task<string> GetRemoteVersion(string proxyUrl = null)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(VersionJsonUrl);
var url = string.IsNullOrEmpty(proxyUrl) ? VersionJsonUrl : proxyUrl + "/" + VersionJsonUrl;
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
Expand All @@ -143,7 +144,7 @@ public async Task<string> GetRemoteVersion()
}
}

public async Task CheckAndUpdate()
public async Task CheckAndUpdate(string proxyUrl = null)
{
try
{
Expand All @@ -153,13 +154,15 @@ public async Task CheckAndUpdate()
}

string localVersion = GetLocalVersion();
string remoteVersion = await GetRemoteVersion();
string remoteVersion = await GetRemoteVersion(proxyUrl);

if (localVersion == null || new Version(remoteVersion) > new Version(localVersion))
{
UpdateStatus($"存在新版本{remoteVersion},正在下载...");
Debug.WriteLine("发现新版本,正在下载...");
string downloadedFilePath = await DownloadLatestPackage(DownloadUrl.Replace("#{version}", remoteVersion), Path.Combine(Application.StartupPath, "Public/Download"));
var url = DownloadUrl.Replace("#{version}", remoteVersion);
url = string.IsNullOrEmpty(proxyUrl) ? url : proxyUrl + "/" + url;
string downloadedFilePath = await DownloadLatestPackage(url, Path.Combine(Application.StartupPath, "Public/Download"));
Debug.WriteLine("下载到:" + downloadedFilePath);
UpdateStatus($"下载成功,解压中...");

Expand All @@ -170,6 +173,7 @@ public async Task CheckAndUpdate()
{
Directory.Delete(extractPath, true);
}

Directory.CreateDirectory(extractPath);
ZipFile.ExtractToDirectory(downloadedFilePath, extractPath);

Expand All @@ -190,10 +194,12 @@ public async Task CheckAndUpdate()
{
File.Delete(downloadedFilePath);
}

if (Directory.Exists(extractPath))
{
Directory.Delete(extractPath, true);
}

Debug.WriteLine("清理完成。");
UpdateStatus($"{remoteVersion}更新完成");
}
Expand All @@ -208,7 +214,6 @@ public async Task CheckAndUpdate()
Debug.WriteLine(e.ToString());
UpdateStatus(Status + " 后发生异常:" + e.Message);
}

}

private string FindDirectoryWithJson(string extractPath, int maxDepth = 2)
Expand Down
1 change: 1 addition & 0 deletions RevokeMsgPatcher/RevokeMsgPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<Compile Include="Utils\PathUtil.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\ProxySpeedTester.cs" />
<Compile Include="Utils\VersionUtil.cs" />
<EmbeddedResource Include="FormMain.resx">
<DependentUpon>FormMain.cs</DependentUpon>
Expand Down
Loading

0 comments on commit ef1d23d

Please sign in to comment.