core/smn_gameconfigs.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Mar 09 18:41:25 2010 -0500 (2010-03-09)
changeset 2932 77cd75e433b4
parent 2902 a324d1afe2ce
permissions -rw-r--r--
Fixed inaccurate description for sm_unban (bug 4245, r=dvander)
faluco@703
     1
/**
faluco@703
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
faluco@703
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
faluco@703
    28
 *
faluco@703
    29
 * Version: $Id$
faluco@703
    30
 */
faluco@703
    31
faluco@703
    32
#include "sourcemod.h"
faluco@703
    33
#include "HandleSys.h"
faluco@703
    34
#include "GameConfigs.h"
faluco@703
    35
faluco@703
    36
HandleType_t g_GameConfigsType;
faluco@703
    37
faluco@703
    38
class GameConfigsNatives :
faluco@703
    39
	public IHandleTypeDispatch, 
faluco@703
    40
	public SourceModBase
faluco@703
    41
{
faluco@703
    42
public:
faluco@703
    43
	void OnSourceModAllInitialized()
faluco@703
    44
	{
faluco@703
    45
		g_GameConfigsType = g_HandleSys.CreateType("GameConfigs", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@703
    46
	}
faluco@703
    47
	void OnSourceModShutdown()
faluco@703
    48
	{
faluco@703
    49
		g_HandleSys.RemoveType(g_GameConfigsType, g_pCoreIdent);
faluco@703
    50
		g_GameConfigsType = 0;
faluco@703
    51
	}
faluco@703
    52
	void OnHandleDestroy(HandleType_t type, void *object)
faluco@703
    53
	{
faluco@703
    54
		g_GameConfigs.CloseGameConfigFile(reinterpret_cast<IGameConfig *>(object));
faluco@703
    55
	}
faluco@703
    56
};
faluco@703
    57
faluco@703
    58
static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
faluco@703
    59
{
faluco@703
    60
	IGameConfig *gc;
faluco@703
    61
	char *filename;
faluco@703
    62
	char error[128];
faluco@703
    63
faluco@703
    64
	pCtx->LocalToString(params[1], &filename);
faluco@703
    65
	if (!g_GameConfigs.LoadGameConfigFile(filename, &gc, error, sizeof(error)))
faluco@703
    66
	{
faluco@703
    67
		return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
faluco@703
    68
	}
faluco@703
    69
faluco@703
    70
	return g_HandleSys.CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@703
    71
}
faluco@703
    72
faluco@703
    73
static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
faluco@703
    74
{
faluco@703
    75
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@703
    76
	HandleError herr;
faluco@703
    77
	HandleSecurity sec;
faluco@703
    78
	IGameConfig *gc;
faluco@703
    79
faluco@703
    80
	sec.pOwner = NULL;
faluco@703
    81
	sec.pIdentity = g_pCoreIdent;
faluco@703
    82
faluco@703
    83
	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703
    84
		!= HandleError_None)
faluco@703
    85
	{
faluco@703
    86
		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703
    87
	}
faluco@703
    88
faluco@703
    89
	char *key;
faluco@703
    90
	int val;
faluco@703
    91
	pCtx->LocalToString(params[2], &key);
faluco@703
    92
faluco@703
    93
	if (!gc->GetOffset(key, &val))
faluco@703
    94
	{
faluco@703
    95
		return -1;
faluco@703
    96
	}
faluco@703
    97
faluco@703
    98
	return val;
faluco@703
    99
}
faluco@703
   100
faluco@703
   101
static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params)
faluco@703
   102
{
faluco@703
   103
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@703
   104
	HandleError herr;
faluco@703
   105
	HandleSecurity sec;
faluco@703
   106
	IGameConfig *gc;
faluco@703
   107
faluco@703
   108
	sec.pOwner = NULL;
faluco@703
   109
	sec.pIdentity = g_pCoreIdent;
faluco@703
   110
faluco@703
   111
	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703
   112
		!= HandleError_None)
faluco@703
   113
	{
faluco@703
   114
		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703
   115
	}
faluco@703
   116
faluco@703
   117
	char *key;
faluco@703
   118
	const char *val;
faluco@703
   119
	pCtx->LocalToString(params[2], &key);
faluco@703
   120
faluco@703
   121
	if ((val=gc->GetKeyValue(key)) == NULL)
faluco@703
   122
	{
faluco@703
   123
		return 0;
faluco@703
   124
	}
faluco@703
   125
faluco@703
   126
	pCtx->StringToLocalUTF8(params[3], params[4], val, NULL);
faluco@703
   127
faluco@703
   128
	return 1;
faluco@703
   129
}
faluco@703
   130
downtown1@2901
   131
downtown1@2901
   132
static cell_t smn_GameConfGetAddress(IPluginContext *pCtx, const cell_t *params)
downtown1@2901
   133
{
downtown1@2901
   134
	Handle_t hndl = static_cast<Handle_t>(params[1]);
downtown1@2901
   135
	HandleError herr;
downtown1@2901
   136
	HandleSecurity sec;
downtown1@2901
   137
	IGameConfig *gc;
downtown1@2901
   138
downtown1@2901
   139
	sec.pOwner = NULL;
downtown1@2901
   140
	sec.pIdentity = g_pCoreIdent;
downtown1@2901
   141
downtown1@2901
   142
	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
downtown1@2901
   143
		!= HandleError_None)
downtown1@2901
   144
	{
downtown1@2901
   145
		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
downtown1@2901
   146
	}
downtown1@2901
   147
downtown1@2901
   148
	char *key;
downtown1@2901
   149
	void* val;
downtown1@2901
   150
	pCtx->LocalToString(params[2], &key);
downtown1@2901
   151
downtown1@2901
   152
	if (!gc->GetAddress(key, &val))
dvander@2902
   153
		return 0;
downtown1@2901
   154
downtown1@2901
   155
	return (cell_t)val;
downtown1@2901
   156
}
downtown1@2901
   157
faluco@703
   158
static GameConfigsNatives s_GameConfigsNatives;
faluco@703
   159
faluco@703
   160
REGISTER_NATIVES(gameconfignatives)
faluco@703
   161
{
faluco@703
   162
	{"LoadGameConfigFile",			smn_LoadGameConfigFile},
faluco@703
   163
	{"GameConfGetOffset",			smn_GameConfGetOffset},
faluco@703
   164
	{"GameConfGetKeyValue",			smn_GameConfGetKeyValue},
downtown1@2901
   165
	{"GameConfGetAddress",			smn_GameConfGetAddress},
faluco@703
   166
	{NULL,							NULL}
faluco@710
   167
};