From 1a59e557230711051cdc9f22bc1c2550202907a5 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Thu, 23 Jul 2026 19:02:33 -0400 Subject: [PATCH] Implement KeyValues.FromAddress (#1467) --- core/smn_keyvalues.cpp | 31 +++++++++++++++++++++++++++++++ plugins/include/keyvalues.inc | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index dbde6952..5e20709d 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -542,6 +542,36 @@ static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params) return handlesys->CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL); } +static cell_t smn_KeyValuesFromAddress(IPluginContext *pCtx, const cell_t *params) +{ + void *addr = reinterpret_cast(params[1]); + if (pCtx->GetRuntime()->FindPubvarByName("__Int64_Address__", nullptr) == SP_ERROR_NONE) + { + cell_t *sp_addr; + if (int err = pCtx->LocalToPhysAddr(params[1], &sp_addr); err != SP_ERROR_NONE) + { + return pCtx->ThrowNativeErrorEx(err, "Could not read argument"); + } + addr = reinterpret_cast(*reinterpret_cast(sp_addr)); + } + + if (addr == NULL) + { + return pCtx->ThrowNativeError("Address cannot be null"); + } + + /* The handle does not own the underlying KeyValues; it belongs to whoever + * gave us the pointer (typically the game engine), so don't delete it when + * the handle is closed. + */ + KeyValueStack *pStk = new KeyValueStack; + pStk->pBase = reinterpret_cast(addr); + pStk->pCurRoot.push(pStk->pBase); + pStk->m_bDeleteOnDestroy = false; + + return handlesys->CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL); +} + static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params) { Handle_t hndl = static_cast(params[1]); @@ -1291,6 +1321,7 @@ REGISTER_NATIVES(keyvaluenatives) // Transitional syntax support. {"KeyValues.KeyValues", smn_CreateKeyValues}, + {"KeyValues.FromAddress", smn_KeyValuesFromAddress}, {"KeyValues.SetString", smn_KvSetString}, {"KeyValues.SetNum", smn_KvSetNum}, {"KeyValues.SetUInt64", smn_KvSetUInt64}, diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index 14b8ae54..0ff35042 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -62,6 +62,19 @@ methodmap KeyValues < Handle // @param firstValue If firstKey is non-empty, specifies the first key's value. public native KeyValues(const char[] name, const char[] firstKey="", const char[] firstValue=""); + // Wraps an existing native KeyValues pointer in a KeyValues Handle, for + // example one obtained from the game through an SDKCall or a detour. + // + // The returned Handle does NOT own the underlying KeyValues: closing or + // deleting it will not free the pointed-to memory (which is owned by + // whoever created it, typically the game). The Handle must not be used + // after the underlying KeyValues has been freed. + // + // @param pKeyValues Address of a native KeyValues object. + // @return A KeyValues Handle wrapping the given pointer. + // @error Address is null. + public static native KeyValues FromAddress(Address pKeyValues); + // Exports a KeyValues tree to a file. The tree is dumped from the current position. // // @param file File to dump write to.