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

Commit c38155a

Browse files
committed
Fix for InputSystem in 3.0.0 (temp fix for il2cpp)
1 parent 97dbeca commit c38155a

File tree

10 files changed

+186
-57
lines changed

10 files changed

+186
-57
lines changed

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace UnityExplorer
1515
public class ExplorerCore
1616
{
1717
public const string NAME = "UnityExplorer";
18-
public const string VERSION = "3.0.2";
18+
public const string VERSION = "3.0.2.1";
1919
public const string AUTHOR = "Sinai";
2020
public const string GUID = "com.sinai.unityexplorer";
2121
public const string EXPLORER_FOLDER = @"Mods\UnityExplorer";

src/Helpers/ReflectionHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ internal static bool LoadModuleInternal(string fullPath)
211211

212212
return false;
213213
}
214+
#else
215+
public static bool LoadModule(string module) => true;
214216
#endif
215217

216218
public static bool IsEnumerable(Type t)

src/Input/IHandleInput.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using UnityEngine.EventSystems;
23

34
namespace UnityExplorer.Input
45
{
@@ -11,5 +12,12 @@ public interface IHandleInput
1112

1213
bool GetMouseButtonDown(int btn);
1314
bool GetMouseButton(int btn);
15+
16+
BaseInputModule UIModule { get; }
17+
18+
PointerEventData InputPointerEvent { get; }
19+
20+
void AddUIInputModule();
21+
void ActivateModule();
1422
}
1523
}

src/Input/InputManager.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
using UnityEngine;
33
using UnityExplorer.Helpers;
44
using System.Diagnostics.CodeAnalysis;
5+
using UnityEngine.EventSystems;
56
#if CPP
67
using UnhollowerBaseLib;
78
#endif
89

910
namespace UnityExplorer.Input
1011
{
12+
public enum InputType
13+
{
14+
InputSystem,
15+
Legacy,
16+
None
17+
}
18+
1119
public static class InputManager
1220
{
21+
public static InputType CurrentType { get; private set; }
22+
1323
private static IHandleInput m_inputModule;
1424

1525
public static Vector3 MousePosition => m_inputModule.MousePosition;
@@ -20,23 +30,35 @@ public static class InputManager
2030
public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn);
2131
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
2232

33+
public static BaseInputModule UIInput => m_inputModule.UIModule;
34+
public static PointerEventData InputPointerEvent => m_inputModule.InputPointerEvent;
35+
36+
public static void ActivateUIModule() => m_inputModule.ActivateModule();
37+
38+
public static void AddUIModule()
39+
{
40+
m_inputModule.AddUIInputModule();
41+
ActivateUIModule();
42+
}
43+
2344
public static void Init()
2445
{
25-
#if CPP
2646
if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null))
2747
{
2848
m_inputModule = new InputSystem();
49+
CurrentType = InputType.InputSystem;
2950
}
3051
else if (LegacyInput.TInput != null || (ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule") && LegacyInput.TInput != null))
3152
{
3253
m_inputModule = new LegacyInput();
54+
CurrentType = InputType.Legacy;
3355
}
34-
#endif
3556

3657
if (m_inputModule == null)
3758
{
3859
ExplorerCore.LogWarning("Could not find any Input module!");
3960
m_inputModule = new NoInput();
61+
CurrentType = InputType.None;
4062
}
4163
}
4264
}

src/Input/InputSystem.cs

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Reflection;
33
using UnityExplorer.Helpers;
44
using UnityEngine;
5+
using UnityEngine.EventSystems;
6+
using UnityExplorer.UI;
7+
using System.Collections.Generic;
58

69
namespace UnityExplorer.Input
710
{
@@ -64,24 +67,46 @@ public InputSystem()
6467
private static PropertyInfo m_positionProp;
6568
private static MethodInfo m_readVector2InputMethod;
6669

67-
public Vector2 MousePosition => (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
68-
69-
public bool GetKeyDown(KeyCode key)
70+
public Vector2 MousePosition
7071
{
71-
var parsedKey = Enum.Parse(TKey, key.ToString());
72-
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsedKey });
73-
74-
return (bool)m_btnWasPressedProp.GetValue(actualKey, null);
72+
get
73+
{
74+
try
75+
{
76+
return (Vector2)m_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
77+
}
78+
catch
79+
{
80+
return Vector2.zero;
81+
}
82+
}
7583
}
7684

77-
public bool GetKey(KeyCode key)
85+
internal static Dictionary<KeyCode, object> ActualKeyDict = new Dictionary<KeyCode, object>();
86+
87+
internal object GetActualKey(KeyCode key)
7888
{
79-
var parsed = Enum.Parse(TKey, key.ToString());
80-
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
89+
if (!ActualKeyDict.ContainsKey(key))
90+
{
91+
var s = key.ToString();
92+
if (s.Contains("Control"))
93+
s = s.Replace("Control", "Ctrl");
94+
else if (s.Contains("Return"))
95+
s = "Enter";
8196

82-
return (bool)m_btnIsPressedProp.GetValue(actualKey, null);
97+
var parsed = Enum.Parse(TKey, s);
98+
var actualKey = m_kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
99+
100+
ActualKeyDict.Add(key, actualKey);
101+
}
102+
103+
return ActualKeyDict[key];
83104
}
84105

