Seperate out the NetworkPacketDictonary

This commit is contained in:
derek.holloway committed 2026-07-08 07:24:05 -07:00
1 parent 531fd2572f
commit 3992c3debc
6 files changed
+48 -46

No files matched your search

+13 -26
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using MessagePack;
using Mistox.Attributes;
@@ -43,42 +42,30 @@ namespace Mistox {
}
public static object Deserialize( Type PacketType, ushort DataLength, ReadOnlyMemory<byte> Data ) {
public static object Deserialize( byte PacketByte, ushort DataLength, ReadOnlyMemory<byte> Data ) {
// Get the type from the tag
if (!NetworkPacketDictionary.ServerToClientPackets.TryGetValue(PacketByte, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {PacketByte}");
}
// Sanity check: Ensure the packet we received actually has the payload bytes it claims to have
if ( DataLength <= Data.Length ) {
if ( DataLength < Data.Length ) {
throw new Exception("Malformed packet: Payload size mismatch.");
}
// 4. Create the low-level reader struct directly over the memory slice
var reader = new MessagePackReader(Data);
// Make sure the data is exactly the size requested
ReadOnlyMemory<byte> payloadSlice = Data.Slice(0, DataLength);
// Create the low-level reader struct directly over the memory slice
var reader = new MessagePackReader(payloadSlice);
// Extract the object
dynamic obj = MessagePackSerializer.Deserialize( PacketType, ref reader, MessagePackSerializerOptions.Standard );
dynamic obj = MessagePackSerializer.Deserialize( resolvedType, ref reader, MessagePackSerializerOptions.Standard );
return obj;
}
// Fills out the NetworkPacket Lookup Dictionary
public static readonly Dictionary<byte, Type> ClientToClientPackets = new();
public static readonly Dictionary<byte, Type> ServerToClientPackets = new();
public static void BuildNetworkPacketList() {
Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes();
foreach( Type cur in allClasses) {
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
if (attribute != null) {
if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) {
ClientToClientPackets[attribute.ID] = cur;
} else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) {
ServerToClientPackets[attribute.ID] = cur;
}
}
}
}
}
}
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Mistox.Attributes {
public class NetworkPacketDictionary {
// Fills out the NetworkPacket Lookup Dictionary
public static readonly Dictionary<byte, Type> ClientToClientPackets = new();
public static readonly Dictionary<byte, Type> ServerToClientPackets = new();
public static void BuildNetworkPacketList() {
Type[] allClasses = Assembly.GetExecutingAssembly().GetTypes();
foreach( Type cur in allClasses) {
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
if (attribute != null) {
if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToClient) ) {
ClientToClientPackets[attribute.ID] = cur;
} else if ( attribute.PacketUse.HasFlag(PacketUsage.ClientToServer) ) {
ServerToClientPackets[attribute.ID] = cur;
}
}
}
}
}
}