diff --git a/BoneSync/MelonLoaderMod.cs b/BoneSync/MelonLoaderMod.cs index 1547f0e..daf1301 100644 --- a/BoneSync/MelonLoaderMod.cs +++ b/BoneSync/MelonLoaderMod.cs @@ -31,7 +31,7 @@ namespace BoneSync public static LobbyManager lobby; - public static TransportBase transport; + public static ITransportBase transport; public override void OnApplicationStart() { SteamClient.Init(823500, true); diff --git a/BoneSync/Networking/NetworkMessage.cs b/BoneSync/Networking/NetworkMessage.cs index 2e0473e..48d59ea 100644 --- a/BoneSync/Networking/NetworkMessage.cs +++ b/BoneSync/Networking/NetworkMessage.cs @@ -1,14 +1,15 @@ -using BoneSync.Data; -using BoneSync.Networking.Transport; -using BoneSync.Sync; -using MelonLoader; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using BoneSync.Data; +using BoneSync.Networking.Transport; +using BoneSync.Sync; +using MelonLoader; +using Oculus.Platform; #if TEST using Xunit; #endif @@ -137,19 +138,34 @@ namespace BoneSync.Networking } } } + + private static readonly Dictionary ConstructorCache = new Dictionary(); + public static ConstructorInfo GetConstructor(Packet packet) + { + if (ConstructorCache.ContainsKey(packet.Info.packetType)) + { + return ConstructorCache[packet.Info.packetType]; + } + if (!PacketTypeMap.ContainsKey(packet.Info.packetType)) + { + throw new Exception("No class found for packet type '" + packet.Info.packetType + "'"); + } + + Type type = PacketTypeMap[packet.Info.packetType]; + // get the constructor that takes a Packet + ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(Packet) }) ?? throw new Exception("No constructor found for type " + type.Name); + ConstructorCache[packet.Info.packetType] = constructor; + return constructor; + } + + public static NetworkMessage ParsePacket(Packet packet) { //SyncLogger.Msg("Received packet of type " + packet.Info.packetType + " from " + packet.Info.senderId + " Length: " + packet.Data.Length); // find a class that can parse this packet using Reflection // and return it - if (!PacketTypeMap.ContainsKey(packet.Info.packetType)) - { - throw new Exception("No class found for packet type '" + packet.Info.packetType+"'"); - } - Type type = PacketTypeMap[packet.Info.packetType]; - // get the constructor that takes a Packet - ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(Packet) }) ?? throw new Exception("No constructor found for type " + type.Name); + ConstructorInfo constructor = GetConstructor(packet); NetworkMessage networkMessage = (NetworkMessage)constructor.Invoke(new object[] { packet }); networkMessage.senderId = packet.Info.senderId; return networkMessage; diff --git a/BoneSync/Networking/Transport/SteamTransport.cs b/BoneSync/Networking/Transport/SteamTransport.cs index b3e76a2..f1b11ba 100644 --- a/BoneSync/Networking/Transport/SteamTransport.cs +++ b/BoneSync/Networking/Transport/SteamTransport.cs @@ -13,7 +13,7 @@ using Oculus.Platform; namespace BoneSync.Networking.Transport { - internal class SteamTransport : TransportBase + internal class SteamTransport : ITransportBase { public SteamTransport() diff --git a/BoneSync/Networking/Transport/TransportBase.cs b/BoneSync/Networking/Transport/TransportBase.cs index 4238098..275245c 100644 --- a/BoneSync/Networking/Transport/TransportBase.cs +++ b/BoneSync/Networking/Transport/TransportBase.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace BoneSync.Networking.Transport { - public interface TransportBase + public interface ITransportBase { ulong BROADCAST_ID { get; } void Send(Packet packet);