mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-09-20 13:36:01 +08:00
192 lines
5.8 KiB
C#
192 lines
5.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Explorer.Helpers;
|
|
using BF = System.Reflection.BindingFlags;
|
|
#if ML
|
|
using Harmony;
|
|
#else
|
|
using HarmonyLib;
|
|
#endif
|
|
|
|
namespace Explorer.UI
|
|
{
|
|
public class ForceUnlockCursor
|
|
{
|
|
public static bool Unlock
|
|
{
|
|
get => m_forceUnlock;
|
|
set => SetForceUnlock(value);
|
|
}
|
|
private static bool m_forceUnlock;
|
|
|
|
public static bool ShouldForceMouse => ExplorerCore.ShowMenu && Unlock;
|
|
|
|
private static CursorLockMode m_lastLockMode;
|
|
private static bool m_lastVisibleState;
|
|
|
|
private static bool m_currentlySettingCursor = false;
|
|
|
|
private static Type CursorType
|
|
=> m_cursorType
|
|
?? (m_cursorType = ReflectionHelpers.GetTypeByName("UnityEngine.Cursor"));
|
|
private static Type m_cursorType;
|
|
|
|
public static void Init()
|
|
{
|
|
try
|
|
{
|
|
if (CursorType == null)
|
|
{
|
|
throw new Exception("Could not find Type 'UnityEngine.Cursor'!");
|
|
}
|
|
|
|
// Get current cursor state and enable cursor
|
|
try
|
|
{
|
|
//m_lastLockMode = Cursor.lockState;
|
|
m_lastLockMode = (CursorLockMode?)typeof(Cursor).GetProperty("lockState", BF.Public | BF.Static)?.GetValue(null, null)
|
|
?? CursorLockMode.None;
|
|
|
|
//m_lastVisibleState = Cursor.visible;
|
|
m_lastVisibleState = (bool?)typeof(Cursor).GetProperty("visible", BF.Public | BF.Static)?.GetValue(null, null)
|
|
?? false;
|
|
}
|
|
catch { }
|
|
|
|
// Setup Harmony Patches
|
|
TryPatch("lockState", new HarmonyMethod(typeof(ForceUnlockCursor).GetMethod(nameof(Prefix_set_lockState))), true);
|
|
TryPatch("lockState", new HarmonyMethod(typeof(ForceUnlockCursor).GetMethod(nameof(Postfix_get_lockState))), false);
|
|
|
|
TryPatch("visible", new HarmonyMethod(typeof(ForceUnlockCursor).GetMethod(nameof(Prefix_set_visible))), true);
|
|
TryPatch("visible", new HarmonyMethod(typeof(ForceUnlockCursor).GetMethod(nameof(Postfix_get_visible))), false);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ExplorerCore.Log($"Exception on CursorControl.Init! {e.GetType()}, {e.Message}");
|
|
}
|
|
|
|
Unlock = true;
|
|
}
|
|
|
|
private static void TryPatch(string property, HarmonyMethod patch, bool setter)
|
|
{
|
|
try
|
|
{
|
|
var harmony =
|
|
#if ML
|
|
ExplorerMelonMod.Instance.harmonyInstance;
|
|
#else
|
|
ExplorerBepInPlugin.HarmonyInstance;
|
|
#endif
|
|
;
|
|
|
|
var prop = typeof(Cursor).GetProperty(property);
|
|
|
|
if (setter)
|
|
{
|
|
// setter is prefix
|
|
harmony.Patch(prop.GetSetMethod(), prefix: patch);
|
|
}
|
|
else
|
|
{
|
|
// getter is postfix
|
|
harmony.Patch(prop.GetGetMethod(), postfix: patch);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string s = setter ? "set_" : "get_" ;
|
|
ExplorerCore.Log($"Unable to patch Cursor.{s}{property}: {e.Message}");
|
|
}
|
|
}
|
|
|
|
private static void SetForceUnlock(bool unlock)
|
|
{
|
|
m_forceUnlock = unlock;
|
|
UpdateCursorControl();
|
|
}
|
|
|
|
public static void Update()
|
|
{
|
|
// Check Force-Unlock input
|
|
if (InputManager.GetKeyDown(KeyCode.LeftAlt))
|
|
{
|
|
Unlock = !Unlock;
|
|
}
|
|
}
|
|
|
|
public static void UpdateCursorControl()
|
|
{
|
|
try
|
|
{
|
|
m_currentlySettingCursor = true;
|
|
if (ShouldForceMouse)
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
else
|
|
{
|
|
Cursor.lockState = m_lastLockMode;
|
|
Cursor.visible = m_lastVisibleState;
|
|
}
|
|
m_currentlySettingCursor = false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ExplorerCore.Log($"Exception setting Cursor state: {e.GetType()}, {e.Message}");
|
|
}
|
|
}
|
|
|
|
// Force mouse to stay unlocked and visible while UnlockMouse and ShowMenu are true.
|
|
// Also keep track of when anything else tries to set Cursor state, this will be the
|
|
// value that we set back to when we close the menu or disable force-unlock.
|
|
|
|
[HarmonyPrefix]
|
|
public static void Prefix_set_lockState(ref CursorLockMode value)
|
|
{
|
|
if (!m_currentlySettingCursor)
|
|
{
|
|
m_lastLockMode = value;
|
|
|
|
if (ShouldForceMouse)
|
|
{
|
|
value = CursorLockMode.None;
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
public static void Prefix_set_visible(ref bool value)
|
|
{
|
|
if (!m_currentlySettingCursor)
|
|
{
|
|
m_lastVisibleState = value;
|
|
|
|
if (ShouldForceMouse)
|
|
{
|
|
value = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
public static void Postfix_get_lockState(ref CursorLockMode __result)
|
|
{
|
|
if (ShouldForceMouse)
|
|
{
|
|
__result = m_lastLockMode;
|
|
}
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
public static void Postfix_get_visible(ref bool __result)
|
|
{
|
|
if (ShouldForceMouse)
|
|
{
|
|
__result = m_lastVisibleState;
|
|
}
|
|
}
|
|
}
|
|
}
|