-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scaffolds_Patch.cs
197 lines (179 loc) · 7.28 KB
/
Scaffolds_Patch.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System.Collections.Generic;
using System;
using HarmonyLib;
using STRINGS;
using KMod;
using UnityEngine;
using PeterHan.PLib.Core;
using PeterHan.PLib.Options;
namespace Scaffolds
{
public sealed class Scaffolds_Patch : UserMod2
{
public static ScaffoldsSettings Settings { get; private set; }
public override void OnLoad(Harmony harmony)
{
base.OnLoad(harmony);
PUtil.InitLibrary(false);
POptions pOptions = new POptions();
pOptions.RegisterOptions(this, typeof(ScaffoldsSettings));
#if DEBUG
Debug.Log("[Scaffolds] Reading settings");
#endif
Settings = POptions.ReadSettings<ScaffoldsSettings>() ?? new ScaffoldsSettings();
#if DEBUG
Debug.Log("[Scaffolds] Loaded");
#endif
}
public static class ScaffoldsPatches
{
[HarmonyPatch(typeof(GeneratedBuildings))]
[HarmonyPatch(nameof(GeneratedBuildings.LoadGeneratedBuildings))]
public static class GeneratedBuildings_LoadGeneratedBuildings_Patch
{
public static void Prefix()
{
// Add scaffold to build menu with the help of utils below
#if DEBUG
Debug.Log("[Scaffolds] Translating");
#endif
Loc_Initialize_Patch.Translate(typeof(ScaffoldConfig));
#if DEBUG
Debug.Log("[Scaffolds] Adding Scaffold to build menu");
#endif
Utils.AddBuildingStrings(ScaffoldConfig.ID, ScaffoldConfig.DisplayName, ScaffoldConfig.Description, ScaffoldConfig.Effect);
Utils.AddPlan("Base", "ladders", ScaffoldConfig.ID, "Ladder");
}
}
[HarmonyPatch(typeof(ProductInfoScreen))]
[HarmonyPatch(nameof(ProductInfoScreen.SetMaterials))]
public static class ProductInfoScreen_SetMaterials_Patch
{
public static void Postfix(BuildingDef def, ref ProductInfoScreen __instance)
{
if (def.name == "Scaffold")
{
#if DEBUG
Debug.Log("[Scaffolds] Removing material selector from Scaffold build menu info screen");
#endif
__instance.materialSelectionPanel.gameObject.SetActive(false); //remove material selector since no materials
}
}
}
[HarmonyPatch(typeof(ResourceRemainingDisplayScreen))]
[HarmonyPatch(nameof(ResourceRemainingDisplayScreen.GetString))]
public static class ResourceRemainingDisplayScreen_Patch
{
// This patch overwrites the 'sandstone 500/1kg' on the hover text card when building scaffolds.
// It checks the BuildingDef in the hover card, since it's public, rather than BuildTool itself
// It also checks to make sure that it's exactly 1kg mass - draggable items (wires, pipes etc) otherwise cause errors since they use a drag tool not build tool
public static string Postfix(string __result, Recipe ___currentRecipe)
{
if (___currentRecipe.Ingredients[0].amount == 1f)
{
if (BuildTool.Instance.GetComponent<BuildToolHoverTextCard>().currentDef.name == "Scaffold")
{
#if DEBUG
Debug.Log("[Scaffolds] Overwriting hover text card to show it is a free insta-build");
#endif
__result = ScaffoldConfig.Free_insta_build;
}
}
return __result;
}
}
[HarmonyPatch(typeof(BuildingDef))]
[HarmonyPatch(nameof(BuildingDef.Instantiate))]
public static class BuildingDef_Instantiate_Patch
{
public static bool Prefix(Vector3 pos, Orientation orientation, IList<Tag> selected_elements, int layer, BuildingDef __instance, ref GameObject __result)
{
// This instantiate function is used to create the construction site when a valid building spot is selected
// since we want instant build, it detects if a scaffold is being built and instant-builds if so
// It uses same build command called in sandbox
BuildingDef def = __instance;
if (__instance.name != "Scaffold")
{ return true; }
else
{
#if DEBUG
Debug.Log($"[Scaffolds] Building Scaffold at {pos} -> {Grid.PosToCell(pos)}");
#endif
selected_elements[0] = TagManager.Create("Vacuum"); //sets to vacuum element to prevent heat exchange... this must be dealt with at deconstruct or it will cause a crash
__instance.Build(Grid.PosToCell(pos), orientation, null, selected_elements, 293.15f, playsound: true, GameClock.Instance.GetTime());
return false; // Any better ideas for ways to accomplish this instant build?
}
}
}
[HarmonyPatch(typeof(FilteredDragTool))]
[HarmonyPatch(nameof(FilteredDragTool.GetFilterLayerFromObjectLayer))]
public static class FilteredDragTool_GetFilterLayerFromObjectLayer_Patch
{
public static void Postfix(ObjectLayer gamer_layer, ref string __result)
{
#if DEBUG
Debug.Log($"[Scaffolds] Postfixing FilteredDragTool.GetFilterLayerFromObjectLayer with {gamer_layer} and {__result}");
#endif
if (gamer_layer == ScaffoldConfig.ObjectLayer)
{
#if DEBUG
Debug.Log($"[Scaffolds] Overriding FilteredDragTool.GetFilterLayerFromObjectLayer with {ToolParameterMenu.FILTERLAYERS.BUILDINGS}");
#endif
__result = ToolParameterMenu.FILTERLAYERS.BUILDINGS;
}
}
}
}
}
// TODO: Switch to using PLib for these build menu utilities
public static class Utils
{
// Copied many times, originally from romen
public static void AddBuildingStrings(string buildingId, string name, string description, string effect)
{
Strings.Add($"STRINGS.BUILDINGS.PREFABS.{buildingId.ToUpperInvariant()}.NAME", UI.FormatAsLink(name, buildingId));
Strings.Add($"STRINGS.BUILDINGS.PREFABS.{buildingId.ToUpperInvariant()}.DESC", description);
Strings.Add($"STRINGS.BUILDINGS.PREFABS.{buildingId.ToUpperInvariant()}.EFFECT", effect);
}
//thanks psyko for new building adding methods
public static void AddPlan(HashedString category, string subcategory, string idBuilding, string addAfter = null)
{
#if DEBUG
Debug.Log("[Scaffolds] Adding " + idBuilding + " to category " + category);
#endif
foreach (PlanScreen.PlanInfo menu in TUNING.BUILDINGS.PLANORDER)
{
if (menu.category == category)
{
AddPlanToCategory(menu, subcategory, idBuilding, addAfter);
return;
}
}
#if DEBUG
Debug.Log($"[Scaffolds] Unknown build menu category: ${category}");
#endif
}
private static void AddPlanToCategory(PlanScreen.PlanInfo menu, string subcategory, string idBuilding, string addAfter = null)
{
List<KeyValuePair<string, string>> data = menu.buildingAndSubcategoryData;
if (data != null)
{
if (addAfter == null)
{
data.Add(new KeyValuePair<string, string>(idBuilding, subcategory));
}
else
{
int index = data.IndexOf(new KeyValuePair<string, string>(addAfter, subcategory));
if (index == -1)
{
Debug.Log($"[Scaffolds] Could not find building {subcategory}/{addAfter} to add {idBuilding} after. Adding at the end !");
data.Add(new KeyValuePair<string, string>(idBuilding, subcategory));
return;
}
data.Insert(index + 1, new KeyValuePair<string, string>(idBuilding, subcategory));
}
}
}
}
}