use MD5 hash

This commit is contained in:
Mister_Nebula 2022-10-09 13:22:53 +01:00
parent 69a56b9783
commit 7bfbd8b6f1
3 changed files with 16 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
@ -214,17 +215,22 @@ public static class Extensions
} }
} }
// Adapted from https://stackoverflow.com/a/30758270 // https://stackoverflow.com/a/24031467
public static int GetSequenceHash(this IEnumerable<string> list) public static string GetMD5Hash(this IEnumerable<string> list)
{ {
const int seed = 487; using var md5 = System.Security.Cryptography.MD5.Create();
const int modifier = 31;
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) => sb.Append(hashBytes[i].ToString("X2"));
(current * modifier) + item.GetStableHashCode());
} }
return sb.ToString();
} }
#endregion #endregion

View File

@ -20,7 +20,7 @@ namespace QSB.WorldSync;
public static class QSBWorldSync public static class QSBWorldSync
{ {
public static WorldObjectManager[] Managers; public static WorldObjectManager[] Managers;
public static int WorldObjectsHash { get; private set; } public static string WorldObjectsHash { get; private set; }
/// <summary> /// <summary>
/// Set when all WorldObjectManagers have called Init() on all their objects (AKA all the objects are created) /// 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; AllObjectsAdded = true;
DebugLog.DebugWrite("World Objects added.", MessageType.Success); 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}"); DebugLog.DebugWrite($"WorldObject hash is {WorldObjectsHash}");
if (!QSBCore.IsHost) if (!QSBCore.IsHost)

View File

@ -8,7 +8,7 @@ namespace QSB.WorldSync;
/// <summary> /// <summary>
/// sends QSBWorldSync.WorldObjectsHash to the server for sanity checking /// sends QSBWorldSync.WorldObjectsHash to the server for sanity checking
/// </summary> /// </summary>
internal class WorldObjectsHashMessage : QSBMessage<int> internal class WorldObjectsHashMessage : QSBMessage<string>
{ {
public WorldObjectsHashMessage() : base(QSBWorldSync.WorldObjectsHash) => To = 0; public WorldObjectsHashMessage() : base(QSBWorldSync.WorldObjectsHash) => To = 0;