@@ -12,6 +12,13 @@ namespace NetcodePatcher.CodeGen;
1212
1313public class NetcodeILPPApplicator
1414{
15+ public NetcodeILPPApplicator ( string assemblyPath , string outputPath , string [ ] references )
16+ {
17+ AssemblyPath = assemblyPath ;
18+ OutputPath = outputPath ;
19+ References = references ;
20+ }
21+
1522 public Action < string > OnWarning { get ; set ; } = _ => { } ;
1623 public Action < string > OnError { get ; set ; } = _ => { } ;
1724
@@ -23,22 +30,16 @@ public class NetcodeILPPApplicator
2330 private string AssemblyFileName => Path . GetFileName ( AssemblyPath ) ;
2431 private string AssemblyDirName => Path . GetDirectoryName ( AssemblyPath ) ! ;
2532 private string PdbPath => Path . Combine ( AssemblyDirName , $ "{ AssemblyName } .pdb") ;
26-
27- public NetcodeILPPApplicator ( string assemblyPath , string outputPath , string [ ] references )
28- {
29- AssemblyPath = assemblyPath ;
30- OutputPath = outputPath ;
31- References = references ;
32- }
33-
33+
3434 public static bool HasNetcodePatchedAttribute ( ICompiledAssembly assembly )
3535 {
3636 // read
37- AssemblyDefinition ? assemblyDefinition = CodeGenHelpers . AssemblyDefinitionFor ( assembly , out _ ) ;
37+ var assemblyDefinition = CodeGenHelpers . AssemblyDefinitionFor ( assembly , out _ ) ;
3838 if ( assemblyDefinition is null ) return false ;
3939
4040 return assemblyDefinition . CustomAttributes . Any (
41- attribute => attribute . Constructor . DeclaringType . FullName . EndsWith ( $ ".{ ApplyPatchedAttributeILPP . AttributeNamespaceSuffix } .{ ApplyPatchedAttributeILPP . AttributeName } ")
41+ attribute => attribute . Constructor . DeclaringType . FullName . EndsWith (
42+ $ ".{ ApplyPatchedAttributeILPP . AttributeNamespaceSuffix } .{ ApplyPatchedAttributeILPP . AttributeName } ")
4243 ) ;
4344 }
4445
@@ -50,7 +51,8 @@ public void ApplyProcesses()
5051 try
5152 {
5253 // read the original assembly from file
53- assemblyFromFile = new CompiledAssemblyFromFile ( AssemblyPath ) {
54+ assemblyFromFile = new CompiledAssemblyFromFile ( AssemblyPath )
55+ {
5456 References = References
5557 } ;
5658 }
@@ -62,9 +64,9 @@ public void ApplyProcesses()
6264
6365 var debugSymbolsAreEmbedded = assemblyFromFile . DebugSymbolsAreEmbedded ;
6466 ICompiledAssembly assembly = assemblyFromFile ;
65-
67+
6668 if ( HasNetcodePatchedAttribute ( assembly ) )
67- {
69+ {
6870 Log . Warning ( "Skipping {FileName} as it has already been patched." , Path . GetFileName ( AssemblyPath ) ) ;
6971 return ;
7072 }
@@ -77,7 +79,7 @@ public void ApplyProcesses()
7779 if ( AssemblyPath == OutputPath )
7880 {
7981 // remove files with _original.dll and _original.pdb
80-
82+
8183 renameAssemblyPath = Path . Combine ( AssemblyDirName , $ "{ AssemblyName } _original.dll") ;
8284 renamePdbPath = Path . Combine ( AssemblyDirName , $ "{ AssemblyName } _original.pdb") ;
8385
@@ -98,19 +100,19 @@ public void ApplyProcesses()
98100 File . Move ( PdbPath , renamePdbPath ) ;
99101 }
100102
101- ICompiledAssembly ApplyProcess < TProcessor > ( ICompiledAssembly assemblyToApplyProcessTo ) where TProcessor : ILPostProcessor , new ( )
103+ ICompiledAssembly ApplyProcess < TProcessor > ( ICompiledAssembly assemblyToApplyProcessTo )
104+ where TProcessor : ILPostProcessor , new ( )
102105 {
103106 var ilpp = new TProcessor ( ) ;
104107 if ( ! ilpp . WillProcess ( assembly ) ) return assemblyToApplyProcessTo ;
105108
106- ILPostProcessResult result = ilpp . Process ( assembly ) ;
109+ var result = ilpp . Process ( assembly ) ;
107110
108111 if ( result is null )
109112 return assemblyToApplyProcessTo ;
110113
111114 // handle the error messages like Unity would
112- foreach ( DiagnosticMessage message in result . Diagnostics )
113- {
115+ foreach ( var message in result . Diagnostics )
114116 switch ( message . DiagnosticType )
115117 {
116118 case DiagnosticType . Warning :
@@ -120,9 +122,9 @@ public void ApplyProcesses()
120122 OnError ( message . MessageData + $ "{ message . File } :{ message . Line } ") ;
121123 continue ;
122124 }
123- }
124125
125- return new CompiledAssemblyFromInMemoryAssembly ( result . InMemoryAssembly , assemblyToApplyProcessTo . Name ) {
126+ return new CompiledAssemblyFromInMemoryAssembly ( result . InMemoryAssembly , assemblyToApplyProcessTo . Name )
127+ {
126128 References = References
127129 } ;
128130 }
@@ -137,19 +139,22 @@ public void ApplyProcesses()
137139 using var peStream = new MemoryStream ( assembly . InMemoryAssembly . PeData ) ;
138140 using var symbolStream = new MemoryStream ( assembly . InMemoryAssembly . PdbData ) ;
139141
140- var assemblyDefinition = AssemblyDefinition . ReadAssembly ( peStream , new ReaderParameters ( )
142+ var assemblyDefinition = AssemblyDefinition . ReadAssembly ( peStream , new ReaderParameters
141143 {
142144 ReadSymbols = true ,
143- SymbolStream = symbolStream ,
145+ SymbolStream = symbolStream
144146 } ) ;
145147
146148 assemblyDefinition . Write ( OutputPath , new WriterParameters
147149 {
148- SymbolWriterProvider = debugSymbolsAreEmbedded ? new EmbeddedPortablePdbWriterProvider ( ) : new DefaultSymbolWriterProvider ( ) ,
149- WriteSymbols = true ,
150+ SymbolWriterProvider = debugSymbolsAreEmbedded
151+ ? new EmbeddedPortablePdbWriterProvider ( )
152+ : new DefaultSymbolWriterProvider ( ) ,
153+ WriteSymbols = true
150154 } ) ;
151155
152- Log . Information ( "Patched successfully : {FileName} -> {OutputPath}" , Path . GetFileName ( AssemblyPath ) , Path . GetFileName ( OutputPath ) ) ;
156+ Log . Information ( "Patched successfully : {FileName} -> {OutputPath}" , Path . GetFileName ( AssemblyPath ) ,
157+ Path . GetFileName ( OutputPath ) ) ;
153158 }
154159 catch ( Exception )
155160 {
@@ -172,4 +177,4 @@ public void ApplyProcesses()
172177 throw ;
173178 }
174179 }
175- }
180+ }
0 commit comments