Skip to content

Commit

Permalink
Changed mod hash creation timing to on mod load
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonFire47 committed Dec 2, 2023
1 parent f0bf196 commit 312323f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
12 changes: 3 additions & 9 deletions PulsarModLoader/MPModChecks/MPModCheckManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,18 @@ private void UpdateMyModList()
stopwatch.Start();
List<PulsarMod> 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());
}



/// <summary>
/// Serilizes user data into a byte array for network transfer. Does not contain a hash
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.IO.Compression;
using PulsarModLoader.MPModChecks;
using System.Security.Cryptography;

namespace PulsarModLoader
{
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions PulsarModLoader/PulsarMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public abstract class PulsarMod
{
internal FileVersionInfo VersionInfo;

internal byte[] ModHash;

/// <summary>
///
/// </summary>
Expand Down

0 comments on commit 312323f

Please sign in to comment.