7
7
using Mono . Cecil . Rocks ;
8
8
using Unity . CompilationPipeline . Common . Diagnostics ;
9
9
using Unity . CompilationPipeline . Common . ILPostProcessing ;
10
+ using UnityEngine ;
10
11
using ILPPInterface = Unity . CompilationPipeline . Common . ILPostProcessing . ILPostProcessor ;
11
12
using MethodAttributes = Mono . Cecil . MethodAttributes ;
12
13
@@ -101,6 +102,8 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
101
102
private ModuleDefinition m_NetcodeModule ;
102
103
private PostProcessorAssemblyResolver m_AssemblyResolver ;
103
104
105
+ private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor ;
106
+
104
107
private MethodReference m_MessageManager_ReceiveMessage_MethodRef ;
105
108
private MethodReference m_MessageManager_CreateMessageAndGetVersion_MethodRef ;
106
109
private TypeReference m_MessageManager_MessageWithHandler_TypeRef ;
@@ -125,6 +128,7 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
125
128
// (i.e., there's no #if UNITY_EDITOR in them that could create invalid IL code)
126
129
TypeDefinition typeTypeDef = moduleDefinition . ImportReference ( typeof ( Type ) ) . Resolve ( ) ;
127
130
TypeDefinition listTypeDef = moduleDefinition . ImportReference ( typeof ( List < > ) ) . Resolve ( ) ;
131
+ m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition . ImportReference ( typeof ( RuntimeInitializeOnLoadMethodAttribute ) . GetConstructor ( new Type [ ] { } ) ) ;
128
132
129
133
TypeDefinition messageHandlerTypeDef = null ;
130
134
TypeDefinition versionGetterTypeDef = null ;
@@ -232,25 +236,6 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
232
236
return true ;
233
237
}
234
238
235
- private MethodDefinition GetOrCreateStaticConstructor ( TypeDefinition typeDefinition )
236
- {
237
- var staticCtorMethodDef = typeDefinition . GetStaticConstructor ( ) ;
238
- if ( staticCtorMethodDef == null )
239
- {
240
- staticCtorMethodDef = new MethodDefinition (
241
- ".cctor" , // Static Constructor (constant-constructor)
242
- MethodAttributes . HideBySig |
243
- MethodAttributes . SpecialName |
244
- MethodAttributes . RTSpecialName |
245
- MethodAttributes . Static ,
246
- typeDefinition . Module . TypeSystem . Void ) ;
247
- staticCtorMethodDef . Body . Instructions . Add ( Instruction . Create ( OpCodes . Ret ) ) ;
248
- typeDefinition . Methods . Add ( staticCtorMethodDef ) ;
249
- }
250
-
251
- return staticCtorMethodDef ;
252
- }
253
-
254
239
private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod , MethodReference versionMethod )
255
240
{
256
241
// NetworkMessageManager.__network_message_types.Add(new NetworkMessageManager.MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
@@ -295,29 +280,32 @@ private void CreateInstructionsToRegisterType(ILProcessor processor, List<Instru
295
280
// https://web.archive.org/web/20100212140402/http://blogs.msdn.com/junfeng/archive/2005/11/19/494914.aspx
296
281
private void CreateModuleInitializer ( AssemblyDefinition assembly , List < TypeDefinition > networkMessageTypes )
297
282
{
298
- foreach ( var typeDefinition in assembly . MainModule . Types )
299
- {
300
- if ( typeDefinition . FullName == "<Module>" )
301
- {
302
- var staticCtorMethodDef = GetOrCreateStaticConstructor ( typeDefinition ) ;
303
-
304
- var processor = staticCtorMethodDef . Body . GetILProcessor ( ) ;
283
+ var typeDefinition = new TypeDefinition ( "__GEN" , "INetworkMessageHelper" , TypeAttributes . NotPublic | TypeAttributes . AnsiClass | TypeAttributes . BeforeFieldInit , assembly . MainModule . TypeSystem . Object ) ;
305
284
306
- var instructions = new List < Instruction > ( ) ;
285
+ var staticCtorMethodDef = new MethodDefinition (
286
+ $ "InitializeMessages",
287
+ MethodAttributes . Assembly |
288
+ MethodAttributes . Static ,
289
+ assembly . MainModule . TypeSystem . Void ) ;
290
+ staticCtorMethodDef . Body . Instructions . Add ( Instruction . Create ( OpCodes . Ret ) ) ;
291
+ staticCtorMethodDef . CustomAttributes . Add ( new CustomAttribute ( m_RuntimeInitializeOnLoadAttribute_Ctor ) ) ;
292
+ typeDefinition . Methods . Add ( staticCtorMethodDef ) ;
307
293
308
- foreach ( var type in networkMessageTypes )
309
- {
310
- var receiveMethod = new GenericInstanceMethod ( m_MessageManager_ReceiveMessage_MethodRef ) ;
311
- receiveMethod . GenericArguments . Add ( type ) ;
312
- var versionMethod = new GenericInstanceMethod ( m_MessageManager_CreateMessageAndGetVersion_MethodRef ) ;
313
- versionMethod . GenericArguments . Add ( type ) ;
314
- CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod , versionMethod ) ;
315
- }
294
+ var instructions = new List < Instruction > ( ) ;
295
+ var processor = staticCtorMethodDef . Body . GetILProcessor ( ) ;
316
296
317
- instructions . ForEach ( instruction => processor . Body . Instructions . Insert ( processor . Body . Instructions . Count - 1 , instruction ) ) ;
318
- break ;
319
- }
297
+ foreach ( var type in networkMessageTypes )
298
+ {
299
+ var receiveMethod = new GenericInstanceMethod ( m_MessageManager_ReceiveMessage_MethodRef ) ;
300
+ receiveMethod . GenericArguments . Add ( type ) ;
301
+ var versionMethod = new GenericInstanceMethod ( m_MessageManager_CreateMessageAndGetVersion_MethodRef ) ;
302
+ versionMethod . GenericArguments . Add ( type ) ;
303
+ CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod , versionMethod ) ;
320
304
}
305
+
306
+ instructions . ForEach ( instruction => processor . Body . Instructions . Insert ( processor . Body . Instructions . Count - 1 , instruction ) ) ;
307
+
308
+ assembly . MainModule . Types . Add ( typeDefinition ) ;
321
309
}
322
310
}
323
311
}
0 commit comments