From 312323fb736867c66e3530edcf800ee8a98532b6 Mon Sep 17 00:00:00 2001 From: DragonFire47 <46509577+DragonFire47@users.noreply.github.com> Date: Sat, 2 Dec 2023 12:30:08 -0800 Subject: [PATCH] Changed mod hash creation timing to on mod load --- PulsarModLoader/MPModChecks/MPModCheckManager.cs | 12 +++--------- PulsarModLoader/ModManager.cs | 13 ++++++++++++- PulsarModLoader/PulsarMod.cs | 2 ++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/PulsarModLoader/MPModChecks/MPModCheckManager.cs b/PulsarModLoader/MPModChecks/MPModCheckManager.cs index 2c35254..9ae81f3 100644 --- a/PulsarModLoader/MPModChecks/MPModCheckManager.cs +++ b/PulsarModLoader/MPModChecks/MPModCheckManager.cs @@ -228,24 +228,18 @@ private void UpdateMyModList() stopwatch.Start(); List UnprocessedMods = GetMPModList(); MPModDataBlock[] ProcessedMods = new MPModDataBlock[UnprocessedMods.Count]; - using (SHA256 MyHasher = SHA256.Create()) - { for (int i = 0; i < UnprocessedMods.Count; i++) { PulsarMod currentMod = UnprocessedMods[i]; - using (FileStream MyStream = File.OpenRead(currentMod.VersionInfo.FileName)) - { - MyStream.Position = 0; - byte[] Hash = MyHasher.ComputeHash(MyStream); - ProcessedMods[i] = new MPModDataBlock(currentMod.HarmonyIdentifier(), currentMod.Name, currentMod.Version, (MPRequirement)currentMod.MPRequirements, currentMod.VersionLink, Hash); - } - } + ProcessedMods[i] = new MPModDataBlock(currentMod.HarmonyIdentifier(), currentMod.Name, currentMod.Version, (MPRequirement)currentMod.MPRequirements, currentMod.VersionLink, currentMod.ModHash); } MyModList = ProcessedMods; stopwatch.Stop(); Logger.Info("Finished Building MyModList, time elapsted: " + stopwatch.ElapsedMilliseconds.ToString()); } + + /// /// Serilizes user data into a byte array for network transfer. Does not contain a hash /// diff --git a/PulsarModLoader/ModManager.cs b/PulsarModLoader/ModManager.cs index 88f8cdf..98de630 100644 --- a/PulsarModLoader/ModManager.cs +++ b/PulsarModLoader/ModManager.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.IO.Compression; using PulsarModLoader.MPModChecks; +using System.Security.Cryptography; namespace PulsarModLoader { @@ -337,13 +338,15 @@ public PulsarMod LoadMod(string assemblyPath) try { - Assembly asm = Assembly.Load(File.ReadAllBytes(assemblyPath)); // load as bytes to avoid locking the file + byte[] FileBytes = File.ReadAllBytes(assemblyPath); + Assembly asm = Assembly.Load(FileBytes); // load as bytes to avoid locking the file Type modType = asm.GetTypes().FirstOrDefault(t => t.IsSubclassOf(typeof(PulsarMod))); if (modType != null) { PulsarMod mod = Activator.CreateInstance(modType) as PulsarMod; mod.VersionInfo = FileVersionInfo.GetVersionInfo(assemblyPath); + mod.ModHash = GetHash(FileBytes); activeMods.Add(mod.Name, mod); OnModSuccessfullyLoaded?.Invoke(mod.Name, mod); @@ -369,6 +372,14 @@ public PulsarMod LoadMod(string assemblyPath) } } + public static byte[] GetHash(byte[] DataForHashing) + { + using (SHA256 Hasher = SHA256.Create()) + { + return Hasher.ComputeHash(DataForHashing); + } + } + internal void UnloadMod(PulsarMod mod, ref Harmony harmony) { activeMods.Remove(mod.Name); // Removes selected mod from activeMods diff --git a/PulsarModLoader/PulsarMod.cs b/PulsarModLoader/PulsarMod.cs index 11c0007..8a40082 100644 --- a/PulsarModLoader/PulsarMod.cs +++ b/PulsarModLoader/PulsarMod.cs @@ -13,6 +13,8 @@ public abstract class PulsarMod { internal FileVersionInfo VersionInfo; + internal byte[] ModHash; + /// /// ///