core/smn_keyvalues.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sun Mar 25 21:18:50 2007 +0000 (2007-03-25)
changeset 655 77fe8887aa2d
parent 654 e00dd6df311e
child 682 a70055d6d3b3
permissions -rw-r--r--
synced linux build
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@654
    51
static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params)
faluco@654
    52
{
faluco@654
    53
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
    54
	HandleError herr;
faluco@654
    55
	HandleSecurity sec;
faluco@654
    56
	KeyValueStack *pStk;
faluco@654
    57
faluco@654
    58
	sec.pOwner = NULL;
faluco@654
    59
	sec.pIdentity = g_pCoreIdent;
faluco@654
    60
faluco@654
    61
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
    62
		!= HandleError_None)
faluco@654
    63
	{
faluco@654
    64
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
    65
	}
faluco@654
    66
faluco@654
    67
	char *key, *value;
faluco@654
    68
	pCtx->LocalToString(params[2], &key);
faluco@654
    69
	pCtx->LocalToString(params[3], &value);
faluco@654
    70
faluco@654
    71
	pStk->pCurRoot.front()->SetString(key, value);
faluco@654
    72
faluco@654
    73
	return 1;
faluco@654
    74
}
faluco@654
    75
faluco@654
    76
static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params)
faluco@654
    77
{
faluco@654
    78
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
    79
	HandleError herr;
faluco@654
    80
	HandleSecurity sec;
faluco@654
    81
	KeyValueStack *pStk;
faluco@654
    82
faluco@654
    83
	sec.pOwner = NULL;
faluco@654
    84
	sec.pIdentity = g_pCoreIdent;
faluco@654
    85
faluco@654
    86
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
    87
		!= HandleError_None)
faluco@654
    88
	{
faluco@654
    89
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
    90
	}
faluco@654
    91
faluco@654
    92
	char *key;
faluco@654
    93
	pCtx->LocalToString(params[2], &key);
faluco@654
    94
faluco@654
    95
	pStk->pCurRoot.front()->SetInt(key, params[3]);
faluco@654
    96
faluco@654
    97
	return 1;
faluco@654
    98
}
faluco@654
    99
faluco@654
   100
static cell_t smn_KvSetUint64(IPluginContext *pCtx, const cell_t *params)
faluco@654
   101
{
faluco@654
   102
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   103
	HandleError herr;
faluco@654
   104
	HandleSecurity sec;
faluco@654
   105
	KeyValueStack *pStk;
faluco@654
   106
faluco@654
   107
	sec.pOwner = NULL;
faluco@654
   108
	sec.pIdentity = g_pCoreIdent;
faluco@654
   109
faluco@654
   110
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   111
		!= HandleError_None)
faluco@654
   112
	{
faluco@654
   113
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   114
	}
faluco@654
   115
faluco@654
   116
	char *key;
faluco@654
   117
	cell_t *addr;
faluco@654
   118
	uint64 value;
faluco@654
   119
	pCtx->LocalToString(params[2], &key);
faluco@654
   120
	pCtx->LocalToPhysAddr(params[3], &addr);
faluco@654
   121
faluco@654
   122
	value = static_cast<uint64>(*addr);
faluco@654
   123
	pStk->pCurRoot.front()->SetUint64(key, value);
faluco@654
   124
faluco@654
   125
	return 1;
faluco@654
   126
}
faluco@654
   127
faluco@654
   128
static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params)
faluco@654
   129
{
faluco@654
   130
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   131
	HandleError herr;
faluco@654
   132
	HandleSecurity sec;
faluco@654
   133
	KeyValueStack *pStk;
faluco@654
   134
faluco@654
   135
	sec.pOwner = NULL;
faluco@654
   136
	sec.pIdentity = g_pCoreIdent;
faluco@654
   137
faluco@654
   138
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   139
		!= HandleError_None)
faluco@654
   140
	{
faluco@654
   141
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   142
	}
faluco@654
   143
faluco@654
   144
	char *key;
