mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-12 19:18:20 +00:00
restructured CConfig
This commit is contained in:
parent
54625ebfbc
commit
de5859b371
|
@ -9,149 +9,126 @@
|
||||||
|
|
||||||
CConfiguration g_Config;
|
CConfiguration g_Config;
|
||||||
|
|
||||||
class CConfig : public IConfig
|
void EscapeParam(char *pDst, const char *pSrc, int size)
|
||||||
{
|
{
|
||||||
IStorage *m_pStorage;
|
for(int i = 0; *pSrc && i < size - 1; ++i)
|
||||||
IConsole *m_pConsole;
|
|
||||||
IOHANDLE m_ConfigFile;
|
|
||||||
int m_FlagMask;
|
|
||||||
|
|
||||||
struct CCallback
|
|
||||||
{
|
{
|
||||||
SAVECALLBACKFUNC m_pfnFunc;
|
if(*pSrc == '"' || *pSrc == '\\') // escape \ and "
|
||||||
void *m_pUserData;
|
*pDst++ = '\\';
|
||||||
};
|
*pDst++ = *pSrc++;
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
MAX_CALLBACKS = 16
|
|
||||||
};
|
|
||||||
|
|
||||||
CCallback m_aCallbacks[MAX_CALLBACKS];
|
|
||||||
int m_NumCallbacks;
|
|
||||||
|
|
||||||
void EscapeParam(char *pDst, const char *pSrc, int size)
|
|
||||||
{
|
|
||||||
for(int i = 0; *pSrc && i < size - 1; ++i)
|
|
||||||
{
|
|
||||||
if(*pSrc == '"' || *pSrc == '\\') // escape \ and "
|
|
||||||
*pDst++ = '\\';
|
|
||||||
*pDst++ = *pSrc++;
|
|
||||||
}
|
|
||||||
*pDst = 0;
|
|
||||||
}
|
}
|
||||||
|
*pDst = 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void Con_SaveConfig(IConsole::IResult *pResult, void *pUserData)
|
static void Con_SaveConfig(IConsole::IResult *pResult, void *pUserData)
|
||||||
|
{
|
||||||
|
char aFilename[128];
|
||||||
|
if(pResult->NumArguments())
|
||||||
|
str_format(aFilename, sizeof(aFilename), "configs/%s.cfg", pResult->GetString(0));
|
||||||
|
else
|
||||||
{
|
{
|
||||||
char aFilename[128];
|
char aDate[20];
|
||||||
if(pResult->NumArguments())
|
str_timestamp(aDate, sizeof(aDate));
|
||||||
str_format(aFilename, sizeof(aFilename), "configs/%s.cfg", pResult->GetString(0));
|
str_format(aFilename, sizeof(aFilename), "configs/config_%s.cfg", aDate);
|
||||||
else
|
|
||||||
{
|
|
||||||
char aDate[20];
|
|
||||||
str_timestamp(aDate, sizeof(aDate));
|
|
||||||
str_format(aFilename, sizeof(aFilename), "configs/config_%s.cfg", aDate);
|
|
||||||
}
|
|
||||||
((CConfig*)pUserData)->Save(aFilename);
|
|
||||||
}
|
}
|
||||||
|
((CConfig*)pUserData)->Save(aFilename);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
CConfig::CConfig()
|
||||||
|
{
|
||||||
|
m_pStorage = 0;
|
||||||
|
m_pConsole = 0;
|
||||||
|
m_ConfigFile = 0;
|
||||||
|
m_FlagMask = 0;
|
||||||
|
m_NumCallbacks = 0;
|
||||||
|
}
|
||||||
|
|
||||||
CConfig()
|
void CConfig::Init(int FlagMask)
|
||||||
{
|
{
|
||||||
m_pStorage = 0;
|
m_pStorage = Kernel()->RequestInterface<IStorage>();
|
||||||
m_pConsole = 0;
|
m_pConsole = Kernel()->RequestInterface<IConsole>();
|
||||||
m_ConfigFile = 0;
|
m_FlagMask = FlagMask;
|
||||||
m_FlagMask = 0;
|
Reset();
|
||||||
m_NumCallbacks = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void Init(int FlagMask)
|
if(m_pConsole)
|
||||||
{
|
m_pConsole->Register("save_config", "?s", CFGFLAG_SERVER|CFGFLAG_CLIENT|CFGFLAG_STORE, Con_SaveConfig, this, "Save config to file");
|
||||||
m_pStorage = Kernel()->RequestInterface<IStorage>();
|
}
|
||||||
m_pConsole = Kernel()->RequestInterface<IConsole>();
|
|
||||||
m_FlagMask = FlagMask;
|
|
||||||
Reset();
|
|
||||||
|
|
||||||
if(m_pConsole)
|
void CConfig::Reset()
|
||||||
m_pConsole->Register("save_config", "?s", CFGFLAG_SERVER|CFGFLAG_CLIENT|CFGFLAG_STORE, Con_SaveConfig, this, "Save config to file");
|
{
|
||||||
}
|
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) g_Config.m_##Name = def;
|
||||||
|
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) str_copy(g_Config.m_##Name, def, len);
|
||||||
|
|
||||||
virtual void Reset()
|
#include "config_variables.h"
|
||||||
{
|
|
||||||
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) g_Config.m_##Name = def;
|
|
||||||
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) str_copy(g_Config.m_##Name, def, len);
|
|
||||||
|
|
||||||
#include "config_variables.h"
|
#undef MACRO_CONFIG_INT
|
||||||
|
#undef MACRO_CONFIG_STR
|
||||||
|
}
|
||||||
|
|
||||||
#undef MACRO_CONFIG_INT
|
void CConfig::RestoreStrings()
|
||||||
#undef MACRO_CONFIG_STR
|
{
|
||||||
}
|
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) // nop
|
||||||
|
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if(!g_Config.m_##Name[0] && def[0]) str_copy(g_Config.m_##Name, def, len);
|
||||||
|
|
||||||
virtual void RestoreStrings()
|
#include "config_variables.h"
|
||||||
{
|
|
||||||
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) // nop
|
|
||||||
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if(!g_Config.m_##Name[0] && def[0]) str_copy(g_Config.m_##Name, def, len);
|
|
||||||
|
|
||||||
#include "config_variables.h"
|
#undef MACRO_CONFIG_INT
|
||||||
|
#undef MACRO_CONFIG_STR
|
||||||
|
}
|
||||||
|
|
||||||
#undef MACRO_CONFIG_INT
|
void CConfig::Save(const char *pFilename)
|
||||||
#undef MACRO_CONFIG_STR
|
{
|
||||||
}
|
if(!m_pStorage)
|
||||||
|
return;
|
||||||
virtual void Save(const char *pFilename)
|
|
||||||
{
|
|
||||||
if(!m_pStorage)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(!pFilename)
|
if(!pFilename)
|
||||||
pFilename = SETTINGS_FILENAME ".cfg";
|
pFilename = SETTINGS_FILENAME ".cfg";
|
||||||
m_ConfigFile = m_pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE);
|
m_ConfigFile = m_pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE);
|
||||||
|
|
||||||
if(!m_ConfigFile)
|
if(!m_ConfigFile)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
WriteLine("# Teeworlds " GAME_VERSION);
|
WriteLine("# Teeworlds " GAME_VERSION);
|
||||||
|
|
||||||
char aLineBuf[1024*2];
|
char aLineBuf[1024*2];
|
||||||
char aEscapeBuf[1024*2];
|
char aEscapeBuf[1024*2];
|
||||||
|
|
||||||
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if(((flags)&(CFGFLAG_SAVE))&&((flags)&(m_FlagMask))&&(g_Config.m_##Name!=int(def))){ str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); }
|
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if(((flags)&(CFGFLAG_SAVE))&&((flags)&(m_FlagMask))&&(g_Config.m_##Name!=int(def))){ str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); }
|
||||||
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if(((flags)&(CFGFLAG_SAVE))&&((flags)&(m_FlagMask)&&(str_comp(g_Config.m_##Name,def)))){ EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLine(aLineBuf); }
|
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if(((flags)&(CFGFLAG_SAVE))&&((flags)&(m_FlagMask)&&(str_comp(g_Config.m_##Name,def)))){ EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLine(aLineBuf); }
|
||||||
|
|
||||||
#include "config_variables.h"
|
#include "config_variables.h"
|
||||||
|
|
||||||
#undef MACRO_CONFIG_INT
|
#undef MACRO_CONFIG_INT
|
||||||
#undef MACRO_CONFIG_STR
|
#undef MACRO_CONFIG_STR
|
||||||
|
|
||||||
for(int i = 0; i < m_NumCallbacks; i++)
|
for(int i = 0; i < m_NumCallbacks; i++)
|
||||||
m_aCallbacks[i].m_pfnFunc(this, m_aCallbacks[i].m_pUserData);
|
m_aCallbacks[i].m_pfnFunc(this, m_aCallbacks[i].m_pUserData);
|
||||||
|
|
||||||
io_close(m_ConfigFile);
|
io_close(m_ConfigFile);
|
||||||
|
|
||||||
if(m_pConsole)
|
if(m_pConsole)
|
||||||
{
|
|
||||||
char aBuf[256];
|
|
||||||
str_format(aBuf, sizeof(aBuf), "saved config to '%s'", pFilename);
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "config", aBuf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData)
|
|
||||||
{
|
{
|
||||||
dbg_assert(m_NumCallbacks < MAX_CALLBACKS, "too many config callbacks");
|
char aBuf[256];
|
||||||
m_aCallbacks[m_NumCallbacks].m_pfnFunc = pfnFunc;
|
str_format(aBuf, sizeof(aBuf), "saved config to '%s'", pFilename);
|
||||||
m_aCallbacks[m_NumCallbacks].m_pUserData = pUserData;
|
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "config", aBuf);
|
||||||
m_NumCallbacks++;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
virtual void WriteLine(const char *pLine)
|
void CConfig::RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData)
|
||||||
{
|
{
|
||||||
if(!m_ConfigFile)
|
dbg_assert(m_NumCallbacks < MAX_CALLBACKS, "too many config callbacks");
|
||||||
return;
|
m_aCallbacks[m_NumCallbacks].m_pfnFunc = pfnFunc;
|
||||||
io_write(m_ConfigFile, pLine, str_length(pLine));
|
m_aCallbacks[m_NumCallbacks].m_pUserData = pUserData;
|
||||||
io_write_newline(m_ConfigFile);
|
m_NumCallbacks++;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
void CConfig::WriteLine(const char *pLine)
|
||||||
|
{
|
||||||
|
if(!m_ConfigFile)
|
||||||
|
return;
|
||||||
|
|
||||||
|
io_write(m_ConfigFile, pLine, str_length(pLine));
|
||||||
|
io_write_newline(m_ConfigFile);
|
||||||
|
}
|
||||||
|
|
||||||
IConfig *CreateConfig() { return new CConfig; }
|
IConfig *CreateConfig() { return new CConfig; }
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
#ifndef ENGINE_SHARED_CONFIG_H
|
#ifndef ENGINE_SHARED_CONFIG_H
|
||||||
#define ENGINE_SHARED_CONFIG_H
|
#define ENGINE_SHARED_CONFIG_H
|
||||||
|
|
||||||
|
#include <engine/config.h>
|
||||||
|
|
||||||
struct CConfiguration
|
struct CConfiguration
|
||||||
{
|
{
|
||||||
#define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) int m_##Name;
|
#define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) int m_##Name;
|
||||||
|
@ -25,4 +27,37 @@ enum
|
||||||
CFGFLAG_BASICACCESS=64,
|
CFGFLAG_BASICACCESS=64,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CConfig : public IConfig
|
||||||
|
{
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
MAX_CALLBACKS = 16
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CCallback
|
||||||
|
{
|
||||||
|
SAVECALLBACKFUNC m_pfnFunc;
|
||||||
|
void *m_pUserData;
|
||||||
|
};
|
||||||
|
|
||||||
|
class IStorage *m_pStorage;
|
||||||
|
class IConsole *m_pConsole;
|
||||||
|
IOHANDLE m_ConfigFile;
|
||||||
|
int m_FlagMask;
|
||||||
|
CCallback m_aCallbacks[MAX_CALLBACKS];
|
||||||
|
int m_NumCallbacks;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CConfig();
|
||||||
|
|
||||||
|
virtual void Init(int FlagMask);
|
||||||
|
virtual void Reset();
|
||||||
|
virtual void RestoreStrings();
|
||||||
|
virtual void Save(const char *pFilename);
|
||||||
|
|
||||||
|
virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData);
|
||||||
|
|
||||||
|
virtual void WriteLine(const char *pLine);
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue