Skip to content

Commit d9b95a0

Browse files
aldelaro5ManlyMarco
authored andcommitted
Add input abstraction layer to support games with the Input class disabled
Originally pull request #444 Co-authored-by: ManlyMarco <39247311+manlymarco@users.noreply.github.com>
1 parent af5d939 commit d9b95a0

3 files changed

Lines changed: 866 additions & 15 deletions

File tree

‎Runtimes/Unity/BepInEx.Unity.Mono/BepInEx.Unity.Mono.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All"/>
3131
<PackageReference Include="HarmonyX" Version="2.10.2" />
3232
<PackageReference Include="UnityEngine" Version="5.6.1" IncludeAssets="compile"/>
33+
<PackageReference Include="Unity.InputSystem" version="1.5.0" IncludeAssets="compile"/>
3334
</ItemGroup>
3435
</Project>

‎Runtimes/Unity/BepInEx.Unity.Mono/Configuration/KeyboardShortcut.cs‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,8 @@ static KeyboardShortcut()
3535
/// </summary>
3636
public static readonly KeyboardShortcut Empty = new();
3737

38-
/// <summary>
39-
/// All KeyCode values that can be used in a keyboard shortcut.
40-
/// </summary>
41-
public static readonly IEnumerable<KeyCode> AllKeyCodes = (KeyCode[]) Enum.GetValues(typeof(KeyCode));
42-
4338
// Don't block hotkeys if mouse is being pressed, e.g. when shooting and trying to strafe
44-
private static readonly KeyCode[] _modifierBlockKeyCodes = AllKeyCodes.Except(new[]
45-
{
46-
KeyCode.Mouse0, KeyCode.Mouse1, KeyCode.Mouse2, KeyCode.Mouse3, KeyCode.Mouse4, KeyCode.Mouse5,
47-
KeyCode.Mouse6, KeyCode.None
48-
}).ToArray();
39+
private static KeyCode[] modifierBlockKeyCodes;
4940

5041
private readonly KeyCode[] _allKeys;
5142

@@ -122,7 +113,7 @@ public bool IsDown()
122113
var mainKey = MainKey;
123114
if (mainKey == KeyCode.None) return false;
124115

125-
return Input.GetKeyDown(mainKey) && ModifierKeyTest();
116+
return UnityInput.Current.GetKeyDown(mainKey) && ModifierKeyTest();
126117
}
127118

128119
/// <summary>
@@ -133,7 +124,7 @@ public bool IsPressed()
133124
var mainKey = MainKey;
134125
if (mainKey == KeyCode.None) return false;
135126

136-
return Input.GetKey(mainKey) && ModifierKeyTest();
127+
return UnityInput.Current.GetKey(mainKey) && ModifierKeyTest();
137128
}
138129

139130
/// <summary>
@@ -144,18 +135,23 @@ public bool IsUp()
144135
var mainKey = MainKey;
145136
if (mainKey == KeyCode.None) return false;
146137

147-
return Input.GetKeyUp(mainKey) && ModifierKeyTest();
138+
return UnityInput.Current.GetKeyUp(mainKey) && ModifierKeyTest();
148139
}
149140

150141
private bool ModifierKeyTest()
151142
{
152143
var allKeys = _allKeys;
153144
var mainKey = MainKey;
154145

155-
var allModifiersPressed = allKeys.All(c => c == mainKey || Input.GetKey(c));
146+
var allModifiersPressed = allKeys.All(c => c == mainKey || UnityInput.Current.GetKey(c));
156147
if (!allModifiersPressed) return false;
157148

158-
var noOtherModifiersPressed = _modifierBlockKeyCodes.All(c => !Input.GetKey(c) || allKeys.Contains(c));
149+
// Lazy init to make sure the game is initialized, and we're in main thread before calling UnityInput.Current
150+
modifierBlockKeyCodes ??= UnityInput.Current.SupportedKeyCodes.Except([KeyCode.Mouse0, KeyCode.Mouse1,
151+
KeyCode.Mouse2, KeyCode.Mouse3, KeyCode.Mouse4, KeyCode.Mouse5, KeyCode.Mouse6, KeyCode.None
152+
]).ToArray();
153+
154+
bool noOtherModifiersPressed = modifierBlockKeyCodes.All(c => !UnityInput.Current.GetKey(c) || allKeys.Contains(c));
159155
return noOtherModifiersPressed;
160156
}
161157

0 commit comments

Comments
 (0)