Skip to content

Commit 1b21f88

Browse files
committed
Prevent unnecessary GetComponent calls and make naming clearer
1 parent cb7e3fe commit 1b21f88

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

BepInEx.InputHotkeyBlock/InputHotkeyBlock.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,43 @@ internal void Main()
2828
Harmony.CreateAndPatchAll(typeof(Hooks));
2929
}
3030

31+
private static GameObject previousSelectedGameObject;
32+
private static bool previousState;
33+
3134
/// <summary>
3235
/// Check if an input field is selected
3336
/// </summary>
34-
private static bool HotkeyBlock()
37+
private static bool AllowInput()
3538
{
3639
//UI elements from some mods
3740
if (GUIUtility.keyboardControl > 0)
3841
return false;
3942

40-
if (EventSystem.current?.currentSelectedGameObject != null)
43+
var currentSelectedGameObject = EventSystem.current?.currentSelectedGameObject; // Checking with ? is fine here
44+
if (currentSelectedGameObject != null)
4145
{
46+
// Buffer results to prevent unnecessary GetComponent calls
47+
if (currentSelectedGameObject == previousSelectedGameObject)
48+
return previousState;
49+
50+
previousSelectedGameObject = currentSelectedGameObject;
51+
previousState = false;
52+
4253
//TextMeshPro InputField
4354
if (TMPInputFieldType != null)
44-
if (EventSystem.current.currentSelectedGameObject.GetComponent(TMPInputFieldType) != null)
55+
if (currentSelectedGameObject.GetComponent(TMPInputFieldType) != null)
4556
return false;
4657

4758
//Other InputFields
48-
if (EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null)
59+
if (currentSelectedGameObject.GetComponent<InputField>() != null)
4960
return false;
5061
}
62+
else
63+
{
64+
previousSelectedGameObject = null;
65+
}
66+
67+
previousState = true;
5168
return true;
5269
}
5370

@@ -57,17 +74,17 @@ private static bool HotkeyBlock()
5774
internal static partial class Hooks
5875
{
5976
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKey), typeof(KeyCode))]
60-
internal static bool GetKeyCode() => HotkeyBlock();
77+
internal static bool GetKeyCode() => AllowInput();
6178
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKey), typeof(string))]
62-
internal static bool GetKeyString() => HotkeyBlock();
79+
internal static bool GetKeyString() => AllowInput();
6380
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyDown), typeof(KeyCode))]
64-
internal static bool GetKeyDownCode() => HotkeyBlock();
81+
internal static bool GetKeyDownCode() => AllowInput();
6582
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyDown), typeof(string))]
66-
internal static bool GetKeyDownString() => HotkeyBlock();
83+
internal static bool GetKeyDownString() => AllowInput();
6784
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyUp), typeof(KeyCode))]
68-
internal static bool GetKeyUpCode() => HotkeyBlock();
85+
internal static bool GetKeyUpCode() => AllowInput();
6986
[HarmonyPrefix, HarmonyPatch(typeof(Input), nameof(Input.GetKeyUp), typeof(string))]
70-
internal static bool GetKeyUpString() => HotkeyBlock();
87+
internal static bool GetKeyUpString() => AllowInput();
7188
}
7289
}
7390
}

0 commit comments

Comments
 (0)