Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyaser authored and Nyaser committed Jul 18, 2024
1 parent 82df40f commit 645a231
Show file tree
Hide file tree
Showing 14 changed files with 558 additions and 246 deletions.
21 changes: 21 additions & 0 deletions Form.Fixer.Designer.cs

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

231 changes: 143 additions & 88 deletions Form.Fixer.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,170 @@
using Newtonsoft.Json;
using System.Resources;

namespace Genshin.Downloader
namespace Genshin.Downloader;

public partial class Form_Fixer : Form
{
public partial class Form_Fixer : Form
private Config? Config;
private readonly List<string> AudioList;
private static readonly ResourceManager resourceManager = new(typeof(Form_Fixer));

public Form_Fixer(Dictionary<string, object> args)
{
private Config? Config;
private readonly List<string> AudioList;
InitializeComponent();
AudioList = (List<string>)args["AudioList"];
}

public Form_Fixer(Dictionary<string, object> args)
private void Form_Fixer_Load(object sender, EventArgs e)
{
Show();
if (StringH.WhiteSpaceCheck(Properties.Settings.Default.GamePath) is null)
{
InitializeComponent();
AudioList = (List<string>)args["AudioList"];
_ = MessageBox.Show(this, "未设置游戏目录!", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Close(); return;
}
textBox_game.Text = DirectoryH.EnsureExists(Properties.Settings.Default.GamePath).FullName;
Config = new Config(Properties.Settings.Default.GamePath);
textBox_gameVersion.Text = Config.Version;
}

private bool CancellingCompare;

private void Form_Fixer_Load(object sender, EventArgs e)
private void Button_Compare_Click(object sender, EventArgs e)
{
button_cancel.Enabled = !(CancellingCompare = button_compare.Enabled = button_start.Enabled = false);
Compare().GetAwaiter().OnCompleted(() =>
{
Show();
if (StringH.WhiteSpaceCheck(Properties.Settings.Default.GamePath) is null)
{
_ = MessageBox.Show(this, "未设置游戏目录!", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Close(); return;
}
textBox_game.Text = DirectoryH.EnsureExists(Properties.Settings.Default.GamePath).FullName;
Config = new Config(Properties.Settings.Default.GamePath);
textBox_gameVersion.Text = Config.Version;
}
GC.Collect(2, GCCollectionMode.Aggressive, true, true);
GC.WaitForFullGCComplete();
button_cancel.Enabled = !(button_compare.Enabled = button_start.Enabled = true);
});
}

private void Button_Compare_Click(object sender, EventArgs e)
private void Button_Cancel_Click(object sender, EventArgs e)
{
CancellingCompare = true;
}

private async Task Compare()
{
if (Config is null || Config.Channel is null) return;
groupBox_suplus.Text = resourceManager.GetString("groupBox_suplus.Text"); textBox_suplus.Clear();
groupBox_missing.Text = resourceManager.GetString("groupBox_missing.Text"); textBox_missing.Clear();
groupBox_progress.Text = resourceManager.GetString("groupBox_progress.Text") + " (等待服务器响应)";
progressBar.Style = ProgressBarStyle.Marquee;
HttpClient http = new()
{
button_compare.Enabled = button_start.Enabled = false;
Compare().GetAwaiter().OnCompleted(() => button_compare.Enabled = button_start.Enabled = true);
BaseAddress = new Uri(await API.GetDecompressedPath(Config.Channel))
};
Dictionary<string, string> pkg_version = [];
pkg_version["game"] = await http.GetStringAsync($"{http.BaseAddress}/pkg_version");
foreach (string item in AudioList)
{
pkg_version[item] = await http.GetStringAsync($"{http.BaseAddress}/{item}_pkg_version");
}

private async Task Compare()
List<FileInfoH> online = [];
online.AddRange(from KeyValuePair<string, string> p in pkg_version
from string i in p.Value.Split('\n')
where StringH.WhiteSpaceCheck(i) is not null
let j = JsonConvert.DeserializeObject<dynamic>(i)
where j is not null
select new FileInfoH(j));
online.Sort((FileInfoH left, FileInfoH right) =>
{
if (Config is null || Config.Channel is null) return;
progressBar.Style = ProgressBarStyle.Marquee;
HttpClient http = new()
{
BaseAddress = new Uri(await API.GetDecompressedPath(Config.Channel))
};
Dictionary<string, string> pkg_version = [];
pkg_version["game"] = await http.GetStringAsync($"{http.BaseAddress}/pkg_version");
foreach (string item in AudioList)
{
pkg_version[item] = await http.GetStringAsync($"{http.BaseAddress}/{item}_pkg_version");
}
return (int)(left.fileSize - right.fileSize);
});

List<FileInfoH> online = [];
online.AddRange(from KeyValuePair<string, string> p in pkg_version
from string i in p.Value.Split('\n')
where StringH.WhiteSpaceCheck(i) is not null
select new FileInfoH(JsonConvert.DeserializeObject<dynamic>(i)));
online.Sort((FileInfoH left, FileInfoH right) =>
{
return (int)(left.fileSize - right.fileSize);
});
List<FileInfoH> local = [];
List<FileInfo> items = [];
foreach (FileInfo item in DirectoryH.EnsureExists(textBox_game.Text).EnumerateFiles("*", SearchOption.AllDirectories))
{
string remoteName = FileInfoH.GetRemoteName(item.FullName);
if (remoteName.Equals("config.ini")
|| remoteName.EndsWith("pkg_version")
|| remoteName.Contains("/webCaches/")
|| remoteName.Contains("/SDKCaches/")
|| (remoteName.Contains("/Persistent/")
&& !remoteName.Contains("/StreamingAssets/")))
continue;
items.Add(item);
}
items.Sort((FileInfo left, FileInfo right) =>
{
return (int)(left.Length - right.Length);
});

List<FileInfoH> local = [];
List<FileInfo> items = [];
foreach (FileInfo item in DirectoryH.EnsureExists(textBox_game.Text).EnumerateFiles("*", SearchOption.AllDirectories))
progressBar.Value = 0;
progressBar.Maximum = items.Count;
progressBar.Style = ProgressBarStyle.Continuous;
foreach (var item in items)
{
using (FileInfoH fi = new(item.FullName))
{
string remoteName = FileInfoH.GetRemoteName(item.FullName);
if (remoteName.Equals("config.ini")
|| remoteName.EndsWith("pkg_version")
|| remoteName.Contains("/webCaches/")
|| remoteName.Contains("/SDKCaches/")
|| (remoteName.Contains("/Persistent/")
&& !remoteName.Contains("/StreamingAssets/")))
continue;
items.Add(item);
if (radioButton_md5.Checked) await fi.ComputeMD5();
if (radioButton_hash.Checked) await fi.ComputeHash();
if (radioButton_both.Checked) await fi.ComputeAll();
local.Add(fi);
}
items.Sort((FileInfo left, FileInfo right) =>
{
return (int)(left.Length - right.Length);
});
progressBar.Value = local.Count;
groupBox_progress.Text = $"{resourceManager.GetString("groupBox_progress.Text")} ({local.Count} of {items.Count})";
if (CancellingCompare) break;
}
items.Clear();
if (CancellingCompare) return;
List<FileInfoH> surplus = local.Except(online).ToList();
List<FileInfoH> missing = online.Except(local).ToList();
groupBox_suplus.Text = $"{resourceManager.GetString("groupBox_suplus.Text")} ({surplus.Count} of {local.Count})"; local.Clear();
groupBox_missing.Text = $"{resourceManager.GetString("groupBox_missing.Text")} ({missing.Count} of {online.Count})"; online.Clear();
surplus.ForEach((i) => textBox_suplus.Text += $"{i.remoteName}\r\n"); surplus.Clear();
missing.ForEach((i) => textBox_missing.Text += $"{i}\r\n"); missing.Clear();
}

progressBar.Style = ProgressBarStyle.Continuous;
progressBar.Maximum = items.Count;
string text = groupBox_progress.Text;
foreach (var item in items)
{
local.Add(await FileInfoH.BuildAsync(item, radioButton_hash.Checked || radioButton_both.Checked, radioButton_md5.Checked || radioButton_both.Checked));
progressBar.Value = local.Count;
groupBox_progress.Text = $"{text} ({local.Count} of {items.Count})";
}
textBox_suplus.Clear(); local.Except(online).ToList().ForEach((i) => textBox_suplus.Text += $"{i}\r\n");
textBox_missing.Clear(); online.Except(local).ToList().ForEach((i) => textBox_missing.Text += $"{i}\r\n");
private void Button_Start_Click(object sender, EventArgs e)
{
button_compare.Enabled = button_start.Enabled = false;
StartFix().GetAwaiter().OnCompleted(() =>
{
radioButton_none.Checked = true;
Button_Compare_Click(sender, e);
});
}

_ = MessageBox.Show(this, $"比对完成啦!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
groupBox_progress.Text = text;
progressBar.Value = 0;
private async Task StartFix()
{
if (string.IsNullOrWhiteSpace(textBox_suplus.Text) && string.IsNullOrWhiteSpace(textBox_missing.Text))
{
_ = MessageBox.Show(this, "没有需要修复的文件!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return;
}

private void Button_Start_Click(object sender, EventArgs e)
string version = await API.GetLatestVersion(Config?.Channel);
string path_temp = DirectoryH.EnsureNew(Properties.Settings.Default.TempPath).FullName;
if (!string.IsNullOrWhiteSpace(textBox_suplus.Text))
{
button_compare.Enabled = button_start.Enabled = false;
StartFix().GetAwaiter().OnCompleted(() => button_compare.Enabled = button_start.Enabled = true); ;
await File.AppendAllTextAsync($"{path_temp}\\deletefiles.txt", textBox_suplus.Text); textBox_suplus.Clear();
}

private async Task StartFix()
if (!string.IsNullOrWhiteSpace(textBox_missing.Text))
{
string version = await API.GetLatestVersion(Config?.Channel);
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))
await File.AppendAllTextAsync($"{path_temp}\\downloadfiles.txt", textBox_missing.Text);
await Worker.HPatchAsync(this, Config?.Channel);
await Worker.ApplyUpdate(this, version);
Directory.Delete(path_temp, true);
await File.AppendAllTextAsync($"{path_temp}\\downloadfiles.txt", textBox_missing.Text); textBox_missing.Clear();
}
await Worker.HPatchAsync(this, Config?.Channel);
await Worker.ApplyUpdate(this, version);
Directory.Delete(path_temp, true);
}

private void Timer_RAM_Tick(object sender, EventArgs e)
{
Resource.MemoryManager(this, resourceManager);
}

private void Form_Fixer_FormClosing(object sender, FormClosingEventArgs e)
{
CancellingCompare = true;
}

private void Form_Fixer_FormClosed(object sender, FormClosedEventArgs e)
{
GC.Collect(2, GCCollectionMode.Aggressive, true, true);
GC.WaitForFullGCComplete();
}
}
Loading

0 comments on commit 645a231

Please sign in to comment.