diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index 12e5239a..f5d4e54b 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -1113,6 +1113,61 @@ static cell_t KeyValues_Import(IPluginContext *pContext, const cell_t *params) return smn_CopySubkeys(pContext, new_params); } +static cell_t smn_KeyValuesToString(IPluginContext *pContext, const cell_t *params) +{ + Handle_t hndl = static_cast(params[1]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=handlesys->ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + KeyValues *kv; + CUtlBuffer buffer; + + kv = pStk->pCurRoot.front(); + + kv->RecursiveSaveToFile(buffer, 0); + + char* outStr; + pContext->LocalToString(params[2], &outStr); + size_t maxlen = static_cast(params[3]); + + buffer.GetString(outStr, maxlen); + return buffer.TellPut(); +} + +static cell_t smn_KeyValuesExportLength(IPluginContext *pContext, const cell_t *params) +{ + Handle_t hndl = static_cast(params[1]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=handlesys->ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + KeyValues *kv; + CUtlBuffer buffer; + + kv = pStk->pCurRoot.front(); + + kv->RecursiveSaveToFile(buffer, 0); + + return (cell_t)buffer.TellPut(); +} + static KeyValueNatives s_KeyValueNatives; REGISTER_NATIVES(keyvaluenatives) @@ -1187,6 +1242,8 @@ REGISTER_NATIVES(keyvaluenatives) {"KeyValues.ImportFromFile", smn_FileToKeyValues}, {"KeyValues.ImportFromString", smn_StringToKeyValues}, {"KeyValues.ExportToFile", smn_KeyValuesToFile}, + {"KeyValues.ExportToString", smn_KeyValuesToString}, + {"KeyValues.ExportLength.get", smn_KeyValuesExportLength}, {NULL, NULL} }; diff --git a/core/smn_keyvalues.h b/core/smn_keyvalues.h index de032506..61e83f3a 100644 --- a/core/smn_keyvalues.h +++ b/core/smn_keyvalues.h @@ -46,6 +46,6 @@ struct KeyValueStack bool m_bDeleteOnDestroy = true; }; -extern HandleType_t g_KeyValueType;; +extern HandleType_t g_KeyValueType; #endif // _INCLUDE_SOURCEMOD_KVWRAPPER_H_ \ No newline at end of file diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index afa228a1..134df4d1 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -68,6 +68,18 @@ methodmap KeyValues < Handle // @return True on success, false otherwise. public native bool ExportToFile(const char[] file); + // Exports a KeyValues tree to a string. The string is dumped from the current position. + // + // @param buffer Buffer to write to. + // @param maxlength Max length of buffer. + // @return Number of bytes that can be written to buffer. + public native int ExportToString(char[] buffer, int maxlength); + + // Amount of bytes written by ExportToFile & ExportToString. + property int ExportLength { + public native get(); + } + // Imports a file in KeyValues format. The file is read into the current // position of the tree. //