@@ -28,13 +28,33 @@ internal static class CecilEmitter
2828 typeof ( UnverifiableCodeAttribute ) . GetConstructor ( Type . EmptyTypes ) ;
2929
3030 public static void Dump ( MethodDefinition md , IEnumerable < string > dumpPaths , MethodBase original = null )
31+ {
32+ using var module = DumpImpl ( md , original ) ;
33+
34+ foreach ( var settingsDumpPath in dumpPaths )
35+ {
36+ var fullPath = Path . GetFullPath ( settingsDumpPath ) ;
37+ try
38+ {
39+ Directory . CreateDirectory ( fullPath ) ;
40+ using var stream = File . OpenWrite ( Path . Combine ( fullPath , $ "{ module . Name } .dll") ) ;
41+ module . Write ( stream ) ;
42+ }
43+ catch ( Exception e )
44+ {
45+ Logger . Log ( Logger . LogChannel . Error , ( ) => $ "Failed to dump { md . GetID ( simple : true ) } to { fullPath } : { e } ") ;
46+ }
47+ }
48+ }
49+
50+ internal static ModuleDefinition DumpImpl ( MethodDefinition md , MethodBase original = null )
3151 {
3252 if ( md . Body is null )
3353 throw new ArgumentException ( $ "Body of MethodDefinition '{ md . Name } ' to dump is null") ;
3454
3555 var name = $ "HarmonyDump.{ SanitizeTypeName ( md . GetID ( withType : false , simple : true ) ) } .{ Guid . NewGuid ( ) . GetHashCode ( ) : X8} ";
3656 var originalName = ( original ? . Name ?? md . Name ) . Replace ( '.' , '_' ) ;
37- using var module = ModuleDefinition . CreateModule ( name ,
57+ var module = ModuleDefinition . CreateModule ( name ,
3858 new ModuleParameters
3959 {
4060 Kind = ModuleKind . Dll , ReflectionImporterProvider = MMReflectionImporter . ProviderNoDefault
@@ -114,20 +134,7 @@ public static void Dump(MethodDefinition md, IEnumerable<string> dumpPaths, Meth
114134 new ParameterDefinition ( "<>_this" , ParameterAttributes . None , type . Relink ( relinker , clone ) ) ) ;
115135 }
116136
117- foreach ( var settingsDumpPath in dumpPaths )
118- {
119- var fullPath = Path . GetFullPath ( settingsDumpPath ) ;
120- try
121- {
122- Directory . CreateDirectory ( fullPath ) ;
123- using var stream = File . OpenWrite ( Path . Combine ( fullPath , $ "{ module . Name } .dll") ) ;
124- module . Write ( stream ) ;
125- }
126- catch ( Exception e )
127- {
128- Logger . Log ( Logger . LogChannel . Error , ( ) => $ "Failed to dump { md . GetID ( simple : true ) } to { fullPath } : { e } ") ;
129- }
130- }
137+ return module ;
131138 }
132139
133140 private static string SanitizeTypeName ( string typeName )
0 commit comments