From 2cfb27820a1407e0a425be35b578a87ae1e1308e Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Fri, 25 Jun 2021 01:59:38 +0200 Subject: [PATCH] switch to toml11 it's better, believe me --- .gitmodules | 8 +-- CMakeLists.txt | 5 +- include/TConfig.h | 2 + include/toml11 | 1 + include/tomlplusplus | 1 - src/TConfig.cpp | 117 +++++++++++++++++-------------------------- src/TLuaFile.cpp | 36 ------------- 7 files changed, 58 insertions(+), 112 deletions(-) create mode 160000 include/toml11 delete mode 160000 include/tomlplusplus diff --git a/.gitmodules b/.gitmodules index e0dfe36..a9df475 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,9 +10,9 @@ [submodule "rapidjson"] path = rapidjson url = https://github.com/Tencent/rapidjson -[submodule "include/tomlplusplus"] - path = include/tomlplusplus - url = https://github.com/marzer/tomlplusplus +[submodule "include/toml11"] + path = include/toml11 + url = https://github.com/ToruNiina/toml11 [submodule "include/sentry-native"] path = include/sentry-native - url = https://github.com/getsentry/sentry-native + url = https://github.com/getsentry/sentry-native \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f9f672..cd6a6ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.0) + message(STATUS "You can find build instructions and a list of dependencies in the README at \ https://github.com/BeamMP/BeamMP-Server") @@ -25,6 +26,7 @@ add_subdirectory("include/sentry-native") message(STATUS "Setting compiler flags") if (WIN32) + #-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}) include_directories(${VcpkgRoot}/include) @@ -60,6 +62,7 @@ add_subdirectory("socket.io-client-cpp") add_subdirectory("include/commandline") set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") message(STATUS "Looking for Boost") @@ -131,4 +134,4 @@ elseif (WIN32) commandline sioclient_tls sentry) -endif () +endif () \ No newline at end of file diff --git a/include/TConfig.h b/include/TConfig.h index ffb2b53..60001bd 100644 --- a/include/TConfig.h +++ b/include/TConfig.h @@ -10,6 +10,8 @@ public: [[nodiscard]] bool Failed() const { return mFailed; } + void FlushToFile(); + private: void CreateConfigFile(std::string_view name); void ParseFromFile(std::string_view name); diff --git a/include/toml11 b/include/toml11 new file mode 160000 index 0000000..6473810 --- /dev/null +++ b/include/toml11 @@ -0,0 +1 @@ +Subproject commit 647381020ef04b5d41d540ec489eba45e82d90a7 diff --git a/include/tomlplusplus b/include/tomlplusplus deleted file mode 160000 index bc6891e..0000000 --- a/include/tomlplusplus +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bc6891e1fbec0b137ac5795542d728a1ad124c11 diff --git a/src/TConfig.cpp b/src/TConfig.cpp index d2a0696..8a2d9f4 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -1,4 +1,6 @@ -#include // header-only version of TOML++ +#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT + +#include // header-only version of TOML++ #include "TConfig.h" #include @@ -32,6 +34,23 @@ TConfig::TConfig() { } } +void TConfig::FlushToFile() { + auto data = toml::parse(ConfigFileName); + data["General"] = toml::table(); + data["General"][StrAuthKey.data()] = Application::Settings.Key; + 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; + std::ofstream Stream(ConfigFileName); + Stream << data << std::flush; +} + void TConfig::CreateConfigFile(std::string_view name) { // build from old config Server.cfg @@ -44,32 +63,31 @@ void TConfig::CreateConfigFile(std::string_view name) { error("an error occurred and was ignored during config transfer: " + std::string(e.what())); } - toml::table tbl { { + { // create file context + std::ofstream ofs(name.data()); + } - { "General", - toml::table { { + auto data = toml::parse(name.data()); - { StrDebug, Application::Settings.DebugModeEnabled }, - { StrPrivate, Application::Settings.Private }, - { StrPort, Application::Settings.Port }, - { StrMaxCars, Application::Settings.MaxCars }, - { StrMaxPlayers, Application::Settings.MaxPlayers }, - { StrMap, Application::Settings.MapName }, - { StrName, Application::Settings.ServerName }, - { StrDescription, Application::Settings.ServerDesc }, - { StrResourceFolder, Application::Settings.Resource }, - { StrAuthKey, Application::Settings.Key }, + data["General"] = toml::table(); + data["General"][StrAuthKey.data()] = Application::Settings.Key; + 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; - } } }, - - } }; std::ofstream ofs { std::string(name) }; if (ofs.good()) { ofs << "# This is the BeamMP-Server config file.\n" "# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\n" "# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n" << '\n'; - ofs << tbl << '\n'; + ofs << data << '\n'; error("There was no \"" + std::string(ConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on."); mFailed = true; } else { @@ -80,58 +98,17 @@ void TConfig::CreateConfigFile(std::string_view name) { void TConfig::ParseFromFile(std::string_view name) { try { - toml::table FullTable = toml::parse_file(name); - toml::table GeneralTable = *FullTable["General"].as_table(); - if (auto val = GeneralTable[StrDebug].value(); val.has_value()) { - Application::Settings.DebugModeEnabled = val.value(); - } else { - throw std::runtime_error(std::string(StrDebug)); - } - if (auto val = GeneralTable[StrPrivate].value(); val.has_value()) { - Application::Settings.Private = val.value(); - } else { - throw std::runtime_error(std::string(StrPrivate)); - } - if (auto val = GeneralTable[StrPort].value(); val.has_value()) { - Application::Settings.Port = val.value(); - } else { - throw std::runtime_error(std::string(StrPort)); - } - if (auto val = GeneralTable[StrMaxCars].value(); val.has_value()) { - Application::Settings.MaxCars = val.value(); - } else { - throw std::runtime_error(std::string(StrMaxCars)); - } - if (auto val = GeneralTable[StrMaxPlayers].value(); val.has_value()) { - Application::Settings.MaxPlayers = val.value(); - } else { - throw std::runtime_error(std::string(StrMaxPlayers)); - } - if (auto val = GeneralTable[StrMap].value(); val.has_value()) { - Application::Settings.MapName = val.value(); - } else { - throw std::runtime_error(std::string(StrMap)); - } - if (auto val = GeneralTable[StrName].value(); val.has_value()) { - Application::Settings.ServerName = val.value(); - } else { - throw std::runtime_error(std::string(StrName)); - } - if (auto val = GeneralTable[StrDescription].value(); val.has_value()) { - Application::Settings.ServerDesc = val.value(); - } else { - throw std::runtime_error(std::string(StrDescription)); - } - if (auto val = GeneralTable[StrResourceFolder].value(); val.has_value()) { - Application::Settings.Resource = val.value(); - } else { - throw std::runtime_error(std::string(StrResourceFolder)); - } - if (auto val = GeneralTable[StrAuthKey].value(); val.has_value()) { - Application::Settings.Key = val.value(); - } else { - throw std::runtime_error(std::string(StrAuthKey)); - } + toml::value data = toml::parse(name.data()); + Application::Settings.DebugModeEnabled = data["General"][StrDebug.data()].as_boolean(); + Application::Settings.Private = data["General"][StrPrivate.data()].as_boolean(); + Application::Settings.Port = data["General"][StrPort.data()].as_integer(); + Application::Settings.MaxCars = data["General"][StrMaxCars.data()].as_integer(); + Application::Settings.MaxPlayers = data["General"][StrMaxPlayers.data()].as_integer(); + Application::Settings.MapName = data["General"][StrMap.data()].as_string(); + Application::Settings.ServerName = data["General"][StrName.data()].as_string(); + Application::Settings.ServerDesc = data["General"][StrDescription.data()].as_string(); + Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string(); + Application::Settings.Key = data["General"][StrAuthKey.data()].as_string(); } catch (const std::exception& err) { error("Error parsing config file value: " + std::string(err.what())); mFailed = true; diff --git a/src/TLuaFile.cpp b/src/TLuaFile.cpp index 0eb7e7c..ab2960c 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -612,42 +612,6 @@ int lua_Set(lua_State* L) { return 0; } -/* -// CallInPlugin(PluginName, FunctionName) -int lua_CallInPlugin(lua_State* L) { - if (!lua_isstring(L, 1)) { - SendError(Engine(), L, "CallInPlugin expects a string as 1. argument."); - return 1; - } - if (!lua_isstring(L, 2)) { - SendError(Engine(), L, "CallInPlugin expects a string as 2. argument."); - return 1; - } - const char* PluginName = lua_tostring(L, 1); - const char* FunctionName = lua_tostring(L, 2); - - bool FoundPlugin = false; - for (const auto& File : Engine().LuaFiles()) { - if (File->GetPluginName() == PluginName) { - FoundPlugin = true; - auto State = File->GetState(); - lua_getglobal(State, FunctionName); - if (!lua_isfunction(State, -1)) { - SendError(Engine(), L, "CallInPlugin: \"" + std::string(FunctionName) + "\" in plugin \"" + std::string(PluginName) + "\" is not a function."); - return 1; - } - ClearStack(State); - CallFunction(File.get(), FunctionName, nullptr); - } - } - if (!FoundPlugin) { - SendError(Engine(), L, "CallInPlugin: Could not find plugin called \"" + std::string(PluginName) + "\""); - return 1; - } - - return 0; -} -*/ extern "C" { int lua_Print(lua_State* L) {