mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-01 12:08:52 +00:00
worldobject hash
This commit is contained in:
parent
81613b9781
commit
f7eb026614
@ -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
|
||||
}
|
27
QSB/WorldSync/HashMessage.cs
Normal file
27
QSB/WorldSync/HashMessage.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user