Skip to content

Commit

Permalink
feat(extra-natives): add a way to disable raw keys
Browse files Browse the repository at this point in the history
This also merges the two implementations into one file since the only
thing that differs between them is the hooking.
  • Loading branch information
AvarianKnight committed Feb 10, 2025
1 parent 0ec3c8f commit 2ca92f1
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 146 deletions.
149 changes: 107 additions & 42 deletions code/components/extra-natives-five/src/InputNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Hooking.h>
#include <ScriptEngine.h>

#include "nutsnbolts.h"

constexpr int KEYS_COUNT = 256;

using keysData = unsigned char[2][KEYS_COUNT];
Expand Down Expand Up @@ -36,64 +38,127 @@ inline int ioKeyboard_KeyReleased(int key)
return (*ioKeyboardKeys)[*ioKeyboardActive ^ 1][key] & ioKeyboard_KeyChanged(key);
}

static HookFunction initFunction([]()
{
ioKeyboardActive = hook::get_address<int*>(hook::get_pattern("8B 2D ? ? ? ? 48 8B 03"), 2, 6);
ioKeyboardKeys = hook::get_address<keysData*>(hook::get_pattern("48 8D 2D ? ? ? ? 49 C1 E6"), 3, 7);
// List of raw keys to count as disabled until the next frame
static std::set<int> disabledKeys{};

fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_PRESSED", [](fx::ScriptContext& context)
template<bool HandleDisabled = true>
static bool IsRawKeyInvalidOrDisabled(int key)
{
if constexpr (HandleDisabled)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (rawKeyIndex >= 0 && rawKeyIndex < KEYS_COUNT)
{
context.SetResult<bool>(ioKeyboard_KeyPressed(rawKeyIndex) != 0);
}
else
if (disabledKeys.find(key) != disabledKeys.end())
{
context.SetResult<bool>(false);
// the key should be disabled
return true;
}
});
}

fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_RELEASED", [](fx::ScriptContext& context)
if (key >= 0 && key < KEYS_COUNT)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);
// the keys valid, we shouldn't disable it
return false;
}

if (rawKeyIndex >= 0 && rawKeyIndex < KEYS_COUNT)
{
context.SetResult<bool>(ioKeyboard_KeyReleased(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
});
// :( out of bounds
return true;
}

fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_DOWN", [](fx::ScriptContext& context)
// TODO: This can probably be simplified, but I'm no c++ expert
template<bool HandleDisabled = true>
static void IsRawKeyPressed(fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (!IsRawKeyInvalidOrDisabled<HandleDisabled>(rawKeyIndex))
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);
context.SetResult<bool>(ioKeyboard_KeyPressed(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
}

if (rawKeyIndex >= 0 && rawKeyIndex < KEYS_COUNT)
{
context.SetResult<bool>(ioKeyboard_KeyDown(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
template<bool HandleDisabled = true>
static void IsRawKeyReleased(fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (!IsRawKeyInvalidOrDisabled<HandleDisabled>(rawKeyIndex))
{
context.SetResult<bool>(ioKeyboard_KeyReleased(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
}

template<bool HandleDisabled = true>
static void IsRawKeyDown(fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (!IsRawKeyInvalidOrDisabled<HandleDisabled>(rawKeyIndex))
{
context.SetResult<bool>(ioKeyboard_KeyDown(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
}

template<bool HandleDisabled = true>
static void IsRawKeyUp(fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (!IsRawKeyInvalidOrDisabled<HandleDisabled>(rawKeyIndex))
{
context.SetResult<bool>(ioKeyboard_KeyUp(rawKeyIndex) != 0);
}
else
{
context.SetResult<bool>(false);
}
}

static HookFunction initFunction([]()
{
#ifdef IS_RDR3
uint8_t* location = hook::get_pattern<uint8_t>("48 63 05 ? ? ? ? 4C 8D 35 ? ? ? ? 48 83 F0 ? B9");
ioKeyboardActive = hook::get_address<int*>(location, 3, 7);
ioKeyboardKeys = hook::get_address<keysData*>(location + 7, 3, 7);
#else
ioKeyboardActive = hook::get_address<int*>(hook::get_pattern("8B 2D ? ? ? ? 48 8B 03"), 2, 6);
ioKeyboardKeys = hook::get_address<keysData*>(hook::get_pattern("48 8D 2D ? ? ? ? 49 C1 E6"), 3, 7);
#endif

// reset the disabled keys every frame
OnGameFrame.Connect([]
{
disabledKeys.clear();
});

fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_UP", [](fx::ScriptContext& context)
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_PRESSED", IsRawKeyPressed<true>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_RELEASED", IsRawKeyReleased<true>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_DOWN", IsRawKeyDown<true>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_UP", IsRawKeyUp<true>);

fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_PRESSED", IsRawKeyPressed<false>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_RELEASED", IsRawKeyReleased<false>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_DOWN", IsRawKeyDown<false>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_UP", IsRawKeyUp<false>);

fx::ScriptEngine::RegisterNativeHandler("DISABLE_RAW_KEY_THIS_FRAME", [](fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (rawKeyIndex >= 0 && rawKeyIndex < KEYS_COUNT)
{
context.SetResult<bool>(ioKeyboard_KeyUp(rawKeyIndex) != 0);
}
else
// We only want the bounds check here, we don't care if its already disabled
if (!IsRawKeyInvalidOrDisabled<false>(rawKeyIndex))
{
context.SetResult<bool>(false);
disabledKeys.insert(rawKeyIndex);
}
});
});
1 change: 1 addition & 0 deletions code/components/extra-natives-rdr3/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ return function()
'components/extra-natives-five/src/PoolTraversalNatives.cpp',
'components/extra-natives-five/src/RadioDSP.cpp',
'components/extra-natives-five/src/NuiAudioSink.cpp',
'components/extra-natives-five/src/InputNatives.cpp',
'components/gta-core-five/include/GameAudioState.h',
'components/extra-natives-five/include/audDspEffect.h',
}
Expand Down
100 changes: 0 additions & 100 deletions code/components/extra-natives-rdr3/src/InputNatives.cpp

This file was deleted.

33 changes: 33 additions & 0 deletions ext/native-decls/DisableRawKeyThisFrame.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
ns: CFX
apiset: client
---
## DISABLE_RAW_KEY_THIS_FRAME

```c
BOOL DISABLE_RAW_KEY_THIS_FRAME(int rawKeyIndex);
```
Disables the specified `rawKeyIndex`, making it not trigger the regular `IS_RAW_KEY_*` natives.
Virtual key codes can be found [here](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)
## Parameters
* **rawKeyIndex**: Index of raw key from keyboard.
## Return value
Returns bool value of down state.
## Examples
```lua
local KEY_SPACE = 32
DisableRawKeyThisFrame(KEY_SPACE)
-- This will not get triggered this frame
if IsRawKeyDown(KEY_SPACE) then
print("unreachable :(")
end
-- this will get triggered
if IsDisabledRawKeyDown(KEY_SPACE) then
print("Spacebar is down")
end
```
26 changes: 26 additions & 0 deletions ext/native-decls/IsDisabledRawKeyDown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
ns: CFX
apiset: client
---
## IS_DISABLED_RAW_KEY_DOWN

```c
BOOL IS_DISABLED_RAW_KEY_DOWN(int rawKeyIndex);
```
Gets if the specified `rawKeyIndex` is pressed down, even if the key is disabled with [DISABLE_RAW_KEY_THIS_FRAME](#_0x8BCF0014).
Virtual key codes can be found [here](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)
## Parameters
* **rawKeyIndex**: Index of raw key from keyboard.
## Return value
Returns bool value of down state.
## Examples
```lua
if IsDisabledRawKeyDown(32) then -- KEY_SPACE
print("Spacebar is down")
end
```
26 changes: 26 additions & 0 deletions ext/native-decls/IsDisabledRawKeyPressed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
ns: CFX
apiset: client
---
## IS_DISABLED_RAW_KEY_PRESSED

```c
BOOL IS_DISABLED_RAW_KEY_PRESSED(int rawKeyIndex);
```
Gets if the specified `rawKeyIndex` is pressed, even if the key is disabled with [DISABLE_RAW_KEY_THIS_FRAME](#_0x8BCF0014).
Virtual key codes can be found [here](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)
## Parameters
* **rawKeyIndex**: Index of raw key from keyboard.
## Return value
Returns bool value of pressed state.
## Examples
```lua
if IsDisabledRawKeyPressed(32) then -- KEY_SPACE
print("Spacebar pressed")
end
```
Loading

0 comments on commit 2ca92f1

Please sign in to comment.