Cstrike: Random cleanup.

This commit is contained in:
Arkshine 2014-07-05 00:04:20 +02:00
parent 7cb004c728
commit c2c78f6724
3 changed files with 77 additions and 79 deletions

View File

@ -40,20 +40,20 @@ using namespace SourceMod; // hashmap
void CtrlDetours_ClientCommand(bool set); void CtrlDetours_ClientCommand(bool set);
void CtrlDetours_BuyCommands(bool set); void CtrlDetours_BuyCommands(bool set);
int g_CSCliCmdFwd = -1; int ForwardInternalCommand = -1;
int g_CSBuyCmdFwd = -1; int ForwardOnBuy = -1;
int g_CSBuyAttemptCmdFwd = -1; int ForwardOnBuyAttempt = -1;
int *g_UseBotArgs = NULL; int *UseBotArgs = NULL;
const char **g_BotArgs = NULL; const char **BotArgs = NULL;
CDetour *g_ClientCommandDetour = NULL; CDetour *ClientCommandDetour = NULL;
CDetour *g_GiveShieldDetour = NULL; CDetour *GiveShieldDetour = NULL;
CDetour *g_GiveNamedItemDetour = NULL; CDetour *GiveNamedItemDetour = NULL;
CDetour *g_AddAccountDetour = NULL; CDetour *AddAccountDetour = NULL;
int g_CurrentItemId = 0; int CurrentItemId = 0;
StringHashMap<int> g_ItemAliasList; StringHashMap<int> ItemAliasList;
void InitializeHacks() void InitializeHacks()
{ {
@ -75,11 +75,11 @@ void ShutdownHacks()
const char *CMD_ARGV(int i) const char *CMD_ARGV(int i)
{ {
if (*g_UseBotArgs) if (*UseBotArgs)
{ {
if (i < 4) if (i < 4)
{ {
return g_BotArgs[i]; return BotArgs[i];
} }
return NULL; return NULL;
@ -93,11 +93,11 @@ DETOUR_DECL_STATIC1(C_ClientCommand, void, edict_t*, pEdict) // void ClientComma
const char *command = CMD_ARGV(0); const char *command = CMD_ARGV(0);
// A new command is triggered, reset variable, always. // A new command is triggered, reset variable, always.
g_CurrentItemId = 0; CurrentItemId = 0;
// Purpose is to retrieve an item id based on alias name or selected item from menu, // Purpose is to retrieve an item id based on alias name or selected item from menu,
// to be used in OnBuy* forwards. // to be used in OnBuy* forwards.
if ((g_CSBuyAttemptCmdFwd != -1 || g_CSBuyCmdFwd != -1) && command && *command) if ((ForwardOnBuyAttempt != -1 || ForwardOnBuy != -1) && command && *command)
{ {
// Just for safety. // Just for safety.
command = UTIL_StringToLower((char *)command); command = UTIL_StringToLower((char *)command);
@ -145,36 +145,36 @@ DETOUR_DECL_STATIC1(C_ClientCommand, void, edict_t*, pEdict) // void ClientComma
if (itemId) if (itemId)
{ {
g_CurrentItemId = itemId; CurrentItemId = itemId;
} }
} }
} }
} }
else // Handling buy via alias else // Handling buy via alias
{ {
if (g_ItemAliasList.retrieve(command, &itemId)) if (ItemAliasList.retrieve(command, &itemId))
{ {
g_CurrentItemId = itemId; CurrentItemId = itemId;
} }
} }
} }
int client = ENTINDEX(pEdict); int client = ENTINDEX(pEdict);
if (g_CSCliCmdFwd != -1 && *g_UseBotArgs) if (ForwardInternalCommand != -1 && *UseBotArgs)
{ {
const char *args = *g_BotArgs; const char *args = *BotArgs;
if (MF_ExecuteForward(g_CSCliCmdFwd, static_cast<cell>(client), args) > 0) if (MF_ExecuteForward(ForwardInternalCommand, static_cast<cell>(client), args) > 0)
{ {
return; return;
} }
} }
if (g_CSBuyAttemptCmdFwd != -1 && if (ForwardOnBuyAttempt != -1 &&
g_CurrentItemId && CurrentItemId &&
MF_IsPlayerAlive(client) && MF_IsPlayerAlive(client) &&
MF_ExecuteForward(g_CSBuyAttemptCmdFwd, static_cast<cell>(client), static_cast<cell>(g_CurrentItemId)) > 0) MF_ExecuteForward(ForwardOnBuyAttempt, static_cast<cell>(client), static_cast<cell>(CurrentItemId)) > 0)
{ {
return; return;
} }
@ -185,11 +185,11 @@ DETOUR_DECL_STATIC1(C_ClientCommand, void, edict_t*, pEdict) // void ClientComma
DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlayer::GiveNamedItem(const char *pszName) DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlayer::GiveNamedItem(const char *pszName)
{ {
// If the current item id is not null, this means player has triggers a buy command. // If the current item id is not null, this means player has triggers a buy command.
if (g_CurrentItemId) if (CurrentItemId)
{ {
int client = PrivateToIndex(this); int client = PrivateToIndex(this);
if (MF_IsPlayerAlive(client) && MF_ExecuteForward(g_CSBuyCmdFwd, static_cast<cell>(client), static_cast<cell>(g_CurrentItemId)) > 0) if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast<cell>(client), static_cast<cell>(CurrentItemId)) > 0)
{ {
return; return;
} }
@ -197,7 +197,7 @@ DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlay
// From here, forward is not blocked, resetting this // From here, forward is not blocked, resetting this
// to ignore code in AddAccount which is called right after. // to ignore code in AddAccount which is called right after.
g_CurrentItemId = 0; CurrentItemId = 0;
// Give me my item! // Give me my item!
DETOUR_MEMBER_CALL(GiveNamedItem)(pszName); DETOUR_MEMBER_CALL(GiveNamedItem)(pszName);
@ -206,11 +206,11 @@ DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlay
DETOUR_DECL_MEMBER1(GiveShield, void, bool, bRetire) // void CBasePlayer::GiveShield(bool bRetire) DETOUR_DECL_MEMBER1(GiveShield, void, bool, bRetire) // void CBasePlayer::GiveShield(bool bRetire)
{ {
// Special case for shield. Game doesn't use GiveNamedItem() to give a shield. // Special case for shield. Game doesn't use GiveNamedItem() to give a shield.
if (g_CurrentItemId == CSI_SHIELDGUN) if (CurrentItemId == CSI_SHIELDGUN)
{ {
int client = PrivateToIndex(this); int client = PrivateToIndex(this);
if (MF_IsPlayerAlive(client) && MF_ExecuteForward(g_CSBuyCmdFwd, static_cast<cell>(client), CSI_SHIELDGUN) > 0) if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast<cell>(client), CSI_SHIELDGUN) > 0)
{ {
return; return;
} }
@ -218,7 +218,7 @@ DETOUR_DECL_MEMBER1(GiveShield, void, bool, bRetire) // void CBasePlayer::GiveSh
// From here, forward is not blocked, resetting this // From here, forward is not blocked, resetting this
// to ignore code in AddAccount which is called right after. // to ignore code in AddAccount which is called right after.
g_CurrentItemId = 0; CurrentItemId = 0;
// Give me my shield! // Give me my shield!
DETOUR_MEMBER_CALL(GiveShield)(bRetire); DETOUR_MEMBER_CALL(GiveShield)(bRetire);
@ -228,13 +228,13 @@ DETOUR_DECL_MEMBER2(AddAccount, void, int, amount, bool, bTrackChange) // void C
{ {
// No buy command or forward not blocked. // No buy command or forward not blocked.
// Resuming game flow. // Resuming game flow.
if (!g_CurrentItemId) if (!CurrentItemId)
{ {
DETOUR_MEMBER_CALL(AddAccount)(amount, bTrackChange); DETOUR_MEMBER_CALL(AddAccount)(amount, bTrackChange);
} }
// Let's reset this right away to avoid issues. // Let's reset this right away to avoid issues.
g_CurrentItemId = 0; CurrentItemId = 0;
} }
@ -246,35 +246,35 @@ void CtrlDetours_ClientCommand(bool set)
#if defined(WIN32) #if defined(WIN32)
g_UseBotArgs = *(int **)((unsigned char *)target + CS_CLICMD_OFFS_USEBOTARGS); UseBotArgs = *(int **)((unsigned char *)target + CS_CLICMD_OFFS_USEBOTARGS);
g_BotArgs = (const char **)*(const char **)((unsigned char *)target + CS_CLICMD_OFFS_BOTARGS); BotArgs = (const char **)*(const char **)((unsigned char *)target + CS_CLICMD_OFFS_BOTARGS);
#elif defined(__linux__) || defined(__APPLE__) #elif defined(__linux__) || defined(__APPLE__)
g_UseBotArgs = (int *)UTIL_FindAddressFromEntry(CS_IDENT_USEBOTARGS, CS_IDENT_HIDDEN_STATE); UseBotArgs = (int *)UTIL_FindAddressFromEntry(CS_IDENT_USEBOTARGS, CS_IDENT_HIDDEN_STATE);
g_BotArgs = (const char **)UTIL_FindAddressFromEntry(CS_IDENT_BOTARGS, CS_IDENT_HIDDEN_STATE); BotArgs = (const char **)UTIL_FindAddressFromEntry(CS_IDENT_BOTARGS, CS_IDENT_HIDDEN_STATE);
#endif #endif
g_ClientCommandDetour = DETOUR_CREATE_STATIC_FIXED(C_ClientCommand, target); ClientCommandDetour = DETOUR_CREATE_STATIC_FIXED(C_ClientCommand, target);
if (g_ClientCommandDetour == NULL) if (ClientCommandDetour == NULL)
{ {
MF_Log("No Client Commands detour could be initialized - Disabled Client Command forward."); MF_Log("No Client Commands detour could be initialized - Disabled Client Command forward.");
} }
} }
else else
{ {
if (g_ClientCommandDetour) if (ClientCommandDetour)
g_ClientCommandDetour->Destroy(); ClientCommandDetour->Destroy();
g_ItemAliasList.clear(); ItemAliasList.clear();
} }
} }
void ToggleDetour_ClientCommands(bool enable) void ToggleDetour_ClientCommands(bool enable)
{ {
if (g_ClientCommandDetour) if (ClientCommandDetour)
(enable) ? g_ClientCommandDetour->EnableDetour() : g_ClientCommandDetour->DisableDetour(); (enable) ? ClientCommandDetour->EnableDetour() : ClientCommandDetour->DisableDetour();
if (enable) if (enable)
{ {
@ -321,12 +321,12 @@ void ToggleDetour_ClientCommands(bool enable)
for (size_t i = 0; aliasToId[i].alias != NULL; ++i) for (size_t i = 0; aliasToId[i].alias != NULL; ++i)
{ {
g_ItemAliasList.insert(aliasToId[i].alias, aliasToId[i].id); ItemAliasList.insert(aliasToId[i].alias, aliasToId[i].id);
} }
} }
else else
{ {
g_ItemAliasList.clear(); ItemAliasList.clear();
} }
} }
@ -339,38 +339,38 @@ void CtrlDetours_BuyCommands(bool set)
void *giveNamedItemAddress = UTIL_FindAddressFromEntry(CS_IDENT_GIVENAMEDITEM, 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); void *addAccountAddress = UTIL_FindAddressFromEntry(CS_IDENT_ADDACCOUNT , CS_IDENT_HIDDEN_STATE);
g_GiveShieldDetour = DETOUR_CREATE_MEMBER_FIXED(GiveShield, giveShieldAddress); GiveShieldDetour = DETOUR_CREATE_MEMBER_FIXED(GiveShield, giveShieldAddress);
g_GiveNamedItemDetour = DETOUR_CREATE_MEMBER_FIXED(GiveNamedItem, giveNamedItemAddress); GiveNamedItemDetour = DETOUR_CREATE_MEMBER_FIXED(GiveNamedItem, giveNamedItemAddress);
g_AddAccountDetour = DETOUR_CREATE_MEMBER_FIXED(AddAccount, addAccountAddress); AddAccountDetour = DETOUR_CREATE_MEMBER_FIXED(AddAccount, addAccountAddress);
if (g_GiveNamedItemDetour == NULL || g_AddAccountDetour == NULL) if (GiveNamedItemDetour == NULL || AddAccountDetour == NULL)
{ {
MF_Log("No Buy Commands detours could be initialized - Disabled Buy forward."); MF_Log("No Buy Commands detours could be initialized - Disabled Buy forward.");
} }
} }
else else
{ {
if (g_GiveShieldDetour) if (GiveShieldDetour)
g_GiveShieldDetour->Destroy(); GiveShieldDetour->Destroy();
if (g_GiveNamedItemDetour) if (GiveNamedItemDetour)
g_GiveNamedItemDetour->Destroy(); GiveNamedItemDetour->Destroy();
if (g_AddAccountDetour) if (AddAccountDetour)
g_AddAccountDetour->Destroy(); AddAccountDetour->Destroy();
g_ItemAliasList.clear(); ItemAliasList.clear();
} }
} }
void ToggleDetour_BuyCommands(bool enable) void ToggleDetour_BuyCommands(bool enable)
{ {
if (g_GiveShieldDetour) if (GiveShieldDetour)
(enable) ? g_GiveShieldDetour->EnableDetour() : g_GiveShieldDetour->DisableDetour(); (enable) ? GiveShieldDetour->EnableDetour() : GiveShieldDetour->DisableDetour();
if (g_GiveNamedItemDetour) if (GiveNamedItemDetour)
(enable) ? g_GiveNamedItemDetour->EnableDetour() : g_GiveNamedItemDetour->DisableDetour(); (enable) ? GiveNamedItemDetour->EnableDetour() : GiveNamedItemDetour->DisableDetour();
if (g_AddAccountDetour) if (AddAccountDetour)
(enable) ? g_AddAccountDetour->EnableDetour() : g_AddAccountDetour->DisableDetour(); (enable) ? AddAccountDetour->EnableDetour() : AddAccountDetour->DisableDetour();
} }

