mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 02:30:54 +00:00
switch to toml11
it's better, believe me
This commit is contained in:
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -10,6 +10,6 @@
|
|||||||
[submodule "rapidjson"]
|
[submodule "rapidjson"]
|
||||||
path = rapidjson
|
path = rapidjson
|
||||||
url = https://github.com/Tencent/rapidjson
|
url = https://github.com/Tencent/rapidjson
|
||||||
[submodule "include/tomlplusplus"]
|
[submodule "include/toml11"]
|
||||||
path = include/tomlplusplus
|
path = include/toml11
|
||||||
url = https://github.com/marzer/tomlplusplus
|
url = https://github.com/ToruNiina/toml11
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ add_executable(BeamMP-Server
|
|||||||
target_include_directories(BeamMP-Server PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/commandline")
|
target_include_directories(BeamMP-Server PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/commandline")
|
||||||
|
|
||||||
find_package(Lua REQUIRED)
|
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)
|
find_package(OpenSSL REQUIRED)
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ public:
|
|||||||
|
|
||||||
[[nodiscard]] bool Failed() const { return mFailed; }
|
[[nodiscard]] bool Failed() const { return mFailed; }
|
||||||
|
|
||||||
|
void FlushToFile();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateConfigFile(std::string_view name);
|
void CreateConfigFile(std::string_view name);
|
||||||
void ParseFromFile(std::string_view name);
|
void ParseFromFile(std::string_view name);
|
||||||
|
|||||||
1
include/toml11
Submodule
1
include/toml11
Submodule
Submodule include/toml11 added at 647381020e
Submodule include/tomlplusplus deleted from bc6891e1fb
117
src/TConfig.cpp
117
src/TConfig.cpp
@@ -1,4 +1,6 @@
|
|||||||
#include <toml.hpp> // header-only version of TOML++
|
#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT
|
||||||
|
|
||||||
|
#include <toml11/toml.hpp> // header-only version of TOML++
|
||||||
|
|
||||||
#include "TConfig.h"
|
#include "TConfig.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -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) {
|
void TConfig::CreateConfigFile(std::string_view name) {
|
||||||
// build from old config Server.cfg
|
// 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()));
|
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",
|
auto data = toml::parse<toml::preserve_comments>(name.data());
|
||||||
toml::table { {
|
|
||||||
|
|
||||||
{ StrDebug, Application::Settings.DebugModeEnabled },
|
data["General"] = toml::table();
|
||||||
{ StrPrivate, Application::Settings.Private },
|
data["General"][StrAuthKey.data()] = Application::Settings.Key;
|
||||||
{ StrPort, Application::Settings.Port },
|
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
|
||||||
{ StrMaxCars, Application::Settings.MaxCars },
|
data["General"][StrPrivate.data()] = Application::Settings.Private;
|
||||||
{ StrMaxPlayers, Application::Settings.MaxPlayers },
|
data["General"][StrPort.data()] = Application::Settings.Port;
|
||||||
{ StrMap, Application::Settings.MapName },
|
data["General"][StrName.data()] = Application::Settings.ServerName;
|
||||||
{ StrName, Application::Settings.ServerName },
|
data["General"][StrMaxCars.data()] = Application::Settings.MaxCars;
|
||||||
{ StrDescription, Application::Settings.ServerDesc },
|
data["General"][StrMaxPlayers.data()] = Application::Settings.MaxPlayers;
|
||||||
{ StrResourceFolder, Application::Settings.Resource },
|
data["General"][StrMap.data()] = Application::Settings.MapName;
|
||||||
{ StrAuthKey, Application::Settings.Key },
|
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
|
||||||
|
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
|
||||||
|
|
||||||
} } },
|
|
||||||
|
|
||||||
} };
|
|
||||||
std::ofstream ofs { std::string(name) };
|
std::ofstream ofs { std::string(name) };
|
||||||
if (ofs.good()) {
|
if (ofs.good()) {
|
||||||
ofs << "# This is the BeamMP-Server config file.\n"
|
ofs << "# This is the BeamMP-Server config file.\n"
|
||||||
"# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\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"
|
"# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n"
|
||||||
<< '\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.");
|
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;
|
mFailed = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -80,58 +98,17 @@ void TConfig::CreateConfigFile(std::string_view name) {
|
|||||||
|
|
||||||
void TConfig::ParseFromFile(std::string_view name) {
|
void TConfig::ParseFromFile(std::string_view name) {
|
||||||
try {
|
try {
|
||||||
toml::table FullTable = toml::parse_file(name);
|
toml::value data = toml::parse<toml::preserve_comments>(name.data());
|
||||||
toml::table GeneralTable = *FullTable["General"].as_table();
|
Application::Settings.DebugModeEnabled = data["General"][StrDebug.data()].as_boolean();
|
||||||
if (auto val = GeneralTable[StrDebug].value<bool>(); val.has_value()) {
|
Application::Settings.Private = data["General"][StrPrivate.data()].as_boolean();
|
||||||
Application::Settings.DebugModeEnabled = val.value();
|
Application::Settings.Port = data["General"][StrPort.data()].as_integer();
|
||||||
} else {
|
Application::Settings.MaxCars = data["General"][StrMaxCars.data()].as_integer();
|
||||||
throw std::runtime_error(std::string(StrDebug));
|
Application::Settings.MaxPlayers = data["General"][StrMaxPlayers.data()].as_integer();
|
||||||
}
|
Application::Settings.MapName = data["General"][StrMap.data()].as_string();
|
||||||
if (auto val = GeneralTable[StrPrivate].value<bool>(); val.has_value()) {
|
Application::Settings.ServerName = data["General"][StrName.data()].as_string();
|
||||||
Application::Settings.Private = val.value();
|
Application::Settings.ServerDesc = data["General"][StrDescription.data()].as_string();
|
||||||
} else {
|
Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string();
|
||||||
throw std::runtime_error(std::string(StrPrivate));
|
Application::Settings.Key = data["General"][StrAuthKey.data()].as_string();
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrPort].value<int>(); val.has_value()) {
|
|
||||||
Application::Settings.Port = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrPort));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrMaxCars].value<int>(); val.has_value()) {
|
|
||||||
Application::Settings.MaxCars = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrMaxCars));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrMaxPlayers].value<int>(); val.has_value()) {
|
|
||||||
Application::Settings.MaxPlayers = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrMaxPlayers));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrMap].value<std::string>(); val.has_value()) {
|
|
||||||
Application::Settings.MapName = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrMap));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrName].value<std::string>(); val.has_value()) {
|
|
||||||
Application::Settings.ServerName = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrName));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrDescription].value<std::string>(); val.has_value()) {
|
|
||||||
Application::Settings.ServerDesc = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrDescription));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrResourceFolder].value<std::string>(); val.has_value()) {
|
|
||||||
Application::Settings.Resource = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrResourceFolder));
|
|
||||||
}
|
|
||||||
if (auto val = GeneralTable[StrAuthKey].value<std::string>(); val.has_value()) {
|
|
||||||
Application::Settings.Key = val.value();
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error(std::string(StrAuthKey));
|
|
||||||
}
|
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
error("Error parsing config file value: " + std::string(err.what()));
|
error("Error parsing config file value: " + std::string(err.what()));
|
||||||
mFailed = true;
|
mFailed = true;
|
||||||
|
|||||||
@@ -586,42 +586,6 @@ int lua_Set(lua_State* L) {
|
|||||||
|
|
||||||
return 0;
|
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" {
|
extern "C" {
|
||||||
int lua_Print(lua_State* L) {
|
int lua_Print(lua_State* L) {
|
||||||
|
|||||||
Reference in New Issue
Block a user