diff --git a/.gitmodules b/.gitmodules index 71828b9..0d23bc7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,6 +10,6 @@ [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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 182749c..2a5a9f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ add_executable(BeamMP-Server target_include_directories(BeamMP-Server PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/commandline") find_package(Lua REQUIRED) -target_include_directories(BeamMP-Server PUBLIC ${Boost_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} "socket.io-client-cpp/src" "include/tomlplusplus") +target_include_directories(BeamMP-Server PUBLIC ${Boost_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} "socket.io-client-cpp/src") find_package(OpenSSL REQUIRED) 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 977535d..71f1198 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -586,42 +586,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) {