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 8472160493
commit 10efab4d71
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
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();

View File

@ -13,10 +13,83 @@
#include "CustomAssert.h"
#include "Http.h"
Application::SettingsMap Application::mSettings = {
{ StrName, std::string("BeamMP Server") },
{ StrDescription, std::string("No description") },
{ StrResourceFolder, std::string("Resources") },
{ StrMap, std::string("/levels/gridmap_v2/info.json") },
{ StrSSLKeyPath, std::string("./.ssl/HttpServer/key.pem") },
{ StrSSLCertPath, std::string("./.ssl/HttpServer/cert.pem") },
{ StrHTTPServerEnabled, false },
{ StrMaxPlayers, int(8) },
{ StrPrivate, true },
{ StrMaxCars, int(1) },
{ StrDebug, false },
{ StrPort, int(30814) },
{ StrCustomIP, std::string("") },
{ StrLogChat, true },
{ StrSendErrors, true },
{ StrSendErrorsMessageEnabled, true },
{ StrHTTPServerPort, int(8080) },
{ StrHTTPServerIP, std::string("127.0.0.1") },
{ StrHTTPServerUseSSL, false },
{ StrHideUpdateMessages, false },
};
// global, yes, this is ugly, no, it cant be done another way
TSentry Sentry {};
Application::TSettings Application::Settings = {};
std::string Application::SettingToString(const Application::SettingValue& Value) {
switch (Value.which()) {
case 0:
return fmt::format("{}", boost::get<std::string>(Value));
case 1:
return fmt::format("{}", boost::get<bool>(Value));
case 2:
return fmt::format("{}", boost::get<int>(Value));
default:
return "<unknown type>";
}
}
std::string Application::GetSettingString(std::string_view Name) {
try {
return boost::get<std::string>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get string setting '{}': {}", Name, e.what());
return "";
}
}
int Application::GetSettingInt(std::string_view Name) {
try {
return boost::get<int>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get int setting '{}': {}", Name, e.what());
return 0;
}
}
bool Application::GetSettingBool(std::string_view Name) {
try {
return boost::get<bool>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get bool setting '{}': {}", Name, e.what());
return false;
}
}
void Application::SetSetting(std::string_view Name, const Application::SettingValue& Value) {
if (mSettings.contains(Name)) {
if (mSettings[Name].type() == Value.type()) {
mSettings[Name] = Value;
} else {
beammp_errorf("Could not change value of setting '{}', because it has a different type.", Name);
}
} else {
mSettings[Name] = Value;
}
}
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
std::unique_lock Lock(mShutdownHandlersMutex);
@ -239,7 +312,7 @@ static std::mutex ThreadNameMapMutex {};
std::string ThreadName(bool DebugModeOverride) {
auto Lock = std::unique_lock(ThreadNameMapMutex);
if (DebugModeOverride || Application::Settings.DebugModeEnabled) {
if (DebugModeOverride || Application::GetSettingBool(StrDebug)) {
auto id = std::this_thread::get_id();
if (threadNameMap.find(id) != threadNameMap.end()) {
// found
@ -251,21 +324,21 @@ std::string ThreadName(bool DebugModeOverride) {
TEST_CASE("ThreadName") {
RegisterThread("MyThread");
auto OrigDebug = Application::Settings.DebugModeEnabled;
auto OrigDebug = Application::GetSettingBool(StrDebug);
// ThreadName adds a space at the end, legacy but we need it still
SUBCASE("Debug mode enabled") {
Application::Settings.DebugModeEnabled = true;
Application::SetSetting(StrDebug, true);
CHECK(ThreadName(true) == "MyThread ");
CHECK(ThreadName(false) == "MyThread ");
}
SUBCASE("Debug mode disabled") {
Application::Settings.DebugModeEnabled = false;
Application::SetSetting(StrDebug, false);
CHECK(ThreadName(true) == "MyThread ");
CHECK(ThreadName(false) == "");
}
// cleanup
Application::Settings.DebugModeEnabled = OrigDebug;
Application::SetSetting(StrDebug, OrigDebug);
}
void RegisterThread(const std::string& str) {
@ -277,7 +350,7 @@ void RegisterThread(const std::string& str) {
#elif defined(BEAMMP_LINUX)
ThreadId = std::to_string(gettid());
#endif
if (Application::Settings.DebugModeEnabled) {
if (Application::GetSettingBool(StrDebug)) {
std::ofstream ThreadFile(".Threads.log", std::ios::app);
ThreadFile << ("Thread \"" + str + "\" is TID " + ThreadId) << std::endl;
}
@ -310,7 +383,7 @@ TEST_CASE("Version::AsString") {
}
void LogChatMessage(const std::string& name, int id, const std::string& msg) {
if (Application::Settings.LogChat) {
if (Application::GetSettingBool(StrLogChat)) {
std::stringstream ss;
ss << ThreadName();
ss << "[CHAT] ";

View File

@ -154,7 +154,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
}
void Http::Server::THttpServerInstance::operator()() try {
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::GetSettingInt(StrHTTPServerPort)));
std::unique_ptr<httplib::Server> HttpLibServerInstance;
HttpLibServerInstance = std::make_unique<httplib::Server>();
// todo: make this IP agnostic so people can set their own IP
@ -196,7 +196,7 @@ void Http::Server::THttpServerInstance::operator()() try {
beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status));
});
Application::SetSubsystemStatus("HTTPServer", Application::Status::Good);
auto ret = HttpLibServerInstance->listen(Application::Settings.HTTPServerIP.c_str(), Application::Settings.HTTPServerPort);
auto ret = HttpLibServerInstance->listen(Application::GetSettingString(StrHTTPServerIP).c_str(), Application::GetSettingInt(StrHTTPServerPort));
if (!ret) {
beammp_error("Failed to start http server (failed to listen). Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it.");
}

View File

@ -208,56 +208,56 @@ void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
switch (ConfigID) {
case 0: // debug
if (NewValue.is<bool>()) {
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
beammp_info(std::string("Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
Application::SetSetting(StrDebug, NewValue.as<bool>());
beammp_info(std::string("Set `Debug` to ") + (Application::GetSettingBool(StrDebug) ? "true" : "false"));
} else {
beammp_lua_error("set invalid argument [2] expected boolean");
}
break;
case 1: // private
if (NewValue.is<bool>()) {
Application::Settings.Private = NewValue.as<bool>();
beammp_info(std::string("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
Application::SetSetting(StrPrivate, NewValue.as<bool>());
beammp_info(std::string("Set `Private` to ") + (Application::GetSettingBool(StrPrivate) ? "true" : "false"));
} else {
beammp_lua_error("set invalid argument [2] expected boolean");
}
break;
case 2: // max cars
if (NewValue.is<int>()) {
Application::Settings.MaxCars = NewValue.as<int>();
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
Application::SetSetting(StrMaxCars, NewValue.as<int>());
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::GetSettingInt(StrMaxCars)));
} else {
beammp_lua_error("set invalid argument [2] expected integer");
}
break;
case 3: // max players
if (NewValue.is<int>()) {
Application::Settings.MaxPlayers = NewValue.as<int>();
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
Application::SetSetting(StrMaxPlayers, NewValue.as<int>());
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::GetSettingInt(StrMaxPlayers)));
} else {
beammp_lua_error("set invalid argument [2] expected integer");
}
break;
case 4: // Map
if (NewValue.is<std::string>()) {
Application::Settings.MapName = NewValue.as<std::string>();
beammp_info(std::string("Set `Map` to ") + Application::Settings.MapName);
Application::SetSetting(StrMap, NewValue.as<std::string>());
beammp_info(std::string("Set `Map` to ") + Application::GetSettingString(StrMap));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}
break;
case 5: // Name
if (NewValue.is<std::string>()) {
Application::Settings.ServerName = NewValue.as<std::string>();
beammp_info(std::string("Set `Name` to ") + Application::Settings.ServerName);
Application::SetSetting(StrName, NewValue.as<std::string>());
beammp_info(std::string("Set `Name` to ") + Application::GetSettingString(StrName));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}
break;
case 6: // Desc
if (NewValue.is<std::string>()) {
Application::Settings.ServerDesc = NewValue.as<std::string>();
beammp_info(std::string("Set `Description` to ") + Application::Settings.ServerDesc);
Application::SetSetting(StrDescription, NewValue.as<std::string>());
beammp_info(std::string("Set `Description` to ") + Application::GetSettingString(StrDescription));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}

View File

@ -6,32 +6,6 @@
#include <istream>
#include <sstream>
// General
static constexpr std::string_view StrDebug = "Debug";
static constexpr std::string_view StrPrivate = "Private";
static constexpr std::string_view StrPort = "Port";
static constexpr std::string_view StrMaxCars = "MaxCars";
static constexpr std::string_view StrMaxPlayers = "MaxPlayers";
static constexpr std::string_view StrMap = "Map";
static constexpr std::string_view StrName = "Name";
static constexpr std::string_view StrDescription = "Description";
static constexpr std::string_view StrResourceFolder = "ResourceFolder";
static constexpr std::string_view StrAuthKey = "AuthKey";
static constexpr std::string_view StrLogChat = "LogChat";
// Misc
static constexpr std::string_view StrSendErrors = "SendErrors";
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
static constexpr std::string_view StrHideUpdateMessages = "ImScaredOfUpdates";
// HTTP
static constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
static constexpr std::string_view StrHTTPServerUseSSL = "UseSSL";
static constexpr std::string_view StrSSLKeyPath = "SSLKeyPath";
static constexpr std::string_view StrSSLCertPath = "SSLCertPath";
static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
static constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
TEST_CASE("TConfig::TConfig") {
const std::string CfgFile = "beammp_server_testconfig.toml";
fs::remove(CfgFile);
@ -93,35 +67,35 @@ void SetComment(CommentsT& Comments, const std::string& Comment) {
void TConfig::FlushToFile() {
// auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
auto data = toml::value {};
data["General"][StrAuthKey.data()] = Application::Settings.Key;
data["General"][StrAuthKey.data()] = Application::GetSettingString(StrAuthKey.data());
SetComment(data["General"][StrAuthKey.data()].comments(), " AuthKey has to be filled out in order to run the server");
data["General"][StrLogChat.data()] = Application::Settings.LogChat;
data["General"][StrLogChat.data()] = Application::GetSettingBool(StrLogChat.data());
SetComment(data["General"][StrLogChat.data()].comments(), " Whether to log chat messages in the console / log");
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
data["General"][StrPrivate.data()] = Application::Settings.Private;
data["General"][StrPort.data()] = Application::Settings.Port;
data["General"][StrName.data()] = Application::Settings.ServerName;
data["General"][StrMaxCars.data()] = Application::Settings.MaxCars;
data["General"][StrMaxPlayers.data()] = Application::Settings.MaxPlayers;
data["General"][StrMap.data()] = Application::Settings.MapName;
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
data["General"][StrDebug.data()] = Application::GetSettingBool(StrDebug.data());
data["General"][StrPrivate.data()] = Application::GetSettingBool(StrPrivate.data());
data["General"][StrPort.data()] = Application::GetSettingInt(StrPort.data());
data["General"][StrName.data()] = Application::GetSettingString(StrName.data());
data["General"][StrMaxCars.data()] = Application::GetSettingInt(StrMaxCars.data());
data["General"][StrMaxPlayers.data()] = Application::GetSettingInt(StrMaxPlayers.data());
data["General"][StrMap.data()] = Application::GetSettingString(StrMap.data());
data["General"][StrDescription.data()] = Application::GetSettingString(StrDescription.data());
data["General"][StrResourceFolder.data()] = Application::GetSettingString(StrResourceFolder.data());
// Misc
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages;
data["Misc"][StrHideUpdateMessages.data()] = Application::GetSettingBool(StrHideUpdateMessages.data());
SetComment(data["Misc"][StrHideUpdateMessages.data()].comments(), " Hides the periodic update message which notifies you of a new server version. You should really keep this on and always update as soon as possible. For more information visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server. An update message will always appear at startup regardless.");
data["Misc"][StrSendErrors.data()] = Application::Settings.SendErrors;
data["Misc"][StrSendErrors.data()] = Application::GetSettingBool(StrSendErrors.data());
SetComment(data["Misc"][StrSendErrors.data()].comments(), " You can turn on/off the SendErrors message you get on startup here");
data["Misc"][StrSendErrorsMessageEnabled.data()] = Application::Settings.SendErrorsMessageEnabled;
data["Misc"][StrSendErrorsMessageEnabled.data()] = Application::GetSettingBool(StrSendErrorsMessageEnabled.data());
SetComment(data["Misc"][StrSendErrorsMessageEnabled.data()].comments(), " If SendErrors is `true`, the server will send helpful info about crashes and other issues back to the BeamMP developers. This info may include your config, who is on your server at the time of the error, and similar general information. This kind of data is vital in helping us diagnose and fix issues faster. This has no impact on server performance. You can opt-out of this system by setting this to `false`");
// HTTP
data["HTTP"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath;
data["HTTP"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath;
data["HTTP"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort;
data["HTTP"][StrSSLKeyPath.data()] = Application::GetSettingString(StrSSLKeyPath.data());
data["HTTP"][StrSSLCertPath.data()] = Application::GetSettingString(StrSSLCertPath.data());
data["HTTP"][StrHTTPServerPort.data()] = Application::GetSettingInt(StrHTTPServerPort.data());
SetComment(data["HTTP"][StrHTTPServerIP.data()].comments(), " Which IP to listen on. Pick 0.0.0.0 for a public-facing server with no specific IP, and 127.0.0.1 or 'localhost' for a local server.");
data["HTTP"][StrHTTPServerIP.data()] = Application::Settings.HTTPServerIP;
data["HTTP"][StrHTTPServerUseSSL.data()] = Application::Settings.HTTPServerUseSSL;
data["HTTP"][StrHTTPServerIP.data()] = Application::GetSettingString(StrHTTPServerIP.data());
data["HTTP"][StrHTTPServerUseSSL.data()] = Application::GetSettingBool(StrHTTPServerUseSSL.data());
SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to have enabled for servers which face the internet. With SSL the server will serve https and requires valid key and cert files");
data["HTTP"][StrHTTPServerEnabled.data()] = Application::Settings.HTTPServerEnabled;
data["HTTP"][StrHTTPServerEnabled.data()] = Application::GetSettingBool(StrHTTPServerEnabled.data());
SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server");
std::stringstream Ss;
Ss << "# This is the BeamMP-Server config file.\n"
@ -156,50 +130,41 @@ void TConfig::CreateConfigFile() {
FlushToFile();
}
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue) {
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key) {
if (Table[Category.c_str()][Key.data()].is_string()) {
OutValue = Table[Category.c_str()][Key.data()].as_string();
Application::SetSetting(Key, std::string(Table[Category.c_str()][Key.data()].as_string()));
} else if (Table[Category.c_str()][Key.data()].is_boolean()) {
Application::SetSetting(Key, bool(Table[Category.c_str()][Key.data()].as_boolean()));
} else if (Table[Category.c_str()][Key.data()].is_integer()) {
Application::SetSetting(Key, int(Table[Category.c_str()][Key.data()].as_integer()));
}
}
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, bool& OutValue) {
if (Table[Category.c_str()][Key.data()].is_boolean()) {
OutValue = Table[Category.c_str()][Key.data()].as_boolean();
}
}
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, int& OutValue) {
if (Table[Category.c_str()][Key.data()].is_integer()) {
OutValue = int(Table[Category.c_str()][Key.data()].as_integer());
}
}
void TConfig::ParseFromFile(std::string_view name) {
try {
toml::value data = toml::parse<toml::preserve_comments>(name.data());
// GENERAL
TryReadValue(data, "General", StrDebug, Application::Settings.DebugModeEnabled);
TryReadValue(data, "General", StrPrivate, Application::Settings.Private);
TryReadValue(data, "General", StrPort, Application::Settings.Port);
TryReadValue(data, "General", StrMaxCars, Application::Settings.MaxCars);
TryReadValue(data, "General", StrMaxPlayers, Application::Settings.MaxPlayers);
TryReadValue(data, "General", StrMap, Application::Settings.MapName);
TryReadValue(data, "General", StrName, Application::Settings.ServerName);
TryReadValue(data, "General", StrDescription, Application::Settings.ServerDesc);
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
TryReadValue(data, "General", StrLogChat, Application::Settings.LogChat);
TryReadValue(data, "General", StrDebug);
TryReadValue(data, "General", StrPrivate);
TryReadValue(data, "General", StrPort);
TryReadValue(data, "General", StrMaxCars);
TryReadValue(data, "General", StrMaxPlayers);
TryReadValue(data, "General", StrMap);
TryReadValue(data, "General", StrName);
TryReadValue(data, "General", StrDescription);
TryReadValue(data, "General", StrResourceFolder);
TryReadValue(data, "General", StrAuthKey);
TryReadValue(data, "General", StrLogChat);
// Misc
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, Application::Settings.SendErrorsMessageEnabled);
TryReadValue(data, "Misc", StrSendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages);
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled);
// HTTP
TryReadValue(data, "HTTP", StrSSLKeyPath, Application::Settings.SSLKeyPath);
TryReadValue(data, "HTTP", StrSSLCertPath, Application::Settings.SSLCertPath);
TryReadValue(data, "HTTP", StrHTTPServerPort, Application::Settings.HTTPServerPort);
TryReadValue(data, "HTTP", StrHTTPServerIP, Application::Settings.HTTPServerIP);
TryReadValue(data, "HTTP", StrHTTPServerEnabled, Application::Settings.HTTPServerEnabled);
TryReadValue(data, "HTTP", StrHTTPServerUseSSL, Application::Settings.HTTPServerUseSSL);
TryReadValue(data, "HTTP", StrSSLKeyPath);
TryReadValue(data, "HTTP", StrSSLCertPath);
TryReadValue(data, "HTTP", StrHTTPServerPort);
TryReadValue(data, "HTTP", StrHTTPServerIP);
TryReadValue(data, "HTTP", StrHTTPServerEnabled);
TryReadValue(data, "HTTP", StrHTTPServerUseSSL);
} catch (const std::exception& err) {
beammp_error("Error parsing config file value: " + std::string(err.what()));
mFailed = true;
@ -211,86 +176,28 @@ void TConfig::ParseFromFile(std::string_view name) {
// Update in any case
FlushToFile();
// all good so far, let's check if there's a key
if (Application::Settings.Key.empty()) {
if (Application::GetSettingString(StrAuthKey).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.");
Application::SetSubsystemStatus("Config", Application::Status::Bad);
mFailed = true;
return;
}
Application::SetSubsystemStatus("Config", Application::Status::Good);
if (Application::Settings.Key.size() != 36) {
if (Application::GetSettingString(StrAuthKey).size() != 36) {
beammp_warn("AuthKey specified is the wrong length and likely isn't valid.");
}
}
void TConfig::PrintDebug() {
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));
beammp_debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars));
beammp_debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers));
beammp_debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\"");
beammp_debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\"");
beammp_debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\"");
beammp_debug(std::string(StrLogChat) + ": \"" + (Application::Settings.LogChat ? "true" : "false") + "\"");
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\"");
beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\"");
beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\"");
beammp_debug(std::string(StrHTTPServerIP) + ": \"" + Application::Settings.HTTPServerIP + "\"");
// special!
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
for (const auto& [k, v] : Application::mSettings) {
if (k == StrAuthKey) {
beammp_debugf("AuthKey: length {}", boost::get<std::string>(v).size());
continue;
}
beammp_debugf("{}: {}", k, Application::SettingToString(v));
}
}
void TConfig::ParseOldFormat() {
std::ifstream File("Server.cfg");
// read all, strip comments
std::string Content;
for (;;) {
std::string Line;
std::getline(File, Line);
if (!Line.empty() && Line.at(0) != '#') {
Line = Line.substr(0, Line.find_first_of('#'));
Content += Line + "\n";
}
if (!File.good()) {
break;
}
}
std::stringstream Str(Content);
std::string Key, Ignore, Value;
for (;;) {
Str >> Key >> std::ws >> Ignore >> std::ws;
std::getline(Str, Value);
if (Str.eof()) {
break;
}
std::stringstream ValueStream(Value);
ValueStream >> std::ws; // strip leading whitespace if any
Value = ValueStream.str();
if (Key == "Debug") {
Application::Settings.DebugModeEnabled = Value.find("true") != std::string::npos;
} else if (Key == "Private") {
Application::Settings.Private = Value.find("true") != std::string::npos;
} else if (Key == "Port") {
ValueStream >> Application::Settings.Port;
} else if (Key == "Cars") {
ValueStream >> Application::Settings.MaxCars;
} else if (Key == "MaxPlayers") {
ValueStream >> Application::Settings.MaxPlayers;
} else if (Key == "Map") {
Application::Settings.MapName = Value.substr(1, Value.size() - 3);
} else if (Key == "Name") {
Application::Settings.ServerName = Value.substr(1, Value.size() - 3);
} else if (Key == "Desc") {
Application::Settings.ServerDesc = Value.substr(1, Value.size() - 3);
} else if (Key == "use") {
Application::Settings.Resource = Value.substr(1, Value.size() - 3);
} else if (Key == "AuthKey") {
Application::Settings.Key = Value.substr(1, Value.size() - 3);
} else {
beammp_warn("unknown key in old auth file (ignored): " + Key);
}
Str >> std::ws;
}
beammp_warnf("You still have a 'Server.cfg' - this will not be used (this server uses 'ServerConfig.toml'. Since v3.0.2 we no longer parse and import these old settings. Remove the file to avoid confusion and disable this message.");
}

