Add logic for new Settings type

Signed-off-by: Lucca Jiménez Könings <development@jimkoen.com>
This commit is contained in:
Lucca Jiménez Könings
2024-02-22 23:15:32 +01:00
parent 55f5437618
commit 89034a64e0
4 changed files with 97 additions and 18 deletions

View File

@@ -103,7 +103,7 @@ public:
static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; }
static TSettings Settings;
static struct Settings SettingsSingleton;
static inline struct Settings SettingsSingleton { };
static std::vector<std::string> GetBackendUrlsInOrder() {
return {

View File

@@ -19,6 +19,7 @@
#pragma once
#include "Common.h"
#include "TSettings.h"
#include <atomic>
#include <filesystem>
@@ -43,6 +44,7 @@ private:
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, std::string& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, bool& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, int& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, Settings::Key key);
void ParseOldFormat();
std::string TagsAsPrettyArray() const;

View File

@@ -49,7 +49,21 @@ struct Settings {
General_Debug
};
std::unordered_map<Key, SettingsTypeVariant> SettingsMap {};
std::unordered_map<Key, SettingsTypeVariant> SettingsMap {
{ General_Description, "BeamMP Default Description" },
{ General_Tags, "Freeroam" },
{ General_MaxPlayers, 8 },
{ General_Name, "BeamMP Server" },
{ General_Map, "/levels/gridmap_v2/info.json" },
{ General_AuthKey, "" },
{ General_Private, true },
{ General_Port, 30814 },
{ General_MaxCars, 1 },
{ General_LogChat, true },
{ General_ResourceFolder, "Resources" },
{ General_Debug, false }
};
std::string getAsString(Key key) {
if (!SettingsMap.contains(key)) {
@@ -72,6 +86,13 @@ struct Settings {
return std::get<bool>(SettingsMap.at(key));
}
SettingsTypeVariant get(Key key) {
if (!SettingsMap.contains(key)) {
throw std::logic_error { "Undefined setting key accessed in Settings::get" };
}
return 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" };