Skip to content

Commit 1a348c2

Browse files
committed
Add SHA256 of mod DLLs to log - implements #38
1 parent d4f02ec commit 1a348c2

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

MonkeyLoader.GamePacks.ResoniteModLoader/ModLoaderHook.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using NuGet.Packaging.Core;
1212
using NuGet.Versioning;
1313
using System.Reflection;
14+
using System.Security.Cryptography;
1415
using System.Text.Json;
1516

1617
namespace ResoniteModLoader
@@ -110,6 +111,21 @@ protected override bool OnEngineInit()
110111
/// <inheritdoc/>
111112
protected override bool OnLoaded() => base.OnEngineReady();
112113

114+
private static string GenerateSHA256(string filepath)
115+
{
116+
try
117+
{
118+
using var hasher = SHA256.Create();
119+
using var stream = File.OpenRead(filepath);
120+
121+
return Convert.ToHexString(hasher.ComputeHash(stream));
122+
}
123+
catch
124+
{
125+
return "Failed to generate hash";
126+
}
127+
}
128+
113129
private static IEnumerable<string> GetAssemblyPaths(string root)
114130
{
115131
if (!Directory.Exists(root))
@@ -167,22 +183,24 @@ private static async Task InitializeFrooxEnginePostfixAsync(Task __result)
167183

168184
private static async IAsyncEnumerable<RmlMod> LoadModsAsync()
169185
{
170-
var modAssemblies = new List<Assembly>();
186+
var modAssemblies = new List<(Assembly, string)>();
171187

172188
foreach (var file in GetAssemblyPaths("rml_mods"))
173189
{
190+
var hash = GenerateSHA256(file);
191+
174192
try
175193
{
176194
var modAssembly = await Task.Run(() => Mod.Loader.AssemblyLoadStrategy.LoadFile(Path.GetFullPath(file!)));
177-
modAssemblies.Add(modAssembly);
195+
modAssemblies.Add((modAssembly, hash));
178196
}
179197
catch (Exception ex)
180198
{
181-
Logger.Warn(() => ex.Format($"Failed to load assembly from rml_mods: {file}"));
199+
Logger.Warn(() => ex.Format($"Failed to load assembly from rml_mods: {file} with SHA256: {hash}"));
182200
}
183201
}
184202

185-
foreach (var modAssembly in modAssemblies)
203+
foreach (var (modAssembly, hash) in modAssemblies)
186204
{
187205
var fileName = Path.GetFileName(modAssembly.Location);
188206
LoadProgressReporter.SetSubphase($"{Environment.NewLine}  {modAssembly.GetName().Name!}");
@@ -193,7 +211,7 @@ private static async IAsyncEnumerable<RmlMod> LoadModsAsync()
193211
try
194212
{
195213
rmlMod = new RmlMod(Mod.Loader, modAssembly);
196-
Logger.Info(() => $"Loaded mod from rml_mods: {fileName}");
214+
Logger.Info(() => $"Loaded mod [{rmlMod.Id}/{rmlMod.Version}] ({fileName}) by {rmlMod.Authors.Join()} with SHA256: {hash}");
197215

198216
Mod.Loader.AddMod(rmlMod);
199217
}

0 commit comments

Comments
 (0)