@@ -59,7 +59,6 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
59
59
60
60
try
61
61
{
62
- types . ForEach ( b => ProcessINetworkMessage ( b ) ) ;
63
62
CreateModuleInitializer ( assemblyDefinition , types ) ;
64
63
}
65
64
catch ( Exception e )
@@ -98,7 +97,11 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
98
97
99
98
private TypeReference m_FastBufferReader_TypeRef ;
100
99
private TypeReference m_NetworkContext_TypeRef ;
101
- private FieldReference m_MessagingSystem___network_message_types_FieldRef ;
100
+ private TypeReference m_MessagingSystem_MessageWithHandler_TypeRef ;
101
+ private MethodReference m_MessagingSystem_MessageHandler_Constructor_TypeRef ;
102
+ private FieldReference m_ILPPMessageProvider___network_message_types_FieldRef ;
103
+ private FieldReference m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ;
104
+ private FieldReference m_MessagingSystem_MessageWithHandler_Handler_FieldRef ;
102
105
private MethodReference m_Type_GetTypeFromHandle_MethodRef ;
103
106
104
107
private MethodReference m_List_Add_MethodRef ;
@@ -107,6 +110,24 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
107
110
{
108
111
m_FastBufferReader_TypeRef = moduleDefinition . ImportReference ( typeof ( FastBufferReader ) ) ;
109
112
m_NetworkContext_TypeRef = moduleDefinition . ImportReference ( typeof ( NetworkContext ) ) ;
113
+ m_MessagingSystem_MessageHandler_Constructor_TypeRef =
114
+ moduleDefinition . ImportReference ( typeof ( MessagingSystem . MessageHandler ) . GetConstructors ( ) [ 0 ] ) ;
115
+
116
+ var messageWithHandlerType = typeof ( MessagingSystem . MessageWithHandler ) ;
117
+ m_MessagingSystem_MessageWithHandler_TypeRef =
118
+ moduleDefinition . ImportReference ( messageWithHandlerType ) ;
119
+ foreach ( var fieldInfo in messageWithHandlerType . GetFields ( ) )
120
+ {
121
+ switch ( fieldInfo . Name )
122
+ {
123
+ case nameof ( MessagingSystem . MessageWithHandler . MessageType ) :
124
+ m_MessagingSystem_MessageWithHandler_MessageType_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
125
+ break ;
126
+ case nameof ( MessagingSystem . MessageWithHandler . Handler ) :
127
+ m_MessagingSystem_MessageWithHandler_Handler_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
128
+ break ;
129
+ }
130
+ }
110
131
111
132
var typeType = typeof ( Type ) ;
112
133
foreach ( var methodInfo in typeType . GetMethods ( ) )
@@ -119,23 +140,23 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
119
140
}
120
141
}
121
142
122
- var messagingSystemType = typeof ( MessagingSystem ) ;
123
- foreach ( var fieldInfo in messagingSystemType . GetFields ( BindingFlags . Static | BindingFlags . NonPublic ) )
143
+ var ilppMessageProviderType = typeof ( ILPPMessageProvider ) ;
144
+ foreach ( var fieldInfo in ilppMessageProviderType . GetFields ( BindingFlags . Static | BindingFlags . NonPublic ) )
124
145
{
125
146
switch ( fieldInfo . Name )
126
147
{
127
- case nameof ( MessagingSystem . __network_message_types ) :
128
- m_MessagingSystem___network_message_types_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
148
+ case nameof ( ILPPMessageProvider . __network_message_types ) :
149
+ m_ILPPMessageProvider___network_message_types_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
129
150
break ;
130
151
}
131
152
}
132
153
133
- var listType = typeof ( List < Type > ) ;
154
+ var listType = typeof ( List < MessagingSystem . MessageWithHandler > ) ;
134
155
foreach ( var methodInfo in listType . GetMethods ( ) )
135
156
{
136
157
switch ( methodInfo . Name )
137
158
{
138
- case nameof ( List < Type > . Add ) :
159
+ case nameof ( List < MessagingSystem . MessageWithHandler > . Add ) :
139
160
m_List_Add_MethodRef = moduleDefinition . ImportReference ( methodInfo ) ;
140
161
break ;
141
162
}
@@ -145,9 +166,8 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
145
166
return true ;
146
167
}
147
168
148
- private void ProcessINetworkMessage ( TypeDefinition typeDefinition )
169
+ private MethodReference GetNetworkMessageRecieveHandler ( TypeDefinition typeDefinition )
149
170
{
150
- var foundAValidMethod = false ;
151
171
SequencePoint typeSequence = null ;
152
172
foreach ( var method in typeDefinition . Methods )
153
173
{
@@ -168,15 +188,12 @@ private void ProcessINetworkMessage(TypeDefinition typeDefinition)
168
188
&& resolved . Parameters [ 1 ] . ParameterType . GetElementType ( ) . Resolve ( ) == m_NetworkContext_TypeRef . Resolve ( )
169
189
&& resolved . ReturnType == resolved . Module . TypeSystem . Void )
170
190
{
171
- foundAValidMethod = true ;
172
- break ;
191
+ return method ;
173
192
}
174
193
}
175
194
176
- if ( ! foundAValidMethod )
177
- {
178
- m_Diagnostics . AddError ( typeSequence , $ "Class { typeDefinition . FullName } does not implement required function: `public static void Receive(FastBufferReader, in NetworkContext)`") ;
179
- }
195
+ m_Diagnostics . AddError ( typeSequence , $ "Class { typeDefinition . FullName } does not implement required method: `public static void Receive(FastBufferReader, in NetworkContext)`") ;
196
+ return null ;
180
197
}
181
198
182
199
private MethodDefinition GetOrCreateStaticConstructor ( TypeDefinition typeDefinition )
@@ -198,12 +215,32 @@ private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinit
198
215
return staticCtorMethodDef ;
199
216
}
200
217
201
- private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type )
218
+ private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod )
202
219
{
203
- // MessagingSystem.__network_message_types.Add(typeof(type));
204
- instructions . Add ( processor . Create ( OpCodes . Ldsfld , m_MessagingSystem___network_message_types_FieldRef ) ) ;
220
+ // MessagingSystem.__network_message_types.Add(new MessagingSystem.MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
221
+ processor . Body . Variables . Add ( new VariableDefinition ( m_MessagingSystem_MessageWithHandler_TypeRef ) ) ;
222
+ int messageWithHandlerLocIdx = processor . Body . Variables . Count - 1 ;
223
+
224
+ instructions . Add ( processor . Create ( OpCodes . Ldsfld , m_ILPPMessageProvider___network_message_types_FieldRef ) ) ;
225
+ instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
226
+ instructions . Add ( processor . Create ( OpCodes . Initobj , m_MessagingSystem_MessageWithHandler_TypeRef ) ) ;
227
+
228
+ // tmp.MessageType = typeof(type);
229
+ instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
205
230
instructions . Add ( processor . Create ( OpCodes . Ldtoken , type ) ) ;
206
231
instructions . Add ( processor . Create ( OpCodes . Call , m_Type_GetTypeFromHandle_MethodRef ) ) ;
232
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ) ) ;
233
+
234
+ // tmp.Handler = type.Receive
235
+ instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
236
+ instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
237
+
238
+ instructions . Add ( processor . Create ( OpCodes . Ldftn , receiveMethod ) ) ;
239
+ instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessagingSystem_MessageHandler_Constructor_TypeRef ) ) ;
240
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_Handler_FieldRef ) ) ;
241
+
242
+ // ILPPMessageProvider.__network_message_types.Add(tmp);
243
+ instructions . Add ( processor . Create ( OpCodes . Ldloc , messageWithHandlerLocIdx ) ) ;
207
244
instructions . Add ( processor . Create ( OpCodes . Callvirt , m_List_Add_MethodRef ) ) ;
208
245
}
209
246
@@ -227,7 +264,12 @@ private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeDefin
227
264
228
265
foreach ( var type in networkMessageTypes )
229
266
{
230
- CreateInstructionsToRegisterType ( processor , instructions , type ) ;
267
+ var receiveMethod = GetNetworkMessageRecieveHandler ( type ) ;
268
+ if ( receiveMethod == null )
269
+ {
270
+ continue ;
271
+ }
272
+ CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod ) ;
231
273
}
232
274
233
275
instructions . ForEach ( instruction => processor . Body . Instructions . Insert ( processor . Body . Instructions . Count - 1 , instruction ) ) ;
0 commit comments