core/smn_keyvalues.cpp
author Borja Ferrer <faluco@alliedmods.net>
Thu Apr 12 00:45:53 2007 +0000 (2007-04-12)
changeset 682 a70055d6d3b3
parent 654 e00dd6df311e
child 703 cddbff919185
permissions -rw-r--r--
added datamap property offset native
added clientofuserid native for fast userid to client id translation
fixed a memory leek (some tries weren't being deleted)
added createdialog native
added 2 million inetchannelinfo wrappers
faluco@654
     1
/**
faluco@654
     2
 * vim: set ts=4 :
faluco@654
     3
 * ===============================================================
faluco@654
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
faluco@654
     5
 * ===============================================================
faluco@654
     6
 *
faluco@654
     7
 * This file is not open source and may not be copied without explicit
faluco@654
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
faluco@654
     9
 * in whole or significant part.
faluco@654
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
faluco@654
    11
 *
faluco@654
    12
 * Version: $Id$
faluco@654
    13
 */
faluco@654
    14
faluco@654
    15
#include "sourcemod.h"
faluco@654
    16
#include "sourcemm_api.h"
faluco@654
    17
#include "sm_stringutil.h"
faluco@654
    18
#include "HandleSys.h"
faluco@654
    19
#include <KeyValues.h>
faluco@654
    20
faluco@654
    21
HandleType_t g_KeyValueType;
faluco@654
    22
faluco@654
    23
struct KeyValueStack 
faluco@654
    24
{
faluco@654
    25
	KeyValues *pBase;
faluco@654
    26
	CStack<KeyValues *> pCurRoot;
faluco@654
    27
};
faluco@654
    28
faluco@654
    29
class KeyValuekNatives : 
faluco@654
    30
	public SMGlobalClass,
faluco@654
    31
	public IHandleTypeDispatch
faluco@654
    32
{
faluco@654
    33
public:
faluco@654
    34
	void OnSourceModAllInitialized()
faluco@654
    35
	{
faluco@654
    36
		g_KeyValueType = g_HandleSys.CreateType("KeyValues", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@654
    37
	}
faluco@654
    38
	void OnSourceModShutdown()
faluco@654
    39
	{
faluco@654
    40
		g_HandleSys.RemoveType(g_KeyValueType, g_pCoreIdent);
faluco@654
    41
		g_KeyValueType = 0;
faluco@654
    42
	}
faluco@654
    43
	void OnHandleDestroy(HandleType_t type, void *object)
faluco@654
    44
	{
faluco@654
    45
		KeyValueStack *pStk = reinterpret_cast<KeyValueStack *>(object);
faluco@654
    46
		pStk->pBase->deleteThis();
faluco@654
    47
		delete pStk;
faluco@654
    48
	}
faluco@654
    49
};
faluco@654
    50
faluco@682
    51
KeyValues *SourceModBase::ReadKeyValuesHandle(Handle_t hndl, HandleError *err, bool root)
faluco@682
    52
{
faluco@682
    53
	HandleError herr;
faluco@682
    54
	HandleSecurity sec;
faluco@682
    55
	KeyValueStack *pStk;
faluco@682
    56
faluco@682
    57
	sec.pOwner = NULL;
faluco@682
    58
	sec.pIdentity = g_pCoreIdent;
faluco@682
    59
faluco@682
    60
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@682
    61
		!= HandleError_None)
faluco@682
    62
	{
faluco@682
    63
		if (err)
faluco@682
    64
		{
faluco@682
    65
			*err = herr;
faluco@682
    66
		}
faluco@682
    67
		return NULL;
faluco@682
    68
	}
faluco@682
    69
faluco@682
    70
	if (err)
faluco@682
    71
	{
faluco@682
    72
		*err = HandleError_None;
faluco@682
    73
	}
faluco@682
    74
faluco@682
    75
	return (root) ? pStk->pBase : pStk->pCurRoot.front();
faluco@682
    76
}
faluco@682
    77
faluco@654
    78
static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params)
faluco@654
    79
{
faluco@654
    80
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
    81
	HandleError herr;
faluco@654
    82
	HandleSecurity sec;
faluco@654
    83
	KeyValueStack *pStk;
faluco@654
    84
faluco@654
    85
	sec.pOwner = NULL;
faluco@654
    86
	sec.pIdentity = g_pCoreIdent;
faluco@654
    87
faluco@654
    88
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
    89
		!= HandleError_None)
