Skip to content

Commit 850c7b6

Browse files
authored
refactor: NetworkMessageSystem (formerly MessagingSystem) (#2505)
1 parent 449bf94 commit 850c7b6

File tree

56 files changed

+766
-780
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+766
-780
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/INetworkMessageILPP.cs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -101,54 +101,51 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
101101
private ModuleDefinition m_NetcodeModule;
102102
private PostProcessorAssemblyResolver m_AssemblyResolver;
103103

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;
109109
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;
113113
private MethodReference m_Type_GetTypeFromHandle_MethodRef;
114114
private MethodReference m_List_Add_MethodRef;
115115

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);
118118

119119
private bool ImportReferences(ModuleDefinition moduleDefinition)
120120
{
121121
// 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)
129126
TypeDefinition typeTypeDef = moduleDefinition.ImportReference(typeof(Type)).Resolve();
130127
TypeDefinition listTypeDef = moduleDefinition.ImportReference(typeof(List<>)).Resolve();
131128

132129
TypeDefinition messageHandlerTypeDef = null;
133130
TypeDefinition versionGetterTypeDef = null;
134131
TypeDefinition messageWithHandlerTypeDef = null;
135132
TypeDefinition ilppMessageProviderTypeDef = null;
136-
TypeDefinition messagingSystemTypeDef = null;
133+
TypeDefinition messageManagerSystemTypeDef = null;
137134
foreach (var netcodeTypeDef in m_NetcodeModule.GetAllTypes())
138135
{
139-
if (messageHandlerTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem.MessageHandler))
136+
if (messageHandlerTypeDef == null && netcodeTypeDef.Name == nameof(NetworkMessageManager.MessageHandler))
140137
{
141138
messageHandlerTypeDef = netcodeTypeDef;
142139
continue;
143140
}
144141

145-
if (versionGetterTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem.VersionGetter))
142+
if (versionGetterTypeDef == null && netcodeTypeDef.Name == nameof(NetworkMessageManager.VersionGetter))
146143
{
147144
versionGetterTypeDef = netcodeTypeDef;
148145
continue;
149146
}
150147

151-
if (messageWithHandlerTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem.MessageWithHandler))
148+
if (messageWithHandlerTypeDef == null && netcodeTypeDef.Name == nameof(NetworkMessageManager.MessageWithHandler))
152149
{
153150
messageWithHandlerTypeDef = netcodeTypeDef;
154151
continue;
@@ -160,29 +157,29 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
160157
continue;
161158
}
162159

163-
if (messagingSystemTypeDef == null && netcodeTypeDef.Name == nameof(MessagingSystem))
160+
if (messageManagerSystemTypeDef == null && netcodeTypeDef.Name == nameof(NetworkMessageManager))
164161
{
165-
messagingSystemTypeDef = netcodeTypeDef;
162+
messageManagerSystemTypeDef = netcodeTypeDef;
166163
continue;
167164
}
168165
}
169166

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());
172169

173-
m_MessagingSystem_MessageWithHandler_TypeRef = moduleDefinition.ImportReference(messageWithHandlerTypeDef);
170+
m_MessageManager_MessageWithHandler_TypeRef = moduleDefinition.ImportReference(messageWithHandlerTypeDef);
174171
foreach (var fieldDef in messageWithHandlerTypeDef.Fields)
175172
{
176173
switch (fieldDef.Name)
177174
{
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);
180177
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);
183180
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);
186183
break;
187184
}
188185
}
@@ -219,15 +216,15 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
219216
}
220217
}
221218

