diff --git a/modules/cstrike/cstrike/CstrikeDatas.h b/modules/cstrike/cstrike/CstrikeDatas.h index 4db529a2..4adcafd3 100644 --- a/modules/cstrike/cstrike/CstrikeDatas.h +++ b/modules/cstrike/cstrike/CstrikeDatas.h @@ -16,6 +16,9 @@ #include "amxxmodule.h" +/** + * Weapon Ids for use with CS_OnBuyAttempt(), CS_OnBuy(). + */ #define CSI_P228 CSW_P228 #define CSI_SCOUT CSW_SCOUT #define CSI_HEGRENADE CSW_HEGRENADE @@ -55,7 +58,9 @@ #define BITS_PISTOLS (1<v; + edict_t *pEdict = INDEXENT(0); + entvars_t *pev = VARS(pEdict); - byte* private_c = (byte*)edict->pvPrivateData; + byte *privateData = reinterpret_cast(pEdict->pvPrivateData); for (int i = 0; i < 0xFFF; i++) { - uintptr_t val = *((uintptr_t*)(private_c + i)); + entvars_t *val = *(reinterpret_cast(privateData + i)); - if (val == (uintptr_t)entvars) + if (val == pev) { this->pev = i; return; @@ -82,7 +82,8 @@ void* HL_TypeConversion::id_to_cbase(int index) entvars_t* HL_TypeConversion::id_to_entvar(int index) { - return &(INDEXENT2(index)->v); + edict_t *pEdict = INDEXENT2(index); + return pEdict ? VARS(pEdict) : nullptr; } entvars_t* HL_TypeConversion::cbase_to_entvar(void* cbase) @@ -92,7 +93,7 @@ entvars_t* HL_TypeConversion::cbase_to_entvar(void* cbase) return nullptr; } - return *(entvars_t **)((char *)(cbase) + G_OffsetHandler->pev); + return *reinterpret_cast(reinterpret_cast(cbase) + G_OffsetHandler->pev); } int HL_TypeConversion::cbase_to_id(void *cbase) @@ -102,7 +103,7 @@ int HL_TypeConversion::cbase_to_id(void *cbase) return -1; } - entvars_t* pev = this->cbase_to_entvar(cbase); + entvars_t *pev = this->cbase_to_entvar(cbase); if (!pev) { diff --git a/modules/cstrike/cstrike/CstrikeHacks.cpp b/modules/cstrike/cstrike/CstrikeHacks.cpp index bad87fc5..db44f616 100644 --- a/modules/cstrike/cstrike/CstrikeHacks.cpp +++ b/modules/cstrike/cstrike/CstrikeHacks.cpp @@ -13,45 +13,47 @@ #include "CstrikeDatas.h" #include "CstrikeUtils.h" -#include "CDetour/detours.h" +#include "CstrikeHacks.h" #include +#include "CstrikeHLTypeConversion.h" void CtrlDetours_ClientCommand(bool set); void CtrlDetours_BuyCommands(bool set); +void CtrlDetours_Natives(bool set); int ForwardInternalCommand = -1; -int ForwardOnBuy = -1; -int ForwardOnBuyAttempt = -1; +int ForwardOnBuy = -1; +int ForwardOnBuyAttempt = -1; -int *UseBotArgs = NULL; -const char **BotArgs = NULL; +int *UseBotArgs; +const char **BotArgs; -CDetour *ClientCommandDetour = NULL; -CDetour *GiveShieldDetour = NULL; -CDetour *GiveNamedItemDetour = NULL; -CDetour *AddAccountDetour = NULL; +CDetour *ClientCommandDetour; +CDetour *GiveShieldDetour; +CDetour *GiveNamedItemDetour; +CDetour *AddAccountDetour; +CDetour *GiveDefaultItemsDetour; -int CurrentItemId = 0; +CreateNamedEntityFunc CS_CreateNamedEntity; +UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString; + +int CurrentItemId; StringHashMap ItemAliasList; -int TeamOffset = 0; -int MenuOffset = 0; - -extern enginefuncs_t *g_pengfuncsTable; +int TeamOffset; +int MenuOffset; void InitializeHacks() { -#if defined AMD64 - #error UNSUPPORTED -#endif - CtrlDetours_ClientCommand(true); CtrlDetours_BuyCommands(true); + CtrlDetours_Natives(true); } void ShutdownHacks() { CtrlDetours_ClientCommand(false); CtrlDetours_BuyCommands(false); + CtrlDetours_Natives(false); } #undef CMD_ARGV @@ -65,7 +67,7 @@ const char *CMD_ARGV(int i) return BotArgs[i]; } - return NULL; + return nullptr; } return g_engfuncs.pfnCmd_Argv(i); @@ -76,7 +78,7 @@ void OnEmitSound(edict_t *entity, int channel, const char *sample, float volume, // If shield is blocked with CS_OnBuy, we need to block the pickup sound ("items/gunpickup2.wav") // as well played right after. Why this sound is not contained in GiveShield()? - g_pengfuncsTable->pfnEmitSound = NULL; + g_pengfuncsTable->pfnEmitSound = nullptr; RETURN_META(MRES_SUPERCEDE); } @@ -177,7 +179,7 @@ DETOUR_DECL_MEMBER1(GiveNamedItem, void, const char*, pszName) // void CBasePlay // If the current item id is not null, this means player has triggers a buy command. if (CurrentItemId) { - int client = PrivateToIndex(this); + int client = G_HL_TypeConversion.cbase_to_id(this); if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast(client), static_cast(CurrentItemId)) > 0) { @@ -198,7 +200,7 @@ DETOUR_DECL_MEMBER1(GiveShield, void, bool, bRetire) // void CBasePlayer::GiveSh // Special case for shield. Game doesn't use GiveNamedItem() to give a shield. if (CurrentItemId == CSI_SHIELDGUN) { - int client = PrivateToIndex(this); + int client = G_HL_TypeConversion.cbase_to_id(this); if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast(client), CSI_SHIELDGUN) > 0) { @@ -244,12 +246,12 @@ void CtrlDetours_ClientCommand(bool set) int offset = 0; - if (MainConfig->GetOffset("UseBotArgs", &offset) && offset) + if (MainConfig->GetOffset("UseBotArgs", &offset)) { UseBotArgs = get_pdata(base, offset); } - if (MainConfig->GetOffset("BotArgs", &offset) && offset) + if (MainConfig->GetOffset("BotArgs", &offset)) { BotArgs = get_pdata(base, offset); } @@ -258,12 +260,12 @@ void CtrlDetours_ClientCommand(bool set) void *address = nullptr; - if (MainConfig->GetMemSig("UseBotArgs", &address) && address) + if (MainConfig->GetMemSig("UseBotArgs", &address)) { UseBotArgs = reinterpret_cast(address); } - if (MainConfig->GetMemSig("BotArgs", &address) && address) + if (MainConfig->GetMemSig("BotArgs", &address)) { BotArgs = reinterpret_cast(address); } @@ -336,10 +338,10 @@ void ToggleDetour_ClientCommands(bool enable) { "nvgs" , CSI_NVGS }, { "shield" , CSI_SHIELDGUN }, { "buyammo1" , CSI_PRIMAMMO }, { "primammo" , CSI_PRIMAMMO }, { "buyammo2" , CSI_SECAMMO }, { "secammo" , CSI_SECAMMO }, - { NULL , 0 } + { nullptr , 0 } }; - for (size_t i = 0; aliasToId[i].alias != NULL; ++i) + for (size_t i = 0; aliasToId[i].alias != nullptr; ++i) { ItemAliasList.insert(aliasToId[i].alias, aliasToId[i].id); } @@ -357,15 +359,21 @@ void CtrlDetours_BuyCommands(bool set) { void *address = nullptr; - MainConfig->GetMemSig("GiveShield", &address); - GiveShieldDetour = DETOUR_CREATE_MEMBER_FIXED(GiveShield, address); address = nullptr; + if (MainConfig->GetMemSig("GiveShield", &address)) + { + GiveShieldDetour = DETOUR_CREATE_MEMBER_FIXED(GiveShield, address); + } - MainConfig->GetMemSig("GiveNamedItem", &address); - GiveNamedItemDetour = DETOUR_CREATE_MEMBER_FIXED(GiveNamedItem, address); address = nullptr; - - MainConfig->GetMemSig("AddAccount", &address); - AddAccountDetour = DETOUR_CREATE_MEMBER_FIXED(AddAccount, address); address = nullptr; + if (MainConfig->GetMemSig("GiveNamedItem", &address)) + { + GiveNamedItemDetour = DETOUR_CREATE_MEMBER_FIXED(GiveNamedItem, address); + } + if (MainConfig->GetMemSig("AddAccount", &address)) + { + AddAccountDetour = DETOUR_CREATE_MEMBER_FIXED(AddAccount, address); + } + if (!GiveShieldDetour || !GiveNamedItemDetour || !AddAccountDetour) { if (!GiveShieldDetour) @@ -424,3 +432,31 @@ void ToggleDetour_BuyCommands(bool enable) (enable) ? AddAccountDetour->EnableDetour() : AddAccountDetour->DisableDetour(); } } + +void CtrlDetours_Natives(bool set) +{ + if (set) + { + void *address = nullptr; + + if (MainConfig->GetMemSig("CreateNamedEntity", &address)) // cs_create_entity() + { + CS_CreateNamedEntity = reinterpret_cast(address); + } + + if (MainConfig->GetMemSig("FindEntityByString", &address)) // cs_find_ent_by_class() + { + CS_UTIL_FindEntityByString = reinterpret_cast(address); + } + + if (!CS_CreateNamedEntity) + { + MF_Log("CREATE_NAMED_ENITTY is not available - native cs_create_entity() has been disabled"); + } + + if (!CS_UTIL_FindEntityByString) + { + MF_Log("UTIL_FindEntByString is not available - native cs_find_ent_by_class() has been disabled"); + } + } +} diff --git a/modules/cstrike/cstrike/CstrikeHacks.h b/modules/cstrike/cstrike/CstrikeHacks.h new file mode 100644 index 00000000..b6d6769f --- /dev/null +++ b/modules/cstrike/cstrike/CstrikeHacks.h @@ -0,0 +1,54 @@ +// vim: set ts=4 sw=4 tw=99 noet: +// +// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO"). +// Copyright (C) The AMX Mod X Development Team. +// +// This software is licensed under the GNU General Public License, version 3 or higher. +// Additional exceptions apply. For full license details, see LICENSE.txt or visit: +// https://alliedmods.net/amxmodx-license + +// +// Counter-Strike Module +// + +#ifndef CSTRIKE_HACKS_H +#define CSTRIKE_HACKS_H + +#include +#include +#include + +void InitializeHacks(); +void ShutdownHacks(); +void ToggleDetour_ClientCommands(bool enable); +void ToggleDetour_BuyCommands(bool enable); + +extern int MessageIdArmorType; +extern int MessageIdMoney; +extern int MessageIdResetHUD; +extern int MessageIdScoreAttrib; +extern int MessageIdScoreInfo; +extern int MessageIdStatusIcon; +extern int MessageIdTeamInfo; +extern int MessageIdTextMsg; + +extern AMX_NATIVE_INFO CstrikeNatives[]; + +extern IGameConfig *MainConfig; +extern IGameConfig *OffsetConfig; + +extern int ForwardInternalCommand; +extern int ForwardOnBuy; +extern int ForwardOnBuyAttempt; + +typedef edict_t* (*CreateNamedEntityFunc)(string_t iszClassname); +typedef void* (*UTIL_FindEntityByStringFunc)(void* pStartEntity, const char *szKeyword, const char *szValue); + +extern CreateNamedEntityFunc CS_CreateNamedEntity; +extern UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString; + +extern CDetour *GiveDefaultItemsDetour; +extern enginefuncs_t *g_pengfuncsTable; +extern bool NoKifesMode; + +#endif // CSTRIKE_HACKS_H diff --git a/modules/cstrike/cstrike/CstrikeNatives.cpp b/modules/cstrike/cstrike/CstrikeNatives.cpp index 1f0d3eed..294beae3 100644 --- a/modules/cstrike/cstrike/CstrikeNatives.cpp +++ b/modules/cstrike/cstrike/CstrikeNatives.cpp @@ -14,23 +14,25 @@ #include "CstrikeDatas.h" #include "CstrikePlayer.h" #include "CstrikeUtils.h" +#include "CstrikeHacks.h" #include "CstrikeHLTypeConversion.h" +#include CCstrikePlayer g_players[33]; int g_zooming[33] = {0}; bool g_precachedknife = false; bool g_noknives = false; -extern CreateNamedEntityFunc CS_CreateNamedEntity; -extern UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString; +bool NoKifesMode = false; -extern int MessageIdMoney; -extern int MessageIdScoreInfo; -extern int MessageIdArmorType; -extern int MessageIdTeamInfo; -extern int MessageIdStatusIcon; -extern int MessageIdScoreAttrib; -extern int MessageIdResetHUD; +int MessageIdArmorType; +int MessageIdMoney; +int MessageIdResetHUD; +int MessageIdScoreAttrib; +int MessageIdScoreInfo; +int MessageIdStatusIcon; +int MessageIdTeamInfo; +int MessageIdTextMsg; // native cs_set_user_money(index, money, flash = 1); static cell AMX_NATIVE_CALL cs_set_user_money(AMX *amx, cell *params) @@ -938,13 +940,13 @@ static cell AMX_NATIVE_CALL cs_get_user_hasprimary(AMX *amx, cell *params) // native cs_get_no_knives(); static cell AMX_NATIVE_CALL cs_get_no_knives(AMX *amx, cell *params) { - return g_noknives ? 1 : 0; + return NoKifesMode ? 1 : 0; } // native cs_set_no_knives(noknives = 0); static cell AMX_NATIVE_CALL cs_set_no_knives(AMX *amx, cell *params) { - g_noknives = params[1] != 0; + NoKifesMode = params[1] != 0; return 1; } diff --git a/modules/cstrike/cstrike/CstrikeUtils.cpp b/modules/cstrike/cstrike/CstrikeUtils.cpp index f56fd685..d6949364 100644 --- a/modules/cstrike/cstrike/CstrikeUtils.cpp +++ b/modules/cstrike/cstrike/CstrikeUtils.cpp @@ -12,11 +12,10 @@ // #include "amxxmodule.h" -#include "MemoryUtils.h" extern int MessageIdTextMsg; -bool UTIL_IsPlayer(edict_t *pPlayer) +bool UTIL_IsPlayer(edict_t *pPlayer) { return strcmp(STRING(pPlayer->v.classname), "player") == 0; } @@ -36,7 +35,7 @@ bool UTIL_CheckForPublic(const char *publicname) int i = 0; char blah[64]; - strncpy(blah, publicname, sizeof(blah)- 1); + strncpy(blah, publicname, sizeof(blah) - 1); while ((amx = MF_GetScriptAmx(i++))) { @@ -46,6 +45,5 @@ bool UTIL_CheckForPublic(const char *publicname) } } - return false; + return false; } - diff --git a/modules/cstrike/cstrike/CstrikeUtils.h b/modules/cstrike/cstrike/CstrikeUtils.h index 9aa4a215..18ae43f7 100644 --- a/modules/cstrike/cstrike/CstrikeUtils.h +++ b/modules/cstrike/cstrike/CstrikeUtils.h @@ -14,16 +14,9 @@ #ifndef CSTRIKE_UTILS_H #define CSTRIKE_UTILS_H -#include - bool UTIL_IsPlayer(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); -char *UTIL_StringToLower(char *str); - -extern IGameConfig *MainConfig; -extern IGameConfig *OffsetConfig; #define GETINFOKEYBUFFER (*g_engfuncs.pfnGetInfoKeyBuffer) #define SETCLIENTKEYVALUE (*g_engfuncs.pfnSetClientKeyValue) @@ -119,39 +112,6 @@ inline void set_pdata(void *pEntity, int offset, T value, int element = 0) *reinterpret_cast(reinterpret_cast(pEntity) + offset + element * sizeof(T)) = value; } - -inline edict_t *PrivateToEdict(const void *pdata) -{ - if (!pdata) - { - return NULL; - } - - char *ptr = (char*)pdata; - ptr += 4; - entvars_t *pev = *(entvars_t **)ptr; - - if (!pev) - { - return NULL; - } - - return pev->pContainingEntity; -}; - -inline int PrivateToIndex(const void *pdata) -{ - edict_t *pEntity = PrivateToEdict(pdata); - - if (!pEntity) - { - return -1; - } - - return ENTINDEX(pEntity); -}; - - class EHANDLE { private: diff --git a/modules/cstrike/cstrike/amxx_api.cpp b/modules/cstrike/cstrike/amxx_api.cpp index 07ac0fac..2a2c9652 100644 --- a/modules/cstrike/cstrike/amxx_api.cpp +++ b/modules/cstrike/cstrike/amxx_api.cpp @@ -14,36 +14,14 @@ #include "amxxmodule.h" #include "CstrikeUtils.h" #include "CstrikeDatas.h" +#include "CstrikeHacks.h" #include "CstrikeHLTypeConversion.h" #include -extern AMX_NATIVE_INFO CstrikeNatives[]; - -extern int ForwardInternalCommand; -extern int ForwardOnBuy; -extern int ForwardOnBuyAttempt; - -void InitializeHacks(); -void ShutdownHacks(); -void ToggleDetour_ClientCommands(bool enable); -void ToggleDetour_BuyCommands(bool enable); - -CreateNamedEntityFunc CS_CreateNamedEntity = nullptr; -UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString = nullptr; - IGameConfig *MainConfig; IGameConfig *OffsetConfig; IGameConfigManager *ConfigManager; -int MessageIdArmorType; -int MessageIdMoney; -int MessageIdResetHUD; -int MessageIdScoreAttrib; -int MessageIdScoreInfo; -int MessageIdStatusIcon; -int MessageIdTeamInfo; -int MessageIdTextMsg; - struct UserMsg { const char* name; @@ -60,10 +38,9 @@ UserMsg MessagesList[] = { "StatusIcon" , &MessageIdStatusIcon }, { "TeamInfo" , &MessageIdTeamInfo }, { "TextMsg" , &MessageIdTextMsg }, - { nullptr , 0 } + { nullptr , nullptr } }; - int AmxxCheckGame(const char *game) { if (strcasecmp(game, "cstrike") == 0 || @@ -80,7 +57,7 @@ void OnAmxxAttach() ConfigManager = MF_GetConfigManager(); - char error[256]; + char error[256]; error[0] = '\0'; if (!ConfigManager->LoadGameConfigFile("modules.games", &MainConfig, error, sizeof(error)) && error[0] != '\0') @@ -97,28 +74,6 @@ void OnAmxxAttach() return; } - void *address = nullptr; - - if (MainConfig->GetMemSig("CreateNamedEntity", &address) && address) // cs_create_entity() - { - CS_CreateNamedEntity = reinterpret_cast(address); - } - - if (MainConfig->GetMemSig("FindEntityByString", &address) && address) // cs_find_ent_by_class() - { - CS_UTIL_FindEntityByString = reinterpret_cast(address); - } - - if (!CS_CreateNamedEntity) - { - MF_Log("CREATE_NAMED_ENITTY is not available - native cs_create_entity() has been disabled"); - } - - if (!CS_UTIL_FindEntityByString) - { - MF_Log("UTIL_FindEntByString is not available - native cs_find_ent_by_class() has been disabled"); - } - InitializeHacks(); } diff --git a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj index 98f84d2e..d62bbb55 100644 --- a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj +++ b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj @@ -158,6 +158,7 @@ + diff --git a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters index be95ef31..29a31039 100644 --- a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters +++ b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters @@ -88,6 +88,9 @@ Module SDK + + Header Files +