faluco@703: /** faluco@703: * vim: set ts=4 : ds@1330: * ============================================================================= dvander@1231: * SourceMod dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved. ds@1330: * ============================================================================= faluco@703: * ds@1330: * This program is free software; you can redistribute it and/or modify it under ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the ds@1330: * Free Software Foundation. dvander@1231: * ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1330: * details. dvander@1231: * ds@1330: * You should have received a copy of the GNU General Public License along with ds@1330: * this program. If not, see . dvander@1231: * ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1330: * this exception to all derivative works. AlliedModders LLC defines further ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1330: * 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: };