Merge pull request #254 from Arkshine/feature/gameconfig

Implement a game config parser and expose functionnalities to the module/plugin API
This commit is contained in:
Vincent Herbet
2015-07-11 13:19:09 +02:00
22 changed files with 1747 additions and 5 deletions

139
public/IGameConfigs.h Normal file
View File

@ -0,0 +1,139 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#ifndef _INCLUDE_IGAMECONFIG_H_
#define _INCLUDE_IGAMECONFIG_H_
#include <ITextParsers.h>
/**
* @brief Describes a game private data config file
*/
class IGameConfig
{
public:
/**
* @brief Returns an offset value.
*
* @param key Key to retrieve from the offset section.
* @param value Pointer to store the offset value in.
* @return True if found, false otherwise.
*/
virtual bool GetOffset(const char *key, int *value) = 0;
/**
* @brief Returns an offset value from given class.
*
* @param classname class name to match from the offset section.
* @param key Key to retrieve from the offset section.
* @param value Pointer to store the offset value in.
* @return True if found, false otherwise.
*/
virtual bool GetOffsetByClass(const char *classname, const char *key, int *value) = 0;
/**
* @brief Returns the value of a key from the "Keys" section.
*
* @param key Key to retrieve from the Keys section.
* @return String containing the value, or NULL if not found.
*/
virtual const char *GetKeyValue(const char *key) = 0;
/**
* @brief Retrieves a cached memory signature.
*
* @param key Name of the signature.
* @param addr Pointer to store the memory address in.
* (NULL is copied if signature is not found in binary).
* @return True if the section exists and key for current
* platform was found, false otherwise.
*/
virtual bool GetMemSig(const char *key, void **addr) = 0;
/**
* @brief Retrieves the value of an address from the "Address" section.
*
* @param key Key to retrieve from the Address section.
* @param addr Pointer to store the memory address.
* @return True on success, false on failure.
*/
virtual bool GetAddress(const char *key, void **addr) = 0;
};
/**
* @brief Manages game config files
*/
class IGameConfigManager
{
public:
/**
* @brief Loads or finds an already loaded game config file.
*
* @param file File to load. The path must be relative to the
* 'gamedata' folder and the extension should be
* omitted.
* @param pConfig Pointer to store the game config pointer. Pointer
* will be valid even on failure.
* @param error Optional error message buffer.
* @param maxlength Maximum length of the error buffer.
* @return True on success, false if the file failed.
*/
virtual bool LoadGameConfigFile(const char *file, IGameConfig **pConfig, char *error, size_t maxlength) = 0;
/**
* @brief Closes an IGameConfig pointer. Since a file can be loaded
* more than once, the file will not actually be removed from memory
* until it is closed once for each call to LoadGameConfigfile().
*
* @param cfg Pointer to the IGameConfig to close.
*/
virtual void CloseGameConfigFile(IGameConfig *cfg) = 0;
/**
* @brief Adds a custom gamedata section hook.
*
* @param sectionname Section name to hook.
* @param listener Listener callback.
* @noreturn
*/
virtual void AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener) = 0;
/**
* @brief Removes a custom gamedata section hook.
*
* @param sectionname Section name to unhook.
* @param listener Listener callback.
* @noreturn
*/
virtual void RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener) = 0;
};
#endif //_INCLUDE_IGAMECONFIG_H_

View File

@ -32,6 +32,8 @@
#ifndef _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
#define _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
#include <string.h> // size_t
/**
* @file ITextParsers.h
* @brief Defines various text/file parsing functions, as well as UTF-8 support code.

View File

@ -28,7 +28,7 @@
*/
#include "MemoryUtils.h"
#include "amxxmodule.h"
#include <stdio.h> // sscanf
#if defined(__linux__)
#include <fcntl.h>
@ -159,6 +159,13 @@ void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)
#elif defined(__linux__)
void *addr = dlsym(handle, symbol);
if (addr)
{
return addr;
}
struct link_map *dlmap;
struct stat dlstat;
int dlfile;
@ -637,7 +644,7 @@ bool MemoryUtils::GetLibraryOfAddress(const void *libPtr, char *buffer, size_t m
return false;
}
const char *dllpath = info.dli_fname;
UTIL_Format(buffer, maxlength, "%s", dllpath);
Format(buffer, maxlength, "%s", dllpath);
if (base)
{
*base = (uintptr_t)info.dli_fbase;
@ -701,4 +708,20 @@ size_t MemoryUtils::DecodeHexString(unsigned char *buffer, size_t maxlength, con
}
return written;
}
size_t MemoryUtils::Format(char *buffer, size_t maxlength, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
size_t len = vsnprintf(buffer, maxlength, fmt, ap);
va_end(ap);
if (len >= maxlength)
{
buffer[maxlength - 1] = '\0';
return (maxlength - 1);
}
return len;
}

