2
0
mirror of https://github.com/rehlds/reapi.git synced 2025-01-16 00:28:17 +03:00

Changed work native rg_find_ent_by_owner.

Added edictByIndexAmx for safe use.
This commit is contained in:
s1lentq 2016-05-03 22:56:07 +06:00
parent c1c59ce26c
commit 88f0a66c0b
8 changed files with 71 additions and 57 deletions

View File

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

View File

@ -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<<event) & (1<<ROUND_TARGET_SAVED) | (1<<ROUND_HOSTAGE_NOT_RESCUED) | (1<<ROUND_TERRORISTS_NOT_ESCAPED) | (1<<ROUND_VIP_NOT_ESCAPED)
enum WeaponIdType
{
WEAPON_NONE,
@ -47,16 +51,16 @@ enum WeaponIdType
WEAPON_SHIELDGUN = 99
};
/* Weapon info types for use with rg_get_weapon_info() */
// Weapon info types for use with rg_get_weapon_info()
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
};
enum WinStatus

View File

@ -9,7 +9,7 @@ void SV_StartSound(IRehldsHook_SV_StartSound *chain, int recipients, edict_t *en
{
auto original = [chain](int _recipients, int _entity, int _channel, const char *_sample, int _volume, float _attenuation, int _fFlags, int _pitch)
{
chain->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);

View File

@ -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<CBaseEntity>(*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<CBaseEntity>(*srcAddr);
break;
}

View File

@ -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<CBaseEntity>(*value);
set_member<CBaseEntity *>(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<EHANDLE>(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<edict_t *>(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<EHANDLE>(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<edict_t *>(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<cell *>(pdata, member->offset, element);
dest[0] = vecSrc[0];
dest[1] = vecSrc[1];
dest[2] = vecSrc[2];
dest = get_member_direct<cell *>(pdata, member->offset, element);
return 1;
}
case MEMBER_STRING:

View File

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

View File

@ -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();

View File

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