faluco@654
    90
	{
faluco@654
    91
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
    92
	}
faluco@654
    93
faluco@654
    94
	char *key, *value;
faluco@654
    95
	pCtx->LocalToString(params[2], &key);
faluco@654
    96
	pCtx->LocalToString(params[3], &value);
faluco@654
    97
faluco@654
    98
	pStk->pCurRoot.front()->SetString(key, value);
faluco@654
    99
faluco@654
   100
	return 1;
faluco@654
   101
}
faluco@654
   102
faluco@654
   103
static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params)
faluco@654
   104
{
faluco@654
   105
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   106
	HandleError herr;
faluco@654
   107
	HandleSecurity sec;
faluco@654
   108
	KeyValueStack *pStk;
faluco@654
   109
faluco@654
   110
	sec.pOwner = NULL;
faluco@654
   111
	sec.pIdentity = g_pCoreIdent;
faluco@654
   112
faluco@654
   113
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   114
		!= HandleError_None)
faluco@654
   115
	{
faluco@654
   116
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   117
	}
faluco@654
   118
faluco@654
   119
	char *key;
faluco@654
   120
	pCtx->LocalToString(params[2], &key);
faluco@654
   121
faluco@654
   122
	pStk->pCurRoot.front()->SetInt(key, params[3]);
faluco@654
   123
faluco@654
   124
	return 1;
faluco@654
   125
}
faluco@654
   126
faluco@654
   127
static cell_t smn_KvSetUint64(IPluginContext *pCtx, const cell_t *params)
faluco@654
   128
{
faluco@654
   129
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   130
	HandleError herr;
faluco@654
   131
	HandleSecurity sec;
faluco@654
   132
	KeyValueStack *pStk;
faluco@654
   133
faluco@654
   134
	sec.pOwner = NULL;
faluco@654
   135
	sec.pIdentity = g_pCoreIdent;
faluco@654
   136
faluco@654
   137
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   138
		!= HandleError_None)
faluco@654
   139
	{
faluco@654
   140
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   141
	}
faluco@654
   142
faluco@654
   143
	char *key;
faluco@654
   144
	cell_t *addr;
faluco@654
   145
	uint64 value;
faluco@654
   146
	pCtx->LocalToString(params[2], &key);
faluco@654
   147
	pCtx->LocalToPhysAddr(params[3], &addr);
faluco@654
   148
faluco@654
   149
	value = static_cast<uint64>(*addr);
faluco@654
   150
	pStk->pCurRoot.front()->SetUint64(key, value);
faluco@654
   151
faluco@654
   152
	return 1;
faluco@654
   153
}
faluco@654
   154
faluco@654
   155
static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params)
faluco@654
   156
{
faluco@654
   157
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   158
	HandleError herr;
faluco@654
   159
	HandleSecurity sec;
faluco@654
   160
	KeyValueStack *pStk;
faluco@654
   161
faluco@654
   162
	sec.pOwner = NULL;
faluco@654
   163
	sec.pIdentity = g_pCoreIdent;
faluco@654
   164
faluco@654
   165
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   166
		!= HandleError_None)
faluco@654
   167
	{
faluco@654
   168
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   169
	}
faluco@654
   170
faluco@654
   171
	char *key;
faluco@654
   172
	pCtx->LocalToString(params[2], &key);
faluco@654
   173
faluco@654
   174
	pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3]));
faluco@654
   175
faluco@654
   176
	return 1;
faluco@654
   177
}
faluco@654
   178
faluco@654
   179
static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params)
faluco@654
   180
{
faluco@654
   181
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   182
	HandleError herr;
faluco@654
   183
	HandleSecurity sec;
faluco@654
   184
	KeyValueStack *pStk;
faluco@654
   185
faluco@654
   186
	sec.pOwner = NULL;
faluco@654
   187
	sec.pIdentity = g_pCoreIdent;
faluco@654
   188
faluco@654
   189
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   190
		!= HandleError_None)
