core/smn_gameconfigs.cpp
author David Anderson <dvander@alliedmods.net>
Tue May 22 20:50:48 2007 +0000 (2007-05-22)
changeset 833 732bedfcbe3e
parent 710 6a74859ed94c
child 1231 17badaf66ce4
permissions -rw-r--r--
added ReplaceString and ReplaceStringEx (phew!)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sourcemod.h"
    16 #include "HandleSys.h"
    17 #include "GameConfigs.h"
    18 
    19 HandleType_t g_GameConfigsType;
    20 
    21 class GameConfigsNatives :
    22 	public IHandleTypeDispatch, 
    23 	public SourceModBase
    24 {
    25 public:
    26 	void OnSourceModAllInitialized()
    27 	{
    28 		g_GameConfigsType = g_HandleSys.CreateType("GameConfigs", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    29 	}
    30 	void OnSourceModShutdown()
    31 	{
    32 		g_HandleSys.RemoveType(g_GameConfigsType, g_pCoreIdent);
    33 		g_GameConfigsType = 0;
    34 	}
    35 	void OnHandleDestroy(HandleType_t type, void *object)
    36 	{
    37 		g_GameConfigs.CloseGameConfigFile(reinterpret_cast<IGameConfig *>(object));
    38 	}
    39 };
    40 
    41 static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
    42 {
    43 	IGameConfig *gc;
    44 	char *filename;
    45 	char error[128];
    46 
    47 	pCtx->LocalToString(params[1], &filename);
    48 	if (!g_GameConfigs.LoadGameConfigFile(filename, &gc, error, sizeof(error)))
    49 	{
    50 		return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
    51 	}
    52 
    53 	return g_HandleSys.CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
    54 }
    55 
    56 static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
    57 {
    58 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    59 	HandleError herr;
    60 	HandleSecurity sec;
    61 	IGameConfig *gc;
    62 
    63 	sec.pOwner = NULL;
    64 	sec.pIdentity = g_pCoreIdent;
    65 
    66 	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
    67 		!= HandleError_None)
    68 	{
    69 		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
    70 	}
    71 
    72 	char *key;
    73 	int val;
    74 	pCtx->LocalToString(params[2], &key);
    75 
    76 	if (!gc->GetOffset(key, &val))
    77 	{
    78 		return -1;
    79 	}
    80 
    81 	return val;
    82 }
    83 
    84 static cell_t smn_GameConfGetKeyValue(IPluginContext *pCtx, const cell_t *params)
    85 {
    86 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    87 	HandleError herr;
    88 	HandleSecurity sec;
    89 	IGameConfig *gc;
    90 
    91 	sec.pOwner = NULL;
    92 	sec.pIdentity = g_pCoreIdent;
    93 
    94 	if ((herr=g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
    95 		!= HandleError_None)
    96 	{
    97 		return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
    98 	}
    99 
   100 	char *key;
   101 	const char *val;
   102 	pCtx->LocalToString(params[2], &key);
   103 
   104 	if ((val=gc->GetKeyValue(key)) == NULL)
   105 	{
   106 		return 0;
   107 	}
   108 
   109 	pCtx->StringToLocalUTF8(params[3], params[4], val, NULL);
   110 
   111 	return 1;
   112 }
   113 
   114 static GameConfigsNatives s_GameConfigsNatives;
   115 
   116 REGISTER_NATIVES(gameconfignatives)
   117 {
   118 	{"LoadGameConfigFile",			smn_LoadGameConfigFile},
   119 	{"GameConfGetOffset",			smn_GameConfGetOffset},
   120 	{"GameConfGetKeyValue",			smn_GameConfGetKeyValue},
   121 	{NULL,							NULL}
   122 };