From c11164c4d9996b63de85188fac8775b7085765ea Mon Sep 17 00:00:00 2001 From: Fyren Date: Mon, 27 Apr 2026 21:58:49 -0400 Subject: [PATCH] Add KeyValues.Merge (#2184) * Add KeyValues.Merge * Disable for ep1 Co-authored-by: Copilot --------- Co-authored-by: Nicholas Hastings Co-authored-by: Copilot --- core/smn_keyvalues.cpp | 32 ++++++++++++++++++++++++++++++++ plugins/include/keyvalues.inc | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index 985e7805..25f2100f 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -1113,6 +1113,37 @@ static cell_t smn_KvGetSectionSymbol(IPluginContext *pCtx, const cell_t *params) return 1; } +static cell_t KeyValues_Merge(IPluginContext *pContext, const cell_t *params) +{ +#if SOURCE_ENGINE == SE_EPISODEONE + return pContext->ThrowNativeError("KeyValues.Merge is not supported on this engine version"); +#else + Handle_t hndl_this = static_cast(params[1]); + Handle_t hndl_other = static_cast(params[2]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk_this, *pStk_other; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=handlesys->ReadHandle(hndl_this, g_KeyValueType, &sec, (void **)&pStk_this)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_this, herr); + } + if ((herr=handlesys->ReadHandle(hndl_other, g_KeyValueType, &sec, (void **)&pStk_other)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_other, herr); + } + + pStk_this->pCurRoot.front()->RecursiveMergeKeyValues(pStk_other->pCurRoot.front()); + + return 1; +#endif +} + static cell_t KeyValues_Import(IPluginContext *pContext, const cell_t *params) { // This version takes (dest, src). The original is (src, dest). @@ -1256,6 +1287,7 @@ REGISTER_NATIVES(keyvaluenatives) {"KeyValues.ExportToFile", smn_KeyValuesToFile}, {"KeyValues.ExportToString", smn_KeyValuesToString}, {"KeyValues.ExportLength.get", smn_KeyValuesExportLength}, + {"KeyValues.Merge", KeyValues_Merge}, {NULL, NULL} }; diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index 3ebda8c1..14b8ae54 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -334,6 +334,13 @@ methodmap KeyValues < Handle // @param id Id of the current section. // @return True on success, false on failure. public native bool GetSectionSymbol(int &id); + + // Merge from the current position of another KeyValues into the current + // position of this one. + // + // @param other KeyValues Handle to merge from. + // @error Invalid Handle or unsupported engine version. + public native void Merge(KeyValues other); }; /**