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

Commit c991cb4

Browse files
committed
1.8.22
* Some performance improvements for the new InputSystem support (affects some 2019.3+ games) * Fixed a small mistake with left/right mouse button checking.
1 parent 3971e49 commit c991cb4

File tree

3 files changed

+93
-109
lines changed

3 files changed

+93
-109
lines changed

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Explorer
55
public class ExplorerCore
66
{
77
public const string NAME = "Explorer (" + PLATFORM + ", " + MODLOADER + ")";
8-
public const string VERSION = "1.8.21";
8+
public const string VERSION = "1.8.22";
99
public const string AUTHOR = "Sinai";
1010
public const string GUID = "com.sinai.explorer";
1111

src/Helpers/InputHelper.cs

Lines changed: 91 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Explorer
99
/// </summary>
1010
public static class InputHelper
1111
{
12-
// If Input module failed to load at all
12+
// If no Input modules loaded at all
1313
public static bool NO_INPUT;
1414

1515
// If using new InputSystem module
@@ -19,129 +19,114 @@ public static class InputHelper
1919
private static Type TInput => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input"));
2020
private static Type _input;
2121

22-
private static Type TKeyboard => _keyboardSys ?? (_keyboardSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
23-
private static Type _keyboardSys;
22+
private static Type TKeyboard => _keyboard ?? (_keyboard = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
23+
private static Type _keyboard;
2424

25-
private static Type TMouse => _mouseSys ?? (_mouseSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse"));
26-
private static Type _mouseSys;
25+
private static Type TMouse => _mouse ?? (_mouse = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse"));
26+
private static Type _mouse;
2727

2828
private static Type TKey => _key ?? (_key = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Key"));
2929
private static Type _key;
3030

3131
// 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;
32+
private static PropertyInfo _btnIsPressedProp;
33+
private static PropertyInfo _btnWasPressedProp;
34+
35+
private static object CurrentKeyboard => _currentKeyboard ?? (_currentKeyboard = _kbCurrentProp.GetValue(null, null));
36+
private static object _currentKeyboard;
37+
private static PropertyInfo _kbCurrentProp;
38+
private static PropertyInfo _kbIndexer;
39+
40+
private static object CurrentMouse => _currentMouse ?? (_currentMouse = _mouseCurrentProp.GetValue(null, null));
41+
private static object _currentMouse;
42+
private static PropertyInfo _mouseCurrentProp;
43+
44+
private static object LeftMouseButton => _lmb ?? (_lmb = _leftButtonProp.GetValue(CurrentMouse, null));
45+
private static object _lmb;
46+
private static PropertyInfo _leftButtonProp;
47+
48+
private static object RightMouseButton => _rmb ?? (_rmb = _rightButtonProp.GetValue(CurrentMouse, null));
49+
private static object _rmb;
50+
private static PropertyInfo _rightButtonProp;
51+
52+
private static object MousePositionInfo => _pos ?? (_pos = _positionProp.GetValue(CurrentMouse, null));
53+
private static object _pos;
54+
private static PropertyInfo _positionProp;
55+
private static MethodInfo _readVector2InputMethod;
4156

4257
// Cached member infos (legacy)
43-
private static PropertyInfo _mousePosition;
44-
private static MethodInfo _getKey;
45-
private static MethodInfo _getKeyDown;
46-
private static MethodInfo _getMouseButton;
47-
private static MethodInfo _getMouseButtonDown;
58+
private static PropertyInfo _mousePositionProp;
59+
private static MethodInfo _getKeyMethod;
60+
private static MethodInfo _getKeyDownMethod;
61+
private static MethodInfo _getMouseButtonMethod;
62+
private static MethodInfo _getMouseButtonDownMethod;
4863

4964
public static void Init()
5065
{
51-
if (TKeyboard != null || TryManuallyLoadNewInput())
66+
if (TKeyboard != null || TryLoadModule("Unity.InputSystem", TKeyboard))
5267
{
5368
InitNewInput();
54-
return;
5569
}
56-
57-
if (TInput != null || TryManuallyLoadLegacyInput())
70+
else if (TInput != null || TryLoadModule("UnityEngine.Input", TInput))
5871
{
5972
InitLegacyInput();
60-
return;
6173
}
62-
63-
ExplorerCore.LogWarning("Could not find any Input module!");
64-
NO_INPUT = true;
74+
else
75+
{
76+
ExplorerCore.LogWarning("Could not find any Input module!");
77+
NO_INPUT = true;
78+
}
6579
}
6680

81+
private static bool TryLoadModule(string dll, Type check) => ReflectionHelpers.LoadModule(dll) && check != null;
82+
6783
private static void InitNewInput()
6884
{
6985
ExplorerCore.Log("Initializing new InputSystem support...");
7086

7187
USING_NEW_INPUT = true;
7288

73-
_keyboardCurrent = TKeyboard.GetProperty("current");
74-
_kbItemProp = TKeyboard.GetProperty("Item", new Type[] { TKey });
89+
_kbCurrentProp = TKeyboard.GetProperty("current");
90+
_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
7591

7692
var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
77-
_isPressed = btnControl.GetProperty("isPressed");
78-
_wasPressedThisFrame = btnControl.GetProperty("wasPressedThisFrame");
93+
_btnIsPressedProp = btnControl.GetProperty("isPressed");
94+
_btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
7995

80-
_mouseCurrent = TMouse.GetProperty("current");
81-
_leftButton = TMouse.GetProperty("leftButton");
82-
_rightButton = TMouse.GetProperty("rightButton");
96+
_mouseCurrentProp = TMouse.GetProperty("current");
97+
_leftButtonProp = TMouse.GetProperty("leftButton");
98+
_rightButtonProp = TMouse.GetProperty("rightButton");
8399

84-
_position = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
85-
.GetProperty("position");
100+
_positionProp = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
101+
.GetProperty("position");
86102

87-
_readValueMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
88-
.MakeGenericType(typeof(Vector2))
89-
.GetMethod("ReadValue");
103+
_readVector2InputMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
104+
.MakeGenericType(typeof(Vector2))
105+
.GetMethod("ReadValue");
90106
}
91107

92108
private static void InitLegacyInput()
93109
{
94110
ExplorerCore.Log("Initializing Legacy Input support...");
95111

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-
}
102-
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-
}
114-
}
115-
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-
}
112+
_mousePositionProp = TInput.GetProperty("mousePosition");
113+
_getKeyMethod = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
114+
_getKeyDownMethod = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
115+
_getMouseButtonMethod = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) });
116+
_getMouseButtonDownMethod = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
128117
}
129118

