Add cs_find_ent_by_class and fix documentation

This commit is contained in:
Arkshine 2015-01-30 15:53:25 +01:00
parent 2446ff49f2
commit c87d46ad5b
8 changed files with 254 additions and 26 deletions

View File

@ -14,6 +14,7 @@ binary.sources = [
'CstrikeNatives.cpp',
'CstrikeHacks.cpp',
'CstrikeUtils.cpp',
'CstrikeHLTypeConversion.cpp',
'../../../public/memtools/MemoryUtils.cpp',
'../../../public/memtools/CDetour/detours.cpp',
'../../../public/memtools/CDetour/asm/asm.c',

View File

@ -141,23 +141,26 @@
#endif
#if defined(__linux__)
#define CS_IDENT_GIVENSHIELD "_ZN11CBasePlayer10GiveShieldEb" // CS_OnBuy forward
#define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc" // CS_OnBuy forward
#define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib" // CS_OnBuy forward
#define CS_IDENT_CREATENAMEDENTITY "_Z19CREATE_NAMED_ENTITYj" // cs_create_entity
#define CS_IDENT_HIDDEN_STATE false
#define CS_IDENT_GIVENSHIELD "_ZN11CBasePlayer10GiveShieldEb" // CS_OnBuy forward
#define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc" // CS_OnBuy forward
#define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib" // CS_OnBuy forward
#define CS_IDENT_CREATENAMEDENTITY "_Z19CREATE_NAMED_ENTITYj" // cs_create_entity
#define CS_IDENTI_UTIL_FINDENTITYBYSTRING "_Z23UTIL_FindEntityByStringP11CBaseEntityPKcS2_" // cs_create_entity
#define CS_IDENT_HIDDEN_STATE false
#elif defined(__APPLE__)
#define CS_IDENT_GIVENSHIELD "_ZN11CBasePlayer10GiveShieldEb"
#define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc"
#define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib"
#define CS_IDENT_CREATENAMEDENTITY "_Z19CREATE_NAMED_ENTITYj"
#define CS_IDENT_HIDDEN_STATE true
#define CS_IDENT_GIVENSHIELD "_ZN11CBasePlayer10GiveShieldEb"
#define CS_IDENT_GIVENAMEDITEM "_ZN11CBasePlayer13GiveNamedItemEPKc"
#define CS_IDENT_ADDACCOUNT "_ZN11CBasePlayer10AddAccountEib"
#define CS_IDENT_CREATENAMEDENTITY "_Z19CREATE_NAMED_ENTITYj"
#define CS_IDENTI_UTIL_FINDENTITYBYSTRING "_Z23UTIL_FindEntityByStringP11CBaseEntityPKcS2_"
#define CS_IDENT_HIDDEN_STATE true
#elif defined(WIN32)
#define CS_IDENT_GIVENSHIELD "\\x56\\x8B\\x2A\\x57\\x33\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\xB0"
#define CS_IDENT_GIVENAMEDITEM "\\x8B\\x2A\\x2A\\x2A\\x56\\x57\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x2B"
#define CS_IDENT_ADDACCOUNT "\\x8B\\x2A\\x2A\\x2A\\x56\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x03"
#define CS_IDENT_CREATENAMEDENTITY "\\x56\\x57\\x8B\\x2A\\x2A\\x2A\\x57\\xFF\\x2A\\x2A\\x2A\\x2A\\x2A\\x8B"
#define CS_IDENT_HIDDEN_STATE false
#define CS_IDENT_GIVENSHIELD "\\x56\\x8B\\x2A\\x57\\x33\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\xB0"
#define CS_IDENT_GIVENAMEDITEM "\\x8B\\x2A\\x2A\\x2A\\x56\\x57\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x2B"
#define CS_IDENT_ADDACCOUNT "\\x8B\\x2A\\x2A\\x2A\\x56\\x8B\\x2A\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x03"
#define CS_IDENT_CREATENAMEDENTITY "\\x56\\x57\\x8B\\x2A\\x2A\\x2A\\x57\\xFF\\x2A\\x2A\\x2A\\x2A\\x2A\\x8B"
#define CS_IDENT_UTIL_FINDENTITYBYSTRING "\\x51\\x8B\\x2A\\x2A\\x2A\\x53\\x55\\x56\\x85\\x2A\\x57"
#define CS_IDENT_HIDDEN_STATE false
#endif
#define CSI_P228 CSW_P228
@ -349,6 +352,7 @@ typedef enum
} Menu;
typedef edict_t* (*CreateNamedEntityFunc)(string_t);
typedef edict_t* (*CreateNamedEntityFunc)(string_t iszClassname);
typedef void* (*UTIL_FindEntityByStringFunc)(void* pStartEntity, const char *szKeyword, const char *szValue);
#endif // CSTRIKE_DATA_H

View File

