core/smn_gameconfigs.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jun 08 01:09:17 2007 +0000 (2007-06-08)
changeset 922 980941c525fa
parent 710 6a74859ed94c
child 1231 17badaf66ce4
permissions -rw-r--r--
sm_kick now uses targeting properly
faluco@703
     1
/**
faluco@703
     2
 * vim: set ts=4 :
faluco@703
     3
 * ===============================================================
faluco@703
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
faluco@703
     5
 * ===============================================================
faluco@703
     6
 *
faluco@703
     7
 * This file is not open source and may not be copied without explicit
faluco@703
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
faluco@703
     9
 * in whole or significant part.
faluco@703
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
faluco@703
    11
 *
faluco@703
    12
 * Version: $Id$
faluco@703
    13
 */
faluco@703
    14
faluco@703
    15
#include "sourcemod.h"
faluco@703
    16
#include "HandleSys.h"
faluco@703
    17
#include "GameConfigs.h"
faluco@703
    18
faluco@703
    19
HandleType_t g_GameConfigsType;
faluco@703
    20
faluco@703
    21
class GameConfigsNatives :
faluco@703
    22
	public IHandleTypeDispatch, 
faluco@703
    23
	public SourceModBase
faluco@703
    24
{
faluco@703
    25
public:
faluco@703
    26
	void OnSourceModAllInitialized()
faluco@703
    27
	{
faluco@703
    28
		g_GameConfigsType = g_HandleSys.CreateType("GameConfigs", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@703
    29
	}
faluco@703
    30
	void OnSourceModShutdown()
faluco@703
    31
	{
faluco@703
    32
		g_HandleSys.RemoveType(g_GameConfigsType, g_pCoreIdent);
faluco@703
    33
		g_GameConfigsType = 0;
faluco@703
    34
	}
faluco@703
    35
	void OnHandleDestroy(HandleType_t type, void *object)
faluco@703
    36
	{
faluco@703
    37
		g_GameConfigs.CloseGameConfigFile(reinterpret_cast<IGameConfig *>(object));
faluco@703
    38
	}
faluco@703
    39
};
faluco@703
    40
faluco@703
    41
static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
faluco@703
    42
{
faluco@703
    43
	IGameConfig *gc;
faluco@703
    44
	char *filename;
faluco@703
    45
	char error[128];
faluco@703
    46
faluco@703
    47
	pCtx->LocalToString(params[1], &filename);
faluco@703
    48
	if (!g_GameConfigs.LoadGameConfigFile(filename, &gc, error, sizeof(error)))
faluco@703
    49
	{
faluco@703
    50
		return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
faluco@703
    51
	}
faluco@703
    52
faluco@703
    53
	return g_HandleSys.CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@703
    54
}
faluco@703
    55
faluco@703
    56
static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
faluco@703
    57
{
faluco@703
    58
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@703
    59
	HandleError herr;
faluco@703
    60
	HandleSecurity sec;
faluco@703
    61
	IGameConfig *gc;
faluco@703
    62
faluco@703
    63
	sec.pOwner = NULL;
faluco@703
    64
	sec.pIdentity = g_pCoreIdent;
faluco@703
    65
faluco@703
    66
	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703
    67
		!= HandleError_None)
faluco@703
    68
	{
faluco@703
    69
		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703
    70
	}
faluco@703
    71
faluco@703
    72
	char *key;
faluco@703
    73
	int val;
faluco@703
    74
	pCtx->LocalToString(params[2], &key);
faluco@703
    75
faluco@703
    76
	if (!gc->GetOffset(key, &val))
faluco@703
    77
	{
faluco@703
    78
		return -1;
faluco@703
    79
	}
faluco@703
    80
faluco@703
    81
	return val;
faluco@703
    82
}
faluco@703
    83
faluco@703
    84
static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params)
faluco@703
    85
{
faluco@703
    86
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@703
    87
	HandleError herr;
faluco@703
    88
	HandleSecurity sec;
faluco@703
    89
	IGameConfig *gc;
faluco@703
    90
faluco@703
    91
	sec.pOwner = NULL;
faluco@703
    92
	sec.pIdentity = g_pCoreIdent;
faluco@703
    93
faluco@703
    94
	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703
    95
		!= HandleError_None)
faluco@703
    96
	{
faluco@703
    97
		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703
    98
	}
faluco@703
    99
faluco@703
   100
	char *key;
faluco@703
   101
	const char *val;
faluco@703
   102
	pCtx->LocalToString(params[2], &key);
faluco@703
   103
faluco@703
   104
	if ((val=gc->GetKeyValue(key)) == NULL)
faluco@703
   105
	{
faluco@703
   106
		return 0;
faluco@703
   107
	}
faluco@703
   108
faluco@703
   109
	pCtx->StringToLocalUTF8(params[3], params[4], val, NULL);
faluco@703
   110
faluco@703
   111
	return 1;
faluco@703
   112
}
faluco@703
   113
faluco@703
   114
static GameConfigsNatives s_GameConfigsNatives;
faluco@703
   115
faluco@703
   116
REGISTER_NATIVES(gameconfignatives)
faluco@703
   117
{
faluco@703
   118
	{"LoadGameConfigFile",			smn_LoadGameConfigFile},
faluco@703
   119
	{"GameConfGetOffset",			smn_GameConfGetOffset},
faluco@703
   120
	{"GameConfGetKeyValue",			smn_GameConfGetKeyValue},
faluco@703
   121
	{NULL,							NULL}
faluco@710
   122
};