faluco@654
   145
	pCtx->LocalToString(params[2], &key);
faluco@654
   146
faluco@654
   147
	pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3]));
faluco@654
   148
faluco@654
   149
	return 1;
faluco@654
   150
}
faluco@654
   151
faluco@654
   152
static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params)
faluco@654
   153
{
faluco@654
   154
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   155
	HandleError herr;
faluco@654
   156
	HandleSecurity sec;
faluco@654
   157
	KeyValueStack *pStk;
faluco@654
   158
faluco@654
   159
	sec.pOwner = NULL;
faluco@654
   160
	sec.pIdentity = g_pCoreIdent;
faluco@654
   161
faluco@654
   162
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   163
		!= HandleError_None)
faluco@654
   164
	{
faluco@654
   165
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   166
	}
faluco@654
   167
faluco@654
   168
	char *key;
faluco@654
   169
	pCtx->LocalToString(params[2], &key);
faluco@654
   170
faluco@654
   171
	Color color(params[3], params[4], params[5], params[6]);
faluco@654
   172
	pStk->pCurRoot.front()->SetColor(key, color);
faluco@654
   173
faluco@654
   174
	return 1;
faluco@654
   175
}
faluco@654
   176
faluco@654
   177
static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params)
faluco@654
   178
{
faluco@654
   179
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   180
	HandleError herr;
faluco@654
   181
	HandleSecurity sec;
faluco@654
   182
	KeyValueStack *pStk;
faluco@654
   183
faluco@654
   184
	sec.pOwner = NULL;
faluco@654
   185
	sec.pIdentity = g_pCoreIdent;
faluco@654
   186
faluco@654
   187
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   188
		!= HandleError_None)
faluco@654
   189
	{
faluco@654
   190
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   191
	}
faluco@654
   192
faluco@654
   193
	const char *value;
faluco@654
   194
	char *key, *defvalue;
faluco@654
   195
	pCtx->LocalToString(params[2], &key);
faluco@654
   196
	pCtx->LocalToString(params[5], &defvalue);
faluco@654
   197
faluco@654
   198
	value = pStk->pCurRoot.front()->GetString(key, defvalue);
faluco@654
   199
	pCtx->StringToLocalUTF8(params[3], params[4], value, NULL);
faluco@654
   200
faluco@654
   201
	return 1;
faluco@654
   202
}
faluco@654
   203
faluco@654
   204
static cell_t smn_KvGetNum(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
	int value;
faluco@654
   221
	char *key;
faluco@654
   222
	pCtx->LocalToString(params[2], &key);
faluco@654
   223
faluco@654
   224
	value = pStk->pCurRoot.front()->GetInt(key, params[3]);
faluco@654
   225
faluco@654
   226
	return value;
faluco@654
   227
}
faluco@654
   228
faluco@654
   229
static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params)
faluco@654
   230
{
faluco@654
   231
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   232
	HandleError herr;
faluco@654
   233
	HandleSecurity sec;
faluco@654
   234
	KeyValueStack *pStk;
faluco@654
   235
faluco@654
   236
	sec.pOwner = NULL;
faluco@654
   237
	sec.pIdentity = g_pCoreIdent;
faluco@654
   238
faluco@654
   239
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   240
		!= HandleError_None)
faluco@654
   241
	{
faluco@654
   242
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   243
	}
faluco@654
   244
faluco@654
   245
	float value;
faluco@654
   246
	char *key;
faluco@654
   247
	pCtx->LocalToString(params[2], &key);
faluco@654
   248
faluco@654
   249
	value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3]));
faluco@654
   250
faluco@654
   251
	return sp_ftoc(value);
faluco@654
   252
}
faluco@654
   253
faluco@654
   254
static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params)
faluco@654
   255
{
faluco@654
   256
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   257
	HandleError herr;
faluco@654
   258
	HandleSecurity sec;
faluco@654
   259
	KeyValueStack *pStk;
faluco@654
   260
faluco@654
   261
	sec.pOwner = NULL;
faluco@654
   262
	sec.pIdentity = g_pCoreIdent;
faluco@654
   263
faluco@654
   264
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   265
		!= HandleError_None)
