Skip to content

Commit

Permalink
adapt hdiffmap.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 13, 2025
1 parent 1efc543 commit e80f085
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
46 changes: 46 additions & 0 deletions src/Starward/Services/Download/DiffMap.cs
Original file line number Diff line number Diff line change
@@ -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<DiffMapItem> 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; }

}
57 changes: 49 additions & 8 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DiffMap>(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");
Expand Down Expand Up @@ -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);
}
}


Expand Down

0 comments on commit e80f085

Please sign in to comment.