From 6e50a0effdd815752b485cffa060f8e41a5c5044 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Thu, 3 Jul 2014 10:10:18 +0200 Subject: [PATCH] Cstrike: Make CS_OnBuy forward more reliable - part 1 Purpose is to have the forward be called only on actual buying. This has been requested on the forum. --- dlls/cstrike/cstrike/CstrikeDatas.h | 6 ++++ dlls/cstrike/cstrike/CstrikeHacks.cpp | 48 ++++++++++++++++++++++----- public/memtools/CDetour/detours.h | 1 + 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/dlls/cstrike/cstrike/CstrikeDatas.h b/dlls/cstrike/cstrike/CstrikeDatas.h index d64c773e..85a61d50 100644 --- a/dlls/cstrike/cstrike/CstrikeDatas.h +++ b/dlls/cstrike/cstrike/CstrikeDatas.h @@ -165,16 +165,22 @@ #define CS_IDENT_CANBUYTHIS "_Z10CanBuyThisP11CBasePlayeri" #define CS_IDENT_BUYITEM "_Z7BuyItemP11CBasePlayeri" #define CS_IDENT_BUYGUNAMMO "_Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb" + #define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc" + #define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib" #define CS_IDENT_HIDDEN_STATE false #elif defined(__APPLE__) #define CS_IDENT_CANBUYTHIS "_Z10CanBuyThisP11CBasePlayeri" #define CS_IDENT_BUYITEM "_Z7BuyItemP11CBasePlayeri" #define CS_IDENT_BUYGUNAMMO "_Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb" + #define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc" + #define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib" #define CS_IDENT_HIDDEN_STATE true #elif defined(WIN32) #define CS_IDENT_CANBUYTHIS "\\x53\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x56\\x8B\\x2A\\x2A\\x2A\\x57" #define CS_IDENT_BUYITEM "\\x53\\x56\\x8B\\x2A\\x2A\\x2A\\xBB\\x2A\\x2A\\x2A\\x2A\\x57\\x53" #define CS_IDENT_BUYGUNAMMO "\\x56\\x57\\x8B\\x2A\\x2A\\x2A\\x6A\\x2A\\x8B\\x2A\\xE8\\x2A\\x2A\\x2A\\x2A\\x84\\x2A\\x0F" + #define CS_IDENT_GIVENAMEDITEM "\\x8B\\x2A\\x2A\\x2A\\x56\\x57\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x2B" + #define CS_IDENT_ADDACCOUNT "\\x8B\\x2A\\x2A\\x2A\\x56\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x03" #define CS_IDENT_HIDDEN_STATE false #endif diff --git a/dlls/cstrike/cstrike/CstrikeHacks.cpp b/dlls/cstrike/cstrike/CstrikeHacks.cpp index e7703597..01f5f4c4 100644 --- a/dlls/cstrike/cstrike/CstrikeHacks.cpp +++ b/dlls/cstrike/cstrike/CstrikeHacks.cpp @@ -47,7 +47,8 @@ CDetour *g_ClientCommandDetour = NULL; CDetour *g_CanBuyThisDetour = NULL; CDetour *g_BuyItemDetour = NULL; CDetour *g_BuyGunAmmoDetour = NULL; - +CDetour *g_GiveNamedItemDetour = NULL; +CDetour *g_AddAccountDetour = NULL; void InitializeHacks() { @@ -137,6 +138,17 @@ DETOUR_DECL_STATIC3(BuyGunAmmo, bool, void*, pvPlayer, void*, pvWeapon, bool, bB return DETOUR_STATIC_CALL(BuyGunAmmo)(pvPlayer, pvWeapon, bBlinkMoney); } +DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlayer::GiveNamedItem(const char *pszName) +{ + DETOUR_MEMBER_CALL(GiveNamedItem)(pszName); +} + + +DETOUR_DECL_MEMBER2(AddAccount, void, int, amount, bool, bTrackChange) // void CBasePlayer::AddAccount(int amount, bool bTrackChange) +{ + DETOUR_MEMBER_CALL(AddAccount)(amount, bTrackChange); +} + void CtrlDetours_ClientCommand(bool set) { @@ -180,15 +192,23 @@ void CtrlDetours_BuyCommands(bool set) { if (set) { - void *canBuyThisAddress = UTIL_FindAddressFromEntry(CS_IDENT_CANBUYTHIS, CS_IDENT_HIDDEN_STATE); - void *buyItemAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYITEM, CS_IDENT_HIDDEN_STATE); - void *buyGunAmmoAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYGUNAMMO, CS_IDENT_HIDDEN_STATE); + void *canBuyThisAddress = UTIL_FindAddressFromEntry(CS_IDENT_CANBUYTHIS , CS_IDENT_HIDDEN_STATE); + void *buyItemAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYITEM , CS_IDENT_HIDDEN_STATE); + void *buyGunAmmoAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYGUNAMMO , CS_IDENT_HIDDEN_STATE); + void *giveNamedItemAddress = UTIL_FindAddressFromEntry(CS_IDENT_GIVENAMEDITEM, CS_IDENT_HIDDEN_STATE); + void *addAccountAddress = UTIL_FindAddressFromEntry(CS_IDENT_ADDACCOUNT , CS_IDENT_HIDDEN_STATE); - g_CanBuyThisDetour = DETOUR_CREATE_STATIC_FIXED(CanBuyThis, canBuyThisAddress); - g_BuyItemDetour = DETOUR_CREATE_STATIC_FIXED(BuyItem, buyItemAddress); - g_BuyGunAmmoDetour = DETOUR_CREATE_STATIC_FIXED(BuyGunAmmo, buyGunAmmoAddress); + g_CanBuyThisDetour = DETOUR_CREATE_STATIC_FIXED(CanBuyThis, canBuyThisAddress); + g_BuyItemDetour = DETOUR_CREATE_STATIC_FIXED(BuyItem, buyItemAddress); + g_BuyGunAmmoDetour = DETOUR_CREATE_STATIC_FIXED(BuyGunAmmo, buyGunAmmoAddress); + g_GiveNamedItemDetour = DETOUR_CREATE_MEMBER_FIXED(GiveNamedItem, giveNamedItemAddress); + g_AddAccountDetour = DETOUR_CREATE_MEMBER_FIXED(AddAccount, addAccountAddress); - if (g_CanBuyThisDetour == NULL || g_BuyItemDetour == NULL || g_BuyGunAmmoDetour == NULL) + if (g_CanBuyThisDetour == NULL || + g_BuyItemDetour == NULL || + g_BuyGunAmmoDetour == NULL || + g_GiveNamedItemDetour == NULL || + g_AddAccountDetour == NULL) { MF_Log("No Buy Commands detours could be initialized - Disabled Buy forward."); } @@ -203,6 +223,12 @@ void CtrlDetours_BuyCommands(bool set) if (g_BuyGunAmmoDetour) g_BuyGunAmmoDetour->Destroy(); + + if (g_GiveNamedItemDetour) + g_GiveNamedItemDetour->Destroy(); + + if (g_AddAccountDetour) + g_AddAccountDetour->Destroy(); } } @@ -216,4 +242,10 @@ void ToggleDetour_BuyCommands(bool enable) if (g_BuyGunAmmoDetour) (enable) ? g_BuyGunAmmoDetour->EnableDetour() : g_BuyGunAmmoDetour->DisableDetour(); + + if (g_GiveNamedItemDetour) + (enable) ? g_GiveNamedItemDetour->EnableDetour() : g_GiveNamedItemDetour->DisableDetour(); + + if (g_AddAccountDetour) + (enable) ? g_AddAccountDetour->EnableDetour() : g_AddAccountDetour->DisableDetour(); } \ No newline at end of file diff --git a/public/memtools/CDetour/detours.h b/public/memtools/CDetour/detours.h index dea92c42..89c2b238 100644 --- a/public/memtools/CDetour/detours.h +++ b/public/memtools/CDetour/detours.h @@ -136,6 +136,7 @@ ret name##Class::name(p1type p1name, p2type p2name, p3type p3name, p4type p4name #define GET_STATIC_TRAMPOLINE(name) (void **)&name##_Actual #define DETOUR_CREATE_MEMBER(name, gamedata, target) CDetourManager::CreateDetour(GET_MEMBER_CALLBACK(name), GET_MEMBER_TRAMPOLINE(name), gamedata, target); +#define DETOUR_CREATE_MEMBER_FIXED(name, address) CDetourManager::CreateDetour(GET_MEMBER_CALLBACK(name), GET_MEMBER_TRAMPOLINE(name), address); #define DETOUR_CREATE_STATIC(name, gamedata, target) CDetourManager::CreateDetour(GET_STATIC_CALLBACK(name), GET_STATIC_TRAMPOLINE(name), gamedata, target); #define DETOUR_CREATE_STATIC_FIXED(name, address) CDetourManager::CreateDetour(GET_STATIC_CALLBACK(name), GET_STATIC_TRAMPOLINE(name), address);