|
1 |
| -using System; |
| 1 | +using HarmonyLib; |
| 2 | +using System; |
2 | 3 | using System.Collections;
|
3 | 4 | using UnityEngine;
|
4 | 5 | using UnityEngine.EventSystems;
|
@@ -149,18 +150,73 @@ public static void ReleaseEventSystem()
|
149 | 150 |
|
150 | 151 | // Patches
|
151 | 152 |
|
152 |
| - private static void SetupPatches() |
| 153 | + public static void SetupPatches() |
153 | 154 | {
|
154 | 155 | try
|
155 | 156 | {
|
156 |
| - ExplorerCore.Loader.SetupCursorPatches(); |
| 157 | + PrefixPropertySetter(typeof(Cursor), |
| 158 | + "lockState", |
| 159 | + new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_set_lockState)))); |
| 160 | + |
| 161 | + PrefixPropertySetter(typeof(Cursor), |
| 162 | + "visible", |
| 163 | + new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_set_visible)))); |
| 164 | + |
| 165 | + PrefixPropertySetter(typeof(EventSystem), |
| 166 | + "current", |
| 167 | + new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_EventSystem_set_current)))); |
| 168 | + |
| 169 | + PrefixMethod(typeof(EventSystem), |
| 170 | + "SetSelectedGameObject", |
| 171 | + new Type[] { typeof(GameObject), typeof(BaseEventData), typeof(int) }, |
| 172 | + new HarmonyMethod(typeof(CursorUnlocker).GetMethod(nameof(CursorUnlocker.Prefix_EventSystem_SetSelectedGameObject)))); |
| 173 | + } |
| 174 | + catch (Exception ex) |
| 175 | + { |
| 176 | + ExplorerCore.Log($"Exception setting up Harmony patches:\r\n{ex.ReflectionExToString()}"); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static void PrefixMethod(Type type, string method, Type[] arguments, HarmonyMethod prefix) |
| 181 | + { |
| 182 | + try |
| 183 | + { |
| 184 | + var processor = ExplorerCore.Harmony.CreateProcessor(type.GetMethod(method, arguments)); |
| 185 | + processor.AddPrefix(prefix); |
| 186 | + processor.Patch(); |
| 187 | + } |
| 188 | + catch (Exception e) |
| 189 | + { |
| 190 | + ExplorerCore.LogWarning($"Unable to patch {type.Name}.{method}: {e.Message}"); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private static void PrefixPropertySetter(Type type, string property, HarmonyMethod prefix) |
| 195 | + { |
| 196 | + try |
| 197 | + { |
| 198 | + var processor = ExplorerCore.Harmony.CreateProcessor(type.GetProperty(property).GetSetMethod()); |
| 199 | + processor.AddPrefix(prefix); |
| 200 | + processor.Patch(); |
157 | 201 | }
|
158 | 202 | catch (Exception e)
|
159 | 203 | {
|
160 |
| - ExplorerCore.Log($"Exception setting up Cursor patches: {e.GetType()}, {e.Message}"); |
| 204 | + ExplorerCore.Log($"Unable to patch {type.Name}.set_{property}: {e.Message}"); |
161 | 205 | }
|
162 | 206 | }
|
163 | 207 |
|
| 208 | + // Prevent setting non-UnityExplorer objects as selected when menu is open |
| 209 | + |
| 210 | + public static bool Prefix_EventSystem_SetSelectedGameObject(GameObject __0) |
| 211 | + { |
| 212 | + if (!UIManager.ShowMenu || !UIManager.CanvasRoot) |
| 213 | + return true; |
| 214 | + |
| 215 | + return __0 && __0.transform.root.gameObject.GetInstanceID() == UIManager.CanvasRoot.GetInstanceID(); |
| 216 | + } |
| 217 | + |
| 218 | + // Force EventSystem.current to be UnityExplorer's when menu is open |
| 219 | + |
164 | 220 | public static void Prefix_EventSystem_set_current(ref EventSystem value)
|
165 | 221 | {
|
166 | 222 | if (!settingEventSystem && value)
|
|
0 commit comments