Added API to grab arbitrary kv pairs from core.cfg.

This commit is contained in:
David Anderson 2009-02-17 16:19:11 -05:00
parent 97e25c37de
commit 9e445a5adf
5 changed files with 49 additions and 1 deletions

View File

@ -116,6 +116,14 @@ void CoreConfig::OnSourceModAllInitialized()
g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL); g_pOnAutoConfigsBuffered = g_Forwards.CreateForward("OnAutoConfigsBuffered", ET_Ignore, 0, NULL);
} }
CoreConfig::CoreConfig() : m_Strings(512)
{
}
CoreConfig::~CoreConfig()
{
}
void CoreConfig::OnSourceModShutdown() void CoreConfig::OnSourceModShutdown()
{ {
g_RootMenu.RemoveRootConsoleCommand("config", this); g_RootMenu.RemoveRootConsoleCommand("config", this);
@ -212,6 +220,10 @@ void CoreConfig::Initialize()
/* Format path to config file */ /* Format path to config file */
g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg); g_LibSys.PathFormat(filePath, sizeof(filePath), "%s/%s", g_SourceMod.GetGamePath(), corecfg);
/* Reset cached key values */
m_KeyValues.clear();
m_Strings.Reset();
/* Parse config file */ /* Parse config file */
if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay) if ((err=textparsers->ParseFile_SMC(filePath, this, NULL)) != SMCError_Okay)
{ {
@ -250,9 +262,21 @@ ConfigResult CoreConfig::SetConfigOption(const char *option, const char *value,
pBase = pBase->m_pGlobalClassNext; pBase = pBase->m_pGlobalClassNext;
} }
m_KeyValues.replace(option, m_Strings.AddString(value));
return ConfigResult_Ignore; return ConfigResult_Ignore;
} }
const char *CoreConfig::GetCoreConfigValue(const char *key)
{
int *pKey = m_KeyValues.retrieve(key);
if (pKey == NULL)
{
return NULL;
}
return m_Strings.GetString(*pKey);
}
bool SM_AreConfigsExecuted() bool SM_AreConfigsExecuted()
{ {
return g_bConfigsExecd; return g_bConfigsExecd;

View File

@ -35,6 +35,8 @@
#include "sm_globals.h" #include "sm_globals.h"
#include <ITextParsers.h> #include <ITextParsers.h>
#include <IRootConsoleMenu.h> #include <IRootConsoleMenu.h>
#include <sm_trie_tpl.h>
#include "sm_memtable.h"
using namespace SourceMod; using namespace SourceMod;
@ -43,6 +45,9 @@ class CoreConfig :
public ITextListener_SMC, public ITextListener_SMC,
public IRootConsoleCommand public IRootConsoleCommand
{ {
public:
CoreConfig();
~CoreConfig();
public: // SMGlobalClass public: // SMGlobalClass
void OnSourceModAllInitialized(); void OnSourceModAllInitialized();
void OnSourceModShutdown(); void OnSourceModShutdown();
@ -56,11 +61,15 @@ public:
* Initializes CoreConfig by reading from core.cfg file * Initializes CoreConfig by reading from core.cfg file
*/ */
void Initialize(); void Initialize();
const char *GetCoreConfigValue(const char *key);
private: private:
/** /**
* Sets configuration option by notifying SourceMod components that rely on core.cfg * Sets configuration option by notifying SourceMod components that rely on core.cfg
*/ */
ConfigResult SetConfigOption(const char *option, const char *value, ConfigSource, char *Error, size_t maxlength); ConfigResult SetConfigOption(const char *option, const char *value, ConfigSource, char *Error, size_t maxlength);
private:
BaseStringTable m_Strings;
KTrie<int> m_KeyValues;
}; };
extern bool SM_AreConfigsExecuted(); extern bool SM_AreConfigsExecuted();

View File

@ -712,6 +712,11 @@ void SourceModBase::AddFrameAction(FRAMEACTION fn, void *data)
::AddFrameAction(FrameAction(fn, data)); ::AddFrameAction(FrameAction(fn, data));
} }
const char *SourceModBase::GetCoreConfigValue(const char *key)
{
return g_CoreConfig.GetCoreConfigValue(key);
}
SMGlobalClass *SMGlobalClass::head = NULL; SMGlobalClass *SMGlobalClass::head = NULL;
SMGlobalClass::SMGlobalClass() SMGlobalClass::SMGlobalClass()

View File

@ -132,6 +132,7 @@ public: // ISourceMod
size_t Format(char *buffer, size_t maxlength, const char *fmt, ...); size_t Format(char *buffer, size_t maxlength, const char *fmt, ...);
size_t FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap); size_t FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap);
void AddFrameAction(FRAMEACTION fn, void *data); void AddFrameAction(FRAMEACTION fn, void *data);
const char *GetCoreConfigValue(const char *key);
private: private:
CStack<CDataPack *> m_freepacks; CStack<CDataPack *> m_freepacks;
char m_SMBaseDir[PLATFORM_MAX_PATH]; char m_SMBaseDir[PLATFORM_MAX_PATH];

View File

@ -43,7 +43,7 @@
#include <time.h> #include <time.h>
#define SMINTERFACE_SOURCEMOD_NAME "ISourceMod" #define SMINTERFACE_SOURCEMOD_NAME "ISourceMod"
#define SMINTERFACE_SOURCEMOD_VERSION 9 #define SMINTERFACE_SOURCEMOD_VERSION 10
/** /**
* @brief Forward declaration of the KeyValues class. * @brief Forward declaration of the KeyValues class.
@ -287,6 +287,15 @@ namespace SourceMod
* @param data Data to pass to function. * @param data Data to pass to function.
*/ */
virtual void AddFrameAction(FRAMEACTION fn, void *data) = 0; virtual void AddFrameAction(FRAMEACTION fn, void *data) = 0;
/**
* @brief Retrieves a core.cfg configuration value.
*
* @param key Core.cfg key phrase.
* @return Value string, or NULL on failure.
* The string will be destroyed on core.cfg reparses.
*/
virtual const char *GetCoreConfigValue(const char *key) = 0;
}; };
} }