@@ -101,54 +101,51 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
101
101
private ModuleDefinition m_NetcodeModule ;
102
102
private PostProcessorAssemblyResolver m_AssemblyResolver ;
103
103
104
- private MethodReference m_MessagingSystem_ReceiveMessage_MethodRef ;
105
- private MethodReference m_MessagingSystem_CreateMessageAndGetVersion_MethodRef ;
106
- private TypeReference m_MessagingSystem_MessageWithHandler_TypeRef ;
107
- private MethodReference m_MessagingSystem_MessageHandler_Constructor_TypeRef ;
108
- private MethodReference m_MessagingSystem_VersionGetter_Constructor_TypeRef ;
104
+ private MethodReference m_MessageManager_ReceiveMessage_MethodRef ;
105
+ private MethodReference m_MessageManager_CreateMessageAndGetVersion_MethodRef ;
106
+ private TypeReference m_MessageManager_MessageWithHandler_TypeRef ;
107
+ private MethodReference m_MessageManager_MessageHandler_Constructor_TypeRef ;
108
+ private MethodReference m_MessageManager_VersionGetter_Constructor_TypeRef ;
109
109
private FieldReference m_ILPPMessageProvider___network_message_types_FieldRef ;
110
- private FieldReference m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ;
111
- private FieldReference m_MessagingSystem_MessageWithHandler_Handler_FieldRef ;
112
- private FieldReference m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef ;
110
+ private FieldReference m_MessageManager_MessageWithHandler_MessageType_FieldRef ;
111
+ private FieldReference m_MessageManager_MessageWithHandler_Handler_FieldRef ;
112
+ private FieldReference m_MessageManager_MessageWithHandler_GetVersion_FieldRef ;
113
113
private MethodReference m_Type_GetTypeFromHandle_MethodRef ;
114
114
private MethodReference m_List_Add_MethodRef ;
115
115
116
- private const string k_ReceiveMessageName = nameof ( MessagingSystem . ReceiveMessage ) ;
117
- private const string k_CreateMessageAndGetVersionName = nameof ( MessagingSystem . CreateMessageAndGetVersion ) ;
116
+ private const string k_ReceiveMessageName = nameof ( NetworkMessageManager . ReceiveMessage ) ;
117
+ private const string k_CreateMessageAndGetVersionName = nameof ( NetworkMessageManager . CreateMessageAndGetVersion ) ;
118
118
119
119
private bool ImportReferences ( ModuleDefinition moduleDefinition )
120
120
{
121
121
// Different environments seem to have different situations...
122
- // Some have these definitions in netstandard.dll...
123
- // some seem to have them elsewhere...
124
- // Since they're standard .net classes they're not going to cause
125
- // the same issues as referencing other assemblies, in theory, since
126
- // the definitions should be standard and consistent across platforms
127
- // (i.e., there's no #if UNITY_EDITOR in them that could create
128
- // invalid IL code)
122
+ // Some have these definitions in netstandard.dll, some seem to have them elsewhere...
123
+ // Since they're standard .net classes they're not going to cause the same issues as referencing other assemblies,
124
+ // in theory, since the definitions should be standard and consistent across platforms
125
+ // (i.e., there's no #if UNITY_EDITOR in them that could create invalid IL code)
129
126
TypeDefinition typeTypeDef = moduleDefinition . ImportReference ( typeof ( Type ) ) . Resolve ( ) ;
130
127
TypeDefinition listTypeDef = moduleDefinition . ImportReference ( typeof ( List < > ) ) . Resolve ( ) ;
131
128
132
129
TypeDefinition messageHandlerTypeDef = null ;
133
130
TypeDefinition versionGetterTypeDef = null ;
134
131
TypeDefinition messageWithHandlerTypeDef = null ;
135
132
TypeDefinition ilppMessageProviderTypeDef = null ;
136
- TypeDefinition messagingSystemTypeDef = null ;
133
+ TypeDefinition messageManagerSystemTypeDef = null ;
137
134
foreach ( var netcodeTypeDef in m_NetcodeModule . GetAllTypes ( ) )
138
135
{
139
- if ( messageHandlerTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem . MessageHandler ) )
136
+ if ( messageHandlerTypeDef == null && netcodeTypeDef . Name == nameof ( NetworkMessageManager . MessageHandler ) )
140
137
{
141
138
messageHandlerTypeDef = netcodeTypeDef ;
142
139
continue ;
143
140
}
144
141
145
- if ( versionGetterTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem . VersionGetter ) )
142
+ if ( versionGetterTypeDef == null && netcodeTypeDef . Name == nameof ( NetworkMessageManager . VersionGetter ) )
146
143
{
147
144
versionGetterTypeDef = netcodeTypeDef ;
148
145
continue ;
149
146
}
150
147
151
- if ( messageWithHandlerTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem . MessageWithHandler ) )
148
+ if ( messageWithHandlerTypeDef == null && netcodeTypeDef . Name == nameof ( NetworkMessageManager . MessageWithHandler ) )
152
149
{
153
150
messageWithHandlerTypeDef = netcodeTypeDef ;
154
151
continue ;
@@ -160,29 +157,29 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
160
157
continue ;
161
158
}
162
159
163
- if ( messagingSystemTypeDef == null && netcodeTypeDef . Name == nameof ( MessagingSystem ) )
160
+ if ( messageManagerSystemTypeDef == null && netcodeTypeDef . Name == nameof ( NetworkMessageManager ) )
164
161
{
165
- messagingSystemTypeDef = netcodeTypeDef ;
162
+ messageManagerSystemTypeDef = netcodeTypeDef ;
166
163
continue ;
167
164
}
168
165
}
169
166
170
- m_MessagingSystem_MessageHandler_Constructor_TypeRef = moduleDefinition . ImportReference ( messageHandlerTypeDef . GetConstructors ( ) . First ( ) ) ;
171
- m_MessagingSystem_VersionGetter_Constructor_TypeRef = moduleDefinition . ImportReference ( versionGetterTypeDef . GetConstructors ( ) . First ( ) ) ;
167
+ m_MessageManager_MessageHandler_Constructor_TypeRef = moduleDefinition . ImportReference ( messageHandlerTypeDef . GetConstructors ( ) . First ( ) ) ;
168
+ m_MessageManager_VersionGetter_Constructor_TypeRef = moduleDefinition . ImportReference ( versionGetterTypeDef . GetConstructors ( ) . First ( ) ) ;
172
169
173
- m_MessagingSystem_MessageWithHandler_TypeRef = moduleDefinition . ImportReference ( messageWithHandlerTypeDef ) ;
170
+ m_MessageManager_MessageWithHandler_TypeRef = moduleDefinition . ImportReference ( messageWithHandlerTypeDef ) ;
174
171
foreach ( var fieldDef in messageWithHandlerTypeDef . Fields )
175
172
{
176
173
switch ( fieldDef . Name )
177
174
{
178
- case nameof ( MessagingSystem . MessageWithHandler . MessageType ) :
179
- m_MessagingSystem_MessageWithHandler_MessageType_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
175
+ case nameof ( NetworkMessageManager . MessageWithHandler . MessageType ) :
176
+ m_MessageManager_MessageWithHandler_MessageType_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
180
177
break ;
181
- case nameof ( MessagingSystem . MessageWithHandler . Handler ) :
182
- m_MessagingSystem_MessageWithHandler_Handler_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
178
+ case nameof ( NetworkMessageManager . MessageWithHandler . Handler ) :
179
+ m_MessageManager_MessageWithHandler_Handler_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
183
180
break ;
184
- case nameof ( MessagingSystem . MessageWithHandler . GetVersion ) :
185
- m_MessagingSystem_MessageWithHandler_GetVersion_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
181
+ case nameof ( NetworkMessageManager . MessageWithHandler . GetVersion ) :
182
+ m_MessageManager_MessageWithHandler_GetVersion_FieldRef = moduleDefinition . ImportReference ( fieldDef ) ;
186
183
break ;
187
184
}
188
185
}
@@ -219,15 +216,15 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
219
216
}
220
217
}
221
218
222
- foreach ( var methodDef in messagingSystemTypeDef . Methods )
219
+ foreach ( var methodDef in messageManagerSystemTypeDef . Methods )
223
220
{
224
221
switch ( methodDef . Name )
225
222
{
226
223
case k_ReceiveMessageName :
227
- m_MessagingSystem_ReceiveMessage_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
224
+ m_MessageManager_ReceiveMessage_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
228
225
break ;
229
226
case k_CreateMessageAndGetVersionName :
230
- m_MessagingSystem_CreateMessageAndGetVersion_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
227
+ m_MessageManager_CreateMessageAndGetVersion_MethodRef = moduleDefinition . ImportReference ( methodDef ) ;
231
228
break ;
232
229
}
233
230
}
@@ -256,43 +253,43 @@ private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinit
256
253
257
254
private void CreateInstructionsToRegisterType ( ILProcessor processor , List < Instruction > instructions , TypeReference type , MethodReference receiveMethod , MethodReference versionMethod )
258
255
{
259
- // MessagingSystem .__network_message_types.Add(new MessagingSystem .MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
260
- processor . Body . Variables . Add ( new VariableDefinition ( m_MessagingSystem_MessageWithHandler_TypeRef ) ) ;
256
+ // NetworkMessageManager .__network_message_types.Add(new NetworkMessageManager .MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
257
+ processor . Body . Variables . Add ( new VariableDefinition ( m_MessageManager_MessageWithHandler_TypeRef ) ) ;
261
258
int messageWithHandlerLocIdx = processor . Body . Variables . Count - 1 ;
262
259
263
260
instructions . Add ( processor . Create ( OpCodes . Ldsfld , m_ILPPMessageProvider___network_message_types_FieldRef ) ) ;
264
261
instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
265
- instructions . Add ( processor . Create ( OpCodes . Initobj , m_MessagingSystem_MessageWithHandler_TypeRef ) ) ;
262
+ instructions . Add ( processor . Create ( OpCodes . Initobj , m_MessageManager_MessageWithHandler_TypeRef ) ) ;
266
263
267
264
// tmp.MessageType = typeof(type);
268
265
instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
269
266
instructions . Add ( processor . Create ( OpCodes . Ldtoken , type ) ) ;
270
267
instructions . Add ( processor . Create ( OpCodes . Call , m_Type_GetTypeFromHandle_MethodRef ) ) ;
271
- instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_MessageType_FieldRef ) ) ;
268
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessageManager_MessageWithHandler_MessageType_FieldRef ) ) ;
272
269
273
270
// tmp.Handler = MessageHandler.ReceveMessage<type>
274
271
instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
275
272
instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
276
273
277
274
instructions . Add ( processor . Create ( OpCodes . Ldftn , receiveMethod ) ) ;
278
- instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessagingSystem_MessageHandler_Constructor_TypeRef ) ) ;
279
- instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessagingSystem_MessageWithHandler_Handler_FieldRef ) ) ;
275
+ instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessageManager_MessageHandler_Constructor_TypeRef ) ) ;
276
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessageManager_MessageWithHandler_Handler_FieldRef ) ) ;
280
277
281
278
282
279
// tmp.GetVersion = MessageHandler.CreateMessageAndGetVersion<type>
283
280
instructions . Add ( processor . Create ( OpCodes . Ldloca , messageWithHandlerLocIdx ) ) ;
284
281
instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
285
282
286
283
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 ) ) ;
284
+ instructions . Add ( processor . Create ( OpCodes . Newobj , m_MessageManager_VersionGetter_Constructor_TypeRef ) ) ;
285
+ instructions . Add ( processor . Create ( OpCodes . Stfld , m_MessageManager_MessageWithHandler_GetVersion_FieldRef ) ) ;
289
286
290
287
// ILPPMessageProvider.__network_message_types.Add(tmp);
291
288
instructions . Add ( processor . Create ( OpCodes . Ldloc , messageWithHandlerLocIdx ) ) ;
292
289
instructions . Add ( processor . Create ( OpCodes . Callvirt , m_List_Add_MethodRef ) ) ;
293
290
}
294
291
295
- // Creates a static module constructor (which is executed when the module is loaded) that registers all the message types in the assembly with MessagingSystem .
292
+ // Creates a static module constructor (which is executed when the module is loaded) that registers all the message types in the assembly with NetworkMessageManager .
296
293
// This is the same behavior as annotating a static method with [ModuleInitializer] in standardized C# (that attribute doesn't exist in Unity, but the static module constructor still works).
297
294
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-5.0
298
295
// https://web.archive.org/web/20100212140402/http://blogs.msdn.com/junfeng/archive/2005/11/19/494914.aspx
@@ -310,9 +307,9 @@ private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeDefin
310
307
311
308
foreach ( var type in networkMessageTypes )
312
309
{
313
- var receiveMethod = new GenericInstanceMethod ( m_MessagingSystem_ReceiveMessage_MethodRef ) ;
310
+ var receiveMethod = new GenericInstanceMethod ( m_MessageManager_ReceiveMessage_MethodRef ) ;
314
311
receiveMethod . GenericArguments . Add ( type ) ;
315
- var versionMethod = new GenericInstanceMethod ( m_MessagingSystem_CreateMessageAndGetVersion_MethodRef ) ;
312
+ var versionMethod = new GenericInstanceMethod ( m_MessageManager_CreateMessageAndGetVersion_MethodRef ) ;
316
313
versionMethod . GenericArguments . Add ( type ) ;
317
314
CreateInstructionsToRegisterType ( processor , instructions , type , receiveMethod , versionMethod ) ;
318
315
}
0 commit comments