11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . Linq ;
45using Mono . Cecil ;
56using Mono . Cecil . Cil ;
6- using NetcodePatcher . Attributes ;
7+ using Mono . Cecil . Rocks ;
78using Unity . CompilationPipeline . Common . Diagnostics ;
89using Unity . CompilationPipeline . Common . ILPostProcessing ;
910using Unity . Netcode . Editor . CodeGen ;
11+ using MethodAttributes = Mono . Cecil . MethodAttributes ;
12+ using TypeAttributes = Mono . Cecil . TypeAttributes ;
1013
1114namespace NetcodePatcher . CodeGen ;
1215
1316public class ApplyPatchedAttributeILPP : ILPostProcessor
1417{
18+ public static readonly string AttributeNamespaceSuffix = "NetcodePatcher" ;
19+
20+ public static readonly string AttributeName = "NetcodePatchedAssemblyAttribute" ;
21+
1522 public override ILPostProcessor GetInstance ( ) => this ;
1623
1724 public override bool WillProcess ( ICompiledAssembly compiledAssembly ) => true ;
@@ -34,9 +41,38 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
3441 }
3542
3643 // do stuff
37- var attributeConstructor =
44+ var patchedAttributeDefinition = new TypeDefinition (
45+ $ "{ assemblyDefinition . Name . Name } .{ AttributeNamespaceSuffix } ",
46+ AttributeName ,
47+ TypeAttributes . NestedPrivate ,
48+ assemblyDefinition . MainModule . ImportReference ( typeof ( Attribute ) )
49+ ) ;
50+
51+ var attributeUsageAttributeConstructor =
3852 assemblyDefinition . MainModule . ImportReference (
39- typeof ( NetcodePatchedAssemblyAttribute ) . GetConstructor ( Type . EmptyTypes ) ) ;
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 ) ;
60+
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 ) ;
68+
69+ assemblyDefinition . MainModule . Types . Add ( patchedAttributeDefinition ) ;
70+
71+ var attributeConstructor = assemblyDefinition . MainModule
72+ . ImportReference ( patchedAttributeDefinition )
73+ . Resolve ( )
74+ . GetConstructors ( )
75+ . First ( ) ;
4076 var attribute = new CustomAttribute ( attributeConstructor ) ;
4177 assemblyDefinition . CustomAttributes . Add ( attribute ) ;
4278
0 commit comments