WKLib 0.2.3
A modding library for White Knuckle
Loading...
Searching...
No Matches
WKLib.cs
Go to the documentation of this file.
1using System.Reflection;
2using BepInEx;
3using HarmonyLib;
4using TMPro;
5using UnityEngine;
6using UnityEngine.EventSystems;
7using UnityEngine.SceneManagement;
8using UnityEngine.UI;
13using WKLib.Core.UI;
14using WKLib.Utilities;
15
16namespace WKLib;
17
21[BepInDependency(ImuiBepInEx.Plugin.GUID, BepInDependency.DependencyFlags.HardDependency)]
22[BepInPlugin(GUID, NAME, VERSION)]
23public class WKLibPlugin : BaseUnityPlugin
24{
25 public const string GUID = "com.monksilly.WKLib";
26 public const string NAME = "WKLib";
27 public const string VERSION = "0.3.0";
28
29 private static Harmony harmony = null;
30
31 private void Awake()
32 {
33 // Initialize Logger
34 WKLog.Initialize(Logger);
35
36 WKLog.Debug($"Initalizing reflection...");
37 ReflectionUtility.Initialize();
38 WKLog.Debug($"Initalizing input utility...");
39 InputUtility.Initialize();
40
41 gameObject.hideFlags = HideFlags.HideAndDontSave; // Hides the Manager GameObject from Unity
42
43 harmony = new Harmony(GUID);
44
45 foreach (var type in typeof(WKLibPlugin).Assembly.GetTypes())
46 {
47 if (type.GetCustomAttribute<PatchOnEntryAttribute>() != null)
48 harmony.PatchAll(type);
49 }
50
52
53 WKLog.Info($"Plugin {NAME} v{VERSION} is loaded!");
54
55 SceneManager.sceneLoaded -= OnSceneLoaded;
56 SceneManager.sceneLoaded += OnSceneLoaded;
57 }
58
59 private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
60 {
61 if (scene == null)
62 return;
63
64 if (scene.name == "Main-Menu")
65 {
66 var versionText = GameObject.Find("Canvas - Main Menu/Main Menu/Version Text")?.GetComponent<TextMeshProUGUI>();
67 if (versionText is not null)
68 {
69 versionText.text += $" (wklib-{VERSION}) ({BepInEx.Bootstrap.Chainloader.PluginInfos.Count} Mods)";
70 }
71
72 var updateInfoGO = GameObject.Find("Canvas - Main Menu/Main Menu/Support Menu/Update Info");
73 if (updateInfoGO != null)
74 {
75 GameObject toggleOverlayGO = Instantiate(updateInfoGO, updateInfoGO.transform.parent);
76 toggleOverlayGO.transform.SetSiblingIndex(0);
77 toggleOverlayGO.name = "Toggle Overlay";
78
79 var uiMenuButton = toggleOverlayGO.GetComponent<UI_MenuButton>();
80 if (uiMenuButton is not null)
81 DestroyImmediate(uiMenuButton);
82
83 var textComponent = toggleOverlayGO.GetComponentInChildren<TextMeshProUGUI>();
84 if (textComponent != null)
85 {
86 textComponent.text = "Toggle Overlay";
87 }
88
89 var buttonComponent = toggleOverlayGO.GetComponent<Button>();
90 if (buttonComponent != null)
91 {
92 buttonComponent.onClick.RemoveAllListeners();
93 buttonComponent.onClick.AddListener(() =>
94 {
95 RootPanel.Instance.IsOpen = !RootPanel.Instance.IsOpen;
96 EventSystem.current.SetSelectedGameObject(null);
97 }
98 );
99 }
100 }
101 }
102 else
103 {
104 // If pause menu exists, we add our button
105 var pauseLayout = GameObject.Find("GameManager/Canvas/Pause/Pause Menu/Pause Buttons/Pause Layout");
106 if (pauseLayout != null)
107 {
108 var uiGap = pauseLayout.transform.Find("Gap.01");
109 var settingsButton = pauseLayout.transform.Find("Settings");
110
111 if (uiGap != null && settingsButton != null)
112 {
113 // Create gap
114 GameObject uiGapGO = Instantiate(uiGap.gameObject, uiGap.transform.parent);
115 uiGapGO.transform.SetSiblingIndex(uiGap.parent.childCount - 1);
116 uiGapGO.name = "Gap.03";
117
118 // Create button
119 GameObject toggleOverlayGO = Instantiate(settingsButton.gameObject, settingsButton.transform.parent);
120 toggleOverlayGO.transform.SetSiblingIndex(settingsButton.parent.childCount - 1);
121 toggleOverlayGO.name = "Toggle Overlay";
122
123 var uiMenuButton = toggleOverlayGO.GetComponent<UI_MenuButton>();
124 if (uiMenuButton is not null)
125 DestroyImmediate(uiMenuButton);
126
127 var textComponent = toggleOverlayGO.GetComponentInChildren<TextMeshProUGUI>();
128 if (textComponent != null)
129 {
130 textComponent.text = "TOGGLE OVERLAY";
131 }
132
133 var buttonComponent = toggleOverlayGO.GetComponent<Button>();
134 if (buttonComponent != null)
135 {
136 buttonComponent.onClick.RemoveAllListeners();
137 buttonComponent.onClick.AddListener(() =>
138 {
139 RootPanel.Instance.IsOpen = !RootPanel.Instance.IsOpen;
140 EventSystem.current.SetSelectedGameObject(null);
141 }
142 );
143 }
144 }
145 }
146 }
147 }
148
149 private void OnDestroy()
150 {
151 harmony?.UnpatchSelf();
152 Destroy(RootPanel.Instance.ImuiPanel.Canvas);
153
154 SceneManager.sceneLoaded -= OnSceneLoaded;
155 WKLog.Info($"Plugin {NAME} unloaded!");
156 }
157}
static void CreateEntries(ConfigFile Config)
Main Entry point of the whole Library.
Definition WKLib.cs:24
const string NAME
Definition WKLib.cs:26
const string GUID
Definition WKLib.cs:25
const string VERSION
Definition WKLib.cs:27