faluco@654
   266
	{
faluco@654
   267
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   268
	}
faluco@654
   269
faluco@654
   270
	Color color;
faluco@654
   271
	char *key;
faluco@654
   272
	cell_t *r, *g, *b, *a;
faluco@654
   273
	pCtx->LocalToString(params[2], &key);
faluco@654
   274
	pCtx->LocalToPhysAddr(params[3], &r);
faluco@654
   275
	pCtx->LocalToPhysAddr(params[4], &g);
faluco@654
   276
	pCtx->LocalToPhysAddr(params[5], &b);
faluco@654
   277
	pCtx->LocalToPhysAddr(params[6], &a);
faluco@654
   278
faluco@654
   279
	color = pStk->pCurRoot.front()->GetColor(key);
faluco@654
   280
	*r = color.r();
faluco@654
   281
	*g = color.g();
faluco@654
   282
	*b = color.b();
faluco@654
   283
	*a = color.a();
faluco@654
   284
faluco@654
   285
	return 1;
faluco@654
   286
}
faluco@654
   287
faluco@654
   288
static cell_t smn_KvGetUint64(IPluginContext *pCtx, const cell_t *params)
faluco@654
   289
{
faluco@654
   290
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   291
	HandleError herr;
faluco@654
   292
	HandleSecurity sec;
faluco@654
   293
	KeyValueStack *pStk;
faluco@654
   294
faluco@654
   295
	sec.pOwner = NULL;
faluco@654
   296
	sec.pIdentity = g_pCoreIdent;
faluco@654
   297
faluco@654
   298
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   299
		!= HandleError_None)
faluco@654
   300
	{
faluco@654
   301
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   302
	}
faluco@654
   303
faluco@654
   304
	char *key;
faluco@654
   305
	cell_t *addr, *defvalue;
faluco@654
   306
	uint64 value;
faluco@654
   307
	pCtx->LocalToString(params[2], &key);
faluco@654
   308
	pCtx->LocalToPhysAddr(params[3], &addr);
faluco@654
   309
	pCtx->LocalToPhysAddr(params[4], &defvalue);
faluco@654
   310
faluco@654
   311
	value = pStk->pCurRoot.front()->GetUint64(key, static_cast<uint64>(*defvalue));
faluco@654
   312
	*reinterpret_cast<uint64 *>(addr) = value;
faluco@654
   313
faluco@654
   314
	return 1;
faluco@654
   315
}
faluco@654
   316
faluco@654
   317
static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params)
faluco@654
   318
{
faluco@654
   319
	KeyValueStack *pStk;
faluco@654
   320
	char *name, *firstkey, *firstvalue;
faluco@654
   321
	bool is_empty;
faluco@654
   322
faluco@654
   323
	pCtx->LocalToString(params[1], &name);
faluco@654
   324
	pCtx->LocalToString(params[2], &firstkey);
faluco@654
   325
	pCtx->LocalToString(params[3], &firstvalue);
faluco@654
   326
faluco@654
   327
	is_empty = (firstkey[0] == '\0');
faluco@654
   328
	pStk = new KeyValueStack;
faluco@654
   329
	pStk->pBase = new KeyValues(name, is_empty ? NULL : firstkey, (is_empty||(firstvalue[0]=='\0')) ? NULL : firstvalue);
faluco@654
   330
	pStk->pCurRoot.push(pStk->pBase);
faluco@654
   331
faluco@654
   332
	return g_HandleSys.CreateHandle(g_KeyValueType, pStk, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@654
   333
}
faluco@654
   334
faluco@654
   335
static cell_t smn_KvJumpToKey(IPluginContext *pCtx, const cell_t *params)
faluco@654
   336
{
faluco@654
   337
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   338
	HandleError herr;
faluco@654
   339
	HandleSecurity sec;
faluco@654
   340
	char *name;	
faluco@654
   341
	KeyValueStack *pStk;
faluco@654
   342
faluco@654
   343
	sec.pOwner = NULL;
faluco@654
   344
	sec.pIdentity = g_pCoreIdent;
faluco@654
   345
faluco@654
   346
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   347
		!= HandleError_None)
faluco@654
   348
	{
faluco@654
   349
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   350
	}
faluco@654
   351
faluco@654
   352
	pCtx->LocalToString(params[2], &name);
faluco@654
   353
faluco@654
   354
	KeyValues *pSubKey = pStk->pBase->FindKey(name, (params[3]) ? true : false);
faluco@654
   355
	if (!pSubKey)
faluco@654
   356
	{
faluco@654
   357
		return 0;
faluco@654
   358
	}
faluco@654
   359
	pStk->pCurRoot.push(pSubKey);
faluco@654
   360
faluco@654
   361
	return 1;
faluco@654
   362
}
faluco@654
   363
