Consistency: Replace GETEDICT and variants with HLTypeConversion in cstrike module

This commit is contained in:
Arkshine 2015-10-07 17:41:54 +02:00
parent 6f301b5cea
commit b65a0600ee
10 changed files with 15 additions and 262 deletions

View File

@ -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',

View File

@ -14,8 +14,6 @@
#ifndef CSTRIKE_DATA_H
#define CSTRIKE_DATA_H
#include "amxxmodule.h"
/**
* Weapon Ids for use with CS_OnBuyAttempt(), CS_OnBuy().
*/

View File

@ -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<byte*>(pEdict->pvPrivateData);
for (int i = 0; i < 0xFFF; i++)
{
entvars_t *val = *(reinterpret_cast<entvars_t**>(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<entvars_t**>(reinterpret_cast<int8*>(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);
}

View File

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

View File

@ -14,7 +14,6 @@
#include "CstrikeDatas.h"
#include "CstrikeUtils.h"
#include "CstrikeHacks.h"
#include "CstrikeHLTypeConversion.h"
#include <sm_stringhashmap.h>
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<cell>(client), static_cast<cell>(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<cell>(client), CSI_SHIELDGUN) > 0)
{

View File

@ -13,16 +13,15 @@
#include "amxxmodule.h"
#include "CstrikeUtils.h"
#include "CstrikeDatas.h"
#include "CstrikeHacks.h"
#include "CstrikeHLTypeConversion.h"
#include <IGameConfigs.h>
#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;

View File

@ -16,9 +16,7 @@
#include "CstrikeUtils.h"
#include "CstrikeHacks.h"
#include "CstrikeUserMessages.h"
#include "CstrikeHLTypeConversion.h"
#include <CDetour/detours.h>
#include <amtl/am-vector.h>
#include <amtl/am-string.h>
bool NoKifesMode = false;
@ -1006,7 +1004,7 @@ static cell AMX_NATIVE_CALL cs_set_hostage_follow(AMX *amx, cell *params)
set_pdata<void*>(pImprov, m_behavior + 4 + m_state, reinterpret_cast<int8*>(pImprov) + m_followState);
set_pdata<float>(pImprov, m_behavior + 4 + m_stateTimer, gpGlobals->time);
get_pdata<EHANDLE>(pImprov, m_followState + m_leader).Set(GETEDICT(target));
get_pdata<EHANDLE>(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<EHANDLE>(pHostage, m_hTargetEnt).Set(target ? GETEDICT(target) : nullptr);
get_pdata<EHANDLE>(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)
{

View File

@ -14,6 +14,10 @@
#ifndef CSTRIKE_UTILS_H
#define CSTRIKE_UTILS_H
#include <HLTypeConversion.h>
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 <typename T>
inline T& get_pdata(edict_t *pEntity, int offset, int element = 0)
{
return *reinterpret_cast<T*>(reinterpret_cast<int8*>(pEntity->pvPrivateData) + offset + element * sizeof(T));
}
template <typename T>
inline T& get_pdata(void *pEntity, int offset, int element = 0)
{
return *reinterpret_cast<T*>(reinterpret_cast<int8*>(pEntity) + offset + element * sizeof(T));
}
template <typename T>
inline void set_pdata(edict_t *pEntity, int offset, T value, int element = 0)
{
*reinterpret_cast<T*>(reinterpret_cast<int8*>(pEntity->pvPrivateData) + offset + element * sizeof(T)) = value;
}
template <typename T>
inline void set_pdata(void *pEntity, int offset, T value, int element = 0)
{
*reinterpret_cast<T*>(reinterpret_cast<int8*>(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:

View File

@ -146,7 +146,6 @@
<ClCompile Include="..\..\..\..\public\memtools\MemoryUtils.cpp" />
<ClCompile Include="..\CstrikeMain.cpp" />
<ClCompile Include="..\CstrikeHacks.cpp" />
<ClCompile Include="..\CstrikeHLTypeConversion.cpp" />
<ClCompile Include="..\CstrikeNatives.cpp" />
<ClCompile Include="..\CstrikePlayer.cpp" />
<ClCompile Include="..\CstrikeUserMessages.cpp" />

View File

@ -53,9 +53,6 @@
<ClCompile Include="..\..\..\..\public\sdk\amxxmodule.cpp">
<Filter>Module SDK\SDK Base</Filter>
</ClCompile>
<ClCompile Include="..\CstrikeHLTypeConversion.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\CstrikeUserMessages.cpp">
<Filter>Source Files</Filter>
</ClCompile>