2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/config.h>
|
|
|
|
#include <engine/storage.h>
|
|
|
|
#include <engine/shared/config.h>
|
2014-05-12 21:57:42 +00:00
|
|
|
#include <engine/shared/protocol.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
CConfiguration g_Config;
|
|
|
|
|
|
|
|
class CConfig : public IConfig
|
|
|
|
{
|
|
|
|
IStorage *m_pStorage;
|
|
|
|
IOHANDLE m_ConfigFile;
|
2018-04-05 15:24:49 +00:00
|
|
|
bool m_Failed;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
struct CCallback
|
|
|
|
{
|
|
|
|
SAVECALLBACKFUNC m_pfnFunc;
|
|
|
|
void *m_pUserData;
|
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAX_CALLBACKS = 16
|
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CCallback m_aCallbacks[MAX_CALLBACKS];
|
|
|
|
int m_NumCallbacks;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-07-08 11:06:03 +00:00
|
|
|
void EscapeParam(char *pDst, const char *pSrc, int Size)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2017-07-08 11:06:03 +00:00
|
|
|
str_escape(&pDst, pSrc, pDst + Size);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
CConfig()
|
|
|
|
{
|
|
|
|
m_ConfigFile = 0;
|
|
|
|
m_NumCallbacks = 0;
|
2018-04-05 15:24:49 +00:00
|
|
|
m_Failed = false;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Init()
|
|
|
|
{
|
|
|
|
m_pStorage = Kernel()->RequestInterface<IStorage>();
|
|
|
|
Reset();
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Reset()
|
|
|
|
{
|
|
|
|
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) g_Config.m_##Name = def;
|
2019-05-15 16:05:19 +00:00
|
|
|
#define MACRO_CONFIG_COL(Name,ScriptName,def,flags,desc) MACRO_CONFIG_INT(Name,ScriptName,def,0,0,flags,desc)
|
2010-05-29 07:25:38 +00:00
|
|
|
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) str_copy(g_Config.m_##Name, def, len);
|
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
#include "config_variables.h"
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
#undef MACRO_CONFIG_INT
|
2019-04-25 11:48:53 +00:00
|
|
|
#undef MACRO_CONFIG_COL
|
2011-04-13 18:37:12 +00:00
|
|
|
#undef MACRO_CONFIG_STR
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2010-08-10 22:31:42 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Save()
|
|
|
|
{
|
2015-10-20 22:54:10 +00:00
|
|
|
if(!m_pStorage || !g_Config.m_ClSaveSettings)
|
2010-05-29 07:25:38 +00:00
|
|
|
return;
|
2018-04-05 15:24:49 +00:00
|
|
|
|
|
|
|
m_ConfigFile = m_pStorage->OpenFile(CONFIG_FILE_TMP, IOFLAG_WRITE, IStorage::TYPE_SAVE);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_ConfigFile)
|
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-04-05 15:24:49 +00:00
|
|
|
m_Failed = false;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
char aLineBuf[1024*2];
|
|
|
|
char aEscapeBuf[1024*2];
|
|
|
|
|
2017-03-04 14:43:49 +00:00
|
|
|
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if((flags)&CFGFLAG_SAVE) { str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); }
|
2019-05-15 16:05:19 +00:00
|
|
|
#define MACRO_CONFIG_COL(Name,ScriptName,def,flags,desc) if((flags)&CFGFLAG_SAVE) { str_format(aLineBuf, sizeof(aLineBuf), "%s %u", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); }
|
2017-03-04 14:43:49 +00:00
|
|
|
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if((flags)&CFGFLAG_SAVE) { EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLine(aLineBuf); }
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
#include "config_variables.h"
|
|
|
|
|
|
|
|
#undef MACRO_CONFIG_INT
|
2019-04-25 11:48:53 +00:00
|
|
|
#undef MACRO_CONFIG_COL
|
2011-04-13 18:37:12 +00:00
|
|
|
#undef MACRO_CONFIG_STR
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < m_NumCallbacks; i++)
|
|
|
|
m_aCallbacks[i].m_pfnFunc(this, m_aCallbacks[i].m_pUserData);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-04-05 15:24:49 +00:00
|
|
|
if(io_close(m_ConfigFile) != 0)
|
|
|
|
m_Failed = true;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_ConfigFile = 0;
|
2018-04-05 15:24:49 +00:00
|
|
|
|
|
|
|
if(m_Failed)
|
|
|
|
{
|
|
|
|
dbg_msg("config", "ERROR: writing to " CONFIG_FILE_TMP " failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_pStorage->RenameFile(CONFIG_FILE_TMP, CONFIG_FILE, IStorage::TYPE_SAVE))
|
|
|
|
dbg_msg("config", "ERROR: renaming " CONFIG_FILE_TMP " to " CONFIG_FILE " failed");
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData)
|
|
|
|
{
|
|
|
|
dbg_assert(m_NumCallbacks < MAX_CALLBACKS, "too many config callbacks");
|
|
|
|
m_aCallbacks[m_NumCallbacks].m_pfnFunc = pfnFunc;
|
|
|
|
m_aCallbacks[m_NumCallbacks].m_pUserData = pUserData;
|
|
|
|
m_NumCallbacks++;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void WriteLine(const char *pLine)
|
|
|
|
{
|
2018-04-05 15:24:49 +00:00
|
|
|
if(!m_ConfigFile ||
|
|
|
|
io_write(m_ConfigFile, pLine, str_length(pLine)) != static_cast<unsigned>(str_length(pLine)) ||
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
io_write_newline(m_ConfigFile) != 2)
|
|
|
|
#else
|
|
|
|
io_write_newline(m_ConfigFile) != 1)
|
|
|
|
#endif
|
|
|
|
m_Failed = true;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IConfig *CreateConfig() { return new CConfig; }
|