106+
public bool GetKeyDown(KeyCode key) => (bool)m_btnWasPressedProp.GetValue(GetActualKey(key), null);
107+
108+
public bool GetKey(KeyCode key) => (bool)m_btnIsPressedProp.GetValue(GetActualKey(key), null);
109+
85110
public bool GetMouseButtonDown(int btn)
86111
{
87112
switch (btn)
@@ -103,5 +128,44 @@ public bool GetMouseButton(int btn)
103128
default: throw new NotImplementedException();
104129
}
105130
}
131+
132+
// UI Input
133+
134+
//public Type TInputSystemUIInputModule
135+
// => m_tUIInputModule
136+
// ?? (m_tUIInputModule = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule"));
137+
//internal Type m_tUIInputModule;
138+
139+
public BaseInputModule UIModule => null; // m_newInputModule;
140+
//internal BaseInputModule m_newInputModule;
141+
142+
public PointerEventData InputPointerEvent => null;
143+
144+
public void AddUIInputModule()
145+
{
146+
// if (TInputSystemUIInputModule != null)
147+
// {
148+
//#if CPP
149+
// // m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast<BaseInputModule>();
150+
//#else
151+
// m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule);
152+
//#endif
153+
// }
154+
// else
155+
// {
156+
// ExplorerCore.LogWarning("New input system: Could not find type by name 'UnityEngine.InputSystem.UI.InputSystemUIInputModule'");
157+
// }
158+
}
159+
160+
public void ActivateModule()
161+
{
162+
//#if CPP
163+
// // m_newInputModule.ActivateModule();
164+
//#else
165+
// m_newInputModule.ActivateModule();
166+
//#endif
167+
168+
169+
}
106170
}
107171
}

src/Input/LegacyInput.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Reflection;
33
using UnityExplorer.Helpers;
44
using UnityEngine;
5+
using UnityEngine.EventSystems;
6+
using UnityExplorer.UI;
57

68
namespace UnityExplorer.Input
79
{
@@ -36,5 +38,27 @@ public LegacyInput()
3638
public bool GetMouseButton(int btn) => (bool)m_getMouseButtonMethod.Invoke(null, new object[] { btn });
3739

3840
public bool GetMouseButtonDown(int btn) => (bool)m_getMouseButtonDownMethod.Invoke(null, new object[] { btn });
41+
42+
// UI Input module
43+
44+
public BaseInputModule UIModule => m_inputModule;
45+
internal StandaloneInputModule m_inputModule;
46+
47+
public PointerEventData InputPointerEvent =>
48+
#if CPP
49+
m_inputModule.m_InputPointerEvent;
50+
#else
51+
null;
52+
#endif
53+
54+
public void AddUIInputModule()
55+
{
56+
m_inputModule = UIManager.CanvasRoot.gameObject.AddComponent<StandaloneInputModule>();
57+
}
58+
59+
public void ActivateModule()
60+
{
61+
m_inputModule.ActivateModule();
62+
}
3963
}
4064
}

src/Input/NoInput.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using UnityEngine.EventSystems;
23

34
namespace UnityExplorer.Input
45
{
@@ -13,5 +14,10 @@ public class NoInput : IHandleInput
1314

1415
public bool GetMouseButton(int btn) => false;
1516
public bool GetMouseButtonDown(int btn) => false;
17+
18+
public BaseInputModule UIModule => null;
19+
public PointerEventData InputPointerEvent => null;
20+
public void ActivateModule() { }
21+
public void AddUIInputModule() { }
1622
}
1723
}

src/UI/ForceUnlockCursor.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ private static void SetupPatches()
6363
throw new Exception("Could not find Type 'UnityEngine.Cursor'!");
6464
}
6565

66+
ExplorerCore.Log("setting up forceunlockcursor patches...");
67+
6668
// Get current cursor state and enable cursor
6769
try
6870
{
@@ -100,7 +102,7 @@ private static void SetupPatches()
100102
}
101103
catch (Exception e)
102104
{
103-
ExplorerCore.Log($"Exception on CursorControl.Init! {e.GetType()}, {e.Message}");
105+
ExplorerCore.Log($"Exception on ForceUnlockCursor.Init! {e.GetType()}, {e.Message}");
104106
}
105107
}
106108

@@ -164,13 +166,20 @@ public static void UpdateCursorControl()
164166

165167
public static void SetEventSystem()
166168
{
169+
if (InputManager.CurrentType == InputType.InputSystem)
170+
return;
171+
167172
m_settingEventSystem = true;
168-
UIManager.SetEventSystem();
173+
EventSystem.current = UIManager.EventSys;
174+
InputManager.ActivateUIModule();
169175
m_settingEventSystem = false;
170176
}
171177

172178
public static void ReleaseEventSystem()
173179
{
180+
if (InputManager.CurrentType == InputType.InputSystem)
181+
return;
182+
174183
if (m_lastEventSystem)
175184
{
176185
m_settingEventSystem = true;

0 commit comments

Comments
 (0)