View File

@ -1749,7 +1749,7 @@ static cell AMX_NATIVE_CALL not_on_64(AMX* amx, cell* params)
#endif #endif
AMX_NATIVE_INFO cstrikeNatives[] = { AMX_NATIVE_INFO CstrikeNatives[] = {
{"cs_set_user_money", cs_set_user_money}, {"cs_set_user_money", cs_set_user_money},
{"cs_get_user_money", cs_get_user_money}, {"cs_get_user_money", cs_get_user_money},
{"cs_get_user_deaths", cs_get_user_deaths}, {"cs_get_user_deaths", cs_get_user_deaths},

View File

@ -33,11 +33,11 @@
#include "amxxmodule.h" #include "amxxmodule.h"
#include "CstrikeUtils.h" #include "CstrikeUtils.h"
extern AMX_NATIVE_INFO cstrikeNatives[]; extern AMX_NATIVE_INFO CstrikeNatives[];
extern int g_CSCliCmdFwd; extern int ForwardInternalCommand;
extern int g_CSBuyCmdFwd; extern int ForwardOnBuy;
extern int g_CSBuyAttemptCmdFwd; extern int ForwardOnBuyAttempt;
void InitializeHacks(); void InitializeHacks();
void ShutdownHacks(); void ShutdownHacks();
@ -57,27 +57,25 @@ int AmxxCheckGame(const char *game)
void OnAmxxAttach() void OnAmxxAttach()
{ {
MF_AddNatives(cstrikeNatives); MF_AddNatives(CstrikeNatives);
InitializeHacks(); InitializeHacks();
} }
void OnPluginsLoaded() void OnPluginsLoaded()
{ {
g_CSCliCmdFwd = MF_RegisterForward("CS_InternalCommand", ET_STOP, FP_CELL, FP_STRING, FP_DONE); ForwardInternalCommand = 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); ForwardOnBuy = MF_RegisterForward("CS_OnBuy" , ET_STOP, FP_CELL, FP_CELL, FP_DONE);
g_CSBuyAttemptCmdFwd = MF_RegisterForward("CS_OnBuyAttempt" , ET_STOP, FP_CELL, FP_CELL, FP_DONE); ForwardOnBuyAttempt = MF_RegisterForward("CS_OnBuyAttempt" , ET_STOP, FP_CELL, FP_CELL, FP_DONE);
// Checking whether such public forwards are used in plugins. // Checking whether such public forwards are used in plugins.
// Resetting variable to -1 to avoid running unnecessary code in ClientCommand. // Resetting variable to -1 to avoid running unnecessary code in ClientCommand.
if (!UTIL_CheckForPublic("CS_InternalCommand")) { ForwardInternalCommand = -1; }
if (!UTIL_CheckForPublic("CS_InternalCommand")) { g_CSCliCmdFwd = -1; } if (!UTIL_CheckForPublic("CS_OnBuy")) { ForwardOnBuy = -1; }
if (!UTIL_CheckForPublic("CS_OnBuy")) { g_CSBuyCmdFwd = -1; } if (!UTIL_CheckForPublic("CS_OnBuyAttempt")) { ForwardOnBuyAttempt = -1; }
if (!UTIL_CheckForPublic("CS_OnBuyAttempt")) { g_CSBuyAttemptCmdFwd = -1; }
// And enable/disable detours when necessary. // And enable/disable detours when necessary.
ToggleDetour_ClientCommands(ForwardInternalCommand != -1 || ForwardOnBuy != -1 || ForwardOnBuy != -1);
ToggleDetour_ClientCommands(g_CSCliCmdFwd != -1 || g_CSBuyCmdFwd != -1 || g_CSBuyCmdFwd != -1); ToggleDetour_BuyCommands(ForwardOnBuy != -1);
ToggleDetour_BuyCommands(g_CSBuyCmdFwd != -1);
} }
void OnAmxxDetach() void OnAmxxDetach()