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