1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/smn_keyvalues.cpp Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,1122 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "sourcemod.h"
1.36 +#include "sourcemm_api.h"
1.37 +#include "sm_stringutil.h"
1.38 +#include "HandleSys.h"
1.39 +#include "HalfLife2.h"
1.40 +#include <KeyValues.h>
1.41 +#include "utlbuffer.h"
1.42 +
1.43 +HandleType_t g_KeyValueType;
1.44 +
1.45 +struct KeyValueStack
1.46 +{
1.47 + KeyValues *pBase;
1.48 + CStack<KeyValues *> pCurRoot;
1.49 +};
1.50 +
1.51 +class KeyValueNatives :
1.52 + public SMGlobalClass,
1.53 + public IHandleTypeDispatch
1.54 +{
1.55 +public:
1.56 + void OnSourceModAllInitialized()
1.57 + {
1.58 + g_KeyValueType = g_HandleSys.CreateType("KeyValues", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.59 + }
1.60 + void OnSourceModShutdown()
1.61 + {
1.62 + g_HandleSys.RemoveType(g_KeyValueType, g_pCoreIdent);
1.63 + g_KeyValueType = 0;
1.64 + }
1.65 + void OnHandleDestroy(HandleType_t type, void *object)
1.66 + {
1.67 + KeyValueStack *pStk = reinterpret_cast<KeyValueStack *>(object);
1.68 + pStk->pBase->deleteThis();
1.69 + delete pStk;
1.70 + }
1.71 + int CalcKVSizeR(KeyValues *pv)
1.72 + {
1.73 + CUtlBuffer buf;
1.74 + int size;
1.75 +
1.76 + pv->RecursiveSaveToFile(buf, 0);
1.77 + size = buf.TellMaxPut();
1.78 +
1.79 + buf.Purge();
1.80 +
1.81 + return size;
1.82 + }
1.83 + bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
1.84 + {
1.85 + KeyValueStack *pStk = (KeyValueStack *)object;
1.86 + unsigned int size = sizeof(KeyValueStack) + (pStk->pCurRoot.size() * sizeof(KeyValues *));
1.87 +
1.88 + /* Check how much memory the actual thing takes up */
1.89 + size += CalcKVSizeR(pStk->pBase);
1.90 +
1.91 + *pSize = size;
1.92 +
1.93 + return true;
1.94 + }
1.95 +};
1.96 +
1.97 +KeyValues *SourceModBase::ReadKeyValuesHandle(Handle_t hndl, HandleError *err, bool root)
1.98 +{
1.99 + HandleError herr;
1.100 + HandleSecurity sec;
1.101 + KeyValueStack *pStk;
1.102 +
1.103 + sec.pOwner = NULL;
1.104 + sec.pIdentity = g_pCoreIdent;
1.105 +
1.106 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.107 + != HandleError_None)
1.108 + {
1.109 + if (err)
1.110 + {
1.111 + *err = herr;
1.112 + }
1.113 + return NULL;
1.114 + }
1.115 +
1.116 + if (err)
1.117 + {
1.118 + *err = HandleError_None;
1.119 + }
1.120 +
1.121 + return (root) ? pStk->pBase : pStk->pCurRoot.front();
1.122 +}
1.123 +
1.124 +static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params)
1.125 +{
1.126 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.127 + HandleError herr;
1.128 + HandleSecurity sec;
1.129 + KeyValueStack *pStk;
1.130 +
1.131 + sec.pOwner = NULL;
1.132 + sec.pIdentity = g_pCoreIdent;
1.133 +
1.134 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.135 + != HandleError_None)
1.136 + {
1.137 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.138 + }
1.139 +
1.140 + char *key, *value;
1.141 + pCtx->LocalToStringNULL(params[2], &key);
1.142 + pCtx->LocalToString(params[3], &value);
1.143 +
1.144 + pStk->pCurRoot.front()->SetString(key, value);
1.145 +
1.146 + return 1;
1.147 +}
1.148 +
1.149 +static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params)
1.150 +{
1.151 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.152 + HandleError herr;
1.153 + HandleSecurity sec;
1.154 + KeyValueStack *pStk;
1.155 +
1.156 + sec.pOwner = NULL;
1.157 + sec.pIdentity = g_pCoreIdent;
1.158 +
1.159 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.160 + != HandleError_None)
1.161 + {
1.162 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.163 + }
1.164 +
1.165 + char *key;
1.166 + pCtx->LocalToStringNULL(params[2], &key);
1.167 +
1.168 + pStk->pCurRoot.front()->SetInt(key, params[3]);
1.169 +
1.170 + return 1;
1.171 +}
1.172 +
1.173 +static cell_t smn_KvSetUInt64(IPluginContext *pCtx, const cell_t *params)
1.174 +{
1.175 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.176 + HandleError herr;
1.177 + HandleSecurity sec;
1.178 + KeyValueStack *pStk;
1.179 +
1.180 + sec.pOwner = NULL;
1.181 + sec.pIdentity = g_pCoreIdent;
1.182 +
1.183 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.184 + != HandleError_None)
1.185 + {
1.186 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.187 + }
1.188 +
1.189 + char *key;
1.190 + cell_t *addr;
1.191 + uint64 value;
1.192 + pCtx->LocalToStringNULL(params[2], &key);
1.193 + pCtx->LocalToPhysAddr(params[3], &addr);
1.194 +
1.195 + value = static_cast<uint64>(*addr);
1.196 + pStk->pCurRoot.front()->SetUint64(key, value);
1.197 +
1.198 + return 1;
1.199 +}
1.200 +
1.201 +static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params)
1.202 +{
1.203 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.204 + HandleError herr;
1.205 + HandleSecurity sec;
1.206 + KeyValueStack *pStk;
1.207 +
1.208 + sec.pOwner = NULL;
1.209 + sec.pIdentity = g_pCoreIdent;
1.210 +
1.211 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.212 + != HandleError_None)
1.213 + {
1.214 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.215 + }
1.216 +
1.217 + char *key;
1.218 + pCtx->LocalToStringNULL(params[2], &key);
1.219 +
1.220 + pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3]));
1.221 +
1.222 + return 1;
1.223 +}
1.224 +
1.225 +static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params)
1.226 +{
1.227 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.228 + HandleError herr;
1.229 + HandleSecurity sec;
1.230 + KeyValueStack *pStk;
1.231 +
1.232 + sec.pOwner = NULL;
1.233 + sec.pIdentity = g_pCoreIdent;
1.234 +
1.235 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.236 + != HandleError_None)
1.237 + {
1.238 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.239 + }
1.240 +
1.241 + char *key;
1.242 + pCtx->LocalToStringNULL(params[2], &key);
1.243 +
1.244 + Color color(params[3], params[4], params[5], params[6]);
1.245 + pStk->pCurRoot.front()->SetColor(key, color);
1.246 +
1.247 + return 1;
1.248 +}
1.249 +
1.250 +static cell_t smn_KvSetVector(IPluginContext *pCtx, const cell_t *params)
1.251 +{
1.252 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.253 + HandleError herr;
1.254 + HandleSecurity sec;
1.255 + KeyValueStack *pStk;
1.256 +
1.257 + sec.pOwner = NULL;
1.258 + sec.pIdentity = g_pCoreIdent;
1.259 +
1.260 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.261 + != HandleError_None)
1.262 + {
1.263 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.264 + }
1.265 +
1.266 + char *key;
1.267 + char buffer[64];
1.268 + cell_t *vector;
1.269 + pCtx->LocalToStringNULL(params[2], &key);
1.270 + pCtx->LocalToPhysAddr(params[3], &vector);
1.271 +
1.272 + UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(vector[0]), sp_ctof(vector[1]), sp_ctof(vector[2]));
1.273 +
1.274 + pStk->pCurRoot.front()->SetString(key, buffer);
1.275 +
1.276 + return 1;
1.277 +}
1.278 +
1.279 +static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params)
1.280 +{
1.281 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.282 + HandleError herr;
1.283 + HandleSecurity sec;
1.284 + KeyValueStack *pStk;
1.285 +
1.286 + sec.pOwner = NULL;
1.287 + sec.pIdentity = g_pCoreIdent;
1.288 +
1.289 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.290 + != HandleError_None)
1.291 + {
1.292 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.293 + }
1.294 +
1.295 + const char *value;
1.296 + char *key, *defvalue;
1.297 + pCtx->LocalToStringNULL(params[2], &key);
1.298 + pCtx->LocalToString(params[5], &defvalue);
1.299 +
1.300 + value = pStk->pCurRoot.front()->GetString(key, defvalue);
1.301 + pCtx->StringToLocalUTF8(params[3], params[4], value, NULL);
1.302 +
1.303 + return 1;
1.304 +}
1.305 +
1.306 +static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params)
1.307 +{
1.308 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.309 + HandleError herr;
1.310 + HandleSecurity sec;
1.311 + KeyValueStack *pStk;
1.312 +
1.313 + sec.pOwner = NULL;
1.314 + sec.pIdentity = g_pCoreIdent;
1.315 +
1.316 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.317 + != HandleError_None)
1.318 + {
1.319 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.320 + }
1.321 +
1.322 + int value;
1.323 + char *key;
1.324 + pCtx->LocalToStringNULL(params[2], &key);
1.325 +
1.326 + value = pStk->pCurRoot.front()->GetInt(key, params[3]);
1.327 +
1.328 + return value;
1.329 +}
1.330 +
1.331 +static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params)
1.332 +{
1.333 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.334 + HandleError herr;
1.335 + HandleSecurity sec;
1.336 + KeyValueStack *pStk;
1.337 +
1.338 + sec.pOwner = NULL;
1.339 + sec.pIdentity = g_pCoreIdent;
1.340 +
1.341 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.342 + != HandleError_None)
1.343 + {
1.344 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.345 + }
1.346 +
1.347 + float value;
1.348 + char *key;
1.349 + pCtx->LocalToStringNULL(params[2], &key);
1.350 +
1.351 + value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3]));
1.352 +
1.353 + return sp_ftoc(value);
1.354 +}
1.355 +
1.356 +static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params)
1.357 +{
1.358 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.359 + HandleError herr;
1.360 + HandleSecurity sec;
1.361 + KeyValueStack *pStk;
1.362 +
1.363 + sec.pOwner = NULL;
1.364 + sec.pIdentity = g_pCoreIdent;
1.365 +
1.366 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.367 + != HandleError_None)
1.368 + {
1.369 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.370 + }
1.371 +
1.372 + Color color;
1.373 + char *key;
1.374 + cell_t *r, *g, *b, *a;
1.375 + pCtx->LocalToStringNULL(params[2], &key);
1.376 + pCtx->LocalToPhysAddr(params[3], &r);
1.377 + pCtx->LocalToPhysAddr(params[4], &g);
1.378 + pCtx->LocalToPhysAddr(params[5], &b);
1.379 + pCtx->LocalToPhysAddr(params[6], &a);
1.380 +
1.381 + color = pStk->pCurRoot.front()->GetColor(key);
1.382 + *r = color.r();
1.383 + *g = color.g();
1.384 + *b = color.b();
1.385 + *a = color.a();
1.386 +
1.387 + return 1;
1.388 +}
1.389 +
1.390 +static cell_t smn_KvGetUInt64(IPluginContext *pCtx, const cell_t *params)
1.391 +{
1.392 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.393 + HandleError herr;
1.394 + HandleSecurity sec;
1.395 + KeyValueStack *pStk;
1.396 +
1.397 + sec.pOwner = NULL;
1.398 + sec.pIdentity = g_pCoreIdent;
1.399 +
1.400 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.401 + != HandleError_None)
1.402 + {
1.403 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.404 + }
1.405 +
1.406 + char *key;
1.407 + cell_t *addr, *defvalue;
1.408 + uint64 value;
1.409 + pCtx->LocalToStringNULL(params[2], &key);
1.410 + pCtx->LocalToPhysAddr(params[3], &addr);
1.411 + pCtx->LocalToPhysAddr(params[4], &defvalue);
1.412 +
1.413 + value = pStk->pCurRoot.front()->GetUint64(key, static_cast<uint64>(*defvalue));
1.414 + *reinterpret_cast<uint64 *>(addr) = value;
1.415 +
1.416 + return 1;
1.417 +}
1.418 +
1.419 +static cell_t smn_KvGetVector(IPluginContext *pCtx, const cell_t *params)
1.420 +{
1.421 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.422 + HandleError herr;
1.423 + HandleSecurity sec;
1.424 + KeyValueStack *pStk;
1.425 +
1.426 + sec.pOwner = NULL;
1.427 + sec.pIdentity = g_pCoreIdent;
1.428 +
1.429 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.430 + != HandleError_None)
1.431 + {
1.432 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.433 + }
1.434 +
1.435 + char *key;
1.436 + const char *value;
1.437 + cell_t *defvector, *outvector;
1.438 + char buffer[64];
1.439 + pCtx->LocalToStringNULL(params[2], &key);
1.440 + pCtx->LocalToPhysAddr(params[3], &outvector);
1.441 + pCtx->LocalToPhysAddr(params[4], &defvector);
1.442 +
1.443 + UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(defvector[0]), sp_ctof(defvector[1]), sp_ctof(defvector[2]));
1.444 +
1.445 + value = pStk->pCurRoot.front()->GetString(key, buffer);
1.446 +
1.447 + float out;
1.448 + int components = 0;
1.449 + while (*value && components < 3)
1.450 + {
1.451 + while ((*value) && (*value == ' '))
1.452 + {
1.453 + value++;
1.454 + }
1.455 +
1.456 + out = 0.0f;
1.457 + bool isnegative;
1.458 + if (*value == '-')
1.459 + {
1.460 + isnegative = true;
1.461 + value++;
1.462 + }
1.463 + else
1.464 + {
1.465 + isnegative = false;
1.466 + }
1.467 +
1.468 + for (; *value && isdigit(*value); ++value)
1.469 + {
1.470 + out *= 10.0f;
1.471 + out += *value - '0';
1.472 + }
1.473 +
1.474 + if (*value == '.')
1.475 + {
1.476 + value++;
1.477 + float factor = 0.1f;
1.478 + for (; *value && isdigit(*value); ++value)
1.479 + {
1.480 + out += (*value - '0') * factor;
1.481 + factor *= 0.1f;
1.482 + }
1.483 + }
1.484 +
1.485 + out = (isnegative) ? -out : out;
1.486 + outvector[components++] = sp_ftoc(out);
1.487 + }
1.488 +
1.489 + return 1;
1.490 +}
1.491 +
1.492 +static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params)
1.493 +{
1.494 + KeyValueStack *pStk;
1.495 + char *name, *firstkey, *firstvalue;
1.496 + bool is_empty;
1.497 +
1.498 + pCtx->LocalToString(params[1], &name);
1.499 + pCtx->LocalToString(params[2], &firstkey);
1.500 + pCtx->LocalToString(params[3], &firstvalue);
1.501 +
1.502 + is_empty = (firstkey[0] == '\0');
1.503 + pStk = new KeyValueStack;
1.504 + pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue);
1.505 + pStk->pCurRoot.push(pStk->pBase);
1.506 +
1.507 + return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL);
1.508 +}
1.509 +
1.510 +static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params)
1.511 +{
1.512 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.513 + HandleError herr;
1.514 + HandleSecurity sec;
1.515 + char *name;
1.516 + KeyValueStack *pStk;
1.517 +
1.518 + sec.pOwner = NULL;
1.519 + sec.pIdentity = g_pCoreIdent;
1.520 +
1.521 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.522 + != HandleError_None)
1.523 + {
1.524 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.525 + }
1.526 +
1.527 + pCtx->LocalToString(params[2], &name);
1.528 +
1.529 + KeyValues *pSubKey = pStk->pCurRoot.front();
1.530 + pSubKey = pSubKey->FindKey(name, (params[3]) ? true : false);
1.531 + if (!pSubKey)
1.532 + {
1.533 + return 0;
1.534 + }
1.535 + pStk->pCurRoot.push(pSubKey);
1.536 +
1.537 + return 1;
1.538 +}
1.539 +
1.540 +static cell_t smn_KvJumpToKeySymbol(IPluginContext *pCtx, const cell_t *params)
1.541 +{
1.542 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.543 + HandleError herr;
1.544 + HandleSecurity sec;
1.545 + KeyValueStack *pStk;
1.546 +
1.547 + sec.pOwner = NULL;
1.548 + sec.pIdentity = g_pCoreIdent;
1.549 +
1.550 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.551 + != HandleError_None)
1.552 + {
1.553 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.554 + }
1.555 +
1.556 + KeyValues *pSubKey = pStk->pCurRoot.front();
1.557 + pSubKey = pSubKey->FindKey(params[2]);
1.558 + if (!pSubKey)
1.559 + {
1.560 + return 0;
1.561 + }
1.562 + pStk->pCurRoot.push(pSubKey);
1.563 +
1.564 + return 1;
1.565 +}
1.566 +
1.567 +static cell_t smn_KvGotoFirstSubKey(IPluginContext *pCtx, const cell_t *params)
1.568 +{
1.569 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.570 + HandleError herr;
1.571 + HandleSecurity sec;
1.572 + KeyValueStack *pStk;
1.573 +
1.574 + sec.pOwner = NULL;
1.575 + sec.pIdentity = g_pCoreIdent;
1.576 +
1.577 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.578 + != HandleError_None)
1.579 + {
1.580 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.581 + }
1.582 +
1.583 + KeyValues *pSubKey = pStk->pCurRoot.front();
1.584 + KeyValues *pFirstSubKey;
1.585 + if (params[2])
1.586 + {
1.587 + pFirstSubKey = pSubKey->GetFirstTrueSubKey();
1.588 + } else {
1.589 + pFirstSubKey = pSubKey->GetFirstSubKey();
1.590 + }
1.591 +
1.592 + if (!pFirstSubKey)
1.593 + {
1.594 + return 0;
1.595 + }
1.596 + pStk->pCurRoot.push(pFirstSubKey);
1.597 +
1.598 + return 1;
1.599 +}
1.600 +
1.601 +static cell_t smn_KvGotoNextKey(IPluginContext *pCtx, const cell_t *params)
1.602 +{
1.603 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.604 + HandleError herr;
1.605 + HandleSecurity sec;
1.606 + KeyValueStack *pStk;
1.607 +
1.608 + sec.pOwner = NULL;
1.609 + sec.pIdentity = g_pCoreIdent;
1.610 +
1.611 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.612 + != HandleError_None)
1.613 + {
1.614 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.615 + }
1.616 +
1.617 + KeyValues *pSubKey = pStk->pCurRoot.front();
1.618 + if (params[2])
1.619 + {
1.620 + pSubKey = pSubKey->GetNextTrueSubKey();
1.621 + } else {
1.622 + pSubKey = pSubKey->GetNextKey();
1.623 + }
1.624 + if (!pSubKey)
1.625 + {
1.626 + return 0;
1.627 + }
1.628 + pStk->pCurRoot.pop();
1.629 + pStk->pCurRoot.push(pSubKey);
1.630 +
1.631 + return 1;
1.632 +}
1.633 +
1.634 +static cell_t smn_KvGoBack(IPluginContext *pCtx, const cell_t *params)
1.635 +{
1.636 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.637 + HandleError herr;
1.638 + HandleSecurity sec;
1.639 + KeyValueStack *pStk;
1.640 +
1.641 + sec.pOwner = NULL;
1.642 + sec.pIdentity = g_pCoreIdent;
1.643 +
1.644 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.645 + != HandleError_None)
1.646 + {
1.647 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.648 + }
1.649 +
1.650 + if (pStk->pCurRoot.size() == 1)
1.651 + {
1.652 + return 0;
1.653 + }
1.654 + pStk->pCurRoot.pop();
1.655 +
1.656 + return 1;
1.657 +}
1.658 +
1.659 +static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params)
1.660 +{
1.661 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.662 + HandleError herr;
1.663 + HandleSecurity sec;
1.664 + KeyValueStack *pStk;
1.665 +
1.666 + sec.pOwner = NULL;
1.667 + sec.pIdentity = g_pCoreIdent;
1.668 +
1.669 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.670 + != HandleError_None)
1.671 + {
1.672 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.673 + }
1.674 +
1.675 + while (pStk->pCurRoot.size() > 1)
1.676 + {
1.677 + pStk->pCurRoot.pop();
1.678 + }
1.679 +
1.680 + return 1;
1.681 +}
1.682 +
1.683 +static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params)
1.684 +{
1.685 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.686 + HandleError herr;
1.687 + HandleSecurity sec;
1.688 + KeyValueStack *pStk;
1.689 +
1.690 + sec.pOwner = NULL;
1.691 + sec.pIdentity = g_pCoreIdent;
1.692 +
1.693 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.694 + != HandleError_None)
1.695 + {
1.696 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.697 + }
1.698 +
1.699 + KeyValues *pSection = pStk->pCurRoot.front();
1.700 + const char *name = pSection->GetName();
1.701 + if (!name)
1.702 + {
1.703 + return 0;
1.704 + }
1.705 + pCtx->StringToLocalUTF8(params[2], params[3], name, NULL);
1.706 +
1.707 + return 1;
1.708 +}
1.709 +
1.710 +static cell_t smn_KvSetSectionName(IPluginContext *pCtx, const cell_t *params)
1.711 +{
1.712 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.713 + HandleError herr;
1.714 + HandleSecurity sec;
1.715 + char *name;
1.716 + KeyValueStack *pStk;
1.717 +
1.718 + sec.pOwner = NULL;
1.719 + sec.pIdentity = g_pCoreIdent;
1.720 +
1.721 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.722 + != HandleError_None)
1.723 + {
1.724 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.725 + }
1.726 +
1.727 + pCtx->LocalToString(params[2], &name);
1.728 +
1.729 + KeyValues *pSection = pStk->pCurRoot.front();
1.730 + pSection->SetName(name);
1.731 +
1.732 + return 1;
1.733 +}
1.734 +
1.735 +static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params)
1.736 +{
1.737 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.738 + HandleError herr;
1.739 + HandleSecurity sec;
1.740 + char *name;
1.741 + KeyValueStack *pStk;
1.742 +
1.743 + sec.pOwner = NULL;
1.744 + sec.pIdentity = g_pCoreIdent;
1.745 +
1.746 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.747 + != HandleError_None)
1.748 + {
1.749 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.750 + }
1.751 +
1.752 + pCtx->LocalToString(params[2], &name);
1.753 +
1.754 + return pStk->pCurRoot.front()->GetDataType(name);
1.755 +}
1.756 +
1.757 +static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params)
1.758 +{
1.759 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.760 + HandleError herr;
1.761 + HandleSecurity sec;
1.762 + char *path;
1.763 + KeyValueStack *pStk;
1.764 +
1.765 + sec.pOwner = NULL;
1.766 + sec.pIdentity = g_pCoreIdent;
1.767 +
1.768 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.769 + != HandleError_None)
1.770 + {
1.771 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.772 + }
1.773 +
1.774 + pCtx->LocalToString(params[2], &path);
1.775 +
1.776 + char realpath[PLATFORM_MAX_PATH];
1.777 + g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
1.778 +
1.779 + return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath);
1.780 +}
1.781 +
1.782 +static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
1.783 +{
1.784 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.785 + HandleError herr;
1.786 + HandleSecurity sec;
1.787 + char *path;
1.788 + KeyValueStack *pStk;
1.789 + KeyValues *kv;
1.790 +
1.791 + sec.pOwner = NULL;
1.792 + sec.pIdentity = g_pCoreIdent;
1.793 +
1.794 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.795 + != HandleError_None)
1.796 + {
1.797 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.798 + }
1.799 +
1.800 + pCtx->LocalToString(params[2], &path);
1.801 +
1.802 + char realpath[PLATFORM_MAX_PATH];
1.803 + g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
1.804 +
1.805 + kv = pStk->pCurRoot.front();
1.806 + return g_HL2.KVLoadFromFile(kv, basefilesystem, realpath);
1.807 +}
1.808 +
1.809 +static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
1.810 +{
1.811 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.812 + HandleError herr;
1.813 + HandleSecurity sec;
1.814 + KeyValueStack *pStk;
1.815 +
1.816 + sec.pOwner = NULL;
1.817 + sec.pIdentity = g_pCoreIdent;
1.818 +
1.819 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.820 + != HandleError_None)
1.821 + {
1.822 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.823 + }
1.824 +
1.825 + pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false);
1.826 +
1.827 + return 1;
1.828 +}
1.829 +
1.830 +static cell_t smn_KvNodesInStack(IPluginContext *pCtx, const cell_t *params)
1.831 +{
1.832 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.833 + HandleError herr;
1.834 + HandleSecurity sec;
1.835 + KeyValueStack *pStk;
1.836 +
1.837 + sec.pOwner = NULL;
1.838 + sec.pIdentity = g_pCoreIdent;
1.839 +
1.840 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.841 + != HandleError_None)
1.842 + {
1.843 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.844 + }
1.845 +
1.846 + return pStk->pCurRoot.size() - 1;
1.847 +}
1.848 +
1.849 +static cell_t smn_KvDeleteThis(IPluginContext *pContext, const cell_t *params)
1.850 +{
1.851 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.852 + HandleError herr;
1.853 + HandleSecurity sec;
1.854 + KeyValueStack *pStk;
1.855 +
1.856 + sec.pOwner = NULL;
1.857 + sec.pIdentity = g_pCoreIdent;
1.858 +
1.859 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.860 + != HandleError_None)
1.861 + {
1.862 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.863 + }
1.864 +
1.865 + if (pStk->pCurRoot.size() < 2)
1.866 + {
1.867 + return 0;
1.868 + }
1.869 +
1.870 + KeyValues *pValues = pStk->pCurRoot.front();
1.871 + pStk->pCurRoot.pop();
1.872 + KeyValues *pRoot = pStk->pCurRoot.front();
1.873 +
1.874 + /* We have to manually verify this since Valve sucks
1.875 + * :TODO: make our own KeyValues.h file and make
1.876 + * the sub stuff private so we can do this ourselves!
1.877 + */
1.878 + KeyValues *sub = pRoot->GetFirstSubKey();
1.879 + while (sub)
1.880 + {
1.881 + if (sub == pValues)
1.882 + {
1.883 + KeyValues *pNext = pValues->GetNextKey();
1.884 + pRoot->RemoveSubKey(pValues);
1.885 + pValues->deleteThis();
1.886 + if (pNext)
1.887 + {
1.888 + pStk->pCurRoot.push(pNext);
1.889 + return 1;
1.890 + } else {
1.891 + return -1;
1.892 + }
1.893 + }
1.894 + sub = sub->GetNextKey();
1.895 + }
1.896 +
1.897 + /* Push this back on :( */
1.898 + pStk->pCurRoot.push(pValues);
1.899 +
1.900 + return 0;
1.901 +}
1.902 +
1.903 +static cell_t smn_KvDeleteKey(IPluginContext *pContext, const cell_t *params)
1.904 +{
1.905 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.906 + HandleError herr;
1.907 + HandleSecurity sec;
1.908 + KeyValueStack *pStk;
1.909 +
1.910 + sec.pOwner = NULL;
1.911 + sec.pIdentity = g_pCoreIdent;
1.912 +
1.913 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.914 + != HandleError_None)
1.915 + {
1.916 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.917 + }
1.918 +
1.919 + if (pStk->pCurRoot.size() < 2)
1.920 + {
1.921 + return 0;
1.922 + }
1.923 +
1.924 + char *keyName;
1.925 + pContext->LocalToString(params[2], &keyName);
1.926 +
1.927 + KeyValues *pRoot = pStk->pCurRoot.front();
1.928 + KeyValues *pValues = pRoot->FindKey(keyName);
1.929 + if (!pValues)
1.930 + {
1.931 + return 0;
1.932 + }
1.933 +
1.934 + pRoot->RemoveSubKey(pValues);
1.935 + pValues->deleteThis();
1.936 +
1.937 + return 1;
1.938 +}
1.939 +
1.940 +static cell_t smn_KvSavePosition(IPluginContext *pContext, const cell_t *params)
1.941 +{
1.942 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.943 + HandleError herr;
1.944 + HandleSecurity sec;
1.945 + KeyValueStack *pStk;
1.946 +
1.947 + sec.pOwner = NULL;
1.948 + sec.pIdentity = g_pCoreIdent;
1.949 +
1.950 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.951 + != HandleError_None)
1.952 + {
1.953 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.954 + }
1.955 +
1.956 + if (pStk->pCurRoot.size() < 2)
1.957 + {
1.958 + return 0;
1.959 + }
1.960 +
1.961 + KeyValues *pValues = pStk->pCurRoot.front();
1.962 + pStk->pCurRoot.push(pValues);
1.963 +
1.964 + return 1;
1.965 +}
1.966 +
1.967 +static cell_t smn_CopySubkeys(IPluginContext *pContext, const cell_t *params)
1.968 +{
1.969 + Handle_t hndl_copied = static_cast<Handle_t>(params[1]);
1.970 + Handle_t hndl_parent = static_cast<Handle_t>(params[2]);
1.971 + HandleError herr;
1.972 + HandleSecurity sec;
1.973 + KeyValueStack *pStk_copied, *pStk_parent;
1.974 +
1.975 + sec.pOwner = NULL;
1.976 + sec.pIdentity = g_pCoreIdent;
1.977 +
1.978 + if ((herr=g_HandleSys.ReadHandle(hndl_copied, g_KeyValueType, &sec, (void **)&pStk_copied))
1.979 + != HandleError_None)
1.980 + {
1.981 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_copied, herr);
1.982 + }
1.983 + if ((herr=g_HandleSys.ReadHandle(hndl_parent, g_KeyValueType, &sec, (void **)&pStk_parent))
1.984 + != HandleError_None)
1.985 + {
1.986 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_parent, herr);
1.987 + }
1.988 +
1.989 + pStk_copied->pCurRoot.front()->CopySubkeys(pStk_parent->pCurRoot.front());
1.990 +
1.991 + return 1;
1.992 +}
1.993 +
1.994 +static cell_t smn_GetNameSymbol(IPluginContext *pContext, const cell_t *params)
1.995 +{
1.996 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.997 + HandleError herr;
1.998 + HandleSecurity sec;
1.999 + KeyValueStack *pStk;
1.1000 + cell_t *val;
1.1001 + char *key;
1.1002 +
1.1003 + sec.pOwner = NULL;
1.1004 + sec.pIdentity = g_pCoreIdent;
1.1005 +
1.1006 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.1007 + != HandleError_None)
1.1008 + {
1.1009 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.1010 + }
1.1011 +
1.1012 + if (pStk->pCurRoot.size() < 2)
1.1013 + {
1.1014 + return 0;
1.1015 + }
1.1016 +
1.1017 + pContext->LocalToString(params[2], &key);
1.1018 +
1.1019 + KeyValues *pKv = pStk->pCurRoot.front()->FindKey(key);
1.1020 + if (!pKv)
1.1021 + {
1.1022 + return 0;
1.1023 + }
1.1024 + pContext->LocalToPhysAddr(params[3], &val);
1.1025 + *val = pKv->GetNameSymbol();
1.1026 +
1.1027 + return 1;
1.1028 +}
1.1029 +
1.1030 +static cell_t smn_FindKeyById(IPluginContext *pContext, const cell_t *params)
1.1031 +{
1.1032 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.1033 + HandleError herr;
1.1034 + HandleSecurity sec;
1.1035 + KeyValueStack *pStk;
1.1036 +
1.1037 + sec.pOwner = NULL;
1.1038 + sec.pIdentity = g_pCoreIdent;
1.1039 +
1.1040 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.1041 + != HandleError_None)
1.1042 + {
1.1043 + return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.1044 + }
1.1045 +
1.1046 + KeyValues *pKv = pStk->pCurRoot.front()->FindKey(params[2]);
1.1047 + if (!pKv)
1.1048 + {
1.1049 + return 0;
1.1050 + }
1.1051 +
1.1052 + pContext->StringToLocalUTF8(params[3], params[4], pKv->GetName(), NULL);
1.1053 +
1.1054 + return 1;
1.1055 +}
1.1056 +
1.1057 +static cell_t smn_KvGetSectionSymbol(IPluginContext *pCtx, const cell_t *params)
1.1058 +{
1.1059 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.1060 + HandleError herr;
1.1061 + HandleSecurity sec;
1.1062 + KeyValueStack *pStk;
1.1063 + cell_t *val;
1.1064 +
1.1065 + sec.pOwner = NULL;
1.1066 + sec.pIdentity = g_pCoreIdent;
1.1067 +
1.1068 + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
1.1069 + != HandleError_None)
1.1070 + {
1.1071 + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
1.1072 + }
1.1073 +
1.1074 + KeyValues *pSection = pStk->pCurRoot.front();
1.1075 +
1.1076 + pCtx->LocalToPhysAddr(params[2], &val);
1.1077 + *val = pSection->GetNameSymbol();
1.1078 +
1.1079 + if (!*val)
1.1080 + {
1.1081 + return 0;
1.1082 + }
1.1083 +
1.1084 + return 1;
1.1085 +}
1.1086 +
1.1087 +static KeyValueNatives s_KeyValueNatives;
1.1088 +
1.1089 +REGISTER_NATIVES(keyvaluenatives)
1.1090 +{
1.1091 + {"KvSetString", smn_KvSetString},
1.1092 + {"KvSetNum", smn_KvSetNum},
1.1093 + {"KvSetUInt64", smn_KvSetUInt64},
1.1094 + {"KvSetFloat", smn_KvSetFloat},
1.1095 + {"KvSetColor", smn_KvSetColor},
1.1096 + {"KvGetString", smn_KvGetString},
1.1097 + {"KvGetNum", smn_KvGetNum},
1.1098 + {"KvGetFloat", smn_KvGetFloat},
1.1099 + {"KvGetColor", smn_KvGetColor},
1.1100 + {"KvGetUInt64", smn_KvGetUInt64},
1.1101 + {"CreateKeyValues", smn_CreateKeyValues},
1.1102 + {"KvJumpToKey", smn_KvJumpToKey},
1.1103 + {"KvJumpToKeySymbol", smn_KvJumpToKeySymbol},
1.1104 + {"KvGotoNextKey", smn_KvGotoNextKey},
1.1105 + {"KvGotoFirstSubKey", smn_KvGotoFirstSubKey},
1.1106 + {"KvGoBack", smn_KvGoBack},
1.1107 + {"KvRewind", smn_KvRewind},
1.1108 + {"KvGetSectionName", smn_KvGetSectionName},
1.1109 + {"KvSetSectionName", smn_KvSetSectionName},
1.1110 + {"KvGetDataType", smn_KvGetDataType},
1.1111 + {"KeyValuesToFile", smn_KeyValuesToFile},
1.1112 + {"FileToKeyValues", smn_FileToKeyValues},
1.1113 + {"KvSetEscapeSequences", smn_KvSetEscapeSequences},
1.1114 + {"KvDeleteThis", smn_KvDeleteThis},
1.1115 + {"KvDeleteKey", smn_KvDeleteKey},
1.1116 + {"KvNodesInStack", smn_KvNodesInStack},
1.1117 + {"KvSavePosition", smn_KvSavePosition},
1.1118 + {"KvCopySubkeys", smn_CopySubkeys},
1.1119 + {"KvFindKeyById", smn_FindKeyById},
1.1120 + {"KvGetNameSymbol", smn_GetNameSymbol},
1.1121 + {"KvGetSectionSymbol", smn_KvGetSectionSymbol},
1.1122 + {"KvGetVector", smn_KvGetVector},
1.1123 + {"KvSetVector", smn_KvSetVector},
1.1124 + {NULL, NULL}
1.1125 +};