Skip to content

Commit 234e179

Browse files
committed
Overhaul how Overlay button toggles work. 1.5 broke some and this was slow anyway.
Instead of checking the translated string at runtime to determine which TD setting bool to use, a transpiler at load checks the TexButton for the icon ( the settings are renamed to "toggle{TexButtonName}" so the transpiiler can find them ) Also temperature and pollution overlay toggles.
1 parent 7688683 commit 234e179

File tree

2 files changed

+108
-47
lines changed

2 files changed

+108
-47
lines changed

Source/RemoveUnusedToggles.cs

+65-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Reflection;
6+
using System.Reflection.Emit;
67
using Verse;
78
using RimWorld;
89
using HarmonyLib;
@@ -19,24 +20,74 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
1920
MethodInfo ToggleableIconInfo = AccessTools.Method(typeof(WidgetRow), nameof(WidgetRow.ToggleableIcon));
2021
MethodInfo ToggleableIconReplacement = AccessTools.Method(typeof(RemoveUnusedToggles), nameof(ToggleableIconFiltered));
2122

22-
return Transpilers.MethodReplacer(instructions, ToggleableIconInfo, ToggleableIconReplacement);
23+
FieldInfo settingsInfo = AccessTools.Field(typeof(Mod), nameof(Mod.settings));
24+
25+
// PlaySettings draws icons via WidgetRow.ToggleableIcon(ref bool toggleable, Texture2D tex ...)
26+
// Each bool has its own icon, so detect PlaySettings by their icon:
27+
string[] fieldNames = [
28+
nameof(Verse.TexButton.ShowLearningHelper),
29+
nameof(Verse.TexButton.ShowZones),
30+
nameof(Verse.TexButton.ShowBeauty),
31+
nameof(Verse.TexButton.ShowRoomStats),
32+
nameof(Verse.TexButton.ShowColonistBar),
33+
nameof(Verse.TexButton.ShowRoofOverlay),
34+
nameof(Verse.TexButton.ShowFertilityOverlay),
35+
nameof(Verse.TexButton.ShowTerrainAffordanceOverlay),
36+
nameof(Verse.TexButton.AutoHomeArea),
37+
nameof(Verse.TexButton.AutoRebuild),
38+
nameof(Verse.TexButton.ShowTemperatureOverlay),
39+
nameof(Verse.TexButton.CategorizedResourceReadout),
40+
nameof(Verse.TexButton.ShowPollutionOverlay)
41+
];
42+
int fieldIndex = 0;
43+
44+
// Find the ILCode that loads this texture:
45+
FieldInfo showToggleButtonTexInfo = AccessTools.Field(typeof(Verse.TexButton), fieldNames[fieldIndex]);
46+
// (These are ordered by when them method uses them, so only need to check one at a time)
47+
48+
bool modifyThisCall = false;
49+
50+
foreach (CodeInstruction inst in instructions)
51+
{
52+
// When we load the next TexButton...
53+
if (showToggleButtonTexInfo != null && inst.LoadsField(showToggleButtonTexInfo))
54+
{
55+
modifyThisCall = true;
56+
57+
58+
// Get the TD Toggle Setting bool for this PlaySetting: named toggle<TexName>
59+
FieldInfo settingToToggleThat = AccessTools.Field(typeof(Settings), "toggle" + fieldNames[fieldIndex]);
60+
61+
// (And prep for next field:)
62+
fieldIndex++;
63+
showToggleButtonTexInfo = fieldIndex < fieldNames.Length ? AccessTools.Field(typeof(Verse.TexButton), fieldNames[fieldIndex]) : null;
64+
65+
// Simply load the TD toggle setting bool before the texture
66+
yield return new CodeInstruction(OpCodes.Ldsfld, settingsInfo); //Mod.settings
67+
yield return new CodeInstruction(OpCodes.Ldfld, settingToToggleThat);//Mod.settings.toggleShow~Whatever~
68+
69+
yield return inst; //TexButton.Show~Whatever~
70+
71+
// And then...
72+
}
73+
else if (inst.Calls(ToggleableIconInfo) && modifyThisCall)
74+
{
75+
// Modify the WidgetRow.ToggleableIcon to our override with inserted bool settingToToggleThat
76+
yield return new CodeInstruction(OpCodes.Call, ToggleableIconReplacement);
77+
78+
79+
// and make sure we don't modify ToggleableIcons calls that didn't just get this treatment
80+
modifyThisCall = false;
81+
}
82+
else
83+
yield return inst;
84+
}
2385
}
2486