faluco@654
   364
static cell_t smn_KvJumpFirstSubKey(IPluginContext *pCtx, const cell_t *params)
faluco@654
   365
{
faluco@654
   366
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   367
	HandleError herr;
faluco@654
   368
	HandleSecurity sec;
faluco@654
   369
	KeyValueStack *pStk;
faluco@654
   370
faluco@654
   371
	sec.pOwner = NULL;
faluco@654
   372
	sec.pIdentity = g_pCoreIdent;
faluco@654
   373
faluco@654
   374
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   375
		!= HandleError_None)
faluco@654
   376
	{
faluco@654
   377
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   378
	}
faluco@654
   379
faluco@654
   380
	KeyValues *pSubKey = pStk->pCurRoot.front();
faluco@654
   381
	KeyValues *pFirstSubKey = pSubKey->GetFirstSubKey();
faluco@654
   382
	if (!pFirstSubKey)
faluco@654
   383
	{
faluco@654
   384
		return 0;
faluco@654
   385
	}
faluco@654
   386
	pStk->pCurRoot.push(pFirstSubKey);
faluco@654
   387
faluco@654
   388
	return 1;
faluco@654
   389
}
faluco@654
   390
faluco@654
   391
static cell_t smn_KvJumpNextSubKey(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 *pNextKey = pSubKey->GetNextKey();
faluco@654
   409
	if (!pNextKey)
faluco@654
   410
	{
faluco@654
   411
		return 0;
faluco@654
   412
	}
faluco@654
   413
	pStk->pCurRoot.push(pNextKey);
faluco@654
   414
faluco@654
   415
	return 1;	
faluco@654
   416
}
faluco@654
   417
faluco@654
   418
static cell_t smn_KvGoBack(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
	if (pStk->pCurRoot.size() == 1)
faluco@654
   435
	{
faluco@654
   436
		return 0;
faluco@654
   437
	}
faluco@654
   438
	pStk->pCurRoot.pop();
faluco@654
   439
faluco@654
   440
	return 1;
faluco@654
   441
}
faluco@654
   442
faluco@654
   443
static cell_t smn_KvRewind(IPluginContext *pCtx, const cell_t *params)
faluco@654
   444
{
faluco@654
   445
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   446
	HandleError herr;
faluco@654
   447
	HandleSecurity sec;
faluco@654
   448
	KeyValueStack *pStk;
faluco@654
   449
faluco@654
   450
	sec.pOwner = NULL;
faluco@654
   451
	sec.pIdentity = g_pCoreIdent;
faluco@654
   452
faluco@654
   453
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   454
		!= HandleError_None)
faluco@654
   455
	{
faluco@654
   456
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   457
	}
faluco@654
   458
faluco@654
   459
	while (pStk->pCurRoot.size() > 1)
faluco@654
   460
	{
faluco@654
   461
		pStk->pCurRoot.pop();
faluco@654
   462
	}
faluco@654
   463
faluco@654
   464
	return 1;
faluco@654
   465
}
faluco@654
   466
