11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4- using System . Linq ;
4+ using Cecilifier . Runtime ;
55using Mono . Cecil ;
66using Mono . Cecil . Cil ;
77using Mono . Cecil . Rocks ;
@@ -25,7 +25,8 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
2525
2626 private readonly List < DiagnosticMessage > m_Diagnostics = [ ] ;
2727 private PostProcessorAssemblyResolver m_AssemblyResolver ;
28-
28+
29+ // This function's implementation was written with the help of https://cecilifier.me/
2930 public override ILPostProcessResult ? Process ( ICompiledAssembly compiledAssembly )
3031 {
3132 if ( ! WillProcess ( compiledAssembly ) ) return null ;
@@ -39,41 +40,32 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
3940 m_Diagnostics . AddError ( $ "Cannot read assembly definition: { compiledAssembly . Name } ") ;
4041 return null ;
4142 }
42-
43- // do stuff
44- var patchedAttributeDefinition = new TypeDefinition (
43+
44+ // Class : NetcodePatchedAttribute
45+ var cls_NetcodePatchedAttribute = new TypeDefinition (
4546 $ "{ assemblyDefinition . Name . Name } .{ AttributeNamespaceSuffix } ",
4647 AttributeName ,
47- TypeAttributes . NestedPrivate ,
48+ TypeAttributes . AnsiClass | TypeAttributes . BeforeFieldInit | TypeAttributes . NotPublic ,
4849 assemblyDefinition . MainModule . ImportReference ( typeof ( Attribute ) )
4950 ) ;
50-
51- var attributeUsageAttributeConstructor =
52- assemblyDefinition . MainModule . ImportReference (
53- typeof ( AttributeUsageAttribute ) . GetConstructor ( [ typeof ( AttributeTargets ) ] )
54- ) ;
55- var attributeUsageAttribute = new CustomAttribute ( attributeUsageAttributeConstructor ) ;
56- attributeUsageAttribute . ConstructorArguments . Add (
57- new CustomAttributeArgument ( assemblyDefinition . MainModule . ImportReference ( typeof ( AttributeTargets ) ) , AttributeTargets . Assembly )
58- ) ;
59- patchedAttributeDefinition . CustomAttributes . Add ( attributeUsageAttribute ) ;
51+ assemblyDefinition . MainModule . Types . Add ( cls_NetcodePatchedAttribute ) ;
6052
61- var methodAttributes = MethodAttributes . Assembly | MethodAttributes . HideBySig | MethodAttributes . SpecialName | MethodAttributes . RTSpecialName ;
62- var method = new MethodDefinition ( ".ctor" , methodAttributes , assemblyDefinition . MainModule . TypeSystem . Void ) ;
63- method . Body . Instructions . Add ( Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
64- var baseCtorReference = new MethodReference ( ".ctor" , assemblyDefinition . MainModule . TypeSystem . Void , patchedAttributeDefinition . BaseType ) { HasThis = true } ;
65- method . Body . Instructions . Add ( Instruction . Create ( OpCodes . Call , baseCtorReference ) ) ;
66- method . Body . Instructions . Add ( Instruction . Create ( OpCodes . Ret ) ) ;
67- patchedAttributeDefinition . Methods . Add ( method ) ;
53+ // 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 ) ) ;
56+ cls_NetcodePatchedAttribute . CustomAttributes . Add ( attr_AttributeUsage ) ;
6857
69- assemblyDefinition . MainModule . Types . Add ( patchedAttributeDefinition ) ;
58+ // Method : NetcodePatchedAttribute.ctor
59+ var ctor_NetcodePatchedAttribute = new MethodDefinition ( ".ctor" , MethodAttributes . Assembly | MethodAttributes . RTSpecialName | MethodAttributes . SpecialName | MethodAttributes . HideBySig , assemblyDefinition . MainModule . TypeSystem . Void ) ;
60+ cls_NetcodePatchedAttribute . Methods . Add ( ctor_NetcodePatchedAttribute ) ;
61+ ctor_NetcodePatchedAttribute . Body . InitLocals = true ;
62+ var il_ctor_NetcodePatchedAttribute = ctor_NetcodePatchedAttribute . Body . GetILProcessor ( ) ;
63+ il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Ldarg_0 ) ;
64+ il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Call , assemblyDefinition . MainModule . ImportReference ( TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute . BaseType ) ) ) ;
65+ il_ctor_NetcodePatchedAttribute . Emit ( OpCodes . Ret ) ;
7066
71- var attributeConstructor = assemblyDefinition . MainModule
72- . ImportReference ( patchedAttributeDefinition )
73- . Resolve ( )
74- . GetConstructors ( )
75- . First ( ) ;
76- var attribute = new CustomAttribute ( attributeConstructor ) ;
67+ // Add NetcodePatchedAttribute to assembly definition
68+ var attribute = new CustomAttribute ( assemblyDefinition . MainModule . ImportReference ( TypeHelpers . DefaultCtorFor ( cls_NetcodePatchedAttribute ) ) ) ;
7769 assemblyDefinition . CustomAttributes . Add ( attribute ) ;
7870
7971 // write
0 commit comments