diff --git a/QSB/Utility/Extensions.cs b/QSB/Utility/Extensions.cs index 473f1d2d..eede0565 100644 --- a/QSB/Utility/Extensions.cs +++ b/QSB/Utility/Extensions.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Text; using UnityEngine; using Object = UnityEngine.Object; @@ -214,17 +215,22 @@ public static class Extensions } } - // Adapted from https://stackoverflow.com/a/30758270 - public static int GetSequenceHash(this IEnumerable list) + // https://stackoverflow.com/a/24031467 + public static string GetMD5Hash(this IEnumerable list) { - const int seed = 487; - const int modifier = 31; + using var md5 = System.Security.Cryptography.MD5.Create(); - unchecked + var longString = string.Concat(list); + var bytes = Encoding.ASCII.GetBytes(longString); + var hashBytes = md5.ComputeHash(bytes); + + var sb = new StringBuilder(); + for (var i = 0; i < hashBytes.Length; i++) { - return list.Aggregate(seed, (current, item) => - (current * modifier) + item.GetStableHashCode()); + sb.Append(hashBytes[i].ToString("X2")); } + + return sb.ToString(); } #endregion diff --git a/QSB/WorldSync/QSBWorldSync.cs b/QSB/WorldSync/QSBWorldSync.cs index 0426a67a..198bfbca 100644 --- a/QSB/WorldSync/QSBWorldSync.cs +++ b/QSB/WorldSync/QSBWorldSync.cs @@ -20,7 +20,7 @@ namespace QSB.WorldSync; public static class QSBWorldSync { public static WorldObjectManager[] Managers; - public static int WorldObjectsHash { get; private set; } + public static string WorldObjectsHash { get; private set; } /// /// Set when all WorldObjectManagers have called Init() on all their objects (AKA all the objects are created) @@ -83,7 +83,7 @@ public static class QSBWorldSync AllObjectsAdded = true; DebugLog.DebugWrite("World Objects added.", MessageType.Success); - WorldObjectsHash = WorldObjects.Select(x => x.GetType().Name).GetSequenceHash(); + WorldObjectsHash = WorldObjects.Select(x => x.GetType().Name).GetMD5Hash(); DebugLog.DebugWrite($"WorldObject hash is {WorldObjectsHash}"); if (!QSBCore.IsHost) diff --git a/QSB/WorldSync/WorldObjectsHashMessage.cs b/QSB/WorldSync/WorldObjectsHashMessage.cs index b46494ea..4d5e6bd2 100644 --- a/QSB/WorldSync/WorldObjectsHashMessage.cs +++ b/QSB/WorldSync/WorldObjectsHashMessage.cs @@ -8,7 +8,7 @@ namespace QSB.WorldSync; /// /// sends QSBWorldSync.WorldObjectsHash to the server for sanity checking /// -internal class WorldObjectsHashMessage : QSBMessage +internal class WorldObjectsHashMessage : QSBMessage { public WorldObjectsHashMessage() : base(QSBWorldSync.WorldObjectsHash) => To = 0;