diff options
Diffstat (limited to 'src/Root.cpp')
-rw-r--r-- | src/Root.cpp | 135 |
1 files changed, 66 insertions, 69 deletions
diff --git a/src/Root.cpp b/src/Root.cpp index 87bc29627..b28e7c894 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -19,6 +19,8 @@ #include "LoggerListeners.h" #include "BuildInfo.h" #include "IniFile.h" +#include "SettingsRepositoryInterface.h" +#include "OverridesSettingsRepository.h" #ifdef _WIN32 #include <conio.h> @@ -33,7 +35,8 @@ -cRoot* cRoot::s_Root = nullptr; +cRoot * cRoot::s_Root = nullptr; +bool cRoot::m_ShouldStop = false; @@ -48,7 +51,6 @@ cRoot::cRoot(void) : m_WebAdmin(nullptr), m_PluginManager(nullptr), m_MojangAPI(nullptr), - m_bStop(false), m_bRestart(false) { s_Root = this; @@ -70,8 +72,8 @@ cRoot::~cRoot() void cRoot::InputThread(cRoot & a_Params) { cLogCommandOutputCallback Output; - - while (!a_Params.m_bStop && !a_Params.m_bRestart && !m_TerminateEventRaised && std::cin.good()) + + while (!cRoot::m_ShouldStop && !a_Params.m_bRestart && !m_TerminateEventRaised && std::cin.good()) { AString Command; std::getline(std::cin, Command); @@ -83,10 +85,11 @@ void cRoot::InputThread(cRoot & a_Params) if (m_TerminateEventRaised || !std::cin.good()) { - // We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running; stop the server: - if (m_RunAsService) // HACK: Dont kill if running as a service + // We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running + // Stop the server: + if (!m_RunAsService) // Dont kill if running as a service { - a_Params.m_bStop = true; + a_Params.m_ShouldStop = true; } } } @@ -95,19 +98,19 @@ void cRoot::InputThread(cRoot & a_Params) -void cRoot::Start(void) +void cRoot::Start(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo) { #ifdef _WIN32 HWND hwnd = GetConsoleWindow(); HMENU hmenu = GetSystemMenu(hwnd, FALSE); EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED); // Disable close button when starting up; it causes problems with our CTRL-CLOSE handling #endif - + cLogger::cListener * consoleLogListener = MakeConsoleListener(); cLogger::cListener * fileLogListener = new cFileListener(); cLogger::GetInstance().AttachListener(consoleLogListener); cLogger::GetInstance().AttachListener(fileLogListener); - + LOG("--- Started Log ---\n"); #ifdef BUILD_ID @@ -117,8 +120,8 @@ void cRoot::Start(void) cDeadlockDetect dd; - m_bStop = false; - while (!m_bStop) + m_ShouldStop = false; + while (!m_ShouldStop) { auto BeginTime = std::chrono::steady_clock::now(); m_bRestart = false; @@ -129,22 +132,24 @@ void cRoot::Start(void) m_Server = new cServer(); LOG("Reading server config..."); - cIniFile IniFile; - if (!IniFile.ReadFile("settings.ini")) + + auto IniFile = cpp14::make_unique<cIniFile>(); + if (!IniFile->ReadFile("settings.ini")) { LOGWARN("Regenerating settings.ini, all settings will be reset"); - IniFile.AddHeaderComment(" This is the main server configuration"); - IniFile.AddHeaderComment(" Most of the settings here can be configured using the webadmin interface, if enabled in webadmin.ini"); - IniFile.AddHeaderComment(" See: http://wiki.mc-server.org/doku.php?id=configure:settings.ini for further configuration help"); + IniFile->AddHeaderComment(" This is the main server configuration"); + IniFile->AddHeaderComment(" Most of the settings here can be configured using the webadmin interface, if enabled in webadmin.ini"); + IniFile->AddHeaderComment(" See: http://wiki.mc-server.org/doku.php?id=configure:settings.ini for further configuration help"); } + auto settingsRepo = cpp14::make_unique<cOverridesSettingsRepository>(std::move(IniFile), std::move(overridesRepo)); LOG("Starting server..."); m_MojangAPI = new cMojangAPI; - bool ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true); - m_MojangAPI->Start(IniFile, ShouldAuthenticate); // Mojang API needs to be started before plugins, so that plugins may use it for DB upgrades on server init - if (!m_Server->InitServer(IniFile, ShouldAuthenticate)) + bool ShouldAuthenticate = settingsRepo->GetValueSetB("Authentication", "Authenticate", true); + m_MojangAPI->Start(*settingsRepo, ShouldAuthenticate); // Mojang API needs to be started before plugins, so that plugins may use it for DB upgrades on server init + if (!m_Server->InitServer(*settingsRepo, ShouldAuthenticate)) { - IniFile.WriteFile("settings.ini"); + settingsRepo->Flush(); LOGERROR("Failure starting server, aborting..."); return; } @@ -157,31 +162,31 @@ void cRoot::Start(void) m_RankManager->Initialize(*m_MojangAPI); m_CraftingRecipes = new cCraftingRecipes; m_FurnaceRecipe = new cFurnaceRecipe(); - + LOGD("Loading worlds..."); - LoadWorlds(IniFile); + LoadWorlds(*settingsRepo); LOGD("Loading plugin manager..."); m_PluginManager = new cPluginManager(); - m_PluginManager->ReloadPluginsNow(IniFile); - + m_PluginManager->ReloadPluginsNow(*settingsRepo); + LOGD("Loading MonsterConfig..."); m_MonsterConfig = new cMonsterConfig; // This sets stuff in motion LOGD("Starting Authenticator..."); - m_Authenticator.Start(IniFile); - + m_Authenticator.Start(*settingsRepo); + LOGD("Starting worlds..."); StartWorlds(); - - if (IniFile.GetValueSetB("DeadlockDetect", "Enabled", true)) + + if (settingsRepo->GetValueSetB("DeadlockDetect", "Enabled", true)) { LOGD("Starting deadlock detector..."); - dd.Start(IniFile.GetValueSetI("DeadlockDetect", "IntervalSec", 20)); + dd.Start(settingsRepo->GetValueSetI("DeadlockDetect", "IntervalSec", 20)); } - - IniFile.WriteFile("settings.ini"); + + settingsRepo->Flush(); LOGD("Finalising startup..."); if (m_Server->Start()) @@ -202,18 +207,22 @@ void cRoot::Start(void) #endif LOG("Startup complete, took %ldms!", static_cast<long int>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - BeginTime).count())); + + // Save the current time + m_StartTime = std::chrono::steady_clock::now(); + #ifdef _WIN32 EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button #endif - while (!m_bStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads + while (!m_ShouldStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads { std::this_thread::sleep_for(std::chrono::seconds(1)); } if (m_TerminateEventRaised) { - m_bStop = true; + m_ShouldStop = true; } // Stop the server: @@ -224,7 +233,7 @@ void cRoot::Start(void) } // if (m_Server->Start()) else { - m_bStop = true; + m_ShouldStop = true; } delete m_MojangAPI; m_MojangAPI = nullptr; @@ -248,7 +257,7 @@ void cRoot::Start(void) LOGD("Unloading worlds..."); UnloadWorlds(); - + LOGD("Stopping plugin manager..."); delete m_PluginManager; m_PluginManager = nullptr; @@ -259,9 +268,9 @@ void cRoot::Start(void) LOG("Shutdown successful!"); } - + LOG("--- Stopped Log ---"); - + cLogger::GetInstance().DetachListener(consoleLogListener); delete consoleLogListener; cLogger::GetInstance().DetachListener(fileLogListener); @@ -271,13 +280,6 @@ void cRoot::Start(void) -void cRoot::SetStopping(bool a_Stopping) -{ - m_bStop = a_Stopping; -} - - - void cRoot::LoadGlobalSettings() { @@ -288,45 +290,44 @@ void cRoot::LoadGlobalSettings() -void cRoot::LoadWorlds(cIniFile & IniFile) +void cRoot::LoadWorlds(cSettingsRepositoryInterface & a_Settings) { // First get the default world - AString DefaultWorldName = IniFile.GetValueSet("Worlds", "DefaultWorld", "world"); + AString DefaultWorldName = a_Settings.GetValueSet("Worlds", "DefaultWorld", "world"); m_pDefaultWorld = new cWorld(DefaultWorldName.c_str()); m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld; // Then load the other worlds - int KeyNum = IniFile.FindKey("Worlds"); - int NumWorlds = IniFile.GetNumValues(KeyNum); - if (NumWorlds <= 0) + auto Worlds = a_Settings.GetValues("Worlds"); + if (Worlds.size() <= 0) { return; } - + bool FoundAdditionalWorlds = false; - for (int i = 0; i < NumWorlds; i++) + for (auto WorldNameValue : Worlds) { - AString ValueName = IniFile.GetValueName(KeyNum, i); + AString ValueName = WorldNameValue.first; if (ValueName.compare("World") != 0) { continue; } - AString WorldName = IniFile.GetValue(KeyNum, i); + AString WorldName = WorldNameValue.second; if (WorldName.empty()) { continue; } FoundAdditionalWorlds = true; - cWorld* NewWorld = new cWorld( WorldName.c_str()); - m_WorldsByName[ WorldName ] = NewWorld; + cWorld * NewWorld = new cWorld(WorldName.c_str()); + m_WorldsByName[WorldName] = NewWorld; } // for i - Worlds if (!FoundAdditionalWorlds) { - if (IniFile.GetKeyComment("Worlds", 0) != " World=secondworld") + if (a_Settings.GetKeyComment("Worlds", 0) != " World=secondworld") { - IniFile.DeleteKeyComment("Worlds", 0); - IniFile.AddKeyComment("Worlds", " World=secondworld"); + a_Settings.DeleteKeyComment("Worlds", 0); + a_Settings.AddKeyComment("Worlds", " World=secondworld"); } } } @@ -466,7 +467,7 @@ void cRoot::QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCall // Some commands are built-in: if (a_Cmd == "stop") { - m_bStop = true; + m_ShouldStop = true; } else if (a_Cmd == "restart") { @@ -498,7 +499,7 @@ void cRoot::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback // cRoot handles stopping and restarting due to our access to controlling variables if (a_Cmd == "stop") { - m_bStop = true; + m_ShouldStop = true; return; } else if (a_Cmd == "restart") @@ -613,7 +614,7 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac size_t Rating = RateCompareString (m_PlayerName, a_pPlayer->GetName()); if ((Rating > 0) && (Rating >= m_BestRating)) { - m_BestMatch = a_pPlayer; + m_BestMatch = a_pPlayer->GetName(); if (Rating > m_BestRating) { m_NumMatches = 0; @@ -633,18 +634,18 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac m_BestRating(0), m_NameLength(a_PlayerName.length()), m_PlayerName(a_PlayerName), - m_BestMatch(nullptr), + m_BestMatch(), m_NumMatches(0) {} - cPlayer * m_BestMatch; + AString m_BestMatch; unsigned m_NumMatches; } Callback (a_PlayerName); ForEachPlayer(Callback); if (Callback.m_NumMatches == 1) { - return a_Callback.Item(Callback.m_BestMatch); + return DoWithPlayer(Callback.m_BestMatch, a_Callback); } return false; } @@ -853,7 +854,3 @@ int cRoot::GetFurnaceFuelBurnTime(const cItem & a_Fuel) cFurnaceRecipe * FR = Get()->GetFurnaceRecipe(); return FR->GetBurnTime(a_Fuel); } - - - - |