worldobject hash

This commit is contained in:
Mister_Nebula 2022-10-08 20:18:22 +01:00
parent 81613b9781
commit f7eb026614
3 changed files with 46 additions and 1 deletions

View File

@ -214,5 +214,18 @@ public static class Extensions
}
}
// Adapted from https://stackoverflow.com/a/30758270
public static int GetSequenceHash(this IEnumerable<string> list)
{
const int seed = 487;
const int modifier = 31;
unchecked
{
return list.Aggregate(seed, (current, item) =>
(current * modifier) + item.GetStableHashCode());
}
}
#endregion
}

View File

@ -0,0 +1,27 @@
using OWML.Common;
using QSB.Messaging;
using QSB.Player.Messages;
using QSB.Utility;
namespace QSB.WorldSync;
internal class HashMessage : QSBMessage<int>
{
public HashMessage(int hash) : base(hash) => To = 0;
public override void OnReceiveRemote()
{
var serverHash = QSBWorldSync.WorldObjectsHash;
if (serverHash != Data)
{
// oh fuck oh no oh god
DebugLog.ToConsole($"Kicking {From} because their WorldObjects hash is wrong. (server:{serverHash}, client:{Data})", MessageType.Error);
new PlayerKickMessage(From, $"WorldObject hash error. (Server:{serverHash}, Client:{Data})").Send();
}
else
{
DebugLog.DebugWrite($"WorldObject hash from {From} verified!", MessageType.Success);
}
}
}

View File

@ -20,6 +20,7 @@ namespace QSB.WorldSync;
public static class QSBWorldSync
{
public static WorldObjectManager[] Managers;
public static int WorldObjectsHash { get; private set; }
/// <summary>
/// Set when all WorldObjectManagers have called Init() on all their objects (AKA all the objects are created)
@ -82,8 +83,12 @@ public static class QSBWorldSync
AllObjectsAdded = true;
DebugLog.DebugWrite("World Objects added.", MessageType.Success);
WorldObjectsHash = WorldObjects.Select(x => x.AttachedObject.GetType().Name).GetSequenceHash();
DebugLog.DebugWrite($"WorldObject hash is {WorldObjectsHash}");
if (!QSBCore.IsHost)
{
new HashMessage(WorldObjectsHash).Send();
new RequestLinksMessage().Send();
}
@ -247,7 +252,7 @@ public static class QSBWorldSync
if (WorldObjects[objectId] is not TWorldObject worldObject)
{
DebugLog.ToConsole($"Error - {typeof(TWorldObject).Name} id {objectId} is actually {WorldObjects[objectId].GetType().Name}.", MessageType.Error);
DebugLog.ToConsole($"Error - WorldObject id {objectId} is {WorldObjects[objectId].GetType().Name}, expected {typeof(TWorldObject).Name}.", MessageType.Error);
return default;
}