Improve/fix GetEntity (bug 4092, r=pred)

This commit is contained in:
Fyren 2009-12-15 23:14:53 -08:00
parent 75611f3bca
commit caa8932c9a
3 changed files with 80 additions and 89 deletions

View File

@ -672,7 +672,10 @@ edict_t *CHalfLife2::GetHandleEntity(CBaseHandle &hndl)
edict_t *pStoredEdict; edict_t *pStoredEdict;
CBaseEntity *pStoredEntity; CBaseEntity *pStoredEntity;
pStoredEdict = GetEntity(index, &pStoredEntity); if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
{
return NULL;
}
if (pStoredEdict == NULL || pStoredEntity == NULL) if (pStoredEdict == NULL || pStoredEntity == NULL)
{ {

View File

@ -166,6 +166,6 @@ private:
extern CHalfLife2 g_HL2; extern CHalfLife2 g_HL2;
edict_t *GetEntity(cell_t num, CBaseEntity **pData); bool IndexToAThings(cell_t, CBaseEntity **pEntData, edict_t **pEdictData);
#endif //_INCLUDE_SOURCEMOD_CHALFLIFE2_H_ #endif //_INCLUDE_SOURCEMOD_CHALFLIFE2_H_

View File

@ -68,18 +68,37 @@ inline edict_t *BaseEntityToEdict(CBaseEntity *pEntity)
} }
inline edict_t *GetEdict(cell_t num) inline edict_t *GetEdict(cell_t num)
{
edict_t *pEdict;
if (!IndexToAThings(num, NULL, &pEdict))
{
return NULL;
}
return pEdict;
}
inline CBaseEntity *GetEntity(cell_t num)
{
CBaseEntity *pEntity;
if (!IndexToAThings(num, &pEntity, NULL))
{
return NULL;
}
return pEntity;
}
/* Given an entity reference or index, fill out a CBaseEntity and/or edict.
If lookup is successful, returns true and writes back the two parameters.
If lookup fails, returns false and doesn't touch the params. */
bool IndexToAThings(cell_t num, CBaseEntity **pEntData, edict_t **pEdictData)
{ {
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num); CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
if (!pEntity) if (!pEntity)
{ {
return 0; return false;
}
edict_t *pEdict = ::BaseEntityToEdict(pEntity);
if (!pEdict || pEdict->IsFree())
{
return NULL;
} }
int index = g_HL2.ReferenceToIndex(num); int index = g_HL2.ReferenceToIndex(num);
@ -88,43 +107,36 @@ inline edict_t *GetEdict(cell_t num)
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index); CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
if (!pPlayer || !pPlayer->IsConnected()) if (!pPlayer || !pPlayer->IsConnected())
{ {
return NULL; return false;
} }
} }
return pEdict; if (pEntData)
}
edict_t *GetEntity(cell_t num, CBaseEntity **pData)
{ {
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num); *pEntData = pEntity;
if (!pEntity)
{
return 0;
} }
int index = g_HL2.ReferenceToIndex(num); if (pEdictData)
if (index > 0 && index <= g_Players.GetMaxClients())
{ {
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
if (!pPlayer || !pPlayer->IsConnected())
{
return NULL;
}
}
*pData = pEntity;
edict_t *pEdict = ::BaseEntityToEdict(pEntity); edict_t *pEdict = ::BaseEntityToEdict(pEntity);
if (!pEdict || pEdict->IsFree()) if (pEdict->IsFree())
{ {
return NULL; pEdict = NULL;
} }
return pEdict; *pEdictData = pEdict;
} }
return true;
}
/*
<predcrab> AThings is like guaranteed r+ by dvander though
<predcrab> he wont even read the rest of the patch to nit stuff
<Fyren> Like I wasn't going to r? you anyway!
<predcrab> he still sees!
<predcrab> he's everywhere
*/
class VEmptyClass {}; class VEmptyClass {};
datamap_t *VGetDataDescMap(CBaseEntity *pThisPtr, int offset) datamap_t *VGetDataDescMap(CBaseEntity *pThisPtr, int offset)
{ {
@ -312,8 +324,7 @@ static cell_t GetEntityNetClass(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntData(IPluginContext *pContext, const cell_t *params) static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (!pEntity) if (!pEntity)
{ {
@ -342,9 +353,9 @@ static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntData(IPluginContext *pContext, const cell_t *params) static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -386,8 +397,7 @@ static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params) static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (!pEntity) if (!pEntity)
{ {
@ -408,9 +418,9 @@ static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params) static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -433,8 +443,7 @@ static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params) static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (!pEntity) if (!pEntity)
{ {
@ -462,9 +471,9 @@ static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params) static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -495,8 +504,7 @@ static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
/* THIS GUY IS DEPRECATED. */ /* THIS GUY IS DEPRECATED. */
static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params) static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (!pEntity) if (!pEntity)
{ {
@ -532,7 +540,10 @@ int CheckBaseHandle(CBaseHandle &hndl)
edict_t *pStoredEdict; edict_t *pStoredEdict;
CBaseEntity *pStoredEntity; CBaseEntity *pStoredEntity;
pStoredEdict = GetEntity(index, &pStoredEntity); if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
{
return -1;
}
if (pStoredEdict == NULL || pStoredEntity == NULL) if (pStoredEdict == NULL || pStoredEntity == NULL)
{ {
@ -556,8 +567,7 @@ int CheckBaseHandle(CBaseHandle &hndl)
static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params) static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (pEntity == NULL) if (pEntity == NULL)
{ {
@ -580,9 +590,9 @@ static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params) static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -599,8 +609,7 @@ static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
{ {
hndl.Set(NULL); hndl.Set(NULL);
} else { } else {
CBaseEntity *pOther; CBaseEntity *pOther = GetEntity(params[3]);
GetEntity(params[3], &pOther);
if (!pOther) if (!pOther)
{ {
@ -622,9 +631,9 @@ static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params) static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -643,8 +652,7 @@ static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
} }
else else
{ {
CBaseEntity *pOther; CBaseEntity *pOther = GetEntity(params[3]);
GetEntity(params[3], &pOther);
if (!pOther) if (!pOther)
{ {
@ -752,7 +760,7 @@ static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
datamap_t *pMap; datamap_t *pMap;
typedescription_t *td; typedescription_t *td;
char *offset; char *offset;
GetEntity(params[1], &pEntity); pEntity = GetEntity(params[1]);
if (!pEntity) if (!pEntity)
{ {
@ -856,8 +864,7 @@ static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params) static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity = GetEntity(params[1]);
GetEntity(params[1], &pEntity);
if (!pEntity) if (!pEntity)
{ {
@ -880,9 +887,9 @@ static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params) static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
{ {
CBaseEntity *pEntity; CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -968,9 +975,7 @@ static cell_t GetEntProp(IPluginContext *pContext, const cell_t *params)
edict_t *pEdict; edict_t *pEdict;
int bit_count; int bit_count;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1060,9 +1065,7 @@ static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
edict_t *pEdict; edict_t *pEdict;
int bit_count; int bit_count;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1150,9 +1153,7 @@ static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1221,9 +1222,7 @@ static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1297,9 +1296,7 @@ static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1367,9 +1364,7 @@ static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1431,8 +1426,7 @@ static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
} }
else else
{ {
CBaseEntity *pOther; CBaseEntity *pOther = GetEntity(params[4]);
GetEntity(params[4], &pOther);
if (!pOther) if (!pOther)
{ {
@ -1459,9 +1453,7 @@ static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1537,9 +1529,7 @@ static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
const char *class_name; const char *class_name;
edict_t *pEdict; edict_t *pEdict;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1621,9 +1611,7 @@ static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
edict_t *pEdict; edict_t *pEdict;
bool bIsStringIndex; bool bIsStringIndex;
pEdict = GetEntity(params[1], &pEntity); if (!IndexToAThings(params[1], &pEntity, &pEdict))
if (!pEntity)
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }
@ -1711,9 +1699,9 @@ static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
char *prop; char *prop;
int offset; int offset;
int maxlen; int maxlen;
edict_t *pEdict = GetEntity(params[1], &pEntity); edict_t *pEdict;
if (!pEntity) if (!IndexToAThings(params[1], &pEntity, &pEdict))
{ {
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]); return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
} }