faluco@703: /**
faluco@703: * vim: set ts=4 :
dvander@1231: * ================================================================
dvander@1231: * SourceMod
dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
dvander@1231: * ================================================================
faluco@703: *
dvander@1231: * This program is free software; you can redistribute it and/or
dvander@1231: * modify it under the terms of the GNU General Public License,
dvander@1231: * version 3.0, as published by the Free Software Foundation.
dvander@1231: *
dvander@1231: * This program is distributed in the hope that it will be useful,
dvander@1231: * but WITHOUT ANY WARRANTY; without even the implied warranty of
dvander@1231: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dvander@1231: * GNU General Public License for more details.
dvander@1231: *
dvander@1231: * You should have received a copy of the GNU General Public License
dvander@1231: * along with this program. If not, see .
dvander@1231: *
dvander@1231: * As a special exception, AlliedModders LLC gives you permission to
dvander@1231: * link the code of this program (as well as its derivative works) to
dvander@1231: * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
dvander@1231: * Game MODs that run on software by the Valve Corporation. You must
dvander@1231: * obey the GNU General Public License in all respects for all other
dvander@1231: * code used. Additionally, AlliedModders LLC grants this exception
dvander@1231: * to all derivative works. AlliedModders LLC defines further
dvander@1231: * exceptions, found in LICENSE.txt (as of this writing, version
dvander@1231: * JULY-31-2007), or .
faluco@703: *
faluco@703: * Version: $Id$
faluco@703: */
faluco@703:
faluco@703: #include "sourcemod.h"
faluco@703: #include "HandleSys.h"
faluco@703: #include "GameConfigs.h"
faluco@703:
faluco@703: HandleType_t g_GameConfigsType;
faluco@703:
faluco@703: class GameConfigsNatives :
faluco@703: public IHandleTypeDispatch,
faluco@703: public SourceModBase
faluco@703: {
faluco@703: public:
faluco@703: void OnSourceModAllInitialized()
faluco@703: {
faluco@703: g_GameConfigsType = g_HandleSys.CreateType("GameConfigs", this, 0, NULL, NULL, g_pCoreIdent, NULL);
faluco@703: }
faluco@703: void OnSourceModShutdown()
faluco@703: {
faluco@703: g_HandleSys.RemoveType(g_GameConfigsType, g_pCoreIdent);
faluco@703: g_GameConfigsType = 0;
faluco@703: }
faluco@703: void OnHandleDestroy(HandleType_t type, void *object)
faluco@703: {
faluco@703: g_GameConfigs.CloseGameConfigFile(reinterpret_cast(object));
faluco@703: }
faluco@703: };
faluco@703:
faluco@703: static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
faluco@703: {
faluco@703: IGameConfig *gc;
faluco@703: char *filename;
faluco@703: char error[128];
faluco@703:
faluco@703: pCtx->LocalToString(params[1], &filename);
faluco@703: if (!g_GameConfigs.LoadGameConfigFile(filename, &gc, error, sizeof(error)))
faluco@703: {
faluco@703: return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
faluco@703: }
faluco@703:
faluco@703: return g_HandleSys.CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@703: }
faluco@703:
faluco@703: static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
faluco@703: {
faluco@703: Handle_t hndl = static_cast(params[1]);
faluco@703: HandleError herr;
faluco@703: HandleSecurity sec;
faluco@703: IGameConfig *gc;
faluco@703:
faluco@703: sec.pOwner = NULL;
faluco@703: sec.pIdentity = g_pCoreIdent;
faluco@703:
faluco@703: if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703: != HandleError_None)
faluco@703: {
faluco@703: return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703: }
faluco@703:
faluco@703: char *key;
faluco@703: int val;
faluco@703: pCtx->LocalToString(params[2], &key);
faluco@703:
faluco@703: if (!gc->GetOffset(key, &val))
faluco@703: {
faluco@703: return -1;
faluco@703: }
faluco@703:
faluco@703: return val;
faluco@703: }
faluco@703:
faluco@703: static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params)
faluco@703: {
faluco@703: Handle_t hndl = static_cast(params[1]);
faluco@703: HandleError herr;
faluco@703: HandleSecurity sec;
faluco@703: IGameConfig *gc;
faluco@703:
faluco@703: sec.pOwner = NULL;
faluco@703: sec.pIdentity = g_pCoreIdent;
faluco@703:
faluco@703: if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
faluco@703: != HandleError_None)
faluco@703: {
faluco@703: return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
faluco@703: }
faluco@703:
faluco@703: char *key;
faluco@703: const char *val;
faluco@703: pCtx->LocalToString(params[2], &key);
faluco@703:
faluco@703: if ((val=gc->GetKeyValue(key)) == NULL)
faluco@703: {
faluco@703: return 0;
faluco@703: }
faluco@703:
faluco@703: pCtx->StringToLocalUTF8(params[3], params[4], val, NULL);
faluco@703:
faluco@703: return 1;
faluco@703: }
faluco@703:
faluco@703: static GameConfigsNatives s_GameConfigsNatives;
faluco@703:
faluco@703: REGISTER_NATIVES(gameconfignatives)
faluco@703: {
faluco@703: {"LoadGameConfigFile", smn_LoadGameConfigFile},
faluco@703: {"GameConfGetOffset", smn_GameConfGetOffset},
faluco@703: {"GameConfGetKeyValue", smn_GameConfGetKeyValue},
faluco@703: {NULL, NULL}
faluco@710: };