11using LiteNetLib ;
22using LiteNetLib . Utils ;
33using Microsoft . CSharp . RuntimeBinder ;
4+ using System . Reflection ;
45
56namespace SharpEngine . Network . Internal ;
67
78internal static class Common
8- {
9+ {
910 public static dynamic ReadPacket ( NetDataReader reader , string packetType , Type type )
1011 {
1112 var numberProp = reader . GetInt ( ) ;
@@ -16,52 +17,18 @@ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type t
1617 if ( packet == null )
1718 throw new UnknownPacketException ( $ "Packet : { packetType } ") ;
1819 }
19- catch ( RuntimeBinderException ) { }
20+ catch ( RuntimeBinderException )
21+ {
22+ // do nothing
23+ }
2024
2125 for ( var i = 0 ; i < numberProp ; i ++ )
2226 {
2327 var propName = reader . GetString ( ) ;
2428 var prop = type . GetProperty ( propName ) ?? throw new UnknownPropertyException ( $ "Property : { propName } - Packet : { packetType } ") ;
2529 object packetObj = packet ! ;
2630
27- #region Basic Type
28-
29- if ( prop . PropertyType == typeof ( string ) )
30- prop . SetValue ( packetObj , reader . GetString ( ) ) ;
31- else if ( prop . PropertyType == typeof ( int ) )
32- prop . SetValue ( packetObj , reader . GetInt ( ) ) ;
33- else if ( prop . PropertyType == typeof ( short ) )
34- prop . SetValue ( packetObj , reader . GetShort ( ) ) ;
35- else if ( prop . PropertyType == typeof ( byte ) )
36- prop . SetValue ( packetObj , reader . GetByte ( ) ) ;
37- else if ( prop . PropertyType == typeof ( char ) )
38- prop . SetValue ( packetObj , reader . GetChar ( ) ) ;
39- else if ( prop . PropertyType == typeof ( float ) )
40- prop . SetValue ( packetObj , reader . GetFloat ( ) ) ;
41- else if ( prop . PropertyType == typeof ( double ) )
42- prop . SetValue ( packetObj , reader . GetDouble ( ) ) ;
43- else if ( prop . PropertyType == typeof ( bool ) )
44- prop . SetValue ( packetObj , reader . GetBool ( ) ) ;
45- #endregion
46-
47- #region Array Type
48-
49- else if ( prop . PropertyType == typeof ( string [ ] ) )
50- prop . SetValue ( packetObj , reader . GetStringArray ( ) ) ;
51- else if ( prop . PropertyType == typeof ( int [ ] ) )
52- prop . SetValue ( packetObj , reader . GetIntArray ( ) ) ;
53- else if ( prop . PropertyType == typeof ( short [ ] ) )
54- prop . SetValue ( packetObj , reader . GetShortArray ( ) ) ;
55- else if ( prop . PropertyType == typeof ( float [ ] ) )
56- prop . SetValue ( packetObj , reader . GetFloatArray ( ) ) ;
57- else if ( prop . PropertyType == typeof ( double [ ] ) )
58- prop . SetValue ( packetObj , reader . GetDoubleArray ( ) ) ;
59- else if ( prop . PropertyType == typeof ( bool [ ] ) )
60- prop . SetValue ( packetObj , reader . GetBoolArray ( ) ) ;
61- #endregion
62-
63- else
64- throw new UnknownPropertyTypeException ( $ "Type : { prop . PropertyType . Name } ") ;
31+ SetPropertyValue ( packetObj , prop , reader ) ;
6532
6633 packet = packetObj ;
6734 }
@@ -72,44 +39,7 @@ public static dynamic ReadPacket(NetDataReader reader, string packetType, Type t
7239 var field = type . GetField ( fieldName ) ?? throw new UnknownFieldException ( $ "Field : { fieldName } - Packet : { packetType } ") ;
7340 object packetObj = packet ! ;
7441
75- #region Basic Type
76-
77- if ( field . FieldType == typeof ( string ) )
78- field . SetValue ( packetObj , reader . GetString ( ) ) ;
79- else if ( field . FieldType == typeof ( int ) )
80- field . SetValue ( packetObj , reader . GetInt ( ) ) ;
81- else if ( field . FieldType == typeof ( short ) )
82- field . SetValue ( packetObj , reader . GetShort ( ) ) ;
83- else if ( field . FieldType == typeof ( byte ) )
84- field . SetValue ( packetObj , reader . GetByte ( ) ) ;
85- else if ( field . FieldType == typeof ( char ) )
86- field . SetValue ( packetObj , reader . GetChar ( ) ) ;
87- else if ( field . FieldType == typeof ( float ) )
88- field . SetValue ( packetObj , reader . GetFloat ( ) ) ;
89- else if ( field . FieldType == typeof ( double ) )
90- field . SetValue ( packetObj , reader . GetDouble ( ) ) ;
91- else if ( field . FieldType == typeof ( bool ) )
92- field . SetValue ( packetObj , reader . GetBool ( ) ) ;
93- #endregion
94-
95- #region Array Type
96-
97- else if ( field . FieldType == typeof ( string [ ] ) )
98- field . SetValue ( packetObj , reader . GetStringArray ( ) ) ;
99- else if ( field . FieldType == typeof ( int [ ] ) )
100- field . SetValue ( packetObj , reader . GetIntArray ( ) ) ;
101- else if ( field . FieldType == typeof ( short [ ] ) )
102- field . SetValue ( packetObj , reader . GetShortArray ( ) ) ;
103- else if ( field . FieldType == typeof ( float [ ] ) )
104- field . SetValue ( packetObj , reader . GetFloatArray ( ) ) ;
105- else if ( field . FieldType == typeof ( double [ ] ) )
106- field . SetValue ( packetObj , reader . GetDoubleArray ( ) ) ;
107- else if ( field . FieldType == typeof ( bool [ ] ) )
108- field . SetValue ( packetObj , reader . GetBoolArray ( ) ) ;
109- #endregion
110-
111- else
112- throw new UnknownFieldTypeException ( $ "Type : { field . FieldType . Name } ") ;
42+ SetFieldValue ( packetObj , field , reader ) ;
11343
11444 packet = packetObj ;
11545 }
@@ -137,44 +67,8 @@ public static void SendPacket<T>(NetPeer peer, T packet)
13767
13868 writer . Put ( prop . Name ) ;
13969
140- #region Basic Type
141-
142- if ( prop . PropertyType == typeof ( string ) )
143- writer . Put ( ( string ) value ) ;
144- else if ( prop . PropertyType == typeof ( int ) )
145- writer . Put ( ( int ) value ) ;
146- else if ( prop . PropertyType == typeof ( short ) )
147- writer . Put ( ( short ) value ) ;
148- else if ( prop . PropertyType == typeof ( byte ) )
149- writer . Put ( ( byte ) value ) ;
150- else if ( prop . PropertyType == typeof ( char ) )
151- writer . Put ( ( char ) value ) ;
152- else if ( prop . PropertyType == typeof ( float ) )
153- writer . Put ( ( float ) value ) ;
154- else if ( prop . PropertyType == typeof ( double ) )
155- writer . Put ( ( double ) value ) ;
156- else if ( prop . PropertyType == typeof ( bool ) )
157- writer . Put ( ( bool ) value ) ;
158- #endregion
159-
160- #region Array Type
161-
162- else if ( prop . PropertyType == typeof ( string [ ] ) )
163- writer . PutArray ( ( string [ ] ) value ) ;
164- else if ( prop . PropertyType == typeof ( int [ ] ) )
165- writer . PutArray ( ( int [ ] ) value ) ;
166- else if ( prop . PropertyType == typeof ( short [ ] ) )
167- writer . PutArray ( ( short [ ] ) value ) ;
168- else if ( prop . PropertyType == typeof ( float [ ] ) )
169- writer . PutArray ( ( float [ ] ) value ) ;
170- else if ( prop . PropertyType == typeof ( double [ ] ) )
171- writer . PutArray ( ( double [ ] ) value ) ;
172- else if ( prop . PropertyType == typeof ( bool [ ] ) )
173- writer . PutArray ( ( bool [ ] ) value ) ;
174- #endregion
175-
176- else
177- throw new UnknownPropertyTypeException ( $ "Type : { prop . PropertyType . Name } ") ;
70+ PutPropertyValue ( value , prop , writer ) ;
71+
17872 }
17973 foreach ( var field in fields )
18074 {
@@ -184,45 +78,144 @@ public static void SendPacket<T>(NetPeer peer, T packet)
18478
18579 writer . Put ( field . Name ) ;
18680
187- #region Basic Type
188-
189- if ( field . FieldType == typeof ( string ) )
190- writer . Put ( ( string ) value ) ;
191- else if ( field . FieldType == typeof ( int ) )
192- writer . Put ( ( int ) value ) ;
193- else if ( field . FieldType == typeof ( short ) )
194- writer . Put ( ( short ) value ) ;
195- else if ( field . FieldType == typeof ( byte ) )
196- writer . Put ( ( byte ) value ) ;
197- else if ( field . FieldType == typeof ( char ) )
198- writer . Put ( ( char ) value ) ;
199- else if ( field . FieldType == typeof ( float ) )
200- writer . Put ( ( float ) value ) ;
201- else if ( field . FieldType == typeof ( double ) )
202- writer . Put ( ( double ) value ) ;
203- else if ( field . FieldType == typeof ( bool ) )
204- writer . Put ( ( bool ) value ) ;
205- #endregion
206-
207- #region Array Type
208-
209- else if ( field . FieldType == typeof ( string [ ] ) )
210- writer . PutArray ( ( string [ ] ) value ) ;
211- else if ( field . FieldType == typeof ( int [ ] ) )
212- writer . PutArray ( ( int [ ] ) value ) ;
213- else if ( field . FieldType == typeof ( short [ ] ) )
214- writer . PutArray ( ( short [ ] ) value ) ;
215- else if ( field . FieldType == typeof ( float [ ] ) )
216- writer . PutArray ( ( float [ ] ) value ) ;
217- else if ( field . FieldType == typeof ( double [ ] ) )
218- writer . PutArray ( ( double [ ] ) value ) ;
219- else if ( field . FieldType == typeof ( bool [ ] ) )
220- writer . PutArray ( ( bool [ ] ) value ) ;
221- #endregion
222-
223- else
224- throw new UnknownFieldTypeException ( $ "Type : { field . FieldType . Name } ") ;
81+ PutFieldValue ( value , field , writer ) ;
22582 }
22683 peer . Send ( writer , DeliveryMethod . ReliableOrdered ) ;
22784 }
85+
86+ private static void PutPropertyValue ( object value , PropertyInfo prop , NetDataWriter writer )
87+ {
88+ if ( prop . PropertyType == typeof ( string ) )
89+ writer . Put ( ( string ) value ) ;
90+ else if ( prop . PropertyType == typeof ( int ) )
91+ writer . Put ( ( int ) value ) ;
92+ else if ( prop . PropertyType == typeof ( short ) )
93+ writer . Put ( ( short ) value ) ;
94+ else if ( prop . PropertyType == typeof ( byte ) )
95+ writer . Put ( ( byte ) value ) ;
96+ else if ( prop . PropertyType == typeof ( char ) )
97+ writer . Put ( ( char ) value ) ;
98+ else if ( prop . PropertyType == typeof ( float ) )
99+ writer . Put ( ( float ) value ) ;
100+ else if ( prop . PropertyType == typeof ( double ) )
101+ writer . Put ( ( double ) value ) ;
102+ else if ( prop . PropertyType == typeof ( bool ) )
103+ writer . Put ( ( bool ) value ) ;
104+ else if ( prop . PropertyType == typeof ( string [ ] ) )
105+ writer . PutArray ( ( string [ ] ) value ) ;
106+ else if ( prop . PropertyType == typeof ( int [ ] ) )
107+ writer . PutArray ( ( int [ ] ) value ) ;
108+ else if ( prop . PropertyType == typeof ( short [ ] ) )
109+ writer . PutArray ( ( short [ ] ) value ) ;
110+ else if ( prop . PropertyType == typeof ( float [ ] ) )
111+ writer . PutArray ( ( float [ ] ) value ) ;
112+ else if ( prop . PropertyType == typeof ( double [ ] ) )
113+ writer . PutArray ( ( double [ ] ) value ) ;
114+ else if ( prop . PropertyType == typeof ( bool [ ] ) )
115+ writer . PutArray ( ( bool [ ] ) value ) ;
116+ else
117+ throw new UnknownPropertyTypeException ( $ "Type : { prop . PropertyType . Name } ") ;
118+ }
119+
120+ private static void PutFieldValue ( object value , FieldInfo field , NetDataWriter writer )
121+ {
122+ if ( field . FieldType == typeof ( string ) )
123+ writer . Put ( ( string ) value ) ;
124+ else if ( field . FieldType == typeof ( int ) )
125+ writer . Put ( ( int ) value ) ;
126+ else if ( field . FieldType == typeof ( short ) )
127+ writer . Put ( ( short ) value ) ;
128+ else if ( field . FieldType == typeof ( byte ) )
129+ writer . Put ( ( byte ) value ) ;
130+ else if ( field . FieldType == typeof ( char ) )
131+ writer . Put ( ( char ) value ) ;
132+ else if ( field . FieldType == typeof ( float ) )
133+ writer . Put ( ( float ) value ) ;
134+ else if ( field . FieldType == typeof ( double ) )
135+ writer . Put ( ( double ) value ) ;
136+ else if ( field . FieldType == typeof ( bool ) )
137+ writer . Put ( ( bool ) value ) ;
138+ else if ( field . FieldType == typeof ( string [ ] ) )
139+ writer . PutArray ( ( string [ ] ) value ) ;
140+ else if ( field . FieldType == typeof ( int [ ] ) )
141+ writer . PutArray ( ( int [ ] ) value ) ;
142+ else if ( field . FieldType == typeof ( short [ ] ) )
143+ writer . PutArray ( ( short [ ] ) value ) ;
144+ else if ( field . FieldType == typeof ( float [ ] ) )
145+ writer . PutArray ( ( float [ ] ) value ) ;
146+ else if ( field . FieldType == typeof ( double [ ] ) )
147+ writer . PutArray ( ( double [ ] ) value ) ;
148+ else if ( field . FieldType == typeof ( bool [ ] ) )
149+ writer . PutArray ( ( bool [ ] ) value ) ;
150+ else
151+ throw new UnknownFieldTypeException ( $ "Type : { field . FieldType . Name } ") ;
152+ }
153+
154+ private static void SetPropertyValue ( object packetObj , PropertyInfo prop , NetDataReader reader )
155+ {
156+ if ( prop . PropertyType == typeof ( string ) )
157+ prop . SetValue ( packetObj , reader . GetString ( ) ) ;
158+ else if ( prop . PropertyType == typeof ( int ) )
159+ prop . SetValue ( packetObj , reader . GetInt ( ) ) ;
160+ else if ( prop . PropertyType == typeof ( short ) )
161+ prop . SetValue ( packetObj , reader . GetShort ( ) ) ;
162+ else if ( prop . PropertyType == typeof ( byte ) )
163+ prop . SetValue ( packetObj , reader . GetByte ( ) ) ;
164+ else if ( prop . PropertyType == typeof ( char ) )
165+ prop . SetValue ( packetObj , reader . GetChar ( ) ) ;
166+ else if ( prop . PropertyType == typeof ( float ) )
167+ prop . SetValue ( packetObj , reader . GetFloat ( ) ) ;
168+ else if ( prop . PropertyType == typeof ( double ) )
169+ prop . SetValue ( packetObj , reader . GetDouble ( ) ) ;
170+ else if ( prop . PropertyType == typeof ( bool ) )
171+ prop . SetValue ( packetObj , reader . GetBool ( ) ) ;
172+ else if ( prop . PropertyType == typeof ( string [ ] ) )
173+ prop . SetValue ( packetObj , reader . GetStringArray ( ) ) ;
174+ else if ( prop . PropertyType == typeof ( int [ ] ) )
175+ prop . SetValue ( packetObj , reader . GetIntArray ( ) ) ;
176+ else if ( prop . PropertyType == typeof ( short [ ] ) )
177+ prop . SetValue ( packetObj , reader . GetShortArray ( ) ) ;
178+ else if ( prop . PropertyType == typeof ( float [ ] ) )
179+ prop . SetValue ( packetObj , reader . GetFloatArray ( ) ) ;
180+ else if ( prop . PropertyType == typeof ( double [ ] ) )
181+ prop . SetValue ( packetObj , reader . GetDoubleArray ( ) ) ;
182+ else if ( prop . PropertyType == typeof ( bool [ ] ) )
183+ prop . SetValue ( packetObj , reader . GetBoolArray ( ) ) ;
184+ else
185+ throw new UnknownPropertyTypeException ( $ "Type : { prop . PropertyType . Name } ") ;
186+ }
187+
188+ private static void SetFieldValue ( object packetObj , FieldInfo field , NetDataReader reader )
189+ {
190+ if ( field . FieldType == typeof ( string ) )
191+ field . SetValue ( packetObj , reader . GetString ( ) ) ;
192+ else if ( field . FieldType == typeof ( int ) )
193+ field . SetValue ( packetObj , reader . GetInt ( ) ) ;
194+ else if ( field . FieldType == typeof ( short ) )
195+ field . SetValue ( packetObj , reader . GetShort ( ) ) ;
196+ else if ( field . FieldType == typeof ( byte ) )
197+ field . SetValue ( packetObj , reader . GetByte ( ) ) ;
198+ else if ( field . FieldType == typeof ( char ) )
199+ field . SetValue ( packetObj , reader . GetChar ( ) ) ;
200+ else if ( field . FieldType == typeof ( float ) )
201+ field . SetValue ( packetObj , reader . GetFloat ( ) ) ;
202+ else if ( field . FieldType == typeof ( double ) )
203+ field . SetValue ( packetObj , reader . GetDouble ( ) ) ;
204+ else if ( field . FieldType == typeof ( bool ) )
205+ field . SetValue ( packetObj , reader . GetBool ( ) ) ;
206+ else if ( field . FieldType == typeof ( string [ ] ) )
207+ field . SetValue ( packetObj , reader . GetStringArray ( ) ) ;
208+ else if ( field . FieldType == typeof ( int [ ] ) )
209+ field . SetValue ( packetObj , reader . GetIntArray ( ) ) ;
210+ else if ( field . FieldType == typeof ( short [ ] ) )
211+ field . SetValue ( packetObj , reader . GetShortArray ( ) ) ;
212+ else if ( field . FieldType == typeof ( float [ ] ) )
213+ field . SetValue ( packetObj , reader . GetFloatArray ( ) ) ;
214+ else if ( field . FieldType == typeof ( double [ ] ) )
215+ field . SetValue ( packetObj , reader . GetDoubleArray ( ) ) ;
216+ else if ( field . FieldType == typeof ( bool [ ] ) )
217+ field . SetValue ( packetObj , reader . GetBoolArray ( ) ) ;
218+ else
219+ throw new UnknownFieldTypeException ( $ "Type : { field . FieldType . Name } ") ;
220+ }
228221}
0 commit comments