66/// <summary>Debug functions that were removed from the game</summary>
77public static class SrDebugExtensions
88{
9- /// <summary>Retrieves a private field of type T2 from a class instance of type T1</summary>
10- /// <param name="fieldName">The name of the field to retrieve</param>
11- /// <param name="instance">The instance to retrieve the field from</param>
12- private static T2 GetPrivateField < T1 , T2 > ( string fieldName , T1 instance )
9+ public static BindingFlags all = BindingFlags . Public
10+ | BindingFlags . NonPublic
11+ | BindingFlags . Instance
12+ | BindingFlags . Static
13+ | BindingFlags . GetField
14+ | BindingFlags . SetField
15+ | BindingFlags . GetProperty
16+ | BindingFlags . SetProperty ;
17+
18+ /// <summary>Retrieves a field/property (which can be private, but doesn't have to be)
19+ /// of type T2 from a class instance of type T1</summary>
20+ /// <param name="fieldName">The name of the field/property to retrieve</param>
21+ /// <param name="instance">The instance to retrieve the field/property from</param>
22+ private static T2 GetFieldOrPropertyValue < T1 , T2 > ( string fieldName , T1 instance )
1323 {
14- var field = typeof ( T1 ) . GetField ( fieldName , BindingFlags . NonPublic | BindingFlags . Instance ) ;
15- return ( T2 ) field ? . GetValue ( instance ) ;
24+ var field = typeof ( T1 ) . GetField ( fieldName , all ) ;
25+ if ( field != null )
26+ return ( T2 ) field ? . GetValue ( instance ) ;
27+
28+ var property = typeof ( T1 ) . GetProperty ( fieldName , all ) ;
29+ return ( T2 ) property ? . GetValue ( instance , null ) ;
1630 }
1731
1832 // PediaDirector
@@ -22,8 +36,8 @@ private static T2 GetPrivateField<T1, T2>(string fieldName, T1 instance)
2236 /// <param name="self">The PediaDirector instance</param>
2337 public static void DebugClearUnlocked ( this PediaDirector self )
2438 {
25- var unlock = typeof ( PediaDirector ) . GetMethod ( "Unlock" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
26- GetPrivateField < PediaDirector , HashSet < PediaDirector . Id > > ( "unlocked" , self ) . Clear ( ) ;
39+ var unlock = typeof ( PediaDirector ) . GetMethod ( "Unlock" , all ) ;
40+ GetFieldOrPropertyValue < PediaDirector , HashSet < PediaDirector . Id > > ( "unlocked" , self ) . Clear ( ) ;
2741 foreach ( var t in self . initUnlocked )
2842 unlock . Invoke ( self , new object [ ] { t } ) ;
2943 }
@@ -32,7 +46,7 @@ public static void DebugClearUnlocked(this PediaDirector self)
3246 /// <param name="self">The PediaDirector instance</param>
3347 public static void DebugAllUnlocked ( this PediaDirector self )
3448 {
35- var unlock = typeof ( PediaDirector ) . GetMethod ( "Unlock" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
49+ var unlock = typeof ( PediaDirector ) . GetMethod ( "Unlock" , all ) ;
3650 foreach ( PediaDirector . Id id in Enum . GetValues ( typeof ( PediaDirector . Id ) ) )
3751 unlock . Invoke ( self , new object [ ] { id } ) ;
3852 }
@@ -44,14 +58,14 @@ public static void DebugAllUnlocked(this PediaDirector self)
4458 /// <param name="self">The TutorialDirector instance</param>
4559 public static void DebugClearCompleted ( this TutorialDirector self )
4660 {
47- GetPrivateField < TutorialDirector , HashSet < TutorialDirector . Id > > ( "completed" , self ) . Clear ( ) ;
61+ GetFieldOrPropertyValue < TutorialDirector , HashSet < TutorialDirector . Id > > ( "completed" , self ) . Clear ( ) ;
4862 }
4963
5064 /// <summary>Completes all tutorials</summary>
5165 /// <param name="self">The TutorialDirector instance</param>
5266 public static void DebugAllCompleted ( this TutorialDirector self )
5367 {
54- var completed = GetPrivateField < TutorialDirector , HashSet < TutorialDirector . Id > > ( "completed" , self ) ;
68+ var completed = GetFieldOrPropertyValue < TutorialDirector , HashSet < TutorialDirector . Id > > ( "completed" , self ) ;
5569 foreach ( TutorialDirector . Id id in Enum . GetValues ( typeof ( TutorialDirector . Id ) ) ) completed . Add ( id ) ;
5670 }
5771
@@ -62,18 +76,18 @@ public static void DebugAllCompleted(this TutorialDirector self)
6276 /// <param name="self">The AchievementsDirector instance</param>
6377 public static void DebugClearAwarded ( this AchievementsDirector self )
6478 {
65- GetPrivateField < AchievementsDirector , HashSet < AchievementsDirector . Achievement > > ( "earnedAchievements" , self ) . Clear ( ) ;
66- GetPrivateField < AchievementsDirector , Dictionary < AchievementsDirector . BoolStat , bool > > ( "boolStatDict" , self ) . Clear ( ) ;
67- GetPrivateField < AchievementsDirector , Dictionary < AchievementsDirector . IntStat , int > > ( "intStatDict" , self ) . Clear ( ) ;
68- GetPrivateField < AchievementsDirector , Dictionary < AchievementsDirector . EnumStat , HashSet < Enum > > > ( "enumStatDict" , self ) . Clear ( ) ;
69- GetPrivateField < AchievementsDirector , Dictionary < AchievementsDirector . GameFloatStat , float > > ( "gameFloatStatDict" , self ) . Clear ( ) ;
79+ GetFieldOrPropertyValue < AchievementsDirector , HashSet < AchievementsDirector . Achievement > > ( "earnedAchievements" , self ) . Clear ( ) ;
80+ GetFieldOrPropertyValue < AchievementsDirector , Dictionary < AchievementsDirector . BoolStat , bool > > ( "boolStatDict" , self ) . Clear ( ) ;
81+ GetFieldOrPropertyValue < AchievementsDirector , Dictionary < AchievementsDirector . IntStat , int > > ( "intStatDict" , self ) . Clear ( ) ;
82+ GetFieldOrPropertyValue < AchievementsDirector , Dictionary < AchievementsDirector . EnumStat , HashSet < Enum > > > ( "enumStatDict" , self ) . Clear ( ) ;
83+ GetFieldOrPropertyValue < AchievementsDirector , Dictionary < AchievementsDirector . GameFloatStat , float > > ( "gameFloatStatDict" , self ) . Clear ( ) ;
7084 }
7185
7286 /// <summary>Awards all achievements</summary>
7387 /// <param name="self">The AchievementsDirector instance</param>
7488 public static void DebugAllAwarded ( this AchievementsDirector self )
7589 {
76- var earnedAchievements = GetPrivateField < AchievementsDirector , HashSet < AchievementsDirector . Achievement > > ( "earnedAchievements" , self ) ;
90+ var earnedAchievements = GetFieldOrPropertyValue < AchievementsDirector , HashSet < AchievementsDirector . Achievement > > ( "earnedAchievements" , self ) ;
7791 foreach ( AchievementsDirector . Achievement achievement in Enum . GetValues ( typeof ( AchievementsDirector . Achievement ) ) ) earnedAchievements . Add ( achievement ) ;
7892 }
7993
@@ -84,7 +98,7 @@ public static void DebugAllAwarded(this AchievementsDirector self)
8498 /// <param name="self">The ProgressDirector instance</param>
8599 public static void DebugClearProgress ( this ProgressDirector self )
86100 {
87- GetPrivateField < ProgressDirector , Dictionary < ProgressDirector . ProgressType , int > > ( "progressDict" , self ) . Clear ( ) ;
101+ GetFieldOrPropertyValue < ProgressDirector , Dictionary < ProgressDirector . ProgressType , int > > ( "progressDict" , self ) . Clear ( ) ;
88102 }
89103
90104 /// <summary>Unlocks all progress</summary>
@@ -126,12 +140,12 @@ public static void DebugGiveAllUpgrades(this PlayerState self)
126140 /// <param name="fillTo"></param>
127141 public static void DebugFillRandomAmmo ( this Ammo self )
128142 {
129- var potentialAmmo = GetPrivateField < Ammo , GameObject [ ] > ( "potentialAmmo" , self ) ;
130- var numSlots = GetPrivateField < Ammo , int > ( "numSlots" , self ) ;
143+ var potentialAmmo = GetFieldOrPropertyValue < Ammo , GameObject [ ] > ( "potentialAmmo" , self ) ;
144+ var numSlots = GetFieldOrPropertyValue < Ammo , int > ( "numSlots" , self ) ;
131145
132- var ammoSlot = typeof ( Ammo ) . GetNestedType ( "Slot" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
133- var slots = GetPrivateField < Ammo , object [ ] > ( "slots" , self ) ;
134- var emotions = ammoSlot . GetField ( "emotions" , BindingFlags . Public | BindingFlags . Instance ) ;
146+ var ammoSlot = typeof ( Ammo ) . GetNestedType ( "Slot" , all ) ;
147+ var slots = GetFieldOrPropertyValue < Ammo , object [ ] > ( "slots" , self ) ;
148+ var emotions = ammoSlot . GetField ( "emotions" , all ) ;
135149
136150 for ( var i = 0 ; i < numSlots ; i ++ )
137151 {
@@ -157,17 +171,20 @@ public static void DebugFillRandomAmmo(this Ammo self)
157171 } ;
158172 emotions ? . SetValue ( slots [ i ] , emotionData ) ;
159173 }
174+
175+ // Refill Ammo again, because some mods might change the max value depending on what is in the slot
176+ DebugRefillAmmo ( self ) ;
160177 }
161178
162179 /// <summary>Refills the player's inventory</summary>
163180 /// <param name="self">The Ammo instance</param>
164181 /// <param name="fillTo"></param>
165182 public static void DebugRefillAmmo ( this Ammo self )
166183 {
167- var ammoSlot = typeof ( Ammo ) . GetNestedType ( "Slot" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
184+ var ammoSlot = typeof ( Ammo ) . GetNestedType ( "Slot" , all ) ;
168185 var slotCount = ammoSlot . GetField ( "count" ) ;
169- var numSlots = GetPrivateField < Ammo , int > ( "numSlots" , self ) ;
170- var slots = GetPrivateField < Ammo , object [ ] > ( "slots" , self ) ;
186+ var numSlots = GetFieldOrPropertyValue < Ammo , int > ( "numSlots" , self ) ;
187+ var slots = GetFieldOrPropertyValue < Ammo , object [ ] > ( "slots" , self ) ;
171188
172189 for ( var i = 0 ; i < numSlots ; i ++ )
173190 {
0 commit comments