use hash for addon messages too (save space)

This commit is contained in:
JohnCorby 2023-08-03 14:55:21 -07:00
parent 4e8f1ade73
commit eaa2f4578a
4 changed files with 24 additions and 22 deletions

View File

@ -1,27 +1,28 @@
using System;
using System.Collections.Generic;
using OWML.Common;
using QSB.Utility;
namespace QSB.API;
public static class AddonDataManager
{
private static readonly Dictionary<string, Action<uint, object>> _handlers = new();
private static readonly Dictionary<int, Action<uint, object>> _handlers = new();
public static void OnReceiveDataMessage(string messageType, object data, uint from)
public static void OnReceiveDataMessage(int hash, object data, uint from)
{
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\" from {from}!");
if (!_handlers.TryGetValue(messageType, out var handler))
DebugLog.DebugWrite($"Received addon message of with hash {hash} from {from}!");
if (!_handlers.TryGetValue(hash, out var handler))
{
DebugLog.DebugWrite($"unknown addon message type with hash {hash}", MessageType.Error);
return;
}
handler(from, data);
}
public static void RegisterHandler<T>(string messageType, Action<uint, T> handler)
public static void RegisterHandler<T>(int hash, Action<uint, T> handler)
{
DebugLog.DebugWrite($"Registering handler for \"{messageType}\" with type of {typeof(T).Name}");
_handlers.Add(messageType, (from, data) => handler(from, (T)data));
DebugLog.DebugWrite($"Registering addon message handler for hash {hash} with type {typeof(T).Name}");
_handlers.Add(hash, (from, data) => handler(from, (T)data));
}
}

View File

@ -3,9 +3,9 @@ using QSB.Utility;
namespace QSB.API.Messages;
public class AddonDataMessage : QSBMessage<(string messageType, byte[] data, bool receiveLocally)>
public class AddonDataMessage : QSBMessage<(int hash, byte[] data, bool receiveLocally)>
{
public AddonDataMessage(string messageType, object data, bool receiveLocally) : base((messageType, data.ToBytes(), receiveLocally)) { }
public AddonDataMessage(int hash, object data, bool receiveLocally) : base((hash, data.ToBytes(), receiveLocally)) { }
public override void OnReceiveLocal()
{
@ -18,6 +18,6 @@ public class AddonDataMessage : QSBMessage<(string messageType, byte[] data, boo
public override void OnReceiveRemote()
{
var obj = Data.data.ToObject();
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj, From);
AddonDataManager.OnReceiveDataMessage(Data.hash, obj, From);
}
}

View File

@ -1,4 +1,5 @@
using OWML.Common;
using Mirror;
using OWML.Common;
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
@ -30,10 +31,10 @@ public class QSBAPI : IQSBAPI
public T GetCustomData<T>(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
public void SendMessage<T>(string messageType, T data, uint to = uint.MaxValue, bool receiveLocally = false)
=> new AddonDataMessage(messageType, data, receiveLocally) { To = to }.Send();
=> new AddonDataMessage(messageType.GetStableHashCode(), data, receiveLocally) { To = to }.Send();
public void RegisterHandler<T>(string messageType, Action<uint, T> handler)
=> AddonDataManager.RegisterHandler(messageType, handler);
=> AddonDataManager.RegisterHandler(messageType.GetStableHashCode(), handler);
}
internal static class QSBAPIEvents

View File

@ -19,14 +19,14 @@ public static class QSBMessageManager
{
#region inner workings
internal static readonly Dictionary<ushort, Type> _types = new();
internal static readonly Dictionary<int, Type> _types = new();
static QSBMessageManager()
{
foreach (var type in typeof(QSBMessage).GetDerivedTypes())
{
var id = (ushort)type.FullName.GetStableHashCode();
_types.Add(id, type);
var hash = type.FullName.GetStableHashCode();
_types.Add(hash, type);
// call static constructor of message if needed
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
}
@ -149,10 +149,10 @@ public static class ReaderWriterExtensions
{
private static QSBMessage ReadQSBMessage(this NetworkReader reader)
{
var id = reader.ReadUShort();
if (!QSBMessageManager._types.TryGetValue(id, out var type))
var hash = reader.ReadInt();
if (!QSBMessageManager._types.TryGetValue(hash, out var type))
{
DebugLog.DebugWrite($"unknown QSBMessage type with id {id}", MessageType.Error);
DebugLog.DebugWrite($"unknown QSBMessage type with hash {hash}", MessageType.Error);
return null;
}
var msg = (QSBMessage)FormatterServices.GetUninitializedObject(type);
@ -163,8 +163,8 @@ public static class ReaderWriterExtensions
private static void WriteQSBMessage(this NetworkWriter writer, QSBMessage msg)
{
var type = msg.GetType();
var id = (ushort)type.FullName.GetStableHashCode();
writer.WriteUShort(id);
var hash = type.FullName.GetStableHashCode();
writer.WriteInt(hash);
msg.Serialize(writer);
}
}