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

Commit b4b5f1e

Browse files
committed
1.8.2
* Added support for games which use the new InputSystem module and have disabled LegacyInputModule
1 parent 5afaf85 commit b4b5f1e

File tree

10 files changed

+200
-85
lines changed

10 files changed

+200
-85
lines changed

src/Config/ModConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public static void OnLoad()
3030
}
3131

3232
// returns true if settings successfully loaded
33-
public static bool LoadSettings(bool checkExist = true)
33+
public static bool LoadSettings()
3434
{
35-
if (checkExist && !File.Exists(SETTINGS_PATH))
35+
if (!File.Exists(SETTINGS_PATH))
3636
return false;
3737

3838
try
@@ -50,9 +50,9 @@ public static bool LoadSettings(bool checkExist = true)
5050
return Instance != null;
5151
}
5252

53-
public static void SaveSettings(bool checkExist = true)
53+
public static void SaveSettings()
5454
{
55-
if (checkExist && File.Exists(SETTINGS_PATH))
55+
if (File.Exists(SETTINGS_PATH))
5656
File.Delete(SETTINGS_PATH);
5757

5858
using (var file = File.Create(SETTINGS_PATH))

src/Explorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<BIECppGameFolder>D:\Steam\steamapps\common\Outward - Il2Cpp</BIECppGameFolder>
3535
<!-- Set this to the BepInEx Mono Game folder, without the ending '\' character. -->
3636
<BIEMonoGameFolder>D:\Steam\steamapps\common\Outward</BIEMonoGameFolder>
37+
<!-- <BIEMonoGameFolder>D:\source\Unity Projects\! My Unity Games\NewInputSystemTest\_BUILD</BIEMonoGameFolder> -->
3738
</PropertyGroup>
3839
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release_ML_Cpp|AnyCPU' ">
3940
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

src/ExplorerBepInPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ internal void Awake()
6666

6767
new ExplorerCore();
6868

69-
HarmonyInstance.PatchAll();
69+
//HarmonyInstance.PatchAll();
7070
}
7171

7272
void LoadMCS()

