core/smn_keyvalues.cpp
changeset 654 e00dd6df311e
child 682 a70055d6d3b3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_keyvalues.cpp	Sun Mar 25 20:20:43 2007 +0000
     1.3 @@ -0,0 +1,639 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "sourcemod.h"
    1.19 +#include "sourcemm_api.h"
    1.20 +#include "sm_stringutil.h"
    1.21 +#include "HandleSys.h"
    1.22 +#include <KeyValues.h>
    1.23 +
    1.24 +HandleType_t g_KeyValueType;
    1.25 +
    1.26 +struct KeyValueStack 
    1.27 +{
    1.28 +	KeyValues *pBase;
    1.29 +	CStack<KeyValues *> pCurRoot;
    1.30 +};
    1.31 +
    1.32 +class KeyValuekNatives : 
    1.33 +	public SMGlobalClass,
    1.34 +	public IHandleTypeDispatch
    1.35 +{
    1.36 +public:
    1.37 +	void OnSourceModAllInitialized()
    1.38 +	{
    1.39 +		g_KeyValueType = g_HandleSys.CreateType("KeyValues", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.40 +	}
    1.41 +	void OnSourceModShutdown()
    1.42 +	{
    1.43 +		g_HandleSys.RemoveType(g_KeyValueType, g_pCoreIdent);
    1.44 +		g_KeyValueType = 0;
    1.45 +	}
    1.46 +	void OnHandleDestroy(HandleType_t type, void *object)
    1.47 +	{
    1.48 +		KeyValueStack *pStk = reinterpret_cast<KeyValueStack *>(object);
    1.49 +		pStk->pBase->deleteThis();
    1.50 +		delete pStk;
    1.51 +	}
    1.52 +};
    1.53 +
    1.54 +static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params)
    1.55 +{
    1.56 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.57 +	HandleError herr;
    1.58 +	HandleSecurity sec;
    1.59 +	KeyValueStack *pStk;
    1.60 +
    1.61 +	sec.pOwner = NULL;
    1.62 +	sec.pIdentity = g_pCoreIdent;
    1.63 +
    1.64 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
    1.65 +		!= HandleError_None)
    1.66 +	{
    1.67 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
    1.68 +	}
    1.69 +
    1.70 +	char *key, *value;
    1.71 +	pCtx->LocalToString(params[2], &key);
    1.72 +	pCtx->LocalToString(params[3], &value);
    1.73 +
    1.74 +	pStk->pCurRoot.front()->SetString(key, value);
    1.75 +
    1.76 +	return 1;
    1.77 +}
    1.78 +
    1.79 +static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params)
    1.80 +{
    1.81 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.82 +	HandleError herr;
    1.83 +	HandleSecurity sec;
    1.84 +	KeyValueStack *pStk;
    1.85 +
    1.86 +	sec.pOwner = NULL;
    1.87 +	sec.pIdentity = g_pCoreIdent;
    1.88 +
    1.89 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
    1.90 +		!= HandleError_None)
    1.91 +	{
    1.92 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
    1.93 +	}
    1.94 +
    1.95 +	char *key;
    1.96 +	pCtx->LocalToString(params[2], &key);
    1.97 +
    1.98 +	pStk->pCurRoot.front()->SetInt(key, params[3]);
    1.99 +
   1.100 +	return 1;
   1.101 +}
   1.102 +
   1.103 +static cell_t smn_KvSetUint64(IPluginContext *pCtx, const cell_t *params)
   1.104 +{
   1.105 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.106 +	HandleError herr;
   1.107 +	HandleSecurity sec;
   1.108 +	KeyValueStack *pStk;
   1.109 +
   1.110 +	sec.pOwner = NULL;
   1.111 +	sec.pIdentity = g_pCoreIdent;
   1.112 +
   1.113 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.114 +		!= HandleError_None)
   1.115 +	{
   1.116 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.117 +	}
   1.118 +
   1.119 +	char *key;
   1.120 +	cell_t *addr;
   1.121 +	uint64 value;
   1.122 +	pCtx->LocalToString(params[2], &key);
   1.123 +	pCtx->LocalToPhysAddr(params[3], &addr);
   1.124 +
   1.125 +	value = static_cast<uint64>(*addr);
   1.126 +	pStk->pCurRoot.front()->SetUint64(key, value);
   1.127 +
   1.128 +	return 1;
   1.129 +}
   1.130 +
   1.131 +static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params)
   1.132 +{
   1.133 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.134 +	HandleError herr;
   1.135 +	HandleSecurity sec;
   1.136 +	KeyValueStack *pStk;
   1.137 +
   1.138 +	sec.pOwner = NULL;
   1.139 +	sec.pIdentity = g_pCoreIdent;
   1.140 +
   1.141 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.142 +		!= HandleError_None)
   1.143 +	{
   1.144 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.145 +	}
   1.146 +
   1.147 +	char *key;
   1.148 +	pCtx->LocalToString(params[2], &key);
   1.149 +
   1.150 +	pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3]));
   1.151 +
   1.152 +	return 1;
   1.153 +}
   1.154 +
   1.155 +static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params)
   1.156 +{
   1.157 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.158 +	HandleError herr;
   1.159 +	HandleSecurity sec;
   1.160 +	KeyValueStack *pStk;
   1.161 +
   1.162 +	sec.pOwner = NULL;
   1.163 +	sec.pIdentity = g_pCoreIdent;
   1.164 +
   1.165 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.166 +		!= HandleError_None)
   1.167 +	{
   1.168 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.169 +	}
   1.170 +
   1.171 +	char *key;
   1.172 +	pCtx->LocalToString(params[2], &key);
   1.173 +
   1.174 +	Color color(params[3], params[4], params[5], params[6]);
   1.175 +	pStk->pCurRoot.front()->SetColor(key, color);
   1.176 +
   1.177 +	return 1;
   1.178 +}
   1.179 +
   1.180 +static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params)
   1.181 +{
   1.182 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.183 +	HandleError herr;
   1.184 +	HandleSecurity sec;
   1.185 +	KeyValueStack *pStk;
   1.186 +
   1.187 +	sec.pOwner = NULL;
   1.188 +	sec.pIdentity = g_pCoreIdent;
   1.189 +
   1.190 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.191 +		!= HandleError_None)
   1.192 +	{
   1.193 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.194 +	}
   1.195 +
   1.196 +	const char *value;
   1.197 +	char *key, *defvalue;
   1.198 +	pCtx->LocalToString(params[2], &key);
   1.199 +	pCtx->LocalToString(params[5], &defvalue);
   1.200 +
   1.201 +	value = pStk->pCurRoot.front()->GetString(key, defvalue);
   1.202 +	pCtx->StringToLocalUTF8(params[3], params[4], value, NULL);
   1.203 +
   1.204 +	return 1;
   1.205 +}
   1.206 +
   1.207 +static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params)
   1.208 +{
   1.209 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.210 +	HandleError herr;
   1.211 +	HandleSecurity sec;
   1.212 +	KeyValueStack *pStk;
   1.213 +
   1.214 +	sec.pOwner = NULL;
   1.215 +	sec.pIdentity = g_pCoreIdent;
   1.216 +
   1.217 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.218 +		!= HandleError_None)
   1.219 +	{
   1.220 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.221 +	}
   1.222 +
   1.223 +	int value;
   1.224 +	char *key;
   1.225 +	pCtx->LocalToString(params[2], &key);
   1.226 +
   1.227 +	value = pStk->pCurRoot.front()->GetInt(key, params[3]);
   1.228 +
   1.229 +	return value;
   1.230 +}
   1.231 +
   1.232 +static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params)
   1.233 +{
   1.234 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.235 +	HandleError herr;
   1.236 +	HandleSecurity sec;
   1.237 +	KeyValueStack *pStk;
   1.238 +
   1.239 +	sec.pOwner = NULL;
   1.240 +	sec.pIdentity = g_pCoreIdent;
   1.241 +
   1.242 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.243 +		!= HandleError_None)
   1.244 +	{
   1.245 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.246 +	}
   1.247 +
   1.248 +	float value;
   1.249 +	char *key;
   1.250 +	pCtx->LocalToString(params[2], &key);
   1.251 +
   1.252 +	value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3]));
   1.253 +
   1.254 +	return sp_ftoc(value);
   1.255 +}
   1.256 +
   1.257 +static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params)
   1.258 +{
   1.259 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.260 +	HandleError herr;
   1.261 +	HandleSecurity sec;
   1.262 +	KeyValueStack *pStk;
   1.263 +
   1.264 +	sec.pOwner = NULL;
   1.265 +	sec.pIdentity = g_pCoreIdent;
   1.266 +
   1.267 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.268 +		!= HandleError_None)
   1.269 +	{
   1.270 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.271 +	}
   1.272 +
   1.273 +	Color color;
   1.274 +	char *key;
   1.275 +	cell_t *r, *g, *b, *a;
   1.276 +	pCtx->LocalToString(params[2], &key);
   1.277 +	pCtx->LocalToPhysAddr(params[3], &r);
   1.278 +	pCtx->LocalToPhysAddr(params[4], &g);
   1.279 +	pCtx->LocalToPhysAddr(params[5], &b);
   1.280 +	pCtx->LocalToPhysAddr(params[6], &a);
   1.281 +
   1.282 +	color = pStk->pCurRoot.front()->GetColor(key);
   1.283 +	*r = color.r();
   1.284 +	*g = color.g();
   1.285 +	*b = color.b();
   1.286 +	*a = color.a();
   1.287 +
   1.288 +	return 1;
   1.289 +}
   1.290 +
   1.291 +static cell_t smn_KvGetUint64(IPluginContext *pCtx, const cell_t *params)
   1.292 +{
   1.293 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.294 +	HandleError herr;
   1.295 +	HandleSecurity sec;
   1.296 +	KeyValueStack *pStk;
   1.297 +
   1.298 +	sec.pOwner = NULL;
   1.299 +	sec.pIdentity = g_pCoreIdent;
   1.300 +
   1.301 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.302 +		!= HandleError_None)
   1.303 +	{
   1.304 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.305 +	}
   1.306 +
   1.307 +	char *key;
   1.308 +	cell_t *addr, *defvalue;
   1.309 +	uint64 value;
   1.310 +	pCtx->LocalToString(params[2], &key);
   1.311 +	pCtx->LocalToPhysAddr(params[3], &addr);
   1.312 +	pCtx->LocalToPhysAddr(params[4], &defvalue);
   1.313 +
   1.314 +	value = pStk->pCurRoot.front()->GetUint64(key, static_cast<uint64>(*defvalue));
   1.315 +	*reinterpret_cast<uint64 *>(addr) = value;
   1.316 +
   1.317 +	return 1;
   1.318 +}
   1.319 +
   1.320 +static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params)
   1.321 +{
   1.322 +	KeyValueStack *pStk;
   1.323 +	char *name, *firstkey, *firstvalue;
   1.324 +	bool is_empty;
   1.325 +
   1.326 +	pCtx->LocalToString(params[1], &name);
   1.327 +	pCtx->LocalToString(params[2], &firstkey);
   1.328 +	pCtx->LocalToString(params[3], &firstvalue);
   1.329 +
   1.330 +	is_empty = (firstkey[0] == '\0');
   1.331 +	pStk = new KeyValueStack;
   1.332 +	pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue);
   1.333 +	pStk->pCurRoot.push(pStk->pBase);
   1.334 +
   1.335 +	return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL);
   1.336 +}
   1.337 +
   1.338 +static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params)
   1.339 +{
   1.340 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.341 +	HandleError herr;
   1.342 +	HandleSecurity sec;
   1.343 +	char *name;	
   1.344 +	KeyValueStack *pStk;
   1.345 +
   1.346 +	sec.pOwner = NULL;
   1.347 +	sec.pIdentity = g_pCoreIdent;
   1.348 +
   1.349 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.350 +		!= HandleError_None)
   1.351 +	{
   1.352 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.353 +	}
   1.354 +
   1.355 +	pCtx->LocalToString(params[2], &name);
   1.356 +
   1.357 +	KeyValues *pSubKey = pStk->pBase->FindKey(name, (params[3]) ? true : false);
   1.358 +	if (!pSubKey)
   1.359 +	{
   1.360 +		return 0;
   1.361 +	}
   1.362 +	pStk->pCurRoot.push(pSubKey);
   1.363 +
   1.364 +	return 1;
   1.365 +}
   1.366 +
   1.367 +static cell_t smn_KvJumpFirstSubKey(IPluginContext *pCtx, const cell_t *params)
   1.368 +{
   1.369 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.370 +	HandleError herr;
   1.371 +	HandleSecurity sec;
   1.372 +	KeyValueStack *pStk;
   1.373 +
   1.374 +	sec.pOwner = NULL;
   1.375 +	sec.pIdentity = g_pCoreIdent;
   1.376 +
   1.377 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.378 +		!= HandleError_None)
   1.379 +	{
   1.380 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.381 +	}
   1.382 +
   1.383 +	KeyValues *pSubKey = pStk->pCurRoot.front();
   1.384 +	KeyValues *pFirstSubKey = pSubKey->GetFirstSubKey();
   1.385 +	if (!pFirstSubKey)
   1.386 +	{
   1.387 +		return 0;
   1.388 +	}
   1.389 +	pStk->pCurRoot.push(pFirstSubKey);
   1.390 +
   1.391 +	return 1;
   1.392 +}
   1.393 +
   1.394 +static cell_t smn_KvJumpNextSubKey(IPluginContext *pCtx, const cell_t *params)
   1.395 +{
   1.396 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.397 +	HandleError herr;
   1.398 +	HandleSecurity sec;
   1.399 +	KeyValueStack *pStk;
   1.400 +
   1.401 +	sec.pOwner = NULL;
   1.402 +	sec.pIdentity = g_pCoreIdent;
   1.403 +
   1.404 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.405 +		!= HandleError_None)
   1.406 +	{
   1.407 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.408 +	}
   1.409 +
   1.410 +	KeyValues *pSubKey = pStk->pCurRoot.front();
   1.411 +	KeyValues *pNextKey = pSubKey->GetNextKey();
   1.412 +	if (!pNextKey)
   1.413 +	{
   1.414 +		return 0;
   1.415 +	}
   1.416 +	pStk->pCurRoot.push(pNextKey);
   1.417 +
   1.418 +	return 1;	
   1.419 +}
   1.420 +
   1.421 +static cell_t smn_KvGoBack(IPluginContext *pCtx, const cell_t *params)
   1.422 +{
   1.423 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.424 +	HandleError herr;
   1.425 +	HandleSecurity sec;
   1.426 +	KeyValueStack *pStk;
   1.427 +
   1.428 +	sec.pOwner = NULL;
   1.429 +	sec.pIdentity = g_pCoreIdent;
   1.430 +
   1.431 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.432 +		!= HandleError_None)
   1.433 +	{
   1.434 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.435 +	}
   1.436 +
   1.437 +	if (pStk->pCurRoot.size() == 1)
   1.438 +	{
   1.439 +		return 0;
   1.440 +	}
   1.441 +	pStk->pCurRoot.pop();
   1.442 +
   1.443 +	return 1;
   1.444 +}
   1.445 +
   1.446 +static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params)
   1.447 +{
   1.448 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.449 +	HandleError herr;
   1.450 +	HandleSecurity sec;
   1.451 +	KeyValueStack *pStk;
   1.452 +
   1.453 +	sec.pOwner = NULL;
   1.454 +	sec.pIdentity = g_pCoreIdent;
   1.455 +
   1.456 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.457 +		!= HandleError_None)
   1.458 +	{
   1.459 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.460 +	}
   1.461 +
   1.462 +	while (pStk->pCurRoot.size() > 1)
   1.463 +	{
   1.464 +		pStk->pCurRoot.pop();
   1.465 +	}
   1.466 +
   1.467 +	return 1;
   1.468 +}
   1.469 +
   1.470 +static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params)
   1.471 +{
   1.472 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.473 +	HandleError herr;
   1.474 +	HandleSecurity sec;
   1.475 +	KeyValueStack *pStk;
   1.476 +
   1.477 +	sec.pOwner = NULL;
   1.478 +	sec.pIdentity = g_pCoreIdent;
   1.479 +
   1.480 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.481 +		!= HandleError_None)
   1.482 +	{
   1.483 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.484 +	}
   1.485 +
   1.486 +	KeyValues *pSection = pStk->pCurRoot.front();
   1.487 +	const char *name = pSection->GetName();
   1.488 +	if (!name)
   1.489 +	{
   1.490 +		return 0;
   1.491 +	}
   1.492 +	pCtx->StringToLocalUTF8(params[2], params[3], name, NULL);
   1.493 +
   1.494 +	return 1;
   1.495 +}
   1.496 +
   1.497 +static cell_t smn_KvSetSectionName(IPluginContext *pCtx, const cell_t *params)
   1.498 +{
   1.499 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.500 +	HandleError herr;
   1.501 +	HandleSecurity sec;
   1.502 +	char *name;
   1.503 +	KeyValueStack *pStk;
   1.504 +
   1.505 +	sec.pOwner = NULL;
   1.506 +	sec.pIdentity = g_pCoreIdent;
   1.507 +
   1.508 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.509 +		!= HandleError_None)
   1.510 +	{
   1.511 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.512 +	}
   1.513 +
   1.514 +	pCtx->LocalToString(params[2], &name);
   1.515 +
   1.516 +	KeyValues *pSection = pStk->pCurRoot.front();
   1.517 +	pSection->SetName(name);
   1.518 +
   1.519 +	return 1;
   1.520 +}
   1.521 +
   1.522 +static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params)
   1.523 +{
   1.524 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.525 +	HandleError herr;
   1.526 +	HandleSecurity sec;
   1.527 +	char *name;
   1.528 +	KeyValueStack *pStk;
   1.529 +
   1.530 +	sec.pOwner = NULL;
   1.531 +	sec.pIdentity = g_pCoreIdent;
   1.532 +
   1.533 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.534 +		!= HandleError_None)
   1.535 +	{
   1.536 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.537 +	}
   1.538 +
   1.539 +	pCtx->LocalToString(params[2], &name);
   1.540 +
   1.541 +	return pStk->pCurRoot.front()->GetDataType(name);
   1.542 +}
   1.543 +
   1.544 +static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params)
   1.545 +{
   1.546 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.547 +	HandleError herr;
   1.548 +	HandleSecurity sec;
   1.549 +	char *path;
   1.550 +	KeyValueStack *pStk;
   1.551 +
   1.552 +	sec.pOwner = NULL;
   1.553 +	sec.pIdentity = g_pCoreIdent;
   1.554 +
   1.555 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.556 +		!= HandleError_None)
   1.557 +	{
   1.558 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.559 +	}
   1.560 +
   1.561 +	pCtx->LocalToString(params[2], &path);
   1.562 +
   1.563 +	char realpath[PLATFORM_MAX_PATH];
   1.564 +	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
   1.565 +
   1.566 +	return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath);
   1.567 +}
   1.568 +
   1.569 +static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
   1.570 +{
   1.571 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.572 +	HandleError herr;
   1.573 +	HandleSecurity sec;
   1.574 +	char *path;
   1.575 +	KeyValueStack *pStk;
   1.576 +
   1.577 +	sec.pOwner = NULL;
   1.578 +	sec.pIdentity = g_pCoreIdent;
   1.579 +
   1.580 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.581 +		!= HandleError_None)
   1.582 +	{
   1.583 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.584 +	}
   1.585 +
   1.586 +	pCtx->LocalToString(params[2], &path);
   1.587 +
   1.588 +	char realpath[PLATFORM_MAX_PATH];
   1.589 +	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
   1.590 +
   1.591 +	return pStk->pCurRoot.front()->LoadFromFile(basefilesystem, realpath);
   1.592 +}
   1.593 +
   1.594 +static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
   1.595 +{
   1.596 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.597 +	HandleError herr;
   1.598 +	HandleSecurity sec;
   1.599 +	KeyValueStack *pStk;
   1.600 +
   1.601 +	sec.pOwner = NULL;
   1.602 +	sec.pIdentity = g_pCoreIdent;
   1.603 +
   1.604 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.605 +		!= HandleError_None)
   1.606 +	{
   1.607 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.608 +	}
   1.609 +
   1.610 +	pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false);
   1.611 +
   1.612 +	return 1;
   1.613 +}
   1.614 +
   1.615 +static KeyValuekNatives s_KeyValuekNatives;
   1.616 +
   1.617 +REGISTER_NATIVES(keyvaluenatives)
   1.618 +{
   1.619 +	{"KvSetString",				smn_KvSetString},
   1.620 +	{"KvSetNum",				smn_KvSetNum},
   1.621 +	{"KvSetUint64",				smn_KvSetUint64},
   1.622 +	{"KvSetFloat",				smn_KvSetFloat},
   1.623 +	{"KvSetColor",				smn_KvSetColor},
   1.624 +	{"KvGetString",				smn_KvGetString},
   1.625 +	{"KvGetNum",				smn_KvGetNum},
   1.626 +	{"KvGetFloat",				smn_KvGetFloat},
   1.627 +	{"KvGetColor",				smn_KvGetColor},
   1.628 +	{"KvGetUint64",				smn_KvGetUint64},
   1.629 +	{"CreateKeyValues",			smn_CreateKeyValues},
   1.630 +	{"KvJumpToKey",				smn_KvJumpToKey},
   1.631 +	{"KvJumpFirstSubKey",		smn_KvJumpFirstSubKey},
   1.632 +	{"KvJumpNextSubKey",		smn_KvJumpNextSubKey},
   1.633 +	{"KvGoBack",				smn_KvGoBack},
   1.634 +	{"KvRewind",				smn_KvRewind},
   1.635 +	{"KvGetSectionName",		smn_KvGetSectionName},
   1.636 +	{"KvSetSectionName",		smn_KvSetSectionName},
   1.637 +	{"KvGetDataType",			smn_KvGetDataType},
   1.638 +	{"KeyValuesToFile",			smn_KeyValuesToFile},
   1.639 +	{"FileToKeyValues",			smn_FileToKeyValues},
   1.640 +	{"KvSetEscapeSequences",	smn_KvSetEscapeSequences},
   1.641 +	{NULL,						NULL}
   1.642 +};