@@ -9,7 +9,7 @@ namespace Explorer
9
9
/// </summary>
10
10
public static class InputHelper
11
11
{
12
- // If Input module failed to load at all
12
+ // If no Input modules loaded at all
13
13
public static bool NO_INPUT ;
14
14
15
15
// If using new InputSystem module
@@ -19,129 +19,114 @@ public static class InputHelper
19
19
private static Type TInput => _input ?? ( _input = ReflectionHelpers . GetTypeByName ( "UnityEngine.Input" ) ) ;
20
20
private static Type _input ;
21
21
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 ;
24
24
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 ;
27
27
28
28
private static Type TKey => _key ?? ( _key = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Key" ) ) ;
29
29
private static Type _key ;
30
30
31
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 ;
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 ;
41
56
42
57
// 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 ;
48
63
49
64
public static void Init ( )
50
65
{
51
- if ( TKeyboard != null || TryManuallyLoadNewInput ( ) )
66
+ if ( TKeyboard != null || TryLoadModule ( "Unity.InputSystem" , TKeyboard ) )
52
67
{
53
68
InitNewInput ( ) ;
54
- return ;
55
69
}
56
-
57
- if ( TInput != null || TryManuallyLoadLegacyInput ( ) )
70
+ else if ( TInput != null || TryLoadModule ( "UnityEngine.Input" , TInput ) )
58
71
{
59
72
InitLegacyInput ( ) ;
60
- return ;
61
73
}
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
+ }
65
79
}
66
80
81
+ private static bool TryLoadModule ( string dll , Type check ) => ReflectionHelpers . LoadModule ( dll ) && check != null ;
82
+
67
83
private static void InitNewInput ( )
68
84
{
69
85
ExplorerCore . Log ( "Initializing new InputSystem support..." ) ;
70
86
71
87
USING_NEW_INPUT = true ;
72
88
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 } ) ;
75
91
76
92
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" ) ;
79
95
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" ) ;
83
99
84
- _position = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Pointer" )
85
- . GetProperty ( "position" ) ;
100
+ _positionProp = ReflectionHelpers . GetTypeByName ( "UnityEngine.InputSystem.Pointer" )
101
+ . GetProperty ( "position" ) ;
86
102
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" ) ;
90
106
}
91
107
92
108
private static void InitLegacyInput ( )
93
109
{
94
110
ExplorerCore . Log ( "Initializing Legacy Input support..." ) ;
95
111
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 ) } ) ;
128
117
}
129
118
130
119
public static Vector3 MousePosition
131
120
{
132
121
get
133
122
{
134
- if ( NO_INPUT ) return Vector3 . zero ;
123
+ if ( NO_INPUT )
124
+ return Vector3 . zero ;
135
125
136
126
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 ] ) ;
143
128
144
- return ( Vector3 ) _mousePosition . GetValue ( null , null ) ;
129
+ return ( Vector3 ) _mousePositionProp . GetValue ( null , null ) ;
145
130
}
146
131
}
147
132
@@ -151,14 +136,13 @@ public static bool GetKeyDown(KeyCode key)
151
136
152
137
if ( USING_NEW_INPUT )
153
138
{
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 } ) ;
157
141
158
- return ( bool ) _wasPressedThisFrame . GetValue ( actualKey , null ) ;
142
+ return ( bool ) _btnWasPressedProp . GetValue ( actualKey , null ) ;
159
143
}
160
144
161
- return ( bool ) _getKeyDown . Invoke ( null , new object [ ] { key } ) ;
145
+ return ( bool ) _getKeyDownMethod . Invoke ( null , new object [ ] { key } ) ;
162
146
}
163
147
164
148
public static bool GetKey ( KeyCode key )
@@ -168,55 +152,54 @@ public static bool GetKey(KeyCode key)
168
152
if ( USING_NEW_INPUT )
169
153
{
170
154
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 } ) ;
173
156
174
- return ( bool ) _isPressed . GetValue ( actualKey , null ) ;
157
+ return ( bool ) _btnIsPressedProp . GetValue ( actualKey , null ) ;
175
158
}
176
159
177
- return ( bool ) _getKey . Invoke ( null , new object [ ] { key } ) ;
160
+ return ( bool ) _getKeyMethod . Invoke ( null , new object [ ] { key } ) ;
178
161
}
179
162
180
- /// <param name="btn">0/1 = left, 2 = middle, 3 = right, etc </param>
163
+ /// <param name="btn">0 = left, 1 = right, 2 = middle. </param>
181
164
public static bool GetMouseButtonDown ( int btn )
182
165
{
183
166
if ( NO_INPUT ) return false ;
184
167
185
168
if ( USING_NEW_INPUT )
186
169
{
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
+ }
194
177
195
- return ( bool ) _wasPressedThisFrame . GetValue ( actualBtn , null ) ;
178
+ return ( bool ) _btnWasPressedProp . GetValue ( actualBtn , null ) ;
196
179
}
197
180
198
- return ( bool ) _getMouseButtonDown . Invoke ( null , new object [ ] { btn } ) ;
181
+ return ( bool ) _getMouseButtonDownMethod . Invoke ( null , new object [ ] { btn } ) ;
199
182
}
200
183
201
- /// <param name="btn">1 = left, 2 = middle, 3 = right, etc </param>
184
+ /// <param name="btn">0 = left, 1 = right, 2 = middle. </param>
202
185
public static bool GetMouseButton ( int btn )
203
186
{
204
187
if ( NO_INPUT ) return false ;
205
188
206
189
if ( USING_NEW_INPUT )
207
190
{
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
+ }
215
198
216
- return ( bool ) _isPressed . GetValue ( actualBtn , null ) ;
199
+ return ( bool ) _btnIsPressedProp . GetValue ( actualBtn , null ) ;
217
200
}
218
201
219
- return ( bool ) _getMouseButton . Invoke ( null , new object [ ] { btn } ) ;
202
+ return ( bool ) _getMouseButtonMethod . Invoke ( null , new object [ ] { btn } ) ;
220
203
}
221
204
}
222
205
}
0 commit comments