diff --git a/src/Components/Modules/Events.cpp b/src/Components/Modules/Events.cpp index 41ce5ded..4122b318 100644 --- a/src/Components/Modules/Events.cpp +++ b/src/Components/Modules/Events.cpp @@ -12,6 +12,9 @@ namespace Components Utils::Concurrency::Container Events::DvarInitTasks_; Utils::Concurrency::Container Events::NetworkInitTasks_; + Events::ClientCmdButtonsCallback Events::ClientCmdButtonsTasks_; + Events::ClientCmdButtonsCallback Events::ClientKeyMoveTasks_; + void Events::OnClientDisconnect(const std::function& callback) { ClientDisconnectTasks_.access([&callback](ClientCallback& tasks) @@ -52,6 +55,16 @@ namespace Components }); } + void Events::OnClientCmdButtons(const std::function& callback) + { + ClientCmdButtonsTasks_.emplace_back(callback); + } + + void Events::OnClientKeyMove(const std::function& callback) + { + ClientKeyMoveTasks_.emplace_back(callback); + } + void Events::OnSVInit(const std::function& callback) { ServerInitTasks_.access([&callback](Callback& tasks) @@ -147,6 +160,60 @@ namespace Components Utils::Hook::Call(0x404CA0)(); // CL_InitOnceForAllClients } + void Events::CL_CmdButtons(Game::usercmd_s* cmd) + { + for (const auto& func : ClientCmdButtonsTasks_) + { + func(cmd); + } + } + + void __declspec(naked) Events::CL_CmdButtons_Stub() + { + static const DWORD CL_CmdButtons_t = 0x5A6510; + + __asm + { + call CL_CmdButtons_t + + pushad + + push esi + call CL_CmdButtons + add esp, 0x4 + + popad + ret + } + } + + void Events::CL_KeyMove(Game::usercmd_s* cmd) + { + for (const auto& func : ClientKeyMoveTasks_) + { + func(cmd); + } + } + + void __declspec(naked) Events::CL_KeyMove_Stub() + { + static const DWORD CL_KeyMove_t = 0x5A5F40; + + __asm + { + call CL_KeyMove_t + + pushad + + push esi + call CL_KeyMove + add esp, 0x4 + + popad + ret + } + } + void Events::SV_Init_Hk() { ServerInitTasks_.access([](Callback& tasks) @@ -213,6 +280,10 @@ namespace Components Utils::Hook(0x60BE5B, CL_InitOnceForAllClients_HK, HOOK_CALL).install()->quick(); // Com_Init_Try_Block_Function + Utils::Hook(0x5A6D84, CL_CmdButtons_Stub, HOOK_CALL).install()->quick(); + + Utils::Hook(0x5A6D8B, CL_KeyMove_Stub, HOOK_CALL).install()->quick(); + Utils::Hook(0x60BB3A, Com_InitDvars_Hk, HOOK_CALL).install()->quick(); // Com_Init_Try_Block_Function Utils::Hook(0x4D3665, SV_Init_Hk, HOOK_CALL).install()->quick(); // SV_Init diff --git a/src/Components/Modules/Events.hpp b/src/Components/Modules/Events.hpp index 1803cc96..0c02763c 100644 --- a/src/Components/Modules/Events.hpp +++ b/src/Components/Modules/Events.hpp @@ -8,6 +8,7 @@ namespace Components using Callback = std::vector>; using ClientConnectCallback = std::vector>; using ClientCallback = std::vector>; + using ClientCmdButtonsCallback = std::vector>; Events(); @@ -24,12 +25,17 @@ namespace Components static void OnClientInit(const std::function& callback); + static void OnClientCmdButtons(const std::function& callback); + + static void OnClientKeyMove(const std::function& callback); + // Client & Server (triggered once) static void OnSVInit(const std::function& callback); // Client & Server (triggered once) static void OnDvarInit(const std::function& callback); + // Client & Server (triggered once) static void OnNetworkInit(const std::function& callback); private: @@ -42,11 +48,21 @@ namespace Components static Utils::Concurrency::Container DvarInitTasks_; static Utils::Concurrency::Container NetworkInitTasks_; + // For speed this one does not use concurrency container. Be careful + static ClientCmdButtonsCallback ClientCmdButtonsTasks_; + static ClientCmdButtonsCallback ClientKeyMoveTasks_; + static void ClientDisconnect_Hk(int clientNum); static void SV_UserinfoChanged_Hk(Game::client_s* cl); static void SteamDisconnect_Hk(); static void Scr_ShutdownSystem_Hk(unsigned char sys); static void CL_InitOnceForAllClients_HK(); + + static void CL_CmdButtons(Game::usercmd_s* cmd); + static void CL_CmdButtons_Stub(); + static void CL_KeyMove(Game::usercmd_s* cmd); + static void CL_KeyMove_Stub(); + static void SV_Init_Hk(); static void Com_InitDvars_Hk(); diff --git a/src/Components/Modules/Lean.cpp b/src/Components/Modules/Lean.cpp index 29b1403e..61415c86 100644 --- a/src/Components/Modules/Lean.cpp +++ b/src/Components/Modules/Lean.cpp @@ -1,4 +1,6 @@ #include + +#include "Events.hpp" #include "Lean.hpp" namespace Components @@ -28,7 +30,7 @@ namespace Components Game::IN_KeyDown(&in_leanright); } - void Lean::SetLeanFlags(Game::usercmd_s* cmd) + void Lean::ApplyLeanFlags(Game::usercmd_s* cmd) { if ((in_leanleft.active || in_leanleft.wasPressed) && BGLean.get()) { @@ -44,23 +46,6 @@ namespace Components in_leanright.wasPressed = false; } - void __declspec(naked) Lean::CL_CmdButtons_Stub() - { - __asm - { - // CL_CmdButtons - mov ecx, 5A6510h - call ecx - - pushad - push esi - call SetLeanFlags - pop esi - popad - retn - } - } - void Lean::PM_UpdateLean_Stub(Game::playerState_s* ps, float msec, Game::usercmd_s* cmd, void(*capsuleTrace)(Game::trace_t*, const float*, const float*, const Game::Bounds*, int, int)) { if (BGLean.get()) @@ -77,12 +62,11 @@ namespace Components Command::AddRaw("+leanright", IN_LeanRight_Down, true); Command::AddRaw("-leanright", IN_LeanRight_Up, true); - Utils::Hook(0x5A6D84, CL_CmdButtons_Stub, HOOK_CALL).install()->quick(); + Events::OnClientCmdButtons(ApplyLeanFlags); Utils::Hook(0x4A0C72, PM_UpdateLean_Stub, HOOK_CALL).install()->quick(); Utils::Hook(0x4A0D72, PM_UpdateLean_Stub, HOOK_CALL).install()->quick(); - BGLean = Dvar::Register("bg_lean", true, - Game::DVAR_CODINFO, "Enable CoD4 leaning"); + BGLean = Dvar::Register("bg_lean", true, Game::DVAR_CODINFO, "Enable CoD4 leaning"); } } diff --git a/src/Components/Modules/Lean.hpp b/src/Components/Modules/Lean.hpp index 183aaaa0..de6a0238 100644 --- a/src/Components/Modules/Lean.hpp +++ b/src/Components/Modules/Lean.hpp @@ -19,8 +19,7 @@ namespace Components static void IN_LeanRight_Up(); static void IN_LeanRight_Down(); - static void CL_CmdButtons_Stub(); - static void SetLeanFlags(Game::usercmd_s* cmd); + static void ApplyLeanFlags(Game::usercmd_s* cmd); static void PM_UpdateLean_Stub(Game::playerState_s* ps, float msec, Game::usercmd_s* cmd, void(*capsuleTrace)(Game::trace_t*, const float*, const float*, const Game::Bounds*, int, int)); }; diff --git a/src/Components/Modules/PlayerMovement.cpp b/src/Components/Modules/PlayerMovement.cpp index 010fd8a7..ca4fb9a3 100644 --- a/src/Components/Modules/PlayerMovement.cpp +++ b/src/Components/Modules/PlayerMovement.cpp @@ -7,11 +7,11 @@ namespace Components { - Dvar::Var PlayerMovement::BGRocketJump; - Dvar::Var PlayerMovement::BGRocketJumpScale; - Dvar::Var PlayerMovement::BGPlayerEjection; - Dvar::Var PlayerMovement::BGPlayerCollision; - Dvar::Var PlayerMovement::BGClimbAnything; + const Game::dvar_t* PlayerMovement::BGRocketJump; + const Game::dvar_t* PlayerMovement::BGRocketJumpScale; + const Game::dvar_t* PlayerMovement::BGPlayerEjection; + const Game::dvar_t* PlayerMovement::BGPlayerCollision; + const Game::dvar_t* PlayerMovement::BGClimbAnything; const Game::dvar_t* PlayerMovement::CGNoclipScaler; const Game::dvar_t* PlayerMovement::CGUfoScaler; const Game::dvar_t* PlayerMovement::PlayerSpectateSpeedScale; @@ -26,7 +26,7 @@ namespace Components { Game::PM_playerTrace(pm, results, start, end, bounds, passEntityNum, contentMask); - if (results && BGClimbAnything.get()) + if (results && BGClimbAnything->current.enabled) { results[0].surfaceFlags |= SURF_LADDER; } @@ -187,10 +187,9 @@ namespace Components { auto* result = Game::Weapon_RocketLauncher_Fire(ent, weaponIndex, spread, wp, gunVel, lockParms, magicBullet); - if (ent->client != nullptr && BGRocketJump.get() && - wp->weapDef->inventoryType != Game::WEAPINVENTORY_EXCLUSIVE) + if (ent->client && BGRocketJump->current.enabled && wp->weapDef->inventoryType != Game::WEAPINVENTORY_EXCLUSIVE) { - const auto scale = BGRocketJumpScale.get(); + const auto scale = BGRocketJumpScale->current.value; ent->client->ps.velocity[0] += (0.0f - wp->forward[0]) * scale; ent->client->ps.velocity[1] += (0.0f - wp->forward[1]) * scale; ent->client->ps.velocity[2] += (0.0f - wp->forward[2]) * scale; @@ -201,7 +200,7 @@ namespace Components int PlayerMovement::StuckInClient_Hk(Game::gentity_s* self) { - if (BGPlayerEjection.get()) + if (BGPlayerEjection->current.enabled) { return Utils::Hook::Call(0x402D30)(self); // StuckInClient } @@ -212,7 +211,7 @@ namespace Components void PlayerMovement::CM_TransformedCapsuleTrace_Hk(Game::trace_t* results, const float* start, const float* end, const Game::Bounds* bounds, const Game::Bounds* capsule, int contents, const float* origin, const float* angles) { - if (BGPlayerCollision.get()) + if (BGPlayerCollision->current.enabled) { Utils::Hook::Call @@ -299,20 +298,20 @@ namespace Components BGBunnyHopAuto = Game::Dvar_RegisterBool("bg_bunnyHopAuto", false, Game::DVAR_CODINFO, "Constantly jump when holding space"); - BGRocketJump = Dvar::Register("bg_rocketJump", + BGRocketJump = Game::Dvar_RegisterBool("bg_rocketJump", false, Game::DVAR_CODINFO, "Enable CoD4 rocket jumps"); - BGRocketJumpScale = Dvar::Register("bg_rocketJumpScale", + BGRocketJumpScale = Game::Dvar_RegisterFloat("bg_rocketJumpScale", 64.0f, 1.0f, std::numeric_limits::max(), Game::DVAR_CODINFO, "The scale applied to the pushback force of a rocket"); - BGPlayerEjection = Dvar::Register("bg_playerEjection", + BGPlayerEjection = Game::Dvar_RegisterBool("bg_playerEjection", true, Game::DVAR_CODINFO, "Push intersecting players away from each other"); - BGPlayerCollision = Dvar::Register("bg_playerCollision", + BGPlayerCollision = Game::Dvar_RegisterBool("bg_playerCollision", true, Game::DVAR_CODINFO, "Push intersecting players away from each other"); - BGClimbAnything = Dvar::Register("bg_climbAnything", + BGClimbAnything = Game::Dvar_RegisterBool("bg_climbAnything", false, Game::DVAR_CODINFO, "Treat any surface as a ladder"); } diff --git a/src/Components/Modules/PlayerMovement.hpp b/src/Components/Modules/PlayerMovement.hpp index 9f87e2cd..82e4ad12 100644 --- a/src/Components/Modules/PlayerMovement.hpp +++ b/src/Components/Modules/PlayerMovement.hpp @@ -12,12 +12,11 @@ namespace Components static constexpr auto SURF_LADDER = 0x8; - static Dvar::Var BGRocketJump; - static Dvar::Var BGRocketJumpScale; - static Dvar::Var BGPlayerEjection; - static Dvar::Var BGPlayerCollision; - static Dvar::Var BGClimbAnything; - // Can't use Var class inside assembly stubs + static const Game::dvar_t* BGRocketJump; + static const Game::dvar_t* BGRocketJumpScale; + static const Game::dvar_t* BGPlayerEjection; + static const Game::dvar_t* BGPlayerCollision; + static const Game::dvar_t* BGClimbAnything; static const Game::dvar_t* CGNoclipScaler; static const Game::dvar_t* CGUfoScaler; static const Game::dvar_t* PlayerSpectateSpeedScale;