Compare commits

..

8 Commits

Author SHA1 Message Date
Lion Kortlepel 2d8c12ccd6 add scripts & dockerfiles to build on multiple platforms 2022-10-23 22:52:46 +02:00
Lion Kortlepel 65e0fd61ba add boost to vcpkg requirements 2022-10-23 18:04:45 +02:00
Lion Kortlepel 5cac6da14e add settings to changelog 2022-10-23 17:48:07 +02:00
Lion Kortlepel d003608503 add settings set/get/list/help console command
this makes use of the new settings-in-a-hashmap in order to make
changing any settings (except authkey) easy.
2022-10-23 17:45:52 +02:00
Lion Kortlepel 35fdb2f8fc bump version to 3.2.0 2022-10-23 17:45:42 +02:00
Lion Kortlepel 10efab4d71 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.
2022-10-23 17:44:57 +02:00
Lion Kortlepel 8472160493 fix EnsureArgsCount not properly printing min/max 2022-10-23 13:57:15 +02:00
Lion Kortlepel 186903be7f bump version to 3.1.1
I'm expecting to release a 3.1.1 with some fixes
2022-10-23 13:48:13 +02:00
27 changed files with 439 additions and 372 deletions
+2 -2
View File
@@ -18,9 +18,9 @@ jobs:
uses: lukka/run-vcpkg@v7
id: runvcpkg
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl boost-variant boost-spirit boost-phoenix boost-core boost-system boost-asio'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: "16ee2ecb31788c336ace8bb14c21801efb6836e4"
vcpkgGitCommitId: "06b5f4a769d848d1a20fa0acd556019728b56273"
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment
+2 -2
View File
@@ -83,9 +83,9 @@ jobs:
uses: lukka/run-vcpkg@v7
id: runvcpkg
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl boost-variant boost-spirit boost-phoenix boost-core boost-system boost-asio'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '16ee2ecb31788c336ace8bb14c21801efb6836e4'
vcpkgGitCommitId: '06b5f4a769d848d1a20fa0acd556019728b56273'
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment
+13 -14
View File
@@ -28,8 +28,6 @@ set(SENTRY_BUILD_SHARED_LIBS OFF)
add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT=1)
option(WIN32_STATIC_RUNTIME "Build statically-linked runtime on windows (don't touch unless you know what you're doing)" ON)
# ------------------------ APPLE ---------------------------------
if(APPLE)
if(IS_DIRECTORY /opt/homebrew/Cellar/lua@5.3/5.3.6)
@@ -48,14 +46,18 @@ if(APPLE)
link_directories(/usr/local/opt/openssl@1.1/lib)
endif()
# ------------------------ WINDOWS ---------------------------------
option(WIN32_STATIC_RUNTIME "Build statically-linked runtime on windows (don't touch unless you know what you're doing)" ON)
elseif (WIN32)
# this has to happen before sentry, so that crashpad on windows links with these settings.
if (WIN32_STATIC_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
message(STATUS "MSVC -> forcing use of statically-linked runtime.")
STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
endif()
# ------------------------ UNIX ------------------------------------
# ------------------------ LINUX ---------------------------------
elseif (UNIX)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin")
option(SANITIZE "Turns on thread and UB sanitizers" OFF)
if (SANITIZE)
message(STATUS "sanitize is ON")
@@ -80,6 +82,9 @@ add_subdirectory("deps/sentry-native")
# ------------------------ C++ SETUP ---------------------------------
set(CMAKE_CXX_STANDARD 17)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif ()
# ------------------------ DEPENDENCIES ------------------------------
message(STATUS "Adding local source dependencies")
@@ -161,8 +166,8 @@ if (UNIX)
-Werror=missing-field-initializers
-Werror=write-strings
-Werror=ctor-dtor-privacy
-Werror=switch-enum
-Werror=switch-default
-Wswitch-enum
-Wswitch-default
-Werror=old-style-cast
-Werror=overloaded-virtual
-Werror=overloaded-virtual
@@ -172,12 +177,6 @@ if (UNIX)
-fstack-protector
-Wzero-as-null-pointer-constant
)
else()
set(BeamMP_CompileOptions
/bigobj
/INCREMENTAL:NO /NODEFAULTLIB:MSVCRT /NODEFAULTLIB:LIBCMT
)
endif()
set(BeamMP_Libraries
@@ -223,7 +222,7 @@ target_include_directories(BeamMP-Server SYSTEM PRIVATE
${BeamMP_Includes}
)
target_link_libraries(BeamMP-Server
target_link_libraries(BeamMP-Server
${BeamMP_Libraries}
${BeamMP_PlatformLibs}
)
+2 -6
View File
@@ -1,10 +1,6 @@
# v3.1.1
# v3.2.0
- FIXED bug which caused GetPlayerIdentifiers, GetPlayerName, etc not to work in `onPlayerDisconnect`
- FIXED some issues which could cause the server to crash when receiving malformed data
- FIXED a bug which caused a server to crash during authentication when receiving malformed data
- FIXED minor vulnerability in chat message handling
- FIXED a minor formatting bug in the `status` command
- ADDED `settings` command, which lets you `get`, `list`, and `set` config options from the console
# v3.1.0
+5
View File
@@ -107,6 +107,11 @@ curl
##### Build Dependencies
These are needed for you to *build* the server, in addition to the [runtime dependencies](#runtime-dependencies).
**Ubuntu 22.04**
```
```
<details>
<summary>
Ubuntu 22.04
+53 -30
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);
@@ -137,7 +160,7 @@ private:
static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 3, 1, 2 };
static inline Version mVersion { 3, 2, 0 };
};
std::string ThreadName(bool DebugModeOverride = false);
@@ -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)
+1 -3
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();
+2 -2
View File
@@ -76,7 +76,7 @@ public:
};
TLuaEngine();
virtual ~TLuaEngine() noexcept {
~TLuaEngine() noexcept {
beammp_debug("Lua Engine terminated");
}
@@ -198,7 +198,7 @@ private:
public:
StateThreadData(const std::string& Name, TLuaStateId StateId, TLuaEngine& Engine);
StateThreadData(const StateThreadData&) = delete;
virtual ~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const TLuaChunk& Script);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy);
+3
View File
@@ -0,0 +1,3 @@
FROM archlinux
RUN pacman -Syu --noconfirm lua53 openssl curl git cmake gcc make zlib boost websocketpp
+9
View File
@@ -0,0 +1,9 @@
FROM debian:11
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y git cmake g++-10 curl libboost1.74-all-dev libssl-dev libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev
ENV CXX=g++-10
+9
View File
@@ -0,0 +1,9 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev git make cmake g++
RUN apt-get install -y libboost1.71-all-dev
+9
View File
@@ -0,0 +1,9 @@
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y git libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev cmake g++-10 libboost1.74-all-dev libssl3 curl
ENV CXX=g++-10
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
set -e
printf "enter DSN (optional): "
read DSN
docker build -f Ubuntu-20.04-Dockerfile . -t beammp-server-build:Ubuntu-20.04
docker build -f Ubuntu-22.04-Dockerfile . -t beammp-server-build:Ubuntu-22.04
docker build -f ArchLinux-Dockerfile . -t beammp-server-build:ArchLinux
docker build -f Debian-11-Dockerfile . -t beammp-server-build:Debian-11
CMD="cd /beammp; cmake . -DGIT_SUBMODULE=OFF -DCMAKE_BUILD_TYPE=Release -DBEAMMP_SECRET_SENTRY_URL=\"${DSN}\" -B /build && make -j -C /build BeamMP-Server"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-ubuntu-20.04:/build -it --rm beammp-server-build:Ubuntu-20.04 bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-ubuntu-22.04:/build -it --rm beammp-server-build:Ubuntu-22.04 bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-archlinux:/build -it --rm beammp-server-build:ArchLinux bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-debian-11:/build -it --rm beammp-server-build:Debian-11 bash -c "${CMD}"
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
cmake . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_SUBMODULE=OFF
make -j -C build BeamMP-Server
-1
View File
@@ -6,7 +6,6 @@
#include <optional>
void TClient::DeleteCar(int Ident) {
// TODO: Send delete packets
std::unique_lock lock(mVehicleDataMutex);
auto iter = std::find_if(mVehicleData.begin(), mVehicleData.end(), [&](auto& elem) {
return Ident == elem.ID();
+81 -8
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] ";
+2 -2
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.");
}
+14 -14
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");
}
+56 -149
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.");
}
+75 -6
View File
@@ -7,9 +7,14 @@
#include "LuaAPI.h"
#include "TLuaEngine.h"
#include <boost/spirit/home/qi/directive/lexeme.hpp>
#include <boost/spirit/home/qi/parse.hpp>
#include <ctime>
#include <sstream>
#include <boost/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
static inline bool StringStartsWith(const std::string& What, const std::string& StartsWith) {
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
}
@@ -68,7 +73,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);
@@ -350,17 +355,81 @@ std::tuple<std::string, std::vector<std::string>> TConsole::ParseCommand(const s
}
void TConsole::Command_Settings(const std::string&, const std::vector<std::string>& args) {
if (!EnsureArgsCount(args, 1, 2)) {
if (!EnsureArgsCount(args, 1, 100)) {
return;
}
static const char* SETTINGS_HELP = R"(Settings:
settings help Displays this help
settings list Lists all settings
settings get <setting> Prints the current value of the specified setting
settings set <setting> <value> Sets the specified setting to the value)";
if (args.at(0) == "help") {
Application::Console().WriteRaw(SETTINGS_HELP);
} else if (args.at(0) == "list") {
Application::Console().WriteRaw("Available settings:");
Application::Console().WriteRaw(fmt::format("\t{:<25} {}", "<NAME>", "<CURRENT VALUE>"));
for (const auto& [k, v] : Application::mSettings) {
if (k == StrAuthKey) {
Application::Console().WriteRaw(fmt::format("\t{:<25} <key of length {}>", k, Application::SettingToString(v).size()));
} else {
Application::Console().WriteRaw(fmt::format("\t{:<25} {}", k, Application::SettingToString(v)));
}
}
} else if (args.at(0) == "get") {
if (args.size() < 2) {
Application::Console().WriteRaw("Not enough arguments: `settings get` requires a setting name.");
} else {
if (Application::mSettings.contains(args.at(1))) {
if (args.at(1) != StrAuthKey) {
Application::Console().WriteRaw(fmt::format("{} = {}", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1)))));
} else {
Application::Console().WriteRaw(fmt::format("{} = <key of length {}>", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1))).size()));
}
} else {
Application::Console().WriteRaw(fmt::format("Setting '{}' doesn't exist.", args.at(1)));
}
}
} else if (args.at(0) == "set") {
if (args.size() < 3) {
Application::Console().WriteRaw("Not enough arguments: `settings set` requires a setting name and value.");
} else {
if (args.at(1) == StrAuthKey) {
Application::Console().WriteRaw("It's not allowed to set the AuthKey during runtime.");
} else {
using namespace boost::spirit;
using qi::_1;
std::string ValueString = args.at(2);
Application::SettingValue Value;
qi::rule<std::string::iterator, std::string()> StringRule;
StringRule
%= qi::lexeme['"' >> *(qi::char_ - '"') >> '"' ]
| +(qi::char_ - '"')
;
qi::rule<std::string::iterator, Application::SettingValue()> ValueRule
= qi::bool_
| qi::int_
| StringRule;
auto It = ValueString.begin();
if (qi::phrase_parse(It, ValueString.end(), ValueRule[boost::phoenix::ref(Value) = _1], ascii::space)
&& It == ValueString.end()) {
Application::SetSetting(args.at(1), Value);
Application::Console().WriteRaw(fmt::format("{} := {}", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1)))));
} else {
Application::Console().WriteRaw(fmt::format("New value '{}' did not parse as a valid value.", ValueString));
}
}
}
} else {
Application::Console().WriteRaw(fmt::format("Unknown argument '{}' - 'settings {}' is not a valid command.", args.at(0), args.at(0)));
}
}
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!");
}
}
@@ -479,12 +548,12 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
<< "\t\tEvent handlers: " << mLuaEngine->GetRegisteredEventHandlerCount() << "\n"
<< "\tSubsystems:\n"
<< "\t\tGood/Starting/Bad: " << SystemsGood << "/" << SystemsStarting << "/" << SystemsBad << "\n"
<< "\t\tShutting down/Shut down: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
<< "\t\tShutting down/Shut down: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
<< "\t\tGood: [ " << SystemsGoodList << " ]\n"
<< "\t\tStarting: [ " << SystemsStartingList << " ]\n"
<< "\t\tBad: [ " << SystemsBadList << " ]\n"
<< "\t\tShutting down: [ " << SystemsShuttingDownList << " ]\n"
<< "\t\tShut down: [ " << SystemsShutdownList << " ]\n"
<< "\t\tShut down: [ " << SystemsShutdownList << " ]\n"
<< "";
Application::Console().WriteRaw(Status.str());
+14 -14
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)
+4 -4
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();
}
+32 -70
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);
@@ -177,29 +177,16 @@ void TNetwork::Identify(TConnection&& RawConnection) {
return;
}
std::shared_ptr<TClient> Client { nullptr };
try {
if (Code == 'C') {
Client = Authentication(std::move(RawConnection));
} else if (Code == 'D') {
HandleDownload(std::move(RawConnection));
} else if (Code == 'P') {
boost::system::error_code ec;
write(RawConnection.Socket, buffer("P"), ec);
return;
} else {
beammp_errorf("Invalid code got in Identify: '{}'", Code);
}
} catch(const std::exception& e) {
beammp_errorf("Error during handling of code {} - client left in invalid state, closing socket", Code);
if (Code == 'C') {
Client = Authentication(std::move(RawConnection));
} else if (Code == 'D') {
HandleDownload(std::move(RawConnection));
} else if (Code == 'P') {
boost::system::error_code ec;
RawConnection.Socket.shutdown(socket_base::shutdown_both, ec);
if (ec) {
beammp_debugf("Failed to shutdown client socket: {}", ec.message());
}
RawConnection.Socket.close(ec);
if (ec) {
beammp_debugf("Failed to close client socket: {}", ec.message());
}
write(RawConnection.Socket, buffer("P"), ec);
return;
} else {
beammp_errorf("Invalid code got in Identify: '{}'", Code);
}
}
@@ -259,24 +246,12 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
return nullptr;
}
std::string key(reinterpret_cast<const char*>(Data.data()), Data.size());
nlohmann::json AuthReq{};
std::string AuthResStr{};
try {
AuthReq = nlohmann::json {
{ "key", key }
};
auto Target = "/pkToUser";
unsigned int ResponseCode = 0;
AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode);
} catch (const std::exception& e) {
beammp_debugf("Invalid json sent by client, kicking: {}", e.what());
ClientKick(*Client, "Invalid Key (invalid UTF8 string)!");
return nullptr;
}
nlohmann::json AuthReq {
{ "key", std::string(reinterpret_cast<const char*>(Data.data()), Data.size()) }
};
auto Target = "/pkToUser";
unsigned int ResponseCode = 0;
const auto AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode);
try {
nlohmann::json AuthRes = nlohmann::json::parse(AuthResStr);
@@ -346,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);
@@ -418,12 +393,6 @@ std::vector<uint8_t> TNetwork::TCPRcv(TClient& c) {
}
Header = *reinterpret_cast<int32_t*>(HeaderData.data());
if (Header < 0) {
ClientKick(c, "Invalid packet - header negative");
beammp_errorf("Client {} send negative TCP header, ignoring packet", c.GetID());
return {};
}
std::vector<uint8_t> Data;
// TODO: This is arbitrary, this needs to be handled another way
if (Header < int32_t(100 * MB)) {
@@ -456,7 +425,7 @@ std::vector<uint8_t> TNetwork::TCPRcv(TClient& c) {
void TNetwork::ClientKick(TClient& c, const std::string& R) {
beammp_info("Client kicked: " + R);
if (!TCPSend(c, StringToVector("K" + R))) {
beammp_debugf("tried to kick player '{}' (id {}), but was already disconnected", c.GetName(), c.GetID());
beammp_debugf("tried to kick player '{}' (id {}), but was already connected", c.GetName(), c.GetID());
}
c.Disconnect("Kicked");
}
@@ -538,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()) {
@@ -553,14 +522,8 @@ void TNetwork::UpdatePlayer(TClient& Client) {
}
void TNetwork::OnDisconnect(const std::weak_ptr<TClient>& ClientPtr) {
std::shared_ptr<TClient> LockedClientPtr { nullptr };
try {
LockedClientPtr = ClientPtr.lock();
} catch (const std::exception&) {
beammp_warn("Client expired in OnDisconnect, this is unexpected");
return;
}
beammp_assert(LockedClientPtr != nullptr);
beammp_assert(!ClientPtr.expired());
auto LockedClientPtr = ClientPtr.lock();
TClient& c = *LockedClientPtr;
beammp_info(c.GetName() + (" Connection Terminated"));
std::string Packet;
@@ -577,7 +540,7 @@ void TNetwork::OnDisconnect(const std::weak_ptr<TClient>& ClientPtr) {
SendToAll(&c, StringToVector(Packet), false, true);
Packet.clear();
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onPlayerDisconnect", "", c.GetID());
LuaAPI::MP::Engine->WaitForAll(Futures);
LuaAPI::MP::Engine->ReportErrors(Futures);
c.Disconnect("Already Disconnected (OnDisconnect)");
mServer.RemoveClient(ClientPtr);
}
@@ -612,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()));
}
@@ -671,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"))) {
@@ -897,13 +860,12 @@ void TNetwork::SendToAll(TClient* c, const std::vector<uint8_t>& Data, bool Self
bool ret = true;
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool {
std::shared_ptr<TClient> Client;
try {
{
ReadLock Lock(mServer.GetClientMutex());
Client = ClientPtr.lock();
} catch (const std::exception&) {
// continue
beammp_warn("Client expired, shouldn't happen - if a client disconnected recently, you can ignore this");
return true;
if (!ClientPtr.expired()) {
Client = ClientPtr.lock();
} else
return true;
}
if (Self || Client.get() != c) {
if (Client->IsSynced() || Client->IsSyncing()) {
+1 -1
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)) {
+6 -6
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"));
}
+19 -36
View File
@@ -1,7 +1,6 @@
#include "TServer.h"
#include "Client.h"
#include "Common.h"
#include "CustomAssert.h"
#include "TNetwork.h"
#include "TPPSMonitor.h"
#include <TLuaPlugin.h>
@@ -81,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");
@@ -95,20 +95,13 @@ TServer::TServer(const std::vector<std::string_view>& Arguments) {
}
void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
std::shared_ptr<TClient> LockedClientPtr { nullptr };
try {
LockedClientPtr = WeakClientPtr.lock();
} catch (const std::exception&) {
// silently fail, as there's nothing to do
return;
if (!WeakClientPtr.expired()) {
TClient& Client = *WeakClientPtr.lock();
beammp_debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
Client.ClearCars();
WriteLock Lock(mClientsMutex);
mClients.erase(WeakClientPtr.lock());
}
beammp_assert(LockedClientPtr != nullptr);
TClient& Client = *LockedClientPtr;
beammp_debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
// TODO: Send delete packets for all cars
Client.ClearCars();
WriteLock Lock(mClientsMutex);
mClients.erase(WeakClientPtr.lock());
}
void TServer::ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn) {
@@ -175,20 +168,14 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
}
ParseVehicle(*LockedClient, StringPacket, Network);
return;
case 'J':
Network.SendToAll(LockedClient.get(), Packet, false, true);
return;
case 'C': {
if (Packet.size() < 4 || std::find(Packet.begin() + 3, Packet.end(), ':') == Packet.end())
break;
const auto PacketAsString = std::string(reinterpret_cast<const char*>(Packet.data()), Packet.size());
std::string Message = "";
const auto ColonPos = PacketAsString.find(':', 3);
if (ColonPos != std::string::npos && ColonPos + 2 < PacketAsString.size()) {
Message = PacketAsString.substr(ColonPos + 2);
}
if (Message.empty()) {
beammp_debugf("Empty chat message received from '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
return;
}
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Message);
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), PacketAsString.substr(PacketAsString.find(':', 3) + 2));
TLuaEngine::WaitForAll(Futures);
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), PacketAsString.substr(PacketAsString.find(':', 3) + 1));
if (std::any_of(Futures.begin(), Futures.end(),
@@ -199,8 +186,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
})) {
break;
}
std::string SanitizedPacket = fmt::format("C:{}: {}", LockedClient->GetName(), Message);
Network.SendToAll(nullptr, StringToVector(SanitizedPacket), true, true);
Network.SendToAll(nullptr, Packet, true, true);
return;
}
case 'E':
@@ -213,6 +199,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
case 'Z': // position packet
PPSMonitor.IncrementInternalPPS();
Network.SendToAll(LockedClient.get(), Packet, false, false);
HandlePosition(*LockedClient, StringPacket);
default:
return;
@@ -222,10 +209,6 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
void TServer::HandleEvent(TClient& c, const std::string& RawData) {
// E:Name:Data
// Data is allowed to have ':'
if (RawData.size() < 2) {
beammp_debugf("Client '{}' ({}) tried to send an empty event, ignoring", c.GetName(), c.GetID());
return;
}
auto NameDataSep = RawData.find(':', 2);
if (NameDataSep == std::string::npos) {
beammp_warn("received event in invalid format (missing ':'), got: '" + RawData + "'");
@@ -253,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);
}
}
+2 -2
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 {};
}