Skip to content

Commit 7ae8296

Browse files
authored
fix: more CoreCLR ILPP fixes (#2656)
1 parent 81fd4ef commit 7ae8296

File tree

2 files changed

+176
-203
lines changed

2 files changed

+176
-203
lines changed

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

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Mono.Cecil.Rocks;
88
using Unity.CompilationPipeline.Common.Diagnostics;
99
using Unity.CompilationPipeline.Common.ILPostProcessing;
10+
using UnityEngine;
1011
using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor;
1112
using MethodAttributes = Mono.Cecil.MethodAttributes;
1213

@@ -101,6 +102,8 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
101102
private ModuleDefinition m_NetcodeModule;
102103
private PostProcessorAssemblyResolver m_AssemblyResolver;
103104

105+
private MethodReference m_RuntimeInitializeOnLoadAttribute_Ctor;
106+
104107
private MethodReference m_MessageManager_ReceiveMessage_MethodRef;
105108
private MethodReference m_MessageManager_CreateMessageAndGetVersion_MethodRef;
106109
private TypeReference m_MessageManager_MessageWithHandler_TypeRef;
@@ -125,6 +128,7 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
125128
// (i.e., there's no #if UNITY_EDITOR in them that could create invalid IL code)
126129
TypeDefinition typeTypeDef = moduleDefinition.ImportReference(typeof(Type)).Resolve();
127130
TypeDefinition listTypeDef = moduleDefinition.ImportReference(typeof(List<>)).Resolve();
131+
m_RuntimeInitializeOnLoadAttribute_Ctor = moduleDefinition.ImportReference(typeof(RuntimeInitializeOnLoadMethodAttribute).GetConstructor(new Type[] { }));
128132

129133
TypeDefinition messageHandlerTypeDef = null;
130134
TypeDefinition versionGetterTypeDef = null;
@@ -232,25 +236,6 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
232236
return true;
233237
}
234238

235-
private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinition)
236-
{
237-
var staticCtorMethodDef = typeDefinition.GetStaticConstructor();
238-
if (staticCtorMethodDef == null)
239-
{
240-
staticCtorMethodDef = new MethodDefinition(
241-
".cctor", // Static Constructor (constant-constructor)
242-
MethodAttributes.HideBySig |
243-
MethodAttributes.SpecialName |
244-
MethodAttributes.RTSpecialName |
245-
MethodAttributes.Static,
246-
typeDefinition.Module.TypeSystem.Void);
247-
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
248-
typeDefinition.Methods.Add(staticCtorMethodDef);
249-
}
250-
251-
return staticCtorMethodDef;
252-
}
253-
254239
private void CreateInstructionsToRegisterType(ILProcessor processor, List<Instruction> instructions, TypeReference type, MethodReference receiveMethod, MethodReference versionMethod)
255240
{
256241
// NetworkMessageManager.__network_message_types.Add(new NetworkMessageManager.MessageWithHandler{MessageType=typeof(type), Handler=type.Receive});
@@ -295,29 +280,32 @@ private void CreateInstructionsToRegisterType(ILProcessor processor, List<Instru
295280
// https://web.archive.org/web/20100212140402/http://blogs.msdn.com/junfeng/archive/2005/11/19/494914.aspx
296281
private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeDefinition> networkMessageTypes)
297282
{
298-
foreach (var typeDefinition in assembly.MainModule.Types)
299-
{
300-
if (typeDefinition.FullName == "<Module>")
301-
{
302-
var staticCtorMethodDef = GetOrCreateStaticConstructor(typeDefinition);
303-
304-
var processor = staticCtorMethodDef.Body.GetILProcessor();
283+
var typeDefinition = new TypeDefinition("__GEN", "INetworkMessageHelper", TypeAttributes.NotPublic | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, assembly.MainModule.TypeSystem.Object);
305284

306-
var instructions = new List<Instruction>();
285+
var staticCtorMethodDef = new MethodDefinition(
286+
$"InitializeMessages",
287+
MethodAttributes.Assembly |
288+
MethodAttributes.Static,
289+
assembly.MainModule.TypeSystem.Void);
290+
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
291+
staticCtorMethodDef.CustomAttributes.Add(new CustomAttribute(m_RuntimeInitializeOnLoadAttribute_Ctor));
292+
typeDefinition.Methods.Add(staticCtorMethodDef);
307293

308-
foreach (var type in networkMessageTypes)
309-
{
310-
var receiveMethod = new GenericInstanceMethod(m_MessageManager_ReceiveMessage_MethodRef);
311-
receiveMethod.GenericArguments.Add(type);
312-
var versionMethod = new GenericInstanceMethod(m_MessageManager_CreateMessageAndGetVersion_MethodRef);
313-
versionMethod.GenericArguments.Add(type);
314-
CreateInstructionsToRegisterType(processor, instructions, type, receiveMethod, versionMethod);
315-
}
294+
var instructions = new List<Instruction>();
295+
var processor = staticCtorMethodDef.Body.GetILProcessor();
316296

317-
instructions.ForEach(instruction => processor.Body.Instructions.Insert(processor.Body.Instructions.Count - 1, instruction));
318-
break;
319-
}
297+
foreach (var type in networkMessageTypes)
298+
{
299+
var receiveMethod = new GenericInstanceMethod(m_MessageManager_ReceiveMessage_MethodRef);
300+
receiveMethod.GenericArguments.Add(type);
301+
var versionMethod = new GenericInstanceMethod(m_MessageManager_CreateMessageAndGetVersion_MethodRef);
302+
versionMethod.GenericArguments.Add(type);
303+
CreateInstructionsToRegisterType(processor, instructions, type, receiveMethod, versionMethod);
320304
}
305+
306+
instructions.ForEach(instruction => processor.Body.Instructions.Insert(processor.Body.Instructions.Count - 1, instruction));
307+
308+
assembly.MainModule.Types.Add(typeDefinition);
321309
}
322310
}
323311
}

0 commit comments

Comments
 (0)