Change manual packet numbering to a dictionary

This commit is contained in:
derek.holloway committed 2026-08-03 10:57:17 -07:00
1 parent 37a6b5d59b
commit b83887cb2a
8 files changed
+53 -44

No files matched your search

+3 -9
View File
@@ -10,7 +10,7 @@ namespace Mistox {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DataPacketHeader {
public byte Type { get; set; }
public ushort Type { get; set; }
public ushort DataLength { get; set; }
}
@@ -24,12 +24,6 @@ namespace Mistox {
// Helper classes
public static byte[] Serialize<T>( T Object ) {
// Get the attribute ID
mPacket attribute = typeof(T).GetCustomAttribute<mPacket>(false);
if (attribute == null) {
throw new Exception($"The datastructure {typeof(T).Name} is not an mPacket");
}
// Use an expandable buffer writer from the system stack
var writer = new ArrayBufferWriter<byte>(256);
@@ -42,7 +36,7 @@ namespace Mistox {
// Build our header struct on the stack
DataPacketHeader header = new DataPacketHeader {
Type = attribute.ID,
Type = NetworkPacketDictionary.PacketIDTable[typeof(T)],
DataLength = (ushort)(writer.WrittenCount - HeaderLength)
};
@@ -59,7 +53,7 @@ namespace Mistox {
public static object Deserialize( DataPacketHeader Header, ReadOnlyMemory<byte> Data ) {
// Get the type from the tag
if (!NetworkPacketDictionary.ServerToClientPackets.TryGetValue(Header.Type, out Type resolvedType)) {
if (!NetworkPacketDictionary.PacketLookupTable.TryGetValue(Header.Type, out Type resolvedType)) {
throw new Exception($"Unknown packet type ID: {Header.Type}");
}