change settings to be a hash map instead of a fixed struct

this makes adding settings and changing settings incredibly easy, both
from the console and from lua.
This commit is contained in:
Lion Kortlepel
2022-10-23 17:44:57 +02:00
parent 7d35595683
commit bec09a4761
14 changed files with 250 additions and 248 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include "CustomAssert.h"
#include "TSentry.h"
extern TSentry Sentry;
@@ -23,6 +24,38 @@ namespace fs = std::filesystem;
#include "TConsole.h"
#include <boost/container/flat_map.hpp>
#include <boost/variant.hpp>
// General
constexpr std::string_view StrDebug = "Debug";
constexpr std::string_view StrPrivate = "Private";
constexpr std::string_view StrPort = "Port";
constexpr std::string_view StrMaxCars = "MaxCars";
constexpr std::string_view StrMaxPlayers = "MaxPlayers";
constexpr std::string_view StrMap = "Map";
constexpr std::string_view StrName = "Name";
constexpr std::string_view StrDescription = "Description";
constexpr std::string_view StrResourceFolder = "ResourceFolder";
constexpr std::string_view StrAuthKey = "AuthKey";
constexpr std::string_view StrLogChat = "LogChat";
// Misc
constexpr std::string_view StrSendErrors = "SendErrors";
constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
constexpr std::string_view StrHideUpdateMessages = "ImScaredOfUpdates";
// HTTP
constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
constexpr std::string_view StrHTTPServerUseSSL = "UseSSL";
constexpr std::string_view StrSSLKeyPath = "SSLKeyPath";
constexpr std::string_view StrSSLCertPath = "SSLCertPath";
constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
// Unused
constexpr std::string_view StrCustomIP = "CustomIP";
struct Version {
uint8_t major;
uint8_t minor;
@@ -35,6 +68,9 @@ struct Version {
template <typename T>
using SparseArray = std::unordered_map<size_t, T>;
using boost::variant;
using boost::container::flat_map;
// static class handling application start, shutdown, etc.
// yes, static classes, singletons, globals are all pretty
// bad idioms. In this case we need a central way to access
@@ -43,30 +79,17 @@ using SparseArray = std::unordered_map<size_t, T>;
class Application final {
public:
// types
struct TSettings {
std::string ServerName { "BeamMP Server" };
std::string ServerDesc { "BeamMP Default Description" };
std::string Resource { "Resources" };
std::string MapName { "/levels/gridmap_v2/info.json" };
std::string Key {};
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(); }
};
using SettingValue = variant<std::string, bool, int>;
using SettingsMap = flat_map<std::string_view, SettingValue>;
static SettingsMap mSettings;
static std::string GetSettingString(std::string_view Name);
static int GetSettingInt(std::string_view Name);
static bool GetSettingBool(std::string_view Name);
static void SetSetting(std::string_view Name, const SettingValue& value);
using TShutdownHandler = std::function<void()>;
@@ -84,8 +107,6 @@ public:
static std::string PPS() { return mPPS; }
static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; }
static TSettings Settings;
static std::vector<std::string> GetBackendUrlsInOrder() {
return {
"backend.beammp.com",
@@ -125,6 +146,8 @@ public:
static void SetSubsystemStatus(const std::string& Subsystem, Status status);
static std::string SettingToString(const SettingValue& Value);
private:
static void SetShutdown(bool Val);
@@ -205,13 +228,13 @@ void RegisterThread(const std::string& str);
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
#define beammp_debug(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
} \
} while (false)
#define beammp_event(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \
} \
} while (false)
@@ -219,7 +242,7 @@ void RegisterThread(const std::string& str);
#if defined(DEBUG)
#define beammp_trace(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \
} \
} while (false)

View File

@@ -22,9 +22,7 @@ private:
void CreateConfigFile();
void ParseFromFile(std::string_view name);
void PrintDebug();
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, bool& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, int& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key);
void ParseOldFormat();
bool IsDefault();