From 55f543761840aa4fb416fbd48cc65f9229c0d1b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucca=20Jim=C3=A9nez=20K=C3=B6nings?= Date: Sun, 18 Feb 2024 19:21:07 +0100 Subject: [PATCH] Add new type `Settings` & refactor TSettings behavior into Settings by adding getters/setters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucca Jiménez Könings --- include/Common.h | 4 +- include/TSettings.h | 136 ++++++++++++++++++++++++++++++++++++++++++++ src/TConfig.cpp | 20 +++++++ src/main.cpp | 5 ++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 include/TSettings.h diff --git a/include/Common.h b/include/Common.h index bb28a64..415e566 100644 --- a/include/Common.h +++ b/include/Common.h @@ -37,6 +37,7 @@ namespace fs = std::filesystem; #include "TConsole.h" +#include "TSettings.h" struct Version { uint8_t major; @@ -65,7 +66,7 @@ public: std::string Resource { "Resources" }; std::string MapName { "/levels/gridmap_v2/info.json" }; std::string Key {}; - std::string Password{}; + std::string Password {}; std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" }; std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" }; bool HTTPServerEnabled { false }; @@ -102,6 +103,7 @@ public: static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; } static TSettings Settings; + static struct Settings SettingsSingleton; static std::vector GetBackendUrlsInOrder() { return { diff --git a/include/TSettings.h b/include/TSettings.h new file mode 100644 index 0000000..1baa2b6 --- /dev/null +++ b/include/TSettings.h @@ -0,0 +1,136 @@ +// BeamMP, the BeamNG.drive multiplayer mod. +// Copyright (C) 2024 BeamMP Ltd., BeamMP team and contributors. +// +// BeamMP Ltd. can be contacted by electronic mail via contact@beammp.com. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#pragma once +#include +#include +#include +#include + +struct Settings { + using SettingsTypeVariant = std::variant; + + enum Key { + // Keys that correspond to the keys set in TOML + // Keys have their TOML section name as prefix + + // [Misc] + Misc_SendErrorsShowMessage, + Misc_SendErrors, + Misc_ImScaredOfUpdates, + + // [General] + General_Description, + General_Tags, + General_MaxPlayers, + General_Name, + General_Map, + General_AuthKey, + General_Private, + General_Port, + General_MaxCars, + General_LogChat, + General_ResourceFolder, + General_Debug + }; + + std::unordered_map SettingsMap {}; + + std::string getAsString(Key key) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined key accessed in Settings::getAsString" }; + } + return std::get(SettingsMap.at(key)); + } + + int getAsInt(Key key) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined key accessed in Settings::getAsInt" }; + } + return std::get(SettingsMap.at(key)); + } + + bool getAsBool(Key key) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined key accessed in Settings::getAsBool" }; + } + return std::get(SettingsMap.at(key)); + } + + void set(Key key, std::string value) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined setting key accessed in Settings::getAsString" }; + } + if (!std::holds_alternative(SettingsMap.at(key))) { + throw std::logic_error { "Wrong value type in Settings::get: std::string" }; + } + SettingsMap.at(key) = value; + } + + void set(Key key, int value) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined setting key accessed in Settings::getAsString" }; + } + if (!std::holds_alternative(SettingsMap.at(key))) { + throw std::logic_error { "Wrong value type in Settings::get: std::string" }; + } + SettingsMap.at(key) = value; + } + + void set(Key key, bool value) { + if (!SettingsMap.contains(key)) { + throw std::logic_error { "Undefined setting key accessed in Settings::getAsString" }; + } + if (!std::holds_alternative(SettingsMap.at(key))) { + throw std::logic_error { "Wrong value type in Settings::get: std::string" }; + } + SettingsMap.at(key) = value; + } +}; + +/*struct TSettings { + + +std::string ServerName { "BeamMP Server" }; +std::string ServerDesc { "BeamMP Default Description" }; +std::string ServerTags { "Freeroam" }; +std::string Resource { "Resources" }; +std::string MapName { "/levels/gridmap_v2/info.json" }; +std::string Key {}; +std::string Password{}; +std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" }; +std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" }; +bool HTTPServerEnabled { false }; +int MaxPlayers { 8 }; +bool Private { true }; +int MaxCars { 1 }; +bool DebugModeEnabled { false }; +int Port { 30814 }; +std::string CustomIP {}; +bool LogChat { true }; +bool SendErrors { true }; +bool SendErrorsMessageEnabled { true }; +int HTTPServerPort { 8080 }; +std::string HTTPServerIP { "127.0.0.1" }; +bool HTTPServerUseSSL { false }; +bool HideUpdateMessages { false }; +[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } + }; + + +}*/ diff --git a/src/TConfig.cpp b/src/TConfig.cpp index 7192f87..46abe3f 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -253,6 +253,26 @@ void TConfig::ParseFromFile(std::string_view name) { TryReadValue(data, "Misc", StrSendErrors, "", Application::Settings.SendErrors); TryReadValue(data, "Misc", StrHideUpdateMessages, "", Application::Settings.HideUpdateMessages); TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, "", Application::Settings.SendErrorsMessageEnabled); + + // Read into new Settings Singleton + TryReadValue(data, "General", StrDebug, EnvStrDebug, Application::Settings.DebugModeEnabled); + TryReadValue(data, "General", StrPrivate, EnvStrPrivate, Application::Settings.Private); + TryReadValue(data, "General", StrPort, EnvStrPort, Application::Settings.Port); + TryReadValue(data, "General", StrMaxCars, EnvStrMaxCars, Application::Settings.MaxCars); + TryReadValue(data, "General", StrMaxPlayers, EnvStrMaxPlayers, Application::Settings.MaxPlayers); + TryReadValue(data, "General", StrMap, EnvStrMap, Application::Settings.MapName); + TryReadValue(data, "General", StrName, EnvStrName, Application::Settings.ServerName); + TryReadValue(data, "General", StrDescription, EnvStrDescription, Application::Settings.ServerDesc); + TryReadValue(data, "General", StrTags, EnvStrTags, Application::Settings.ServerTags); + TryReadValue(data, "General", StrResourceFolder, EnvStrResourceFolder, Application::Settings.Resource); + TryReadValue(data, "General", StrAuthKey, EnvStrAuthKey, Application::Settings.Key); + TryReadValue(data, "General", StrLogChat, EnvStrLogChat, Application::Settings.LogChat); + TryReadValue(data, "General", StrPassword, "", Application::Settings.Password); + // Misc + TryReadValue(data, "Misc", StrSendErrors, "", Application::Settings.SendErrors); + TryReadValue(data, "Misc", StrHideUpdateMessages, "", Application::Settings.HideUpdateMessages); + TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, "", Application::Settings.SendErrorsMessageEnabled); + } catch (const std::exception& err) { beammp_error("Error parsing config file value: " + std::string(err.what())); mFailed = true; diff --git a/src/main.cpp b/src/main.cpp index 309eef7..d3f1473 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,7 @@ #include "TPluginMonitor.h" #include "TResourceManager.h" #include "TServer.h" +#include "TSettings.h" #include #include @@ -165,6 +166,10 @@ int BeamMPServerMain(MainArguments Arguments) { SetupSignalHandlers(); + Settings settings {}; + settings.SettingsMap.emplace(Settings::Key::General_Name, Settings::SettingsTypeVariant { "Your mom" }); + beammp_infof("Server name set in new impl: {}", settings.getAsString(Settings::Key::General_Name)); + bool Shutdown = false; Application::RegisterShutdownHandler([&Shutdown] { beammp_info("If this takes too long, you can press Ctrl+C repeatedly to force a shutdown.");