-
Notifications
You must be signed in to change notification settings - Fork 34
/
TextChanges.cs
172 lines (156 loc) · 8.72 KB
/
TextChanges.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using Verse;
using RimWorld;
using UnityEngine;
namespace CommonSense
{
public class TextChanges
{
[HarmonyPatch(typeof(ThingFilter), "SetAllowAllWhoCanMake")]
public static class ThingFilter_SetAllowAllWhoCanMake_CommonSensePatch
{
public static bool Prefix(ThingFilter __instance, ThingDef thing)
{
List<ThingDef> allowAllWhoCanMake = Traverse.Create(__instance).Field("allowAllWhoCanMake").GetValue<List<ThingDef>>();
if (allowAllWhoCanMake == null)
{
allowAllWhoCanMake = new List<ThingDef>();
Traverse.Create(__instance).Field("allowAllWhoCanMake").SetValue(allowAllWhoCanMake);
allowAllWhoCanMake.Add(thing);
}
return true;
}
}
private static string ShortCategory(ThingCategoryDef tcDef)
{
if (tcDef.parent == null)
return "NoCategory".Translate().CapitalizeFirst();
else
return tcDef.label.CapitalizeFirst();
}
private static string GetCategoryPath(ThingCategoryDef tcDef)
{
if (tcDef.parent == null)
return "NoCategory".Translate().CapitalizeFirst();
else
{
string s = tcDef.label.CapitalizeFirst();
ThingCategoryDef def = tcDef.parent;
while (def.parent != null)
{
s += " \\ " + def.label.CapitalizeFirst();
def = def.parent;
}
return s;
}
}
private static string GetCategList(List<ThingCategoryDef> list)
{
string s = "";
foreach (var i in list)
s += GetCategoryPath(i) + "\n";
if (s == "") return "NoCategory".Translate().CapitalizeFirst();
else return s;
}
public static StatDrawEntry CategoryEntry(ThingCategoryDef tcDef)
{
return new StatDrawEntry(StatCategoryDefOf.Basics, "Category".Translate(), GetCategoryPath(tcDef), ShortCategory(tcDef), 1000);
}
public static IEnumerable<StatDrawEntry> CategoryEntryRow(Thing thing)
{
ThingCategoryDef d;
if (thing == null || thing.def == null || (d = thing.def.FirstThingCategory) == null)
yield break;
yield return new StatDrawEntry(StatCategoryDefOf.Basics, "Category".Translate(), GetCategoryPath(d), GetCategList(thing.def.thingCategories), 1000);
}
[HarmonyPatch(typeof(StatsReportUtility), "DrawStatsReport", new Type[] { typeof(Rect), typeof(Thing) })]
public static class StatsReportUtility_DrawStatsReport_CommonSensePatch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il, MethodBase mb)
{
FieldInfo LcachedDrawEntries = AccessTools.Field(typeof(StatsReportUtility), "cachedDrawEntries");
MethodInfo LAddRange = AccessTools.Method(typeof(List<StatDrawEntry>), "AddRange");
bool b = false;
foreach (var i in (instructions))
{
yield return i;
if (!b && i.opcode == OpCodes.Brfalse)
{
b = true;
yield return new CodeInstruction(OpCodes.Ldsfld, LcachedDrawEntries);
yield return new CodeInstruction(OpCodes.Ldarg_1);
yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(TextChanges), nameof(TextChanges.CategoryEntryRow)));
yield return new CodeInstruction(OpCodes.Callvirt, LAddRange);
}
}
}
}
[HarmonyPatch(typeof(ThingFilter), nameof(ThingFilter.Summary), MethodType.Getter)]
public static class ThingFilter_Summary_CommonSensePatch
{
public static void Postfix(ThingFilter __instance, ref string __result)
{
if (__instance == null || !Settings.gui_extended_recipe)
return;
if (!__instance.customSummary.NullOrEmpty() && __instance.customSummary != "UsableIngredients".Translate())
{
return;
//__result = __instance.customSummary;
}
var thingDefs = Traverse.Create(__instance).Field("thingDefs").GetValue() as List<ThingDef>;
var categories = Traverse.Create(__instance).Field("categories").GetValue() as List<string>;
var allowAllWhoCanMake = Traverse.Create(__instance).Field("allowAllWhoCanMake").GetValue() as List<ThingDef>;
var allowedDefs = Traverse.Create(__instance).Field("allowedDefs").GetValue() as HashSet<ThingDef>;
//List<string> tradeTagsToAllow = Traverse.Create(__instance).Field("tradeTagsToAllow").GetValue<List<string>>();
//List<string> tradeTagsToDisallow = Traverse.Create(__instance).Field("tradeTagsToDisallow").GetValue<List<string>>();
//List<string> thingSetMakerTagsToAllow = Traverse.Create(__instance).Field("thingSetMakerTagsToAllow").GetValue<List<string>>();
//List<string> thingSetMakerTagsToDisallow = Traverse.Create(__instance).Field("thingSetMakerTagsToDisallow").GetValue<List<string>>();
//List<string> disallowedCategories = Traverse.Create(__instance).Field("disallowedCategories").GetValue<List<string>>();
//List<string> specialFiltersToAllow = Traverse.Create(__instance).Field("specialFiltersToAllow").GetValue<List<string>>();
//List<string> specialFiltersToDisallow = Traverse.Create(__instance).Field("specialFiltersToDisallow").GetValue<List<string>>();
//List<StuffCategoryDef> stuffCategoriesToAllow = Traverse.Create(__instance).Field("stuffCategoriesToAllow").GetValue<List<StuffCategoryDef>>();
//FoodPreferability disallowWorsePreferability = Traverse.Create(__instance).Field("disallowWorsePreferability").GetValue<FoodPreferability>();
//bool disallowInedibleByHuman = Traverse.Create(__instance).Field("disallowInedibleByHuman").GetValue<bool>();
//Type allowWithComp = Traverse.Create(__instance).Field("allowWithComp").GetValue<Type>();
//Type disallowWithComp = Traverse.Create(__instance).Field("disallowWithComp").GetValue<Type>();
//float disallowCheaperThan = Traverse.Create(__instance).Field("disallowCheaperThan").GetValue<float>();
//List<ThingDef> disallowedThingDefs = Traverse.Create(__instance).Field("disallowedThingDefs").GetValue<List<ThingDef>>();
if (!categories.NullOrEmpty())
{
__result = DefDatabase<ThingCategoryDef>.GetNamed(categories[0])?.label ?? "";
for (int i = 1; i < categories.Count; i++)
__result += ", " + DefDatabase<ThingCategoryDef>.GetNamed(categories[i])?.label ?? "";
}
else if (!allowAllWhoCanMake.NullOrEmpty())
{
HashSet<StuffCategoryDef> l = new HashSet<StuffCategoryDef>();
foreach (var c in (allowAllWhoCanMake))
if(c != null)
l.AddRange(c.stuffCategories);
__result = "";
foreach (var def in l)
__result += __result == "" ? def?.label?.CapitalizeFirst() ?? "" : ", " + def?.label?.CapitalizeFirst() ?? "";
}
else if (allowedDefs != null && allowedDefs.Count > 0)
{
__result = "";
foreach (var thing in allowedDefs)
__result += __result == "" ? thing?.label ?? "" : ", " + thing?.label ?? "";
}
else if (!thingDefs.NullOrEmpty())
{
__result = thingDefs[0]?.label ?? "";
for (int i = 1; i < thingDefs.Count; i++)
__result += ", " + thingDefs[i]?.label ?? "";
}
else __result = "UsableIngredients".Translate();
__instance.customSummary = __result;
return;// false;
}
}
}
}