src/ExplorerCore.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ namespace Explorer
44
{
55
public class ExplorerCore
66
{
7-
public const string NAME = "Explorer (" + PLATFORM + ", " + MODLOADER + ")";
8-
public const string VERSION = "1.8.1";
7+
public const string NAME = "Explorer (" + PLATFORM + ", " + MODLOADER + ")";
8+
public const string VERSION = "1.8.2";
99
public const string AUTHOR = "Sinai";
1010
public const string GUID = "com.sinai.explorer";
1111

12-
public const string MODLOADER =
13-
#if ML
14-
"MelonLoader";
15-
#else
16-
"BepInEx";
17-
#endif
1812
public const string PLATFORM =
1913
#if CPP
2014
"Il2Cpp";
2115
#else
2216
"Mono";
2317
#endif
18+
public const string MODLOADER =
19+
#if ML
20+
"MelonLoader";
21+
#else
22+
"BepInEx";
23+
#endif
2424

2525
public static ExplorerCore Instance { get; private set; }
2626

@@ -30,11 +30,10 @@ public ExplorerCore()
3030

3131
ModConfig.OnLoad();
3232

33-
InputHelper.Init();
34-
3533
new MainMenu();
3634
new WindowManager();
3735

36+
InputHelper.Init();
3837
CursorControl.Init();
3938

4039
Log($"{NAME} {VERSION} initialized.");

src/Helpers/InputHelper.cs

Lines changed: 155 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,41 @@
55
namespace Explorer
66
{
77
/// <summary>
8-
/// Version-agnostic UnityEngine Input module using Reflection.
8+
/// Version-agnostic Input module using Reflection.
99
/// </summary>
1010
public static class InputHelper
1111
{
1212
// If Input module failed to load at all
1313
public static bool NO_INPUT;
1414

15-
// Base UnityEngine.Input class
16-
private static Type Input => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input"));
15+
// If using new InputSystem module
16+
public static bool USING_NEW_INPUT;
17+
18+
// Cached Types
19+
private static Type TInput => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input"));
1720
private static Type _input;
1821

19-
// Cached member infos
22+
private static Type TKeyboard => _keyboardSys ?? (_keyboardSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
23+
private static Type _keyboardSys;
24+
25+
private static Type TMouse => _mouseSys ?? (_mouseSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse"));
26+
private static Type _mouseSys;
27+
28+
private static Type TKey => _key ?? (_key = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Key"));
29+
private static Type _key;
30+
31+
// Cached member infos (new system)
32+
private static PropertyInfo _keyboardCurrent;
33+
private static PropertyInfo _kbItemProp;
34+
private static PropertyInfo _isPressed;
35+
private static PropertyInfo _wasPressedThisFrame;
36+
private static PropertyInfo _mouseCurrent;
37+
private static PropertyInfo _leftButton;
38+
private static PropertyInfo _rightButton;
39+
private static PropertyInfo _position;
40+
private static MethodInfo _readValueMethod;
41+
42+
// Cached member infos (legacy)
2043
private static PropertyInfo _mousePosition;
2144
private static MethodInfo _getKey;
2245
private static MethodInfo _getKeyDown;
@@ -25,74 +48,175 @@ public static class InputHelper
2548

2649
public static void Init()
2750
{
28-
if (Input == null && !TryManuallyLoadInput())
51+
if (TKeyboard != null || TryManuallyLoadNewInput())
2952
{
30-
NO_INPUT = true;
53+
InitNewInput();
3154
return;
3255
}
3356

34-
// Cache reflection now that we know Input is loaded
57+
if (TInput != null || TryManuallyLoadLegacyInput())
58+
{
59+
InitLegacyInput();
60+
return;
61+
}
62+
63+
ExplorerCore.LogWarning("Could not find any Input module!");
64+
NO_INPUT = true;
65+
}
66+
67+
private static void InitNewInput()
68+
{
69+
ExplorerCore.Log("Initializing new InputSystem support...");
70+
71+
USING_NEW_INPUT = true;
72+
73+
_keyboardCurrent = TKeyboard.GetProperty("current");
74+
_kbItemProp = TKeyboard.GetProperty("Item", new Type[] { TKey });
75+
76+
var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
77+
_isPressed = btnControl.GetProperty("isPressed");
78+
_wasPressedThisFrame = btnControl.GetProperty("wasPressedThisFrame");
79+
80+
_mouseCurrent = TMouse.GetProperty("current");
81+
_leftButton = TMouse.GetProperty("leftButton");
82+
_rightButton = TMouse.GetProperty("rightButton");
83+
84+
_position = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
85+
.GetProperty("position");
86+
87+
_readValueMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
88+
.MakeGenericType(typeof(Vector2))
89+
.GetMethod("ReadValue");
90+
}
91+
92+
private static void InitLegacyInput()
93+
{
94+
ExplorerCore.Log("Initializing Legacy Input support...");
3595

36-
_mousePosition = Input.GetProperty("mousePosition");
96+
_mousePosition = TInput.GetProperty("mousePosition");
97+
_getKey = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
98+
_getKeyDown = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
99+
_getMouseButton = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) });
100+
_getMouseButtonDown = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
101+
}
37102

38-
_getKey = Input.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
39-
_getKeyDown = Input.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
40-
_getMouseButton = Input.GetMethod("GetMouseButton", new Type[] { typeof(int) });
41-
_getMouseButtonDown = Input.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
103+
private static bool TryManuallyLoadNewInput()
104+
{
105+
if (ReflectionHelpers.LoadModule("Unity.InputSystem") && TKeyboard != null)
106+
{
107+
ExplorerCore.Log("Loaded new InputSystem module!");
108+
return true;
109+
}
110+
else
111+
{
112+
return false;
113+
}
42114
}
43115

44-
#pragma warning disable IDE1006 // Camel-case property (Unity style)
45-
public static Vector3 mousePosition
116+
private static bool TryManuallyLoadLegacyInput()
117+
{
118+
if ((ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule") || ReflectionHelpers.LoadModule("UnityEngine.CoreModule"))
119+
&& TInput != null)
120+
{
121+
ExplorerCore.Log("Loaded legacy InputModule!");
122+
return true;
123+
}
124+
else
125+
{
126+
return false;
127+
}
128+
}
129+
130+
public static Vector3 MousePosition
46131
{
47132
get
48133
{
49134
if (NO_INPUT) return Vector3.zero;
135+
136+
if (USING_NEW_INPUT)
137+
{
138+
var mouse = _mouseCurrent.GetValue(null, null);
139+
var pos = _position.GetValue(mouse, null);
140+
141+
return (Vector2)_readValueMethod.Invoke(pos, new object[0]);
142+
}
143+
50144
return (Vector3)_mousePosition.GetValue(null, null);
51145
}
52146
}
53-
#pragma warning restore IDE1006
54147

55148
public static bool GetKeyDown(KeyCode key)
56149
{
57150
if (NO_INPUT) return false;
151+
152+
if (USING_NEW_INPUT)
153+
{
154+
var parsed = Enum.Parse(TKey, key.ToString());
155+
var currentKB = _keyboardCurrent.GetValue(null, null);
156+
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
157+
158+
return (bool)_wasPressedThisFrame.GetValue(actualKey, null);
159+
}
160+
58161
return (bool)_getKeyDown.Invoke(null, new object[] { key });
59162
}
60163

61164
public static bool GetKey(KeyCode key)
62165
{
63166
if (NO_INPUT) return false;
167+
168+
if (USING_NEW_INPUT)
169+
{
170+
var parsed = Enum.Parse(TKey, key.ToString());
171+
var currentKB = _keyboardCurrent.GetValue(null, null);
172+
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
173+
174+
return (bool)_isPressed.GetValue(actualKey, null);
175+
}
176+
64177
return (bool)_getKey.Invoke(null, new object[] { key });
65178
}
66179

67-
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
180+
/// <param name="btn">0/1 = left, 2 = middle, 3 = right, etc</param>
68181
public static bool GetMouseButtonDown(int btn)
69182
{
70183
if (NO_INPUT) return false;
184+
185+
if (USING_NEW_INPUT)
186+
{
187+
var mouse = _mouseCurrent.GetValue(null, null);
188+
189+
PropertyInfo btnProp;
190+
if (btn < 2) btnProp = _leftButton;
191+
else btnProp = _rightButton;
192+
193+
var actualBtn = btnProp.GetValue(mouse, null);
194+
195+
return (bool)_wasPressedThisFrame.GetValue(actualBtn, null);
196+
}
197+
71198
return (bool)_getMouseButtonDown.Invoke(null, new object[] { btn });
72199
}
73200

74201
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
75202
public static bool GetMouseButton(int btn)
76203
{
77204
if (NO_INPUT) return false;
78-
return (bool)_getMouseButton.Invoke(null, new object[] { btn });
79-
}
80-
81-
private static bool TryManuallyLoadInput()
82-
{
83-
ExplorerCore.Log("UnityEngine.Input is null, trying to load manually....");
84205

85-
if ((ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule") || ReflectionHelpers.LoadModule("UnityEngine.CoreModule"))
86-
&& Input != null)
87-
{
88-
ExplorerCore.Log("Ok!");
89-
return true;
90-
}
91-
else
206+
if (USING_NEW_INPUT)
92207
{
93-
ExplorerCore.Log("Could not load Input module!");
94-
return false;
208+
var mouse = _mouseCurrent.GetValue(null, null);
209+
210+
PropertyInfo btnProp;
211+
if (btn < 2) btnProp = _leftButton;
212+
else btnProp = _rightButton;
213+
214+
var actualBtn = btnProp.GetValue(mouse, null);
215+
216+
return (bool)_isPressed.GetValue(actualBtn, null);
95217
}
218+
219+
return (bool)_getMouseButton.Invoke(null, new object[] { btn });
96220
}
97221
}
98222
}

src/Menu/InspectUnderMouse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void InspectRaycast()
3333
if (!UnityHelpers.MainCamera)
3434
return;
3535

36-
var ray = UnityHelpers.MainCamera.ScreenPointToRay(InputHelper.mousePosition);
36+
var ray = UnityHelpers.MainCamera.ScreenPointToRay(InputHelper.MousePosition);
3737

3838
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
3939
{
@@ -61,7 +61,7 @@ public static void OnGUI()
6161
{
6262
if (m_objUnderMouseName != "")
6363
{
64-
var pos = InputHelper.mousePosition;
64+
var pos = InputHelper.MousePosition;
6565
var rect = new Rect(
6666
pos.x - (Screen.width / 2), // x
6767
Screen.height - pos.y - 50, // y

src/Menu/ResizeDrag.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Rect ResizeWindow(Rect _rect, int ID)
3838
//var r = GUILayoutUtility.GetLastRect();
3939
var r = Internal_LayoutUtility.GetLastRect();
4040

41-
var mousePos = InputHelper.mousePosition;
41+
var mousePos = InputHelper.MousePosition;
4242

4343
try
4444
{
@@ -125,7 +125,7 @@ public static Rect ResizeWindow(Rect _rect, int ID)
125125
//var r = GUILayoutUtility.GetLastRect();
126126
var r = GUILayoutUtility.GetLastRect();
127127

128-
var mousePos = InputHelper.mousePosition;
128+
var mousePos = InputHelper.MousePosition;
129129

130130
var mouse = GUIUnstrip.ScreenToGUIPoint(new Vector2(mousePos.x, Screen.height - mousePos.y));
131131
if (r.Contains(mouse) && InputHelper.GetMouseButtonDown(0))

src/Menu/Windows/WindowManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static bool IsMouseInWindow
189189

190190
private static bool RectContainsMouse(Rect rect)
191191
{
192-
var mousePos = InputHelper.mousePosition;
192+
var mousePos = InputHelper.MousePosition;
193193
return rect.Contains(new Vector2(mousePos.x, Screen.height - mousePos.y));
194194
}
195195

0 commit comments

Comments
 (0)