44using Cecilifier . Runtime ;
55using Mono . Cecil ;
66using Mono . Cecil . Cil ;
7- using Mono . Cecil . Rocks ;
87using Unity . CompilationPipeline . Common . Diagnostics ;
98using Unity . CompilationPipeline . Common . ILPostProcessing ;
109using Unity . Netcode . Editor . CodeGen ;
@@ -18,24 +17,30 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
1817 public static readonly string AttributeNamespaceSuffix = "NetcodePatcher" ;
1918
2019 public static readonly string AttributeName = "NetcodePatchedAssemblyAttribute" ;
21-
22- public override ILPostProcessor GetInstance ( ) => this ;
23-
24- public override bool WillProcess ( ICompiledAssembly compiledAssembly ) => true ;
2520
2621 private readonly List < DiagnosticMessage > m_Diagnostics = [ ] ;
2722 private PostProcessorAssemblyResolver m_AssemblyResolver ;
28-
23+
24+ public override ILPostProcessor GetInstance ( )
25+ {
26+ return this ;
27+ }
28+
29+ public override bool WillProcess ( ICompiledAssembly compiledAssembly )
30+ {
31+ return true ;
32+ }
33+
2934 // This function's implementation was written with the help of https://cecilifier.me/
3035 public override ILPostProcessResult ? Process ( ICompiledAssembly compiledAssembly )
3136 {
3237 if ( ! WillProcess ( compiledAssembly ) ) return null ;
33-
38+
3439 m_Diagnostics . Clear ( ) ;
35-
36- // read
40+
41+ // read
3742 var assemblyDefinition = CodeGenHelpers . AssemblyDefinitionFor ( compiledAssembly , out m_AssemblyResolver ) ;
38- if ( assemblyDefinition == null )
43+ if ( assemblyDefinition is null )
3944 {
4045 m_Diagnostics . AddError ( $ "Cannot read assembly definition: { compiledAssembly . Name } ") ;
4146 return null ;
@@ -49,25 +54,42 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
4954 assemblyDefinition . MainModule . ImportReference ( typeof ( Attribute ) )
5055 ) ;
5156 assemblyDefinition . MainModule . Types . Add ( cls_NetcodePatchedAttribute ) ;
52-
57+
5358 // Add AttributeUsage(AttributeTargets.Assembly) to NetcodePatchedAttribute
54- var attr_AttributeUsage = new CustomAttribute ( assemblyDefinition . MainModule . ImportReference ( typeof ( AttributeUsageAttribute ) . GetConstructor ( [ typeof ( AttributeTargets ) ] ) ) ) ;
55- attr_AttributeUsage . ConstructorArguments . Add ( new CustomAttributeArgument ( assemblyDefinition . MainModule . ImportReference ( typeof ( AttributeTargets ) ) , 4 ) ) ;
59+ var attr_AttributeUsage = new CustomAttribute (
60+ assemblyDefinition . MainModule . ImportReference (
61+ typeof ( AttributeUsageAttribute ) . GetConstructor ( [ typeof ( AttributeTargets ) ] )
62+ )
63+ ) ;
64+ attr_AttributeUsage . ConstructorArguments . Add (
65+ new CustomAttributeArgument ( assemblyDefinition . MainModule . ImportReference ( typeof ( AttributeTargets ) ) , 4 )
66+ ) ;
5667 cls_NetcodePatchedAttribute . CustomAttributes . Add ( attr_AttributeUsage ) ;
57-
68+
5869 // Method : NetcodePatchedAttribute.ctor
59- var ctor_NetcodePatchedAttribute = new MethodDefinition ( ".ctor" , MethodAttributes . Assembly | MethodAttributes . RTSpecialName | MethodAttributes . SpecialName | MethodAttributes . HideBySig , assemblyDefinition . MainModule . TypeSystem . Void ) ;
70+ var ctor_NetcodePatchedAttribute = new MethodDefinition (
71+ ".ctor" ,
72+ MethodAttributes . Assembly | MethodAttributes . RTSpecialName | MethodAttributes . SpecialName |
73+ MethodAttributes . HideBySig , assemblyDefinition . MainModule . TypeSystem . Void
74+ ) ;
6075 cls_NetcodePatchedAttribute . Methods . Add ( ctor_NetcodePatchedAttribute ) ;
6176 ctor_NetcodePatchedAttribute . Body . InitLocals = true ;
6277 var il_ctor_NetcodePatchedAttribute = ctor_NetcodePatchedAttribute . Body . GetILProcessor ( ) ;
6378 il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Ldarg_0 ) ;
64- il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Call , assemblyDefinition . MainModule . ImportReference ( TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute . BaseType ) ) ) ;
79+ il_ctor_NetcodePatchedAttribute . Emit (
80+ OpCodes . Call ,
81+ assemblyDefinition . MainModule . ImportReference (
82+ TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute . BaseType )
83+ )
84+ ) ;
6585 il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Ret ) ;
66-
86+
6787 // Add NetcodePatchedAttribute to assembly definition
68- var attribute = new CustomAttribute ( assemblyDefinition . MainModule . ImportReference ( TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute ) ) ) ;
88+ var attribute = new CustomAttribute (
89+ assemblyDefinition . MainModule . ImportReference ( TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute ) )
90+ ) ;
6991 assemblyDefinition . CustomAttributes . Add ( attribute ) ;
70-
92+
7193 // write
7294 var pe = new MemoryStream ( ) ;
7395 var pdb = new MemoryStream ( ) ;
@@ -80,7 +102,7 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
80102 } ;
81103
82104 assemblyDefinition . Write ( pe , writerParameters ) ;
83-
105+
84106 return new ILPostProcessResult ( new InMemoryAssembly ( pe . ToArray ( ) , pdb . ToArray ( ) ) , m_Diagnostics ) ;
85107 }
86- }
108+ }
0 commit comments