@@ -59,7 +59,6 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
5959
6060 try
6161 {
62- types . ForEach ( b => ProcessINetworkMessage ( b ) ) ;
6362 CreateModuleInitializer ( assemblyDefinition , types ) ;
6463 }
6564 catch ( Exception e )
@@ -98,7 +97,11 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
9897
9998 private TypeReference m_FastBufferReader_TypeRef ;
10099 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 ;
102105 private MethodReference m_Type_GetTypeFromHandle_MethodRef ;
103106
104107 private MethodReference m_List_Add_MethodRef ;
@@ -107,6 +110,24 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
107110 {
108111 m_FastBufferReader_TypeRef = moduleDefinition . ImportReference ( typeof ( FastBufferReader ) ) ;
109112 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+ }
110131
111132 var typeType = typeof ( Type ) ;
112133 foreach ( var methodInfo in typeType . GetMethods ( ) )
@@ -119,23 +140,23 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
119140 }
120141 }
121142
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 ) )
124145 {
125146 switch ( fieldInfo . Name )
126147 {
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 ) ;
129150 break ;
130151 }
131152 }
132153
133- var listType = typeof ( List < Type > ) ;
154+ var listType = typeof ( List < MessagingSystem . MessageWithHandler > ) ;
134155 foreach ( var methodInfo in listType . GetMethods ( ) )
135156 {
136157 switch ( methodInfo . Name )
137158 {
138- case nameof ( List < Type > . Add ) :
159+ case nameof ( List < MessagingSystem . MessageWithHandler > . Add ) :
139160 m_List_Add_MethodRef = moduleDefinition . ImportReference ( methodInfo ) ;
140161 break ;
141162 }
@@ -145,9 +166,8 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
145166 return true ;
146167 }
147168
148- private void ProcessINetworkMessage ( TypeDefinition typeDefinition )
169+ private MethodReference GetNetworkMessageRecieveHandler ( TypeDefinition typeDefinition )
149170 {
150- var foundAValidMethod = false ;
151171 SequencePoint typeSequence = null ;
152172 foreach ( var method in typeDefinition . Methods )
153173 {
@@ -168,15 +188,12 @@ private void ProcessINetworkMessage(TypeDefinition typeDefinition)
168188 && resolved . Parameters [ 1 ] . ParameterType . GetElementType ( ) . Resolve ( ) == m_NetworkContext_TypeRef . Resolve ( )
169189 && resolved . ReturnType == resolved . Module . TypeSystem . Void )
170190 {
171- foundAValidMethod = true ;
172- break ;
191+ return method ;
173192 }
174193 }
175194
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 ;
180197 }
181198
182199 private MethodDefinition GetOrCreateStaticConstructor ( TypeDefinition typeDefinition )
@@ -198,12 +215,32 @@ private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinit
198215 return staticCtorMethodDef ;
199216 }
200217
201- private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type )
218+ private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod )
202219 {
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 ) ) ;
205230 instructions . Add ( processor . Create ( OpCodes . Ldtoken , type ) ) ;
206231 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 ) ) ;
207244 instructions . Add ( processor . Create ( OpCodes . Callvirt , m_List_Add_MethodRef ) ) ;
208245 }
209246
@@ -227,7 +264,12 @@ private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeDefin
227264
228265 foreach ( var type in networkMessageTypes )
229266 {
230- CreateInstructionsToRegisterType ( processor , instructions , type ) ;
267+ var receiveMethod = GetNetworkMessageRecieveHandler ( type ) ;
268+ if ( receiveMethod == null )
269+ {
270+ continue ;
271+ }
272+ CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod ) ;
231273 }
232274
233275 instructions . ForEach ( instruction => processor . Body . Instructions . Insert ( processor . Body . Instructions . Count - 1 , instruction ) ) ;
0 commit comments