@@ -102,15 +102,19 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
102
102
private PostProcessorAssemblyResolver m_AssemblyResolver ;
103
103
104
104
private MethodReference m_MessagingSystem_ReceiveMessage_MethodRef ;
105
+ private MethodReference m_MessagingSystem_CreateMessageAndGetVersion_MethodRef ;
105
106
private TypeReference m_MessagingSystem_MessageWithHandler_TypeRef ;
106
107
private MethodReference m_MessagingSystem_MessageHandler_Constructor_TypeRef ;
108
+ private MethodReference m_MessagingSystem_VersionGetter_Constructor_TypeRef ;
107
109
private FieldReference m_ILPPMessageProvider___network_message_types_FieldRef ;
108
110
private FieldReference m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ;
109
111
private FieldReference m_MessagingSystem_MessageWithHandler_Handler_FieldRef ;
112
+ private FieldReference m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef ;
110
113
private MethodReference m_Type_GetTypeFromHandle_MethodRef ;
111
114
private MethodReference m_List_Add_MethodRef ;
112
115
113
116
private const string k_ReceiveMessageName = nameof ( MessagingSystem . ReceiveMessage ) ;
117
+ private const string k_CreateMessageAndGetVersionName = nameof ( MessagingSystem . CreateMessageAndGetVersion ) ;
114
118
115
119
private bool ImportReferences ( ModuleDefinition moduleDefinition )
116
120
{
@@ -126,6 +130,7 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
126
130
TypeDefinition listTypeDef = moduleDefinition . ImportReference ( typeof ( List < > ) ) . Resolve ( ) ;
127
131
128
132
TypeDefinition messageHandlerTypeDef = null ;
133
+ TypeDefinition versionGetterTypeDef = null ;
129
134
TypeDefinition messageWithHandlerTypeDef = null ;
130
135
TypeDefinition ilppMessageProviderTypeDef = null ;
131
136
TypeDefinition messagingSystemTypeDef = null ;
@@ -137,6 +142,12 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
137
142
continue ;
138
143
}
139
144
145
+ if ( versionGetterTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem . VersionGetter ) )
146
+ {
147
+ versionGetterTypeDef = netcodeTypeDef ;
148
+ continue ;
149
+ }
150
+
140
151
if ( messageWithHandlerTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem . MessageWithHandler ) )
141
152
{
142
153
messageWithHandlerTypeDef = netcodeTypeDef ;
@@ -157,6 +168,7 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
157
168
}
158
169
159
170
m_MessagingSystem_MessageHandler_Constructor_TypeRef = moduleDefinition . ImportReference ( messageHandlerTypeDef . GetConstructors ( ) . First ( ) ) ;
171
+ m_MessagingSystem_VersionGetter_Constructor_TypeRef = moduleDefinition . ImportReference ( versionGetterTypeDef . GetConstructors ( ) . First ( ) ) ;
160
172
161
173
m_MessagingSystem_MessageWithHandler_TypeRef = moduleDefinition . ImportReference ( messageWithHandlerTypeDef ) ;
162
174
foreach ( var fieldDef in messageWithHandlerTypeDef . Fields )
@@ -169,6 +181,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
169
181
case nameof ( MessagingSystem . MessageWithHandler . Handler ) :
170
182
m_MessagingSystem_MessageWithHandler_Handler_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
171
183
break ;
184
+ case nameof ( MessagingSystem . MessageWithHandler . GetVersion ) :
185
+ m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
186
+ break ;
172
187
}
173
188
}
174
189
@@ -211,6 +226,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
211
226
case k_ReceiveMessageName :
212
227
m_MessagingSystem_ReceiveMessage_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
213
228
break ;
229
+ case k_CreateMessageAndGetVersionName :
230
+ m_MessagingSystem_CreateMessageAndGetVersion_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
231
+ break ;
214
232
}
215
233
}
216
234
@@ -236,7 +254,7 @@ private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinit
236
254
return staticCtorMethodDef ;
237
255
}
238
256
239
- private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod )
257
+ private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod , MethodReference versionMethod )
240
258
{
241
259
// MessagingSystem.__network_message_types.Add(new MessagingSystem.MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
242
260
processor . Body . Variables . Add ( new VariableDefinition ( m_MessagingSystem_MessageWithHandler_TypeRef ) ) ;
@@ -252,14 +270,23 @@ private void CreateInstructionsToRegisterType(ILProcessor processor, List<Instru
252
270
instructions . Add ( processor . Create ( OpCodes . Call , m_Type_GetTypeFromHandle_MethodRef ) ) ;
253
271
instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ) ) ;
254
272
255
- // tmp.Handler = type.Receive
273
+ // tmp.Handler = MessageHandler.ReceveMessage<type>
256
274
instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
257
275
instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
258
276
259
277
instructions . Add ( processor . Create ( OpCodes . Ldftn , receiveMethod ) ) ;
260
278
instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessagingSystem_MessageHandler_Constructor_TypeRef ) ) ;
261
279
instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_Handler_FieldRef ) ) ;
262
280
281
+
282
+ // tmp.GetVersion = MessageHandler.CreateMessageAndGetVersion<type>
283
+ instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
284
+ instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
285
+
286
+ instructions . Add ( processor . Create ( OpCodes . Ldftn , versionMethod ) ) ;
287
+ instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessagingSystem_VersionGetter_Constructor_TypeRef ) ) ;
288
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef ) ) ;
289
+
263
290
// ILPPMessageProvider.__network_message_types.Add(tmp);
264
291
instructions . Add ( processor . Create ( OpCodes . Ldloc , messageWithHandlerLocIdx ) ) ;
265
292
instructions . Add ( processor . Create ( OpCodes . Callvirt , m_List_Add_MethodRef ) ) ;
@@ -285,7 +312,9 @@ private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeDefin
285
312
{
286
313
var receiveMethod = new GenericInstanceMethod ( m_MessagingSystem_ReceiveMessage_MethodRef ) ;
287
314
receiveMethod . GenericArguments . Add ( type ) ;
288
- CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod ) ;
315
+ var versionMethod = new GenericInstanceMethod ( m_MessagingSystem_CreateMessageAndGetVersion_MethodRef ) ;
316
+ versionMethod . GenericArguments . Add ( type ) ;
317
+ CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod , versionMethod ) ;
289
318
}
290
319
291
320
instructions . ForEach ( instruction => processor . Body . Instructions . Insert ( processor . Body . Instructions . Count - 1 , instruction ) ) ;
0 commit comments