Adding the command save_conf that allows to save current server configuration to a file

This commit is contained in:
Rudi 2014-12-17 22:24:17 +01:00 committed by oy
parent d85937738a
commit b3c51c33bb
4 changed files with 60 additions and 2 deletions

View file

@ -4,6 +4,7 @@
#define ENGINE_CONFIG_H
#include "kernel.h"
#include "console.h"
class IConfig : public IInterface
{
@ -15,6 +16,7 @@ public:
virtual void Reset() = 0;
virtual void RestoreStrings() = 0;
virtual void Save() = 0;
virtual int SaveServerConfigs(const char *filename) = 0;
virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) = 0;

View file

@ -1500,6 +1500,25 @@ void CServer::ConMapReload(IConsole::IResult *pResult, void *pUser)
((CServer *)pUser)->m_MapReload = 1;
}
void CServer::ConSaveConfig(IConsole::IResult *pResult, void *pUser)
{
CServer *pThis = ((CServer *)pUser);
IConfig* currentConfig = pThis->Kernel()->RequestInterface<IConfig>();
const char *pFileName = pResult->GetString(0);
char aBuf[256];
if (currentConfig->SaveServerConfigs(pFileName) == 0)
{
str_format(aBuf, sizeof(aBuf), "saved server configuration to %s", pFileName);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
else
{
str_format(aBuf, sizeof(aBuf), "failed to save server configuration to %s", pFileName);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
}
void CServer::ConLogout(IConsole::IResult *pResult, void *pUser)
{
CServer *pServer = (CServer *)pUser;
@ -1594,6 +1613,8 @@ void CServer::RegisterCommands()
Console()->Register("reload", "", CFGFLAG_SERVER, ConMapReload, this, "Reload the map");
Console()->Register("save_conf", "s", CFGFLAG_SERVER, ConSaveConfig, this, "Save current configuration to file");
Console()->Chain("sv_name", ConchainSpecialInfoupdate, this);
Console()->Chain("password", ConchainSpecialInfoupdate, this);

View file

@ -228,6 +228,7 @@ public:
static void ConRecord(IConsole::IResult *pResult, void *pUser);
static void ConStopRecord(IConsole::IResult *pResult, void *pUser);
static void ConMapReload(IConsole::IResult *pResult, void *pUser);
static void ConSaveConfig(IConsole::IResult *pResult, void *pUser);
static void ConLogout(IConsole::IResult *pResult, void *pUser);
static void ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);

View file

@ -99,6 +99,32 @@ public:
m_ConfigFile = 0;
}
virtual int SaveServerConfigs(const char *filename)
{
if(!m_pStorage)
return -1;
IOHANDLE serverConfig = m_pStorage->OpenFile(filename, IOFLAG_WRITE, IStorage::TYPE_SAVE);
if (!serverConfig)
return -1;
char aLineBuf[1024*2];
char aEscapeBuf[1024*2];
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if((flags)&CFGFLAG_SERVER){ str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLineToConfigFile(aLineBuf, serverConfig); }
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if((flags)&CFGFLAG_SERVER){ EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLineToConfigFile(aLineBuf, serverConfig); }
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
io_close(serverConfig);
serverConfig = 0;
return 0;
}
virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData)
{
dbg_assert(m_NumCallbacks < MAX_CALLBACKS, "too many config callbacks");
@ -111,9 +137,17 @@ public:
{
if(!m_ConfigFile)
return;
io_write(m_ConfigFile, pLine, str_length(pLine));
io_write_newline(m_ConfigFile);
WriteLineToConfigFile(pLine, m_ConfigFile);
}
private:
void WriteLineToConfigFile(const char *pLine, const IOHANDLE hConfig)
{
io_write(hConfig, pLine, str_length(pLine));
io_write_newline(hConfig);
}
};
IConfig *CreateConfig() { return new CConfig; }