Cstrike: Enable/Disable detours whether CS_OnBuy and CS_InternalCommand are used in plugins + Add more check for safety.

This commit is contained in:
Arkshine 2014-06-01 22:24:52 +02:00
parent f1e206fde6
commit d2f2748e9e
4 changed files with 61 additions and 14 deletions

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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)

View File

@ -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()