faluco@654
   191
	{
faluco@654
   192
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   193
	}
faluco@654
   194
faluco@654
   195
	char *key;
faluco@654
   196
	pCtx->LocalToString(params[2], &key);
faluco@654
   197
faluco@654
   198
	Color color(params[3], params[4], params[5], params[6]);
faluco@654
   199
	pStk->pCurRoot.front()->SetColor(key, color);
faluco@654
   200
faluco@654
   201
	return 1;
faluco@654
   202
}
faluco@654
   203
faluco@654
   204
static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params)
faluco@654
   205
{
faluco@654
   206
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   207
	HandleError herr;
faluco@654
   208
	HandleSecurity sec;
faluco@654
   209
	KeyValueStack *pStk;
faluco@654
   210
faluco@654
   211
	sec.pOwner = NULL;
faluco@654
   212
	sec.pIdentity = g_pCoreIdent;
faluco@654
   213
faluco@654
   214
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   215
		!= HandleError_None)
faluco@654
   216
	{
faluco@654
   217
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   218
	}
faluco@654
   219
faluco@654
   220
	const char *value;
faluco@654
   221
	char *key, *defvalue;
faluco@654
   222
	pCtx->LocalToString(params[2], &key);
faluco@654
   223
	pCtx->LocalToString(params[5], &defvalue);
faluco@654
   224
faluco@654
   225
	value = pStk->pCurRoot.front()->GetString(key, defvalue);
faluco@654
   226
	pCtx->StringToLocalUTF8(params[3], params[4], value, NULL);
faluco@654
   227
faluco@654
   228
	return 1;
faluco@654
   229
}
faluco@654
   230
faluco@654
   231
static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params)
faluco@654
   232
{
faluco@654
   233
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   234
	HandleError herr;
faluco@654
   235
	HandleSecurity sec;
faluco@654
   236
	KeyValueStack *pStk;
faluco@654
   237
faluco@654
   238
	sec.pOwner = NULL;
faluco@654
   239
	sec.pIdentity = g_pCoreIdent;
faluco@654
   240
faluco@654
   241
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   242
		!= HandleError_None)
faluco@654
   243
	{
faluco@654
   244
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   245
	}
faluco@654
   246
faluco@654
   247
	int value;
faluco@654
   248
	char *key;
faluco@654
   249
	pCtx->LocalToString(params[2], &key);
faluco@654
   250
faluco@654
   251
	value = pStk->pCurRoot.front()->GetInt(key, params[3]);
faluco@654
   252
faluco@654
   253
	return value;
faluco@654
   254
}
faluco@654
   255
faluco@654
   256
static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params)
faluco@654
   257
{
faluco@654
   258
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   259
	HandleError herr;
faluco@654
   260
	HandleSecurity sec;
faluco@654
   261
	KeyValueStack *pStk;
faluco@654
   262
faluco@654
   263
	sec.pOwner = NULL;
faluco@654
   264
	sec.pIdentity = g_pCoreIdent;
faluco@654
   265
faluco@654
   266
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   267
		!= HandleError_None)
faluco@654
   268
	{
faluco@654
   269
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   270
	}
faluco@654
   271
faluco@654
   272
	float value;
faluco@654
   273
	char *key;
faluco@654
   274
	pCtx->LocalToString(params[2], &key);
faluco@654
   275
faluco@654
   276
	value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3]));
faluco@654
   277
faluco@654
   278
	return sp_ftoc(value);
faluco@654
   279
}
faluco@654
   280
faluco@654
   281
static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params)
faluco@654
   282
{
faluco@654
   283
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   284
	HandleError herr;
faluco@654
   285
	HandleSecurity sec;
faluco@654
   286
	KeyValueStack *pStk;
faluco@654
   287
faluco@654
   288
	sec.pOwner = NULL;
faluco@654
   289
	sec.pIdentity = g_pCoreIdent;
faluco@654
   290
faluco@654
   291
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   292
		!= HandleError_None)
