-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
401 lines (371 loc) · 16.3 KB
/
Plugin.cs
File metadata and controls
401 lines (371 loc) · 16.3 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using PersistentPurchases;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Pool;
namespace PersistentPurchases
{
[BepInPlugin("beeisyou.PersistentPurchases", "Persistent Purchases", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
private static ConfigEntry<bool> resetSuits;
private static ConfigEntry<bool> resetFurniture;
private static ConfigEntry<bool> resetUpgrades;
private static ConfigEntry<bool> defaultPlacement;
public static ManualLogSource log = new ManualLogSource("Persistent Purchases");
public static Harmony harmony = new Harmony("beeisyou.PersistentPurchases");
private void Awake()
{
BepInEx.Logging.Logger.Sources.Add(log);
resetSuits = Config.Bind("Resetting", "ResetSuits", false, "Remove all but the orange suit on game over");
resetFurniture = Config.Bind("Resetting", "ResetFurniture", false, "Remove cosmetic purchases on game over");
resetUpgrades = Config.Bind("Resetting", "ResetUpgrades", true, "Remove ship upgrades on game over");
defaultPlacement = Config.Bind("Resetting", "DefaultPlacement", true, "Reset objects to their default position, and put everything else in storage");
harmony.PatchAll(typeof(Patches));
harmony.PatchAll(typeof(ResetSavedGameValues));
harmony.PatchAll(typeof(ResetShip));
harmony.PatchAll(typeof(InSanityCheck));
log.LogMessage("Plugin Persistent Purchases is loaded!");
}
public static int[] suits = { 0, 1, 2, 3 }; // orange, green, yellow, pajamas
public static int[] upgrades = { 5, 18, 19 }; // teleporter, horn, inv teleporter
public static int[] defaults = { 7, 8, 11, 15, 16 }; // cupboard, cabinet, light switch, bunkbeds, terminal
public static bool shouldReset(int id, string debugType="")
{
if (suits.Contains(id))
{
if (!resetSuits.Value)
{
log.LogInfo($"Not resetting {id} {debugType}");
return false;
}
}
else if (upgrades.Contains(id))
{
if (!resetUpgrades.Value) {
log.LogInfo($"Not resetting {id} {debugType}");
return false;
}
}
else if (!resetFurniture.Value)
{
log.LogInfo($"Not resetting {id} {debugType}");
return false;
}
if (defaults.Contains(id))
{
log.LogInfo($"Not resetting {id} {debugType}");
return false;
}
log.LogInfo($"Is resetting {id} {debugType}");
return true;
}
public static bool shouldDefault()
{
return defaultPlacement.Value;
}
}
}
[HarmonyPatch]
public class Patches
{
[HarmonyPrefix, HarmonyPatch(typeof(GameNetworkManager), nameof(GameNetworkManager.ResetUnlockablesListValues))]
public static bool dontResetAnything()
{
if (StartOfRound.Instance != null)
{
UnityEngine.Debug.Log("CONDITIONALLY resetting unlockables list!");
List<UnlockableItem> list = StartOfRound.Instance.unlockablesList.unlockables;
for (int i = 0; i < list.Count; i++)
{
if (Plugin.shouldReset(i, "dontResetAnything")) // the only modified line lol
{
Plugin.log.LogInfo($"Resetting {list[i].unlockableName}");
list[i].hasBeenUnlockedByPlayer = false;
if (list[i].unlockableType == 1)
{
list[i].placedPosition = Vector3.zero;
list[i].placedRotation = Vector3.zero;
list[i].hasBeenMoved = false;
list[i].inStorage = false;
}
}
}
}
return false; // skip original function
}
[HarmonyPostfix, HarmonyPatch(typeof(GameNetworkManager), nameof(GameNetworkManager.ResetSavedGameValues))]
public static void okayMaybeDoALittleResetting()
{
if (ES3.KeyExists("UnlockedShipObjects", GameNetworkManager.Instance.currentSaveFileName))
{
int[] arr = ES3.Load<int[]>("UnlockedShipObjects", GameNetworkManager.Instance.currentSaveFileName);
arr = arr.Where(id => !Plugin.shouldReset(id, "okayMaybeDoALittleResetting1")).ToArray(); // only keep items that should not be reset
ES3.Save("UnlockedShipObjects", arr, GameNetworkManager.Instance.currentSaveFileName);
for (int i = 0; i < StartOfRound.Instance.unlockablesList.unlockables.Count; i++)
{
UnlockableItem item = StartOfRound.Instance.unlockablesList.unlockables[i];
if (Plugin.shouldReset(i, "okayMaybeDoALittleResetting2") && item.unlockableType == 1)
{
ES3.DeleteKey("ShipUnlockMoved_" + item.unlockableName, GameNetworkManager.Instance.currentSaveFileName);
ES3.DeleteKey("ShipUnlockStored_" + item.unlockableName, GameNetworkManager.Instance.currentSaveFileName);
ES3.DeleteKey("ShipUnlockPos_" + item.unlockableName, GameNetworkManager.Instance.currentSaveFileName);
ES3.DeleteKey("ShipUnlockRot_" + item.unlockableName, GameNetworkManager.Instance.currentSaveFileName);
}
}
}
}
[HarmonyPostfix, HarmonyPatch(typeof(StartOfRound), nameof(StartOfRound.ResetShip))]
public static void unfuckObjects()
{
PlaceableShipObject[] array = UnityEngine.Object.FindObjectsOfType<PlaceableShipObject>();
for (int i = 0; i < array.Length; i++)
{
UnlockableItem item = StartOfRound.Instance.unlockablesList.unlockables[array[i].unlockableID];
if (item.spawnPrefab)
{
if (item.hasBeenUnlockedByPlayer && !Plugin.shouldReset(array[i].unlockableID))
{
if (Plugin.shouldDefault())
{
item.inStorage = true;
}
}
else
{
item.hasBeenUnlockedByPlayer = false;
item.inStorage = false;
}
Collider[] componentsInChildren = array[i].parentObject.GetComponentsInChildren<Collider>();
for (int j = 0; j < componentsInChildren.Length; j++)
{
componentsInChildren[j].enabled = true;
}
}
else if(Plugin.shouldDefault())
{
if (item.alreadyUnlocked)
{
item.inStorage = false;
array[i].parentObject.disableObject = false;
ShipBuildModeManager.Instance.ResetShipObjectToDefaultPosition(array[i]);
}
else
{
if (Plugin.shouldReset(array[i].unlockableID))
{
item.hasBeenUnlockedByPlayer = false;
item.inStorage = false;
}
else
{
item.hasBeenUnlockedByPlayer = true;
item.inStorage = true;
}
array[i].parentObject.disableObject = true;
ShipBuildModeManager.Instance.StoreObjectServerRpc(
array[i].parentObject.GetComponent<NetworkObject>(),
(int)GameNetworkManager.Instance.localPlayerController.playerClientId
);
}
}
}
for (int i = 0; i < StartOfRound.Instance.unlockablesList.unlockables.Count; i++)
{
UnlockableItem item = StartOfRound.Instance.unlockablesList.unlockables[i];
if (!item.alreadyUnlocked && item.spawnPrefab && Plugin.shouldReset(i))
{
GameObject gameObject;
if (!StartOfRound.Instance.SpawnedShipUnlockables.TryGetValue(i, out gameObject))
{
StartOfRound.Instance.SpawnedShipUnlockables.Remove(i);
}
else if (gameObject == null)
{
StartOfRound.Instance.SpawnedShipUnlockables.Remove(i);
}
else
{
StartOfRound.Instance.SpawnedShipUnlockables.Remove(i);
NetworkObject component = gameObject.GetComponent<NetworkObject>();
if (component != null && component.IsSpawned)
{
component.Despawn(true);
}
}
}
}
}
}
[HarmonyPatch(typeof(GameNetworkManager), nameof(GameNetworkManager.ResetSavedGameValues))]
public class ResetSavedGameValues {
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
Plugin.log.LogWarning("Beginning transpilation of GameNetworkManager.ResetSavedGameValues()");
var codes = new List<CodeInstruction>(instructions);
int rm = -1;
List<int> rm2 = new List<int>();
for (var i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Ldstr)
{
if (codes[i].operand.ToString() == "UnlockedShipObjects")
{
rm = i;
Plugin.log.LogInfo($"Found ship object opcode at {i}");
}
else if (codes[i].operand.ToString().EndsWith("_"))
{
rm2.Add(i);
Plugin.log.LogInfo($"Found placeable ship object data opcode at {i}");
}
}
}
for (int i = rm2.Count - 1; i >= 0; i--)
{
/*
* REMOVES:
* ldstr [string name_]
* ldloc.1
* ldfld class UnlockablesList StartOfRound::unlockablesList
* ldfld class [netstandard]System.Collections.Generic.List`1<class UnlockableItem> UnlockablesList::unlockables
* ldloc.2
* callvirt instance !0 class [netstandard]System.Collections.Generic.List`1<class UnlockableItem>::get_Item(int32)
* ldfld string UnlockableItem::unlockableName
* call string [netstandard]System.String::Concat(string, string)
* ldarg.0
* ldfld string GameNetworkManager::currentSaveFileName
* call void ['Assembly-CSharp-firstpass']ES3::DeleteKey(string, string)
* AKA:
* ES3.DeleteKey([string name_] + startOfRound.unlockablesList.unlockables[i].unlockableName, this.currentSaveFileName);
*/
codes.RemoveRange(rm2[i], 11);
}
if (rm >= 0)
{
/*
* REMOVES:
* ldstr [string name]
* call class GameNetworkManager GameNetworkManager::get_Instance()
* ldfld string GameNetworkManager::currentSaveFileName
* call void ['Assembly-CSharp-firstpass']ES3::DeleteKey(string, string)
* AKA:
* ES3.DeleteKey("UnlockedShipObjects", GameNetworkManager.Instance.currentSaveFileName);
*/
codes.RemoveRange(rm, 4);
}
return codes.AsEnumerable();
}
}
[HarmonyPatch(typeof(StartOfRound), nameof(StartOfRound.ResetShip))]
public class ResetShip {
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
Plugin.log.LogWarning("Beginning transpilation of StartOfRound.ResetShip()");
var codes = new List<CodeInstruction>(instructions);
int prefabI1 = -1;
CodeInstruction prefabJ1 = null;
int prefabI2 = -1;
CodeInstruction prefabJ2 = null;
int p2 = -1;
for (var i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Ldfld && codes[i].operand.ToString() == "System.Boolean spawnPrefab")
{
if (prefabI1 == -1)
{
prefabI1 = i + 2;
prefabJ1 = codes[i + 1].Clone();
prefabJ1.opcode = OpCodes.Brfalse_S;
Plugin.log.LogInfo($"Found ship object prefab opcode at {i} (1)");
}
else if (prefabI2 == -1)
{
prefabI2 = i + 2;
prefabJ2 = codes[i + 1].Clone();
prefabJ2.opcode = OpCodes.Brfalse_S;
Plugin.log.LogInfo($"Found ship object prefab opcode at {i} (1)");
}
}
if (codes[i].opcode == OpCodes.Call && codes[i].operand.ToString() == "Void SwitchSuitForPlayer(GameNetcodeStuff.PlayerControllerB, Int32, Boolean)")
{
p2 = i;
Plugin.log.LogInfo($"Found suit reset opcode at {i}");
}
if (codes[i].opcode == OpCodes.Stfld && codes[i].operand.ToString() == "System.Boolean disableObject")
{
// array[j].parentObject.disableObject = false; -> ... = true;
codes[i - 1].opcode = OpCodes.Ldc_I4_1;
}
}
if (p2 >= 0)
{
codes.RemoveRange(p2 - 6, 7);
}
if (prefabI2 >= 0)
{
codes.InsertRange(prefabI2, new CodeInstruction[] {
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Plugin), nameof(Plugin.shouldDefault))),
prefabJ2
});
}
if (prefabI1 >= 0)
{
codes.InsertRange(prefabI1, new CodeInstruction[]
{
new CodeInstruction(OpCodes.Ldloc_3),
new CodeInstruction(OpCodes.Ldstr, "StartOfRound1"),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Plugin), nameof(Plugin.shouldReset))),
prefabJ1
});
}
return codes.AsEnumerable();
}
}
[HarmonyPatch(typeof(StartOfRound), nameof(StartOfRound.SyncShipUnlockablesServerRpc))]
public class InSanityCheck
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
Plugin.log.LogWarning("Beginning transpilation of StartOfRound.SyncShipUnlockablesServerRpc()");
var codes = new List<CodeInstruction>(instructions);
for (int i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Ldstr && codes[i].operand.ToString() == "Server: placeableObject #{0}: {1}")
{
codes.InsertRange(i - 1, new CodeInstruction[]
{
new CodeInstruction(OpCodes.Ldloc_S, 5),
new CodeInstruction(OpCodes.Ldloc_S, 10),
new CodeInstruction(OpCodes.Ldelem_Ref),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(InSanityCheck), nameof(WhatIsGoingOn)))
}
);
Plugin.log.LogWarning("This crash is not evading me");
break;
}
}
return codes.AsEnumerable();
}
public static void WhatIsGoingOn(PlaceableShipObject pso)
{
Plugin.log.LogWarning("Seeing what the fuck is going wrong here with syncing unlockables, prepare for spam");
Plugin.log.LogInfo($"ship object is not null? {pso != null}");
Plugin.log.LogInfo($"ship object transform is not null? {pso.transform != null}");
int id = pso.unlockableID;
Plugin.log.LogInfo($"ship object unlockable id {id}");
Plugin.log.LogInfo($"unlockables has id? {id < StartOfRound.Instance.unlockablesList.unlockables.Count}");
UnlockableItem item = StartOfRound.Instance.unlockablesList.unlockables[id];
Plugin.log.LogInfo($"unlockable is not null? {item != null}");
Plugin.log.LogInfo($"unlockable position {item.placedPosition}");
Plugin.log.LogInfo($"unlockable rotation {item.placedPosition}");
Plugin.log.LogInfo($"unlockable in storage? {item.inStorage}");
}
}