2587
//public void ToggleableIcon(ref bool toggleable, Texture2D tex, string tooltip, SoundDef mouseoverSound = null, string tutorTag = null)
26-
public static void ToggleableIconFiltered(WidgetRow row, ref bool toggleable, Texture2D tex, string tooltip, SoundDef mouseoverSound = null, string tutorTag = null)
88+
public static void ToggleableIconFiltered(WidgetRow row, ref bool toggleable, bool settingToToggleThat, Texture2D tex, string tooltip, SoundDef mouseoverSound = null, string tutorTag = null)
2789
{
28-
if(tooltip == "ShowLearningHelperWhenEmptyToggleButton".Translate() ? Mod.settings.showToggleLearning :
29-
tooltip == "ZoneVisibilityToggleButton".Translate() ? Mod.settings.showToggleZone :
30-
tooltip == "ShowBeautyToggleButton".Translate() ? Mod.settings.showToggleBeauty :
31-
tooltip == "ShowRoomStatsToggleButton".Translate() ? Mod.settings.showToggleRoomstats :
32-
tooltip == "ShowColonistBarToggleButton".Translate() ? Mod.settings.showToggleColonists :
33-
tooltip == "ShowRoofOverlayToggleButton".Translate() ? Mod.settings.showToggleRoof :
34-
tooltip == "AutoHomeAreaToggleButton".Translate() ? Mod.settings.showToggleHomeArea :
35-
tooltip == "AutoRebuildButton".Translate() ? Mod.settings.showToggleRebuild :
36-
tooltip == "ShowFertilityOverlayToggleButton".Translate() ? Mod.settings.showToggleFertility :
37-
tooltip == "ShowTerrainAffordanceOverlayToggleButton".Translate() ? Mod.settings.showToggleAffordance :
38-
tooltip == "CategorizedResourceReadoutToggleButton".Translate() ? Mod.settings.showToggleCategorized :
39-
true)
90+
if(settingToToggleThat)
4091
row.ToggleableIcon(ref toggleable, tex, tooltip, mouseoverSound, tutorTag);
4192
}
4293
}

Source/Settings.cs

+43-33
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,22 @@ public class Settings : ModSettings
8989
public bool areasUnlimited = true;
9090
public bool matchGrowButton = true;
9191

92-
public bool showToggleLearning = true;
93-
public bool showToggleZone = true;
94-
public bool showToggleBeauty = true;
95-
public bool showToggleRoomstats = true;
96-
public bool showToggleColonists = true;
97-
public bool showToggleRoof = true;
98-
public bool showToggleHomeArea = true;
99-
public bool showToggleRebuild = true;
100-
public bool showToggleFertility = false;
101-
public bool showToggleAffordance = false;
102-
public bool showToggleCategorized = true;
92+
// These are named based on the TexButton. texture name of the icon of the setting
93+
// And automatically loaded in a transpiler by that name
94+
public bool toggleShowLearningHelper = true;
95+
public bool toggleShowZones = true;
96+
public bool toggleShowBeauty = true;
97+
public bool toggleShowRoomStats = true;
98+
public bool toggleShowColonistBar = true;
99+
public bool toggleShowRoofOverlay = true;
100+
public bool toggleShowFertilityOverlay = false;
101+
public bool toggleShowTerrainAffordanceOverlay = false;
102+
public bool toggleAutoHomeArea = true;
103+
public bool toggleAutoRebuild = true;
104+
public bool toggleShowTemperatureOverlay = true;
105+
public bool toggleCategorizedResourceReadout = true;
106+
public bool toggleShowPollutionOverlay = true;
107+
103108

