diff --git a/SharpEngine.Network/Internal/Common.cs b/SharpEngine.Network/Internal/Common.cs index ed714bc..8dfc5ab 100644 --- a/SharpEngine.Network/Internal/Common.cs +++ b/SharpEngine.Network/Internal/Common.cs @@ -1,11 +1,12 @@ using LiteNetLib; using LiteNetLib.Utils; using Microsoft.CSharp.RuntimeBinder; +using System.Reflection; namespace SharpEngine.Network.Internal; internal static class Common -{ +{ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type type) { var numberProp = reader.GetInt(); @@ -16,7 +17,10 @@ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type t if (packet == null) throw new UnknownPacketException($"Packet : {packetType}"); } - catch (RuntimeBinderException) { } + catch (RuntimeBinderException) + { + // do nothing + } for (var i = 0; i < numberProp; i++) { @@ -24,44 +28,7 @@ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type t var prop = type.GetProperty(propName) ?? throw new UnknownPropertyException($"Property : {propName} - Packet : {packetType}"); object packetObj = packet!; - #region Basic Type - - if (prop.PropertyType == typeof(string)) - prop.SetValue(packetObj, reader.GetString()); - else if (prop.PropertyType == typeof(int)) - prop.SetValue(packetObj, reader.GetInt()); - else if (prop.PropertyType == typeof(short)) - prop.SetValue(packetObj, reader.GetShort()); - else if (prop.PropertyType == typeof(byte)) - prop.SetValue(packetObj, reader.GetByte()); - else if (prop.PropertyType == typeof(char)) - prop.SetValue(packetObj, reader.GetChar()); - else if (prop.PropertyType == typeof(float)) - prop.SetValue(packetObj, reader.GetFloat()); - else if (prop.PropertyType == typeof(double)) - prop.SetValue(packetObj, reader.GetDouble()); - else if (prop.PropertyType == typeof(bool)) - prop.SetValue(packetObj, reader.GetBool()); - #endregion - - #region Array Type - - else if (prop.PropertyType == typeof(string[])) - prop.SetValue(packetObj, reader.GetStringArray()); - else if (prop.PropertyType == typeof(int[])) - prop.SetValue(packetObj, reader.GetIntArray()); - else if (prop.PropertyType == typeof(short[])) - prop.SetValue(packetObj, reader.GetShortArray()); - else if (prop.PropertyType == typeof(float[])) - prop.SetValue(packetObj, reader.GetFloatArray()); - else if (prop.PropertyType == typeof(double[])) - prop.SetValue(packetObj, reader.GetDoubleArray()); - else if (prop.PropertyType == typeof(bool[])) - prop.SetValue(packetObj, reader.GetBoolArray()); - #endregion - - else - throw new UnknownPropertyTypeException($"Type : {prop.PropertyType.Name}"); + SetPropertyValue(packetObj, prop, reader); packet = packetObj; } @@ -72,44 +39,7 @@ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type t var field = type.GetField(fieldName) ?? throw new UnknownFieldException($"Field : {fieldName} - Packet : {packetType}"); object packetObj = packet!; - #region Basic Type - - if (field.FieldType == typeof(string)) - field.SetValue(packetObj, reader.GetString()); - else if (field.FieldType == typeof(int)) - field.SetValue(packetObj, reader.GetInt()); - else if (field.FieldType == typeof(short)) - field.SetValue(packetObj, reader.GetShort()); - else if (field.FieldType == typeof(byte)) - field.SetValue(packetObj, reader.GetByte()); - else if (field.FieldType == typeof(char)) - field.SetValue(packetObj, reader.GetChar()); - else if (field.FieldType == typeof(float)) - field.SetValue(packetObj, reader.GetFloat()); - else if (field.FieldType == typeof(double)) - field.SetValue(packetObj, reader.GetDouble()); - else if (field.FieldType == typeof(bool)) - field.SetValue(packetObj, reader.GetBool()); - #endregion - - #region Array Type - - else if (field.FieldType == typeof(string[])) - field.SetValue(packetObj, reader.GetStringArray()); - else if (field.FieldType == typeof(int[])) - field.SetValue(packetObj, reader.GetIntArray()); - else if (field.FieldType == typeof(short[])) - field.SetValue(packetObj, reader.GetShortArray()); - else if (field.FieldType == typeof(float[])) - field.SetValue(packetObj, reader.GetFloatArray()); - else if (field.FieldType == typeof(double[])) - field.SetValue(packetObj, reader.GetDoubleArray()); - else if (field.FieldType == typeof(bool[])) - field.SetValue(packetObj, reader.GetBoolArray()); - #endregion - - else - throw new UnknownFieldTypeException($"Type : {field.FieldType.Name}"); + SetFieldValue(packetObj, field, reader); packet = packetObj; } @@ -137,44 +67,8 @@ public static void SendPacket(NetPeer peer, T packet) writer.Put(prop.Name); - #region Basic Type - - if (prop.PropertyType == typeof(string)) - writer.Put((string)value); - else if (prop.PropertyType == typeof(int)) - writer.Put((int)value); - else if (prop.PropertyType == typeof(short)) - writer.Put((short)value); - else if (prop.PropertyType == typeof(byte)) - writer.Put((byte)value); - else if (prop.PropertyType == typeof(char)) - writer.Put((char)value); - else if (prop.PropertyType == typeof(float)) - writer.Put((float)value); - else if (prop.PropertyType == typeof(double)) - writer.Put((double)value); - else if (prop.PropertyType == typeof(bool)) - writer.Put((bool)value); - #endregion - - #region Array Type - - else if (prop.PropertyType == typeof(string[])) - writer.PutArray((string[])value); - else if (prop.PropertyType == typeof(int[])) - writer.PutArray((int[])value); - else if (prop.PropertyType == typeof(short[])) - writer.PutArray((short[])value); - else if (prop.PropertyType == typeof(float[])) - writer.PutArray((float[])value); - else if (prop.PropertyType == typeof(double[])) - writer.PutArray((double[])value); - else if (prop.PropertyType == typeof(bool[])) - writer.PutArray((bool[])value); - #endregion - - else - throw new UnknownPropertyTypeException($"Type : {prop.PropertyType.Name}"); + PutPropertyValue(value, prop, writer); + } foreach (var field in fields) { @@ -184,45 +78,144 @@ public static void SendPacket(NetPeer peer, T packet) writer.Put(field.Name); - #region Basic Type - - if (field.FieldType == typeof(string)) - writer.Put((string)value); - else if (field.FieldType == typeof(int)) - writer.Put((int)value); - else if (field.FieldType == typeof(short)) - writer.Put((short)value); - else if (field.FieldType == typeof(byte)) - writer.Put((byte)value); - else if (field.FieldType == typeof(char)) - writer.Put((char)value); - else if (field.FieldType == typeof(float)) - writer.Put((float)value); - else if (field.FieldType == typeof(double)) - writer.Put((double)value); - else if (field.FieldType == typeof(bool)) - writer.Put((bool)value); - #endregion - - #region Array Type - - else if (field.FieldType == typeof(string[])) - writer.PutArray((string[])value); - else if (field.FieldType == typeof(int[])) - writer.PutArray((int[])value); - else if (field.FieldType == typeof(short[])) - writer.PutArray((short[])value); - else if (field.FieldType == typeof(float[])) - writer.PutArray((float[])value); - else if (field.FieldType == typeof(double[])) - writer.PutArray((double[])value); - else if (field.FieldType == typeof(bool[])) - writer.PutArray((bool[])value); - #endregion - - else - throw new UnknownFieldTypeException($"Type : {field.FieldType.Name}"); + PutFieldValue(value, field, writer); } peer.Send(writer, DeliveryMethod.ReliableOrdered); } + + private static void PutPropertyValue(object value, PropertyInfo prop, NetDataWriter writer) + { + if (prop.PropertyType == typeof(string)) + writer.Put((string)value); + else if (prop.PropertyType == typeof(int)) + writer.Put((int)value); + else if (prop.PropertyType == typeof(short)) + writer.Put((short)value); + else if (prop.PropertyType == typeof(byte)) + writer.Put((byte)value); + else if (prop.PropertyType == typeof(char)) + writer.Put((char)value); + else if (prop.PropertyType == typeof(float)) + writer.Put((float)value); + else if (prop.PropertyType == typeof(double)) + writer.Put((double)value); + else if (prop.PropertyType == typeof(bool)) + writer.Put((bool)value); + else if (prop.PropertyType == typeof(string[])) + writer.PutArray((string[])value); + else if (prop.PropertyType == typeof(int[])) + writer.PutArray((int[])value); + else if (prop.PropertyType == typeof(short[])) + writer.PutArray((short[])value); + else if (prop.PropertyType == typeof(float[])) + writer.PutArray((float[])value); + else if (prop.PropertyType == typeof(double[])) + writer.PutArray((double[])value); + else if (prop.PropertyType == typeof(bool[])) + writer.PutArray((bool[])value); + else + throw new UnknownPropertyTypeException($"Type : {prop.PropertyType.Name}"); + } + + private static void PutFieldValue(object value, FieldInfo field, NetDataWriter writer) + { + if (field.FieldType == typeof(string)) + writer.Put((string)value); + else if (field.FieldType == typeof(int)) + writer.Put((int)value); + else if (field.FieldType == typeof(short)) + writer.Put((short)value); + else if (field.FieldType == typeof(byte)) + writer.Put((byte)value); + else if (field.FieldType == typeof(char)) + writer.Put((char)value); + else if (field.FieldType == typeof(float)) + writer.Put((float)value); + else if (field.FieldType == typeof(double)) + writer.Put((double)value); + else if (field.FieldType == typeof(bool)) + writer.Put((bool)value); + else if (field.FieldType == typeof(string[])) + writer.PutArray((string[])value); + else if (field.FieldType == typeof(int[])) + writer.PutArray((int[])value); + else if (field.FieldType == typeof(short[])) + writer.PutArray((short[])value); + else if (field.FieldType == typeof(float[])) + writer.PutArray((float[])value); + else if (field.FieldType == typeof(double[])) + writer.PutArray((double[])value); + else if (field.FieldType == typeof(bool[])) + writer.PutArray((bool[])value); + else + throw new UnknownFieldTypeException($"Type : {field.FieldType.Name}"); + } + + private static void SetPropertyValue(object packetObj, PropertyInfo prop, NetDataReader reader) + { + if (prop.PropertyType == typeof(string)) + prop.SetValue(packetObj, reader.GetString()); + else if (prop.PropertyType == typeof(int)) + prop.SetValue(packetObj, reader.GetInt()); + else if (prop.PropertyType == typeof(short)) + prop.SetValue(packetObj, reader.GetShort()); + else if (prop.PropertyType == typeof(byte)) + prop.SetValue(packetObj, reader.GetByte()); + else if (prop.PropertyType == typeof(char)) + prop.SetValue(packetObj, reader.GetChar()); + else if (prop.PropertyType == typeof(float)) + prop.SetValue(packetObj, reader.GetFloat()); + else if (prop.PropertyType == typeof(double)) + prop.SetValue(packetObj, reader.GetDouble()); + else if (prop.PropertyType == typeof(bool)) + prop.SetValue(packetObj, reader.GetBool()); + else if (prop.PropertyType == typeof(string[])) + prop.SetValue(packetObj, reader.GetStringArray()); + else if (prop.PropertyType == typeof(int[])) + prop.SetValue(packetObj, reader.GetIntArray()); + else if (prop.PropertyType == typeof(short[])) + prop.SetValue(packetObj, reader.GetShortArray()); + else if (prop.PropertyType == typeof(float[])) + prop.SetValue(packetObj, reader.GetFloatArray()); + else if (prop.PropertyType == typeof(double[])) + prop.SetValue(packetObj, reader.GetDoubleArray()); + else if (prop.PropertyType == typeof(bool[])) + prop.SetValue(packetObj, reader.GetBoolArray()); + else + throw new UnknownPropertyTypeException($"Type : {prop.PropertyType.Name}"); + } + + private static void SetFieldValue(object packetObj, FieldInfo field, NetDataReader reader) + { + if (field.FieldType == typeof(string)) + field.SetValue(packetObj, reader.GetString()); + else if (field.FieldType == typeof(int)) + field.SetValue(packetObj, reader.GetInt()); + else if (field.FieldType == typeof(short)) + field.SetValue(packetObj, reader.GetShort()); + else if (field.FieldType == typeof(byte)) + field.SetValue(packetObj, reader.GetByte()); + else if (field.FieldType == typeof(char)) + field.SetValue(packetObj, reader.GetChar()); + else if (field.FieldType == typeof(float)) + field.SetValue(packetObj, reader.GetFloat()); + else if (field.FieldType == typeof(double)) + field.SetValue(packetObj, reader.GetDouble()); + else if (field.FieldType == typeof(bool)) + field.SetValue(packetObj, reader.GetBool()); + else if (field.FieldType == typeof(string[])) + field.SetValue(packetObj, reader.GetStringArray()); + else if (field.FieldType == typeof(int[])) + field.SetValue(packetObj, reader.GetIntArray()); + else if (field.FieldType == typeof(short[])) + field.SetValue(packetObj, reader.GetShortArray()); + else if (field.FieldType == typeof(float[])) + field.SetValue(packetObj, reader.GetFloatArray()); + else if (field.FieldType == typeof(double[])) + field.SetValue(packetObj, reader.GetDoubleArray()); + else if (field.FieldType == typeof(bool[])) + field.SetValue(packetObj, reader.GetBoolArray()); + else + throw new UnknownFieldTypeException($"Type : {field.FieldType.Name}"); + } }