From 6cae803f7ca1440d3276765275e74beaf7f132a0 Mon Sep 17 00:00:00 2001 From: evanlin96069 <72735402+evanlin96069@users.noreply.github.com> Date: Sun, 24 Nov 2024 20:34:16 -0500 Subject: [PATCH] Make DecodeUserCmdFromBuffer a signal --- spt/features/aim.cpp | 3 +- spt/features/ihud.cpp | 73 +++++++++------------------------------ spt/features/playerio.cpp | 67 ++++++++++++++++++++++------------- spt/features/playerio.hpp | 17 ++------- spt/utils/signals.cpp | 2 +- spt/utils/signals.hpp | 1 + 6 files changed, 67 insertions(+), 96 deletions(-) diff --git a/spt/features/aim.cpp b/spt/features/aim.cpp index d84a61b48..12edbc6ef 100644 --- a/spt/features/aim.cpp +++ b/spt/features/aim.cpp @@ -5,6 +5,7 @@ #include "ent_utils.hpp" #include "math.hpp" #include "playerio.hpp" +#include "signals.hpp" #undef min #undef max @@ -330,7 +331,7 @@ CON_COMMAND(_y_spt_setangle, void AimFeature::LoadFeature() { - if (spt_generic.ORIG_ControllerMove && spt_playerio.ORIG_CreateMove) + if (spt_generic.ORIG_ControllerMove && CreateMoveSignal.Works) { InitCommand(tas_aim_reset); InitCommand(tas_aim); diff --git a/spt/features/ihud.cpp b/spt/features/ihud.cpp index f6f4f3847..7813703c7 100644 --- a/spt/features/ihud.cpp +++ b/spt/features/ihud.cpp @@ -70,13 +70,10 @@ class InputHud : public FeatureWrapper virtual void LoadFeature() override; - virtual void PreHook() override; - virtual void UnloadFeature() override; private: - DECL_HOOK_THISCALL(void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number); - void CreateMove(uintptr_t pCmd); + void SetData(uintptr_t pCmd); void DrawRectAndCenterTxt(Color buttonColor, int x0, int y0, @@ -101,8 +98,6 @@ class InputHud : public FeatureWrapper int buttonBits = 0; bool awaitingFrameDraw = false; - - bool loadingSuccessful = false; }; InputHud spt_ihud; @@ -564,22 +559,7 @@ CON_COMMAND(y_spt_ihud_add_key, "Add custom key to ihud.") } #endif -namespace patterns -{ - PATTERNS( - DecodeUserCmdFromBuffer, - "5135", - "83 EC 54 33 C0 D9 EE 89 44 24 ?? D9 54 24 ?? 89 44 24 ??", - "7197370", - "55 8B EC 83 EC 54 56 8B F1 C7 45 ?? ?? ?? ?? ?? 8D 4D ?? C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 E8 ?? ?? ?? ?? 8B 4D ??", - "4044", - "83 EC 54 53 57 8D 44 24 ?? 50 8B 44 24 ?? 99"); -} - -void InputHud::InitHooks() -{ - HOOK_FUNCTION(client, DecodeUserCmdFromBuffer); -} +void InputHud::InitHooks() {} bool InputHud::ShouldLoadFeature() { @@ -588,10 +568,11 @@ bool InputHud::ShouldLoadFeature() void InputHud::LoadFeature() { - if (!loadingSuccessful) + if (!(CreateMoveSignal.Works && DecodeUserCmdFromBufferSignal.Works)) return; - if (CreateMoveSignal.Works) - CreateMoveSignal.Connect(this, &InputHud::CreateMove); + + CreateMoveSignal.Connect(this, &InputHud::SetData); + DecodeUserCmdFromBufferSignal.Connect(this, &InputHud::SetData); bool result = spt_hud_feat.AddHudDefaultGroup(HudCallback( std::bind(&InputHud::DrawInputHud, this), []() { return y_spt_ihud.GetBool(); }, false)); @@ -664,27 +645,8 @@ void InputHud::LoadFeature() anglesSetting = {true, false, L"", font, 4, 1, 0, 0, background, highlight, textcolor, texthighlight, 0}; } -void InputHud::PreHook() -{ - loadingSuccessful = !!(ORIG_DecodeUserCmdFromBuffer); -} - void InputHud::UnloadFeature() {} -void InputHud::SetInputInfo(int button, Vector movement) -{ - if (awaitingFrameDraw) - { - buttonBits |= button; - } - else - { - buttonBits = button; - } - inputMovement = movement; - awaitingFrameDraw = true; -} - bool InputHud::ModifySetting(const char* element, const char* param, const char* value) { InputHud::Button* target; @@ -1057,21 +1019,20 @@ void InputHud::DrawButton(Button button) button.text.c_str()); } -IMPL_HOOK_THISCALL(InputHud, void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number) +void InputHud::SetData(uintptr_t pCmd) { - spt_ihud.ORIG_DecodeUserCmdFromBuffer(thisptr, buf, sequence_number); - - auto m_pCommands = - *reinterpret_cast(reinterpret_cast(thisptr) + spt_playerio.offM_pCommands); - auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90); auto cmd = reinterpret_cast(pCmd); - spt_ihud.SetInputInfo(cmd->buttons, Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove)); -} -void InputHud::CreateMove(uintptr_t pCmd) -{ - auto cmd = reinterpret_cast(pCmd); - spt_ihud.SetInputInfo(cmd->buttons, Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove)); + if (awaitingFrameDraw) + { + buttonBits |= cmd->buttons; + } + else + { + buttonBits = cmd->buttons; + } + inputMovement = Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove); + awaitingFrameDraw = true; } std::string InputHud::Button::ToString() const diff --git a/spt/features/playerio.cpp b/spt/features/playerio.cpp index f5974a0d1..113e1cdf3 100644 --- a/spt/features/playerio.cpp +++ b/spt/features/playerio.cpp @@ -97,6 +97,14 @@ namespace patterns "8B 44 24 ?? 83 EC 1C 53 55 56 57", "dmomm", "83 EC 10 53 8B 5C 24 ?? 55 56 8B F1"); + PATTERNS( + DecodeUserCmdFromBuffer, + "5135", + "83 EC 54 33 C0 D9 EE 89 44 24 ?? D9 54 24 ?? 89 44 24 ??", + "7197370", + "55 8B EC 83 EC 54 56 8B F1 C7 45 ?? ?? ?? ?? ?? 8D 4D ?? C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 E8 ?? ?? ?? ?? 8B 4D ??", + "4044", + "83 EC 54 53 57 8D 44 24 ?? 50 8B 44 24 ?? 99"); PATTERNS( GetGroundEntity, "5135", @@ -114,6 +122,7 @@ namespace patterns void PlayerIOFeature::InitHooks() { HOOK_FUNCTION(client, CreateMove); + HOOK_FUNCTION(client, DecodeUserCmdFromBuffer); HOOK_FUNCTION(client, GetButtonBits); FIND_PATTERN(client, GetGroundEntity); } @@ -171,6 +180,11 @@ void PlayerIOFeature::PreHook() offSidemove = 28; } + if (ORIG_DecodeUserCmdFromBuffer) + { + DecodeUserCmdFromBufferSignal.Works = true; + } + GetPlayerFields(); } @@ -284,38 +298,48 @@ Strafe::MovementVars PlayerIOFeature::GetMovementVars() return vars; } -void __fastcall PlayerIOFeature::HOOKED_CreateMove_Func(void* thisptr, - int edx, - int sequence_number, - float input_sample_frametime, - bool active) +IMPL_HOOK_THISCALL(PlayerIOFeature, + void, + CreateMove, + void*, + int sequence_number, + float input_sample_frametime, + bool active) { - auto m_pCommands = *reinterpret_cast(reinterpret_cast(thisptr) + offM_pCommands); - pCmd = m_pCommands + sizeofCUserCmd * (sequence_number % 90); + auto m_pCommands = + *reinterpret_cast(reinterpret_cast(thisptr) + spt_playerio.offM_pCommands); + auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90); + spt_playerio.pCmd = pCmd; - ORIG_CreateMove(thisptr, edx, sequence_number, input_sample_frametime, active); + spt_playerio.ORIG_CreateMove(thisptr, sequence_number, input_sample_frametime, active); CreateMoveSignal(pCmd); - pCmd = 0; + spt_playerio.pCmd = 0; } -void __fastcall PlayerIOFeature::HOOKED_CreateMove(void* thisptr, - int edx, - int sequence_number, - float input_sample_frametime, - bool active) +IMPL_HOOK_THISCALL(PlayerIOFeature, void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number) { - spt_playerio.HOOKED_CreateMove_Func(thisptr, edx, sequence_number, input_sample_frametime, active); + spt_playerio.ORIG_DecodeUserCmdFromBuffer(thisptr, buf, sequence_number); + + auto m_pCommands = + *reinterpret_cast(reinterpret_cast(thisptr) + spt_playerio.offM_pCommands); + auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90); + + DecodeUserCmdFromBufferSignal(pCmd); } -int __fastcall PlayerIOFeature::HOOKED_GetButtonBits_Func(void* thisptr, int edx, int bResetState) +IMPL_HOOK_THISCALL(PlayerIOFeature, int, GetButtonBits, void*, int bResetState) { - int rv = ORIG_GetButtonBits(thisptr, edx, bResetState); + int rv = spt_playerio.ORIG_GetButtonBits(thisptr, bResetState); if (bResetState == 1) { static int keyPressed = 0; + int spamButtons = spt_playerio.spamButtons; + bool& forceJump = spt_playerio.forceJump; + bool& forceUnduck = spt_playerio.forceUnduck; + keyPressed = (keyPressed ^ spamButtons) & spamButtons; rv |= keyPressed; @@ -335,11 +359,6 @@ int __fastcall PlayerIOFeature::HOOKED_GetButtonBits_Func(void* thisptr, int edx return rv; } -int __fastcall PlayerIOFeature::HOOKED_GetButtonBits(void* thisptr, int edx, int bResetState) -{ - return spt_playerio.HOOKED_GetButtonBits_Func(thisptr, edx, bResetState); -} - bool PlayerIOFeature::GetFlagsDucking() { return m_fFlags.GetValue() & FL_DUCKING; @@ -354,7 +373,7 @@ Strafe::PlayerData PlayerIOFeature::GetPlayerData() const int IN_DUCK = 1 << 2; data.Ducking = GetFlagsDucking(); - data.DuckPressed = (ORIG_GetButtonBits(cinput_thisptr, 0, 0) & IN_DUCK); + data.DuckPressed = (ORIG_GetButtonBits(cinput_thisptr, 0) & IN_DUCK); data.UnduckedOrigin = m_vecAbsOrigin.GetValue(); data.Velocity = GetPlayerVelocity(); data.Basevelocity = Vector(); @@ -454,7 +473,7 @@ bool PlayerIOFeature::IsGroundEntitySet() bool PlayerIOFeature::TryJump() { const int IN_JUMP = (1 << 1); - return ORIG_GetButtonBits(cinput_thisptr, 0, 0) & IN_JUMP; + return ORIG_GetButtonBits(cinput_thisptr, 0) & IN_JUMP; } bool PlayerIOFeature::PlayerIOAddressesFound() diff --git a/spt/features/playerio.hpp b/spt/features/playerio.hpp index 11d48b80d..a7d387d7f 100644 --- a/spt/features/playerio.hpp +++ b/spt/features/playerio.hpp @@ -23,18 +23,9 @@ typedef void*(__cdecl* _GetLocalPlayer)(); class PlayerIOFeature : public FeatureWrapper { private: - void __fastcall HOOKED_CreateMove_Func(void* thisptr, - int edx, - int sequence_number, - float input_sample_frametime, - bool active); - static void __fastcall HOOKED_CreateMove(void* thisptr, - int edx, - int sequence_number, - float input_sample_frametime, - bool active); - int __fastcall HOOKED_GetButtonBits_Func(void* thisptr, int edx, int bResetState); - static int __fastcall HOOKED_GetButtonBits(void* thisptr, int edx, int bResetState); + DECL_HOOK_THISCALL(void, CreateMove, void*, int sequence_number, float input_sample_frametime, bool active); + DECL_HOOK_THISCALL(void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number); + DECL_HOOK_THISCALL(int, GetButtonBits, void*, int bResetState); public: virtual bool ShouldLoadFeature() override; @@ -60,10 +51,8 @@ class PlayerIOFeature : public FeatureWrapper int spamButtons = 0; ptrdiff_t offServerAbsOrigin = 0; uintptr_t pCmd = 0; - _CreateMove ORIG_CreateMove = nullptr; _GetGroundEntity ORIG_GetGroundEntity = nullptr; - _GetButtonBits ORIG_GetButtonBits = nullptr; Vector currentVelocity; Vector previousVelocity; diff --git a/spt/utils/signals.cpp b/spt/utils/signals.cpp index fa4b5988e..4fa7962c5 100644 --- a/spt/utils/signals.cpp +++ b/spt/utils/signals.cpp @@ -10,6 +10,7 @@ Gallant::Signal1 OngroundSignal; Gallant::Signal2 SetPausedSignal; Gallant::Signal1 SV_ActivateServerSignal; Gallant::Signal1 CreateMoveSignal; +Gallant::Signal1 DecodeUserCmdFromBufferSignal; Gallant::Signal0 VagCrashSignal; Gallant::Signal0 DemoStartPlaybackSignal; Gallant::Signal1 SV_FrameSignal; @@ -18,7 +19,6 @@ Gallant::Signal2 ProcessMovementPre_Signal; Gallant::Signal2 RenderViewPre_Signal; Gallant::Signal2 SetSignonStateSignal; - // Plugin callbacks Gallant::Signal0 TickSignal; Gallant::Signal1 LevelInitSignal; diff --git a/spt/utils/signals.hpp b/spt/utils/signals.hpp index 6a99f1794..744757990 100644 --- a/spt/utils/signals.hpp +++ b/spt/utils/signals.hpp @@ -14,6 +14,7 @@ extern Gallant::Signal1 OngroundSignal; extern Gallant::Signal2 SetPausedSignal; extern Gallant::Signal1 SV_ActivateServerSignal; extern Gallant::Signal1 CreateMoveSignal; +extern Gallant::Signal1 DecodeUserCmdFromBufferSignal; extern Gallant::Signal0 VagCrashSignal; extern Gallant::Signal0 DemoStartPlaybackSignal; extern Gallant::Signal1 SV_FrameSignal;