-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patches.cs
129 lines (118 loc) · 5.14 KB
/
Patches.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
using System;
using System.Collections.Generic;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Object = System.Object;
namespace ModAge;
[HarmonyPatch(typeof(FejdStartup), nameof(FejdStartup.LoadMainScene))]
static class FejdStartupLoadMainScenePatch
{
static void Prefix(FejdStartup __instance)
{
if (ModAgePlugin.modAgeUIFinal != null)
{
// Destroy it
UnityEngine.Object.Destroy(ModAgePlugin.modAgeUIFinal);
ModAgePlugin.modAgeUIFinal = null!; // Just in case
}
}
}
[HarmonyPatch(typeof(FejdStartup), nameof(FejdStartup.SetupGui))]
static class FejdStartupSetupGuiPatch
{
static void Postfix(FejdStartup __instance)
{
try
{
if (__instance.m_mainMenu.transform != null && ModAgePlugin.modAgeUIFinal != null)
{
ModAgePlugin.modAgeUIFinal.transform.SetParent(__instance.m_mainMenu.transform, false);
Transform? menuList = Utils.FindChild(__instance.m_mainMenu.transform, "MenuEntries");
CreateMenu(menuList);
}
else if (__instance.m_mainMenu.transform != null && ModAgePlugin.modAgeUIFinal == null)
{
GameObject? instantiatedObject = UnityEngine.Object.Instantiate(ModAgePlugin.modAgeUIAsset);
ModAgePlugin.modAgeUIFinal = instantiatedObject;
ModAgePlugin.modAgeUIcomp = instantiatedObject.GetComponent<ModAgeUI>();
ModAgePlugin.DontDestroyOnLoad(ModAgePlugin.modAgeUIFinal);
ModAgePlugin.modAgeUIFinal.transform.SetParent(__instance.m_mainMenu.transform, false);
Transform? menuList = Utils.FindChild(__instance.m_mainMenu.transform, "MenuEntries");
FejdStartupSetupGuiPatch.CreateMenu(menuList);
Utilities.CompareLocalModsToThunderstore();
}
}
catch (Exception ex)
{
ModAgePlugin.ModAgeLogger.LogWarning($"Exception caught while creating the Mod Age Menu Option: {ex}");
}
}
internal static void CreateMenu(Transform menuList)
{
ModAgePlugin.ModAgeLogger.LogDebug("Instantiating Mod Age");
bool settingsFound = false;
List<Button>? mainMenuButtons = new List<Button>();
for (int i = 0; i < menuList.childCount; ++i)
{
if (menuList.GetChild(i).gameObject.activeInHierarchy &&
menuList.GetChild(i).name != "ModAge" &&
menuList.GetChild(i).TryGetComponent<Button>(out Button? menuButton))
{
mainMenuButtons.Add(menuButton);
}
if (menuList.GetChild(i).name == "Settings")
{
Transform modSettings = UnityEngine.Object.Instantiate(menuList.GetChild(i), menuList);
modSettings.name = "ModAge";
modSettings.GetComponentInChildren<TextMeshProUGUI>().text = Localization.instance.Localize("$menu_title");
Button modSettingsButton = modSettings.GetComponent<Button>();
for (int j = 0; j < modSettingsButton.onClick.GetPersistentEventCount(); ++j)
{
modSettingsButton.onClick.SetPersistentListenerState(j, UnityEventCallState.Off);
}
modSettingsButton.onClick.RemoveAllListeners();
modSettingsButton.onClick.AddListener(() =>
{
try
{
ModAgePlugin.modAgeUIFinal.SetActive(!ModAgePlugin.modAgeUIFinal.activeSelf);
}
catch (Exception ex)
{
ModAgePlugin.ModAgeLogger.LogWarning($"Exception caught while showing the Mod Age window: {ex}");
}
});
mainMenuButtons.Add(modSettingsButton);
Transform left = modSettings.Find("LeftKnot");
if (left != null)
{
Vector3 localPosition = left.localPosition;
localPosition = new Vector2(localPosition.x - 10f, localPosition.y);
left.localPosition = localPosition;
}
Transform right = modSettings.Find("RightKnot");
if (right != null)
{
Vector3 localPosition = right.localPosition;
localPosition = new Vector2(localPosition.x + 10f, localPosition.y);
right.localPosition = localPosition;
}
settingsFound = true;
}
else if (settingsFound)
{
RectTransform rectTransform = menuList.GetChild(i).GetComponent<RectTransform>();
Vector2 anchoredPosition = rectTransform.anchoredPosition;
anchoredPosition = new Vector2(anchoredPosition.x, anchoredPosition.y - 40);
rectTransform.anchoredPosition = anchoredPosition;
}
}
if (FejdStartup.instance != null)
{
FejdStartup.instance.m_menuButtons = mainMenuButtons.ToArray();
}
}
}