@ -0,0 +1,118 @@
//
// 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* edict = INDEXENT(0);
entvars_t* entvars = &edict->v;
byte* private_c = (byte*)edict->pvPrivateData;
for (int i = 0; i < 0xFFF; i++)
{
uintptr_t val = *((uintptr_t*)(private_c + i));
if (val == (uintptr_t)entvars)
{
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)
{
return &(INDEXENT2(index)->v);
}
entvars_t* HL_TypeConversion::cbase_to_entvar(void* cbase)
{
if (!cbase)
{
return nullptr;
}
return *(entvars_t **)((char *)(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

@ -0,0 +1,49 @@
//
// 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,6 +14,7 @@
#include "CstrikeDatas.h"
#include "CstrikePlayer.h"
#include "CstrikeUtils.h"
#include "CstrikeHLTypeConversion.h"
CCstrikePlayer g_players[33];
int g_zooming[33] = {0};
@ -1708,6 +1709,7 @@ static cell AMX_NATIVE_CALL cs_set_c4_defusing(AMX* amx, cell* params)
}
extern CreateNamedEntityFunc CS_CreateNamedEntity;
extern UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString;
// cs_create_entity(const classname[])
static cell AMX_NATIVE_CALL cs_create_entity(AMX* amx, cell* params)
@ -1728,6 +1730,26 @@ static cell AMX_NATIVE_CALL cs_create_entity(AMX* amx, cell* params)
return 0;
}
// cs_find_ent_by_class(start_index, const classname[])
static cell AMX_NATIVE_CALL cs_find_ent_by_class(AMX* amx, cell* params)
{
if (CS_UTIL_FindEntityByString > 0)
{
int len;
void* pEntity = G_HL_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));
if (index != -1)
{
return index;
}
}
return 0;
}
#else
@ -1814,6 +1836,7 @@ AMX_NATIVE_INFO CstrikeNatives[] = {
{"cs_get_c4_defusing", cs_get_c4_defusing},
{"cs_set_c4_defusing", cs_set_c4_defusing},
{"cs_create_entity", cs_create_entity },
{"cs_find_ent_by_class", cs_find_ent_by_class },
{NULL, NULL}
};

View File

@ -15,7 +15,7 @@ PUBLIC_ROOT = ../../../public
PROJECT = cstrike
OBJECTS = amxxmodule.cpp amxx_api.cpp CstrikePlayer.cpp CstrikeNatives.cpp CstrikeHacks.cpp CstrikeUtils.cpp \
OBJECTS = amxxmodule.cpp amxx_api.cpp CstrikePlayer.cpp CstrikeNatives.cpp CstrikeHacks.cpp CstrikeUtils.cpp CstrikeHLTypeConversion.cpp \
MemoryUtils.cpp detours.cpp asm.c
##############################################

View File

@ -14,6 +14,7 @@
#include "amxxmodule.h"
#include "CstrikeUtils.h"
#include "CstrikeDatas.h"
#include "CstrikeHLTypeConversion.h"
extern AMX_NATIVE_INFO CstrikeNatives[];
@ -26,7 +27,8 @@ void ShutdownHacks();
void ToggleDetour_ClientCommands(bool enable);
void ToggleDetour_BuyCommands(bool enable);
CreateNamedEntityFunc CS_CreateNamedEntity = NULL;
CreateNamedEntityFunc CS_CreateNamedEntity = nullptr;
UTIL_FindEntityByStringFunc CS_UTIL_FindEntityByString = nullptr;
int AmxxCheckGame(const char *game)
{
@ -44,8 +46,14 @@ void OnAmxxAttach()
InitializeHacks();
// cs_create_entity().
// cs_create_entity()
CS_CreateNamedEntity = reinterpret_cast<CreateNamedEntityFunc>(UTIL_FindAddressFromEntry(CS_IDENT_CREATENAMEDENTITY, CS_IDENT_HIDDEN_STATE));
// cs_find_ent_by_class()
CS_UTIL_FindEntityByString = reinterpret_cast<UTIL_FindEntityByStringFunc>(UTIL_FindAddressFromEntry(CS_IDENT_UTIL_FINDENTITYBYSTRING, CS_IDENT_HIDDEN_STATE));
// Search pev offset automatically.
G_OffsetHandler = new OffsetHandler;
}
void OnPluginsLoaded()

View File

@ -351,14 +351,20 @@ native bool:cs_get_c4_defusing(c4index);
native cs_set_c4_defusing(c4index, bool:defusing);
/**
* Creates an entity.
* Creates an entity using Counter-Strike's custom CreateNamedEntity wrapper.
*
* @note Similar as create_entity() native from engine module, with the difference that in Counter-Strike
* to improve lookup performance on entities, the classname is hashed and saved in a global list.
* This means for classname, game uses a custom function which checks that list instead of calling pfnFindEntityByString.
* You would want to use this native if you want having custom entities to be known by the game.
* E.g. By creating a "weaponbox" entity, this will be automatically deleted by game on map restart.
* With engine version, you would have to remove it manually.
* @note Unlike other mods CS keeps track of entities using a custom hashtable.
* This function will add entities to this hashtable, providing benefits
* over the default engine functions (used by create_entity() for example):
* - Storing entities in a hashtable allows CS to improve classname lookup
* performance compared to functions like FindEntityByString that usually
* have to loop incrementally through all entities.
* - As CS exclusively uses the hashtable for classname lookup, entities
* created using the default engine functions will not be found by the
* game. For example "weaponbox" entities are supposed to be
* automatically cleaned up on round restart but are not considered if
* they have not been added to the hashtable.
* @note CS's faster hashtable lookup can be utilized with cs_find_ent_by_class()
*
* @param classname Entity class name
*
@ -366,6 +372,25 @@ native cs_set_c4_defusing(c4index, bool:defusing);
*/
native cs_create_entity(const classname[]);
/**
* Finds an entity in the world using Counter-Strike's custom FindEntityByString
* wrapper.
*
* @note Unlike other mods CS keeps track of entities using a custom hashtable.
* This function utilizes the custom hasthable and allows for considerably
* faster classname lookup compared to the default engine functions (used
* by find_ent_by_class() for example).
* @note This exclusively considers entities in the hashtable, created by the
* game itself or using cs_create_entity().
*
* @param start_index Entity index to start searching from. -1 to start from
* the first entity
* @param classname Classname to search for
*
* @return Entity index > 0 if found, 0 otherwise
*/
native cs_find_ent_by_class(start_index, const classname[]);
/**
* Called when CS internally fires a command to a player. It does this for a few
* functions, most notably rebuy/autobuy functionality. This is also used to pass