Fix for InputSystem in 3.0.0 (temp fix for il2cpp)

This commit is contained in:
sinaioutlander 2020-11-20 17:12:40 +11:00
parent 97dbecaa2a
commit c38155ab04
10 changed files with 187 additions and 58 deletions

View File

@ -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";

View File

@ -211,6 +211,8 @@ namespace UnityExplorer.Helpers
return false;
}
#else
public static bool LoadModule(string module) => true;
#endif
public static bool IsEnumerable(Type t)

View File

@ -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();
}
}

View File

@ -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;
}
}
}

View File

@ -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)
internal static Dictionary<KeyCode, object> ActualKeyDict = new Dictionary<KeyCode, object>();
internal object GetActualKey(KeyCode key)
{
var parsed = Enum.Parse(TKey, key.ToString());
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 });
return (bool)m_btnIsPressedProp.GetValue(actualKey, null);
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<BaseInputModule>();
//#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
}
}
}

View File

@ -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<StandaloneInputModule>();
}
public void ActivateModule()
{
m_inputModule.ActivateModule();
}
}
}

View File

@ -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() { }
}
}

View File

@ -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;

View File

@ -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<Shader>("DefaultUI");
}
ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
ConsoleFont = bundle.LoadAsset<Font>("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,24 +49,21 @@ 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<Shader>("DefaultUI");
}
ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
ConsoleFont = bundle.LoadAsset<Font>("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<EventSystem>();
InputModule = rootObj.AddComponent<StandaloneInputModule>();
InputModule.ActivateModule();
InputManager.AddUIModule();
Canvas canvas = rootObj.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceCamera;

View File

@ -25,7 +25,7 @@
<Prefer32Bit>false</Prefer32Bit>
<RootNamespace>UnityExplorer</RootNamespace>
<!-- Set this to the BepInEx Il2Cpp Game folder, without the ending '\' character. -->
<BIECppGameFolder>D:\Steam\steamapps\common\Outward</BIECppGameFolder>
<BIECppGameFolder>D:\Steam\steamapps\common\Outward_IL2CPP</BIECppGameFolder>
<!-- Set this to the BepInEx Mono Game folder, without the ending '\' character. -->
<BIEMonoGameFolder>D:\source\Unity Projects\Test\_BUILD_MONO</BIEMonoGameFolder>
<!-- Set this to the BepInEx Mono Managed folder, without the ending '\' character. -->