faluco@654
   467
static cell_t smn_KvGetSectionName(IPluginContext *pCtx, const cell_t *params)
faluco@654
   468
{
faluco@654
   469
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   470
	HandleError herr;
faluco@654
   471
	HandleSecurity sec;
faluco@654
   472
	KeyValueStack *pStk;
faluco@654
   473
faluco@654
   474
	sec.pOwner = NULL;
faluco@654
   475
	sec.pIdentity = g_pCoreIdent;
faluco@654
   476
faluco@654
   477
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   478
		!= HandleError_None)
faluco@654
   479
	{
faluco@654
   480
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   481
	}
faluco@654
   482
faluco@654
   483
	KeyValues *pSection = pStk->pCurRoot.front();
faluco@654
   484
	const char *name = pSection->GetName();
faluco@654
   485
	if (!name)
faluco@654
   486
	{
faluco@654
   487
		return 0;
faluco@654
   488
	}
faluco@654
   489
	pCtx->StringToLocalUTF8(params[2], params[3], name, NULL);
faluco@654
   490
faluco@654
   491
	return 1;
faluco@654
   492
}
faluco@654
   493
faluco@654
   494
static cell_t smn_KvSetSectionName(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
	char *name;
faluco@654
   500
	KeyValueStack *pStk;
faluco@654
   501
faluco@654
   502
	sec.pOwner = NULL;
faluco@654
   503
	sec.pIdentity = g_pCoreIdent;
faluco@654
   504
faluco@654
   505
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   506
		!= HandleError_None)
faluco@654
   507
	{
faluco@654
   508
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   509
	}
faluco@654
   510
faluco@654
   511
	pCtx->LocalToString(params[2], &name);
faluco@654
   512
faluco@654
   513
	KeyValues *pSection = pStk->pCurRoot.front();
faluco@654
   514
	pSection->SetName(name);
faluco@654
   515
faluco@654
   516
	return 1;
faluco@654
   517
}
faluco@654
   518
faluco@654
   519
static cell_t smn_KvGetDataType(IPluginContext *pCtx, const cell_t *params)
faluco@654
   520
{
faluco@654
   521
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   522
	HandleError herr;
faluco@654
   523
	HandleSecurity sec;
faluco@654
   524
	char *name;
faluco@654
   525
	KeyValueStack *pStk;
faluco@654
   526
faluco@654
   527
	sec.pOwner = NULL;
faluco@654
   528
	sec.pIdentity = g_pCoreIdent;
faluco@654
   529
faluco@654
   530
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   531
		!= HandleError_None)
faluco@654
   532
	{
faluco@654
   533
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   534
	}
faluco@654
   535
faluco@654
   536
	pCtx->LocalToString(params[2], &name);
faluco@654
   537
faluco@654
   538
	return pStk->pCurRoot.front()->GetDataType(name);
faluco@654
   539
}
faluco@654
   540
faluco@654
   541
static cell_t smn_KeyValuesToFile(IPluginContext *pCtx, const cell_t *params)
faluco@654
   542
{
faluco@654
   543
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   544
	HandleError herr;
faluco@654
   545
	HandleSecurity sec;
faluco@654
   546
	char *path;
faluco@654
   547
	KeyValueStack *pStk;
faluco@654
   548
faluco@654
   549
	sec.pOwner = NULL;
faluco@654
   550
	sec.pIdentity = g_pCoreIdent;
faluco@654
   551
faluco@654
   552
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   553
		!= HandleError_None)
faluco@654
   554
	{
faluco@654
   555
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   556
	}
faluco@654
   557
faluco@654
   558
	pCtx->LocalToString(params[2], &path);
faluco@654
   559
faluco@654
   560
	char realpath[PLATFORM_MAX_PATH];
faluco@654
   561
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
faluco@654
   562
faluco@654
   563
	return pStk->pCurRoot.front()->SaveToFile(basefilesystem, realpath);
faluco@654
   564
}
faluco@654
   565