faluco@654
   293
	{
faluco@654
   294
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   295
	}
faluco@654
   296
faluco@654
   297
	Color color;
faluco@654
   298
	char *key;
faluco@654
   299
	cell_t *r, *g, *b, *a;
faluco@654
   300
	pCtx->LocalToString(params[2], &key);
faluco@654
   301
	pCtx->LocalToPhysAddr(params[3], &r);
faluco@654
   302
	pCtx->LocalToPhysAddr(params[4], &g);
faluco@654
   303
	pCtx->LocalToPhysAddr(params[5], &b);
faluco@654
   304
	pCtx->LocalToPhysAddr(params[6], &a);
faluco@654
   305
faluco@654
   306
	color = pStk->pCurRoot.front()->GetColor(key);
faluco@654
   307
	*r = color.r();
faluco@654
   308
	*g = color.g();
faluco@654
   309
	*b = color.b();
faluco@654
   310
	*a = color.a();
faluco@654
   311
faluco@654
   312
	return 1;
faluco@654
   313
}
faluco@654
   314
faluco@654
   315
static cell_t smn_KvGetUint64(IPluginContext *pCtx, const cell_t *params)
faluco@654
   316
{
faluco@654
   317
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   318
	HandleError herr;
faluco@654
   319
	HandleSecurity sec;
faluco@654
   320
	KeyValueStack *pStk;
faluco@654
   321
faluco@654
   322
	sec.pOwner = NULL;
faluco@654
   323
	sec.pIdentity = g_pCoreIdent;
faluco@654
   324
faluco@654
   325
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   326
		!= HandleError_None)
faluco@654
   327
	{
faluco@654
   328
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   329
	}
faluco@654
   330
faluco@654
   331
	char *key;
faluco@654
   332
	cell_t *addr, *defvalue;
faluco@654
   333
	uint64 value;
faluco@654
   334
	pCtx->LocalToString(params[2], &key);
faluco@654
   335
	pCtx->LocalToPhysAddr(params[3], &addr);
faluco@654
   336
	pCtx->LocalToPhysAddr(params[4], &defvalue);
faluco@654
   337
faluco@654
   338
	value = pStk->pCurRoot.front()->GetUint64(key, static_cast<uint64>(*defvalue));
faluco@654
   339
	*reinterpret_cast<uint64 *>(addr) = value;
faluco@654
   340
faluco@654
   341
	return 1;
faluco@654
   342
}
faluco@654
   343
faluco@654
   344
static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params)
faluco@654
   345
{
faluco@654
   346
	KeyValueStack *pStk;
faluco@654
   347
	char *name, *firstkey, *firstvalue;
faluco@654
   348
	bool is_empty;
faluco@654
   349
faluco@654
   350
	pCtx->LocalToString(params[1], &name);
faluco@654
   351
	pCtx->LocalToString(params[2], &firstkey);
faluco@654
   352
	pCtx->LocalToString(params[3], &firstvalue);
faluco@654
   353
faluco@654
   354
	is_empty = (firstkey[0] == '\0');
faluco@654
   355
	pStk = new KeyValueStack;
faluco@654
   356
	pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue);
faluco@654
   357
	pStk->pCurRoot.push(pStk->pBase);
faluco@654
   358
faluco@654
   359
	return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@654
   360
}
faluco@654
   361
faluco@654
   362
static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params)
faluco@654
   363
{
faluco@654
   364
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   365
	HandleError herr;
faluco@654
   366
	HandleSecurity sec;
faluco@654
   367
	char *name;	
faluco@654
   368
	KeyValueStack *pStk;
faluco@654
   369
faluco@654
   370
	sec.pOwner = NULL;
faluco@654
   371
	sec.pIdentity = g_pCoreIdent;
faluco@654
   372
faluco@654
   373
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   374
		!= HandleError_None)
faluco@654
   375
	{
faluco@654
   376
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   377
	}
faluco@654
   378
faluco@654
   379
	pCtx->LocalToString(params[2], &name);
faluco@654
   380
faluco@654
   381
	KeyValues *pSubKey = pStk->pBase->FindKey(name, (params[3]) ? true : false);