View File

@ -68,7 +68,7 @@ static std::string GetDate() {
auto local_tm = std::localtime(&tt);
char buf[30];
std::string date;
if (Application::Settings.DebugModeEnabled) {
if (Application::GetSettingBool(StrDebug)) {
std::strftime(buf, sizeof(buf), "[%d/%m/%y %T.", local_tm);
date += buf;
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
@ -360,7 +360,7 @@ void TConsole::Command_Say(const std::string& FullCmd) {
if (FullCmd.size() > 3) {
auto Message = FullCmd.substr(4);
LuaAPI::MP::SendChatMessage(-1, Message);
if (!Application::Settings.LogChat) {
if (!Application::GetSettingBool(StrLogChat)) {
Application::Console().WriteRaw("Chat message sent!");
}
}

View File

@ -36,8 +36,8 @@ void THeartbeatThread::operator()() {
Last = Body;
LastNormalUpdateTime = Now;
if (!Application::Settings.CustomIP.empty()) {
Body += "&ip=" + Application::Settings.CustomIP;
if (!Application::GetSettingString(StrCustomIP).empty()) {
Body += "&ip=" + Application::GetSettingString(StrCustomIP);
}
auto SentryReportError = [&](const std::string& transaction, int status) {
@ -59,7 +59,7 @@ void THeartbeatThread::operator()() {
T = Http::POST(Url, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } });
Doc.Parse(T.data(), T.size());
if (Doc.HasParseError() || !Doc.IsObject()) {
if (!Application::Settings.Private) {
if (!Application::GetSettingBool(StrPrivate)) {
beammp_trace("Backend response failed to parse as valid json");
beammp_trace("Response was: `" + T + "`");
}
@ -105,12 +105,12 @@ void THeartbeatThread::operator()() {
Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__));
}
} else {
if (!Application::Settings.Private) {
if (!Application::GetSettingBool(StrPrivate)) {
beammp_warn("Backend failed to respond to a heartbeat. Your server may temporarily disappear from the server list. This is not an error, and will likely resolve itself soon. Direct connect will still work.");
}
}
if (Ok && !isAuth && !Application::Settings.Private) {
if (Ok && !isAuth && !Application::GetSettingBool(StrPrivate)) {
if (Status == "2000") {
beammp_info(("Authenticated! " + Message));
isAuth = true;
@ -124,10 +124,10 @@ void THeartbeatThread::operator()() {
beammp_error("Backend REFUSED the auth key. Reason: " + Message);
}
}
if (isAuth || Application::Settings.Private) {
if (isAuth || Application::GetSettingBool(StrPrivate)) {
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
}
if (!Application::Settings.HideUpdateMessages && UpdateReminderCounter % 5) {
if (!Application::GetSettingBool(StrHideUpdateMessages) && UpdateReminderCounter % 5) {
Application::CheckForUpdates();
}
}
@ -136,20 +136,20 @@ void THeartbeatThread::operator()() {
std::string THeartbeatThread::GenerateCall() {
std::stringstream Ret;
Ret << "uuid=" << Application::Settings.Key
Ret << "uuid=" << Application::GetSettingString(StrAuthKey)
<< "&players=" << mServer.ClientCount()
<< "&maxplayers=" << Application::Settings.MaxPlayers
<< "&port=" << Application::Settings.Port
<< "&map=" << Application::Settings.MapName
<< "&private=" << (Application::Settings.Private ? "true" : "false")
<< "&maxplayers=" << Application::GetSettingInt(StrMaxPlayers)
<< "&port=" << Application::GetSettingInt(StrPort)
<< "&map=" << Application::GetSettingString(StrMap)
<< "&private=" << (Application::GetSettingBool(StrPrivate) ? "true" : "false")
<< "&version=" << Application::ServerVersionString()
<< "&clientversion=" << std::to_string(Application::ClientMajorVersion()) + ".0" // FIXME: Wtf.
<< "&name=" << Application::Settings.ServerName
<< "&name=" << Application::GetSettingString(StrName)
<< "&modlist=" << mResourceManager.TrimmedList()
<< "&modstotalsize=" << mResourceManager.MaxModSize()
<< "&modstotal=" << mResourceManager.ModsLoaded()
<< "&playerslist=" << GetPlayers()
<< "&desc=" << Application::Settings.ServerDesc;
<< "&desc=" << Application::GetSettingString(StrDescription);
return Ret.str();
}
THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& Server)

View File

@ -16,11 +16,11 @@
TLuaEngine* LuaAPI::MP::Engine;
TLuaEngine::TLuaEngine()
: mResourceServerPath(fs::path(Application::Settings.Resource) / "Server") {
: mResourceServerPath(fs::path(Application::GetSettingString(StrResourceFolder)) / "Server") {
Application::SetSubsystemStatus("LuaEngine", Application::Status::Starting);
LuaAPI::MP::Engine = this;
if (!fs::exists(Application::Settings.Resource)) {
fs::create_directory(Application::Settings.Resource);
if (!fs::exists(Application::GetSettingString(StrResourceFolder))) {
fs::create_directory(Application::GetSettingString(StrResourceFolder));
}
if (!fs::exists(mResourceServerPath)) {
fs::create_directory(mResourceServerPath);
@ -36,7 +36,7 @@ TLuaEngine::TLuaEngine()
}
TEST_CASE("TLuaEngine ctor & dtor") {
Application::Settings.Resource = "beammp_server_test_resources";
Application::SetSetting(StrResourceFolder, "beammp_server_test_resources");
TLuaEngine engine;
Application::GracefullyShutdown();
}

View File

@ -60,7 +60,7 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
void TNetwork::UDPServerMain() {
RegisterThread("UDPServer");
ip::udp::endpoint UdpListenEndpoint(ip::address::from_string("0.0.0.0"), Application::Settings.Port);
ip::udp::endpoint UdpListenEndpoint(ip::address::from_string("0.0.0.0"), Application::GetSettingInt(StrPort));
boost::system::error_code ec;
mUDPSock.open(UdpListenEndpoint.protocol(), ec);
if (ec) {
@ -75,8 +75,8 @@ void TNetwork::UDPServerMain() {
Application::GracefullyShutdown();
}
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Good);
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::GetSettingInt(StrPort)) + (" with a Max of ")
+ std::to_string(Application::GetSettingInt(StrMaxPlayers)) + (" Clients"));
while (!Application::IsShuttingDown()) {
try {
ip::udp::endpoint client {};
@ -113,7 +113,7 @@ void TNetwork::UDPServerMain() {
void TNetwork::TCPServerMain() {
RegisterThread("TCPServer");
ip::tcp::endpoint ListenEp(ip::address::from_string("0.0.0.0"), Application::Settings.Port);
ip::tcp::endpoint ListenEp(ip::address::from_string("0.0.0.0"), Application::GetSettingInt(StrPort));
ip::tcp::socket Listener(mServer.IoCtx());
boost::system::error_code ec;
Listener.open(ListenEp.protocol(), ec);
@ -321,7 +321,7 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
return {};
}
if (mServer.ClientCount() < size_t(Application::Settings.MaxPlayers)) {
if (mServer.ClientCount() < size_t(Application::GetSettingInt(StrMaxPlayers))) {
beammp_info("Identification success");
mServer.InsertClient(Client);
TCPClient(Client);
@ -507,7 +507,7 @@ void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
}
void TNetwork::UpdatePlayer(TClient& Client) {
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":";
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::GetSettingInt(StrMaxPlayers)) + ":";
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
ReadLock Lock(mServer.GetClientMutex());
if (!ClientPtr.expired()) {
@ -575,7 +575,7 @@ void TNetwork::OnConnect(const std::weak_ptr<TClient>& c) {
SyncResources(*LockedClient);
if (LockedClient->IsDisconnected())
return;
(void)Respond(*LockedClient, StringToVector("M" + Application::Settings.MapName), true); // Send the Map on connect
(void)Respond(*LockedClient, StringToVector("M" + Application::GetSettingString(StrMap)), true); // Send the Map on connect
beammp_info(LockedClient->GetName() + " : Connected");
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoining", "", LockedClient->GetID()));
}
@ -634,7 +634,7 @@ void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
return;
}
auto FileName = fs::path(UnsafeName).filename().string();
FileName = Application::Settings.Resource + "/Client/" + FileName;
FileName = Application::GetSettingString(StrResourceFolder) + "/Client/" + FileName;
if (!std::filesystem::exists(FileName)) {
if (!TCPSend(c, StringToVector("CO"))) {

View File

@ -7,7 +7,7 @@ namespace fs = std::filesystem;
TResourceManager::TResourceManager() {
Application::SetSubsystemStatus("ResourceManager", Application::Status::Starting);
std::string Path = Application::Settings.Resource + "/Client";
std::string Path = Application::GetSettingString(StrResourceFolder) + "/Client";
if (!fs::exists(Path))
fs::create_directories(Path);
for (const auto& entry : fs::directory_iterator(Path)) {

View File

@ -28,22 +28,22 @@ TSentry::~TSentry() {
void TSentry::PrintWelcome() {
if (mValid) {
if (!Application::Settings.SendErrors) {
if (!Application::GetSettingBool("SendErrors")) {
mValid = false;
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Opted out of error reporting (SendErrors), Sentry disabled.");
} else {
beammp_info("Sentry disabled");
}
} else {
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Sentry started! Reporting errors automatically. This sends data to the developers in case of errors and crashes. You can learn more, turn this message off or opt-out of this in the ServerConfig.toml.");
} else {
beammp_info("Sentry started");
}
}
} else {
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Sentry disabled in unofficial build. Automatic error reporting disabled.");
} else {
beammp_info("Sentry disabled in unofficial build");
@ -57,8 +57,8 @@ void TSentry::SetupUser() {
}
Application::SetSubsystemStatus("Sentry", Application::Status::Good);
sentry_value_t user = sentry_value_new_object();
if (Application::Settings.Key.size() == 36) {
sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::Settings.Key.c_str()));
if (Application::GetSettingString(StrAuthKey).size() == 36) {
sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::GetSettingString(StrAuthKey).c_str()));
} else {
sentry_value_set_by_key(user, "id", sentry_value_new_string("unauthenticated"));
}

View File

@ -80,11 +80,12 @@ TServer::TServer(const std::vector<std::string_view>& Arguments) {
beammp_info("BeamMP Server v" + Application::ServerVersionString());
Application::SetSubsystemStatus("Server", Application::Status::Starting);
if (Arguments.size() > 1) {
Application::Settings.CustomIP = Arguments[0];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');
auto p = Application::Settings.CustomIP.find_first_not_of(".0123456789");
if (p != std::string::npos || n != 3 || Application::Settings.CustomIP.substr(0, 3) == "127") {
Application::Settings.CustomIP.clear();
Application::SetSetting(StrCustomIP, std::string(Arguments[0]));
auto CustomIP = Application::GetSettingString(StrCustomIP);
size_t n = std::count(CustomIP.begin(), CustomIP.end(), '.');
auto p = Application::GetSettingString(StrCustomIP).find_first_not_of(".0123456789");
if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == "127") {
Application::SetSetting(StrCustomIP, "");
beammp_warn("IP Specified is invalid! Ignoring");
} else {
beammp_info("server started with custom IP");
@ -235,7 +236,7 @@ bool TServer::ShouldSpawn(TClient& c, const std::string& CarJson, int ID) {
c.SetUnicycleID(ID);
return true;
} else {
return c.GetCarCount() < Application::Settings.MaxCars;
return c.GetCarCount() < Application::GetSettingInt(StrMaxCars);
}
}

View File

@ -158,9 +158,9 @@ int BeamMPServerMain(MainArguments Arguments) {
PPSMonitor.SetNetwork(Network);
Application::CheckForUpdates();
TPluginMonitor PluginMonitor(fs::path(Application::Settings.Resource) / "Server", LuaEngine);
TPluginMonitor PluginMonitor(fs::path(Application::GetSettingString(StrResourceFolder)) / "Server", LuaEngine);
if (Application::Settings.HTTPServerEnabled) {
if (Application::GetSettingBool(StrHTTPServerEnabled)) {
Http::Server::THttpServerInstance HttpServerInstance {};
}