From 8922ed0c5de357b4afa17e1b493edd22b8d97ddd Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sat, 14 Jan 2023 09:48:05 -0500 Subject: [PATCH] Add TE_WriteEnt and TE_ReadEnt natives to SDKTools. (#1905) --- extensions/sdktools/tempents.cpp | 38 +++++++++++++++++++ extensions/sdktools/tempents.h | 2 + extensions/sdktools/tenatives.cpp | 53 +++++++++++++++++++++++++++ plugins/include/sdktools_tempents.inc | 18 +++++++++ 4 files changed, 111 insertions(+) diff --git a/extensions/sdktools/tempents.cpp b/extensions/sdktools/tempents.cpp index 55d2ab89..e71d652f 100644 --- a/extensions/sdktools/tempents.cpp +++ b/extensions/sdktools/tempents.cpp @@ -30,6 +30,7 @@ */ #include "tempents.h" +#include TempEntityManager g_TEManager; ICallWrapper *g_GetServerClass = NULL; @@ -183,6 +184,43 @@ bool TempEntityInfo::TE_GetEntData(const char *name, int *value) return true; } +bool TempEntityInfo::TE_SetEntDataEnt(const char *name, IHandleEntity *value) +{ + /* Search for our offset */ + int offset = _FindOffset(name); + + if (offset < 0) + { + return false; + } + + auto *pHndl = (CBaseHandle *)((uint8_t *)m_Me + offset); + pHndl->Set(value); + + return true; +} + + +bool TempEntityInfo::TE_GetEntDataEnt(const char *name, IHandleEntity **value) +{ + /* Search for our offset */ + int offset = _FindOffset(name); + + if (offset < 0) + { + return false; + } + + auto *pHndl = (CBaseHandle *)((uint8_t *)m_Me + offset); + auto *pEnt = reinterpret_cast(gamehelpers->ReferenceToEntity(pHndl->GetEntryIndex())); + if (!pEnt || *pHndl != pEnt->GetRefEHandle()) + return false; + + *value = pEnt; + + return true; +} + bool TempEntityInfo::TE_SetEntDataFloat(const char *name, float value) { /* Search for our offset */ diff --git a/extensions/sdktools/tempents.h b/extensions/sdktools/tempents.h index 366ccc6a..fddb8614 100644 --- a/extensions/sdktools/tempents.h +++ b/extensions/sdktools/tempents.h @@ -47,10 +47,12 @@ public: ServerClass *GetServerClass(); bool IsValidProp(const char *name); bool TE_SetEntData(const char *name, int value); + bool TE_SetEntDataEnt(const char *name, IHandleEntity *value); bool TE_SetEntDataFloat(const char *name, float value); bool TE_SetEntDataVector(const char *name, float vector[3]); bool TE_SetEntDataFloatArray(const char *name, cell_t *array, int size); bool TE_GetEntData(const char *name, int *value); + bool TE_GetEntDataEnt(const char *name, IHandleEntity **value); bool TE_GetEntDataFloat(const char *name, float *value); bool TE_GetEntDataVector(const char *name, float vector[3]); void Send(IRecipientFilter &filter, float delay); diff --git a/extensions/sdktools/tenatives.cpp b/extensions/sdktools/tenatives.cpp index 8ccebd54..014538ae 100644 --- a/extensions/sdktools/tenatives.cpp +++ b/extensions/sdktools/tenatives.cpp @@ -307,6 +307,57 @@ static cell_t smn_TEReadNum(IPluginContext *pContext, const cell_t *params) return val; } +static cell_t smn_TEWriteEnt(IPluginContext *pContext, const cell_t *params) +{ + if (!g_TEManager.IsAvailable()) + { + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report"); + } + if (!g_CurrentTE) + { + return pContext->ThrowNativeError("No TempEntity call is in progress"); + } + + char *prop; + pContext->LocalToString(params[1], &prop); + + CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(params[2]); + if (!pEntity && params[2] != -1) + { + return pContext->ThrowNativeError("Entity %d (%d) is invalid", gamehelpers->ReferenceToIndex(params[2])); + } + + if (!g_CurrentTE->TE_SetEntDataEnt(prop, reinterpret_cast(pEntity))) + { + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop); + } + + return 1; +} + +static cell_t smn_TEReadEnt(IPluginContext *pContext, const cell_t *params) +{ + if (!g_TEManager.IsAvailable()) + { + return pContext->ThrowNativeError("TempEntity System unsupported or not available, file a bug report"); + } + if (!g_CurrentTE) + { + return pContext->ThrowNativeError("No TempEntity call is in progress"); + } + + char *prop; + IHandleEntity *val; + pContext->LocalToString(params[1], &prop); + + if (!g_CurrentTE->TE_GetEntDataEnt(prop, &val)) + { + return pContext->ThrowNativeError("Temp entity property \"%s\" not found", prop); + } + + return gamehelpers->EntityToBCompatRef(reinterpret_cast(val)); +} + static cell_t smn_TE_WriteFloat(IPluginContext *pContext, const cell_t *params) { if (!g_TEManager.IsAvailable()) @@ -547,6 +598,8 @@ sp_nativeinfo_t g_TENatives[] = {"TE_Start", smn_TEStart}, {"TE_WriteNum", smn_TEWriteNum}, {"TE_ReadNum", smn_TEReadNum}, + {"TE_WriteEnt", smn_TEWriteEnt}, + {"TE_ReadEnt", smn_TEReadEnt}, {"TE_WriteFloat", smn_TE_WriteFloat}, {"TE_ReadFloat", smn_TE_ReadFloat}, {"TE_WriteVector", smn_TEWriteVector}, diff --git a/plugins/include/sdktools_tempents.inc b/plugins/include/sdktools_tempents.inc index 510d90d6..9bfe932b 100644 --- a/plugins/include/sdktools_tempents.inc +++ b/plugins/include/sdktools_tempents.inc @@ -98,6 +98,24 @@ native void TE_WriteNum(const char[] prop, int value); */ native int TE_ReadNum(const char[] prop); +/** + * Sets an entity value in the current temp entity. + * + * @param prop Property to use. + * @param value Entity reference or index value to set. + * @error Property not found. + */ +native void TE_WriteEnt(const char[] prop, int value); + +/** + * Reads an entity value in the current temp entity. + * + * @param prop Property to use. + * @return Property value as backwards compatible entity reference. + * @error Property not found. + */ +native int TE_ReadEnt(const char[] prop); + /** * Sets a floating point number in the current temp entity. *