From d2f2748e9ed3eec02a1876adaa9de361ccc34e3c Mon Sep 17 00:00:00 2001 From: Arkshine Date: Sun, 1 Jun 2014 22:24:52 +0200 Subject: [PATCH] Cstrike: Enable/Disable detours whether CS_OnBuy and CS_InternalCommand are used in plugins + Add more check for safety. --- dlls/cstrike/cstrike/CstrikeHacks.cpp | 47 +++++++++++++++++++-------- dlls/cstrike/cstrike/CstrikeUtils.cpp | 20 ++++++++++++ dlls/cstrike/cstrike/CstrikeUtils.h | 1 + dlls/cstrike/cstrike/amxx_api.cpp | 7 ++++ 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/dlls/cstrike/cstrike/CstrikeHacks.cpp b/dlls/cstrike/cstrike/CstrikeHacks.cpp index 0794a239..e7703597 100644 --- a/dlls/cstrike/cstrike/CstrikeHacks.cpp +++ b/dlls/cstrike/cstrike/CstrikeHacks.cpp @@ -157,17 +157,25 @@ void CtrlDetours_ClientCommand(bool set) #endif g_ClientCommandDetour = DETOUR_CREATE_STATIC_FIXED(C_ClientCommand, target); - if (g_ClientCommandDetour != NULL) - g_ClientCommandDetour->EnableDetour(); - else + if (g_ClientCommandDetour == NULL) + { MF_Log("No Client Commands detour could be initialized - Disabled Client Command forward."); + } } else { - g_ClientCommandDetour->Destroy(); + if (g_ClientCommandDetour) + g_ClientCommandDetour->Destroy(); } } +void ToggleDetour_ClientCommands(bool enable) +{ + if (g_ClientCommandDetour) + (enable) ? g_ClientCommandDetour->EnableDetour() : g_ClientCommandDetour->DisableDetour(); +} + + void CtrlDetours_BuyCommands(bool set) { if (set) @@ -180,21 +188,32 @@ void CtrlDetours_BuyCommands(bool set) g_BuyItemDetour = DETOUR_CREATE_STATIC_FIXED(BuyItem, buyItemAddress); g_BuyGunAmmoDetour = DETOUR_CREATE_STATIC_FIXED(BuyGunAmmo, buyGunAmmoAddress); - if (g_CanBuyThisDetour != NULL && g_BuyItemDetour != NULL && g_BuyGunAmmoDetour != NULL) - { - g_CanBuyThisDetour->EnableDetour(); - g_BuyItemDetour->EnableDetour(); - g_BuyGunAmmoDetour->EnableDetour(); - } - else + if (g_CanBuyThisDetour == NULL || g_BuyItemDetour == NULL || g_BuyGunAmmoDetour == NULL) { MF_Log("No Buy Commands detours could be initialized - Disabled Buy forward."); } } else { - g_BuyItemDetour->Destroy(); - g_BuyGunAmmoDetour->Destroy(); - g_ClientCommandDetour->Destroy(); + if (g_CanBuyThisDetour) + g_CanBuyThisDetour->Destroy(); + + if (g_BuyItemDetour) + g_BuyItemDetour->Destroy(); + + if (g_BuyGunAmmoDetour) + g_BuyGunAmmoDetour->Destroy(); } +} + +void ToggleDetour_BuyCommands(bool enable) +{ + if (g_CanBuyThisDetour) + (enable) ? g_CanBuyThisDetour->EnableDetour() : g_CanBuyThisDetour->DisableDetour(); + + if (g_BuyItemDetour) + (enable) ? g_BuyItemDetour->EnableDetour() : g_BuyItemDetour->DisableDetour(); + + if (g_BuyGunAmmoDetour) + (enable) ? g_BuyGunAmmoDetour->EnableDetour() : g_BuyGunAmmoDetour->DisableDetour(); } \ No newline at end of file diff --git a/dlls/cstrike/cstrike/CstrikeUtils.cpp b/dlls/cstrike/cstrike/CstrikeUtils.cpp index 70218fe4..2c4fe6a3 100644 --- a/dlls/cstrike/cstrike/CstrikeUtils.cpp +++ b/dlls/cstrike/cstrike/CstrikeUtils.cpp @@ -119,4 +119,24 @@ void *UTIL_FindAddressFromEntry(const char *entry, bool isHidden, const char *li } return finalAddress != NULL ? finalAddress : NULL; +} + +bool UTIL_CheckForPublic(const char *publicname) +{ + AMX* amx; + int iFunctionIndex; + int i = 0; + char blah[64]; + + strncpy(blah, publicname, sizeof(blah)- 1); + + while ((amx = MF_GetScriptAmx(i++)) != NULL) + { + if (MF_AmxFindPublic(amx, blah, &iFunctionIndex) == AMX_ERR_NONE) + { + return true; + } + } + + return false; } \ No newline at end of file diff --git a/dlls/cstrike/cstrike/CstrikeUtils.h b/dlls/cstrike/cstrike/CstrikeUtils.h index 443454ea..ec2c7bc4 100644 --- a/dlls/cstrike/cstrike/CstrikeUtils.h +++ b/dlls/cstrike/cstrike/CstrikeUtils.h @@ -36,6 +36,7 @@ bool UTIL_IsPlayer(AMX* amx, edict_t* pPlayer); void UTIL_TextMsg_Generic(edict_t* pPlayer, const char* message); void *UTIL_FindAddressFromEntry(const char *entry, bool isHidden = false, const char *library = "mod"); +bool UTIL_CheckForPublic(const char *publicname); #define GETINFOKEYBUFFER (*g_engfuncs.pfnGetInfoKeyBuffer) #define SETCLIENTKEYVALUE (*g_engfuncs.pfnSetClientKeyValue) diff --git a/dlls/cstrike/cstrike/amxx_api.cpp b/dlls/cstrike/cstrike/amxx_api.cpp index e190e4a5..67088517 100644 --- a/dlls/cstrike/cstrike/amxx_api.cpp +++ b/dlls/cstrike/cstrike/amxx_api.cpp @@ -31,6 +31,7 @@ * version. */ #include "amxxmodule.h" +#include "CstrikeUtils.h" extern AMX_NATIVE_INFO cstrikeNatives[]; @@ -39,6 +40,9 @@ extern int g_CSBuyCmdFwd; void InitializeHacks(); void ShutdownHacks(); +void ToggleDetour_ClientCommands(bool enable); +void ToggleDetour_BuyCommands(bool enable); + int AmxxCheckGame(const char *game) { @@ -60,6 +64,9 @@ void OnPluginsLoaded() { g_CSCliCmdFwd = MF_RegisterForward("CS_InternalCommand", ET_STOP, FP_CELL, FP_STRING, FP_DONE); g_CSBuyCmdFwd = MF_RegisterForward("CS_OnBuy", ET_STOP, FP_CELL, FP_CELL, FP_DONE); + + ToggleDetour_ClientCommands(UTIL_CheckForPublic("CS_InternalCommand")); + ToggleDetour_BuyCommands(UTIL_CheckForPublic("CS_OnBuy")); } void OnAmxxDetach()