Added support for logical (non-networked) entities using entity references - See http://wiki.alliedmods.net/Entity_References_%28SourceMod%29 (bug 2459, r=dvander)

This commit is contained in:
Matt Woodrow
2009-07-24 12:34:31 +12:00
parent 7ec656db09
commit 3e36382b58
27 changed files with 863 additions and 480 deletions
+219
View File
@@ -37,10 +37,14 @@
#include "sm_stringutil.h"
#include "GameConfigs.h"
#include <compat_wrappers.h>
#include <Logger.h>
CHalfLife2 g_HL2;
ConVar *sv_lan = NULL;
static void *g_EntList = NULL;
static int entInfoOffset = -1;
namespace SourceHook
{
template<>
@@ -123,6 +127,51 @@ void CHalfLife2::OnSourceModAllInitialized()
g_ShareSys.AddInterface(NULL, this);
}
void CHalfLife2::OnSourceModAllInitialized_Post()
{
char *addr = NULL;
#ifdef PLATFORM_WINDOWS
int offset;
/* gEntList and/or g_pEntityList */
if (!g_pGameConf->GetMemSig("LevelShutdown", (void **)&addr))
{
g_Logger.LogError("Logical Entities not supported by this mod (LevelShutdown) - Reverting to networkable entities only");
return;
}
if (!addr)
{
g_Logger.LogError("Failed lookup of LevelShutdown - Reverting to networkable entities only");
return;
}
if (!g_pGameConf->GetOffset("gEntList", &offset))
{
g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
return;
}
g_EntList = *reinterpret_cast<void **>(addr + offset);
#elif defined PLATFORM_LINUX
/* gEntList and/or g_pEntityList */
if (!g_pGameConf->GetMemSig("gEntList", (void **)&addr))
{
g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
return;
}
if (!addr)
{
g_Logger.LogError("Failed lookup of gEntList - Reverting to networkable entities only");
return;
}
g_EntList = reinterpret_cast<void *>(addr);
#endif
if (!g_pGameConf->GetOffset("EntInfo", &entInfoOffset))
{
g_Logger.LogError("Logical Entities not supported by this mod (EntInfo) - Reverting to networkable entities only");
return;
}
}
#if !defined METAMOD_PLAPI_VERSION
bool CHalfLife2::IsOriginalEngine()
{
@@ -642,3 +691,173 @@ void CHalfLife2::ServerCommand(const char *buffer)
{
engine->ServerCommand(buffer);
}
cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity)
{
IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
CBaseHandle hndl = pUnknown->GetRefEHandle();
return (hndl.ToInt() | (1<<31));
}
CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef)
{
CEntInfo *pInfo = NULL;
if (entRef & (1<<31))
{
/* Proper ent reference */
int hndlValue = entRef & ~(1<<31);
CBaseHandle hndl(hndlValue);
pInfo = LookupEntity(hndl.GetEntryIndex());
if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
{
return NULL;
}
}
else
{
/* Old style index only */
pInfo = LookupEntity(entRef);
}
if (!pInfo)
{
return NULL;
}
IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
if (pUnk)
{
return pUnk->GetBaseEntity();
}
return NULL;
}
/**
* Retrieves the CEntInfo pointer from g_EntList for a given entity index
*/
CEntInfo *CHalfLife2::LookupEntity(int entIndex)
{
if (!g_EntList || entInfoOffset == -1)
{
/* Attempt to use engine interface instead */
static CEntInfo tempInfo;
tempInfo.m_pNext = NULL;
tempInfo.m_pPrev = NULL;
edict_t *pEdict = engine->PEntityOfEntIndex(entIndex);
if (!pEdict)
{
return NULL;
}
IServerUnknown *pUnk = pEdict->GetUnknown();
if (!pUnk)
{
return NULL;
}
tempInfo.m_pEntity = pUnk;
tempInfo.m_SerialNumber = pUnk->GetRefEHandle().GetSerialNumber();
return &tempInfo;
}
CEntInfo *pArray = (CEntInfo *)(((unsigned char *)g_EntList) + entInfoOffset);
return &pArray[entIndex];
}
/**
SERIAL_MASK = 0x7fff (15 bits)
#define MAX_EDICT_BITS 11
#define NUM_ENT_ENTRY_BITS (MAX_EDICT_BITS + 1)
m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS);
Top 5 bits of a handle are unused.
Bit 31 - Our 'reference' flag indicator
*/
cell_t CHalfLife2::IndexToReference(int entIndex)
{
CBaseEntity *pEnt = ReferenceToEntity(entIndex);
if (!pEnt)
{
return INVALID_EHANDLE_INDEX;
}
return EntityToReference(pEnt);
}
int CHalfLife2::ReferenceToIndex(cell_t entRef)
{
if (entRef == INVALID_EHANDLE_INDEX)
{
return INVALID_EHANDLE_INDEX;
}
if (entRef & (1<<31))
{
/* Proper ent reference */
int hndlValue = entRef & ~(1<<31);
CBaseHandle hndl(hndlValue);
CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex());
if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
{
return INVALID_EHANDLE_INDEX;
}
return hndl.GetEntryIndex();
}
return entRef;
}
cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
{
if (pEntity == NULL)
{
return INVALID_EHANDLE_INDEX;
}
IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
CBaseHandle hndl = pUnknown->GetRefEHandle();
if (hndl.GetEntryIndex() >= MAX_EDICTS)
{
return (hndl.ToInt() | (1<<31));
}
else
{
return hndl.GetEntryIndex();
}
}
cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef)
{
if (entRef == INVALID_EHANDLE_INDEX)
{
return INVALID_EHANDLE_INDEX;
}
int hndlValue = entRef & ~(1<<31);
CBaseHandle hndl(hndlValue);
if (hndl.GetEntryIndex() < MAX_EDICTS)
{
return hndl.GetEntryIndex();
}
return entRef;
}
void *CHalfLife2::GetGlobalEntityList()
{
return g_EntList;
}
+19
View File
@@ -43,6 +43,7 @@
#include <KeyValues.h>
#include <server_class.h>
#include <datamap.h>
#include <ihandleentity.h>
class CCommand;
@@ -86,6 +87,15 @@ struct DelayedKickInfo
char buffer[384];
};
class CEntInfo
{
public:
IHandleEntity *m_pEntity;
int m_SerialNumber;
CEntInfo *m_pPrev;
CEntInfo *m_pNext;
};
class CHalfLife2 :
public SMGlobalClass,
public IGameHelpers
@@ -96,6 +106,7 @@ public:
public:
void OnSourceModStartup(bool late);
void OnSourceModAllInitialized();
void OnSourceModAllInitialized_Post();
/*void OnSourceModAllShutdown();*/
public: //IGameHelpers
SendProp *FindInSendTable(const char *classname, const char *offset);
@@ -115,6 +126,14 @@ public: //IGameHelpers
void SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt);
const char *GetCurrentMap();
void ServerCommand(const char *buffer);
CBaseEntity *ReferenceToEntity(cell_t entRef);
cell_t EntityToReference(CBaseEntity *pEntity);
cell_t IndexToReference(int entIndex);
int ReferenceToIndex(cell_t entRef);
cell_t ReferenceToBCompatRef(cell_t entRef);
CEntInfo *LookupEntity(int entIndex);
cell_t EntityToBCompatRef(CBaseEntity *pEntity);
void *GetGlobalEntityList();
public:
void AddToFakeCliCmdQueue(int client, int userid, const char *cmd);
void ProcessFakeCliCmdQueue();
+1 -1
View File
@@ -248,7 +248,7 @@ static cell_t BanClient(IPluginContext *pContext, const cell_t *params)
char *ban_reason, *ban_cmd;
int client, ban_flags, ban_source, ban_time;
client = params[1];
client = g_HL2.ReferenceToIndex(params[1]);
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer || !pPlayer->IsConnected())
+12 -2
View File
@@ -33,6 +33,7 @@
#include "HandleSys.h"
#include <bitbuf.h>
#include <vector.h>
#include <HalfLife2.h>
static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
{
@@ -226,7 +227,14 @@ static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
}
pBitBuf->WriteShort(params[2]);
int index = g_HL2.ReferenceToIndex(params[2]);
if (index == -1)
{
return 0;
}
pBitBuf->WriteShort(index);
return 1;
}
@@ -523,7 +531,9 @@ static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
}
return pBitBuf->ReadShort();
int ref = g_HL2.IndexToReference(pBitBuf->ReadShort());
return g_HL2.ReferenceToBCompatRef(ref);
}
static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
+161 -129
View File
@@ -54,45 +54,74 @@ enum PropFieldType
PropField_String_T, /**< Valid for Data fields. Read only! */
};
inline edict_t *BaseEntityToEdict(CBaseEntity *pEntity)
{
IServerUnknown *pUnk = (IServerUnknown *)pEntity;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if (!pNet)
{
return NULL;
}
return pNet->GetEdict();
}
inline edict_t *GetEdict(cell_t num)
{
edict_t *pEdict = PEntityOfEntIndex(num);
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
if (!pEntity)
{
return 0;
}
edict_t *pEdict = ::BaseEntityToEdict(pEntity);
if (!pEdict || pEdict->IsFree())
{
return NULL;
}
if (num > 0 && num <= g_Players.GetMaxClients())
int index = g_HL2.ReferenceToIndex(num);
if (index > 0 && index <= g_Players.GetMaxClients())
{
CPlayer *pPlayer = g_Players.GetPlayerByIndex(num);
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
if (!pPlayer || !pPlayer->IsConnected())
{
return NULL;
}
}
return pEdict;
}
edict_t *GetEntity(cell_t num, CBaseEntity **pData)
{
edict_t *pEdict = PEntityOfEntIndex(num);
if (!pEdict || pEdict->IsFree())
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
if (!pEntity)
{
return NULL;
return 0;
}
if (num > 0 && num <= g_Players.GetMaxClients())
int index = g_HL2.ReferenceToIndex(num);
if (index > 0 && index <= g_Players.GetMaxClients())
{
CPlayer *pPlayer = g_Players.GetPlayerByIndex(num);
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
if (!pPlayer || !pPlayer->IsConnected())
{
return NULL;
}
}
IServerUnknown *pUnk;
if ((pUnk=pEdict->GetUnknown()) == NULL)
*pData = pEntity;
edict_t *pEdict = ::BaseEntityToEdict(pEntity);
if (!pEdict || pEdict->IsFree())
{
return NULL;
}
*pData = pUnk->GetBaseEntity();
return pEdict;
}
@@ -161,11 +190,11 @@ static cell_t CreateEdict(IPluginContext *pContext, const cell_t *params)
static cell_t RemoveEdict(IPluginContext *pContext, const cell_t *params)
{
edict_t *pEdict = PEntityOfEntIndex(params[1]);
edict_t *pEdict = GetEdict(params[1]);
if (!pEdict)
{
return pContext->ThrowNativeError("Edict %d is not a valid edict", params[1]);
return pContext->ThrowNativeError("Edict %d (%d) is not a valid edict", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
engine->RemoveEdict(pEdict);
@@ -188,20 +217,8 @@ static cell_t IsValidEdict(IPluginContext *pContext, const cell_t *params)
static cell_t IsValidEntity(IPluginContext *pContext, const cell_t *params)
{
edict_t *pEdict = GetEdict(params[1]);
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
if (!pEdict)
{
return 0;
}
IServerUnknown *pUnknown = pEdict->GetUnknown();
if (!pUnknown)
{
return 0;
}
CBaseEntity *pEntity = pUnknown->GetBaseEntity();
return (pEntity != NULL) ? 1 : 0;
}
@@ -228,7 +245,7 @@ static cell_t GetEdictFlags(IPluginContext *pContext, const cell_t *params)
if (!pEdict)
{
return pContext->ThrowNativeError("Invalid edict (%d)", params[1]);
return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
return pEdict->m_fStateFlags;
@@ -240,7 +257,7 @@ static cell_t SetEdictFlags(IPluginContext *pContext, const cell_t *params)
if (!pEdict)
{
return pContext->ThrowNativeError("Invalid edict (%d)", params[1]);
return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
pEdict->m_fStateFlags = params[2];
@@ -254,7 +271,7 @@ static cell_t GetEdictClassname(IPluginContext *pContext, const cell_t *params)
if (!pEdict)
{
return pContext->ThrowNativeError("Invalid edict (%d)", params[1]);
return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
const char *cls = pEdict->GetClassName();
@@ -271,14 +288,16 @@ static cell_t GetEdictClassname(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntityNetClass(IPluginContext *pContext, const cell_t *params)
{
edict_t *pEdict = GetEdict(params[1]);
CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
if (!pEdict)
IServerUnknown *pUnk = (IServerUnknown *)pEntity;
if (!pEntity)
{
return pContext->ThrowNativeError("Invalid edict (%d)", params[1]);
return pContext->ThrowNativeError("Invalid entity (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
IServerNetworkable *pNet = pEdict->GetNetworkable();
IServerNetworkable *pNet = pUnk->GetNetworkable();
if (!pNet)
{
return 0;
@@ -294,11 +313,11 @@ static cell_t GetEntityNetClass(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -325,9 +344,9 @@ static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -336,7 +355,7 @@ static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Offset %d is invalid", offset);
}
if (params[5])
if (params[5] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -368,11 +387,11 @@ static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -391,9 +410,9 @@ static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -404,7 +423,7 @@ static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[3]);
if (params[4])
if (params[4] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -415,11 +434,11 @@ static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -445,9 +464,9 @@ static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -465,7 +484,7 @@ static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
v->y = sp_ctof(vec[1]);
v->z = sp_ctof(vec[2]);
if (params[4])
if (params[4] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -477,11 +496,11 @@ static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -497,7 +516,8 @@ static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
return 0;
}
return hndl.GetEntryIndex();
int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
return g_HL2.ReferenceToBCompatRef(ref);
}
int CheckBaseHandle(CBaseHandle &hndl)
@@ -537,11 +557,11 @@ int CheckBaseHandle(CBaseHandle &hndl)
static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (pEdict == NULL || pEntity == NULL)
if (pEntity == NULL)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -552,7 +572,8 @@ static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
return CheckBaseHandle(hndl);
int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
return g_HL2.ReferenceToBCompatRef(ref);
}
/* THIS GUY IS DEPRECATED. */
@@ -561,9 +582,9 @@ static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -574,20 +595,23 @@ static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
if (params[3] == 0)
if (params[3] == 0 || params[3] == INVALID_EHANDLE_INDEX)
{
hndl.Set(NULL);
} else {
edict_t *pEdict = GetEdict(params[3]);
if (!pEdict)
CBaseEntity *pOther;
GetEntity(params[3], &pOther);
if (!pOther)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[3]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
}
IServerEntity *pEntOther = pEdict->GetIServerEntity();
hndl.Set(pEntOther);
IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
hndl.Set(pHandleEnt);
}
if (params[4])
if (params[4] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -600,9 +624,9 @@ static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -613,22 +637,25 @@ static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
if (params[3] == -1)
if (params[3] == INVALID_EHANDLE_INDEX)
{
hndl.Set(NULL);
}
else
{
edict_t *pEdict = GetEdict(params[3]);
if (!pEdict)
CBaseEntity *pOther;
GetEntity(params[3], &pOther);
if (!pOther)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[3]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
}
IServerEntity *pEntOther = pEdict->GetIServerEntity();
hndl.Set(pEntOther);
IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
hndl.Set(pHandleEnt);
}
if (params[4])
if (params[4] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -642,7 +669,7 @@ static cell_t ChangeEdictState(IPluginContext *pContext, const cell_t *params)
if (!pEdict)
{
return pContext->ThrowNativeError("Edict %d is invalid", params[1]);
return pContext->ThrowNativeError("Edict %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
g_HL2.SetEdictStateChanged(pEdict, params[2]);
@@ -725,11 +752,11 @@ static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
datamap_t *pMap;
typedescription_t *td;
char *offset;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
@@ -830,11 +857,11 @@ static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -855,9 +882,9 @@ static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pEntity;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
int offset = params[2];
@@ -872,7 +899,7 @@ static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
pContext->LocalToString(params[3], &src);
size_t len = strncopy(dest, src, params[4]);
if (params[5])
if (params[5] && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -895,10 +922,11 @@ static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
}
#define FIND_PROP_SEND(pProp) \
IServerNetworkable *pNet = pEdict->GetNetworkable(); \
IServerUnknown *pUnk = (IServerUnknown *)pEntity; \
IServerNetworkable *pNet = pUnk->GetNetworkable(); \
if (!pNet) \
{ \
return pContext->ThrowNativeError("Edict %d is not networkable", params[1]); \
return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]); \
} \
if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, pProp)) \
{ \
@@ -942,12 +970,13 @@ static cell_t GetEntProp(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
/* TODO: Find a way to lookup classname without having an edict - Is this a guaranteed prop? */
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1033,12 +1062,12 @@ static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1123,12 +1152,12 @@ static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1194,12 +1223,12 @@ static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1252,7 +1281,7 @@ static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[4]);
if (params[2] == Prop_Send)
if (params[2] == Prop_Send && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -1270,12 +1299,12 @@ static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1326,7 +1355,8 @@ static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
return CheckBaseHandle(hndl);
int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
return g_HL2.ReferenceToBCompatRef(ref);
}
static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
@@ -1339,12 +1369,12 @@ static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1401,18 +1431,19 @@ static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
}
else
{
edict_t *pOther;
CBaseEntity *pOtherEnt;
CBaseEntity *pOther;
GetEntity(params[4], &pOther);
if ((pOther = GetEntity(params[4], &pOtherEnt)) == NULL)
if (!pOther)
{
return pContext->ThrowNativeError("Invalid entity %d", params[4]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[4]), params[4]);
}
hndl.Set(pOther->GetIServerEntity());
IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
hndl.Set(pHandleEnt);
}
if (params[2] == Prop_Send)
if (params[2] == Prop_Send && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -1430,12 +1461,12 @@ static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1508,12 +1539,12 @@ static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1573,7 +1604,7 @@ static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
v->y = sp_ctof(vec[1]);
v->z = sp_ctof(vec[2]);
if (params[2] == Prop_Send)
if (params[2] == Prop_Send && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
@@ -1592,12 +1623,12 @@ static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
if ((class_name = pEdict->GetClassName()) == NULL)
if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
{
class_name = "";
}
@@ -1682,9 +1713,9 @@ static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
int maxlen;
edict_t *pEdict = GetEntity(params[1], &pEntity);
if (!pEdict || !pEntity)
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}
switch (params[2])
@@ -1713,7 +1744,8 @@ static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
case Prop_Send:
{
char *prop;
IServerNetworkable *pNet = pEdict->GetNetworkable();
IServerUnknown *pUnk = (IServerUnknown *)pEntity;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if (!pNet)
{
return pContext->ThrowNativeError("The edict is not networkable");
@@ -1744,7 +1776,7 @@ static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
pContext->LocalToString(params[4], &src);
size_t len = strncopy(dest, src, maxlen);
if (params[2] == Prop_Send)
if (params[2] == Prop_Send && (pEdict != NULL))
{
g_HL2.SetEdictStateChanged(pEdict, offset);
}
+23
View File
@@ -475,6 +475,26 @@ static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
return 0;
}
static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
{
if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
{
return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
}
return g_HL2.IndexToReference(params[1]);
}
static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
{
return g_HL2.ReferenceToIndex(params[1]);
}
static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
{
return g_HL2.ReferenceToBCompatRef(params[1]);
}
REGISTER_NATIVES(halflifeNatives)
{
{"CreateFakeClient", CreateFakeClient},
@@ -505,5 +525,8 @@ REGISTER_NATIVES(halflifeNatives)
{"ShowVGUIPanel", ShowVGUIPanel},
{"IsPlayerAlive", smn_IsPlayerAlive},
{"GuessSDKVersion", GuessSDKVersion},
{"EntIndexToEntRef", IndexToReference},
{"EntRefToEntIndex", ReferenceToIndex},
{"MakeCompatEntRef", ReferenceToBCompatRef},
{NULL, NULL},
};