222-
foreach (var methodDef in messagingSystemTypeDef.Methods)
219+
foreach (var methodDef in messageManagerSystemTypeDef.Methods)
223220
{
224221
switch (methodDef.Name)
225222
{
226223
case k_ReceiveMessageName:
227-
m_MessagingSystem_ReceiveMessage_MethodRef = moduleDefinition.ImportReference(methodDef);
224+
m_MessageManager_ReceiveMessage_MethodRef = moduleDefinition.ImportReference(methodDef);
228225
break;
229226
case k_CreateMessageAndGetVersionName:
230-
m_MessagingSystem_CreateMessageAndGetVersion_MethodRef = moduleDefinition.ImportReference(methodDef);
227+
m_MessageManager_CreateMessageAndGetVersion_MethodRef = moduleDefinition.ImportReference(methodDef);
231228
break;
232229
}
233230
}
@@ -256,43 +253,43 @@ private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinit
256253

257254
private void CreateInstructionsToRegisterType(ILProcessor processor, List<Instruction> instructions, TypeReference type, MethodReference receiveMethod, MethodReference versionMethod)
258255
{
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));
261258
int messageWithHandlerLocIdx = processor.Body.Variables.Count - 1;
262259

263260
instructions.Add(processor.Create(OpCodes.Ldsfld, m_ILPPMessageProvider___network_message_types_FieldRef));
264261
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));
266263

267264
// tmp.MessageType = typeof(type);
268265
instructions.Add(processor.Create(OpCodes.Ldloca, messageWithHandlerLocIdx));
269266
instructions.Add(processor.Create(OpCodes.Ldtoken, type));
270267
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));
272269

273270
// tmp.Handler = MessageHandler.ReceveMessage<type>
274271
instructions.Add(processor.Create(OpCodes.Ldloca, messageWithHandlerLocIdx));
275272
instructions.Add(processor.Create(OpCodes.Ldnull));
276273

277274
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));
280277

281278

282279
// tmp.GetVersion = MessageHandler.CreateMessageAndGetVersion<type>
283280
instructions.Add(processor.Create(OpCodes.Ldloca, messageWithHandlerLocIdx));
284281
instructions.Add(processor.Create(OpCodes.Ldnull));
285282

286283
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));
289286

290287
// ILPPMessageProvider.__network_message_types.Add(tmp);
291288
instructions.Add(processor.Create(OpCodes.Ldloc, messageWithHandlerLocIdx));
292289
instructions.Add(processor.Create(OpCodes.Callvirt, m_List_Add_MethodRef));
293290
}
294291

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.
296293
// 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).
297294
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-5.0
298295
// 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
310307

311308
foreach (var type in networkMessageTypes)
312309
{
313-
var receiveMethod = new GenericInstanceMethod(m_MessagingSystem_ReceiveMessage_MethodRef);
310+
var receiveMethod = new GenericInstanceMethod(m_MessageManager_ReceiveMessage_MethodRef);
314311
receiveMethod.GenericArguments.Add(type);
315-
var versionMethod = new GenericInstanceMethod(m_MessagingSystem_CreateMessageAndGetVersion_MethodRef);
312+
var versionMethod = new GenericInstanceMethod(m_MessageManager_CreateMessageAndGetVersion_MethodRef);
316313
versionMethod.GenericArguments.Add(type);
317314
CreateInstructionsToRegisterType(processor, instructions, type, receiveMethod, versionMethod);
318315
}

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public class NetworkConfig
156156
public string ToBase64()
157157
{
158158
NetworkConfig config = this;
159-
var writer = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp);
159+
var writer = new FastBufferWriter(NetworkMessageManager.NonFragmentedMessageMaxSize, Allocator.Temp);
160160
using (writer)
161161
{
162162
writer.WriteValueSafe(config.ProtocolVersion);
@@ -228,7 +228,7 @@ public ulong GetConfig(bool cache = true)
228228
return m_ConfigHash.Value;
229229
}
230230

231-
var writer = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, int.MaxValue);
231+
var writer = new FastBufferWriter(NetworkMessageManager.NonFragmentedMessageMaxSize, Allocator.Temp, int.MaxValue);
232232
using (writer)
233233
{
234234
writer.WriteValueSafe(ProtocolVersion);

0 commit comments

Comments
 (0)