55namespace 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}
0 commit comments