View File

@ -80,6 +80,7 @@ class MemoryUtils
public:
size_t DecodeHexString(unsigned char *buffer, size_t maxlength, const char *hexstr);
size_t Format(char *buffer, size_t maxlength, const char *fmt, ...);
#if defined(__linux__) || defined(__APPLE__)
private:

View File

@ -2501,6 +2501,7 @@ PFN_GETLOCALINFO g_fn_GetLocalInfo;
PFN_AMX_REREGISTER g_fn_AmxReRegister;
PFN_REGISTERFUNCTIONEX g_fn_RegisterFunctionEx;
PFN_MESSAGE_BLOCK g_fn_MessageBlock;
PFN_GET_CONFIG_MANAGER g_fn_GetConfigManager;
// *** Exports ***
C_DLLEXPORT int AMXX_Query(int *interfaceVersion, amxx_module_info_s *moduleInfo)
@ -2560,6 +2561,7 @@ C_DLLEXPORT int AMXX_Attach(PFN_REQ_FNPTR reqFnptrFunc)
REQFUNC("Format", g_fn_Format, PFN_FORMAT);
REQFUNC("RegisterFunction", g_fn_RegisterFunction, PFN_REGISTERFUNCTION);
REQFUNC("RegisterFunctionEx", g_fn_RegisterFunctionEx, PFN_REGISTERFUNCTIONEX);
REQFUNC("GetConfigManager", g_fn_GetConfigManager, PFN_GET_CONFIG_MANAGER);
// Amx scripts
REQFUNC("GetAmxScript", g_fn_GetAmxScript, PFN_GET_AMXSCRIPT);
@ -2787,6 +2789,7 @@ void ValidateMacros_DontCallThis_Smiley()
MF_RemoveLibraries(NULL);
MF_OverrideNatives(NULL, NULL);
MF_MessageBlock(0, 0, NULL);
MF_GetConfigManager();
}
#endif

View File

@ -18,6 +18,7 @@
// config
#include "moduleconfig.h"
#include <IGameConfigs.h>
#include <stddef.h> // size_t
// metamod include files
@ -2217,6 +2218,7 @@ typedef const char * (*PFN_GETLOCALINFO) (const char * /*name*/, const char *
typedef int (*PFN_AMX_REREGISTER) (AMX * /*amx*/, AMX_NATIVE_INFO * /*list*/, int /*list*/);
typedef void * (*PFN_REGISTERFUNCTIONEX) (void * /*pfn*/, const char * /*desc*/);
typedef void (*PFN_MESSAGE_BLOCK) (int /* mode */, int /* message */, int * /* opt */);
typedef IGameConfigManager* (*PFN_GET_CONFIG_MANAGER) ();
extern PFN_ADD_NATIVES g_fn_AddNatives;
extern PFN_ADD_NEW_NATIVES g_fn_AddNewNatives;
@ -2297,6 +2299,7 @@ extern PFN_GETLOCALINFO g_fn_GetLocalInfo;
extern PFN_AMX_REREGISTER g_fn_AmxReRegister;
extern PFN_REGISTERFUNCTIONEX g_fn_RegisterFunctionEx;
extern PFN_MESSAGE_BLOCK g_fn_MessageBlock;
extern PFN_GET_CONFIG_MANAGER g_fn_GetConfigManager;
#ifdef MAY_NEVER_BE_DEFINED
// Function prototypes for intellisense and similar systems
@ -2374,6 +2377,7 @@ const char * MF_GetLocalInfo (const char *name, const char *def) { }
int MF_AmxReRegister (AMX *amx, AMX_NATIVE_INFO *list, int number) { return 0; }
void * MF_RegisterFunctionEx (void *pfn, const char *description) { }
void * MF_MessageBlock (int mode, int msg, int *opt) { }
IGameConfigManager* MF_GetConfigManager (void) { }
#endif // MAY_NEVER_BE_DEFINED
#define MF_AddNatives g_fn_AddNatives
@ -2456,6 +2460,7 @@ void MF_LogError(AMX *amx, int err, const char *fmt, ...);
#define MF_AmxReRegister g_fn_AmxReRegister
#define MF_RegisterFunctionEx g_fn_RegisterFunctionEx
#define MF_MessageBlock g_fn_MessageBlock
#define MF_GetConfigManager g_fn_GetConfigManager
#ifdef MEMORY_TEST
/*** Memory ***/