faluco@654
   566
static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
faluco@654
   567
{
faluco@654
   568
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   569
	HandleError herr;
faluco@654
   570
	HandleSecurity sec;
faluco@654
   571
	char *path;
faluco@654
   572
	KeyValueStack *pStk;
faluco@654
   573
faluco@654
   574
	sec.pOwner = NULL;
faluco@654
   575
	sec.pIdentity = g_pCoreIdent;
faluco@654
   576
faluco@654
   577
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   578
		!= HandleError_None)
faluco@654
   579
	{
faluco@654
   580
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   581
	}
faluco@654
   582
faluco@654
   583
	pCtx->LocalToString(params[2], &path);
faluco@654
   584
faluco@654
   585
	char realpath[PLATFORM_MAX_PATH];
faluco@654
   586
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
faluco@654
   587
faluco@654
   588
	return pStk->pCurRoot.front()->LoadFromFile(basefilesystem, realpath);
faluco@654
   589
}
faluco@654
   590
faluco@654
   591
static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
faluco@654
   592
{
faluco@654
   593
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@654
   594
	HandleError herr;
faluco@654
   595
	HandleSecurity sec;
faluco@654
   596
	KeyValueStack *pStk;
faluco@654
   597
faluco@654
   598
	sec.pOwner = NULL;
faluco@654
   599
	sec.pIdentity = g_pCoreIdent;
faluco@654
   600
faluco@654
   601
	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
faluco@654
   602
		!= HandleError_None)
faluco@654
   603
	{
faluco@654
   604
		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
faluco@654
   605
	}
faluco@654
   606
faluco@654
   607
	pStk->pCurRoot.front()->UsesEscapeSequences(params[2] ? true : false);
faluco@654
   608
faluco@654
   609
	return 1;
faluco@654
   610
}
faluco@654
   611
faluco@654
   612
static KeyValuekNatives s_KeyValuekNatives;
faluco@654
   613
faluco@654
   614
REGISTER_NATIVES(keyvaluenatives)
faluco@654
   615
{
faluco@654
   616
	{"KvSetString",				smn_KvSetString},
faluco@654
   617
	{"KvSetNum",				smn_KvSetNum},
faluco@654
   618
	{"KvSetUint64",				smn_KvSetUint64},
faluco@654
   619
	{"KvSetFloat",				smn_KvSetFloat},
faluco@654
   620
	{"KvSetColor",				smn_KvSetColor},
faluco@654
   621
	{"KvGetString",				smn_KvGetString},
faluco@654
   622
	{"KvGetNum",				smn_KvGetNum},
faluco@654
   623
	{"KvGetFloat",				smn_KvGetFloat},
faluco@654
   624
	{"KvGetColor",				smn_KvGetColor},
faluco@654
   625
	{"KvGetUint64",				smn_KvGetUint64},
faluco@654
   626
	{"CreateKeyValues",			smn_CreateKeyValues},
faluco@654
   627
	{"KvJumpToKey",				smn_KvJumpToKey},
faluco@654
   628
	{"KvJumpFirstSubKey",		smn_KvJumpFirstSubKey},
faluco@654
   629
	{"KvJumpNextSubKey",		smn_KvJumpNextSubKey},
faluco@654
   630
	{"KvGoBack",				smn_KvGoBack},
faluco@654
   631
	{"KvRewind",				smn_KvRewind},
faluco@654
   632
	{"KvGetSectionName",		smn_KvGetSectionName},
faluco@654
   633
	{"KvSetSectionName",		smn_KvSetSectionName},
faluco@654
   634
	{"KvGetDataType",			smn_KvGetDataType},
faluco@654
   635
	{"KeyValuesToFile",			smn_KeyValuesToFile},
faluco@654
   636
	{"FileToKeyValues",			smn_FileToKeyValues},
faluco@654
   637
	{"KvSetEscapeSequences",	smn_KvSetEscapeSequences},
faluco@654
   638
	{NULL,						NULL}
faluco@654
   639
};