From add4867d21fe82f0775879a59997116b8b67332a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Gr=C3=BCnbacher?= Date: Sun, 10 May 2015 23:56:06 +0200 Subject: [PATCH] Engine: Add safe get_global_edict2() and entity_get_edict2() --- modules/engine/entity.cpp | 17 ++++++++++++++--- modules/engine/globals.cpp | 17 ++++++++++++++--- plugins/include/engine.inc | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/modules/engine/entity.cpp b/modules/engine/entity.cpp index c1f76491..a0fc86c8 100644 --- a/modules/engine/entity.cpp +++ b/modules/engine/entity.cpp @@ -1057,7 +1057,7 @@ static cell AMX_NATIVE_CALL entity_set_string(AMX *amx, cell *params) return 1; } -static cell AMX_NATIVE_CALL entity_get_edict(AMX *amx, cell *params) +static cell AMX_NATIVE_CALL entity_get_edict2(AMX *amx, cell *params) { int iEnt = params[1]; int idx = params[2]; @@ -1103,16 +1103,26 @@ static cell AMX_NATIVE_CALL entity_get_edict(AMX *amx, cell *params) pRet = pEnt->v.euser4; break; default: - return 0; + return -1; break; } if (FNullEnt(pRet)) - return 0; + return -1; return ENTINDEX(pRet); } +static cell AMX_NATIVE_CALL entity_get_edict(AMX *amx, cell *params) +{ + cell res = entity_get_edict2(amx, params); + + if (res == -1) + res = 0; + + return res; +} + static cell AMX_NATIVE_CALL entity_set_edict(AMX *amx, cell *params) { int iEnt = params[1]; @@ -1602,6 +1612,7 @@ AMX_NATIVE_INFO ent_Natives[] = { {"entity_get_string", entity_get_string}, {"entity_set_string", entity_set_string}, {"entity_get_edict", entity_get_edict}, + {"entity_get_edict2", entity_get_edict2}, {"entity_set_edict", entity_set_edict}, {"entity_get_byte", entity_get_byte}, {"entity_set_byte", entity_set_byte}, diff --git a/modules/engine/globals.cpp b/modules/engine/globals.cpp index 3b8a281d..4b859c12 100644 --- a/modules/engine/globals.cpp +++ b/modules/engine/globals.cpp @@ -157,7 +157,7 @@ static cell AMX_NATIVE_CALL get_global_vector(AMX *amx, cell *params) // globals return 1; } -static cell AMX_NATIVE_CALL get_global_edict(AMX *amx, cell *params) // globals_get_edict(variable); = 1 param +static cell AMX_NATIVE_CALL get_global_edict2(AMX *amx, cell *params) { edict_t* pReturnEntity; @@ -167,14 +167,24 @@ static cell AMX_NATIVE_CALL get_global_edict(AMX *amx, cell *params) // globals_ break; default: MF_LogError(amx, AMX_ERR_NATIVE, "Undefined global_edict index %d", params[1]); - return 0; + return -1; } // Will crash if ENTINDEX() is called on bad pointer? if(!FNullEnt(pReturnEntity)) return ENTINDEX(pReturnEntity); - return 0; + return -1; +} + +static cell AMX_NATIVE_CALL get_global_edict(AMX *amx, cell *params) // globals_get_edict(variable); = 1 param +{ + cell res = get_global_edict2(amx, params); + + if (res == -1) + res = 0; + + return res; } AMX_NATIVE_INFO global_Natives[] = { @@ -182,6 +192,7 @@ AMX_NATIVE_INFO global_Natives[] = { {"get_global_int", get_global_int}, {"get_global_string", get_global_string}, {"get_global_edict", get_global_edict}, + {"get_global_edict2", get_global_edict2}, {"get_global_vector", get_global_vector}, {NULL, NULL}, /////////////////// diff --git a/plugins/include/engine.inc b/plugins/include/engine.inc index ae3bd98a..4b811429 100755 --- a/plugins/include/engine.inc +++ b/plugins/include/engine.inc @@ -306,6 +306,21 @@ native get_global_vector(variable, Float:vector[3]); */ native get_global_edict(variable); +/** + * Returns a edict type value from the server globals. + * + * @note For a list of valid edict type entries, see the GL_* constants in + * engine_const.inc under the "Edict" section. + * @note This native returns -1 as a safe error value if the edict retrieved is + * an invalid entity. Otherwise it is identical to get_global_edict(). + * + * @param variable Entry to retrieve from + * + * @return Value of specified entry + * @error If an invalid entry is provided, an error will be thrown. + */ +native get_global_edict2(variable); + /** * Sets the size of the entity bounding box, as described by the minimum and * maximum vectors relative to the origin. @@ -476,6 +491,26 @@ native entity_set_vector(iIndex, iKey, const Float:vNewVector[3]); */ native entity_get_edict(iIndex, iKey); +/** + * Returns an edict type value from an entities entvar struct. + * + * @note For a list of valid edict type entries, see the EV_ENT_* constants in + * engine_const.inc + * @note This native returns -1 as a safe error value if the edict retrieved + * from the entvar is an invalid entity. Otherwise it is identical to + * entity_get_edict(). + * + * @param iIndex Entity index + * @param iKey Entry to retrieve from + * + * @return Entity index in specified entry, -1 if the edict in the + * entvar is not a valid entity or an invalid entry was + * specified + * @error If an invalid entity index is provided, an error will be + * thrown. + */ +native entity_get_edict2(iIndex, iKey); + /** * Sets an edict type value in an entities entvar struct. *