diff --git a/include/Env.h b/include/Env.h index 030eeaee..880a2c91 100644 --- a/include/Env.h +++ b/include/Env.h @@ -7,6 +7,7 @@ namespace Env { enum class Key { // provider settings PROVIDER_UPDATE_MESSAGE, + PROVIDER_DISABLE_CONFIG, }; std::optional Get(Key key); diff --git a/include/TConfig.h b/include/TConfig.h index 84e534a1..9efceec8 100644 --- a/include/TConfig.h +++ b/include/TConfig.h @@ -30,5 +30,6 @@ class TConfig { std::string TagsAsPrettyArray() const; bool IsDefault(); bool mFailed { false }; + bool mDisableConfig { false }; std::string mConfigFileName; }; diff --git a/src/Env.cpp b/src/Env.cpp index 205b35b8..41b2fded 100644 --- a/src/Env.cpp +++ b/src/Env.cpp @@ -15,6 +15,9 @@ std::string_view Env::ToString(Env::Key key) { case Key::PROVIDER_UPDATE_MESSAGE: return "BEAMMP_PROVIDER_UPDATE_MESSAGE"; break; + case Key::PROVIDER_DISABLE_CONFIG: + return "BEAMMP_PROVIDER_DISABLE_CONFIG"; + break; } return ""; } diff --git a/src/TConfig.cpp b/src/TConfig.cpp index 9584c579..2dc1ab38 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -1,5 +1,6 @@ #include "Common.h" +#include "Env.h" #include "TConfig.h" #include #include @@ -69,7 +70,9 @@ TEST_CASE("TConfig::TConfig") { TConfig::TConfig(const std::string& ConfigFileName) : mConfigFileName(ConfigFileName) { Application::SetSubsystemStatus("Config", Application::Status::Starting); - if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) { + auto DisableConfig = Env::Get(Env::Key::PROVIDER_DISABLE_CONFIG).value_or("false"); + mDisableConfig = DisableConfig == "true" || DisableConfig == "1"; + if (!mDisableConfig && (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName))) { beammp_info("No config file found! Generating one..."); CreateConfigFile(); } @@ -143,7 +146,9 @@ void TConfig::FlushToFile() { void TConfig::CreateConfigFile() { // build from old config Server.cfg - + if (mDisableConfig) { + return; + } try { if (fs::exists("Server.cfg")) { // parse it (this is weird and bad and should be removed in some future version) @@ -163,6 +168,9 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons return; } } + if (mDisableConfig) { + return; + } if (Table[Category.c_str()][Key.data()].is_string()) { OutValue = Table[Category.c_str()][Key.data()].as_string(); } @@ -176,6 +184,9 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons return; } } + if (mDisableConfig) { + return; + } if (Table[Category.c_str()][Key.data()].is_boolean()) { OutValue = Table[Category.c_str()][Key.data()].as_boolean(); } @@ -188,6 +199,9 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons return; } } + if (mDisableConfig) { + return; + } if (Table[Category.c_str()][Key.data()].is_integer()) { OutValue = int(Table[Category.c_str()][Key.data()].as_integer()); } @@ -195,7 +209,10 @@ void TConfig::TryReadValue(toml::value& Table, const std::string& Category, cons void TConfig::ParseFromFile(std::string_view name) { try { - toml::value data = toml::parse(name.data()); + toml::value data {}; + if (!mDisableConfig) { + data = toml::parse(name.data()); + } // GENERAL TryReadValue(data, "General", StrDebug, EnvStrDebug, Application::Settings.DebugModeEnabled); TryReadValue(data, "General", StrPrivate, EnvStrPrivate, Application::Settings.Private); @@ -223,10 +240,16 @@ void TConfig::ParseFromFile(std::string_view name) { PrintDebug(); // Update in any case - FlushToFile(); + if (!mDisableConfig) { + FlushToFile(); + } // all good so far, let's check if there's a key if (Application::Settings.Key.empty()) { - beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server."); + if (mDisableConfig) { + beammp_error("No AuthKey specified in the environment."); + } else { + beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server."); + } Application::SetSubsystemStatus("Config", Application::Status::Bad); mFailed = true; return; @@ -238,6 +261,9 @@ void TConfig::ParseFromFile(std::string_view name) { } void TConfig::PrintDebug() { + if (mDisableConfig) { + beammp_debug("Provider turned off the generation and parsing of the ServerConfig.toml"); + } beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false")); beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false")); beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port)); @@ -313,6 +339,6 @@ std::string TConfig::TagsAsPrettyArray() const { for (size_t i = 0; i < TagsArray.size() - 1; ++i) { Pretty += '\"' + TagsArray[i] + "\", "; } - Pretty += '\"' + TagsArray.at(TagsArray.size()-1) + "\""; + Pretty += '\"' + TagsArray.at(TagsArray.size() - 1) + "\""; return Pretty; }