* Some performance improvements for the new InputSystem support (affects some 2019.3+ games)
* Fixed a small mistake with left/right mouse button checking.
This commit is contained in:
sinaioutlander 2020-10-02 18:40:51 +10:00
parent 3971e49ce1
commit c991cb4b22
3 changed files with 93 additions and 109 deletions

View File

@ -5,7 +5,7 @@ namespace Explorer
public class ExplorerCore public class ExplorerCore
{ {
public const string NAME = "Explorer (" + PLATFORM + ", " + MODLOADER + ")"; public const string NAME = "Explorer (" + PLATFORM + ", " + MODLOADER + ")";
public const string VERSION = "1.8.21"; public const string VERSION = "1.8.22";
public const string AUTHOR = "Sinai"; public const string AUTHOR = "Sinai";
public const string GUID = "com.sinai.explorer"; public const string GUID = "com.sinai.explorer";

View File

@ -9,7 +9,7 @@ namespace Explorer
/// </summary> /// </summary>
public static class InputHelper public static class InputHelper
{ {
// If Input module failed to load at all // If no Input modules loaded at all
public static bool NO_INPUT; public static bool NO_INPUT;
// If using new InputSystem module // If using new InputSystem module
@ -19,129 +19,114 @@ namespace Explorer
private static Type TInput => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input")); private static Type TInput => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input"));
private static Type _input; private static Type _input;
private static Type TKeyboard => _keyboardSys ?? (_keyboardSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard")); private static Type TKeyboard => _keyboard ?? (_keyboard = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
private static Type _keyboardSys; private static Type _keyboard;
private static Type TMouse => _mouseSys ?? (_mouseSys = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse")); private static Type TMouse => _mouse ?? (_mouse = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Mouse"));
private static Type _mouseSys; private static Type _mouse;
private static Type TKey => _key ?? (_key = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Key")); private static Type TKey => _key ?? (_key = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Key"));
private static Type _key; private static Type _key;
// Cached member infos (new system) // Cached member infos (new system)
private static PropertyInfo _keyboardCurrent; private static PropertyInfo _btnIsPressedProp;
private static PropertyInfo _kbItemProp; private static PropertyInfo _btnWasPressedProp;
private static PropertyInfo _isPressed;
private static PropertyInfo _wasPressedThisFrame; private static object CurrentKeyboard => _currentKeyboard ?? (_currentKeyboard = _kbCurrentProp.GetValue(null, null));
private static PropertyInfo _mouseCurrent; private static object _currentKeyboard;
private static PropertyInfo _leftButton; private static PropertyInfo _kbCurrentProp;
private static PropertyInfo _rightButton; private static PropertyInfo _kbIndexer;
private static PropertyInfo _position;
private static MethodInfo _readValueMethod; private static object CurrentMouse => _currentMouse ?? (_currentMouse = _mouseCurrentProp.GetValue(null, null));
private static object _currentMouse;
private static PropertyInfo _mouseCurrentProp;
private static object LeftMouseButton => _lmb ?? (_lmb = _leftButtonProp.GetValue(CurrentMouse, null));
private static object _lmb;
private static PropertyInfo _leftButtonProp;
private static object RightMouseButton => _rmb ?? (_rmb = _rightButtonProp.GetValue(CurrentMouse, null));
private static object _rmb;
private static PropertyInfo _rightButtonProp;
private static object MousePositionInfo => _pos ?? (_pos = _positionProp.GetValue(CurrentMouse, null));
private static object _pos;
private static PropertyInfo _positionProp;
private static MethodInfo _readVector2InputMethod;
// Cached member infos (legacy) // Cached member infos (legacy)
private static PropertyInfo _mousePosition; private static PropertyInfo _mousePositionProp;
private static MethodInfo _getKey; private static MethodInfo _getKeyMethod;
private static MethodInfo _getKeyDown; private static MethodInfo _getKeyDownMethod;
private static MethodInfo _getMouseButton; private static MethodInfo _getMouseButtonMethod;
private static MethodInfo _getMouseButtonDown; private static MethodInfo _getMouseButtonDownMethod;
public static void Init() public static void Init()
{ {
if (TKeyboard != null || TryManuallyLoadNewInput()) if (TKeyboard != null || TryLoadModule("Unity.InputSystem", TKeyboard))
{ {
InitNewInput(); InitNewInput();
return;
} }
else if (TInput != null || TryLoadModule("UnityEngine.Input", TInput))
if (TInput != null || TryManuallyLoadLegacyInput())
{ {
InitLegacyInput(); InitLegacyInput();
return;
} }
else
ExplorerCore.LogWarning("Could not find any Input module!"); {
NO_INPUT = true; ExplorerCore.LogWarning("Could not find any Input module!");
NO_INPUT = true;
}
} }
private static bool TryLoadModule(string dll, Type check) => ReflectionHelpers.LoadModule(dll) && check != null;
private static void InitNewInput() private static void InitNewInput()
{ {
ExplorerCore.Log("Initializing new InputSystem support..."); ExplorerCore.Log("Initializing new InputSystem support...");
USING_NEW_INPUT = true; USING_NEW_INPUT = true;
_keyboardCurrent = TKeyboard.GetProperty("current"); _kbCurrentProp = TKeyboard.GetProperty("current");
_kbItemProp = TKeyboard.GetProperty("Item", new Type[] { TKey }); _kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl"); var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
_isPressed = btnControl.GetProperty("isPressed"); _btnIsPressedProp = btnControl.GetProperty("isPressed");
_wasPressedThisFrame = btnControl.GetProperty("wasPressedThisFrame"); _btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
_mouseCurrent = TMouse.GetProperty("current"); _mouseCurrentProp = TMouse.GetProperty("current");
_leftButton = TMouse.GetProperty("leftButton"); _leftButtonProp = TMouse.GetProperty("leftButton");
_rightButton = TMouse.GetProperty("rightButton"); _rightButtonProp = TMouse.GetProperty("rightButton");
_position = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer") _positionProp = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
.GetProperty("position"); .GetProperty("position");
_readValueMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1") _readVector2InputMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
.MakeGenericType(typeof(Vector2)) .MakeGenericType(typeof(Vector2))
.GetMethod("ReadValue"); .GetMethod("ReadValue");
} }
private static void InitLegacyInput() private static void InitLegacyInput()
{ {
ExplorerCore.Log("Initializing Legacy Input support..."); ExplorerCore.Log("Initializing Legacy Input support...");
_mousePosition = TInput.GetProperty("mousePosition"); _mousePositionProp = TInput.GetProperty("mousePosition");
_getKey = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) }); _getKeyMethod = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
_getKeyDown = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) }); _getKeyDownMethod = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
_getMouseButton = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) }); _getMouseButtonMethod = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) });
_getMouseButtonDown = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) }); _getMouseButtonDownMethod = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
}
private static bool TryManuallyLoadNewInput()
{
if (ReflectionHelpers.LoadModule("Unity.InputSystem") && TKeyboard != null)
{
ExplorerCore.Log("Loaded new InputSystem module!");
return true;
}
else
{
return false;
}
}
private static bool TryManuallyLoadLegacyInput()
{
if ((ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule") || ReflectionHelpers.LoadModule("UnityEngine.CoreModule"))
&& TInput != null)
{
ExplorerCore.Log("Loaded legacy InputModule!");
return true;
}
else
{
return false;
}
} }
public static Vector3 MousePosition public static Vector3 MousePosition
{ {
get get
{ {
if (NO_INPUT) return Vector3.zero; if (NO_INPUT)
return Vector3.zero;
if (USING_NEW_INPUT) if (USING_NEW_INPUT)
{ return (Vector2)_readVector2InputMethod.Invoke(MousePositionInfo, new object[0]);
var mouse = _mouseCurrent.GetValue(null, null);
var pos = _position.GetValue(mouse, null);
return (Vector2)_readValueMethod.Invoke(pos, new object[0]); return (Vector3)_mousePositionProp.GetValue(null, null);
}
return (Vector3)_mousePosition.GetValue(null, null);
} }
} }
@ -151,14 +136,13 @@ namespace Explorer
if (USING_NEW_INPUT) if (USING_NEW_INPUT)
{ {
var parsed = Enum.Parse(TKey, key.ToString()); var parsedKey = Enum.Parse(TKey, key.ToString());
var currentKB = _keyboardCurrent.GetValue(null, null); var actualKey = _kbIndexer.GetValue(CurrentKeyboard, new object[] { parsedKey });
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
return (bool)_wasPressedThisFrame.GetValue(actualKey, null); return (bool)_btnWasPressedProp.GetValue(actualKey, null);
} }
return (bool)_getKeyDown.Invoke(null, new object[] { key }); return (bool)_getKeyDownMethod.Invoke(null, new object[] { key });
} }
public static bool GetKey(KeyCode key) public static bool GetKey(KeyCode key)
@ -168,55 +152,54 @@ namespace Explorer
if (USING_NEW_INPUT) if (USING_NEW_INPUT)
{ {
var parsed = Enum.Parse(TKey, key.ToString()); var parsed = Enum.Parse(TKey, key.ToString());
var currentKB = _keyboardCurrent.GetValue(null, null); var actualKey = _kbIndexer.GetValue(CurrentKeyboard, new object[] { parsed });
var actualKey = _kbItemProp.GetValue(currentKB, new object[] { parsed });
return (bool)_isPressed.GetValue(actualKey, null); return (bool)_btnIsPressedProp.GetValue(actualKey, null);
} }
return (bool)_getKey.Invoke(null, new object[] { key }); return (bool)_getKeyMethod.Invoke(null, new object[] { key });
} }
/// <param name="btn">0/1 = left, 2 = middle, 3 = right, etc</param> /// <param name="btn">0 = left, 1 = right, 2 = middle.</param>
public static bool GetMouseButtonDown(int btn) public static bool GetMouseButtonDown(int btn)
{ {
if (NO_INPUT) return false; if (NO_INPUT) return false;
if (USING_NEW_INPUT) if (USING_NEW_INPUT)
{ {
var mouse = _mouseCurrent.GetValue(null, null); object actualBtn;
switch (btn)
{
case 0: actualBtn = LeftMouseButton; break;
case 1: actualBtn = RightMouseButton; break;
default: throw new NotImplementedException();
}
PropertyInfo btnProp; return (bool)_btnWasPressedProp.GetValue(actualBtn, null);
if (btn < 2) btnProp = _leftButton;
else btnProp = _rightButton;
var actualBtn = btnProp.GetValue(mouse, null);
return (bool)_wasPressedThisFrame.GetValue(actualBtn, null);
} }
return (bool)_getMouseButtonDown.Invoke(null, new object[] { btn }); return (bool)_getMouseButtonDownMethod.Invoke(null, new object[] { btn });
} }
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param> /// <param name="btn">0 = left, 1 = right, 2 = middle.</param>
public static bool GetMouseButton(int btn) public static bool GetMouseButton(int btn)
{ {
if (NO_INPUT) return false; if (NO_INPUT) return false;
if (USING_NEW_INPUT) if (USING_NEW_INPUT)
{ {
var mouse = _mouseCurrent.GetValue(null, null); object actualBtn;
switch (btn)
{
case 0: actualBtn = LeftMouseButton; break;
case 1: actualBtn = RightMouseButton; break;
default: throw new NotImplementedException();
}
PropertyInfo btnProp; return (bool)_btnIsPressedProp.GetValue(actualBtn, null);
if (btn < 2) btnProp = _leftButton;
else btnProp = _rightButton;
var actualBtn = btnProp.GetValue(mouse, null);
return (bool)_isPressed.GetValue(actualBtn, null);
} }
return (bool)_getMouseButton.Invoke(null, new object[] { btn }); return (bool)_getMouseButtonMethod.Invoke(null, new object[] { btn });
} }
} }
} }

View File

@ -45,6 +45,7 @@ namespace Explorer
MethodInput = @"// This is a basic C# console. MethodInput = @"// This is a basic C# console.
// Some common using directives are added by default, you can add more below. // Some common using directives are added by default, you can add more below.
// If you want to return some output, Debug.Log() or MelonLogger.Log() it. // If you want to return some output, Debug.Log() or MelonLogger.Log() it.
" "
#if ML #if ML
+ @"MelonLogger.Log(""hello world"");"; + @"MelonLogger.Log(""hello world"");";