ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "sourcemod.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "sm_stringutil.h" ds@1952: #include "HandleSys.h" ds@1952: #include "HalfLife2.h" ds@1952: #include ds@1952: #include "utlbuffer.h" ds@1952: ds@1952: HandleType_t g_KeyValueType; ds@1952: ds@1952: struct KeyValueStack ds@1952: { ds@1952: KeyValues *pBase; ds@1952: CStack pCurRoot; ds@1952: }; ds@1952: ds@1952: class KeyValueNatives : ds@1952: public SMGlobalClass, ds@1952: public IHandleTypeDispatch ds@1952: { ds@1952: public: ds@1952: void OnSourceModAllInitialized() ds@1952: { ds@1952: g_KeyValueType = g_HandleSys.CreateType("KeyValues", this, 0, NULL, NULL, g_pCoreIdent, NULL); ds@1952: } ds@1952: void OnSourceModShutdown() ds@1952: { ds@1952: g_HandleSys.RemoveType(g_KeyValueType, g_pCoreIdent); ds@1952: g_KeyValueType = 0; ds@1952: } ds@1952: void OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: KeyValueStack *pStk = reinterpret_cast(object); ds@1952: pStk->pBase->deleteThis(); ds@1952: delete pStk; ds@1952: } ds@1952: int CalcKVSizeR(KeyValues *pv) ds@1952: { ds@1952: CUtlBuffer buf; ds@1952: int size; ds@1952: ds@1952: pv->RecursiveSaveToFile(buf, 0); ds@1952: size = buf.TellMaxPut(); ds@1952: ds@1952: buf.Purge(); ds@1952: ds@1952: return size; ds@1952: } ds@1952: bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize) ds@1952: { ds@1952: KeyValueStack *pStk = (KeyValueStack *)object; ds@1952: unsigned int size = sizeof(KeyValueStack) + (pStk->pCurRoot.size() * sizeof(KeyValues *)); ds@1952: ds@1952: /* Check how much memory the actual thing takes up */ ds@1952: size += CalcKVSizeR(pStk->pBase); ds@1952: ds@1952: *pSize = size; ds@1952: ds@1952: return true; ds@1952: } ds@1952: }; ds@1952: ds@1952: KeyValues *SourceModBase::ReadKeyValuesHandle(Handle_t hndl, HandleError *err, bool root) ds@1952: { ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: if (err) ds@1952: { ds@1952: *err = herr; ds@1952: } ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: if (err) ds@1952: { ds@1952: *err = HandleError_None; ds@1952: } ds@1952: ds@1952: return (root) ? pStk->pBase : pStk->pCurRoot.front(); ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key, *value; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToString(params[3], &value); ds@1952: ds@1952: pStk->pCurRoot.front()->SetString(key, value); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: ds@1952: pStk->pCurRoot.front()->SetInt(key, params[3]); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetUInt64(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: cell_t *addr; ds@1952: uint64 value; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToPhysAddr(params[3], &addr); ds@1952: ds@1952: value = static_cast(*addr); ds@1952: pStk->pCurRoot.front()->SetUint64(key, value); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: ds@1952: pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3])); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: ds@1952: Color color(params[3], params[4], params[5], params[6]); ds@1952: pStk->pCurRoot.front()->SetColor(key, color); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetVector(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: char buffer[64]; ds@1952: cell_t *vector; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToPhysAddr(params[3], &vector); ds@1952: ds@1952: UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(vector[0]), sp_ctof(vector[1]), sp_ctof(vector[2])); ds@1952: ds@1952: pStk->pCurRoot.front()->SetString(key, buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: const char *value; ds@1952: char *key, *defvalue; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToString(params[5], &defvalue); ds@1952: ds@1952: value = pStk->pCurRoot.front()->GetString(key, defvalue); ds@1952: pCtx->StringToLocalUTF8(params[3], params[4], value, NULL); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: int value; ds@1952: char *key; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: ds@1952: value = pStk->pCurRoot.front()->GetInt(key, params[3]); ds@1952: ds@1952: return value; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: float value; ds@1952: char *key; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: ds@1952: value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3])); ds@1952: ds@1952: return sp_ftoc(value); ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: Color color; ds@1952: char *key; ds@1952: cell_t *r, *g, *b, *a; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToPhysAddr(params[3], &r); ds@1952: pCtx->LocalToPhysAddr(params[4], &g); ds@1952: pCtx->LocalToPhysAddr(params[5], &b); ds@1952: pCtx->LocalToPhysAddr(params[6], &a); ds@1952: ds@1952: color = pStk->pCurRoot.front()->GetColor(key); ds@1952: *r = color.r(); ds@1952: *g = color.g(); ds@1952: *b = color.b(); ds@1952: *a = color.a(); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetUInt64(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: cell_t *addr, *defvalue; ds@1952: uint64 value; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToPhysAddr(params[3], &addr); ds@1952: pCtx->LocalToPhysAddr(params[4], &defvalue); ds@1952: ds@1952: value = pStk->pCurRoot.front()->GetUint64(key, static_cast(*defvalue)); ds@1952: *reinterpret_cast(addr) = value; ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetVector(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: const char *value; ds@1952: cell_t *defvector, *outvector; ds@1952: char buffer[64]; ds@1952: pCtx->LocalToStringNULL(params[2], &key); ds@1952: pCtx->LocalToPhysAddr(params[3], &outvector); ds@1952: pCtx->LocalToPhysAddr(params[4], &defvector); ds@1952: ds@1952: UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(defvector[0]), sp_ctof(defvector[1]), sp_ctof(defvector[2])); ds@1952: ds@1952: value = pStk->pCurRoot.front()->GetString(key, buffer); ds@1952: ds@1952: float out; ds@1952: int components = 0; ds@1952: while (*value && components < 3) ds@1952: { ds@1952: while ((*value) && (*value == ' ')) ds@1952: { ds@1952: value++; ds@1952: } ds@1952: ds@1952: out = 0.0f; ds@1952: bool isnegative; ds@1952: if (*value == '-') ds@1952: { ds@1952: isnegative = true; ds@1952: value++; ds@1952: } ds@1952: else ds@1952: { ds@1952: isnegative = false; ds@1952: } ds@1952: ds@1952: for (; *value && isdigit(*value); ++value) ds@1952: { ds@1952: out *= 10.0f; ds@1952: out += *value - '0'; ds@1952: } ds@1952: ds@1952: if (*value == '.') ds@1952: { ds@1952: value++; ds@1952: float factor = 0.1f; ds@1952: for (; *value && isdigit(*value); ++value) ds@1952: { ds@1952: out += (*value - '0') * factor; ds@1952: factor *= 0.1f; ds@1952: } ds@1952: } ds@1952: ds@1952: out = (isnegative) ? -out : out; ds@1952: outvector[components++] = sp_ftoc(out); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: KeyValueStack *pStk; ds@1952: char *name, *firstkey, *firstvalue; ds@1952: bool is_empty; ds@1952: ds@1952: pCtx->LocalToString(params[1], &name); ds@1952: pCtx->LocalToString(params[2], &firstkey); ds@1952: pCtx->LocalToString(params[3], &firstvalue); ds@1952: ds@1952: is_empty = (firstkey[0] == '\0'); ds@1952: pStk = new KeyValueStack; ds@1952: pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue); ds@1952: pStk->pCurRoot.push(pStk->pBase); ds@1952: ds@1952: return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: char *name; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pCtx->LocalToString(params[2], &name); ds@1952: ds@1952: KeyValues *pSubKey = pStk->pCurRoot.front(); ds@1952: pSubKey = pSubKey->FindKey(name, (params[3]) ? true : false); ds@1952: if (!pSubKey) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pStk->pCurRoot.push(pSubKey); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvJumpToKeySymbol(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pSubKey = pStk->pCurRoot.front(); ds@1952: pSubKey = pSubKey->FindKey(params[2]); ds@1952: if (!pSubKey) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pStk->pCurRoot.push(pSubKey); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGotoFirstSubKey(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pSubKey = pStk->pCurRoot.front(); ds@1952: KeyValues *pFirstSubKey; ds@1952: if (params[2]) ds@1952: { ds@1952: pFirstSubKey = pSubKey->GetFirstTrueSubKey(); ds@1952: } else { ds@1952: pFirstSubKey = pSubKey->GetFirstSubKey(); ds@1952: } ds@1952: ds@1952: if (!pFirstSubKey) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pStk->pCurRoot.push(pFirstSubKey); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGotoNextKey(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pSubKey = pStk->pCurRoot.front(); ds@1952: if (params[2]) ds@1952: { ds@1952: pSubKey = pSubKey->GetNextTrueSubKey(); ds@1952: } else { ds@1952: pSubKey = pSubKey->GetNextKey(); ds@1952: } ds@1952: if (!pSubKey) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pStk->pCurRoot.pop(); ds@1952: pStk->pCurRoot.push(pSubKey); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGoBack(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (pStk->pCurRoot.size() == 1) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pStk->pCurRoot.pop(); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: while (pStk->pCurRoot.size() > 1) ds@1952: { ds@1952: pStk->pCurRoot.pop(); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pSection = pStk->pCurRoot.front(); ds@1952: const char *name = pSection->GetName(); ds@1952: if (!name) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pCtx->StringToLocalUTF8(params[2], params[3], name, NULL); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetSectionName(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: char *name; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pCtx->LocalToString(params[2], &name); ds@1952: ds@1952: KeyValues *pSection = pStk->pCurRoot.front(); ds@1952: pSection->SetName(name); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: char *name; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pCtx->LocalToString(params[2], &name); ds@1952: ds@1952: return pStk->pCurRoot.front()->GetDataType(name); ds@1952: } ds@1952: ds@1952: static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: char *path; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pCtx->LocalToString(params[2], &path); ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); ds@1952: ds@1952: return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath); ds@1952: } ds@1952: ds@1952: static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: char *path; ds@1952: KeyValueStack *pStk; ds@1952: KeyValues *kv; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pCtx->LocalToString(params[2], &path); ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); ds@1952: ds@1952: kv = pStk->pCurRoot.front(); ds@1952: return g_HL2.KVLoadFromFile(kv, basefilesystem, realpath); ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvNodesInStack(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return pStk->pCurRoot.size() - 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvDeleteThis(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (pStk->pCurRoot.size() < 2) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: KeyValues *pValues = pStk->pCurRoot.front(); ds@1952: pStk->pCurRoot.pop(); ds@1952: KeyValues *pRoot = pStk->pCurRoot.front(); ds@1952: ds@1952: /* We have to manually verify this since Valve sucks ds@1952: * :TODO: make our own KeyValues.h file and make ds@1952: * the sub stuff private so we can do this ourselves! ds@1952: */ ds@1952: KeyValues *sub = pRoot->GetFirstSubKey(); ds@1952: while (sub) ds@1952: { ds@1952: if (sub == pValues) ds@1952: { ds@1952: KeyValues *pNext = pValues->GetNextKey(); ds@1952: pRoot->RemoveSubKey(pValues); ds@1952: pValues->deleteThis(); ds@1952: if (pNext) ds@1952: { ds@1952: pStk->pCurRoot.push(pNext); ds@1952: return 1; ds@1952: } else { ds@1952: return -1; ds@1952: } ds@1952: } ds@1952: sub = sub->GetNextKey(); ds@1952: } ds@1952: ds@1952: /* Push this back on :( */ ds@1952: pStk->pCurRoot.push(pValues); ds@1952: ds@1952: return 0; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvDeleteKey(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (pStk->pCurRoot.size() < 2) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char *keyName; ds@1952: pContext->LocalToString(params[2], &keyName); ds@1952: ds@1952: KeyValues *pRoot = pStk->pCurRoot.front(); ds@1952: KeyValues *pValues = pRoot->FindKey(keyName); ds@1952: if (!pValues) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pRoot->RemoveSubKey(pValues); ds@1952: pValues->deleteThis(); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvSavePosition(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (pStk->pCurRoot.size() < 2) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: KeyValues *pValues = pStk->pCurRoot.front(); ds@1952: pStk->pCurRoot.push(pValues); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_CopySubkeys(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl_copied = static_cast(params[1]); ds@1952: Handle_t hndl_parent = static_cast(params[2]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk_copied, *pStk_parent; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl_copied, g_KeyValueType, &sec, (void **)&pStk_copied)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_copied, herr); ds@1952: } ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl_parent, g_KeyValueType, &sec, (void **)&pStk_parent)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_parent, herr); ds@1952: } ds@1952: ds@1952: pStk_copied->pCurRoot.front()->CopySubkeys(pStk_parent->pCurRoot.front()); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_GetNameSymbol(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: cell_t *val; ds@1952: char *key; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (pStk->pCurRoot.size() < 2) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: KeyValues *pKv = pStk->pCurRoot.front()->FindKey(key); ds@1952: if (!pKv) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: pContext->LocalToPhysAddr(params[3], &val); ds@1952: *val = pKv->GetNameSymbol(); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_FindKeyById(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pKv = pStk->pCurRoot.front()->FindKey(params[2]); ds@1952: if (!pKv) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pContext->StringToLocalUTF8(params[3], params[4], pKv->GetName(), NULL); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_KvGetSectionSymbol(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: KeyValueStack *pStk; ds@1952: cell_t *val; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: KeyValues *pSection = pStk->pCurRoot.front(); ds@1952: ds@1952: pCtx->LocalToPhysAddr(params[2], &val); ds@1952: *val = pSection->GetNameSymbol(); ds@1952: ds@1952: if (!*val) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static KeyValueNatives s_KeyValueNatives; ds@1952: ds@1952: REGISTER_NATIVES(keyvaluenatives) ds@1952: { ds@1952: {"KvSetString", smn_KvSetString}, ds@1952: {"KvSetNum", smn_KvSetNum}, ds@1952: {"KvSetUInt64", smn_KvSetUInt64}, ds@1952: {"KvSetFloat", smn_KvSetFloat}, ds@1952: {"KvSetColor", smn_KvSetColor}, ds@1952: {"KvGetString", smn_KvGetString}, ds@1952: {"KvGetNum", smn_KvGetNum}, ds@1952: {"KvGetFloat", smn_KvGetFloat}, ds@1952: {"KvGetColor", smn_KvGetColor}, ds@1952: {"KvGetUInt64", smn_KvGetUInt64}, ds@1952: {"CreateKeyValues", smn_CreateKeyValues}, ds@1952: {"KvJumpToKey", smn_KvJumpToKey}, ds@1952: {"KvJumpToKeySymbol", smn_KvJumpToKeySymbol}, ds@1952: {"KvGotoNextKey", smn_KvGotoNextKey}, ds@1952: {"KvGotoFirstSubKey", smn_KvGotoFirstSubKey}, ds@1952: {"KvGoBack", smn_KvGoBack}, ds@1952: {"KvRewind", smn_KvRewind}, ds@1952: {"KvGetSectionName", smn_KvGetSectionName}, ds@1952: {"KvSetSectionName", smn_KvSetSectionName}, ds@1952: {"KvGetDataType", smn_KvGetDataType}, ds@1952: {"KeyValuesToFile", smn_KeyValuesToFile}, ds@1952: {"FileToKeyValues", smn_FileToKeyValues}, ds@1952: {"KvSetEscapeSequences", smn_KvSetEscapeSequences}, ds@1952: {"KvDeleteThis", smn_KvDeleteThis}, ds@1952: {"KvDeleteKey", smn_KvDeleteKey}, ds@1952: {"KvNodesInStack", smn_KvNodesInStack}, ds@1952: {"KvSavePosition", smn_KvSavePosition}, ds@1952: {"KvCopySubkeys", smn_CopySubkeys}, ds@1952: {"KvFindKeyById", smn_FindKeyById}, ds@1952: {"KvGetNameSymbol", smn_GetNameSymbol}, ds@1952: {"KvGetSectionSymbol", smn_KvGetSectionSymbol}, ds@1952: {"KvGetVector", smn_KvGetVector}, ds@1952: {"KvSetVector", smn_KvSetVector}, ds@1952: {NULL, NULL} ds@1952: };