104109
public bool colorVariation = false;
105110
public bool colorGenerator = false;
@@ -174,17 +179,20 @@ public void DoWindowContents(Rect wrect)
174179
//Hide bottom-right Toggleable buttons
175180
options.LabelHeader("TD.SettingHeaderToggleButtons".Translate());
176181
options.Label("TD.SettingHeaderToggleButtonsDesc".Translate());
177-
options.CheckboxLabeled("TD.Show".Translate("ShowLearningHelperWhenEmptyToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleLearning);
178-
options.CheckboxLabeled("TD.Show".Translate("ZoneVisibilityToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleZone);
179-
options.CheckboxLabeled("TD.Show".Translate("ShowBeautyToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleBeauty);
180-
options.CheckboxLabeled("TD.Show".Translate("ShowRoomStatsToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleRoomstats);
181-
options.CheckboxLabeled("TD.Show".Translate("ShowColonistBarToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleColonists);
182-
options.CheckboxLabeled("TD.Show".Translate("ShowRoofOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleRoof);
183-
options.CheckboxLabeled("TD.Show".Translate("AutoHomeAreaToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleHomeArea);
184-
options.CheckboxLabeled("TD.Show".Translate("AutoRebuildButton".Translate().RawText.Split('\n')[0]), ref showToggleRebuild);
185-
options.CheckboxLabeled("TD.Show".Translate("ShowFertilityOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleFertility);
186-
options.CheckboxLabeled("TD.Show".Translate("ShowTerrainAffordanceOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleAffordance);
187-
options.CheckboxLabeled("TD.Show".Translate("CategorizedResourceReadoutToggleButton".Translate().RawText.Split('\n')[0]), ref showToggleCategorized);
182+
options.CheckboxLabeled("TD.Show".Translate("ShowLearningHelperWhenEmptyToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowLearningHelper);
183+
options.CheckboxLabeled("TD.Show".Translate("ZoneVisibilityToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowZones);
184+
options.CheckboxLabeled("TD.Show".Translate("ShowBeautyToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowBeauty);
185+
options.CheckboxLabeled("TD.Show".Translate("ShowRoomStatsToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowRoomStats);
186+
options.CheckboxLabeled("TD.Show".Translate("ShowColonistBarToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowColonistBar);
187+
options.CheckboxLabeled("TD.Show".Translate("ShowRoofOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowRoofOverlay);
188+
options.CheckboxLabeled("TD.Show".Translate("ShowFertilityOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowFertilityOverlay);
189+
options.CheckboxLabeled("TD.Show".Translate("ShowTerrainAffordanceOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowTerrainAffordanceOverlay);
190+
options.CheckboxLabeled("TD.Show".Translate("AutoHomeAreaToggleButton".Translate().RawText.Split('\n')[0]), ref toggleAutoHomeArea);
191+
options.CheckboxLabeled("TD.Show".Translate("AutoRebuildButton".Translate().RawText.Split('\n')[0]), ref toggleAutoRebuild);
192+
options.CheckboxLabeled("TD.Show".Translate("ShowTemperatureOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowTemperatureOverlay);
193+
options.CheckboxLabeled("TD.Show".Translate("CategorizedResourceReadoutToggleButton".Translate().RawText.Split('\n')[0]), ref toggleCategorizedResourceReadout);
194+
if (ModsConfig.BiotechActive) // Mainly so that the translation key exists
195+
options.CheckboxLabeled("TD.Show".Translate("ShowPollutionOverlayToggleButton".Translate().RawText.Split('\n')[0]), ref toggleShowPollutionOverlay);
188196
options.GapLine();
189197

190198

@@ -374,17 +382,19 @@ public override void ExposeData()
374382
Scribe_Values.Look(ref areasUnlimited, "areasUnlimited", true);
375383
Scribe_Values.Look(ref matchGrowButton, "matchGrowButton", true);
376384

377-
Scribe_Values.Look(ref showToggleLearning, "showToggleLearning", true);
378-
Scribe_Values.Look(ref showToggleZone, "showToggleZone", true);
379-
Scribe_Values.Look(ref showToggleBeauty, "showToggleBeauty", true);
380-
Scribe_Values.Look(ref showToggleRoomstats, "showToggleRoomstats", true);
381-
Scribe_Values.Look(ref showToggleColonists, "showToggleColonists", true);
382-
Scribe_Values.Look(ref showToggleRoof, "showToggleRoof", true);
383-
Scribe_Values.Look(ref showToggleHomeArea, "showToggleHomeArea", true);
384-
Scribe_Values.Look(ref showToggleRebuild, "showToggleRebuild", true);
385-
Scribe_Values.Look(ref showToggleFertility, "showToggleFertility", false);
386-
Scribe_Values.Look(ref showToggleAffordance, "showToggleAffordance", false);
387-
Scribe_Values.Look(ref showToggleCategorized, "showToggleCategorized", true);
385+
Scribe_Values.Look(ref toggleShowLearningHelper, "showToggleLearning", true);
386+
Scribe_Values.Look(ref toggleShowZones, "showToggleZone", true);
387+
Scribe_Values.Look(ref toggleShowBeauty, "showToggleBeauty", true);
388+
Scribe_Values.Look(ref toggleShowRoomStats, "showToggleRoomstats", true);
389+
Scribe_Values.Look(ref toggleShowColonistBar, "showToggleColonists", true);
390+
Scribe_Values.Look(ref toggleShowRoofOverlay, "showToggleRoof", true);
391+
Scribe_Values.Look(ref toggleShowFertilityOverlay, "showToggleFertility", false);
392+
Scribe_Values.Look(ref toggleShowTerrainAffordanceOverlay, "showToggleAffordance", false);
393+
Scribe_Values.Look(ref toggleAutoHomeArea, "showToggleHomeArea", true);
394+
Scribe_Values.Look(ref toggleAutoRebuild, "showToggleRebuild", true);
395+
Scribe_Values.Look(ref toggleShowTemperatureOverlay, "showToggleTemperature", true);
396+
Scribe_Values.Look(ref toggleCategorizedResourceReadout, "showToggleCategorized", true);
397+
Scribe_Values.Look(ref toggleShowPollutionOverlay, "showTogglePollution", true);
388398

389399
Scribe_Values.Look(ref colorVariation, "colorVariation", false);
390400
Scribe_Values.Look(ref colorGenerator, "colorGenerator", false);

0 commit comments

Comments
 (0)