mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-17 01:13:05 +00:00
use MD5 hash
This commit is contained in:
parent
69a56b9783
commit
7bfbd8b6f1
@ -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<string> list)
|
||||
// https://stackoverflow.com/a/24031467
|
||||
public static string GetMD5Hash(this IEnumerable<string> 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
|
||||
|
@ -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; }
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
|
@ -8,7 +8,7 @@ namespace QSB.WorldSync;
|
||||
/// <summary>
|
||||
/// sends QSBWorldSync.WorldObjectsHash to the server for sanity checking
|
||||
/// </summary>
|
||||
internal class WorldObjectsHashMessage : QSBMessage<int>
|
||||
internal class WorldObjectsHashMessage : QSBMessage<string>
|
||||
{
|
||||
public WorldObjectsHashMessage() : base(QSBWorldSync.WorldObjectsHash) => To = 0;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user