From 390e6b5a005e9d44f94c43000e5153bf2fca97d3 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 3 Aug 2023 14:40:47 -0700 Subject: [PATCH] use GetStableHashCode for message --- QSB/Messaging/QSBMessageManager.cs | 35 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/QSB/Messaging/QSBMessageManager.cs b/QSB/Messaging/QSBMessageManager.cs index e8152508..8c0e8ad8 100644 --- a/QSB/Messaging/QSBMessageManager.cs +++ b/QSB/Messaging/QSBMessageManager.cs @@ -10,7 +10,6 @@ using QSB.Utility; using QSB.WorldSync; using System; using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.Serialization; @@ -20,17 +19,16 @@ public static class QSBMessageManager { #region inner workings - internal static readonly Type[] _types; - internal static readonly Dictionary _typeToId = new(); + internal static readonly Dictionary _types = new(); static QSBMessageManager() { - _types = typeof(QSBMessage).GetDerivedTypes().ToArray(); - for (ushort i = 0; i < _types.Length; i++) + foreach (var type in typeof(QSBMessage).GetDerivedTypes()) { - _typeToId.Add(_types[i], i); + var id = (ushort)type.FullName.GetStableHashCode(); + _types.Add(id, type); // call static constructor of message if needed - RuntimeHelpers.RunClassConstructor(_types[i].TypeHandle); + RuntimeHelpers.RunClassConstructor(type.TypeHandle); } } @@ -42,6 +40,11 @@ public static class QSBMessageManager private static void OnServerReceive(QSBMessage msg) { + if (msg == null) + { + return; + } + if (msg.To == uint.MaxValue) { NetworkServer.SendToAll(msg); @@ -77,9 +80,9 @@ public static class QSBMessageManager var player = QSBPlayerManager.GetPlayer(msg.From); if (!player.IsReady - && player.PlayerId != QSBPlayerManager.LocalPlayerId - && player.State is ClientState.AliveInSolarSystem or ClientState.AliveInEye or ClientState.DeadInSolarSystem - && msg is not (PlayerInformationMessage or PlayerReadyMessage or RequestStateResyncMessage or ServerStateMessage)) + && player.PlayerId != QSBPlayerManager.LocalPlayerId + && player.State is ClientState.AliveInSolarSystem or ClientState.AliveInEye or ClientState.DeadInSolarSystem + && msg is not (PlayerInformationMessage or PlayerReadyMessage or RequestStateResyncMessage or ServerStateMessage)) { //DebugLog.ToConsole($"Warning - Got message {msg} from player {msg.From}, but they were not ready. Asking for state resync, just in case.", MessageType.Warning); new RequestStateResyncMessage().Send(); @@ -147,7 +150,11 @@ public static class ReaderWriterExtensions private static QSBMessage ReadQSBMessage(this NetworkReader reader) { var id = reader.ReadUShort(); - var type = QSBMessageManager._types[id]; + if (!QSBMessageManager._types.TryGetValue(id, out var type)) + { + DebugLog.DebugWrite($"unknown QSBMessage type with id {id}", MessageType.Error); + return null; + } var msg = (QSBMessage)FormatterServices.GetUninitializedObject(type); msg.Deserialize(reader); return msg; @@ -156,8 +163,8 @@ public static class ReaderWriterExtensions private static void WriteQSBMessage(this NetworkWriter writer, QSBMessage msg) { var type = msg.GetType(); - var id = QSBMessageManager._typeToId[type]; - writer.Write(id); + var id = (ushort)type.FullName.GetStableHashCode(); + writer.WriteUShort(id); msg.Serialize(writer); } -} \ No newline at end of file +}