@@ -139,6 +139,19 @@ private bool IsMemcpyableType(TypeReference type)
139
139
return false ;
140
140
}
141
141
142
+ private bool IsSpecialCaseType ( TypeReference type )
143
+ {
144
+ foreach ( var supportedType in SpecialCaseTypes )
145
+ {
146
+ if ( type . FullName == supportedType . FullName )
147
+ {
148
+ return true ;
149
+ }
150
+ }
151
+
152
+ return false ;
153
+ }
154
+
142
155
private void CreateNetworkVariableTypeInitializers ( AssemblyDefinition assembly )
143
156
{
144
157
foreach ( var typeDefinition in assembly . MainModule . Types )
@@ -153,6 +166,11 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
153
166
154
167
foreach ( var type in m_WrappedNetworkVariableTypes )
155
168
{
169
+ if ( IsSpecialCaseType ( type ) )
170
+ {
171
+ continue ;
172
+ }
173
+
156
174
// If a serializable type isn't found, FallbackSerializer will be used automatically, which will
157
175
// call into UserNetworkVariableSerialization, giving the user a chance to define their own serializaiton
158
176
// for types that aren't in our official supported types list.
@@ -257,6 +275,20 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
257
275
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_UnmanagedValueEquals_MethodRef ;
258
276
private MethodReference m_NetworkVariableSerializationTypes_InitializeEqualityChecker_ManagedClassEquals_MethodRef ;
259
277
278
+ private MethodReference m_BytePacker_WriteValueBitPacked_Short_MethodRef ;
279
+ private MethodReference m_BytePacker_WriteValueBitPacked_UShort_MethodRef ;
280
+ private MethodReference m_BytePacker_WriteValueBitPacked_Int_MethodRef ;
281
+ private MethodReference m_BytePacker_WriteValueBitPacked_UInt_MethodRef ;
282
+ private MethodReference m_BytePacker_WriteValueBitPacked_Long_MethodRef ;
283
+ private MethodReference m_BytePacker_WriteValueBitPacked_ULong_MethodRef ;
284
+
285
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef ;
286
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef ;
287
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef ;
288
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef ;
289
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef ;
290
+ private MethodReference m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef ;
291
+
260
292
private TypeReference m_FastBufferWriter_TypeRef ;
261
293
private readonly Dictionary < string , MethodReference > m_FastBufferWriter_WriteValue_MethodRefs = new Dictionary < string , MethodReference > ( ) ;
262
294
private readonly List < MethodReference > m_FastBufferWriter_ExtensionMethodRefs = new List < MethodReference > ( ) ;
@@ -276,12 +308,13 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
276
308
typeof ( decimal ) ,
277
309
typeof ( double ) ,
278
310
typeof ( float ) ,
279
- typeof ( int ) ,
311
+ // the following types have special handling
312
+ /*typeof(int),
280
313
typeof(uint),
281
314
typeof(long),
282
315
typeof(ulong),
283
316
typeof(short),
284
- typeof ( ushort ) ,
317
+ typeof(ushort),*/
285
318
typeof ( Vector2 ) ,
286
319
typeof ( Vector3 ) ,
287
320
typeof ( Vector2Int ) ,
@@ -293,6 +326,16 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
293
326
typeof ( Ray ) ,
294
327
typeof ( Ray2D )
295
328
} ;
329
+ internal static readonly Type [ ] SpecialCaseTypes = new [ ]
330
+ {
331
+ // the following types have special handling
332
+ typeof ( int ) ,
333
+ typeof ( uint ) ,
334
+ typeof ( long ) ,
335
+ typeof ( ulong ) ,
336
+ typeof ( short ) ,
337
+ typeof ( ushort ) ,
338
+ } ;
296
339
297
340
private const string k_Debug_LogError = nameof ( Debug . LogError ) ;
298
341
private const string k_NetworkManager_LocalClientId = nameof ( NetworkManager . LocalClientId ) ;
@@ -343,6 +386,8 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
343
386
TypeDefinition fastBufferWriterTypeDef = null ;
344
387
TypeDefinition fastBufferReaderTypeDef = null ;
345
388
TypeDefinition networkVariableSerializationTypesTypeDef = null ;
389
+ TypeDefinition bytePackerTypeDef = null ;
390
+ TypeDefinition byteUnpackerTypeDef = null ;
346
391
foreach ( var netcodeTypeDef in m_NetcodeModule . GetAllTypes ( ) )
347
392
{
348
393
if ( networkManagerTypeDef == null && netcodeTypeDef . Name == nameof ( NetworkManager ) )
@@ -398,6 +443,18 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
398
443
networkVariableSerializationTypesTypeDef = netcodeTypeDef ;
399
444
continue ;
400
445
}
446
+
447
+ if ( bytePackerTypeDef == null && netcodeTypeDef . Name == nameof ( BytePacker ) )
448
+ {
449
+ bytePackerTypeDef = netcodeTypeDef ;
450
+ continue ;
451
+ }
452
+
453
+ if ( byteUnpackerTypeDef == null && netcodeTypeDef . Name == nameof ( ByteUnpacker ) )
454
+ {
455
+ byteUnpackerTypeDef = netcodeTypeDef ;
456
+ continue ;
457
+ }
401
458
}
402
459
403
460
foreach ( var methodDef in debugTypeDef . Methods )
@@ -652,6 +709,82 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
652
709
}
653
710
}
654
711
712
+ foreach ( var method in bytePackerTypeDef . Methods )
713
+ {
714
+ if ( ! method . IsStatic )
715
+ {
716
+ continue ;
717
+ }
718
+
719
+ switch ( method . Name )
720
+ {
721
+ case nameof ( BytePacker . WriteValueBitPacked ) :
722
+ if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( short ) . FullName )
723
+ {
724
+ m_BytePacker_WriteValueBitPacked_Short_MethodRef = m_MainModule . ImportReference ( method ) ;
725
+ }
726
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( ushort ) . FullName )
727
+ {
728
+ m_BytePacker_WriteValueBitPacked_UShort_MethodRef = m_MainModule . ImportReference ( method ) ;
729
+ }
730
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( int ) . FullName )
731
+ {
732
+ m_BytePacker_WriteValueBitPacked_Int_MethodRef = m_MainModule . ImportReference ( method ) ;
733
+ }
734
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( uint ) . FullName )
735
+ {
736
+ m_BytePacker_WriteValueBitPacked_UInt_MethodRef = m_MainModule . ImportReference ( method ) ;
737
+ }
738
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( long ) . FullName )
739
+ {
740
+ m_BytePacker_WriteValueBitPacked_Long_MethodRef = m_MainModule . ImportReference ( method ) ;
741
+ }
742
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( ulong ) . FullName )
743
+ {
744
+ m_BytePacker_WriteValueBitPacked_ULong_MethodRef = m_MainModule . ImportReference ( method ) ;
745
+ }
746
+ break ;
747
+ }
748
+ }
749
+
750
+ foreach ( var method in byteUnpackerTypeDef . Methods )
751
+ {
752
+ if ( ! method . IsStatic )
753
+ {
754
+ continue ;
755
+ }
756
+
757
+ switch ( method . Name )
758
+ {
759
+ case nameof ( ByteUnpacker . ReadValueBitPacked ) :
760
+ if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( short ) . MakeByRefType ( ) . FullName )
761
+ {
762
+ m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef = m_MainModule . ImportReference ( method ) ;
763
+ }
764
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( ushort ) . MakeByRefType ( ) . FullName )
765
+ {
766
+ m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef = m_MainModule . ImportReference ( method ) ;
767
+ }
768
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( int ) . MakeByRefType ( ) . FullName )
769
+ {
770
+ m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef = m_MainModule . ImportReference ( method ) ;
771
+ }
772
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( uint ) . MakeByRefType ( ) . FullName )
773
+ {
774
+ m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef = m_MainModule . ImportReference ( method ) ;
775
+ }
776
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( long ) . MakeByRefType ( ) . FullName )
777
+ {
778
+ m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef = m_MainModule . ImportReference ( method ) ;
779
+ }
780
+ else if ( method . Parameters [ 1 ] . ParameterType . FullName == typeof ( ulong ) . MakeByRefType ( ) . FullName )
781
+ {
782
+ m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef = m_MainModule . ImportReference ( method ) ;
783
+ }
784
+ break ;
785
+ }
786
+ }
787
+
655
788
return true ;
656
789
}
657
790
@@ -1008,6 +1141,36 @@ private MethodReference GetFastBufferWriterWriteMethod(string name, TypeReferenc
1008
1141
1009
1142
private bool GetWriteMethodForParameter ( TypeReference paramType , out MethodReference methodRef )
1010
1143
{
1144
+ if ( paramType . FullName == typeof ( short ) . FullName )
1145
+ {
1146
+ methodRef = m_BytePacker_WriteValueBitPacked_Short_MethodRef ;
1147
+ return true ;
1148
+ }
1149
+ if ( paramType . FullName == typeof ( ushort ) . FullName )
1150
+ {
1151
+ methodRef = m_BytePacker_WriteValueBitPacked_UShort_MethodRef ;
1152
+ return true ;
1153
+ }
1154
+ if ( paramType . FullName == typeof ( int ) . FullName )
1155
+ {
1156
+ methodRef = m_BytePacker_WriteValueBitPacked_Int_MethodRef ;
1157
+ return true ;
1158
+ }
1159
+ if ( paramType . FullName == typeof ( uint ) . FullName )
1160
+ {
1161
+ methodRef = m_BytePacker_WriteValueBitPacked_UInt_MethodRef ;
1162
+ return true ;
1163
+ }
1164
+ if ( paramType . FullName == typeof ( long ) . FullName )
1165
+ {
1166
+ methodRef = m_BytePacker_WriteValueBitPacked_Long_MethodRef ;
1167
+ return true ;
1168
+ }
1169
+ if ( paramType . FullName == typeof ( ulong ) . FullName )
1170
+ {
1171
+ methodRef = m_BytePacker_WriteValueBitPacked_ULong_MethodRef ;
1172
+ return true ;
1173
+ }
1011
1174
var assemblyQualifiedName = paramType . FullName + ", " + paramType . Resolve ( ) . Module . Assembly . FullName ;
1012
1175
var foundMethodRef = m_FastBufferWriter_WriteValue_MethodRefs . TryGetValue ( assemblyQualifiedName , out methodRef ) ;
1013
1176
@@ -1154,6 +1317,36 @@ private MethodReference GetFastBufferReaderReadMethod(string name, TypeReference
1154
1317
1155
1318
private bool GetReadMethodForParameter ( TypeReference paramType , out MethodReference methodRef )
1156
1319
{
1320
+ if ( paramType . FullName == typeof ( short ) . FullName )
1321
+ {
1322
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Short_MethodRef ;
1323
+ return true ;
1324
+ }
1325
+ if ( paramType . FullName == typeof ( ushort ) . FullName )
1326
+ {
1327
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_UShort_MethodRef ;
1328
+ return true ;
1329
+ }
1330
+ if ( paramType . FullName == typeof ( int ) . FullName )
1331
+ {
1332
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Int_MethodRef ;
1333
+ return true ;
1334
+ }
1335
+ if ( paramType . FullName == typeof ( uint ) . FullName )
1336
+ {
1337
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_UInt_MethodRef ;
1338
+ return true ;
1339
+ }
1340
+ if ( paramType . FullName == typeof ( long ) . FullName )
1341
+ {
1342
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_Long_MethodRef ;
1343
+ return true ;
1344
+ }
1345
+ if ( paramType . FullName == typeof ( ulong ) . FullName )
1346
+ {
1347
+ methodRef = m_ByteUnpacker_ReadValueBitPacked_ULong_MethodRef ;
1348
+ return true ;
1349
+ }
1157
1350
var assemblyQualifiedName = paramType . FullName + ", " + paramType . Resolve ( ) . Module . Assembly . FullName ;
1158
1351
1159
1352
var foundMethodRef = m_FastBufferReader_ReadValue_MethodRefs . TryGetValue ( assemblyQualifiedName , out methodRef ) ;
0 commit comments