From b3c51c33bb785d5d09874a955ef0ce0e98d2d80c Mon Sep 17 00:00:00 2001 From: Rudi Date: Wed, 17 Dec 2014 22:24:17 +0100 Subject: [PATCH] Adding the command save_conf that allows to save current server configuration to a file --- src/engine/config.h | 2 ++ src/engine/server/server.cpp | 21 ++++++++++++++++++++ src/engine/server/server.h | 1 + src/engine/shared/config.cpp | 38 ++++++++++++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/engine/config.h b/src/engine/config.h index 35ef9fb9f..2e6355494 100644 --- a/src/engine/config.h +++ b/src/engine/config.h @@ -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; diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index c7b6bc9fa..e9656460d 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -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(); + 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); diff --git a/src/engine/server/server.h b/src/engine/server/server.h index bfab45c9c..359df2119 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -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); diff --git a/src/engine/shared/config.cpp b/src/engine/shared/config.cpp index d0cb7a6b6..8c50760c1 100644 --- a/src/engine/shared/config.cpp +++ b/src/engine/shared/config.cpp @@ -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; }