Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 6e6b623

Browse files
committed
Do patching in CursorUnlocker, add patch for SetSelectedGameObject
1 parent 03c8e5a commit 6e6b623

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

src/Core/Input/CursorUnlocker.cs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using HarmonyLib;
2+
using System;
23
using System.Collections;
34
using UnityEngine;
45
using UnityEngine.EventSystems;
@@ -149,18 +150,73 @@ public static void ReleaseEventSystem()
149150

150151
// Patches
151152

152-
private static void SetupPatches()
153+
public static void SetupPatches()
153154
{
154155
try
155156
{
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();
157201
}
158202
catch (Exception e)
159203
{
160-
ExplorerCore.Log($"Exception setting up Cursor patches: {e.GetType()}, {e.Message}");
204+
ExplorerCore.Log($"Unable to patch {type.Name}.set_{property}: {e.Message}");
161205
}
162206
}
163207

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+
164220
public static void Prefix_EventSystem_set_current(ref EventSystem value)
165221
{
166222
if (!settingEventSystem && value)

0 commit comments

Comments
 (0)