From 1b41b53d78cd0ad8e668db651f10e091c09d9e34 Mon Sep 17 00:00:00 2001 From: ineed bots Date: Sat, 29 Apr 2023 13:54:00 -0600 Subject: [PATCH] usercall detour test! --- src/component/test.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/game/game.cpp | 6 ++---- src/game/game.hpp | 3 ++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/component/test.cpp b/src/component/test.cpp index 444e346..9d4cafd 100644 --- a/src/component/test.cpp +++ b/src/component/test.cpp @@ -11,6 +11,9 @@ namespace test { utils::hook::detour gscr_spawn_hook; + // a __usercall detour! :o + utils::hook::detour scr_getentityid_hook; + namespace { game::dvar_s* custom_dvar; @@ -91,6 +94,37 @@ namespace test retn; } } + + unsigned int __stdcall scr_getentityid_call(game::scriptInstance_t inst, game::classNum_e classnum, unsigned int clientnum, unsigned int entnum) + { + // minhook allocated space for the original asm, we want to execute that instead because the original gamecode has the jump from the detour + return game::Scr_GetEntityId(inst, entnum, classnum, clientnum, scr_getentityid_hook.get_original()); + } + + unsigned int __declspec(naked) __cdecl scr_getentityid_stub(game::scriptInstance_t inst, game::classNum_e classnum, unsigned int clientnum) + { + // 00692520 unsigned int __usercall Scr_GetEntityId@(unsigned int entnum@, scriptInstance_t inst, classNum_e classnum, unsigned int clientnum) + __asm + { + // prol + push ebp; + mov ebp, esp; + + // push shit for our call, remember eax is a param in the usercall, rest was on stack + // we can access params like this in naked because we correctly setup the ebp + push eax; + push clientnum; + push classnum; + push inst; + call scr_getentityid_call; + // we made this a __stdcall, so we dont need to clean up stack + + // epil + mov esp, ebp; + pop ebp; + ret; + } + } } class component final : public component_interface @@ -137,6 +171,9 @@ namespace test // fix NEGOTIATION links //utils::hook::jump(0x4D3296, our_funny_hook); + + // test usercall detour! + scr_getentityid_hook.create(0x692520, scr_getentityid_stub); } private: diff --git a/src/game/game.cpp b/src/game/game.cpp index 48d65d5..81c34af 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -303,10 +303,8 @@ namespace game } } - unsigned int Scr_GetEntityId(scriptInstance_t inst, int entNum, classNum_e classnum, unsigned int clientnum) + unsigned int Scr_GetEntityId(scriptInstance_t inst, unsigned int entnum, classNum_e classnum, unsigned int clientnum, void* call_addr) { - static const auto call_addr = SELECT(0x0, 0x692520); - unsigned int answer; __asm @@ -314,7 +312,7 @@ namespace game push clientnum; push classnum; push inst; - mov eax, entNum; + mov eax, entnum; call call_addr; add esp, 0xC; mov answer, eax; diff --git a/src/game/game.hpp b/src/game/game.hpp index 689f43a..321a6a0 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -3,6 +3,7 @@ #include "structs.hpp" #define SELECT(mp, sp) (game::environment::t4mp() ? mp : sp) +#define CALL_ADDR(mp, sp) reinterpret_cast(SELECT(mp, sp)) namespace game { @@ -50,7 +51,7 @@ namespace game void Scr_AddUndefined(game::scriptInstance_t inst); gentity_s* Scr_GetEntity(unsigned int arg_index); void Scr_AddEntity(game::scriptInstance_t inst, gentity_s* ent); - unsigned int Scr_GetEntityId(scriptInstance_t inst, int entNum, classNum_e classnum, unsigned int clientnum); + unsigned int Scr_GetEntityId(scriptInstance_t inst, unsigned int entnum, classNum_e classnum, unsigned int clientnum, void* call_addr = CALL_ADDR(0x0, 0x692520)); void Scr_AddEntityNum(scriptInstance_t inst, unsigned int entid); pathnode_t* Scr_GetPathnode(scriptInstance_t inst); void Scr_AddPathnode(scriptInstance_t inst, pathnode_t* node);