Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyaser authored and Nyaser committed Aug 29, 2024
1 parent 9395036 commit c4add46
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 50 deletions.
89 changes: 52 additions & 37 deletions Form.Fixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public partial class Form_Fixer : Form
private Config? Config;
private readonly List<string> AudioList;
private static readonly ResourceManager Resources = new(typeof(Form_Fixer));
private CancellationTokenSource cancellationTokenSource = new();

public Form_Fixer(Dictionary<string, object> args)
{
Expand All @@ -29,10 +28,13 @@ private void Form_Fixer_Load(object sender, EventArgs e)
textBox_gameVersion.Text = Config.Version;
}

private CancellationTokenSource? source;

private void Button_Compare_Click(object sender, EventArgs e)
{
button_cancel.Enabled = !(button_compare.Enabled = button_start.Enabled = false);
Compare(cancellationTokenSource.Token).GetAwaiter().OnCompleted(() =>
source = new CancellationTokenSource();
Compare(source.Token).GetAwaiter().OnCompleted(() =>
{
GC.Collect(2, GCCollectionMode.Aggressive, true, true); GC.WaitForFullGCComplete();
progressBar.Value = 0; progressBar.Maximum = 100; progressBar.Style = ProgressBarStyle.Continuous;
Expand All @@ -42,27 +44,19 @@ private void Button_Compare_Click(object sender, EventArgs e)

private void Button_Cancel_Click(object sender, EventArgs e)
{
cancellationTokenSource.Cancel(); cancellationTokenSource.Token.WaitHandle.WaitOne();
cancellationTokenSource.Dispose(); cancellationTokenSource = new CancellationTokenSource();
source?.Cancel();
source?.Token.WaitHandle.WaitOne();
source?.Dispose();
}

private async Task Compare(CancellationToken token)
private async Task Compare(CancellationToken token = default)
{
if (Config is null || Config.Channel is null) return;
groupBox_suplus.Text = Resources.GetString("groupBox_suplus.Text"); textBox_suplus.Clear();
groupBox_missing.Text = Resources.GetString("groupBox_missing.Text"); textBox_missing.Clear();
groupBox_progress.Text = $"{Resources.GetString("groupBox_progress.Text")} ({Downloader.Text.tbox_waitServer})";
progressBar.Style = ProgressBarStyle.Marquee;
HttpClient http = new()
{
BaseAddress = new Uri((await API.GetAsync(Config.Channel)).main.major.res_list_url)
};
Dictionary<string, string> pkg_version = [];
pkg_version["game"] = await http.GetStringAsync($"{http.BaseAddress}/pkg_version", token);
foreach (string item in AudioList)
{
pkg_version[item] = await http.GetStringAsync($"{http.BaseAddress}/{item}_pkg_version", token);
}
Dictionary<string, string> pkg_version = await GetOnlinePackageVersionAsync(Config.Channel, token);

List<FileInfoH> online = [];
online.AddRange(from KeyValuePair<string, string> p in pkg_version
Expand All @@ -82,6 +76,7 @@ where j is not null
{
string remoteName = FileInfoH.GetRemoteName(item.FullName);
if (remoteName.Equals("config.ini")
|| remoteName.EndsWith("pkg_version")
|| remoteName.StartsWith("ScreenShot")
|| remoteName.Contains("/webCaches/")
|| remoteName.Contains("/SDKCaches/")
Expand Down Expand Up @@ -121,6 +116,22 @@ where j is not null
string missing_str = string.Empty; missing.ForEach((i) => missing_str += $"{i}\r\n"); textBox_missing.Text = missing_str; missing.Clear();
}

public async Task<Dictionary<string, string>> GetOnlinePackageVersionAsync(string? channel, CancellationToken token = default)
{
HttpClient http = new()
{
BaseAddress = new Uri($"{(await API.GetAsync(channel, token)).main.major.res_list_url}")
};
Dictionary<string, string> pkg_version = [];
pkg_version["pkg_version"] = await http.GetStringAsync($"{http.BaseAddress}/pkg_version", token);
foreach (string item in AudioList)
{
pkg_version[item + "_pkg_version"] = await http.GetStringAsync($"{http.BaseAddress}/{item}_pkg_version", token);
}

return pkg_version;
}

private void Button_Start_Click(object sender, EventArgs e)
{
button_compare.Enabled = button_start.Enabled = false;
Expand All @@ -133,34 +144,38 @@ private void Button_Start_Click(object sender, EventArgs e)
});
}

private async Task StartFix()
private async Task StartFix(CancellationToken token = default)
{
if (string.IsNullOrWhiteSpace(textBox_suplus.Text) && string.IsNullOrWhiteSpace(textBox_missing.Text))
if (!string.IsNullOrWhiteSpace(textBox_suplus.Text) || !string.IsNullOrWhiteSpace(textBox_missing.Text) || DialogResult.OK == MessageBox.Show(this, Downloader.Text.mbox_nothing2Fix, Resources.GetString("$this.Text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{
_ = MessageBox.Show(this, Downloader.Text.mbox_nothing2Fix, Resources.GetString("$this.Text"), MessageBoxButtons.OK, MessageBoxIcon.Information); return;
}
string version = (await API.GetAsync(Config?.Channel)).main.major.version;
try
{
string path_temp = DirectoryH.EnsureNew(Properties.Settings.Default.TempPath).FullName;
if (!string.IsNullOrWhiteSpace(textBox_suplus.Text))
{
await File.AppendAllTextAsync($"{path_temp}\\deletefiles.txt", textBox_suplus.Text);
}
if (!string.IsNullOrWhiteSpace(textBox_missing.Text))
string version = (await API.GetAsync(Config?.Channel, token)).main.major.version;
try
{
await File.AppendAllTextAsync($"{path_temp}\\downloadfiles.txt", textBox_missing.Text);
string path_temp = DirectoryH.EnsureNew(Properties.Settings.Default.TempPath).FullName;
if (!string.IsNullOrWhiteSpace(textBox_suplus.Text))
{
await File.AppendAllTextAsync($"{path_temp}\\deletefiles.txt", textBox_suplus.Text, token);
}
if (!string.IsNullOrWhiteSpace(textBox_missing.Text))
{
await File.AppendAllTextAsync($"{path_temp}\\downloadfiles.txt", textBox_missing.Text, token);
}
await Worker.HPatchAsync(this, Config?.Channel, token);
Dictionary<string, string> pkg_version = await GetOnlinePackageVersionAsync(Config?.Channel, token);
foreach (var item in pkg_version)
{
await File.AppendAllTextAsync($"{path_temp}\\{item.Key}", item.Value, token);
}
await Worker.ApplyUpdate(this, version, token);
}
await Worker.HPatchAsync(this, Config?.Channel);
await Worker.ApplyUpdate(this, version);
}
catch (IOException ex)
{
if (DialogResult.Retry == MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error))
catch (IOException ex)
{
await StartFix();
if (DialogResult.Retry == MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error))
{
await StartFix(token);
}
else throw;
}
else throw;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Helper.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ internal class API

public static Dictionary<string, string> AudioList => audioList;

public async static Task<dynamic> GetAsync(string? channel)
public async static Task<dynamic> GetAsync(string? channel, CancellationToken token = default)
{
if (channel is not null && ApiList.TryGetValue(channel, out string? api) && api is not null)
{
using HttpClient http = new();
string value = await http.GetStringAsync(api);
string value = await http.GetStringAsync(api, token);
dynamic ret = JsonConvert.DeserializeObject<dynamic>(value) ?? throw new Exception();
if ((int?)ret.retcode is not 0) throw new Exception(ret.message);
dynamic res = ret.data.game_packages[0] ?? throw new Exception();
Expand Down
18 changes: 9 additions & 9 deletions Helper.Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static async Task<int> ApplyDownload(string? channel)
return exitCode;
}

public static async Task<int> ApplyHDiff()
public static async Task<int> ApplyHDiff(CancellationToken token = default)
{
string path_game = DirectoryH.EnsureExists(Properties.Settings.Default.GamePath).FullName;
string path_temp = DirectoryH.EnsureExists(Properties.Settings.Default.TempPath).FullName;
Expand All @@ -81,7 +81,7 @@ public static async Task<int> ApplyHDiff()
await writer.WriteLineAsync($"@echo off & title {Genshin.Downloader.Text.msg_doing_hpatch} & chcp 65001 & cls");
while (!reader.EndOfStream)
{
string? line = await reader.ReadLineAsync();
string? line = await reader.ReadLineAsync(token);
if (line != null)
{
string pattern = @"{""remoteName"": ""(.+)""}";
Expand All @@ -107,13 +107,13 @@ public static async Task<int> ApplyHDiff()
});
if (process is not null)
{
await process.WaitForExitAsync();
await process.WaitForExitAsync(token);
return process.ExitCode;
}
return -1;
}

public static async Task ApplyUpdate(Form? owner = null, string? version = null)
public static async Task ApplyUpdate(Form? owner = null, string? version = null, CancellationToken token = default)
{
string path_game = DirectoryH.EnsureExists(Properties.Settings.Default.GamePath).FullName;
string path_temp = DirectoryH.EnsureExists(Properties.Settings.Default.TempPath).FullName;
Expand All @@ -125,7 +125,7 @@ public static async Task ApplyUpdate(Form? owner = null, string? version = null)
});
if (process is not null)
{
await process.WaitForExitAsync();
await process.WaitForExitAsync(token);
}
if (process?.ExitCode is 0)
{
Expand All @@ -140,7 +140,7 @@ public static async Task ApplyUpdate(Form? owner = null, string? version = null)
}
else if (DialogResult.Retry == MessageBox.Show(owner, $"{Genshin.Downloader.Text.msg_failed_code}{process?.ExitCode}", Genshin.Downloader.Text.msg_failed_task, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error))
{
await ApplyUpdate(owner, version);
await ApplyUpdate(owner, version, token);
}
}

Expand All @@ -167,17 +167,17 @@ public static async Task ApplyUnzip(Form? owner, string path)
}
}

public static async Task HPatchAsync(Form? owner = null, string? channel = null)
public static async Task HPatchAsync(Form? owner = null, string? channel = null, CancellationToken token = default)
{
string path_temp = DirectoryH.EnsureExists(Properties.Settings.Default.TempPath).FullName;
int exitCode;
if ((File.Exists($"{path_temp}\\downloadfiles.txt") && 0 != (exitCode = await ApplyDownload(channel)))
|| (File.Exists($"{path_temp}\\hdifffiles.txt") && 0 != (exitCode = await ApplyHDiff()))
|| (File.Exists($"{path_temp}\\hdifffiles.txt") && 0 != (exitCode = await ApplyHDiff(token)))
|| (File.Exists($"{path_temp}\\deletefiles.txt") && 0 != (exitCode = await ApplyDelete())))
{
if (DialogResult.Retry == MessageBox.Show(owner, $"{Genshin.Downloader.Text.msg_failed_code}{exitCode}", Genshin.Downloader.Text.msg_failed_task, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error))
{
await HPatchAsync(owner, channel);
await HPatchAsync(owner, channel, token);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Text.Designer.cs

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

2 changes: 1 addition & 1 deletion Text.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<value>启动失败,或许你没有安装好原神?</value>
</data>
<data name="mbox.nothing2Fix" xml:space="preserve">
<value>没有需要修复的文件!</value>
<value>没有需要修复的资源文件,将仅为你更新 pkg_version 文件。</value>
</data>
<data name="mbox.packageEmpty" xml:space="preserve">
<value>未指定资源包或指定的资源包不存在</value>
Expand Down

0 comments on commit c4add46

Please sign in to comment.