faluco@654
   382
	if (!pSubKey)
faluco@654
   383
	{
faluco@654
   384
		return 0;
faluco@654
   385
	}
faluco@654
   386
	pStk->pCurRoot.push(pSubKey);
faluco@654
   387
faluco@654
   388
	return 1;
faluco@654
   389
}
faluco@654
   390
faluco@654
   391
static cell_t smn_KvJumpFirstSubKey(IPluginContext *pCtx, const cell_t *params)
faluco@654
   392
{
faluco@654
   393
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   394
	HandleError herr;
faluco@654
   395
	HandleSecurity sec;
faluco@654
   396
	KeyValueStack *pStk;
faluco@654
   397
faluco@654
   398
	sec.pOwner = NULL;
faluco@654
   399
	sec.pIdentity = g_pCoreIdent;
faluco@654
   400
faluco@654
   401
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   402
		!= HandleError_None)
faluco@654
   403
	{
faluco@654
   404
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   405
	}
faluco@654
   406
faluco@654
   407
	KeyValues *pSubKey = pStk->pCurRoot.front();
faluco@654
   408
	KeyValues *pFirstSubKey = pSubKey->GetFirstSubKey();
faluco@654
   409
	if (!pFirstSubKey)
faluco@654
   410
	{
faluco@654
   411
		return 0;
faluco@654
   412
	}
faluco@654
   413
	pStk->pCurRoot.push(pFirstSubKey);
faluco@654
   414
faluco@654
   415
	return 1;
faluco@654
   416
}
faluco@654
   417
faluco@654
   418
static cell_t smn_KvJumpNextSubKey(IPluginContext *pCtx, const cell_t *params)
faluco@654
   419
{
faluco@654
   420
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   421
	HandleError herr;
faluco@654
   422
	HandleSecurity sec;
faluco@654
   423
	KeyValueStack *pStk;
faluco@654
   424
faluco@654
   425
	sec.pOwner = NULL;
faluco@654
   426
	sec.pIdentity = g_pCoreIdent;
faluco@654
   427
faluco@654
   428
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   429
		!= HandleError_None)
faluco@654
   430
	{
faluco@654
   431
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   432
	}
faluco@654
   433
faluco@654
   434
	KeyValues *pSubKey = pStk->pCurRoot.front();
faluco@654
   435
	KeyValues *pNextKey = pSubKey->GetNextKey();
faluco@654
   436
	if (!pNextKey)
faluco@654
   437
	{
faluco@654
   438
		return 0;
faluco@654
   439
	}
faluco@654
   440
	pStk->pCurRoot.push(pNextKey);
faluco@654
   441
faluco@654
   442
	return 1;	
faluco@654
   443
}
faluco@654
   444
faluco@654
   445
static cell_t smn_KvGoBack(IPluginContext *pCtx, const cell_t *params)
faluco@654
   446
{
faluco@654
   447
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   448
	HandleError herr;
faluco@654
   449
	HandleSecurity sec;
faluco@654
   450
	KeyValueStack *pStk;
faluco@654
   451
faluco@654
   452
	sec.pOwner = NULL;
faluco@654
   453
	sec.pIdentity = g_pCoreIdent;
faluco@654
   454
faluco@654
   455
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   456
		!= HandleError_None)
faluco@654
   457
	{
faluco@654
   458
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   459
	}
faluco@654
   460
faluco@654
   461
	if (pStk->pCurRoot.size() == 1)
faluco@654
   462
	{
faluco@654
   463
		return 0;
faluco@654
   464
	}
faluco@654
   465
	pStk->pCurRoot.pop();
faluco@654
   466
faluco@654
   467
	return 1;
faluco@654
   468
}
faluco@654
   469
faluco@654
   470
static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params)
faluco@654
   471
{
faluco@654
   472
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   473
	HandleError herr;
faluco@654
   474
	HandleSecurity sec;
faluco@654
   475
	KeyValueStack *pStk;
faluco@654
   476
faluco@654
   477
	sec.pOwner = NULL;
faluco@654
   478
	sec.pIdentity = g_pCoreIdent;
faluco@654
   479
faluco@654
   480
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   481
		!= HandleError_None)
