@@ -16,54 +16,57 @@ public static bool HasNetcodePatchedAttribute(ICompiledAssembly assembly)
1616 // read
1717 AssemblyDefinition ? assemblyDefinition = CodeGenHelpers . AssemblyDefinitionFor ( assembly , out _ ) ;
1818 if ( assemblyDefinition == null ) return false ;
19-
19+
2020 return assemblyDefinition . CustomAttributes . Any (
2121 attribute => attribute . Constructor . DeclaringType . FullName . EndsWith ( $ ".{ ApplyPatchedAttributeILPP . AttributeNamespaceSuffix } .{ ApplyPatchedAttributeILPP . AttributeName } ")
2222 ) ;
2323 }
24-
24+
2525 public static void ILPostProcessFile ( string assemblyPath , string outputPath , string [ ] references , Action < string > onWarning , Action < string > onError )
2626 {
2727 var assemblyName = Path . GetFileNameWithoutExtension ( assemblyPath ) ;
2828 var assemblyDirectoryName = Path . GetDirectoryName ( assemblyPath ) ! ;
2929 var pdbPath = Path . Combine ( assemblyDirectoryName , $ "{ assemblyName } .pdb") ;
30-
30+
3131 Log . Information ( "Reading : {FileName}" , Path . GetFileName ( assemblyPath ) ) ;
32-
32+
3333 // read the original assembly from file
3434 ICompiledAssembly assembly = new CompiledAssemblyFromFile ( assemblyPath ) {
3535 References = references
3636 } ;
37-
37+
3838 if ( HasNetcodePatchedAttribute ( assembly ) )
3939 {
4040 Log . Warning ( "Skipping {FileName} as it has already been patched." , Path . GetFileName ( assemblyPath ) ) ;
4141 return ;
4242 }
43-
43+
4444 Log . Information ( "Patching : {FileName}" , Path . GetFileName ( assemblyPath ) ) ;
45-
45+
46+ string ? renameAssemblyPath = null ;
47+ string ? renamePdbPath = null ;
48+
4649 if ( assemblyPath == outputPath )
4750 {
4851 // remove files with _original.dll and _original.pdb
52+
53+ renameAssemblyPath = Path . Combine ( assemblyDirectoryName , $ "{ assemblyName } _original.dll") ;
54+ renamePdbPath = Path . Combine ( assemblyDirectoryName , $ "{ assemblyName } _original.pdb") ;
4955
50- var newAssemblyPath = Path . Combine ( assemblyDirectoryName , $ "{ assemblyName } _original.dll") ;
51- var newPdbPath = Path . Combine ( assemblyDirectoryName , $ "{ assemblyName } _original.pdb") ;
52-
53- if ( File . Exists ( newAssemblyPath ) )
56+ if ( File . Exists ( renameAssemblyPath ) )
5457 {
55- Log . Information ( "Deleting : {FileName}" , Path . GetFileName ( newAssemblyPath ) ) ;
56- File . Delete ( newAssemblyPath ) ;
58+ Log . Information ( "Deleting : {FileName}" , Path . GetFileName ( renameAssemblyPath ) ) ;
59+ File . Delete ( renameAssemblyPath ) ;
5760 }
58-
59- if ( File . Exists ( newPdbPath ) )
61+
62+ if ( File . Exists ( renamePdbPath ) )
6063 {
61- Log . Information ( "Deleting : {FileName}" , Path . GetFileName ( newPdbPath ) ) ;
62- File . Delete ( newPdbPath ) ;
64+ Log . Information ( "Deleting : {FileName}" , Path . GetFileName ( renamePdbPath ) ) ;
65+ File . Delete ( renamePdbPath ) ;
6366 }
6467
65- File . Move ( assemblyPath , newAssemblyPath ) ;
66- File . Move ( pdbPath , newPdbPath ) ;
68+ File . Move ( assemblyPath , renameAssemblyPath ) ;
69+ File . Move ( pdbPath , renamePdbPath ) ;
6770 }
6871
6972 ICompiledAssembly ApplyProcess < TProcessor > ( ICompiledAssembly assemblyToApplyProcessTo ) where TProcessor : ILPostProcessor , new ( )
@@ -92,20 +95,41 @@ public static void ILPostProcessFile(string assemblyPath, string outputPath, str
9295 } ;
9396 }
9497
95- assembly = ApplyProcess < NetworkBehaviourILPP > ( assembly ) ;
96- assembly = ApplyProcess < INetworkMessageILPP > ( assembly ) ;
97- assembly = ApplyProcess < INetworkSerializableILPP > ( assembly ) ;
98- assembly = ApplyProcess < ApplyPatchedAttributeILPP > ( assembly ) ;
99-
100- var outputAssemblyName = Path . GetFileNameWithoutExtension ( outputPath ) ;
101- var outputDirectoryName = Path . GetDirectoryName ( outputPath ) ! ;
102- var outputPdbPath = Path . Combine ( outputDirectoryName , $ "{ outputAssemblyName } .pdb") ;
103-
104- // save the weaved assembly to file.
105- // some tests open it and check for certain IL code.
106- File . WriteAllBytes ( outputPath , assembly . InMemoryAssembly . PeData ) ;
107- File . WriteAllBytes ( outputPdbPath , assembly . InMemoryAssembly . PdbData ) ;
108-
109- Log . Information ( "Patched successfully : {FileName} -> {OutputPath}" , Path . GetFileName ( assemblyPath ) , Path . GetFileName ( outputPath ) ) ;
98+ try
99+ {
100+ assembly = ApplyProcess < NetworkBehaviourILPP > ( assembly ) ;
101+ assembly = ApplyProcess < INetworkMessageILPP > ( assembly ) ;
102+ assembly = ApplyProcess < INetworkSerializableILPP > ( assembly ) ;
103+ assembly = ApplyProcess < ApplyPatchedAttributeILPP > ( assembly ) ;
104+
105+ var outputAssemblyName = Path . GetFileNameWithoutExtension ( outputPath ) ;
106+ var outputDirectoryName = Path . GetDirectoryName ( outputPath ) ! ;
107+ var outputPdbPath = Path . Combine ( outputDirectoryName , $ "{ outputAssemblyName } .pdb") ;
108+
109+ // save the weaved assembly to file.
110+ // some tests open it and check for certain IL code.
111+ File . WriteAllBytes ( outputPath , assembly . InMemoryAssembly . PeData ) ;
112+ File . WriteAllBytes ( outputPdbPath , assembly . InMemoryAssembly . PdbData ) ;
113+
114+ Log . Information ( "Patched successfully : {FileName} -> {OutputPath}" , Path . GetFileName ( assemblyPath ) , Path . GetFileName ( outputPath ) ) ;
115+ }
116+ catch ( Exception )
117+ {
118+ if ( assemblyPath == outputPath )
119+ {
120+ // rename file from _original.dll to .dll
121+ if ( File . Exists ( renameAssemblyPath ) )
122+ {
123+ File . Move ( renameAssemblyPath ! , assemblyPath ) ;
124+ }
125+
126+ if ( File . Exists ( renamePdbPath ! ) )
127+ {
128+ File . Move ( renamePdbPath ! , pdbPath ) ;
129+ }
130+ }
131+
132+ throw ;
133+ }
110134 }
111135}
0 commit comments