diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index 6cde5b9b..622c4506 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -637,6 +637,72 @@ static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *param return 1; } +static cell_t smn_KvDeleteThis(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=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + + if (pStk->pCurRoot.size() < 2) + { + return 0; + } + + KeyValues *pValues = pStk->pCurRoot.front(); + pStk->pCurRoot.pop(); + pStk->pCurRoot.front()->RemoveSubKey(pValues); + pValues->deleteThis(); + + return 1; +} + +static cell_t smn_KvDeleteKey(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=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + + if (pStk->pCurRoot.size() < 2) + { + return 0; + } + + char *keyName; + pContext->LocalToString(params[2], &keyName); + + KeyValues *pRoot = pStk->pCurRoot.front(); + KeyValues *pValues = pRoot->FindKey(keyName); + if (!pValues) + { + return 0; + } + + pRoot->RemoveSubKey(pValues); + pValues->deleteThis(); + + return 1; +} + static KeyValueNatives s_KeyValueNatives; REGISTER_NATIVES(keyvaluenatives) @@ -663,5 +729,7 @@ REGISTER_NATIVES(keyvaluenatives) {"KeyValuesToFile", smn_KeyValuesToFile}, {"FileToKeyValues", smn_FileToKeyValues}, {"KvSetEscapeSequences", smn_KvSetEscapeSequences}, + {"KvDeleteThis", smn_KvDeleteThis}, + {"KvDeleteKey", smn_KvDeleteKey}, {NULL, NULL} }; diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index edfcd6ce..9082bfec 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -192,6 +192,26 @@ native bool:KvJumpFirstSubKey(Handle:kv); */ native bool:KvJumpNextSubKey(Handle:kv); +/** + * Removes the given key from the current position. + * + * @param kv KeyValues Handle. + * @param key Name of the key. + * @return True on success, false if key did not exist. + * @error Invalid Handle. + */ +native bool:KvDeleteKey(Handle:kv, const String:key[]); + +/** + * Removes the current sub-key and jumps back one position, using the previous + * position as the search point. This will not work if used on the root node. + * + * @param kv KeyValues Handle. + * @return True on success, false if there was no sub key. + * @error Invalid Handle. + */ +native bool:KvDeleteThis(Handle:kv); + /** * Jumps back to the previous position. Returns false if there are no * previous positions (i.e., at the root node). This should be called