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

Commit 3ffdcea

Browse files
committed
Force all devices to always be supported by InputSystem
1 parent dfd5526 commit 3ffdcea

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/Core/Input/InputSystem.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class InputSystem : IHandleInput
1212
{
1313
public InputSystem()
1414
{
15+
SetupSupportedDevices();
16+
1517
m_kbCurrentProp = TKeyboard.GetProperty("current");
1618
m_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
1719

@@ -32,7 +34,37 @@ public InputSystem()
3234
.GetMethod("ReadValue");
3335
}
3436

35-
#region reflection cache
37+
internal static void SetupSupportedDevices()
38+
{
39+
try
40+
{
41+
// typeof(InputSystem)
42+
Type TInputSystem = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputSystem");
43+
// InputSystem.settings
44+
var settings = TInputSystem.GetProperty("settings", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
45+
// typeof(InputSettings)
46+
Type TSettings = settings.GetActualType();
47+
// InputSettings.supportedDevices
48+
PropertyInfo supportedProp = TSettings.GetProperty("supportedDevices", BindingFlags.Public | BindingFlags.Instance);
49+
var supportedDevices = supportedProp.GetValue(settings, null);
50+
// An empty supportedDevices list means all devices are supported.
51+
#if CPP
52+
// weird hack for il2cpp, use the implicit operator and cast Il2CppStringArray to ReadOnlyArray<string>
53+
var args = new object[] { new UnhollowerBaseLib.Il2CppStringArray(0) };
54+
var method = supportedDevices.GetActualType().GetMethod("op_Implicit", BindingFlags.Static | BindingFlags.Public);
55+
supportedProp.SetValue(settings, method.Invoke(null, args), null);
56+
#else
57+
supportedProp.SetValue(settings, Activator.CreateInstance(supportedDevices.GetActualType(), new object[] { new string[0] }), null);
58+
#endif
59+
}
60+
catch (Exception ex)
61+
{
62+
ExplorerCore.LogWarning($"Exception setting up InputSystem.settings.supportedDevices list!");
63+
ExplorerCore.Log(ex);
64+
}
65+
}
66+
67+
#region reflection cache
3668

3769
public static Type TKeyboard => m_tKeyboard ?? (m_tKeyboard = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
3870
private static Type m_tKeyboard;
@@ -73,7 +105,7 @@ public InputSystem()
73105
private static object m_scrollInfo;
74106
private static PropertyInfo m_scrollDeltaProp;
75107

76-
#endregion
108+
#endregion
77109

78110
public Vector2 MousePosition
79111
{
@@ -138,6 +170,8 @@ internal object GetActualKey(KeyCode key)
138170

139171
public bool GetMouseButtonDown(int btn)
140172
{
173+
if (CurrentMouse == null)
174+
return false;
141175
switch (btn)
142176
{
143177
case 0: return (bool)m_btnWasPressedProp.GetValue(LeftMouseButton, null);
@@ -149,6 +183,8 @@ public bool GetMouseButtonDown(int btn)
149183

150184
public bool GetMouseButton(int btn)
151185
{
186+
if (CurrentMouse == null)
187+
return false;
152188
switch (btn)
153189
{
154190
case 0: return (bool)m_btnIsPressedProp.GetValue(LeftMouseButton, null);

0 commit comments

Comments
 (0)