Add KeyValues.ExportToString (#706)
This commit is contained in:
parent
90ddc16a4b
commit
404e96ad45
@ -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<Handle_t>(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<size_t>(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<Handle_t>(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}
|
||||
};
|
||||
|
@ -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_
|
@ -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.
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user