From c2f276cee16172b76128b4c1d0d18f17bbaac57b Mon Sep 17 00:00:00 2001 From: Alexander Akulich Date: Sun, 10 Jan 2021 15:47:07 +0300 Subject: [PATCH] Port CConfig API from the upstream (0.7.5) The old (g_Config) API is kept to not break the stuff. See commits: de5859b3717406f9f0e728506ad0ef15d4e87337 78076761ebaecb3b60e1ff733d30bed23028d77a --- src/engine/client/client.cpp | 13 +- src/engine/client/client.h | 4 + src/engine/client/friends.cpp | 10 +- src/engine/client/friends.h | 2 +- src/engine/client/serverbrowser.cpp | 10 +- src/engine/client/serverbrowser.h | 2 +- src/engine/config.h | 7 +- src/engine/server/register.cpp | 3 +- src/engine/server/register.h | 3 +- src/engine/server/server.cpp | 16 +- src/engine/server/server.h | 4 +- src/engine/shared/config.cpp | 161 ++++++++---------- src/engine/shared/config.h | 38 ++++- src/engine/shared/console.cpp | 1 + src/engine/shared/console.h | 1 + src/engine/shared/econ.cpp | 3 +- src/engine/shared/econ.h | 5 +- src/game/client/component.h | 1 + src/game/client/components/binds.cpp | 12 +- src/game/client/components/binds.h | 2 +- src/game/client/components/menus_settings.cpp | 2 - src/game/client/gameclient.h | 2 + src/game/editor/editor.cpp | 1 + src/game/editor/editor.h | 2 + src/game/server/entity.h | 1 + src/game/server/gamecontext.cpp | 2 + src/game/server/gamecontext.h | 3 + src/game/server/gamecontroller.cpp | 1 + src/game/server/gamecontroller.h | 2 + src/game/server/gameworld.cpp | 2 + src/game/server/gameworld.h | 2 + src/game/server/teehistorian.h | 4 +- src/mastersrv/mastersrv.cpp | 6 +- src/test/teehistorian.cpp | 2 +- 34 files changed, 192 insertions(+), 138 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index e1c846448..d70c16702 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -272,6 +272,8 @@ CClient::CClient() : m_pSound = 0; m_pGameClient = 0; m_pMap = 0; + m_pConfigManager = 0; + m_pConfig = 0; m_pConsole = 0; m_RenderFrameTime = 0.0001f; @@ -2961,6 +2963,8 @@ void CClient::InitInterfaces() m_pInput = Kernel()->RequestInterface(); m_pMap = Kernel()->RequestInterface(); m_pMasterServer = Kernel()->RequestInterface(); + m_pConfigManager = Kernel()->RequestInterface(); + m_pConfig = m_pConfigManager->Values(); #if defined(CONF_AUTOUPDATE) m_pUpdater = Kernel()->RequestInterface(); #endif @@ -3338,8 +3342,7 @@ void CClient::Run() if(!s_SavedConfig) { // write down the config and quit - IConfig *pConfig = Kernel()->RequestInterface(); - if(!pConfig->Save()) + if(!m_pConfigManager->Save()) m_Warnings.emplace_back(SWarning(Localize("Saving ddnet-settings.cfg failed"))); s_SavedConfig = true; } @@ -4265,7 +4268,7 @@ int main(int argc, const char **argv) // ignore_convention IEngine *pEngine = CreateEngine("DDNet", Silent, 1); IConsole *pConsole = CreateConsole(CFGFLAG_CLIENT); IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_CLIENT, argc, argv); // ignore_convention - IConfig *pConfig = CreateConfig(); + IConfigManager *pConfigManager = CreateConfigManager(); IEngineSound *pEngineSound = CreateEngineSound(); IEngineInput *pEngineInput = CreateEngineInput(); IEngineTextRender *pEngineTextRender = CreateEngineTextRender(); @@ -4284,7 +4287,7 @@ int main(int argc, const char **argv) // ignore_convention RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngine); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig); + RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfigManager); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineSound); // IEngineSound RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineSound), false); @@ -4316,7 +4319,7 @@ int main(int argc, const char **argv) // ignore_convention } pEngine->Init(); - pConfig->Init(); + pConfigManager->Init(); pConsole->Init(); pEngineMasterServer->Init(); pEngineMasterServer->Load(); diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 53ef17668..dae4c1131 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -90,6 +90,8 @@ class CClient : public IClient, public CDemoPlayer::IListener IEngineSound *m_pSound; IGameClient *m_pGameClient; IEngineMap *m_pMap; + IConfigManager *m_pConfigManager; + CConfig *m_pConfig; IConsole *m_pConsole; IStorage *m_pStorage; IUpdater *m_pUpdater; @@ -274,6 +276,8 @@ public: IEngineSound *Sound() { return m_pSound; } IGameClient *GameClient() { return m_pGameClient; } IEngineMasterServer *MasterServer() { return m_pMasterServer; } + IConfigManager *ConfigManager() { return m_pConfigManager; } + CConfig *Config() { return m_pConfig; } IStorage *Storage() { return m_pStorage; } IUpdater *Updater() { return m_pUpdater; } ISteam *Steam() { return m_pSteam; } diff --git a/src/engine/client/friends.cpp b/src/engine/client/friends.cpp index ca07aee11..aed61520a 100644 --- a/src/engine/client/friends.cpp +++ b/src/engine/client/friends.cpp @@ -38,9 +38,9 @@ void CFriends::Init(bool Foes) { m_Foes = Foes; - IConfig *pConfig = Kernel()->RequestInterface(); - if(pConfig) - pConfig->RegisterCallback(ConfigSaveCallback, this); + IConfigManager *pConfigManager = Kernel()->RequestInterface(); + if(pConfigManager) + pConfigManager->RegisterCallback(ConfigSaveCallback, this); IConsole *pConsole = Kernel()->RequestInterface(); if(pConsole) @@ -158,7 +158,7 @@ void CFriends::Friends() } } -void CFriends::ConfigSaveCallback(IConfig *pConfig, void *pUserData) +void CFriends::ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData) { CFriends *pSelf = (CFriends *)pUserData; char aBuf[128]; @@ -175,6 +175,6 @@ void CFriends::ConfigSaveCallback(IConfig *pConfig, void *pUserData) str_escape(&pDst, pSelf->m_aFriends[i].m_aClan, pEnd); str_append(aBuf, "\"", sizeof(aBuf)); - pConfig->WriteLine(aBuf); + pConfigManager->WriteLine(aBuf); } } diff --git a/src/engine/client/friends.h b/src/engine/client/friends.h index 8e98c9b49..983137862 100644 --- a/src/engine/client/friends.h +++ b/src/engine/client/friends.h @@ -17,7 +17,7 @@ class CFriends : public IFriends static void ConRemoveFriend(IConsole::IResult *pResult, void *pUserData); static void ConFriends(IConsole::IResult *pResult, void *pUserData); - static void ConfigSaveCallback(IConfig *pConfig, void *pUserData); + static void ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData); public: CFriends(); diff --git a/src/engine/client/serverbrowser.cpp b/src/engine/client/serverbrowser.cpp index b24b25c9d..a7e992118 100644 --- a/src/engine/client/serverbrowser.cpp +++ b/src/engine/client/serverbrowser.cpp @@ -89,9 +89,9 @@ void CServerBrowser::SetBaseInfo(class CNetClient *pClient, const char *pNetVers m_pMasterServer = Kernel()->RequestInterface(); m_pConsole = Kernel()->RequestInterface(); m_pFriends = Kernel()->RequestInterface(); - IConfig *pConfig = Kernel()->RequestInterface(); - if(pConfig) - pConfig->RegisterCallback(ConfigSaveCallback, this); + IConfigManager *pConfigManager = Kernel()->RequestInterface(); + if(pConfigManager) + pConfigManager->RegisterCallback(ConfigSaveCallback, this); } const CServerInfo *CServerBrowser::SortedGet(int Index) const @@ -1264,7 +1264,7 @@ int CServerBrowser::LoadingProgression() const return 100.0f * Loaded / Servers; } -void CServerBrowser::ConfigSaveCallback(IConfig *pConfig, void *pUserData) +void CServerBrowser::ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData) { CServerBrowser *pSelf = (CServerBrowser *)pUserData; @@ -1274,7 +1274,7 @@ void CServerBrowser::ConfigSaveCallback(IConfig *pConfig, void *pUserData) { net_addr_str(&pSelf->m_aFavoriteServers[i], aAddrStr, sizeof(aAddrStr), true); str_format(aBuffer, sizeof(aBuffer), "add_favorite %s", aAddrStr); - pConfig->WriteLine(aBuffer); + pConfigManager->WriteLine(aBuffer); } } diff --git a/src/engine/client/serverbrowser.h b/src/engine/client/serverbrowser.h index 97e503300..9409dc22f 100644 --- a/src/engine/client/serverbrowser.h +++ b/src/engine/client/serverbrowser.h @@ -206,7 +206,7 @@ private: void SetInfo(CServerEntry *pEntry, const CServerInfo &Info); - static void ConfigSaveCallback(IConfig *pConfig, void *pUserData); + static void ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData); }; #endif diff --git a/src/engine/config.h b/src/engine/config.h index 087626c8f..ab6d1a176 100644 --- a/src/engine/config.h +++ b/src/engine/config.h @@ -5,21 +5,22 @@ #include "kernel.h" -class IConfig : public IInterface +class IConfigManager : public IInterface { MACRO_INTERFACE("config", 0) public: - typedef void (*SAVECALLBACKFUNC)(IConfig *pConfig, void *pUserData); + typedef void (*SAVECALLBACKFUNC)(IConfigManager *pConfig, void *pUserData); virtual void Init() = 0; virtual void Reset() = 0; virtual bool Save() = 0; + virtual class CConfig *Values() = 0; virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) = 0; virtual void WriteLine(const char *pLine) = 0; }; -extern IConfig *CreateConfig(); +extern IConfigManager *CreateConfigManager(); #endif diff --git a/src/engine/server/register.cpp b/src/engine/server/register.cpp index 90fdde17b..63b8b5d67 100644 --- a/src/engine/server/register.cpp +++ b/src/engine/server/register.cpp @@ -118,10 +118,11 @@ void CRegister::RegisterGotCount(CNetChunk *pChunk) } } -void CRegister::Init(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, IConsole *pConsole) +void CRegister::Init(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole) { m_pNetServer = pNetServer; m_pMasterServer = pMasterServer; + m_pConfig = pConfig; m_pConsole = pConsole; } diff --git a/src/engine/server/register.h b/src/engine/server/register.h index 6dfed8c33..36583f3c7 100644 --- a/src/engine/server/register.h +++ b/src/engine/server/register.h @@ -29,6 +29,7 @@ class CRegister class CNetServer *m_pNetServer; class IEngineMasterServer *m_pMasterServer; + class CConfig *m_pConfig; class IConsole *m_pConsole; bool m_Sixup; @@ -51,7 +52,7 @@ class CRegister public: CRegister(bool Sixup); - void Init(class CNetServer *pNetServer, class IEngineMasterServer *pMasterServer, class IConsole *pConsole); + void Init(class CNetServer *pNetServer, class IEngineMasterServer *pMasterServer, class CConfig *pConfig, class IConsole *pConsole); void RegisterUpdate(int Nettype); int RegisterProcessPacket(struct CNetChunk *pPacket, SECURITY_TOKEN ResponseToken = 0); void FeedToken(NETADDR Addr, SECURITY_TOKEN ResponseToken); diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 5984a73c3..8a0fef07c 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -2335,10 +2335,10 @@ int CServer::LoadMap(const char *pMapName) return 1; } -void CServer::InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, IConsole *pConsole) +void CServer::InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole) { - m_Register.Init(pNetServer, pMasterServer, pConsole); - m_RegSixup.Init(pNetServer, pMasterServer, pConsole); + m_Register.Init(pNetServer, pMasterServer, pConfig, pConsole); + m_RegSixup.Init(pNetServer, pMasterServer, pConfig, pConsole); } int CServer::Run() @@ -2407,7 +2407,7 @@ int CServer::Run() m_NetServer.SetCallbacks(NewClientCallback, NewClientNoAuthCallback, ClientRejoinCallback, DelClientCallback, this); - m_Econ.Init(Console(), &m_ServerBan); + m_Econ.Init(Config(), Console(), &m_ServerBan); #if defined(CONF_FAMILY_UNIX) m_Fifo.Init(Console(), g_Config.m_SvInputFifo, CFGFLAG_SERVER); @@ -3495,10 +3495,10 @@ int main(int argc, const char **argv) // ignore_convention IConsole *pConsole = CreateConsole(CFGFLAG_SERVER | CFGFLAG_ECON); IEngineMasterServer *pEngineMasterServer = CreateEngineMasterServer(); IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_SERVER, argc, argv); // ignore_convention - IConfig *pConfig = CreateConfig(); + IConfigManager *pConfigManager = CreateConfigManager(); IEngineAntibot *pEngineAntibot = CreateEngineAntibot(); - pServer->InitRegister(&pServer->m_NetServer, pEngineMasterServer, pConsole); + pServer->InitRegister(&pServer->m_NetServer, pEngineMasterServer, pConfigManager->Values(), pConsole); { bool RegisterFail = false; @@ -3510,7 +3510,7 @@ int main(int argc, const char **argv) // ignore_convention RegisterFail = RegisterFail || !pKernel->RegisterInterface(pGameServer); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig); + RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfigManager); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineMasterServer); // register as both RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineMasterServer), false); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineAntibot); @@ -3524,7 +3524,7 @@ int main(int argc, const char **argv) // ignore_convention } pEngine->Init(); - pConfig->Init(); + pConfigManager->Init(); pConsole->Init(); pEngineMasterServer->Init(); pEngineMasterServer->Load(); diff --git a/src/engine/server/server.h b/src/engine/server/server.h index 02ed93a4e..61052e302 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -89,6 +89,7 @@ public: class CServer : public IServer { class IGameServer *m_pGameServer; + class CConfig *m_pConfig; class IConsole *m_pConsole; class IStorage *m_pStorage; class IEngineAntibot *m_pAntibot; @@ -107,6 +108,7 @@ class CServer : public IServer public: class IGameServer *GameServer() { return m_pGameServer; } + class CConfig *Config() { return m_pConfig; } class IConsole *Console() { return m_pConsole; } class IStorage *Storage() { return m_pStorage; } class IEngineAntibot *Antibot() { return m_pAntibot; } @@ -366,7 +368,7 @@ public: void StopRecord(int ClientID); bool IsRecording(int ClientID); - void InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, IConsole *pConsole); + void InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole); int Run(); static void ConTestingCommands(IConsole::IResult *pResult, void *pUser); diff --git a/src/engine/shared/config.cpp b/src/engine/shared/config.cpp index 7de9cbe8f..bc56c1f2e 100644 --- a/src/engine/shared/config.cpp +++ b/src/engine/shared/config.cpp @@ -5,49 +5,35 @@ #include #include -CConfiguration g_Config; +CConfig g_Config; -class CConfig : public IConfig +void EscapeParam(char *pDst, const char *pSrc, int size) { - IStorage *m_pStorage; - IOHANDLE m_ConfigFile; - bool m_Failed; - - struct CCallback + for(int i = 0; *pSrc && i < size - 1; ++i) { - SAVECALLBACKFUNC m_pfnFunc; - void *m_pUserData; - }; - - enum - { - MAX_CALLBACKS = 16 - }; - - CCallback m_aCallbacks[MAX_CALLBACKS]; - int m_NumCallbacks; - - void EscapeParam(char *pDst, const char *pSrc, int Size) - { - str_escape(&pDst, pSrc, pDst + Size); + if(*pSrc == '"' || *pSrc == '\\') // escape \ and " + *pDst++ = '\\'; + *pDst++ = *pSrc++; } + *pDst = 0; +} -public: - CConfig() - { - m_ConfigFile = 0; - m_NumCallbacks = 0; - m_Failed = false; - } +CConfigManager::CConfigManager() +{ + m_pStorage = 0; + m_ConfigFile = 0; + m_NumCallbacks = 0; + m_Failed = false; +} - virtual void Init() - { - m_pStorage = Kernel()->RequestInterface(); - Reset(); - } +void CConfigManager::Init() +{ + m_pStorage = Kernel()->RequestInterface(); + Reset(); +} - virtual void Reset() - { +void CConfigManager::Reset() +{ #define MACRO_CONFIG_INT(Name, ScriptName, def, min, max, flags, desc) g_Config.m_##Name = def; #define MACRO_CONFIG_COL(Name, ScriptName, def, flags, desc) MACRO_CONFIG_INT(Name, ScriptName, def, 0, 0, flags, desc) #define MACRO_CONFIG_STR(Name, ScriptName, len, def, flags, desc) str_copy(g_Config.m_##Name, def, len); @@ -57,28 +43,28 @@ public: #undef MACRO_CONFIG_INT #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR +} + +bool CConfigManager::Save() +{ + if(!m_pStorage || !g_Config.m_ClSaveSettings) + return true; + + char aConfigFileTmp[64]; + str_format(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE ".%d.tmp", pid()); + + m_ConfigFile = m_pStorage->OpenFile(aConfigFileTmp, IOFLAG_WRITE, IStorage::TYPE_SAVE); + + if(!m_ConfigFile) + { + dbg_msg("config", "ERROR: opening %s failed", aConfigFileTmp); + return false; } - virtual bool Save() - { - if(!m_pStorage || !g_Config.m_ClSaveSettings) - return true; + m_Failed = false; - char aConfigFileTmp[64]; - str_format(aConfigFileTmp, sizeof(aConfigFileTmp), CONFIG_FILE ".%d.tmp", pid()); - - m_ConfigFile = m_pStorage->OpenFile(aConfigFileTmp, IOFLAG_WRITE, IStorage::TYPE_SAVE); - - if(!m_ConfigFile) - { - dbg_msg("config", "ERROR: opening %s failed", aConfigFileTmp); - return false; - } - - m_Failed = false; - - char aLineBuf[1024 * 2]; - char aEscapeBuf[1024 * 2]; + char aLineBuf[1024 * 2]; + char aEscapeBuf[1024 * 2]; #define MACRO_CONFIG_INT(Name, ScriptName, def, min, max, flags, desc) \ if((flags)&CFGFLAG_SAVE && g_Config.m_##Name != def) \ @@ -106,48 +92,47 @@ public: #undef MACRO_CONFIG_COL #undef MACRO_CONFIG_STR - for(int i = 0; i < m_NumCallbacks; i++) - m_aCallbacks[i].m_pfnFunc(this, m_aCallbacks[i].m_pUserData); + for(int i = 0; i < m_NumCallbacks; i++) + m_aCallbacks[i].m_pfnFunc(this, m_aCallbacks[i].m_pUserData); - if(io_close(m_ConfigFile) != 0) - m_Failed = true; + if(io_close(m_ConfigFile) != 0) + m_Failed = true; - m_ConfigFile = 0; + m_ConfigFile = 0; - if(m_Failed) - { - dbg_msg("config", "ERROR: writing to %s failed", aConfigFileTmp); - return false; - } - - if(!m_pStorage->RenameFile(aConfigFileTmp, CONFIG_FILE, IStorage::TYPE_SAVE)) - { - dbg_msg("config", "ERROR: renaming %s to " CONFIG_FILE " failed", aConfigFileTmp); - return false; - } - - return true; + if(m_Failed) + { + dbg_msg("config", "ERROR: writing to %s failed", aConfigFileTmp); + return false; } - virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData) + if(!m_pStorage->RenameFile(aConfigFileTmp, CONFIG_FILE, IStorage::TYPE_SAVE)) { - 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++; + dbg_msg("config", "ERROR: renaming %s to " CONFIG_FILE " failed", aConfigFileTmp); + return false; } - virtual void WriteLine(const char *pLine) - { - if(!m_ConfigFile || - io_write(m_ConfigFile, pLine, str_length(pLine)) != static_cast(str_length(pLine)) || + return true; +} + +void CConfigManager::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++; +} + +void CConfigManager::WriteLine(const char *pLine) +{ + if(!m_ConfigFile || + io_write(m_ConfigFile, pLine, str_length(pLine)) != static_cast(str_length(pLine)) || #if defined(CONF_FAMILY_WINDOWS) - io_write_newline(m_ConfigFile) != 2) + io_write_newline(m_ConfigFile) != 2) #else - io_write_newline(m_ConfigFile) != 1) + io_write_newline(m_ConfigFile) != 1) #endif - m_Failed = true; - } -}; + m_Failed = true; +} -IConfig *CreateConfig() { return new CConfig; } +IConfigManager *CreateConfigManager() { return new CConfigManager; } diff --git a/src/engine/shared/config.h b/src/engine/shared/config.h index f5c3c5b4f..ef5555926 100644 --- a/src/engine/shared/config.h +++ b/src/engine/shared/config.h @@ -4,14 +4,16 @@ #define ENGINE_SHARED_CONFIG_H #include +#include #define CONFIG_FILE "settings_ddnet.cfg" #define AUTOEXEC_FILE "autoexec.cfg" #define AUTOEXEC_CLIENT_FILE "autoexec_client.cfg" #define AUTOEXEC_SERVER_FILE "autoexec_server.cfg" -struct CConfiguration +class CConfig { +public: #define MACRO_CONFIG_INT(Name, ScriptName, Def, Min, Max, Save, Desc) int m_##Name; #define MACRO_CONFIG_COL(Name, ScriptName, Def, Save, Desc) unsigned m_##Name; #define MACRO_CONFIG_STR(Name, ScriptName, Len, Def, Save, Desc) char m_##Name[Len]; // Flawfinder: ignore @@ -21,7 +23,7 @@ struct CConfiguration #undef MACRO_CONFIG_STR }; -extern CConfiguration g_Config; +extern CConfig g_Config; enum { @@ -41,4 +43,36 @@ enum CFGFLAG_COLALPHA = 1 << 11, }; +class CConfigManager : public IConfigManager +{ + enum + { + MAX_CALLBACKS = 16 + }; + + struct CCallback + { + SAVECALLBACKFUNC m_pfnFunc; + void *m_pUserData; + }; + + class IStorage *m_pStorage; + IOHANDLE m_ConfigFile; + bool m_Failed; + CCallback m_aCallbacks[MAX_CALLBACKS]; + int m_NumCallbacks; + +public: + CConfigManager(); + + virtual void Init(); + virtual void Reset(); + virtual bool Save(); + virtual CConfig *Values() { return &g_Config; } + + virtual void RegisterCallback(SAVECALLBACKFUNC pfnFunc, void *pUserData); + + virtual void WriteLine(const char *pLine); +}; + #endif diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 45aaac6ec..949c7923d 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -971,6 +971,7 @@ CConsole::~CConsole() void CConsole::Init() { + m_pConfig = Kernel()->RequestInterface()->Values(); m_pStorage = Kernel()->RequestInterface(); // TODO: this should disappear diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 045409d98..7cf26724c 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -46,6 +46,7 @@ class CConsole : public IConsole }; CExecFile *m_pFirstExec; + class CConfig *m_pConfig; class IStorage *m_pStorage; int m_AccessLevel; diff --git a/src/engine/shared/econ.cpp b/src/engine/shared/econ.cpp index f48f4499b..2a7412d40 100644 --- a/src/engine/shared/econ.cpp +++ b/src/engine/shared/econ.cpp @@ -59,8 +59,9 @@ void CEcon::ConLogout(IConsole::IResult *pResult, void *pUserData) pThis->m_NetConsole.Drop(pThis->m_UserClientID, "Logout"); } -void CEcon::Init(IConsole *pConsole, CNetBan *pNetBan) +void CEcon::Init(CConfig *pConfig, IConsole *pConsole, CNetBan *pNetBan) { + m_pConfig = pConfig; m_pConsole = pConsole; for(auto &Client : m_aClients) diff --git a/src/engine/shared/econ.h b/src/engine/shared/econ.h index a7f82a984..f45aba38a 100644 --- a/src/engine/shared/econ.h +++ b/src/engine/shared/econ.h @@ -5,6 +5,8 @@ #include +class CConfig; + class CEcon { enum @@ -28,6 +30,7 @@ class CEcon }; CClient m_aClients[NET_MAX_CONSOLE_CLIENTS]; + CConfig *m_pConfig; IConsole *m_pConsole; CNetConsole m_NetConsole; @@ -45,7 +48,7 @@ class CEcon public: IConsole *Console() { return m_pConsole; } - void Init(IConsole *pConsole, class CNetBan *pNetBan); + void Init(CConfig *pConfig, IConsole *pConsole, class CNetBan *pNetBan); void Update(); void Send(int ClientID, const char *pLine); void Shutdown(); diff --git a/src/game/client/component.h b/src/game/client/component.h index 89ad7dd42..f9f60ebe5 100644 --- a/src/game/client/component.h +++ b/src/game/client/component.h @@ -26,6 +26,7 @@ protected: class CUI *UI() const { return m_pClient->UI(); } class ISound *Sound() const { return m_pClient->Sound(); } class CRenderTools *RenderTools() const { return m_pClient->RenderTools(); } + class CConfig *Config() const { return m_pClient->Config(); } class IConsole *Console() const { return m_pClient->Console(); } class IDemoPlayer *DemoPlayer() const { return m_pClient->DemoPlayer(); } class IDemoRecorder *DemoRecorder(int Recorder) const { return m_pClient->DemoRecorder(Recorder); } diff --git a/src/game/client/components/binds.cpp b/src/game/client/components/binds.cpp index 724482997..e1ded9e76 100644 --- a/src/game/client/components/binds.cpp +++ b/src/game/client/components/binds.cpp @@ -250,9 +250,9 @@ void CBinds::SetDefaults() void CBinds::OnConsoleInit() { // bindings - IConfig *pConfig = Kernel()->RequestInterface(); - if(pConfig) - pConfig->RegisterCallback(ConfigSaveCallback, this); + IConfigManager *pConfigManager = Kernel()->RequestInterface(); + if(pConfigManager) + pConfigManager->RegisterCallback(ConfigSaveCallback, this); Console()->Register("bind", "s[key] r[command]", CFGFLAG_CLIENT, ConBind, this, "Bind key to execute the command"); Console()->Register("dump_binds", "?s[key]", CFGFLAG_CLIENT, ConDumpBinds, this, "Print command executed by this keybindind or all binds"); @@ -423,13 +423,13 @@ const char *CBinds::GetKeyBindModifiersName(int Modifier) return aModifier; } -void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData) +void CBinds::ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData) { CBinds *pSelf = (CBinds *)pUserData; char aBuffer[256]; char *pEnd = aBuffer + sizeof(aBuffer); - pConfig->WriteLine("unbindall"); + pConfigManager->WriteLine("unbindall"); for(int i = 0; i < MODIFIER_COMBINATION_COUNT; i++) { for(int j = 0; j < KEY_LAST; j++) @@ -443,7 +443,7 @@ void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData) str_escape(&pDst, pSelf->m_aapKeyBindings[i][j], pEnd); str_append(aBuffer, "\"", sizeof(aBuffer)); - pConfig->WriteLine(aBuffer); + pConfigManager->WriteLine(aBuffer); } } } diff --git a/src/game/client/components/binds.h b/src/game/client/components/binds.h index 4eaff794e..f09475dcf 100644 --- a/src/game/client/components/binds.h +++ b/src/game/client/components/binds.h @@ -15,7 +15,7 @@ class CBinds : public CComponent static void ConUnbindAll(IConsole::IResult *pResult, void *pUserData); class IConsole *GetConsole() const { return Console(); } - static void ConfigSaveCallback(class IConfig *pConfig, void *pUserData); + static void ConfigSaveCallback(IConfigManager *pConfigManager, void *pUserData); public: CBinds(); diff --git a/src/game/client/components/menus_settings.cpp b/src/game/client/components/menus_settings.cpp index d1feeaa58..149fbae49 100644 --- a/src/game/client/components/menus_settings.cpp +++ b/src/game/client/components/menus_settings.cpp @@ -744,8 +744,6 @@ void CMenus::RenderSettingsTee(CUIRect MainView) TextRender()->SetCurFont(NULL); } -typedef void (*pfnAssignFuncCallback)(CConfiguration *pConfig, int Value); - typedef struct { CLocConstString m_Name; diff --git a/src/game/client/gameclient.h b/src/game/client/gameclient.h index bcf02fc4e..cce0d3639 100644 --- a/src/game/client/gameclient.h +++ b/src/game/client/gameclient.h @@ -84,6 +84,7 @@ class CGameClient : public IGameClient class ITextRender *m_pTextRender; class IClient *m_pClient; class ISound *m_pSound; + class CConfig *m_pConfig; class IConsole *m_pConsole; class IStorage *m_pStorage; class IDemoPlayer *m_pDemoPlayer; @@ -135,6 +136,7 @@ public: class ISound *Sound() const { return m_pSound; } class IInput *Input() const { return m_pInput; } class IStorage *Storage() const { return m_pStorage; } + class CConfig *Config() const { return m_pConfig; } class IConsole *Console() { return m_pConsole; } class ITextRender *TextRender() const { return m_pTextRender; } class IDemoPlayer *DemoPlayer() const { return m_pDemoPlayer; } diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index a9412c846..fb91eaebe 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -6346,6 +6346,7 @@ void CEditor::Init() { m_pInput = Kernel()->RequestInterface(); m_pClient = Kernel()->RequestInterface(); + m_pConfig = Kernel()->RequestInterface()->Values(); m_pConsole = Kernel()->RequestInterface(); m_pGraphics = Kernel()->RequestInterface(); m_pTextRender = Kernel()->RequestInterface(); diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index abce25cd6..fd793ea98 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -631,6 +631,7 @@ class CEditor : public IEditor { class IInput *m_pInput; class IClient *m_pClient; + class CConfig *m_pConfig; class IConsole *m_pConsole; class IGraphics *m_pGraphics; class ITextRender *m_pTextRender; @@ -642,6 +643,7 @@ class CEditor : public IEditor public: class IInput *Input() { return m_pInput; }; class IClient *Client() { return m_pClient; }; + class CConfig *Config() { return m_pConfig; } class IConsole *Console() { return m_pConsole; }; class IGraphics *Graphics() { return m_pGraphics; }; class ISound *Sound() { return m_pSound; } diff --git a/src/game/server/entity.h b/src/game/server/entity.h index b057a0830..1c314a225 100644 --- a/src/game/server/entity.h +++ b/src/game/server/entity.h @@ -57,6 +57,7 @@ public: /* Objects */ class CGameWorld *GameWorld() { return m_pGameWorld; } + class CConfig *Config() { return m_pGameWorld->Config(); } class CGameContext *GameServer() { return m_pGameWorld->GameServer(); } class IServer *Server() { return m_pGameWorld->Server(); } diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 03993d968..2f988d8f7 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3006,6 +3006,7 @@ void CGameContext::ConchainSpecialMotdupdate(IConsole::IResult *pResult, void *p void CGameContext::OnConsoleInit() { m_pServer = Kernel()->RequestInterface(); + m_pConfig = Kernel()->RequestInterface()->Values(); m_pConsole = Kernel()->RequestInterface(); m_pEngine = Kernel()->RequestInterface(); m_pStorage = Kernel()->RequestInterface(); @@ -3052,6 +3053,7 @@ void CGameContext::OnConsoleInit() void CGameContext::OnInit(/*class IKernel *pKernel*/) { m_pServer = Kernel()->RequestInterface(); + m_pConfig = Kernel()->RequestInterface()->Values(); m_pConsole = Kernel()->RequestInterface(); m_pEngine = Kernel()->RequestInterface(); m_pStorage = Kernel()->RequestInterface(); diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index 656285322..977bbbe75 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -56,6 +56,7 @@ enum NUM_TUNEZONES = 256 }; +class CConfig; class CHeap; class CPlayer; class CScore; @@ -69,6 +70,7 @@ struct CScoreRandomMapResult; class CGameContext : public IGameServer { IServer *m_pServer; + CConfig *m_pConfig; IConsole *m_pConsole; IEngine *m_pEngine; IStorage *m_pStorage; @@ -131,6 +133,7 @@ class CGameContext : public IGameServer public: IServer *Server() const { return m_pServer; } + CConfig *Config() { return m_pConfig; } IConsole *Console() { return m_pConsole; } IEngine *Engine() { return m_pEngine; } IStorage *Storage() { return m_pStorage; } diff --git a/src/game/server/gamecontroller.cpp b/src/game/server/gamecontroller.cpp index eb4653a44..4f91e7a52 100644 --- a/src/game/server/gamecontroller.cpp +++ b/src/game/server/gamecontroller.cpp @@ -22,6 +22,7 @@ IGameController::IGameController(class CGameContext *pGameServer) { m_pGameServer = pGameServer; + m_pConfig = m_pGameServer->Config(); m_pServer = m_pGameServer->Server(); m_pGameType = "unknown"; diff --git a/src/game/server/gamecontroller.h b/src/game/server/gamecontroller.h index 600fdfde1..d1fecfcd7 100644 --- a/src/game/server/gamecontroller.h +++ b/src/game/server/gamecontroller.h @@ -29,10 +29,12 @@ class IGameController int m_aNumSpawnPoints[3]; class CGameContext *m_pGameServer; + class CConfig *m_pConfig; class IServer *m_pServer; protected: CGameContext *GameServer() const { return m_pGameServer; } + CConfig *Config() { return m_pConfig; } IServer *Server() const { return m_pServer; } struct CSpawnEval diff --git a/src/game/server/gameworld.cpp b/src/game/server/gameworld.cpp index f3d314332..b8e023bc6 100644 --- a/src/game/server/gameworld.cpp +++ b/src/game/server/gameworld.cpp @@ -16,6 +16,7 @@ CGameWorld::CGameWorld() { m_pGameServer = 0x0; + m_pConfig = 0x0; m_pServer = 0x0; m_Paused = false; @@ -35,6 +36,7 @@ CGameWorld::~CGameWorld() void CGameWorld::SetGameServer(CGameContext *pGameServer) { m_pGameServer = pGameServer; + m_pConfig = m_pGameServer->Config(); m_pServer = m_pGameServer->Server(); } diff --git a/src/game/server/gameworld.h b/src/game/server/gameworld.h index dcb16ee29..23b41abf6 100644 --- a/src/game/server/gameworld.h +++ b/src/game/server/gameworld.h @@ -36,12 +36,14 @@ private: CEntity *m_apFirstEntityTypes[NUM_ENTTYPES]; class CGameContext *m_pGameServer; + class CConfig *m_pConfig; class IServer *m_pServer; void UpdatePlayerMaps(); public: class CGameContext *GameServer() { return m_pGameServer; } + class CConfig *Config() { return m_pConfig; } class IServer *Server() { return m_pServer; } bool m_ResetRequested; diff --git a/src/game/server/teehistorian.h b/src/game/server/teehistorian.h index 0e3be5141..32518b131 100644 --- a/src/game/server/teehistorian.h +++ b/src/game/server/teehistorian.h @@ -9,7 +9,7 @@ #include -struct CConfiguration; +class CConfig; class CTuningParams; class CUuidManager; @@ -34,7 +34,7 @@ public: SHA256_DIGEST m_MapSha256; int m_MapCrc; - CConfiguration *m_pConfig; + CConfig *m_pConfig; CTuningParams *m_pTuning; CUuidManager *m_pUuids; }; diff --git a/src/mastersrv/mastersrv.cpp b/src/mastersrv/mastersrv.cpp index 4e95ea218..3843a7f44 100644 --- a/src/mastersrv/mastersrv.cpp +++ b/src/mastersrv/mastersrv.cpp @@ -332,17 +332,17 @@ int main(int argc, const char **argv) // ignore_convention IKernel *pKernel = IKernel::Create(); IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_BASIC, argc, argv); - IConfig *pConfig = CreateConfig(); + IConfigManager *pConfigManager = CreateConfigManager(); m_pConsole = CreateConsole(CFGFLAG_MASTER); bool RegisterFail = !pKernel->RegisterInterface(pStorage); RegisterFail |= !pKernel->RegisterInterface(m_pConsole); - RegisterFail |= !pKernel->RegisterInterface(pConfig); + RegisterFail |= !pKernel->RegisterInterface(pConfigManager); if(RegisterFail) return -1; - pConfig->Init(); + pConfigManager->Init(); m_pConsole->Init(); m_NetBan.Init(m_pConsole, pStorage); if(argc > 1) // ignore_convention diff --git a/src/test/teehistorian.cpp b/src/test/teehistorian.cpp index 2618d12c0..071aa7005 100644 --- a/src/test/teehistorian.cpp +++ b/src/test/teehistorian.cpp @@ -12,7 +12,7 @@ class TeeHistorian : public ::testing::Test { protected: CTeeHistorian m_TH; - CConfiguration m_Config; + CConfig m_Config; CTuningParams m_Tuning; CUuidManager m_UuidManager; CTeeHistorian::CGameInfo m_GameInfo;