Skip to content

Commit 74984e0

Browse files
Fixed code diff with matching code bodies
1 parent 04c131f commit 74984e0

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

Source/HedgeModManager.UI/ViewModels/MainWindowViewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,14 @@ public async Task UpdateCodesAsync(bool force, bool append)
633633
await CreateSimpleDownload("Download.Text.DownloadCodes", "Failed to download community codes",
634634
async (d, p, c) =>
635635
{
636-
await gameGeneric.DownloadCodes(null);
636+
var diff = await gameGeneric.UpdateCodes();
637+
var blocks = diff.ToList();
638+
639+
// Print to logger for now
640+
Logger.Debug($"Updated codes with {blocks.Count} blocks:");
641+
foreach (var block in blocks)
642+
Logger.Debug($" {block.Type} {block.Description}");
643+
637644
if (append)
638645
gameGeneric.ModDatabase.LoadSingleCodeFile(mainCodesPath);
639646
else

Source/HedgeModManager/ModdableGameGeneric.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
namespace HedgeModManager;
22
using CoreLib;
33
using Foundation;
4+
using HedgeModManager.CodeCompiler;
5+
using HedgeModManager.Diagnostics;
46
using HedgeModManager.IO;
57
using HedgeModManager.Properties;
8+
using System;
69

710
public class ModdableGameGeneric : IModdableGameTDatabase<ModDatabaseGeneric>, IModdableGameTConfiguration<ModLoaderConfiguration>
811
{
@@ -47,17 +50,32 @@ public ModdableGameGeneric(IGame game)
4750
NativeOS = game.NativeOS;
4851
}
4952

50-
public async Task DownloadCodes(string? url)
53+
public async Task<Diff> UpdateCodes(bool dryRun = false, string? downloadUrl = null)
5154
{
52-
url ??= Resources.CommunityCodesURL;
53-
if (url.EndsWith('/'))
54-
url += $"{Name}.hmm";
55+
downloadUrl ??= Resources.CommunityCodesURL;
56+
if (downloadUrl.EndsWith('/'))
57+
downloadUrl += $"{Name}.hmm?t={DateTime.Now:yyyyMMddHHmmss}";
5558

56-
string contents = await Network.Client.GetStringAsync(url);
57-
string modsRoot = PathEx.GetDirectoryName(ModLoaderConfiguration.DatabasePath).ToString();
59+
string codesPath = Path.Combine(PathEx.GetDirectoryName(ModLoaderConfiguration.DatabasePath).ToString(), ModDatabaseGeneric.MainCodesFileName);
5860

59-
Directory.CreateDirectory(modsRoot);
60-
File.WriteAllText(Path.Combine(modsRoot, ModDatabaseGeneric.MainCodesFileName), contents);
61+
var remoteContents = await Network.Client.GetStringAsync(downloadUrl);
62+
63+
Diff? diff = null;
64+
if (File.Exists(codesPath))
65+
{
66+
var localCodes = CodeFile.FromText(File.ReadAllText(codesPath));
67+
var remoteCodes = CodeFile.FromText(remoteContents);
68+
69+
diff = remoteCodes.CalculateDiff(localCodes);
70+
}
71+
72+
if (!dryRun)
73+
{
74+
Directory.CreateDirectory(PathEx.GetDirectoryName(codesPath).ToString());
75+
File.WriteAllText(codesPath, remoteContents);
76+
}
77+
78+
return diff ?? new Diff();
6179
}
6280

6381
public async Task InitializeAsync()

Source/Libraries/HedgeModManager.CodeCompiler/CodeFile.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ string GetCodeDiffName(CSharpCode code, bool showCodeCategory = false)
6565
{
6666
// Added
6767
if (old.Codes.All(x => x != code))
68-
{
6968
addedCodes.Add(code);
70-
continue;
71-
}
7269
}
7370

7471
foreach (var code in old.Codes)
@@ -99,7 +96,13 @@ void CreateMetadataDiff(CSharpCode compare)
9996

10097
try
10198
{
102-
if (Codes.SingleOrDefault(x => x.Body.Trim() == code.Body.Trim()) is { } renamed)
99+
if (Codes.SingleOrDefault(x => x.Name == code.Name && x.Category == code.Category && x.Body.Trim() == code.Body.Trim()) is { } unchanged)
100+
{
101+
if (addedCodes.Contains(unchanged))
102+
addedCodes.Remove(unchanged);
103+
}
104+
else if (Codes.Count(x => x.Body.Trim() == code.Body.Trim()) == 1 &&
105+
Codes.SingleOrDefault(x => x.Body.Trim() == code.Body.Trim()) is { } renamed)
103106
{
104107
CreateMetadataDiff(renamed);
105108
}

0 commit comments

Comments
 (0)