core/smn_keyvalues.cpp
author David Anderson <dvander@alliedmods.net>
Mon Mar 17 01:09:28 2008 +0000 (2008-03-17)
changeset 1941 54539fa4e51f
parent 1940 dda2bdafa470
child 1944 17984feecafd
permissions -rw-r--r--
bumped versions
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sourcemod.h"
    33 #include "sourcemm_api.h"
    34 #include "sm_stringutil.h"
    35 #include "HandleSys.h"
    36 #include <KeyValues.h>
    37 #include "utlbuffer.h"
    38 
    39 HandleType_t g_KeyValueType;
    40 
    41 struct KeyValueStack 
    42 {
    43 	KeyValues *pBase;
    44 	CStack<KeyValues *> pCurRoot;
    45 };
    46 
    47 class KeyValueNatives : 
    48 	public SMGlobalClass,
    49 	public IHandleTypeDispatch
    50 {
    51 public:
    52 	void OnSourceModAllInitialized()
    53 	{
    54 		g_KeyValueType = g_HandleSys.CreateType("KeyValues", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    55 	}
    56 	void OnSourceModShutdown()
    57 	{
    58 		g_HandleSys.RemoveType(g_KeyValueType, g_pCoreIdent);
    59 		g_KeyValueType = 0;
    60 	}
    61 	void OnHandleDestroy(HandleType_t type, void *object)
    62 	{
    63 		KeyValueStack *pStk = reinterpret_cast<KeyValueStack *>(object);
    64 		pStk->pBase->deleteThis();
    65 		delete pStk;
    66 	}
    67 	int CalcKVSizeR(KeyValues *pv)
    68 	{
    69 		CUtlBuffer buf;
    70 		int size;
    71 
    72 		pv->RecursiveSaveToFile(buf, 0);
    73 		size = buf.TellMaxPut();
    74 
    75 		buf.Purge();
    76 
    77 		return size;
    78 	}
    79 	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
    80 	{
    81 		KeyValueStack *pStk = (KeyValueStack *)object;
    82 		unsigned int size = sizeof(KeyValueStack) + (pStk->pCurRoot.size() * sizeof(KeyValues *));
    83 
    84 		/* Check how much memory the actual thing takes up */		
    85 		size += CalcKVSizeR(pStk->pBase);
    86 
    87 		*pSize = size;
    88 
    89 		return true;
    90 	}
    91 };
    92 
    93 KeyValues *SourceModBase::ReadKeyValuesHandle(Handle_t hndl, HandleError *err, bool root)
    94 {
    95 	HandleError herr;
    96 	HandleSecurity sec;
    97 	KeyValueStack *pStk;
    98 
    99 	sec.pOwner = NULL;
   100 	sec.pIdentity = g_pCoreIdent;
   101 
   102 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   103 		!= HandleError_None)
   104 	{
   105 		if (err)
   106 		{
   107 			*err = herr;
   108 		}
   109 		return NULL;
   110 	}
   111 
   112 	if (err)
   113 	{
   114 		*err = HandleError_None;
   115 	}
   116 
   117 	return (root) ? pStk->pBase : pStk->pCurRoot.front();
   118 }
   119 
   120 static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params)
   121 {
   122 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   123 	HandleError herr;
   124 	HandleSecurity sec;
   125 	KeyValueStack *pStk;
   126 
   127 	sec.pOwner = NULL;
   128 	sec.pIdentity = g_pCoreIdent;
   129 
   130 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   131 		!= HandleError_None)
   132 	{
   133 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   134 	}
   135 
   136 	char *key, *value;
   137 	pCtx->LocalToStringNULL(params[2], &key);
   138 	pCtx->LocalToString(params[3], &value);
   139 
   140 	pStk->pCurRoot.front()->SetString(key, value);
   141 
   142 	return 1;
   143 }
   144 
   145 static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params)
   146 {
   147 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   148 	HandleError herr;
   149 	HandleSecurity sec;
   150 	KeyValueStack *pStk;
   151 
   152 	sec.pOwner = NULL;
   153 	sec.pIdentity = g_pCoreIdent;
   154 
   155 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   156 		!= HandleError_None)
   157 	{
   158 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   159 	}
   160 
   161 	char *key;
   162 	pCtx->LocalToStringNULL(params[2], &key);
   163 
   164 	pStk->pCurRoot.front()->SetInt(key, params[3]);
   165 
   166 	return 1;
   167 }
   168 
   169 static cell_t smn_KvSetUInt64(IPluginContext *pCtx, const cell_t *params)
   170 {
   171 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   172 	HandleError herr;
   173 	HandleSecurity sec;
   174 	KeyValueStack *pStk;
   175 
   176 	sec.pOwner = NULL;
   177 	sec.pIdentity = g_pCoreIdent;
   178 
   179 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   180 		!= HandleError_None)
   181 	{
   182 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   183 	}
   184 
   185 	char *key;
   186 	cell_t *addr;
   187 	uint64 value;
   188 	pCtx->LocalToStringNULL(params[2], &key);
   189 	pCtx->LocalToPhysAddr(params[3], &addr);
   190 
   191 	value = static_cast<uint64>(*addr);
   192 	pStk->pCurRoot.front()->SetUint64(key, value);
   193 
   194 	return 1;
   195 }
   196 
   197 static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params)
   198 {
   199 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   200 	HandleError herr;
   201 	HandleSecurity sec;
   202 	KeyValueStack *pStk;
   203 
   204 	sec.pOwner = NULL;
   205 	sec.pIdentity = g_pCoreIdent;
   206 
   207 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   208 		!= HandleError_None)
   209 	{
   210 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   211 	}
   212 
   213 	char *key;
   214 	pCtx->LocalToStringNULL(params[2], &key);
   215 
   216 	pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3]));
   217 
   218 	return 1;
   219 }
   220 
   221 static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params)
   222 {
   223 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   224 	HandleError herr;
   225 	HandleSecurity sec;
   226 	KeyValueStack *pStk;
   227 
   228 	sec.pOwner = NULL;
   229 	sec.pIdentity = g_pCoreIdent;
   230 
   231 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   232 		!= HandleError_None)
   233 	{
   234 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   235 	}
   236 
   237 	char *key;
   238 	pCtx->LocalToStringNULL(params[2], &key);
   239 
   240 	Color color(params[3], params[4], params[5], params[6]);
   241 	pStk->pCurRoot.front()->SetColor(key, color);
   242 
   243 	return 1;
   244 }
   245 
   246 static cell_t smn_KvSetVector(IPluginContext *pCtx, const cell_t *params)
   247 {
   248 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   249 	HandleError herr;
   250 	HandleSecurity sec;
   251 	KeyValueStack *pStk;
   252 
   253 	sec.pOwner = NULL;
   254 	sec.pIdentity = g_pCoreIdent;
   255 
   256 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   257 		!= HandleError_None)
   258 	{
   259 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   260 	}
   261 
   262 	char *key;
   263 	char buffer[64];
   264 	cell_t *vector;
   265 	pCtx->LocalToStringNULL(params[2], &key);
   266 	pCtx->LocalToPhysAddr(params[3], &vector);
   267 
   268 	UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(vector[0]), sp_ctof(vector[1]), sp_ctof(vector[2]));
   269 
   270 	pStk->pCurRoot.front()->SetString(key, buffer);
   271 
   272 	return 1;
   273 }
   274 
   275 static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params)
   276 {
   277 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   278 	HandleError herr;
   279 	HandleSecurity sec;
   280 	KeyValueStack *pStk;
   281 
   282 	sec.pOwner = NULL;
   283 	sec.pIdentity = g_pCoreIdent;
   284 
   285 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   286 		!= HandleError_None)
   287 	{
   288 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   289 	}
   290 
   291 	const char *value;
   292 	char *key, *defvalue;
   293 	pCtx->LocalToStringNULL(params[2], &key);
   294 	pCtx->LocalToString(params[5], &defvalue);
   295 
   296 	value = pStk->pCurRoot.front()->GetString(key, defvalue);
   297 	pCtx->StringToLocalUTF8(params[3], params[4], value, NULL);
   298 
   299 	return 1;
   300 }
   301 
   302 static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params)
   303 {
   304 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   305 	HandleError herr;
   306 	HandleSecurity sec;
   307 	KeyValueStack *pStk;
   308 
   309 	sec.pOwner = NULL;
   310 	sec.pIdentity = g_pCoreIdent;
   311 
   312 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   313 		!= HandleError_None)
   314 	{
   315 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   316 	}
   317 
   318 	int value;
   319 	char *key;
   320 	pCtx->LocalToStringNULL(params[2], &key);
   321 
   322 	value = pStk->pCurRoot.front()->GetInt(key, params[3]);
   323 
   324 	return value;
   325 }
   326 
   327 static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params)
   328 {
   329 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   330 	HandleError herr;
   331 	HandleSecurity sec;
   332 	KeyValueStack *pStk;
   333 
   334 	sec.pOwner = NULL;
   335 	sec.pIdentity = g_pCoreIdent;
   336 
   337 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   338 		!= HandleError_None)
   339 	{
   340 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   341 	}
   342 
   343 	float value;
   344 	char *key;
   345 	pCtx->LocalToStringNULL(params[2], &key);
   346 
   347 	value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3]));
   348 
   349 	return sp_ftoc(value);
   350 }
   351 
   352 static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params)
   353 {
   354 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   355 	HandleError herr;
   356 	HandleSecurity sec;
   357 	KeyValueStack *pStk;
   358 
   359 	sec.pOwner = NULL;
   360 	sec.pIdentity = g_pCoreIdent;
   361 
   362 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   363 		!= HandleError_None)
   364 	{
   365 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   366 	}
   367 
   368 	Color color;
   369 	char *key;
   370 	cell_t *r, *g, *b, *a;
   371 	pCtx->LocalToStringNULL(params[2], &key);
   372 	pCtx->LocalToPhysAddr(params[3], &r);
   373 	pCtx->LocalToPhysAddr(params[4], &g);
   374 	pCtx->LocalToPhysAddr(params[5], &b);
   375 	pCtx->LocalToPhysAddr(params[6], &a);
   376 
   377 	color = pStk->pCurRoot.front()->GetColor(key);
   378 	*r = color.r();
   379 	*g = color.g();
   380 	*b = color.b();
   381 	*a = color.a();
   382 
   383 	return 1;
   384 }
   385 
   386 static cell_t smn_KvGetUInt64(IPluginContext *pCtx, const cell_t *params)
   387 {
   388 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   389 	HandleError herr;
   390 	HandleSecurity sec;
   391 	KeyValueStack *pStk;
   392 
   393 	sec.pOwner = NULL;
   394 	sec.pIdentity = g_pCoreIdent;
   395 
   396 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   397 		!= HandleError_None)
   398 	{
   399 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   400 	}
   401 
   402 	char *key;
   403 	cell_t *addr, *defvalue;
   404 	uint64 value;
   405 	pCtx->LocalToStringNULL(params[2], &key);
   406 	pCtx->LocalToPhysAddr(params[3], &addr);
   407 	pCtx->LocalToPhysAddr(params[4], &defvalue);
   408 
   409 	value = pStk->pCurRoot.front()->GetUint64(key, static_cast<uint64>(*defvalue));
   410 	*reinterpret_cast<uint64 *>(addr) = value;
   411 
   412 	return 1;
   413 }
   414 
   415 static cell_t smn_KvGetVector(IPluginContext *pCtx, const cell_t *params)
   416 {
   417 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   418 	HandleError herr;
   419 	HandleSecurity sec;
   420 	KeyValueStack *pStk;
   421 
   422 	sec.pOwner = NULL;
   423 	sec.pIdentity = g_pCoreIdent;
   424 
   425 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   426 		!= HandleError_None)
   427 	{
   428 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   429 	}
   430 
   431 	char *key;
   432 	const char *value;
   433 	cell_t *defvector, *outvector;
   434 	char buffer[64];
   435 	pCtx->LocalToStringNULL(params[2], &key);
   436 	pCtx->LocalToPhysAddr(params[3], &outvector);
   437 	pCtx->LocalToPhysAddr(params[4], &defvector);
   438 
   439 	UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(defvector[0]), sp_ctof(defvector[1]), sp_ctof(defvector[2]));
   440 
   441 	value = pStk->pCurRoot.front()->GetString(key, buffer);
   442 
   443 	float out;
   444 	int components = 0;
   445 	while (*value && components < 3)
   446 	{
   447 		while ((*value) && (*value == ' '))
   448 		{
   449 			value++;
   450 		}
   451 
   452 		out = 0.0f;
   453 		bool isnegative;
   454 		if (*value == '-')
   455 		{
   456 			isnegative = true;
   457 			value++;
   458 		}
   459 		else
   460 		{
   461 			isnegative = false;
   462 		}
   463 
   464 		for (; *value && isdigit(*value); ++value)
   465 		{
   466 			out *= 10.0f;
   467 			out += *value - '0';
   468 		}
   469 
   470 		if (*value == '.')
   471 		{
   472 			value++;
   473 			float factor = 0.1f;
   474 			for (; *value && isdigit(*value); ++value)
   475 			{
   476 				out += (*value - '0') * factor;
   477 				factor *= 0.1f;
   478 			}
   479 		}
   480 
   481 		out = (isnegative) ? -out : out;
   482 		outvector[components++] = sp_ftoc(out);
   483 	}
   484 
   485 	return 1;
   486 }
   487 
   488 static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params)
   489 {
   490 	KeyValueStack *pStk;
   491 	char *name, *firstkey, *firstvalue;
   492 	bool is_empty;
   493 
   494 	pCtx->LocalToString(params[1], &name);
   495 	pCtx->LocalToString(params[2], &firstkey);
   496 	pCtx->LocalToString(params[3], &firstvalue);
   497 
   498 	is_empty = (firstkey[0] == '\0');
   499 	pStk = new KeyValueStack;
   500 	pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue);
   501 	pStk->pCurRoot.push(pStk->pBase);
   502 
   503 	return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL);
   504 }
   505 
   506 static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params)
   507 {
   508 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   509 	HandleError herr;
   510 	HandleSecurity sec;
   511 	char *name;	
   512 	KeyValueStack *pStk;
   513 
   514 	sec.pOwner = NULL;
   515 	sec.pIdentity = g_pCoreIdent;
   516 
   517 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   518 		!= HandleError_None)
   519 	{
   520 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   521 	}
   522 
   523 	pCtx->LocalToString(params[2], &name);
   524 
   525 	KeyValues *pSubKey = pStk->pCurRoot.front();
   526 	pSubKey = pSubKey->FindKey(name, (params[3]) ? true : false);
   527 	if (!pSubKey)
   528 	{
   529 		return 0;
   530 	}
   531 	pStk->pCurRoot.push(pSubKey);
   532 
   533 	return 1;
   534 }
   535 
   536 static cell_t smn_KvJumpToKeySymbol(IPluginContext *pCtx, const cell_t *params)
   537 {
   538 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   539 	HandleError herr;
   540 	HandleSecurity sec;
   541 	KeyValueStack *pStk;
   542 
   543 	sec.pOwner = NULL;
   544 	sec.pIdentity = g_pCoreIdent;
   545 
   546 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   547 		!= HandleError_None)
   548 	{
   549 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   550 	}
   551 
   552 	KeyValues *pSubKey = pStk->pCurRoot.front();
   553 	pSubKey = pSubKey->FindKey(params[2]);
   554 	if (!pSubKey)
   555 	{
   556 		return 0;
   557 	}
   558 	pStk->pCurRoot.push(pSubKey);
   559 
   560 	return 1;
   561 }
   562 
   563 static cell_t smn_KvGotoFirstSubKey(IPluginContext *pCtx, const cell_t *params)
   564 {
   565 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   566 	HandleError herr;
   567 	HandleSecurity sec;
   568 	KeyValueStack *pStk;
   569 
   570 	sec.pOwner = NULL;
   571 	sec.pIdentity = g_pCoreIdent;
   572 
   573 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   574 		!= HandleError_None)
   575 	{
   576 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   577 	}
   578 
   579 	KeyValues *pSubKey = pStk->pCurRoot.front();
   580 	KeyValues *pFirstSubKey;
   581 	if (params[2])
   582 	{
   583 		pFirstSubKey = pSubKey->GetFirstTrueSubKey();
   584 	} else {
   585 		pFirstSubKey = pSubKey->GetFirstSubKey();
   586 	}
   587 
   588 	if (!pFirstSubKey)
   589 	{
   590 		return 0;
   591 	}
   592 	pStk->pCurRoot.push(pFirstSubKey);
   593 
   594 	return 1;
   595 }
   596 
   597 static cell_t smn_KvGotoNextKey(IPluginContext *pCtx, const cell_t *params)
   598 {
   599 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   600 	HandleError herr;
   601 	HandleSecurity sec;
   602 	KeyValueStack *pStk;
   603 
   604 	sec.pOwner = NULL;
   605 	sec.pIdentity = g_pCoreIdent;
   606 
   607 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   608 		!= HandleError_None)
   609 	{
   610 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   611 	}
   612 
   613 	KeyValues *pSubKey = pStk->pCurRoot.front();
   614 	if (params[2])
   615 	{
   616 		pSubKey = pSubKey->GetNextTrueSubKey();
   617 	} else {
   618 		pSubKey = pSubKey->GetNextKey();
   619 	}
   620 	if (!pSubKey)
   621 	{
   622 		return 0;
   623 	}
   624 	pStk->pCurRoot.pop();
   625 	pStk->pCurRoot.push(pSubKey);
   626 
   627 	return 1;	
   628 }
   629 
   630 static cell_t smn_KvGoBack(IPluginContext *pCtx, const cell_t *params)
   631 {
   632 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   633 	HandleError herr;
   634 	HandleSecurity sec;
   635 	KeyValueStack *pStk;
   636 
   637 	sec.pOwner = NULL;
   638 	sec.pIdentity = g_pCoreIdent;
   639 
   640 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   641 		!= HandleError_None)
   642 	{
   643 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   644 	}
   645 
   646 	if (pStk->pCurRoot.size() == 1)
   647 	{
   648 		return 0;
   649 	}
   650 	pStk->pCurRoot.pop();
   651 
   652 	return 1;
   653 }
   654 
   655 static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params)
   656 {
   657 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   658 	HandleError herr;
   659 	HandleSecurity sec;
   660 	KeyValueStack *pStk;
   661 
   662 	sec.pOwner = NULL;
   663 	sec.pIdentity = g_pCoreIdent;
   664 
   665 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   666 		!= HandleError_None)
   667 	{
   668 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   669 	}
   670 
   671 	while (pStk->pCurRoot.size() > 1)
   672 	{
   673 		pStk->pCurRoot.pop();
   674 	}
   675 
   676 	return 1;
   677 }
   678 
   679 static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params)
   680 {
   681 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   682 	HandleError herr;
   683 	HandleSecurity sec;
   684 	KeyValueStack *pStk;
   685 
   686 	sec.pOwner = NULL;
   687 	sec.pIdentity = g_pCoreIdent;
   688 
   689 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   690 		!= HandleError_None)
   691 	{
   692 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   693 	}
   694 
   695 	KeyValues *pSection = pStk->pCurRoot.front();
   696 	const char *name = pSection->GetName();
   697 	if (!name)
   698 	{
   699 		return 0;
   700 	}
   701 	pCtx->StringToLocalUTF8(params[2], params[3], name, NULL);
   702 
   703 	return 1;
   704 }
   705 
   706 static cell_t smn_KvSetSectionName(IPluginContext *pCtx, const cell_t *params)
   707 {
   708 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   709 	HandleError herr;
   710 	HandleSecurity sec;
   711 	char *name;
   712 	KeyValueStack *pStk;
   713 
   714 	sec.pOwner = NULL;
   715 	sec.pIdentity = g_pCoreIdent;
   716 
   717 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   718 		!= HandleError_None)
   719 	{
   720 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   721 	}
   722 
   723 	pCtx->LocalToString(params[2], &name);
   724 
   725 	KeyValues *pSection = pStk->pCurRoot.front();
   726 	pSection->SetName(name);
   727 
   728 	return 1;
   729 }
   730 
   731 static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params)
   732 {
   733 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   734 	HandleError herr;
   735 	HandleSecurity sec;
   736 	char *name;
   737 	KeyValueStack *pStk;
   738 
   739 	sec.pOwner = NULL;
   740 	sec.pIdentity = g_pCoreIdent;
   741 
   742 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   743 		!= HandleError_None)
   744 	{
   745 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   746 	}
   747 
   748 	pCtx->LocalToString(params[2], &name);
   749 
   750 	return pStk->pCurRoot.front()->GetDataType(name);
   751 }
   752 
   753 static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params)
   754 {
   755 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   756 	HandleError herr;
   757 	HandleSecurity sec;
   758 	char *path;
   759 	KeyValueStack *pStk;
   760 
   761 	sec.pOwner = NULL;
   762 	sec.pIdentity = g_pCoreIdent;
   763 
   764 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   765 		!= HandleError_None)
   766 	{
   767 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   768 	}
   769 
   770 	pCtx->LocalToString(params[2], &path);
   771 
   772 	char realpath[PLATFORM_MAX_PATH];
   773 	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
   774 
   775 	return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath);
   776 }
   777 
   778 static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
   779 {
   780 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   781 	HandleError herr;
   782 	HandleSecurity sec;
   783 	char *path;
   784 	KeyValueStack *pStk;
   785 
   786 	sec.pOwner = NULL;
   787 	sec.pIdentity = g_pCoreIdent;
   788 
   789 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   790 		!= HandleError_None)
   791 	{
   792 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   793 	}
   794 
   795 	pCtx->LocalToString(params[2], &path);
   796 
   797 	char realpath[PLATFORM_MAX_PATH];
   798 	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
   799 
   800 	return pStk->pCurRoot.front()->LoadFromFile(basefilesystem, realpath);
   801 }
   802 
   803 static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
   804 {
   805 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   806 	HandleError herr;
   807 	HandleSecurity sec;
   808 	KeyValueStack *pStk;
   809 
   810 	sec.pOwner = NULL;
   811 	sec.pIdentity = g_pCoreIdent;
   812 
   813 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   814 		!= HandleError_None)
   815 	{
   816 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   817 	}
   818 
   819 	pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false);
   820 
   821 	return 1;
   822 }
   823 
   824 static cell_t smn_KvNodesInStack(IPluginContext *pCtx, const cell_t *params)
   825 {
   826 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   827 	HandleError herr;
   828 	HandleSecurity sec;
   829 	KeyValueStack *pStk;
   830 
   831 	sec.pOwner = NULL;
   832 	sec.pIdentity = g_pCoreIdent;
   833 
   834 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   835 		!= HandleError_None)
   836 	{
   837 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   838 	}
   839 
   840 	return pStk->pCurRoot.size() - 1;
   841 }
   842 
   843 static cell_t smn_KvDeleteThis(IPluginContext *pContext, const cell_t *params)
   844 {
   845 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   846 	HandleError herr;
   847 	HandleSecurity sec;
   848 	KeyValueStack *pStk;
   849 
   850 	sec.pOwner = NULL;
   851 	sec.pIdentity = g_pCoreIdent;
   852 
   853 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   854 		!= HandleError_None)
   855 	{
   856 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   857 	}
   858 
   859 	if (pStk->pCurRoot.size() < 2)
   860 	{
   861 		return 0;
   862 	}
   863 
   864 	KeyValues *pValues = pStk->pCurRoot.front();
   865 	pStk->pCurRoot.pop();
   866 	KeyValues *pRoot = pStk->pCurRoot.front();
   867 
   868 	/* We have to manually verify this since Valve sucks
   869 	 * :TODO: make our own KeyValues.h file and make
   870 	 * the sub stuff private so we can do this ourselves!
   871  	 */
   872 	KeyValues *sub = pRoot->GetFirstSubKey();
   873 	while (sub)
   874 	{
   875 		if (sub == pValues)
   876 		{
   877 			KeyValues *pNext = pValues->GetNextKey();
   878 			pRoot->RemoveSubKey(pValues);
   879 			pValues->deleteThis();
   880 			if (pNext)
   881 			{
   882 				pStk->pCurRoot.push(pNext);
   883 				return 1;
   884 			} else {
   885 				return -1;
   886 			}
   887 		}
   888 		sub = sub->GetNextKey();
   889 	}
   890 
   891 	/* Push this back on :( */
   892 	pStk->pCurRoot.push(pValues);
   893 
   894 	return 0;
   895 }
   896 
   897 static cell_t smn_KvDeleteKey(IPluginContext *pContext, const cell_t *params)
   898 {
   899 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   900 	HandleError herr;
   901 	HandleSecurity sec;
   902 	KeyValueStack *pStk;
   903 
   904 	sec.pOwner = NULL;
   905 	sec.pIdentity = g_pCoreIdent;
   906 
   907 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   908 		!= HandleError_None)
   909 	{
   910 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   911 	}
   912 
   913 	if (pStk->pCurRoot.size() < 2)
   914 	{
   915 		return 0;
   916 	}
   917 
   918 	char *keyName;
   919 	pContext->LocalToString(params[2], &keyName);
   920 
   921 	KeyValues *pRoot = pStk->pCurRoot.front();
   922 	KeyValues *pValues = pRoot->FindKey(keyName);
   923 	if (!pValues)
   924 	{
   925 		return 0;
   926 	}
   927 
   928 	pRoot->RemoveSubKey(pValues);
   929 	pValues->deleteThis();
   930 
   931 	return 1;
   932 }
   933 
   934 static cell_t smn_KvSavePosition(IPluginContext *pContext, const cell_t *params)
   935 {
   936 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   937 	HandleError herr;
   938 	HandleSecurity sec;
   939 	KeyValueStack *pStk;
   940 
   941 	sec.pOwner = NULL;
   942 	sec.pIdentity = g_pCoreIdent;
   943 
   944 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   945 		!= HandleError_None)
   946 	{
   947 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   948 	}
   949 
   950 	if (pStk->pCurRoot.size() < 2)
   951 	{
   952 		return 0;
   953 	}
   954 
   955 	KeyValues *pValues = pStk->pCurRoot.front();
   956 	pStk->pCurRoot.push(pValues);
   957 
   958 	return 1;
   959 }
   960 
   961 static cell_t smn_CopySubkeys(IPluginContext *pContext, const cell_t *params)
   962 {
   963 	Handle_t hndl_copied = static_cast<Handle_t>(params[1]);
   964 	Handle_t hndl_parent = static_cast<Handle_t>(params[2]);
   965 	HandleError herr;
   966 	HandleSecurity sec;
   967 	KeyValueStack *pStk_copied, *pStk_parent;
   968 
   969 	sec.pOwner = NULL;
   970 	sec.pIdentity = g_pCoreIdent;
   971 
   972 	if ((herr=g_HandleSys.ReadHandle(hndl_copied, g_KeyValueType, &sec, (void **)&pStk_copied))
   973 		!= HandleError_None)
   974 	{
   975 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_copied, herr);
   976 	}
   977 	if ((herr=g_HandleSys.ReadHandle(hndl_parent, g_KeyValueType, &sec, (void **)&pStk_parent))
   978 		!= HandleError_None)
   979 	{
   980 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl_parent, herr);
   981 	}
   982 
   983 	pStk_copied->pCurRoot.front()->CopySubkeys(pStk_parent->pCurRoot.front());
   984 
   985 	return 1;
   986 }
   987 
   988 static cell_t smn_GetNameSymbol(IPluginContext *pContext, const cell_t *params)
   989 {
   990 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   991 	HandleError herr;
   992 	HandleSecurity sec;
   993 	KeyValueStack *pStk;
   994 	cell_t *val;
   995 	char *key;
   996 
   997 	sec.pOwner = NULL;
   998 	sec.pIdentity = g_pCoreIdent;
   999 
  1000 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
  1001 		!= HandleError_None)
  1002 	{
  1003 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
  1004 	}
  1005 
  1006 	if (pStk->pCurRoot.size() < 2)
  1007 	{
  1008 		return 0;
  1009 	}
  1010 
  1011 	pContext->LocalToString(params[2], &key);
  1012 
  1013 	KeyValues *pKv = pStk->pCurRoot.front()->FindKey(key);
  1014 	if (!pKv)
  1015 	{
  1016 		return 0;
  1017 	}
  1018 	pContext->LocalToPhysAddr(params[3], &val);
  1019 	*val = pKv->GetNameSymbol();
  1020 
  1021 	return 1;
  1022 }
  1023 
  1024 static cell_t smn_FindKeyById(IPluginContext *pContext, const cell_t *params)
  1025 {
  1026 	Handle_t hndl = static_cast<Handle_t>(params[1]);
  1027 	HandleError herr;
  1028 	HandleSecurity sec;
  1029 	KeyValueStack *pStk;
  1030 
  1031 	sec.pOwner = NULL;
  1032 	sec.pIdentity = g_pCoreIdent;
  1033 
  1034 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
  1035 		!= HandleError_None)
  1036 	{
  1037 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
  1038 	}
  1039 
  1040 	KeyValues *pKv = pStk->pCurRoot.front()->FindKey(params[2]);
  1041 	if (!pKv)
  1042 	{
  1043 		return 0;
  1044 	}
  1045 
  1046 	pContext->StringToLocalUTF8(params[3], params[4], pKv->GetName(), NULL);
  1047 
  1048 	return 1;
  1049 }
  1050 
  1051 static cell_t smn_KvGetSectionSymbol(IPluginContext *pCtx, const cell_t *params)
  1052 {
  1053 	Handle_t hndl = static_cast<Handle_t>(params[1]);
  1054 	HandleError herr;
  1055 	HandleSecurity sec;
  1056 	KeyValueStack *pStk;
  1057 	cell_t *val;
  1058 
  1059 	sec.pOwner = NULL;
  1060 	sec.pIdentity = g_pCoreIdent;
  1061 
  1062 	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
  1063 		!= HandleError_None)
  1064 	{
  1065 		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
  1066 	}
  1067 
  1068 	KeyValues *pSection = pStk->pCurRoot.front();
  1069 
  1070 	pCtx->LocalToPhysAddr(params[2], &val);
  1071 	*val = pSection->GetNameSymbol();
  1072 
  1073 	if (!*val)
  1074 	{
  1075 		return 0;
  1076 	}
  1077 
  1078 	return 1;
  1079 }
  1080 
  1081 static KeyValueNatives s_KeyValueNatives;
  1082 
  1083 REGISTER_NATIVES(keyvaluenatives)
  1084 {
  1085 	{"KvSetString",				smn_KvSetString},
  1086 	{"KvSetNum",				smn_KvSetNum},
  1087 	{"KvSetUInt64",				smn_KvSetUInt64},
  1088 	{"KvSetFloat",				smn_KvSetFloat},
  1089 	{"KvSetColor",				smn_KvSetColor},
  1090 	{"KvGetString",				smn_KvGetString},
  1091 	{"KvGetNum",				smn_KvGetNum},
  1092 	{"KvGetFloat",				smn_KvGetFloat},
  1093 	{"KvGetColor",				smn_KvGetColor},
  1094 	{"KvGetUInt64",				smn_KvGetUInt64},
  1095 	{"CreateKeyValues",			smn_CreateKeyValues},
  1096 	{"KvJumpToKey",				smn_KvJumpToKey},
  1097 	{"KvJumpToKeySymbol",		smn_KvJumpToKeySymbol},
  1098 	{"KvGotoNextKey",			smn_KvGotoNextKey},
  1099 	{"KvGotoFirstSubKey",		smn_KvGotoFirstSubKey},
  1100 	{"KvGoBack",				smn_KvGoBack},
  1101 	{"KvRewind",				smn_KvRewind},
  1102 	{"KvGetSectionName",		smn_KvGetSectionName},
  1103 	{"KvSetSectionName",		smn_KvSetSectionName},
  1104 	{"KvGetDataType",			smn_KvGetDataType},
  1105 	{"KeyValuesToFile",			smn_KeyValuesToFile},
  1106 	{"FileToKeyValues",			smn_FileToKeyValues},
  1107 	{"KvSetEscapeSequences",	smn_KvSetEscapeSequences},
  1108 	{"KvDeleteThis",			smn_KvDeleteThis},
  1109 	{"KvDeleteKey",				smn_KvDeleteKey},
  1110 	{"KvNodesInStack",			smn_KvNodesInStack},
  1111 	{"KvSavePosition",			smn_KvSavePosition},
  1112 	{"KvCopySubkeys",			smn_CopySubkeys},
  1113 	{"KvFindKeyById",			smn_FindKeyById},
  1114 	{"KvGetNameSymbol",			smn_GetNameSymbol},
  1115 	{"KvGetSectionSymbol",		smn_KvGetSectionSymbol},
  1116 	{"KvGetVector",				smn_KvGetVector},
  1117 	{"KvSetVector",				smn_KvSetVector},
  1118 	{NULL,						NULL}
  1119 };