From 88f0a66c0bc45bdb62ad356f09d0d424568e48a3 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 3 May 2016 22:56:07 +0600 Subject: [PATCH] Changed work native rg_find_ent_by_owner. Added edictByIndexAmx for safe use. --- .../scripting/include/reapi_gamedll.inc | 4 +- .../scripting/include/reapi_gamedll_const.inc | 20 ++++---- reapi/src/hook_callback.cpp | 2 +- reapi/src/natives/natives_hookchains.cpp | 4 +- reapi/src/natives/natives_members.cpp | 20 ++++---- reapi/src/natives/natives_misc.cpp | 46 +++++++++++-------- reapi/src/natives/natives_misc.h | 14 +++--- reapi/src/reapi_utils.h | 18 +++++--- 8 files changed, 71 insertions(+), 57 deletions(-) diff --git a/reapi/extra/amxmodx/scripting/include/reapi_gamedll.inc b/reapi/extra/amxmodx/scripting/include/reapi_gamedll.inc index a6e65ce..275c191 100644 --- a/reapi/extra/amxmodx/scripting/include/reapi_gamedll.inc +++ b/reapi/extra/amxmodx/scripting/include/reapi_gamedll.inc @@ -228,10 +228,10 @@ native rg_find_ent_by_class(start_index, const classname[]); * @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 +* @return 1 if found, 0 otherwise * */ -native rg_find_ent_by_owner(start_index, const classname[], owner); +native rg_find_ent_by_owner(&start_index, const classname[], owner); /** * Returns some information about a weapon. diff --git a/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc b/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc index ce1e3dc..3383611 100644 --- a/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc +++ b/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc @@ -11,6 +11,10 @@ #define SIGNAL_ESCAPE (1<<3) #define SIGNAL_VIPSAFETY (1<<4) +// Returns 1, if round ended by expired time +// NOTE: Use this for hookchain RG_RoundEnd with the parametr ScenarioEventEndRound:event +#define HadRoundExpired(event) (1<callNext(_recipients, INDEXENT(_entity), _channel, _sample, _volume, _attenuation, _fFlags, _pitch); + chain->callNext(_recipients, edictByIndexAmx(_entity), _channel, _sample, _volume, _attenuation, _fFlags, _pitch); }; callVoidForward(RH_SV_StartSound, original, recipients, indexOfEdict(entity), channel, sample, volume, attenuation, fFlags, pitch); diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index 6cf124b..98db2c3 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -148,7 +148,7 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params) break; } case ATYPE_CLASSPTR: - retVal._classptr = CBaseEntity::Instance(INDEXENT(*srcAddr)); + retVal._classptr = getPrivate(*srcAddr); break; default: return FALSE; @@ -258,7 +258,7 @@ cell AMX_NATIVE_CALL SetHookChainArg(AMX *amx, cell *params) *(char **)destAddr = getAmxStringTemp(srcAddr, temp_strings[number], sizeof temp_strings[0] - 1); break; case ATYPE_CLASSPTR: - *(CBaseEntity **)destAddr = CBaseEntity::Instance(INDEXENT(*srcAddr)); + *(CBaseEntity **)destAddr = getPrivate(*srcAddr); break; } diff --git a/reapi/src/natives/natives_members.cpp b/reapi/src/natives/natives_members.cpp index 9d9b268..fa98f8e 100644 --- a/reapi/src/natives/natives_members.cpp +++ b/reapi/src/natives/natives_members.cpp @@ -11,7 +11,7 @@ cell AMX_NATIVE_CALL set_member(AMX *amx, cell *params) return FALSE; } - edict_t *pEdict = INDEXENT(params[arg_index]); + edict_t *pEdict = edictByIndexAmx(params[arg_index]); if (pEdict == nullptr || pEdict->pvPrivateData == nullptr) { MF_LogError(amx, AMX_ERR_NATIVE, "%s: invalid or uninitialized entity", __FUNCTION__); return FALSE; @@ -34,7 +34,7 @@ cell AMX_NATIVE_CALL get_member(AMX *amx, cell *params) return FALSE; } - edict_t *pEdict = INDEXENT(params[arg_index]); + edict_t *pEdict = edictByIndexAmx(params[arg_index]); if (pEdict == nullptr || pEdict->pvPrivateData == nullptr) { MF_LogError(amx, AMX_ERR_NATIVE, "%s: invalid or uninitialized entity", __FUNCTION__); return FALSE; @@ -155,7 +155,7 @@ BOOL set_member(void* pdata, const member_t *member, size_t element, cell* value case MEMBER_CLASSPTR: { // native set_member(_index, any:_member, _value, _elem); - CBaseEntity *pEntity = CBaseEntity::Instance(INDEXENT(*value)); + CBaseEntity *pEntity = getPrivate(*value); set_member(pdata, member->offset, pEntity, element); return TRUE; } @@ -163,14 +163,14 @@ BOOL set_member(void* pdata, const member_t *member, size_t element, cell* value { // native set_member(_index, any:_member, _value, _elem); EHANDLE& ehandle = get_member(pdata, member->offset, element); - edict_t *pEdictValue = INDEXENT(*value); + edict_t *pEdictValue = edictByIndexAmx(*value); ehandle.Set(pEdictValue); return TRUE; } case MEMBER_EDICT: { // native set_member(_index, any:_member, _value, _elem); - edict_t *pEdictValue = INDEXENT(*value); + edict_t *pEdictValue = edictByIndexAmx(*value); set_member(pdata, member->offset, pEdictValue, element); return TRUE; } @@ -261,7 +261,7 @@ cell get_member(void* pdata, const member_t *member, size_t element, cell* dest) EHANDLE ehandle = get_member(pdata, member->offset, element); edict_t *pEntity = ehandle.Get(); if (pEntity != nullptr) { - return ENTINDEX(pEntity); + return indexOfEdict(pEntity); } return -1; } @@ -270,7 +270,7 @@ cell get_member(void* pdata, const member_t *member, size_t element, cell* dest) // native any:get_member(_index, any:_member, element); edict_t *pEntity = get_member(pdata, member->offset, element); if (pEntity != nullptr) { - return ENTINDEX(pEntity); + return indexOfEdict(pEntity); } return -1; @@ -281,11 +281,7 @@ cell get_member(void* pdata, const member_t *member, size_t element, cell* dest) if (!dest) return 0; - cell* vecSrc = get_member_direct(pdata, member->offset, element); - - dest[0] = vecSrc[0]; - dest[1] = vecSrc[1]; - dest[2] = vecSrc[2]; + dest = get_member_direct(pdata, member->offset, element); return 1; } case MEMBER_STRING: diff --git a/reapi/src/natives/natives_misc.cpp b/reapi/src/natives/natives_misc.cpp index b9216a5..7dad29c 100644 --- a/reapi/src/natives/natives_misc.cpp +++ b/reapi/src/natives/natives_misc.cpp @@ -422,7 +422,6 @@ cell AMX_NATIVE_CALL rg_update_teamscores(AMX *amx, cell *params) */ cell AMX_NATIVE_CALL rg_create_entity(AMX *amx, cell *params) { - //g_ReGameFuncs->CREATE_NAMED_ENTITY2(); enum args_e { arg_count, arg_classname }; string_t iClass = g_engfuncs.pfnAllocString(getAmxString(amx, params[arg_classname])); @@ -430,7 +429,7 @@ cell AMX_NATIVE_CALL rg_create_entity(AMX *amx, cell *params) if (pEnt != nullptr) { - return ENTINDEX(pEnt); + return indexOfEdict(pEnt); } return 0; @@ -468,9 +467,9 @@ cell AMX_NATIVE_CALL rg_find_ent_by_class(AMX *amx, cell *params) * @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 +* @return 1 if found, 0 otherwise * -* native rg_find_ent_by_owner(start_index, const classname[], owner); +* native rg_find_ent_by_owner(&start_index, const classname[], owner); */ cell AMX_NATIVE_CALL rg_find_ent_by_owner(AMX *amx, cell *params) { @@ -478,24 +477,35 @@ cell AMX_NATIVE_CALL rg_find_ent_by_owner(AMX *amx, cell *params) CHECK_ISENTITY(arg_onwer); + cell& startIndex = *getAmxAddr(amx, params[arg_start_index]); const char* value = getAmxString(amx, params[arg_classname]); - edict_t *pOwner = edictByIndex(params[arg_onwer]); - edict_t *pEntity = g_pEdicts; + edict_t* pOwner = edictByIndexAmx(params[arg_onwer]); + edict_t* pEntity = &g_pEdicts[startIndex]; - for (int i = 0; i < gpGlobals->maxEntities; i++, pEntity++) + for (int i = startIndex; i < gpGlobals->maxEntities; i++, pEntity++) { - if (pEntity->v.owner == pOwner && !strcmp(STRING(pEntity->v.classname), value)) - return i; + if (pEntity->v.owner != pOwner) + continue; + + // yet not allocated + if (!pEntity->pvPrivateData || pEntity->free) + continue; + + if (!strcmp(STRING(pEntity->v.classname), value)) + { + startIndex = i; + return TRUE; + } } - return 0; + return FALSE; } /** * Returns some information about a weapon. * * @param weapon_id Weapon id, see WEAPON_* constants -* @param type Info type, see WPINFO_* constants +* @param type Info type, see WI_* constants * * @return Weapon information value * @error If weapon_id and type are out of bound, an error will be thrown. @@ -518,19 +528,19 @@ cell AMX_NATIVE_CALL rg_get_weapon_info(AMX *amx, cell *params) switch (info_type) { - case WPINFO_COST: + case WI_COST: return info->cost; - case WPINFO_CLIP_COST: + case WI_CLIP_COST: return info->clipCost; - case WPINFO_BUY_CLIP_SIZE: + case WI_BUY_CLIP_SIZE: return info->buyClipSize; - case WPINFO_GUN_CLIP_SIZE: + case WI_GUN_CLIP_SIZE: return info->gunClipSize; - case WPINFO_MAX_ROUNDS: + case WI_MAX_ROUNDS: return info->maxRounds; - case WPINFO_AMMO_TYPE: + case WI_AMMO_TYPE: return info->ammoType; - case WPINFO_NAME: + case WI_NAME: { if (PARAMS_COUNT != arg_4) { MF_LogError(amx, AMX_ERR_NATIVE, "%s: bad parameter count, got %i, expected %i", __FUNCTION__, PARAMS_COUNT, arg_4); diff --git a/reapi/src/natives/natives_misc.h b/reapi/src/natives/natives_misc.h index e2ca32b..fd6154b 100644 --- a/reapi/src/natives/natives_misc.h +++ b/reapi/src/natives/natives_misc.h @@ -2,13 +2,13 @@ enum WpnInfo { - WPINFO_COST, - WPINFO_CLIP_COST, - WPINFO_BUY_CLIP_SIZE, - WPINFO_GUN_CLIP_SIZE, - WPINFO_MAX_ROUNDS, - WPINFO_AMMO_TYPE, - WPINFO_NAME + WI_COST, + WI_CLIP_COST, + WI_BUY_CLIP_SIZE, + WI_GUN_CLIP_SIZE, + WI_MAX_ROUNDS, + WI_AMMO_TYPE, + WI_NAME }; void RegisterNatives_Misc(); diff --git a/reapi/src/reapi_utils.h b/reapi/src/reapi_utils.h index d70b548..825f521 100644 --- a/reapi/src/reapi_utils.h +++ b/reapi/src/reapi_utils.h @@ -19,7 +19,15 @@ inline size_t indexOfEdict(entvars_t* pev) return indexOfEdict(pev->pContainingEntity); } -inline edict_t* edictByIndex(size_t index) +// safe to index -1 +inline edict_t* edictByIndexAmx(int index) +{ + auto ed = g_pEdicts + index; + return index < 0 ? nullptr : ed; +} + +// fast +inline edict_t* edictByIndex(int index) { return g_pEdicts + index; } @@ -27,16 +35,12 @@ inline edict_t* edictByIndex(size_t index) template T* getPrivate(int index) { - edict_t* pent = edictByIndex(index); - if (pent) - return (T *)pent->pvPrivateData; - - return nullptr; + return (T *)GET_PRIVATE(edictByIndexAmx(index)); } inline entvars_t* PEV(int index) { - return &edictByIndex(index)->v; + return VARS(edictByIndexAmx(index)); } // HLTypeConversion.h -> AMXModX