Add new type Settings & refactor TSettings behavior

into Settings by adding getters/setters

Signed-off-by: Lucca Jiménez Könings <development@jimkoen.com>
This commit is contained in:
Lucca Jiménez Könings 2024-02-18 19:21:07 +01:00
parent 4d7967d5d9
commit 55f5437618
4 changed files with 164 additions and 1 deletions

View File

@ -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<std::string> GetBackendUrlsInOrder() {
return {

136
include/TSettings.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
#pragma once
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <variant>
struct Settings {
using SettingsTypeVariant = std::variant<std::string, bool, int>;
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<Key, SettingsTypeVariant> SettingsMap {};
std::string getAsString(Key key) {
if (!SettingsMap.contains(key)) {
throw std::logic_error { "Undefined key accessed in Settings::getAsString" };
}
return std::get<std::string>(SettingsMap.at(key));
}
int getAsInt(Key key) {
if (!SettingsMap.contains(key)) {
throw std::logic_error { "Undefined key accessed in Settings::getAsInt" };
}
return std::get<int>(SettingsMap.at(key));
}
bool getAsBool(Key key) {
if (!SettingsMap.contains(key)) {
throw std::logic_error { "Undefined key accessed in Settings::getAsBool" };
}
return std::get<bool>(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<std::string>(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<int>(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<bool>(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(); }
};
}*/

View File

@ -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;

View File

@ -29,6 +29,7 @@
#include "TPluginMonitor.h"
#include "TResourceManager.h"
#include "TServer.h"
#include "TSettings.h"
#include <cstdint>
#include <iostream>
@ -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.");