Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extra-natives): add a way to disable raw keys #3147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 61 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,81 @@ 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)
template<bool HandleDisabled = true, auto fn>
static void IsRawKeyWrapper(fx::ScriptContext& context)
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);

if (!IsRawKeyInvalidOrDisabled<HandleDisabled>(rawKeyIndex))
{
auto rawKeyIndex = context.GetArgument<uint32_t>(0);
context.SetResult<bool>(fn(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);
}
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", IsRawKeyWrapper<true, &ioKeyboard_KeyPressed>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_RELEASED", IsRawKeyWrapper<true, &ioKeyboard_KeyReleased>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_DOWN", IsRawKeyWrapper<true, &ioKeyboard_KeyDown>);
fx::ScriptEngine::RegisterNativeHandler("IS_RAW_KEY_UP", IsRawKeyWrapper<true, &ioKeyboard_KeyUp>);

fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_PRESSED", IsRawKeyWrapper<false, &ioKeyboard_KeyPressed>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_RELEASED", IsRawKeyWrapper<false, &ioKeyboard_KeyReleased>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_DOWN", IsRawKeyWrapper<false, &ioKeyboard_KeyDown>);
fx::ScriptEngine::RegisterNativeHandler("IS_DISABLED_RAW_KEY_UP", IsRawKeyWrapper<false, &ioKeyboard_KeyUp>);

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
```
26 changes: 26 additions & 0 deletions ext/native-decls/IsDisabledRawKeyReleased.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
ns: CFX
apiset: client
---
## IS_DISABLED_RAW_KEY_RELEASED

```c
BOOL IS_DISABLED_RAW_KEY_RELEASED(int rawKeyIndex);
```

Gets if the specified `rawKeyIndex` was released, 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 released state.

## Examples
```lua
if IsDisabledRawKeyReleased(32) then -- KEY_SPACE
print("Spacebar released")
end
```
26 changes: 26 additions & 0 deletions ext/native-decls/IsDisabledRawKeyUp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
ns: CFX
apiset: client
---
## IS_DISABLED_RAW_KEY_UP

```c
BOOL IS_DISABLED_RAW_KEY_UP(int rawKeyIndex);
```

Gets if the specified `rawKeyIndex` is up, 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 up state.

## Examples
```lua
if IsDisabledRawKeyUp(32) then -- KEY_SPACE
print("Spacebar is up")
end
```
4 changes: 3 additions & 1 deletion ext/native-decls/IsRawKeyDown.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ apiset: client
BOOL IS_RAW_KEY_DOWN(int rawKeyIndex);
```

Can be used to get state of raw key on keyboard.
Gets if the specified `rawKeyIndex` is pressed down on the keyboard.

This will not be triggered 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)

Expand Down
Loading
Loading