@@ -9,15 +9,15 @@ public static class DebugService
99{
1010 static DebugService ( )
1111 {
12- Logger = new Debugger < Action < MethodBase , object [ ] > > ( AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . loghook ) ) ) ;
13- ExceptionHandler = new Debugger < Action < MethodBase , Exception > > ( null , null , AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . finalizer ) ) ) ;
14- Profiler = new Debugger < Action < MethodBase , long > > ( AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . prefix ) ) , AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . postfix ) ) ) ;
12+ Logger = new Debugger < object [ ] > ( AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . loghook ) ) ) ;
13+ ExceptionHandler = new Debugger < Exception > ( null , null , AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . finalizer ) ) ) ;
14+ Profiler = new Debugger < long > ( AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . prefix ) ) , AccessTools . Method ( typeof ( Hooks ) , nameof ( Hooks . postfix ) ) ) ;
1515 }
1616 class Hooks
1717 {
1818 public static void loghook ( MethodBase __originalMethod , object [ ] __args )
1919 {
20- Logger . Handler ( __originalMethod , __args ) ;
20+ Logger . Handler ( __originalMethod , __args ) ;
2121 }
2222 public static void prefix ( out long __state )
2323 {
@@ -43,13 +43,13 @@ public static bool IsDebuggable(MethodBase method)
4343 }
4444 return true ;
4545 }
46- public class Debugger < T > where T : Delegate
46+ public class Debugger < T >
4747 {
48- public void AddHandler ( T handler )
48+ public void AddHandler ( Action < MethodBase , T > handler )
4949 {
50- Handler = ( T ) Delegate . Combine ( Handler , handler ) ;
50+ Handler += handler ;
5151 }
52- public T Handler { get ; private set ; }
52+ public Action < MethodBase , T > Handler ;
5353 public Debugger ( MethodInfo Prefix = null , MethodInfo Postfix = null , MethodInfo Finalizer = null )
5454 {
5555 if ( Prefix is not null )
@@ -70,7 +70,7 @@ public Debugger(MethodInfo Prefix = null, MethodInfo Postfix = null, MethodInfo
7070 HarmonyMethod Finalizer ;
7171 public void Attach ( Assembly assembly , Func < MethodBase , bool > predicate = null )
7272 {
73- foreach ( var Type in assembly . GetTypes ( ) )
73+ foreach ( var Type in AccessTools . GetTypesFromAssembly ( assembly ) )
7474 {
7575 Attach ( Type , predicate ) ;
7676 }
@@ -110,8 +110,61 @@ public void Attach(MethodBase method)
110110 }
111111 }
112112 static readonly Harmony Patcher = new ( Others . harmony_id ) ;
113- public static readonly Debugger < Action < MethodBase , object [ ] > > Logger ;
114- public static readonly Debugger < Action < MethodBase , Exception > > ExceptionHandler ;
115- public static readonly Debugger < Action < MethodBase , long > > Profiler ;
113+ public static readonly Debugger < object [ ] > Logger ;
114+ public static readonly Debugger < Exception > ExceptionHandler ;
115+ public static readonly Debugger < long > Profiler ;
116116 public static readonly Func < MethodBase , bool > DefaultPredicate = method => ! method . IsDefined ( typeof ( DoNotDebug ) , true ) && ! method . DeclaringType ! . IsDefined ( typeof ( DoNotDebug ) , true ) ;
117+ }
118+ public class HarmonyPatcher //any harmony patches causing you trouble? this lets you single them out!
119+ {
120+ private static readonly FieldInfo containerAttributes = AccessTools . Field ( typeof ( PatchClassProcessor ) , "containerAttributes" ) ;
121+ public Dictionary < Type , PatchClassProcessor > Processors = new ( ) ;
122+ private Harmony harmony ;
123+ public HarmonyPatcher ( string ID )
124+ {
125+ harmony = new Harmony ( ID ) ;
126+ }
127+ public HarmonyPatcher ( string ID , Assembly assembly )
128+ {
129+ harmony = new Harmony ( ID ) ;
130+ Add ( assembly ) ;
131+ }
132+ public void Add ( Assembly assembly )
133+ {
134+ AccessTools . GetTypesFromAssembly ( assembly ) . Do ( type => Add ( type ) ) ;
135+ }
136+ public bool Add ( Type type )
137+ {
138+ var processor = harmony . CreateClassProcessor ( type , true ) ;
139+ if ( containerAttributes . GetValue ( processor ) is null )
140+ {
141+ return false ;
142+ }
143+ Processors . Add ( type , processor ) ;
144+ return true ;
145+ }
146+ public void Patch ( Type type )
147+ {
148+ Processors [ type ] . Patch ( ) ;
149+ }
150+ public void PatchAll ( )
151+ {
152+ foreach ( var processor in Processors . Values )
153+ {
154+ processor . Patch ( ) ;
155+ }
156+ Processors . Clear ( ) ;
157+ }
158+ public bool PatchRandom ( out Type type )
159+ {
160+ type = null ;
161+ if ( Processors . Count == 0 )
162+ {
163+ return false ;
164+ }
165+ type = Processors . ToList ( ) . GetRandom ( ) . Key ;
166+ Processors . Remove ( type ) ;
167+ Patch ( type ) ;
168+ return true ;
169+ }
117170}
0 commit comments