-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cs
More file actions
501 lines (457 loc) · 24 KB
/
main.cs
File metadata and controls
501 lines (457 loc) · 24 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using static TrapController.TrapController;
using static TrapController.SharedState;
namespace TrapController
{
public static class SharedState
{
public static Dictionary<string, string> AvailableTrapOptions = new Dictionary<string, string>()
{
["animalcorpse"] = "Animal Corpse",
["bananaplant"] = "Banana Plant",
["barbedwirefence"] = "Barbed Wire Fence",
["beartrap"] = "Bear Trap",
["browncap"] = "Brown Cap",
["brownshroom"] = "Brown Shroom",
["cactus"] = "Cactus",
["CaveTicks"] = "Cave Ticks",
["ceilingrye"] = "Ceiling Rye",
["coil"] = "Coil",
["containercrate"] = "Container Crate",
["corpse"] = "Corpse",
["drillpod"] = "Drill Pod",
["droppings"] = "Droppings",
["drybush"] = "Dry Bush",
["foodbox"] = "Food Box",
["geotree"] = "Geo Tree",
["geyser"] = "Geyser",
["glowplant"] = "Glow Plant",
["grabberplant"] = "Grabber Plant",
["jumppad"] = "Jump Pad",
["landmine"] = "Land Mine",
["leadbush"] = "Lead Bush",
["medcrate"] = "Med Crate",
["mushplant"] = "Mush Plant",
["overgrowntick"] = "Overgrown Tick",
["plantiation"] = "Plantiation",
["rag"] = "Rag",
["sandrose"] = "Sand Rose",
["shadecrawler"] = "Shade Crawler",
["skullcrusher"] = "Skullcrusher",
["soundcannon"] = "Sound Cannon",
["spentfuel"] = "Spent Fuel",
["spikestabber"] = "Spike Stabber",
["stalagmite"] = "Stalagmite",
["stoneplant"] = "Stone Plant",
["thornbackyoung"] = "Thornback Young",
["wallbiter"] = "Wallbiter"
};
public static List<string> TrapsDisabled = new List<string>();
public static bool pulledEntities = false;
public static Harmony harmony;
}
[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
public class TrapController : BaseUnityPlugin
{
public static ManualLogSource logger;
public const string pluginGuid = "shushu.casualtiesunknown.trapcontroller";
public const string pluginName = "Trap Controller";
// Year.Month.Version.Bugfix
public const string pluginVersion = "26.1.1.0";
public static TrapController Instance;
public static int isOkayToPatch = 0;
public static string activeVersion = "";
public void Awake()
{
Instance = this;
logger = Logger;
logger.LogInfo("Awake() ran - mod loaded!");
harmony = new Harmony(pluginGuid);
StartCoroutine(CheckGameVersion(harmony));
}
public static void Log(string message)
{
logger.LogInfo(message);
}
public static IEnumerator CheckGameVersion(Harmony harmony)
{
harmony.Patch(AccessTools.Method(typeof(PreRunScript), "Awake"), prefix: new HarmonyMethod(typeof(TrapController).GetMethod("VersionCheck")));
while (true)
{
if (isOkayToPatch == 1)
{
break;
}
if (isOkayToPatch == -1)
{
harmony.Unpatch(AccessTools.Method(typeof(PreRunScript), "Awake"), HarmonyPatchType.Prefix);
logger.LogError($"Game version is not {activeVersion}, mod exiting...");
yield break;
}
yield return null;
}
harmony.Unpatch(AccessTools.Method(typeof(PreRunScript), "Awake"), HarmonyPatchType.Prefix);
List<MethodInfo> patches = typeof(MyPatches).GetMethods(BindingFlags.Static | BindingFlags.Public).ToList();
foreach (MethodInfo patch in patches)
{
try
{
string[] splitName = patch.Name.Replace("__", "$").Split('_');
for (int i = 0; i < splitName.Length; i++)
splitName[i] = splitName[i].Replace("$", "_");
if (splitName.Length < 3)
throw new Exception($"Patch method is named incorrectly\nPlease make sure the Patch method is named in the following pattern:\n\tTargetClass_TargetMethod_PatchType[_Version]");
if (splitName.Length >= 4)
if (splitName[3] != activeVersion)
{
Log($"{patch.Name} is not supported by version {activeVersion}");
continue;
}
string targetType = splitName[0];
MethodType targetMethodType;
if (splitName[1].Contains("get_"))
targetMethodType = MethodType.Getter;
else if (splitName[1].Contains("set_"))
targetMethodType = MethodType.Setter;
else
targetMethodType = MethodType.Normal;
string ogTargetMethod = splitName[1];
string targetMethod = splitName[1].Replace("get_", "").Replace("set_", "");
string patchType = splitName[2];
MethodInfo ogScript = null;
switch (targetMethodType)
{
case MethodType.Enumerator:
case MethodType.Normal:
ogScript = AccessTools.Method(AccessTools.TypeByName(targetType), targetMethod);
break;
case MethodType.Getter:
ogScript = AccessTools.PropertyGetter(AccessTools.TypeByName(targetType), targetMethod);
break;
case MethodType.Setter:
case MethodType.Constructor:
case MethodType.StaticConstructor:
default:
throw new Exception($"Unknown patch method\nPatch method type \"{targetMethodType}\" currently has no handling");
}
MethodInfo patchScript = typeof(MyPatches).GetMethod(patch.Name);
List<string> validPatchTypes = new List<string>
{
"Prefix",
"Postfix",
"Transpiler"
};
if (ogScript == null || patchScript == null || !validPatchTypes.Contains(patchType))
{
throw new Exception("Patch method is named incorrectly\nPlease make sure the Patch method is named in the following pattern:\n\tTargetClass_TargetMethod_PatchType[_Version]");
}
HarmonyMethod harmonyMethod = new HarmonyMethod(patchScript)
{
methodType = targetMethodType
};
HarmonyMethod postfix = null;
HarmonyMethod prefix = null;
HarmonyMethod transpiler = null;
switch (patchType)
{
case "Prefix":
prefix = harmonyMethod;
break;
case "Postfix":
postfix = harmonyMethod;
break;
case "Transpiler":
transpiler = harmonyMethod;
break;
}
harmony.Patch(ogScript, prefix: prefix, postfix: postfix, transpiler: transpiler);
Log("Patched " + targetType + "." + targetMethod + " as a " + patchType);
}
catch (Exception exception)
{
logger.LogError($"Failed to patch {patch.Name}");
logger.LogError(exception);
}
}
foreach (KeyValuePair<string, string> modifier in AvailableTrapOptions)
{
int modActive = PlayerPrefs.GetInt("TrapController_" + modifier.Key.Replace(" ", "_"), 1);
if (modActive == 0)
TrapsDisabled.Add(modifier.Key);
}
// If you have any PreRunScript Awake/Start patches, invoke them here
PreRunScript prerunscript = GameObject.FindAnyObjectByType<PreRunScript>();
AccessTools.Method(typeof(MyPatches), "PreRunScript_Start_Prefix").Invoke(prerunscript, new UnityEngine.Object[] { prerunscript });
}
public static void VersionCheck()
{
Dictionary<string, string[]> supportedVersions = new Dictionary<string, string[]>
{
["Text (TMP) (18)"] = new string[] { "V5 Pre-testing 5", "5_5" },
["Text (TMP) (17)"] = new string[] { "V5 Pre-testing 4", "5_4" }
};
foreach (var supportedVersion in supportedVersions)
{
if (isOkayToPatch == 0)
{
GameObject obj = GameObject.Find(supportedVersion.Key);
if (obj == null)
continue;
if (obj.GetComponent<TextMeshProUGUI>().text.Contains(supportedVersion.Value[0]))
{
activeVersion = supportedVersion.Value[1];
isOkayToPatch = 1;
break;
}
}
}
if (isOkayToPatch == 0)
isOkayToPatch = -1;
}
public static IEnumerator CreateModeUI()
{
yield return null;
Canvas canvas = GameObject.Find("Canvas")?.GetComponent<Canvas>();
GameObject challengeModeDropdown = new GameObject("TrapControllerOptionsMain");
RectTransform dropdownRect = challengeModeDropdown.AddComponent<RectTransform>();
dropdownRect.SetParent(canvas.transform, false);
dropdownRect.anchorMax = new Vector2(0f, 1f);
dropdownRect.anchorMin = new Vector2(0f, 1f);
dropdownRect.pivot = new Vector2(0f, 1f);
dropdownRect.anchoredPosition = new Vector2(19f, -35f);
VerticalLayoutGroup challengeModeDropdownVLG = challengeModeDropdown.AddComponent<VerticalLayoutGroup>();
challengeModeDropdownVLG.spacing = 15;
challengeModeDropdownVLG.childAlignment = TextAnchor.UpperLeft;
challengeModeDropdownVLG.childControlHeight = false;
challengeModeDropdownVLG.childControlWidth = false;
challengeModeDropdownVLG.childForceExpandHeight = false;
challengeModeDropdownVLG.childForceExpandWidth = false;
challengeModeDropdownVLG.padding.top += 31;
ContentSizeFitter challengeModeDropdownFitter = challengeModeDropdown.AddComponent<ContentSizeFitter>();
challengeModeDropdownFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
challengeModeDropdownFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
GameObject dropdownToggleContainer = new GameObject("TrapsControllerButtonContainer");
RectTransform dropdownToggleContainerRect = dropdownToggleContainer.AddComponent<RectTransform>();
dropdownToggleContainerRect.SetParent(challengeModeDropdown.transform, false);
dropdownToggleContainerRect.anchorMin = new Vector2(0f, 1f);
dropdownToggleContainerRect.anchorMax = new Vector2(0f, 1f);
dropdownToggleContainerRect.pivot = new Vector2(0f, 1f);
LayoutElement dropdownToggleContainerLE = dropdownToggleContainer.AddComponent<LayoutElement>();
dropdownToggleContainerLE.ignoreLayout = true;
GameObject dropdownToggle = new GameObject("TrapsControllerButton");
RectTransform dropdownToggleRect = dropdownToggle.AddComponent<RectTransform>();
dropdownToggleRect.SetParent(dropdownToggleContainer.transform, false);
dropdownToggleRect.anchorMin = new Vector2(0f, 1f);
dropdownToggleRect.anchorMax = new Vector2(0f, 1f);
dropdownToggleRect.pivot = new Vector2(0f, 1f);
dropdownToggleRect.sizeDelta = new Vector2(173f, 31f);
dropdownToggleRect.anchoredPosition = Vector2.zero;
Image dropdownToggleImage = dropdownToggle.AddComponent<Image>();
dropdownToggleImage.sprite = Resources.FindObjectsOfTypeAll<Sprite>().FirstOrDefault(f => f.name.Contains("uiBlockNano"));
dropdownToggleImage.type = Image.Type.Sliced;
Button dropdownToggleButton = dropdownToggle.AddComponent<Button>();
dropdownToggleButton.transition = Selectable.Transition.ColorTint;
dropdownToggleButton.colors = new ColorBlock()
{
normalColor = new Color(1f, 1f, 1f, 1f),
highlightedColor = new Color(0.7453f, 0.7453f, 0.7453f, 1f),
pressedColor = new Color(0.4906f, 0.4906f, 0.4906f, 1f),
selectedColor = new Color(0.9608f, 0.9608f, 0.9608f, 1f),
disabledColor = new Color(0.7843f, 0.7843f, 0.7843f, 0.502f),
colorMultiplier = 1f,
fadeDuration = 0f
};
GameObject dropdownToggleText = new GameObject("TrapsControllerButtonLabel");
dropdownToggleText.transform.SetParent(dropdownToggle.transform, false);
TextMeshProUGUI dropdownToggleTMP = dropdownToggleText.AddComponent<TextMeshProUGUI>();
dropdownToggleTMP.text = "Trap Toggles";
dropdownToggleTMP.font = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().FirstOrDefault(f => f.name.Contains("Retro GamingPix"));
dropdownToggleTMP.fontSize = 15;
ContentSizeFitter dropdownToggleTextFitter = dropdownToggleText.AddComponent<ContentSizeFitter>();
dropdownToggleTextFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
dropdownToggleTextFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
GameObject scrollView = new GameObject("TrapsControllerOptionsScrollView");
RectTransform scrollViewRect = scrollView.AddComponent<RectTransform>();
scrollViewRect.SetParent(challengeModeDropdown.transform, false);
scrollViewRect.sizeDelta = new Vector2(0f, 650f);
ScrollRect scrollRect = scrollView.AddComponent<ScrollRect>();
scrollRect.horizontal = false;
scrollRect.vertical = true;
scrollRect.movementType = ScrollRect.MovementType.Clamped;
scrollRect.inertia = true;
scrollRect.scrollSensitivity = 20f;
GameObject viewport = new GameObject("TrapsControllerOptionsViewport");
RectTransform viewportRect = viewport.AddComponent<RectTransform>();
viewportRect.SetParent(scrollView.transform, false);
viewportRect.anchorMin = Vector2.zero;
viewportRect.anchorMax = Vector2.one;
viewportRect.offsetMin = Vector2.zero;
viewportRect.offsetMax = Vector2.zero;
viewportRect.pivot = new Vector2(0f, 1f);
viewport.AddComponent<RectMask2D>();
scrollRect.viewport = viewportRect;
GameObject scrollbar = new GameObject("TrapsControllerOptionsScrollBar");
RectTransform scrollbarRect = scrollbar.AddComponent<RectTransform>();
scrollbarRect.SetParent(scrollView.transform, false);
scrollbarRect.anchorMin = new Vector2(1f, 0f);
scrollbarRect.anchorMax = Vector2.one;
scrollbarRect.pivot = Vector2.one;
scrollbarRect.sizeDelta = new Vector2(20f, 0f);
scrollbarRect.anchoredPosition = Vector2.zero;
Image scrollbarImage = scrollbar.AddComponent<Image>();
scrollbarImage.color = Color.gray;
Scrollbar scrollbarScroller = scrollbar.AddComponent<Scrollbar>();
scrollbarScroller.direction = Scrollbar.Direction.BottomToTop;
scrollRect.verticalScrollbar = scrollbarScroller;
scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.Permanent;
scrollRect.verticalScrollbarSpacing = -3;
GameObject scrollHandle = new GameObject("TrapsControllerOptionsScrollHandle");
RectTransform scrollHandleRect = scrollHandle.AddComponent<RectTransform>();
scrollHandleRect.SetParent(scrollbar.transform, false);
scrollHandleRect.anchorMin = Vector2.zero;
scrollHandleRect.anchorMax = Vector2.one;
scrollHandleRect.sizeDelta = Vector2.zero;
Image scrollHandleImage = scrollHandle.AddComponent<Image>();
scrollHandleImage.color = Color.white;
scrollbarScroller.handleRect = scrollHandleRect;
GameObject dropdownOptions = new GameObject("TrapsControllerOptions");
dropdownOptions.transform.SetParent(viewport.transform, false);
RectTransform rect = dropdownOptions.AddComponent<RectTransform>();
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = Vector2.zero;
rect.offsetMax = Vector2.zero;
rect.pivot = new Vector2(0f, 1f);
Image dropdownOptionsImage = dropdownOptions.AddComponent<Image>();
dropdownOptionsImage.color = new Color(0, 0, 0, 0.8f);
dropdownOptionsImage.raycastTarget = true;
UITooltip dropdownOptionsUITooltip = dropdownOptions.AddComponent<UITooltip>();
dropdownOptionsUITooltip.skipLocale = true;
dropdownOptionsUITooltip.tipName = "";
dropdownOptionsUITooltip.tipDesc = "";
var layout = dropdownOptions.AddComponent<VerticalLayoutGroup>();
layout.childControlHeight = true;
layout.childControlWidth = true;
layout.spacing = 15;
layout.padding = new RectOffset
{
top = 15,
bottom = 15,
left = 15,
right = 15
};
ContentSizeFitter CSF = dropdownOptions.AddComponent<ContentSizeFitter>();
CSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
CSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
foreach (KeyValuePair<string, string> modifier in AvailableTrapOptions)
{
string key = modifier.Key;
string name = modifier.Value;
GameObject modifierToggle = new GameObject(key);
modifierToggle.transform.SetParent(dropdownOptions.transform, false);
modifierToggle.layer = LayerMask.NameToLayer("UI");
UITooltip modifierToggleUITooltip = modifierToggle.AddComponent<UITooltip>();
modifierToggleUITooltip.skipLocale = true;
modifierToggleUITooltip.tipName = name;
Image modifierToggleRaycastTarget = modifierToggle.AddComponent<Image>();
modifierToggleRaycastTarget.color = new Color(0, 0, 0, 0);
modifierToggleRaycastTarget.raycastTarget = true;
Toggle toggle = modifierToggle.AddComponent<Toggle>();
HorizontalLayoutGroup layout2 = modifierToggle.AddComponent<HorizontalLayoutGroup>();
layout2.spacing = 5;
layout2.childForceExpandWidth = false;
var bg = new GameObject("TrapBackground");
bg.transform.SetParent(modifierToggle.transform, false);
RectTransform bgRect = bg.AddComponent<RectTransform>();
bgRect.sizeDelta = new Vector2(32.4001f, 32.4001f);
layout2.childControlHeight = false;
layout2.childControlWidth = false;
Image bgImage = bg.AddComponent<Image>();
bgImage.sprite = Resources.FindObjectsOfTypeAll<Sprite>().FirstOrDefault(f => f.name.Contains("uiBlockNano"));
bgImage.type = Image.Type.Sliced;
var check = new GameObject("TrapCheckmark");
check.transform.SetParent(bg.transform, false);
RectTransform rect2 = check.AddComponent<RectTransform>();
rect2.sizeDelta = new Vector2(20, 20);
check.AddComponent<Image>();
toggle.targetGraphic = bg.GetComponent<Image>();
toggle.graphic = check.GetComponent<Image>();
toggle.isOn = !TrapsDisabled.Contains(key);
toggle.onValueChanged.AddListener(isOn =>
{
if (!isOn)
{
if (!TrapsDisabled.Contains(key))
{
TrapsDisabled.Add(key);
PlayerPrefs.SetInt("TrapController_" + key.Replace(" ", "_"), 0);
}
}
else if (TrapsDisabled.Contains(name))
{
TrapsDisabled.Remove(key);
PlayerPrefs.SetInt("TrapController_" + key.Replace(" ", "_"), 1);
}
PlayerPrefs.Save();
});
var textObj = new GameObject("TrapLabel");
textObj.transform.SetParent(modifierToggle.transform, false);
textObj.AddComponent<RectTransform>().pivot = new Vector2(-1f, 0.5f);
ContentSizeFitter textObjFitter = textObj.AddComponent<ContentSizeFitter>();
textObjFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
textObjFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
TextMeshProUGUI text = textObj.AddComponent<TextMeshProUGUI>();
text.text = name;
text.enableWordWrapping = false;
text.font = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().FirstOrDefault(f => f.name.Contains("Retro GamingPix"));
text.fontSize = 23;
}
dropdownToggleButton.onClick.AddListener(() =>
{
scrollView.SetActive(!scrollView.activeSelf);
});
scrollRect.content = rect;
Canvas.ForceUpdateCanvases();
LayoutRebuilder.ForceRebuildLayoutImmediate(dropdownOptions.transform as RectTransform);
float preferredWidth = LayoutUtility.GetPreferredWidth(rect);
viewportRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredWidth);
scrollViewRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredWidth + 20);
scrollView.SetActive(false);
}
}
public class MyPatches
{
[HarmonyPatch(typeof(PreRunScript), "Start")]
[HarmonyPrefix]
public static void PreRunScript_Start_Prefix(PreRunScript __instance)
{
AudioListener.volume = 1f;
Limb.dislocationHealSpeed = 0.07f;
Limb.boneHealSpeed = 0.043f;
Instance.StartCoroutine(CreateModeUI());
}
public static bool WorldGeneration_DistributeEntities_Prefix(GameObject basObj, float minPerChunk, float maxPerChunk, float spawnYOffset = 0f, float randomRotation = 0f, float spawnYOffsetDeviation = 0f, bool spawnInGround = false, bool randomFlip = false, WorldGeneration.PlaceCheckDelegate checkFunc = null, bool isTrap = false, Vector2 dir = default(Vector2), bool forceFlip = false)
{
if (TrapsDisabled.Contains(basObj.name))
return false;
return true;
}
}
}