From a737dd16e428efd55dcc41945e6f982c910d4b82 Mon Sep 17 00:00:00 2001 From: INeedBots Date: Sat, 2 Jan 2021 13:42:52 -0600 Subject: [PATCH] [XInput] Got lJoystick working --- src/Components/Modules/Script.cpp | 2 -- src/Components/Modules/XInput.cpp | 48 +++++++++++++++++++++++++------ src/Components/Modules/XInput.hpp | 1 + 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 51f0e394..3cd8f3d3 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -352,11 +352,9 @@ namespace Components { // execute our hook pushad - pusha call Script::StoreScriptBaseProgramNum - popa popad // execute overwritten code caused by the jump hook diff --git a/src/Components/Modules/XInput.cpp b/src/Components/Modules/XInput.cpp index 4feb3520..9b31b0bf 100644 --- a/src/Components/Modules/XInput.cpp +++ b/src/Components/Modules/XInput.cpp @@ -3,12 +3,16 @@ namespace Components { XINPUT_STATE XInput::xiStates[XUSER_MAX_COUNT]; + int XInput::xiPlayerNum = -1; void XInput::PollXInputDevices() { - for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) + XInput::xiPlayerNum = -1; + + for (int i = XUSER_MAX_COUNT; i >= 0; i--) { - XInputGetState(i, &xiStates[i]); + if (XInputGetState(i, &xiStates[i]) == ERROR_SUCCESS) + XInput::xiPlayerNum = i; } } @@ -17,14 +21,8 @@ namespace Components __asm { // poll the xinput devices on every client frame - pusha - pushad - call XInput::PollXInputDevices - popad - popa - // execute the code we patched over sub esp, 0Ch push ebx @@ -37,8 +35,42 @@ namespace Components } } + void XInput::CL_GamepadMove(int localClientNum, Game::usercmd_s* cmd) + { + if (XInput::xiPlayerNum != -1) + { + XINPUT_STATE* xiState = &xiStates[xiPlayerNum]; + + cmd->rightmove = xiState->Gamepad.sThumbLX / 256; + cmd->forwardmove = xiState->Gamepad.sThumbLY / 256; + } + } + + __declspec(naked) void XInput::CL_CreateCmdStub() + { + __asm + { + // do xinput! + push esi + push ebp + call XInput::CL_GamepadMove + add esp, 8h + + // execute code we patched over + add esp, 4 + fld st + pop ebx + + // return back + push 0x5A6DBF + retn + } + } + XInput::XInput() { Utils::Hook(0x486970, XInput::CL_FrameStub, HOOK_JUMP).install()->quick(); + + Utils::Hook(0x5A6DB9, XInput::CL_CreateCmdStub, HOOK_JUMP).install()->quick(); } } diff --git a/src/Components/Modules/XInput.hpp b/src/Components/Modules/XInput.hpp index 21670867..b2c1ab1e 100644 --- a/src/Components/Modules/XInput.hpp +++ b/src/Components/Modules/XInput.hpp @@ -9,6 +9,7 @@ namespace Components private: static XINPUT_STATE xiStates[XUSER_MAX_COUNT]; + static int xiPlayerNum; static void CL_FrameStub(); static void PollXInputDevices();