From e80f085091ab2f352fc4c27f1d17f1f7d3125dad Mon Sep 17 00:00:00 2001 From: Scighost Date: Mon, 13 Jan 2025 23:11:46 +0800 Subject: [PATCH] adapt hdiffmap.json --- src/Starward/Services/Download/DiffMap.cs | 46 +++++++++++++++ .../Services/Download/InstallGameService.cs | 57 ++++++++++++++++--- 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/Starward/Services/Download/DiffMap.cs diff --git a/src/Starward/Services/Download/DiffMap.cs b/src/Starward/Services/Download/DiffMap.cs new file mode 100644 index 000000000..c44b9b29c --- /dev/null +++ b/src/Starward/Services/Download/DiffMap.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace Starward.Services.Download; + + +internal class DiffMap +{ + + [JsonPropertyName("diff_map")] + public List DiffMapItems { get; set; } + +} + + +internal class DiffMapItem +{ + + [JsonPropertyName("source_file_name")] + public string SourceFileName { get; set; } + + [JsonPropertyName("source_file_md5")] + public string SourceFileMd5 { get; set; } + + [JsonPropertyName("source_file_size")] + public long SourceFileSize { get; set; } + + [JsonPropertyName("target_file_name")] + public string TargetFileName { get; set; } + + [JsonPropertyName("target_file_md5")] + public string TargetFileMd5 { get; set; } + + [JsonPropertyName("target_file_size")] + public long TargetFileSize { get; set; } + + [JsonPropertyName("patch_file_name")] + public string PatchFileName { get; set; } + + [JsonPropertyName("patch_file_md5")] + public string PatchFileMd5 { get; set; } + + [JsonPropertyName("patch_file_size")] + public long PatchFileSize { get; set; } + +} diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index 187e3a2dd..a169c4164 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -19,6 +19,7 @@ using System.Net.Sockets; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using System.Text.Json.Nodes; using System.Threading; using System.Threading.RateLimiting; @@ -1370,19 +1371,44 @@ await Task.Run(async () => protected async Task ApplyDiffFilesAsync(string installPath) { - var delete = Path.Combine(installPath, "deletefiles.txt"); - if (File.Exists(delete)) + var hdiffmap = Path.Combine(installPath, "hdiffmap.json"); + if (File.Exists(hdiffmap)) { - var deleteFiles = await File.ReadAllLinesAsync(delete).ConfigureAwait(false); - foreach (var file in deleteFiles) + var hpatch = Path.Combine(AppContext.BaseDirectory, "hpatchz.exe"); + var content = await File.ReadAllTextAsync(hdiffmap).ConfigureAwait(false); + var diffmap = JsonSerializer.Deserialize(content); + foreach (var item in diffmap?.DiffMapItems ?? []) { - var target = Path.Combine(installPath, file); - if (File.Exists(target)) + var source = Path.Join(installPath, item.SourceFileName); + var target = Path.Join(installPath, item.TargetFileName); + var diff = Path.Join(installPath, item.PatchFileName); + if (File.Exists(source) && File.Exists(diff)) { - File.Delete(target); + if (File.Exists(target)) + { + File.SetAttributes(target, FileAttributes.Archive); + } + using var process = Process.Start(new ProcessStartInfo + { + FileName = hpatch, + Arguments = $"""-f "{source}" "{diff}" "{target}" """, + CreateNoWindow = true, + }); + if (process != null) + { + await process.WaitForExitAsync().ConfigureAwait(false); + } + if (File.Exists(diff)) + { + File.Delete(diff); + } + if (source != target && File.Exists(source)) + { + File.Delete(source); + } } } - File.Delete(delete); + File.Delete(hdiffmap); } var hdifffiles = Path.Combine(installPath, "hdifffiles.txt"); @@ -1417,6 +1443,21 @@ protected async Task ApplyDiffFilesAsync(string installPath) } File.Delete(hdifffiles); } + + var delete = Path.Combine(installPath, "deletefiles.txt"); + if (File.Exists(delete)) + { + var deleteFiles = await File.ReadAllLinesAsync(delete).ConfigureAwait(false); + foreach (var file in deleteFiles) + { + var target = Path.Combine(installPath, file); + if (File.Exists(target)) + { + File.Delete(target); + } + } + File.Delete(delete); + } }