@@ -15,93 +15,106 @@ namespace Explorer
15
15
/// </summary>
16
16
public static class InputHelper
17
17
{
18
- public static void CheckInput ( )
19
- {
20
- if ( Input == null )
21
- {
22
- MelonLogger . Log ( "UnityEngine.Input is null, trying to load manually...." ) ;
23
-
24
- if ( ( TryLoad ( "UnityEngine.InputLegacyModule.dll" ) || TryLoad ( "UnityEngine.CoreModule.dll" ) ) && Input != null )
25
- {
26
- MelonLogger . Log ( "Ok!" ) ;
27
- }
28
- else
29
- {
30
- MelonLogger . Log ( "Could not load Input module!" ) ;
31
- }
32
-
33
- bool TryLoad ( string module )
34
- {
35
- var path = $@ "MelonLoader\Managed\{ module } ";
36
- if ( ! File . Exists ( path ) ) return false ;
37
-
38
- try
39
- {
40
- Assembly . Load ( File . ReadAllBytes ( path ) ) ;
41
- return true ;
42
- }
43
- catch ( Exception e )
44
- {
45
- MelonLogger . Log ( e . GetType ( ) + ", " + e . Message ) ;
46
- return false ;
47
- }
48
- }
49
- }
50
- }
18
+ // If Input module failed to load at all
19
+ public static bool NO_INPUT ;
51
20
52
- public static Type Input => _input ?? ( _input = ReflectionHelpers . GetTypeByName ( "UnityEngine.Input" ) ) ;
21
+ // Base UnityEngine.Input class
22
+ private static Type Input => _input ?? ( _input = ReflectionHelpers . GetTypeByName ( "UnityEngine.Input" ) ) ;
53
23
private static Type _input ;
54
24
55
- private static PropertyInfo MousePosInfo => _mousePosition ?? ( _mousePosition = Input ? . GetProperty ( "mousePosition" ) ) ;
25
+ // Cached member infos
56
26
private static PropertyInfo _mousePosition ;
57
-
58
- private static MethodInfo GetKeyInfo => _getKey ?? ( _getKey = Input ? . GetMethod ( "GetKey" , new Type [ ] { typeof ( KeyCode ) } ) ) ;
59
27
private static MethodInfo _getKey ;
60
-
61
- private static MethodInfo GetKeyDownInfo => _getKeyDown ?? ( _getKeyDown = Input ? . GetMethod ( "GetKeyDown" , new Type [ ] { typeof ( KeyCode ) } ) ) ;
62
28
private static MethodInfo _getKeyDown ;
63
-
64
- private static MethodInfo GetMouseButtonInfo => _getMouseButton ?? ( _getMouseButton = Input ? . GetMethod ( "GetMouseButton" , new Type [ ] { typeof ( int ) } ) ) ;
65
29
private static MethodInfo _getMouseButton ;
66
-
67
- private static MethodInfo GetMouseButtonDownInfo => _getMouseButtonDown ?? ( _getMouseButtonDown = Input ? . GetMethod ( "GetMouseButtonDown" , new Type [ ] { typeof ( int ) } ) ) ;
68
30
private static MethodInfo _getMouseButtonDown ;
69
31
32
+ public static void Init ( )
33
+ {
34
+ if ( Input == null && ! TryManuallyLoadInput ( ) )
35
+ {
36
+ NO_INPUT = true ;
37
+ return ;
38
+ }
39
+
40
+ // Cache reflection now that we know Input is loaded
41
+
42
+ _mousePosition = Input . GetProperty ( "mousePosition" ) ;
43
+
44
+ _getKey = Input . GetMethod ( "GetKey" , new Type [ ] { typeof ( KeyCode ) } ) ;
45
+ _getKeyDown = Input . GetMethod ( "GetKeyDown" , new Type [ ] { typeof ( KeyCode ) } ) ;
46
+ _getMouseButton = Input . GetMethod ( "GetMouseButton" , new Type [ ] { typeof ( int ) } ) ;
47
+ _getMouseButtonDown = Input . GetMethod ( "GetMouseButtonDown" , new Type [ ] { typeof ( int ) } ) ;
48
+ }
49
+
70
50
#pragma warning disable IDE1006 // Camel-case property (Unity style)
71
51
public static Vector3 mousePosition
72
52
{
73
53
get
74
54
{
75
- if ( Input == null ) return Vector3 . zero ;
76
- return ( Vector3 ) MousePosInfo . GetValue ( null ) ;
55
+ if ( NO_INPUT ) return Vector3 . zero ;
56
+ return ( Vector3 ) _mousePosition . GetValue ( null ) ;
77
57
}
78
58
}
79
59
#pragma warning restore IDE1006
80
60
81
61
public static bool GetKeyDown ( KeyCode key )
82
62
{
83
- if ( Input == null ) return false ;
84
- return ( bool ) GetKeyDownInfo . Invoke ( null , new object [ ] { key } ) ;
63
+ if ( NO_INPUT ) return false ;
64
+ return ( bool ) _getKeyDown . Invoke ( null , new object [ ] { key } ) ;
85
65
}
86
66
87
67
public static bool GetKey ( KeyCode key )
88
68
{
89
- if ( Input == null ) return false ;
90
- return ( bool ) GetKeyInfo . Invoke ( null , new object [ ] { key } ) ;
69
+ if ( NO_INPUT ) return false ;
70
+ return ( bool ) _getKey . Invoke ( null , new object [ ] { key } ) ;
91
71
}
92
72
93
73
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
94
74
public static bool GetMouseButtonDown ( int btn )
95
75
{
96
- if ( Input == null ) return false ;
97
- return ( bool ) GetMouseButtonDownInfo . Invoke ( null , new object [ ] { btn } ) ;
76
+ if ( NO_INPUT ) return false ;
77
+ return ( bool ) _getMouseButtonDown . Invoke ( null , new object [ ] { btn } ) ;
98
78
}
99
79
100
80
/// <param name="btn">1 = left, 2 = middle, 3 = right, etc</param>
101
81
public static bool GetMouseButton ( int btn )
102
82
{
103
- if ( Input == null ) return false ;
104
- return ( bool ) GetMouseButtonInfo . Invoke ( null , new object [ ] { btn } ) ;
83
+ if ( NO_INPUT ) return false ;
84
+ return ( bool ) _getMouseButton . Invoke ( null , new object [ ] { btn } ) ;
85
+ }
86
+
87
+ private static bool TryManuallyLoadInput ( )
88
+ {
89
+ MelonLogger . Log ( "UnityEngine.Input is null, trying to load manually...." ) ;
90
+
91
+ if ( ( TryLoad ( "UnityEngine.InputLegacyModule.dll" ) || TryLoad ( "UnityEngine.CoreModule.dll" ) ) && Input != null )
92
+ {
93
+ MelonLogger . Log ( "Ok!" ) ;
94
+ return true ;
95
+ }
96
+ else
97
+ {
98
+ MelonLogger . Log ( "Could not load Input module!" ) ;
99
+ return false ;
100
+ }
101
+
102
+ bool TryLoad ( string module )
103
+ {
104
+ var path = $@ "MelonLoader\Managed\{ module } ";
105
+ if ( ! File . Exists ( path ) ) return false ;
106
+
107
+ try
108
+ {
109
+ Assembly . Load ( File . ReadAllBytes ( path ) ) ;
110
+ return true ;
111
+ }
112
+ catch ( Exception e )
113
+ {
114
+ MelonLogger . Log ( e . GetType ( ) + ", " + e . Message ) ;
115
+ return false ;
116
+ }
117
+ }
105
118
}
106
119
}
107
120
}
0 commit comments