From b65a0600eec015376c380fd3f74b10acb3379f92 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Wed, 7 Oct 2015 17:41:54 +0200 Subject: [PATCH] Consistency: Replace GETEDICT and variants with HLTypeConversion in cstrike module --- modules/cstrike/cstrike/AMBuilder | 1 - modules/cstrike/cstrike/CstrikeDatas.h | 2 - .../cstrike/CstrikeHLTypeConversion.cpp | 119 ------------------ .../cstrike/cstrike/CstrikeHLTypeConversion.h | 49 -------- modules/cstrike/cstrike/CstrikeHacks.cpp | 5 +- modules/cstrike/cstrike/CstrikeMain.cpp | 12 +- modules/cstrike/cstrike/CstrikeNatives.cpp | 12 +- modules/cstrike/cstrike/CstrikeUtils.h | 73 +---------- .../cstrike/cstrike/msvc12/cstrike.vcxproj | 1 - .../cstrike/msvc12/cstrike.vcxproj.filters | 3 - 10 files changed, 15 insertions(+), 262 deletions(-) delete mode 100644 modules/cstrike/cstrike/CstrikeHLTypeConversion.cpp delete mode 100644 modules/cstrike/cstrike/CstrikeHLTypeConversion.h diff --git a/modules/cstrike/cstrike/AMBuilder b/modules/cstrike/cstrike/AMBuilder index e89158c6..0db59424 100644 --- a/modules/cstrike/cstrike/AMBuilder +++ b/modules/cstrike/cstrike/AMBuilder @@ -15,7 +15,6 @@ binary.sources = [ 'CstrikeHacks.cpp', 'CstrikeUtils.cpp', 'CstrikeUserMessages.cpp', - 'CstrikeHLTypeConversion.cpp', '../../../public/memtools/MemoryUtils.cpp', '../../../public/memtools/CDetour/detours.cpp', '../../../public/memtools/CDetour/asm/asm.c', diff --git a/modules/cstrike/cstrike/CstrikeDatas.h b/modules/cstrike/cstrike/CstrikeDatas.h index 8d9e1be6..6167f7f3 100644 --- a/modules/cstrike/cstrike/CstrikeDatas.h +++ b/modules/cstrike/cstrike/CstrikeDatas.h @@ -14,8 +14,6 @@ #ifndef CSTRIKE_DATA_H #define CSTRIKE_DATA_H -#include "amxxmodule.h" - /** * Weapon Ids for use with CS_OnBuyAttempt(), CS_OnBuy(). */ diff --git a/modules/cstrike/cstrike/CstrikeHLTypeConversion.cpp b/modules/cstrike/cstrike/CstrikeHLTypeConversion.cpp deleted file mode 100644 index ebf59ad8..00000000 --- a/modules/cstrike/cstrike/CstrikeHLTypeConversion.cpp +++ /dev/null @@ -1,119 +0,0 @@ -// -// 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 -// - -#include "CstrikeHLTypeConversion.h" - -OffsetHandler* G_OffsetHandler; -HL_TypeConversion G_HL_TypeConversion; - -void OffsetHandler::search_pev() -{ - edict_t *pEdict = INDEXENT(0); - entvars_t *pev = VARS(pEdict); - - byte *privateData = reinterpret_cast(pEdict->pvPrivateData); - - for (int i = 0; i < 0xFFF; i++) - { - entvars_t *val = *(reinterpret_cast(privateData + i)); - - if (val == pev) - { - this->pev = i; - return; - } - } - - // This should not happen. - this->pev = 0; -} - -inline edict_t* HL_TypeConversion::INDEXENT2(int iEdictNum) -{ - if (iEdictNum >= 1 && iEdictNum <= gpGlobals->maxClients) - { - return MF_GetPlayerEdict(iEdictNum); - } - else - { - return (*g_engfuncs.pfnPEntityOfEntIndex)(iEdictNum); - } -} - -edict_t* HL_TypeConversion::entvar_to_edict(entvars_t *pev) -{ - if (!pev) - { - return nullptr; - } - - return pev->pContainingEntity; -} - -int HL_TypeConversion::entvar_to_id(entvars_t *pev) -{ - if (!pev) - { - return -1; - } - - if (!pev->pContainingEntity) - { - return -1; - } - - return ENTINDEX(pev->pContainingEntity); -} - -void* HL_TypeConversion::id_to_cbase(int index) -{ - edict_t* edict = INDEXENT2(index); - return edict ? edict->pvPrivateData : nullptr; -} - -entvars_t* HL_TypeConversion::id_to_entvar(int index) -{ - edict_t *pEdict = INDEXENT2(index); - return pEdict ? VARS(pEdict) : nullptr; -} - -entvars_t* HL_TypeConversion::cbase_to_entvar(void* cbase) -{ - if (!cbase) - { - return nullptr; - } - - return *reinterpret_cast(reinterpret_cast(cbase) + G_OffsetHandler->pev); -} - -int HL_TypeConversion::cbase_to_id(void *cbase) -{ - if (!cbase) - { - return -1; - } - - entvars_t *pev = this->cbase_to_entvar(cbase); - - if (!pev) - { - return -1; - } - - if (!pev->pContainingEntity || FNullEnt(pev->pContainingEntity)) - { - return -1; - } - - return ENTINDEX(pev->pContainingEntity); -} diff --git a/modules/cstrike/cstrike/CstrikeHLTypeConversion.h b/modules/cstrike/cstrike/CstrikeHLTypeConversion.h deleted file mode 100644 index 327878e3..00000000 --- a/modules/cstrike/cstrike/CstrikeHLTypeConversion.h +++ /dev/null @@ -1,49 +0,0 @@ -// -// 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 HL_TYPE_CONVERSION_H -#define HL_TYPE_CONVERSION_H - -#include "amxxmodule.h" - -struct OffsetHandler -{ - int pev; - - void search_pev(); - - OffsetHandler() - { - search_pev(); - } -}; - -class HL_TypeConversion -{ - public: - - inline edict_t* INDEXENT2(int iEdictNum); - - edict_t* entvar_to_edict(entvars_t *pev); - int entvar_to_id(entvars_t *pev); - - void* id_to_cbase(int index); - int cbase_to_id(void *cbase); - - entvars_t* id_to_entvar(int index); - entvars_t* cbase_to_entvar(void* cbase); -}; - -extern OffsetHandler* G_OffsetHandler; -extern HL_TypeConversion G_HL_TypeConversion; - -#endif // HL_TYPE_CONVERSION_H diff --git a/modules/cstrike/cstrike/CstrikeHacks.cpp b/modules/cstrike/cstrike/CstrikeHacks.cpp index 8beb6bb7..f81f804c 100644 --- a/modules/cstrike/cstrike/CstrikeHacks.cpp +++ b/modules/cstrike/cstrike/CstrikeHacks.cpp @@ -14,7 +14,6 @@ #include "CstrikeDatas.h" #include "CstrikeUtils.h" #include "CstrikeHacks.h" -#include "CstrikeHLTypeConversion.h" #include void CtrlDetours_ClientCommand(bool set); @@ -213,7 +212,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 = G_HL_TypeConversion.cbase_to_id(this); + int client = TypeConversion.cbase_to_id(this); if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast(client), static_cast(CurrentItemId)) > 0) { @@ -234,7 +233,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 = G_HL_TypeConversion.cbase_to_id(this); + int client = TypeConversion.cbase_to_id(this); if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnBuy, static_cast(client), CSI_SHIELDGUN) > 0) { diff --git a/modules/cstrike/cstrike/CstrikeMain.cpp b/modules/cstrike/cstrike/CstrikeMain.cpp index 05bfe478..1313bc36 100644 --- a/modules/cstrike/cstrike/CstrikeMain.cpp +++ b/modules/cstrike/cstrike/CstrikeMain.cpp @@ -13,16 +13,15 @@ #include "amxxmodule.h" #include "CstrikeUtils.h" -#include "CstrikeDatas.h" #include "CstrikeHacks.h" -#include "CstrikeHLTypeConversion.h" #include -#include "engine_strucs.h" IGameConfig *MainConfig; IGameConfig *CommonConfig; IGameConfigManager *ConfigManager; +HLTypeConversion TypeConversion; + int AmxxCheckGame(const char *game) { if (strcasecmp(game, "cstrike") == 0 || @@ -75,11 +74,8 @@ void OnPluginsLoaded() ToggleDetour_ClientCommands(ForwardInternalCommand != -1 || ForwardOnBuy != -1 || ForwardOnBuyAttempt != -1); ToggleDetour_BuyCommands(ForwardOnBuy != -1); - // Search pev offset automatically. - if (!G_OffsetHandler) - { - G_OffsetHandler = new OffsetHandler; - } + // Search pev/vtable offset automatically. + TypeConversion.init(); // Used with model natives, enabled on demand. g_pengfuncsTable->pfnSetClientKeyValue = nullptr; diff --git a/modules/cstrike/cstrike/CstrikeNatives.cpp b/modules/cstrike/cstrike/CstrikeNatives.cpp index ed8aee2d..e1419a98 100644 --- a/modules/cstrike/cstrike/CstrikeNatives.cpp +++ b/modules/cstrike/cstrike/CstrikeNatives.cpp @@ -16,9 +16,7 @@ #include "CstrikeUtils.h" #include "CstrikeHacks.h" #include "CstrikeUserMessages.h" -#include "CstrikeHLTypeConversion.h" #include -#include #include bool NoKifesMode = false; @@ -999,14 +997,14 @@ static cell AMX_NATIVE_CALL cs_set_hostage_follow(AMX *amx, cell *params) GET_OFFSET("CHostageImprov", m_idleState); GET_OFFSET("HostageFollowState", m_leader); GET_OFFSET("SimpleStateMachine", m_state); // +4 for virtual table pointer of IImprovEvent. - GET_OFFSET("SimpleStateMachine", m_stateTimer); // + GET_OFFSET("SimpleStateMachine", m_stateTimer); // if (target) { set_pdata(pImprov, m_behavior + 4 + m_state, reinterpret_cast(pImprov) + m_followState); set_pdata(pImprov, m_behavior + 4 + m_stateTimer, gpGlobals->time); - get_pdata(pImprov, m_followState + m_leader).Set(GETEDICT(target)); + get_pdata(pImprov, m_followState + m_leader).Set(TypeConversion.id_to_edict(target)); } else { @@ -1018,7 +1016,7 @@ static cell AMX_NATIVE_CALL cs_set_hostage_follow(AMX *amx, cell *params) } else { - get_pdata(pHostage, m_hTargetEnt).Set(target ? GETEDICT(target) : nullptr); + get_pdata(pHostage, m_hTargetEnt).Set(target ? TypeConversion.id_to_edict(target) : nullptr); } return 1; @@ -1728,10 +1726,10 @@ static cell AMX_NATIVE_CALL cs_find_ent_by_class(AMX* amx, cell* params) } int len; - void* pEntity = G_HL_TypeConversion.id_to_cbase(params[1]); + void* pEntity = TypeConversion.id_to_cbase(params[1]); const char* value = MF_GetAmxString(amx, params[2], 0, &len); - int index = G_HL_TypeConversion.cbase_to_id(CS_UTIL_FindEntityByString(pEntity, "classname", value)); + int index = TypeConversion.cbase_to_id(CS_UTIL_FindEntityByString(pEntity, "classname", value)); if (index != -1) { diff --git a/modules/cstrike/cstrike/CstrikeUtils.h b/modules/cstrike/cstrike/CstrikeUtils.h index 14e19901..ac94a113 100644 --- a/modules/cstrike/cstrike/CstrikeUtils.h +++ b/modules/cstrike/cstrike/CstrikeUtils.h @@ -14,6 +14,10 @@ #ifndef CSTRIKE_UTILS_H #define CSTRIKE_UTILS_H +#include + +extern HLTypeConversion TypeConversion; + bool UTIL_IsPlayer(edict_t *pPlayer); void UTIL_TextMsg_Generic(edict_t* pPlayer, const char* message); bool UTIL_CheckForPublic(const char *publicname); @@ -70,10 +74,6 @@ bool UTIL_CheckForPublic(const char *publicname); return 0; \ } -#define GETEDICT(n) \ - ((n >= 1 && n <= gpGlobals->maxClients) ? MF_GetPlayerEdict(n) : INDEXENT(n)) - - #define GET_OFFSET(classname, member) \ static int member = -1; \ if (member == -1) \ @@ -99,71 +99,6 @@ bool UTIL_CheckForPublic(const char *publicname); member = type.fieldOffset; \ } -template -inline T& get_pdata(edict_t *pEntity, int offset, int element = 0) -{ - return *reinterpret_cast(reinterpret_cast(pEntity->pvPrivateData) + offset + element * sizeof(T)); -} - -template -inline T& get_pdata(void *pEntity, int offset, int element = 0) -{ - return *reinterpret_cast(reinterpret_cast(pEntity) + offset + element * sizeof(T)); -} - -template -inline void set_pdata(edict_t *pEntity, int offset, T value, int element = 0) -{ - *reinterpret_cast(reinterpret_cast(pEntity->pvPrivateData) + offset + element * sizeof(T)) = value; -} - -template -inline void set_pdata(void *pEntity, int offset, T value, int element = 0) -{ - *reinterpret_cast(reinterpret_cast(pEntity) + offset + element * sizeof(T)) = value; -} - -class EHANDLE -{ - private: - - edict_t* m_pent; - int m_serialnumber; - - public: - - edict_t* Get(void) - { - if (!FNullEnt(m_pent)) - { - if (m_pent->serialnumber == m_serialnumber) - { - return m_pent; - } - - return nullptr; - } - - return nullptr; - }; - - edict_t* Set(edict_t *pent) - { - if (!FNullEnt(pent)) - { - m_pent = pent; - m_serialnumber = m_pent->serialnumber; - } - else - { - m_pent = nullptr; - m_serialnumber = 0; - } - - return pent; - }; -}; - class CUnifiedSignals { public: diff --git a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj index 88bb6b77..e50b7f14 100644 --- a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj +++ b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj @@ -146,7 +146,6 @@ - diff --git a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters index 1a91cf72..33e30dbe 100644 --- a/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters +++ b/modules/cstrike/cstrike/msvc12/cstrike.vcxproj.filters @@ -53,9 +53,6 @@ Module SDK\SDK Base - - Source Files - Source Files