From 6f188fd54ee12716ca2bbd5ffb733886978b6dc4 Mon Sep 17 00:00:00 2001 From: Vladimir <47463683+Wend4r@users.noreply.github.com> Date: Tue, 11 Oct 2022 17:30:22 +0300 Subject: [PATCH 1/3] Push `KeyValues.Export()` method --- core/smn_keyvalues.cpp | 31 +++++++++++++++++++++++++++++++ plugins/include/keyvalues.inc | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index 8d018820af..c6b9687790 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -751,6 +751,36 @@ static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params) return pStk->pCurRoot.front()->GetDataType(name); } +staitc cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) +{ + Handle_t hndl = static_cast(params[1]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk; + char *name; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=handlesys->ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + pCtx->LocalToString(params[2], &name); + + KeyValues *pNewKV = new KeyValues(name[0] == '\0' ? NULL : name); + + *pNewKV = *pStk->pCurRoot; // KeyValues::operator= to recursive copy. + + KeyValueStack *pExportStk = new KeyValueStack; + + pExportStk->pBase = pNewKV; + pExportStk->pCurRoot.push(pNewKV); + + return handlesys->CreateHandle(g_KeyValueType, pExportStk, pCtx->GetIdentity(), g_pCoreIdent, NULL); +} + static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params) { Handle_t hndl = static_cast(params[1]); @@ -1241,6 +1271,7 @@ REGISTER_NATIVES(keyvaluenatives) {"KeyValues.Import", KeyValues_Import}, {"KeyValues.ImportFromFile", smn_FileToKeyValues}, {"KeyValues.ImportFromString", smn_StringToKeyValues}, + {"KeyValues.Export", smn_KeyValuesExport}, {"KeyValues.ExportToFile", smn_KeyValuesToFile}, {"KeyValues.ExportToString", smn_KeyValuesToString}, {"KeyValues.ExportLength.get", smn_KeyValuesExportLength}, diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index 7e38ad1069..58f3b886dc 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -62,6 +62,14 @@ 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=""); + // Exports a KeyValues tree to new descriptor. The tree is dumped from the current position. + // The Handle must be closed. + // + // @param name Name of the root section where the export is placed. + // If NULL_STIRNG, export without a root section. + // @return A Handle to a new KeyValues structure. + public native KeyValues Export(const char[] name = NULL_STRING); + // Exports a KeyValues tree to a file. The tree is dumped from the current position. // // @param file File to dump write to. From 873fa9959cc3f8bb96ea88f62bd46b41d5b77453 Mon Sep 17 00:00:00 2001 From: Wend4r <47463683+Wend4r@users.noreply.github.com> Date: Tue, 11 Oct 2022 17:59:13 +0300 Subject: [PATCH 2/3] Fix compiler errors --- core/smn_keyvalues.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index c6b9687790..beed4b8fa8 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -751,7 +751,7 @@ static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params) return pStk->pCurRoot.front()->GetDataType(name); } -staitc cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) +static cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) { Handle_t hndl = static_cast(params[1]); HandleError herr; @@ -771,7 +771,7 @@ staitc cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) KeyValues *pNewKV = new KeyValues(name[0] == '\0' ? NULL : name); - *pNewKV = *pStk->pCurRoot; // KeyValues::operator= to recursive copy. + *pNewKV = *(pStk->pCurRoot.front()); // KeyValues::operator= to recursive copy. KeyValueStack *pExportStk = new KeyValueStack; From 630277c4dcfdd0ab7b23b787e4a325fece0f6700 Mon Sep 17 00:00:00 2001 From: Wend4r <47463683+Wend4r@users.noreply.github.com> Date: Tue, 11 Oct 2022 18:54:50 +0300 Subject: [PATCH 3/3] Remove not working argument --- core/smn_keyvalues.cpp | 6 ++---- plugins/include/keyvalues.inc | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index beed4b8fa8..439e643b7e 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -757,7 +757,6 @@ static cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) HandleError herr; HandleSecurity sec; KeyValueStack *pStk; - char *name; sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; @@ -767,11 +766,10 @@ static cell_t smn_KeyValuesExport(IPluginContext *pCtx, const cell_t *params) { return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); } - pCtx->LocalToString(params[2], &name); - KeyValues *pNewKV = new KeyValues(name[0] == '\0' ? NULL : name); + KeyValues *pNewKV = new KeyValues(NULL /* Will be initialized in KeyValues::operator=() */ ); - *pNewKV = *(pStk->pCurRoot.front()); // KeyValues::operator= to recursive copy. + *pNewKV = *(pStk->pCurRoot.front()); // KeyValues::operator=() to recursive copy. KeyValueStack *pExportStk = new KeyValueStack; diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index 58f3b886dc..d9f42dac75 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -65,10 +65,8 @@ methodmap KeyValues < Handle // Exports a KeyValues tree to new descriptor. The tree is dumped from the current position. // The Handle must be closed. // - // @param name Name of the root section where the export is placed. - // If NULL_STIRNG, export without a root section. // @return A Handle to a new KeyValues structure. - public native KeyValues Export(const char[] name = NULL_STRING); + public native KeyValues Export(); // Exports a KeyValues tree to a file. The tree is dumped from the current position. //