130119
public static Vector3 MousePosition
131120
{
132121
get
133122
{
134-
if (NO_INPUT) return Vector3.zero;
123+
if (NO_INPUT)
124+
return Vector3.zero;
135125

136126
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-
}
127+
return (Vector2)_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
143128

144-
return (Vector3)_mousePosition.GetValue(null, null);
129+
return (Vector3)_mousePositionProp.GetValue(null, null);
145130
}
146131
}
147132

@@ -151,14 +136,13 @@ public static bool GetKeyDown(KeyCode key)
151136

152137
if (USING_NEW_INPUT)
153138
{
154-
var parsed = Enum.Parse(TKey, key.ToString());
155-
var currentKB = _keyboardCurrent.GetValue(null, null);
156-
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
139+
var parsedKey = Enum.Parse(TKey, key.ToString());
140+
var actualKey = _kbIndexer.GetValue(CurrentKeyboard, new object[] { parsedKey });
157141

158-
return (bool)_wasPressedThisFrame.GetValue(actualKey, null);
142+
return (bool)_btnWasPressedProp.GetValue(actualKey, null);
159143
}
160144

161-
return (bool)_getKeyDown.Invoke(null, new object[] { key });
145+
return (bool)_getKeyDownMethod.Invoke(null, new object[] { key });
162146
}
163147

164148
public static bool GetKey(KeyCode key)
@@ -168,55 +152,54 @@ public static bool GetKey(KeyCode key)
168152
if (USING_NEW_INPUT)
169153
{
170154
var parsed = Enum.Parse(TKey, key.ToString());
171-
var currentKB = _keyboardCurrent.GetValue(null, null);
172-
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
155+
var actualKey = _kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
173156

174-
return (bool)_isPressed.GetValue(actualKey, null);
157+
return (bool)_btnIsPressedProp.GetValue(actualKey, null);
175158
}
176159

177-
return (bool)_getKey.Invoke(null, new object[] { key });
160+
return (bool)_getKeyMethod.Invoke(null, new object[] { key });
178161
}
179162

180-
/// <param name="btn">0/1 = left, 2 = middle, 3 = right, etc</param>
163+
/// <param name="btn">0 = left, 1 = right, 2 = middle.</param>
181164
public static bool GetMouseButtonDown(int btn)
182165
{
183166
if (NO_INPUT) return false;
184167

185168
if (USING_NEW_INPUT)
186169
{
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);
170+
object actualBtn;
171+
switch (btn)
172+
{
173+
case 0: actualBtn = LeftMouseButton; break;
174+
case 1: actualBtn = RightMouseButton; break;
175+
default: throw new NotImplementedException();
176+
}
194177

195-
return (bool)_wasPressedThisFrame.GetValue(actualBtn, null);
178+
return (bool)_btnWasPressedProp.GetValue(actualBtn, null);
196179
}
197180

198-
return (bool)_getMouseButtonDown.Invoke(null, new object[] { btn });
181+
return (bool)_getMouseButtonDownMethod.Invoke(null, new object[] { btn });
199182
}
200183

201-
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
184+
/// <param name="btn">0 = left, 1 = right, 2 = middle.</param>
202185
public static bool GetMouseButton(int btn)
203186
{
204187
if (NO_INPUT) return false;
205188

206189
if (USING_NEW_INPUT)
207190
{
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);
191+
object actualBtn;
192+
switch (btn)
193+
{
194+
case 0: actualBtn = LeftMouseButton; break;
195+
case 1: actualBtn = RightMouseButton; break;
196+
default: throw new NotImplementedException();
197+
}
215198

216-
return (bool)_isPressed.GetValue(actualBtn, null);
199+
return (bool)_btnIsPressedProp.GetValue(actualBtn, null);
217200
}
218201

219-
return (bool)_getMouseButton.Invoke(null, new object[] { btn });
202+
return (bool)_getMouseButtonMethod.Invoke(null, new object[] { btn });
220203
}
221204
}
222205
}

src/Menu/MainMenu/Pages/ConsolePage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public override void Init()
4545
MethodInput = @"// This is a basic C# console.
4646
// Some common using directives are added by default, you can add more below.
4747
// If you want to return some output, Debug.Log() or MelonLogger.Log() it.
48+
4849
"
4950
#if ML
5051
+ @"MelonLogger.Log(""hello world"");";

0 commit comments

Comments
 (0)