faluco@654
   482
	{
faluco@654
   483
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   484
	}
faluco@654
   485
faluco@654
   486
	while (pStk->pCurRoot.size() > 1)
faluco@654
   487
	{
faluco@654
   488
		pStk->pCurRoot.pop();
faluco@654
   489
	}
faluco@654
   490
faluco@654
   491
	return 1;
faluco@654
   492
}
faluco@654
   493
faluco@654
   494
static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params)
faluco@654
   495
{
faluco@654
   496
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   497
	HandleError herr;
faluco@654
   498
	HandleSecurity sec;
faluco@654
   499
	KeyValueStack *pStk;
faluco@654
   500
faluco@654
   501
	sec.pOwner = NULL;
faluco@654
   502
	sec.pIdentity = g_pCoreIdent;
faluco@654
   503
faluco@654
   504
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   505
		!= HandleError_None)
faluco@654
   506
	{
faluco@654
   507
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   508
	}
faluco@654
   509
faluco@654
   510
	KeyValues *pSection = pStk->pCurRoot.front();
faluco@654
   511
	const char *name = pSection->GetName();
faluco@654
   512
	if (!name)
faluco@654
   513
	{
faluco@654
   514
		return 0;
faluco@654
   515
	}
faluco@654
   516
	pCtx->StringToLocalUTF8(params[2], params[3], name, NULL);
faluco@654
   517
faluco@654
   518
	return 1;
faluco@654
   519
}
faluco@654
   520
faluco@654
   521
static cell_t smn_KvSetSectionName(IPluginContext *pCtx, const cell_t *params)
faluco@654
   522
{
faluco@654
   523
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   524
	HandleError herr;
faluco@654
   525
	HandleSecurity sec;
faluco@654
   526
	char *name;
faluco@654
   527
	KeyValueStack *pStk;
faluco@654
   528
faluco@654
   529
	sec.pOwner = NULL;
faluco@654
   530
	sec.pIdentity = g_pCoreIdent;
faluco@654
   531
faluco@654
   532
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   533
		!= HandleError_None)
faluco@654
   534
	{
faluco@654
   535
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   536
	}
faluco@654
   537
faluco@654
   538
	pCtx->LocalToString(params[2], &name);
faluco@654
   539
faluco@654
   540
	KeyValues *pSection = pStk->pCurRoot.front();
faluco@654
   541
	pSection->SetName(name);
faluco@654
   542
faluco@654
   543
	return 1;
faluco@654
   544
}
faluco@654
   545
faluco@654
   546
static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params)
faluco@654
   547
{
faluco@654
   548
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   549
	HandleError herr;
faluco@654
   550
	HandleSecurity sec;
faluco@654
   551
	char *name;
faluco@654
   552
	KeyValueStack *pStk;
faluco@654
   553
faluco@654
   554
	sec.pOwner = NULL;
faluco@654
   555
	sec.pIdentity = g_pCoreIdent;
faluco@654
   556
faluco@654
   557
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   558
		!= HandleError_None)
faluco@654
   559
	{
faluco@654
   560
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   561
	}
faluco@654
   562
faluco@654
   563
	pCtx->LocalToString(params[2], &name);
faluco@654
   564
faluco@654
   565
	return pStk->pCurRoot.front()->GetDataType(name);
faluco@654
   566
}
faluco@654
   567
faluco@654
   568
static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params)
faluco@654
   569
{
faluco@654
   570
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   571
	HandleError herr;
faluco@654
   572
	HandleSecurity sec;
faluco@654
   573
	char *path;
faluco@654
   574
	KeyValueStack *pStk;
faluco@654
   575
faluco@654
   576
	sec.pOwner = NULL;
faluco@654
   577
	sec.pIdentity = g_pCoreIdent;
faluco@654
   578
faluco@654
   579
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   580
		!= HandleError_None)
faluco@654
   581
	{
faluco@654
   582
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   583
	}
faluco@654
   584
faluco@654
   585
	pCtx->LocalToString(params[2], &path);
