core/smn_gameconfigs.cpp
changeset 703 cddbff919185
child 710 6a74859ed94c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_gameconfigs.cpp	Sun Apr 22 18:25:10 2007 +0000
     1.3 @@ -0,0 +1,122 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "sourcemod.h"
    1.19 +#include "HandleSys.h"
    1.20 +#include "GameConfigs.h"
    1.21 +
    1.22 +HandleType_t g_GameConfigsType;
    1.23 +
    1.24 +class GameConfigsNatives :
    1.25 +	public IHandleTypeDispatch, 
    1.26 +	public SourceModBase
    1.27 +{
    1.28 +public:
    1.29 +	void OnSourceModAllInitialized()
    1.30 +	{
    1.31 +		g_GameConfigsType = g_HandleSys.CreateType("GameConfigs", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.32 +	}
    1.33 +	void OnSourceModShutdown()
    1.34 +	{
    1.35 +		g_HandleSys.RemoveType(g_GameConfigsType, g_pCoreIdent);
    1.36 +		g_GameConfigsType = 0;
    1.37 +	}
    1.38 +	void OnHandleDestroy(HandleType_t type, void *object)
    1.39 +	{
    1.40 +		g_GameConfigs.CloseGameConfigFile(reinterpret_cast<IGameConfig *>(object));
    1.41 +	}
    1.42 +};
    1.43 +
    1.44 +static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
    1.45 +{
    1.46 +	IGameConfig *gc;
    1.47 +	char *filename;
    1.48 +	char error[128];
    1.49 +
    1.50 +	pCtx->LocalToString(params[1], &filename);
    1.51 +	if (!g_GameConfigs.LoadGameConfigFile(filename, &gc, error, sizeof(error)))
    1.52 +	{
    1.53 +		return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
    1.54 +	}
    1.55 +
    1.56 +	return g_HandleSys.CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
    1.57 +}
    1.58 +
    1.59 +static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
    1.60 +{
    1.61 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.62 +	HandleError herr;
    1.63 +	HandleSecurity sec;
    1.64 +	IGameConfig *gc;
    1.65 +
    1.66 +	sec.pOwner = NULL;
    1.67 +	sec.pIdentity = g_pCoreIdent;
    1.68 +
    1.69 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
    1.70 +		!= HandleError_None)
    1.71 +	{
    1.72 +		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
    1.73 +	}
    1.74 +
    1.75 +	char *key;
    1.76 +	int val;
    1.77 +	pCtx->LocalToString(params[2], &key);
    1.78 +
    1.79 +	if (!gc->GetOffset(key, &val))
    1.80 +	{
    1.81 +		return -1;
    1.82 +	}
    1.83 +
    1.84 +	return val;
    1.85 +}
    1.86 +
    1.87 +static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params)
    1.88 +{
    1.89 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.90 +	HandleError herr;
    1.91 +	HandleSecurity sec;
    1.92 +	IGameConfig *gc;
    1.93 +
    1.94 +	sec.pOwner = NULL;
    1.95 +	sec.pIdentity = g_pCoreIdent;
    1.96 +
    1.97 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
    1.98 +		!= HandleError_None)
    1.99 +	{
   1.100 +		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
   1.101 +	}
   1.102 +
   1.103 +	char *key;
   1.104 +	const char *val;
   1.105 +	pCtx->LocalToString(params[2], &key);
   1.106 +
   1.107 +	if ((val=gc->GetKeyValue(key)) == NULL)
   1.108 +	{
   1.109 +		return 0;
   1.110 +	}
   1.111 +
   1.112 +	pCtx->StringToLocalUTF8(params[3], params[4], val, NULL);
   1.113 +
   1.114 +	return 1;
   1.115 +}
   1.116 +
   1.117 +static GameConfigsNatives s_GameConfigsNatives;
   1.118 +
   1.119 +REGISTER_NATIVES(gameconfignatives)
   1.120 +{
   1.121 +	{"LoadGameConfigFile",			smn_LoadGameConfigFile},
   1.122 +	{"GameConfGetOffset",			smn_GameConfGetOffset},
   1.123 +	{"GameConfGetKeyValue",			smn_GameConfGetKeyValue},
   1.124 +	{NULL,							NULL}
   1.125 +};
   1.126 \ No newline at end of file