From c38155ab04dc3c58d74a944aad119ae499323c15 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Fri, 20 Nov 2020 17:12:40 +1100 Subject: [PATCH] Fix for InputSystem in 3.0.0 (temp fix for il2cpp) --- src/ExplorerCore.cs | 2 +- src/Helpers/ReflectionHelpers.cs | 2 + src/Input/IHandleInput.cs | 8 +++ src/Input/InputManager.cs | 26 +++++++++- src/Input/InputSystem.cs | 88 +++++++++++++++++++++++++++----- src/Input/LegacyInput.cs | 24 +++++++++ src/Input/NoInput.cs | 6 +++ src/UI/ForceUnlockCursor.cs | 13 ++++- src/UI/UIManager.cs | 74 ++++++++++++--------------- src/UnityExplorer.csproj | 2 +- 10 files changed, 187 insertions(+), 58 deletions(-) diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 8845d65..4868d48 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -15,7 +15,7 @@ namespace UnityExplorer public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.0.2"; + public const string VERSION = "3.0.2.1"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; public const string EXPLORER_FOLDER = @"Mods\UnityExplorer"; diff --git a/src/Helpers/ReflectionHelpers.cs b/src/Helpers/ReflectionHelpers.cs index 8eb4f28..810a873 100644 --- a/src/Helpers/ReflectionHelpers.cs +++ b/src/Helpers/ReflectionHelpers.cs @@ -211,6 +211,8 @@ namespace UnityExplorer.Helpers return false; } +#else + public static bool LoadModule(string module) => true; #endif public static bool IsEnumerable(Type t) diff --git a/src/Input/IHandleInput.cs b/src/Input/IHandleInput.cs index a2e9d8e..1422e03 100644 --- a/src/Input/IHandleInput.cs +++ b/src/Input/IHandleInput.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEngine.EventSystems; namespace UnityExplorer.Input { @@ -11,5 +12,12 @@ namespace UnityExplorer.Input bool GetMouseButtonDown(int btn); bool GetMouseButton(int btn); + + BaseInputModule UIModule { get; } + + PointerEventData InputPointerEvent { get; } + + void AddUIInputModule(); + void ActivateModule(); } } diff --git a/src/Input/InputManager.cs b/src/Input/InputManager.cs index 4578aea..563a63f 100644 --- a/src/Input/InputManager.cs +++ b/src/Input/InputManager.cs @@ -2,14 +2,24 @@ using UnityEngine; using UnityExplorer.Helpers; using System.Diagnostics.CodeAnalysis; +using UnityEngine.EventSystems; #if CPP using UnhollowerBaseLib; #endif namespace UnityExplorer.Input { + public enum InputType + { + InputSystem, + Legacy, + None + } + public static class InputManager { + public static InputType CurrentType { get; private set; } + private static IHandleInput m_inputModule; public static Vector3 MousePosition => m_inputModule.MousePosition; @@ -20,23 +30,35 @@ namespace UnityExplorer.Input public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn); public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn); + public static BaseInputModule UIInput => m_inputModule.UIModule; + public static PointerEventData InputPointerEvent => m_inputModule.InputPointerEvent; + + public static void ActivateUIModule() => m_inputModule.ActivateModule(); + + public static void AddUIModule() + { + m_inputModule.AddUIInputModule(); + ActivateUIModule(); + } + public static void Init() { -#if CPP if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null)) { m_inputModule = new InputSystem(); + CurrentType = InputType.InputSystem; } else if (LegacyInput.TInput != null || (ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule") && LegacyInput.TInput != null)) { m_inputModule = new LegacyInput(); + CurrentType = InputType.Legacy; } -#endif if (m_inputModule == null) { ExplorerCore.LogWarning("Could not find any Input module!"); m_inputModule = new NoInput(); + CurrentType = InputType.None; } } } diff --git a/src/Input/InputSystem.cs b/src/Input/InputSystem.cs index 79157a9..e32427a 100644 --- a/src/Input/InputSystem.cs +++ b/src/Input/InputSystem.cs @@ -2,6 +2,9 @@ using System.Reflection; using UnityExplorer.Helpers; using UnityEngine; +using UnityEngine.EventSystems; +using UnityExplorer.UI; +using System.Collections.Generic; namespace UnityExplorer.Input { @@ -64,24 +67,46 @@ namespace UnityExplorer.Input private static PropertyInfo m_positionProp; private static MethodInfo m_readVector2InputMethod; - public Vector2 MousePosition => (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]); - - public bool GetKeyDown(KeyCode key) + public Vector2 MousePosition { - var parsedKey = Enum.Parse(TKey, key.ToString()); - var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsedKey }); - - return (bool)m_btnWasPressedProp.GetValue(actualKey, null); + get + { + try + { + return (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]); + } + catch + { + return Vector2.zero; + } + } } - public bool GetKey(KeyCode key) - { - var parsed = Enum.Parse(TKey, key.ToString()); - var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed }); + internal static Dictionary ActualKeyDict = new Dictionary(); - return (bool)m_btnIsPressedProp.GetValue(actualKey, null); + internal object GetActualKey(KeyCode key) + { + if (!ActualKeyDict.ContainsKey(key)) + { + var s = key.ToString(); + if (s.Contains("Control")) + s = s.Replace("Control", "Ctrl"); + else if (s.Contains("Return")) + s = "Enter"; + + var parsed = Enum.Parse(TKey, s); + var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed }); + + ActualKeyDict.Add(key, actualKey); + } + + return ActualKeyDict[key]; } + public bool GetKeyDown(KeyCode key) => (bool)m_btnWasPressedProp.GetValue(GetActualKey(key), null); + + public bool GetKey(KeyCode key) => (bool)m_btnIsPressedProp.GetValue(GetActualKey(key), null); + public bool GetMouseButtonDown(int btn) { switch (btn) @@ -103,5 +128,44 @@ namespace UnityExplorer.Input default: throw new NotImplementedException(); } } + + // UI Input + + //public Type TInputSystemUIInputModule + // => m_tUIInputModule + // ?? (m_tUIInputModule = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule")); + //internal Type m_tUIInputModule; + + public BaseInputModule UIModule => null; // m_newInputModule; + //internal BaseInputModule m_newInputModule; + + public PointerEventData InputPointerEvent => null; + + public void AddUIInputModule() + { +// if (TInputSystemUIInputModule != null) +// { +//#if CPP +// // m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast(); +//#else +// m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule); +//#endif +// } +// else +// { +// ExplorerCore.LogWarning("New input system: Could not find type by name 'UnityEngine.InputSystem.UI.InputSystemUIInputModule'"); +// } + } + + public void ActivateModule() + { +//#if CPP +// // m_newInputModule.ActivateModule(); +//#else +// m_newInputModule.ActivateModule(); +//#endif + + + } } } diff --git a/src/Input/LegacyInput.cs b/src/Input/LegacyInput.cs index d4dc042..a868ae1 100644 --- a/src/Input/LegacyInput.cs +++ b/src/Input/LegacyInput.cs @@ -2,6 +2,8 @@ using System.Reflection; using UnityExplorer.Helpers; using UnityEngine; +using UnityEngine.EventSystems; +using UnityExplorer.UI; namespace UnityExplorer.Input { @@ -36,5 +38,27 @@ namespace UnityExplorer.Input public bool GetMouseButton(int btn) => (bool)m_getMouseButtonMethod.Invoke(null, new object[] { btn }); public bool GetMouseButtonDown(int btn) => (bool)m_getMouseButtonDownMethod.Invoke(null, new object[] { btn }); + + // UI Input module + + public BaseInputModule UIModule => m_inputModule; + internal StandaloneInputModule m_inputModule; + + public PointerEventData InputPointerEvent => +#if CPP + m_inputModule.m_InputPointerEvent; +#else + null; +#endif + + public void AddUIInputModule() + { + m_inputModule = UIManager.CanvasRoot.gameObject.AddComponent(); + } + + public void ActivateModule() + { + m_inputModule.ActivateModule(); + } } } diff --git a/src/Input/NoInput.cs b/src/Input/NoInput.cs index 7fe18a7..1e70357 100644 --- a/src/Input/NoInput.cs +++ b/src/Input/NoInput.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEngine.EventSystems; namespace UnityExplorer.Input { @@ -13,5 +14,10 @@ namespace UnityExplorer.Input public bool GetMouseButton(int btn) => false; public bool GetMouseButtonDown(int btn) => false; + + public BaseInputModule UIModule => null; + public PointerEventData InputPointerEvent => null; + public void ActivateModule() { } + public void AddUIInputModule() { } } } diff --git a/src/UI/ForceUnlockCursor.cs b/src/UI/ForceUnlockCursor.cs index a68da1d..df87bf4 100644 --- a/src/UI/ForceUnlockCursor.cs +++ b/src/UI/ForceUnlockCursor.cs @@ -63,6 +63,8 @@ namespace UnityExplorer.UI throw new Exception("Could not find Type 'UnityEngine.Cursor'!"); } + ExplorerCore.Log("setting up forceunlockcursor patches..."); + // Get current cursor state and enable cursor try { @@ -100,7 +102,7 @@ namespace UnityExplorer.UI } catch (Exception e) { - ExplorerCore.Log($"Exception on CursorControl.Init! {e.GetType()}, {e.Message}"); + ExplorerCore.Log($"Exception on ForceUnlockCursor.Init! {e.GetType()}, {e.Message}"); } } @@ -164,13 +166,20 @@ namespace UnityExplorer.UI public static void SetEventSystem() { + if (InputManager.CurrentType == InputType.InputSystem) + return; + m_settingEventSystem = true; - UIManager.SetEventSystem(); + EventSystem.current = UIManager.EventSys; + InputManager.ActivateUIModule(); m_settingEventSystem = false; } public static void ReleaseEventSystem() { + if (InputManager.CurrentType == InputType.InputSystem) + return; + if (m_lastEventSystem) { m_settingEventSystem = true; diff --git a/src/UI/UIManager.cs b/src/UI/UIManager.cs index 74b4d44..9a5a8b1 100644 --- a/src/UI/UIManager.cs +++ b/src/UI/UIManager.cs @@ -8,6 +8,7 @@ using System.IO; using System.Reflection; using UnityExplorer.Helpers; using UnityExplorer.UI.Shared; +using UnityExplorer.Input; #if CPP using UnityExplorer.Unstrip; #endif @@ -18,37 +19,13 @@ namespace UnityExplorer.UI { public static GameObject CanvasRoot { get; private set; } public static EventSystem EventSys { get; private set; } - public static StandaloneInputModule InputModule { get; private set; } - //internal static Material UIMaterial { get; private set; } internal static Sprite ResizeCursor { get; private set; } internal static Font ConsoleFont { get; private set; } public static void Init() { - var bundlePath = ExplorerCore.EXPLORER_FOLDER + @"\explorerui.bundle"; - if (File.Exists(bundlePath)) - { - var bundle = AssetBundle.LoadFromFile(bundlePath); - - // Fix for games which don't ship with 'UI/Default' shader. - if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default") - { - ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader..."); - Graphic.defaultGraphicMaterial.shader = bundle.LoadAsset("DefaultUI"); - } - - ResizeCursor = bundle.LoadAsset("cursor"); - - ConsoleFont = bundle.LoadAsset("CONSOLA"); - - ExplorerCore.Log("Loaded UI bundle"); - } - else - { - ExplorerCore.LogWarning("Could not find the ExplorerUI Bundle! It should exist at '" + bundlePath + "'"); - return; - } + LoadBundle(); // Create core UI Canvas and Event System handler CreateRootCanvas(); @@ -62,12 +39,6 @@ namespace UnityExplorer.UI Canvas.ForceUpdateCanvases(); } - public static void SetEventSystem() - { - EventSystem.current = EventSys; - InputModule.ActivateModule(); - } - public static void OnSceneChange() { SceneExplorer.Instance?.OnSceneChange(); @@ -78,23 +49,20 @@ namespace UnityExplorer.UI { MainMenu.Instance?.Update(); - if (EventSys && InputModule) + if (EventSys) { if (EventSystem.current != EventSys) { ForceUnlockCursor.SetEventSystem(); - //ForceUnlockCursor.Unlock = true; } - // Fix for games which override the InputModule pointer events (eg, VRChat) #if CPP - if (InputModule.m_InputPointerEvent != null) + // Fix for games which override the InputModule pointer events (eg, VRChat) + var evt = InputManager.InputPointerEvent; + if (evt != null) { - PointerEventData evt = InputModule.m_InputPointerEvent; if (!evt.eligibleForClick && evt.selectedObject) - { evt.eligibleForClick = true; - } } #endif } @@ -125,6 +93,33 @@ namespace UnityExplorer.UI } } + private static void LoadBundle() + { + var bundlePath = ExplorerCore.EXPLORER_FOLDER + @"\explorerui.bundle"; + if (File.Exists(bundlePath)) + { + var bundle = AssetBundle.LoadFromFile(bundlePath); + + // Fix for games which don't ship with 'UI/Default' shader. + if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default") + { + ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader..."); + Graphic.defaultGraphicMaterial.shader = bundle.LoadAsset("DefaultUI"); + } + + ResizeCursor = bundle.LoadAsset("cursor"); + + ConsoleFont = bundle.LoadAsset("CONSOLA"); + + ExplorerCore.Log("Loaded UI bundle"); + } + else + { + ExplorerCore.LogWarning("Could not find the ExplorerUI Bundle! It should exist at '" + bundlePath + "'"); + return; + } + } + private static GameObject CreateRootCanvas() { GameObject rootObj = new GameObject("ExplorerCanvas"); @@ -135,8 +130,7 @@ namespace UnityExplorer.UI CanvasRoot.transform.position = new Vector3(0f, 0f, 1f); EventSys = rootObj.AddComponent(); - InputModule = rootObj.AddComponent(); - InputModule.ActivateModule(); + InputManager.AddUIModule(); Canvas canvas = rootObj.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceCamera; diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index cebedc8..2d2d0aa 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -25,7 +25,7 @@ false UnityExplorer - D:\Steam\steamapps\common\Outward + D:\Steam\steamapps\common\Outward_IL2CPP D:\source\Unity Projects\Test\_BUILD_MONO