faluco@654
   586
faluco@654
   587
	char realpath[PLATFORM_MAX_PATH];
faluco@654
   588
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
faluco@654
   589
faluco@654
   590
	return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath);
faluco@654
   591
}
faluco@654
   592
faluco@654
   593
static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
faluco@654
   594
{
faluco@654
   595
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   596
	HandleError herr;
faluco@654
   597
	HandleSecurity sec;
faluco@654
   598
	char *path;
faluco@654
   599
	KeyValueStack *pStk;
faluco@654
   600
faluco@654
   601
	sec.pOwner = NULL;
faluco@654
   602
	sec.pIdentity = g_pCoreIdent;
faluco@654
   603
faluco@654
   604
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   605
		!= HandleError_None)
faluco@654
   606
	{
faluco@654
   607
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   608
	}
faluco@654
   609
faluco@654
   610
	pCtx->LocalToString(params[2], &path);
faluco@654
   611
faluco@654
   612
	char realpath[PLATFORM_MAX_PATH];
faluco@654
   613
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
faluco@654
   614
faluco@654
   615
	return pStk->pCurRoot.front()->LoadFromFile(basefilesystem, realpath);
faluco@654
   616
}
faluco@654
   617
faluco@654
   618
static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
faluco@654
   619
{
faluco@654
   620
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   621
	HandleError herr;
faluco@654
   622
	HandleSecurity sec;
faluco@654
   623
	KeyValueStack *pStk;
faluco@654
   624
faluco@654
   625
	sec.pOwner = NULL;
faluco@654
   626
	sec.pIdentity = g_pCoreIdent;
faluco@654
   627
faluco@654
   628
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   629
		!= HandleError_None)
faluco@654
   630
	{
faluco@654
   631
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   632
	}
faluco@654
   633
faluco@654
   634
	pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false);
faluco@654
   635
faluco@654
   636
	return 1;
faluco@654
   637
}
faluco@654
   638
faluco@654
   639
static KeyValuekNatives s_KeyValuekNatives;
faluco@654
   640
faluco@654
   641
REGISTER_NATIVES(keyvaluenatives)
faluco@654
   642
{
faluco@654
   643
	{"KvSetString",				smn_KvSetString},
faluco@654
   644
	{"KvSetNum",				smn_KvSetNum},
faluco@654
   645
	{"KvSetUint64",				smn_KvSetUint64},
faluco@654
   646
	{"KvSetFloat",				smn_KvSetFloat},
faluco@654
   647
	{"KvSetColor",				smn_KvSetColor},
faluco@654
   648
	{"KvGetString",				smn_KvGetString},
faluco@654
   649
	{"KvGetNum",				smn_KvGetNum},
faluco@654
   650
	{"KvGetFloat",				smn_KvGetFloat},
faluco@654
   651
	{"KvGetColor",				smn_KvGetColor},
faluco@654
   652
	{"KvGetUint64",				smn_KvGetUint64},
faluco@654
   653
	{"CreateKeyValues",			smn_CreateKeyValues},
faluco@654
   654
	{"KvJumpToKey",				smn_KvJumpToKey},
faluco@654
   655
	{"KvJumpFirstSubKey",		smn_KvJumpFirstSubKey},
faluco@654
   656
	{"KvJumpNextSubKey",		smn_KvJumpNextSubKey},
faluco@654
   657
	{"KvGoBack",				smn_KvGoBack},
faluco@654
   658
	{"KvRewind",				smn_KvRewind},
faluco@654
   659
	{"KvGetSectionName",		smn_KvGetSectionName},
faluco@654
   660
	{"KvSetSectionName",		smn_KvSetSectionName},
faluco@654
   661
	{"KvGetDataType",			smn_KvGetDataType},
faluco@654
   662
	{"KeyValuesToFile",			smn_KeyValuesToFile},
faluco@654
   663
	{"FileToKeyValues",			smn_FileToKeyValues},
faluco@654
   664
	{"KvSetEscapeSequences",	smn_KvSetEscapeSequences},
faluco@654
   665
	{NULL,						NULL}
faluco@654
   666
};