-
Notifications
You must be signed in to change notification settings - Fork 7
/
CustomBarnKit.cs
279 lines (247 loc) · 12.3 KB
/
CustomBarnKit.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using CommNet;
using System;
using System.Reflection;
using UnityEngine;
using Upgradeables;
namespace CustomBarnKit
{
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class CustomBarnKit : MonoBehaviour
{
internal static CustomGameVariables customGameVariables;
private static bool varLoaded = false;
private static bool brokeStuff = false;
public void Start()
{
if (!varLoaded)
{
customGameVariables = GameVariables.Instance.gameObject.AddComponent<CustomGameVariables>();
customGameVariables.Load(GameVariables.Instance);
GameVariables.Instance = customGameVariables;
// Make sure the network uses the new config. I could not find an event that only update the TrackingStation
// But I guess something exist for when you update it....
if (CommNetNetwork.Instance != null)
CommNetNetwork.Reset();
log(customGameVariables.ToString());
// We have to reload everything at each scene changes because the game mess up some of the values...
GameEvents.onLevelWasLoaded.Add(LoadUpgradesPricesSceneChange);
// The "Switch Editor" button also reset everything
GameEvents.onEditorRestart.Add(LoadUpgradesPrices);
BreakStuff();
}
}
private void BreakStuff()
{
if (brokeStuff)
return;
MethodInfo originalCall = typeof(ModuleEvaChute).GetMethod("CanCrewMemberUseParachute", BindingFlags.Public | BindingFlags.Static);
MethodInfo improvedCall = typeof(CustomGameVariables).GetMethod("CanCrewMemberUseParachute", BindingFlags.Public | BindingFlags.Static);
if (originalCall != null && improvedCall != null)
Detourer.TryDetourFromTo(originalCall, improvedCall);
else
print("BreakStuff " + (originalCall != null) + " " + (improvedCall != null));
brokeStuff = true;
}
//private float nextTick = 0;
public void Update()
{
if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.T))
{
customGameVariables.Test();
}
//if (((EditorDriver.fetch != null && EditorDriver.fetch.restartingEditor )|| Time.unscaledTime > nextTick) && customGameVariables != null)
//{
// nextTick = Time.unscaledTime + 5;
// StringBuilder sb = new StringBuilder();
// sb.AppendLine();
// sb.AppendLine($" {EditorDriver.fetch != null && EditorDriver.fetch.restartingEditor} {customGameVariables.levelsTracking:F3} {customGameVariables.upgradesTracking.Length}");
// foreach (ScenarioUpgradeableFacilities.ProtoUpgradeable facility in ScenarioUpgradeableFacilities.protoUpgradeables.Values)
// {
// if (facility.facilityRefs.Count > 0)
// {
// sb.AppendLine(facility.facilityRefs.First().id +
// " lvl " + facility.facilityRefs.First().FacilityLevel +
// " / " + facility.facilityRefs.First().MaxLevel +
// " = " + facility.facilityRefs.First().GetNormLevel());
// sb.AppendLine( facility.configNode.CountNodes + " " + facility.configNode.CountValues + " " + facility.configNode.ToString());
// }
// }
// sb.AppendLine("------------");
// log(sb.ToString());
//}
}
// With the help of NoMoreGrind code by nlight
private void LoadUpgradesPricesSceneChange(GameScenes data)
{
if (data == GameScenes.MAINMENU || data == GameScenes.SETTINGS || data == GameScenes.CREDITS)
return;
LoadUpgradesPrices();
}
private void LoadUpgradesPrices()
{
log("Loading new upgrades prices");
foreach (UpgradeableFacility facility in GameObject.FindObjectsOfType<UpgradeableFacility>())
{
SpaceCenterFacility facilityType;
try
{
facilityType = (SpaceCenterFacility)Enum.Parse(typeof(SpaceCenterFacility), facility.name);
}
catch (ArgumentException)
{
// Mods can add new Facilities that are not in the stock enum list.
continue;
}
float[] prices = getFacilityUpgradePrices(facilityType);
int levels = getFacilityLevels(facilityType);
int[] levelsVisual = getFacilityLevelsVisual(facilityType);
if (prices == null)
{
log("No upgrades prices set for " + facilityType + ". Skipping");
continue;
}
if (levels != prices.Length)
{
log("Wrong numbers of upgrade price for " + facility + " expecting " + levels + " and have " + prices.Length + ". Check your configs");
continue;
}
UpgradeableObject.UpgradeLevel[] upgradeLevels = facility.UpgradeLevels;
for (int i = 0; i < upgradeLevels.Length; i++)
{
UpgradeableObject.UpgradeLevel oldLevel = upgradeLevels[i];
if (oldLevel.Spawned)
oldLevel.Despawn();
}
UpgradeableObject.UpgradeLevel[] newUpgradeLevels = new UpgradeableObject.UpgradeLevel[levels];
for (int i = 0; i < levels; i++)
{
int originalLevel = Math.Min(i, upgradeLevels.Length - 1);
UpgradeableObject.UpgradeLevel level = new UpgradeableObject.UpgradeLevel();
var sourceLvl = upgradeLevels[originalLevel];
level.Setup(facility);
level.levelCost = prices[i];
level.levelText = sourceLvl.levelText;
level.levelStats = new KSCFacilityLevelText
{
facility = sourceLvl.levelStats.facility,
linePrefix = sourceLvl.levelStats.linePrefix,
textBase = sourceLvl.levelStats.textBase
};
level.facilityPrefab = sourceLvl.facilityPrefab;
level.facilityInstance = null;
// Only redo the visual on the first load. Doing it on the next loads would mess up the order.
if (!varLoaded)
{
if (levelsVisual.Length == levels)
{
//log(facility.name + " Copying level " + (levelsVisual[i] - 1) + " for level " + (i + 1));
level.facilityPrefab = upgradeLevels[levelsVisual[i] - 1].facilityPrefab;
}
else
{
log("Wrong levelsVisual length " + levelsVisual.Length + " for " + facility.name + " expected " + levels);
}
}
newUpgradeLevels[i] = level;
}
facility.UpgradeLevels = newUpgradeLevels;
// Use the current save state to (re)init the facility
if (ScenarioUpgradeableFacilities.protoUpgradeables.TryGetValue( facility.id, out ScenarioUpgradeableFacilities.ProtoUpgradeable protoUpgradeable) &&
protoUpgradeable.configNode != null)
{
facility.Load(protoUpgradeable.configNode);
}
else
{
facility.SetupLevels();
facility.setLevel(HighLogic.CurrentGame.Mode == Game.Modes.CAREER ? facility.FacilityLevel : facility.MaxLevel);
}
}
if (!varLoaded)
log("New upgrades prices are Loaded");
varLoaded = true;
}
private float[] getFacilityUpgradePrices(SpaceCenterFacility f)
{
switch (f)
{
case SpaceCenterFacility.Administration:
return customGameVariables.upgradesAdministration;
case SpaceCenterFacility.AstronautComplex:
return customGameVariables.upgradesAstronauts;
case SpaceCenterFacility.LaunchPad:
return customGameVariables.upgradesLaunchPad;
case SpaceCenterFacility.MissionControl:
return customGameVariables.upgradesMission;
case SpaceCenterFacility.ResearchAndDevelopment:
return customGameVariables.upgradesRnD;
case SpaceCenterFacility.Runway:
return customGameVariables.upgradesRunway;
case SpaceCenterFacility.SpaceplaneHangar:
return customGameVariables.upgradesSPH;
case SpaceCenterFacility.TrackingStation:
return customGameVariables.upgradesTracking;
case SpaceCenterFacility.VehicleAssemblyBuilding:
return customGameVariables.upgradesVAB;
default:
return customGameVariables.upgradesTracking;
}
}
private int getFacilityLevels(SpaceCenterFacility f)
{
switch (f)
{
case SpaceCenterFacility.Administration:
return customGameVariables.levelsAdministration;
case SpaceCenterFacility.AstronautComplex:
return customGameVariables.levelsAstronauts;
case SpaceCenterFacility.LaunchPad:
return customGameVariables.levelsLaunchPad;
case SpaceCenterFacility.MissionControl:
return customGameVariables.levelsMission;
case SpaceCenterFacility.ResearchAndDevelopment:
return customGameVariables.levelsRnD;
case SpaceCenterFacility.Runway:
return customGameVariables.levelsRunway;
case SpaceCenterFacility.SpaceplaneHangar:
return customGameVariables.levelsSPH;
case SpaceCenterFacility.TrackingStation:
return customGameVariables.levelsTracking;
case SpaceCenterFacility.VehicleAssemblyBuilding:
return customGameVariables.levelsVAB;
default:
return customGameVariables.levelsTracking;
}
}
private int[] getFacilityLevelsVisual(SpaceCenterFacility f)
{
switch (f)
{
case SpaceCenterFacility.Administration:
return customGameVariables.upgradesVisualAdministration;
case SpaceCenterFacility.AstronautComplex:
return customGameVariables.upgradesVisualAstronauts;
case SpaceCenterFacility.LaunchPad:
return customGameVariables.upgradesVisualLaunchPad;
case SpaceCenterFacility.MissionControl:
return customGameVariables.upgradesVisualMission;
case SpaceCenterFacility.ResearchAndDevelopment:
return customGameVariables.upgradesVisualRnD;
case SpaceCenterFacility.Runway:
return customGameVariables.upgradesVisualRunway;
case SpaceCenterFacility.SpaceplaneHangar:
return customGameVariables.upgradesVisualSPH;
case SpaceCenterFacility.TrackingStation:
return customGameVariables.upgradesVisualTracking;
case SpaceCenterFacility.VehicleAssemblyBuilding:
return customGameVariables.upgradesVisualVAB;
default:
return customGameVariables.upgradesVisualTracking;
}
}
public static void log(string s)
{
MonoBehaviour.print(string.Format("[CustomBarnKit] {0}", s));
}
}
}