mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 10:41:01 +00:00
Merge branch 'minor' into 158-bug-running-settings-help-returns-nothing
This commit is contained in:
27
src/ChronoWrapper.cpp
Normal file
27
src/ChronoWrapper.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "ChronoWrapper.h"
|
||||
#include "Common.h"
|
||||
#include <regex>
|
||||
|
||||
std::chrono::high_resolution_clock::duration ChronoWrapper::TimeFromStringWithLiteral(const std::string& time_str)
|
||||
{
|
||||
// const std::regex time_regex(R"((\d+\.{0,1}\d*)(min|ms|us|ns|[dhs]))"); //i.e one of: "25ns, 6us, 256ms, 2s, 13min, 69h, 356d" will get matched (only available in newer C++ versions)
|
||||
const std::regex time_regex(R"((\d+\.{0,1}\d*)(min|[dhs]))"); //i.e one of: "2.01s, 13min, 69h, 356.69d" will get matched
|
||||
std::smatch match;
|
||||
float time_value;
|
||||
if (!std::regex_search(time_str, match, time_regex)) return std::chrono::nanoseconds(0);
|
||||
time_value = stof(match.str(1));
|
||||
beammp_debugf("Parsed time was: {}{}", time_value, match.str(2));
|
||||
if (match.str(2) == "d") {
|
||||
return std::chrono::seconds((uint64_t)(time_value * 86400)); //86400 seconds in a day
|
||||
}
|
||||
else if (match.str(2) == "h") {
|
||||
return std::chrono::seconds((uint64_t)(time_value * 3600)); //3600 seconds in an hour
|
||||
}
|
||||
else if (match.str(2) == "min") {
|
||||
return std::chrono::seconds((uint64_t)(time_value * 60));
|
||||
}
|
||||
else if (match.str(2) == "s") {
|
||||
return std::chrono::seconds((uint64_t)time_value);
|
||||
}
|
||||
return std::chrono::nanoseconds(0);
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include "Compat.h"
|
||||
#include "CustomAssert.h"
|
||||
@@ -382,3 +383,4 @@ void SplitString(const std::string& str, const char delim, std::vector<std::stri
|
||||
out.push_back(str.substr(start, end - start));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
src/Http.cpp
51
src/Http.cpp
@@ -28,15 +28,42 @@
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
|
||||
// TODO: Add sentry error handling back
|
||||
|
||||
using json = nlohmann::json;
|
||||
struct Connection {
|
||||
std::string host{};
|
||||
int port{};
|
||||
Connection() = default;
|
||||
Connection(std::string host, int port)
|
||||
: host(host)
|
||||
, port(port) {};
|
||||
};
|
||||
constexpr uint8_t CONNECTION_AMOUNT = 10;
|
||||
static thread_local uint8_t write_index = 0;
|
||||
static thread_local std::array<Connection, CONNECTION_AMOUNT> connections;
|
||||
static thread_local std::array<std::shared_ptr<httplib::SSLClient>, CONNECTION_AMOUNT> clients;
|
||||
|
||||
[[nodiscard]] static std::shared_ptr<httplib::SSLClient> getClient(Connection connectionInfo) {
|
||||
for (uint8_t i = 0; i < CONNECTION_AMOUNT; i++) {
|
||||
if (connectionInfo.host == connections[i].host
|
||||
&& connectionInfo.port == connections[i].port) {
|
||||
beammp_tracef("Old client reconnected, with ip {} and port {}", connectionInfo.host, connectionInfo.port);
|
||||
return clients[i];
|
||||
}
|
||||
}
|
||||
uint8_t i = write_index;
|
||||
write_index++;
|
||||
write_index %= CONNECTION_AMOUNT;
|
||||
clients[i] = std::make_shared<httplib::SSLClient>(connectionInfo.host, connectionInfo.port);
|
||||
connections[i] = {connectionInfo.host, connectionInfo.port};
|
||||
beammp_tracef("New client connected, with ip {} and port {}", connectionInfo.host, connectionInfo.port);
|
||||
return clients[i];
|
||||
}
|
||||
|
||||
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
||||
httplib::SSLClient client(host, port);
|
||||
client.enable_server_certificate_verification(false);
|
||||
client.set_address_family(AF_INET);
|
||||
auto res = client.Get(target.c_str());
|
||||
std::shared_ptr<httplib::SSLClient> client = getClient({host, port});
|
||||
client->enable_server_certificate_verification(false);
|
||||
client->set_address_family(AF_INET);
|
||||
auto res = client->Get(target.c_str());
|
||||
if (res) {
|
||||
if (status) {
|
||||
*status = res->status;
|
||||
@@ -48,12 +75,12 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ
|
||||
}
|
||||
|
||||
std::string Http::POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status, const httplib::Headers& headers) {
|
||||
httplib::SSLClient client(host, port);
|
||||
client.set_read_timeout(std::chrono::seconds(10));
|
||||
beammp_assert(client.is_valid());
|
||||
client.enable_server_certificate_verification(false);
|
||||
client.set_address_family(AF_INET);
|
||||
auto res = client.Post(target.c_str(), headers, body.c_str(), body.size(), ContentType.c_str());
|
||||
std::shared_ptr<httplib::SSLClient> client = getClient({host, port});
|
||||
client->set_read_timeout(std::chrono::seconds(10));
|
||||
beammp_assert(client->is_valid());
|
||||
client->enable_server_certificate_verification(false);
|
||||
client->set_address_family(AF_INET);
|
||||
auto res = client->Post(target.c_str(), headers, body.c_str(), body.size(), ContentType.c_str());
|
||||
if (res) {
|
||||
if (status) {
|
||||
*status = res->status;
|
||||
|
||||
@@ -61,7 +61,11 @@ std::string LuaAPI::LuaToString(const sol::object Value, size_t Indent, bool Quo
|
||||
}
|
||||
case sol::type::number: {
|
||||
std::stringstream ss;
|
||||
ss << Value.as<float>();
|
||||
if (Value.is<int>()) {
|
||||
ss << Value.as<int>();
|
||||
} else {
|
||||
ss << Value.as<float>();
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
case sol::type::lua_nil:
|
||||
@@ -562,7 +566,11 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
|
||||
key = left.as<std::string>();
|
||||
break;
|
||||
case sol::type::number:
|
||||
key = std::to_string(left.as<double>());
|
||||
if (left.is<int>()) {
|
||||
key = std::to_string(left.as<int>());
|
||||
} else {
|
||||
key = std::to_string(left.as<double>());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
beammp_assert_not_reachable();
|
||||
@@ -590,21 +598,30 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
|
||||
case sol::type::string:
|
||||
value = right.as<std::string>();
|
||||
break;
|
||||
case sol::type::number:
|
||||
value = right.as<double>();
|
||||
case sol::type::number: {
|
||||
if (right.is<int>()) {
|
||||
value = right.as<int>();
|
||||
} else {
|
||||
value = right.as<double>();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case sol::type::function:
|
||||
beammp_lua_warn("unsure what to do with function in JsonEncode, ignoring");
|
||||
return;
|
||||
case sol::type::table: {
|
||||
bool local_is_array = true;
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
local_is_array = false;
|
||||
if (right.as<sol::table>().empty()) {
|
||||
value = nlohmann::json::object();
|
||||
} else {
|
||||
bool local_is_array = true;
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
local_is_array = false;
|
||||
}
|
||||
}
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
JsonEncodeRecursive(value, pair.first, pair.second, local_is_array, depth + 1);
|
||||
}
|
||||
}
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
JsonEncodeRecursive(value, pair.first, pair.second, local_is_array, depth + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -621,14 +638,18 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
|
||||
std::string LuaAPI::MP::JsonEncode(const sol::table& object) {
|
||||
nlohmann::json json;
|
||||
// table
|
||||
bool is_array = true;
|
||||
for (const auto& pair : object.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
is_array = false;
|
||||
if (object.as<sol::table>().empty()) {
|
||||
json = nlohmann::json::object();
|
||||
} else {
|
||||
bool is_array = true;
|
||||
for (const auto& pair : object.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
is_array = false;
|
||||
}
|
||||
}
|
||||
for (const auto& entry : object) {
|
||||
JsonEncodeRecursive(json, entry.first, entry.second, is_array);
|
||||
}
|
||||
}
|
||||
for (const auto& entry : object) {
|
||||
JsonEncodeRecursive(json, entry.first, entry.second, is_array);
|
||||
}
|
||||
return json.dump();
|
||||
}
|
||||
|
||||
60
src/Profiling.cpp
Normal file
60
src/Profiling.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "Profiling.h"
|
||||
#include <limits>
|
||||
|
||||
prof::Duration prof::duration(const TimePoint& start, const TimePoint& end) {
|
||||
return end - start;
|
||||
}
|
||||
prof::TimePoint prof::now() {
|
||||
return std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
prof::Stats prof::UnitProfileCollection::stats(const std::string& unit) {
|
||||
return m_map->operator[](unit).stats();
|
||||
}
|
||||
|
||||
size_t prof::UnitProfileCollection::measurement_count(const std::string& unit) {
|
||||
return m_map->operator[](unit).measurement_count();
|
||||
}
|
||||
|
||||
void prof::UnitProfileCollection::add_sample(const std::string& unit, const Duration& duration) {
|
||||
m_map->operator[](unit).add_sample(duration);
|
||||
}
|
||||
|
||||
prof::Stats prof::UnitExecutionTime::stats() const {
|
||||
std::unique_lock lock(m_mtx);
|
||||
Stats result {};
|
||||
// calculate sum
|
||||
result.n = m_total_calls;
|
||||
result.max = m_min;
|
||||
result.min = m_max;
|
||||
// calculate mean: mean = sum_x / n
|
||||
result.mean = m_sum / double(m_total_calls);
|
||||
// calculate stdev: stdev = sqrt((sum_x2 / n) - (mean * mean))
|
||||
result.stdev = std::sqrt((m_measurement_sqr_sum / double(result.n)) - (result.mean * result.mean));
|
||||
return result;
|
||||
}
|
||||
|
||||
void prof::UnitExecutionTime::add_sample(const Duration& dur) {
|
||||
std::unique_lock lock(m_mtx);
|
||||
m_sum += dur.count();
|
||||
m_measurement_sqr_sum += dur.count() * dur.count();
|
||||
m_min = std::min(dur.count(), m_min);
|
||||
m_max = std::max(dur.count(), m_max);
|
||||
++m_total_calls;
|
||||
}
|
||||
|
||||
prof::UnitExecutionTime::UnitExecutionTime() {
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, prof::Stats> prof::UnitProfileCollection::all_stats() {
|
||||
auto map = m_map.synchronize();
|
||||
std::unordered_map<std::string, Stats> result {};
|
||||
for (const auto& [name, time] : *map) {
|
||||
result[name] = time.stats();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
size_t prof::UnitExecutionTime::measurement_count() const {
|
||||
std::unique_lock lock(m_mtx);
|
||||
return m_total_calls;
|
||||
}
|
||||
|
||||
@@ -54,12 +54,15 @@ static constexpr std::string_view StrAuthKey = "AuthKey";
|
||||
static constexpr std::string_view EnvStrAuthKey = "BEAMMP_AUTH_KEY";
|
||||
static constexpr std::string_view StrLogChat = "LogChat";
|
||||
static constexpr std::string_view EnvStrLogChat = "BEAMMP_LOG_CHAT";
|
||||
static constexpr std::string_view StrAllowGuests = "AllowGuests";
|
||||
static constexpr std::string_view EnvStrAllowGuests = "BEAMMP_ALLOW_GUESTS";
|
||||
static constexpr std::string_view StrPassword = "Password";
|
||||
|
||||
// Misc
|
||||
static constexpr std::string_view StrSendErrors = "SendErrors";
|
||||
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
|
||||
static constexpr std::string_view StrHideUpdateMessages = "ImScaredOfUpdates";
|
||||
static constexpr std::string_view StrUpdateReminderTime = "UpdateReminderTime";
|
||||
|
||||
TEST_CASE("TConfig::TConfig") {
|
||||
const std::string CfgFile = "beammp_server_testconfig.toml";
|
||||
@@ -129,6 +132,8 @@ void TConfig::FlushToFile() {
|
||||
SetComment(data["General"][StrLogChat.data()].comments(), " Whether to log chat messages in the console / log");
|
||||
data["General"][StrDebug.data()] = Application::Settings.getAsBool(Settings::Key::General_Debug);
|
||||
data["General"][StrPrivate.data()] = Application::Settings.getAsBool(Settings::Key::General_Private);
|
||||
data["General"][StrAllowGuests.data()] = Application::Settings.AllowGuests;
|
||||
SetComment(data["General"][StrAllowGuests.data()].comments(), " Whether to allow guests");
|
||||
data["General"][StrPort.data()] = Application::Settings.getAsInt(Settings::Key::General_Port);
|
||||
data["General"][StrName.data()] = Application::Settings.getAsString(Settings::Key::General_Name);
|
||||
SetComment(data["General"][StrTags.data()].comments(), " Add custom identifying tags to your server to make it easier to find. Format should be TagA,TagB,TagC. Note the comma seperation.");
|
||||
@@ -144,6 +149,8 @@ void TConfig::FlushToFile() {
|
||||
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.getAsBool(Settings::Key::Misc_ImScaredOfUpdates);
|
||||
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.getAsBool(Settings::Key::Misc_SendErrors);
|
||||
data["Misc"][StrUpdateReminderTime.data()] = Application::Settings.UpdateReminderTime;
|
||||
SetComment(data["Misc"][StrUpdateReminderTime.data()].comments(), " Specifies the time between update reminders. You can use any of \"s, min, h, d\" at the end to specify the units seconds, minutes, hours or days. So 30d or 0.5min will print the update message every 30 days or half a minute.");
|
||||
SetComment(data["Misc"][StrSendErrors.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`");
|
||||
data["Misc"][StrSendErrorsMessageEnabled.data()] = Application::Settings.getAsBool(Settings::Key::Misc_SendErrorsShowMessage);
|
||||
SetComment(data["Misc"][StrSendErrorsMessageEnabled.data()].comments(), " You can turn on/off the SendErrors message you get on startup here");
|
||||
@@ -260,10 +267,12 @@ void TConfig::ParseFromFile(std::string_view name) {
|
||||
TryReadValue(data, "General", StrResourceFolder, EnvStrResourceFolder, Settings::Key::General_ResourceFolder);
|
||||
TryReadValue(data, "General", StrAuthKey, EnvStrAuthKey, Settings::Key::General_AuthKey);
|
||||
TryReadValue(data, "General", StrLogChat, EnvStrLogChat, Settings::Key::General_LogChat);
|
||||
TryReadValue(data, "General", StrAllowGuests, EnvStrAllowGuests, Application::Settings.AllowGuests);
|
||||
// Misc
|
||||
TryReadValue(data, "Misc", StrSendErrors, "", Settings::Key::Misc_SendErrors);
|
||||
TryReadValue(data, "Misc", StrHideUpdateMessages, "", Settings::Misc_ImScaredOfUpdates);
|
||||
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, "", Settings::Misc_SendErrorsShowMessage);
|
||||
TryReadValue(data, "Misc", StrUpdateReminderTime, "", Application::Settings.UpdateReminderTime);
|
||||
|
||||
} catch (const std::exception& err) {
|
||||
beammp_error("Error parsing config file value: " + std::string(err.what()));
|
||||
@@ -308,6 +317,7 @@ void TConfig::PrintDebug() {
|
||||
beammp_debug(std::string(StrTags) + ": " + TagsAsPrettyArray());
|
||||
beammp_debug(std::string(StrLogChat) + ": \"" + (Application::Settings.getAsBool(Settings::Key::General_LogChat) ? "true" : "false") + "\"");
|
||||
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.getAsString(Settings::Key::General_ResourceFolder) + "\"");
|
||||
beammp_debug(std::string(StrAllowGuests) + ": \"" + (Application::Settings.AllowGuests ? "true" : "false") + "\"");
|
||||
// special!
|
||||
beammp_debug("Key Length: " + std::to_string(Application::Settings.getAsString(Settings::Key::General_AuthKey).length()) + "");
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@
|
||||
#include "TLuaEngine.h"
|
||||
|
||||
#include <ctime>
|
||||
#include <lua.hpp>
|
||||
#include <mutex>
|
||||
#include <openssl/opensslv.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
@@ -240,7 +242,25 @@ void TConsole::Command_Version(const std::string& cmd, const std::vector<std::st
|
||||
return;
|
||||
}
|
||||
|
||||
Application::Console().WriteRaw("Current version: v" + Application::ServerVersionString());
|
||||
std::string platform;
|
||||
#if defined(BEAMMP_WINDOWS)
|
||||
platform = "Windows";
|
||||
#elif defined(BEAMMP_LINUX)
|
||||
platform = "Linux";
|
||||
#elif defined(BEAMMP_FREEBSD)
|
||||
platform = "FreeBSD";
|
||||
#elif defined(BEAMMP_APPLE)
|
||||
platform = "Apple";
|
||||
#else
|
||||
platform = "Unknown";
|
||||
#endif
|
||||
|
||||
Application::Console().WriteRaw("Platform: " + platform);
|
||||
Application::Console().WriteRaw("Server: v" + Application::ServerVersionString());
|
||||
std::string lua_version = fmt::format("Lua: v{}.{}.{}", LUA_VERSION_MAJOR, LUA_VERSION_MINOR, LUA_VERSION_RELEASE);
|
||||
Application::Console().WriteRaw(lua_version);
|
||||
std::string openssl_version = fmt::format("OpenSSL: v{}.{}.{}", OPENSSL_VERSION_MAJOR, OPENSSL_VERSION_MINOR, OPENSSL_VERSION_PATCH);
|
||||
Application::Console().WriteRaw(openssl_version);
|
||||
}
|
||||
|
||||
void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "Client.h"
|
||||
#include "Http.h"
|
||||
#include "ChronoWrapper.h"
|
||||
//#include "SocketIO.h"
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/rapidjson.h>
|
||||
@@ -36,15 +37,17 @@ void THeartbeatThread::operator()() {
|
||||
static std::string Last;
|
||||
|
||||
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
|
||||
static std::chrono::high_resolution_clock::time_point LastUpdateReminderTime = std::chrono::high_resolution_clock::now();
|
||||
bool isAuth = false;
|
||||
size_t UpdateReminderCounter = 0;
|
||||
std::chrono::high_resolution_clock::duration UpdateReminderTimePassed;
|
||||
auto UpdateReminderTimeout = ChronoWrapper::TimeFromStringWithLiteral(Application::Settings.UpdateReminderTime);
|
||||
while (!Application::IsShuttingDown()) {
|
||||
++UpdateReminderCounter;
|
||||
Body = GenerateCall();
|
||||
// a hot-change occurs when a setting has changed, to update the backend of that change.
|
||||
auto Now = std::chrono::high_resolution_clock::now();
|
||||
bool Unchanged = Last == Body;
|
||||
auto TimePassed = (Now - LastNormalUpdateTime);
|
||||
UpdateReminderTimePassed = (Now - LastUpdateReminderTime);
|
||||
auto Threshold = Unchanged ? 30 : 5;
|
||||
if (TimePassed < std::chrono::seconds(Threshold)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
@@ -126,7 +129,8 @@ void THeartbeatThread::operator()() {
|
||||
if (isAuth || Application::Settings.getAsBool(Settings::Key::General_Private)) {
|
||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
|
||||
}
|
||||
if (!Application::Settings.getAsBool(Settings::Key::Misc_ImScaredOfUpdates) && UpdateReminderCounter % 5) {
|
||||
if (!Application::Settings.HideUpdateMessages && UpdateReminderTimePassed.count() > UpdateReminderTimeout.count()) {
|
||||
LastUpdateReminderTime = std::chrono::high_resolution_clock::now();
|
||||
Application::CheckForUpdates();
|
||||
}
|
||||
}
|
||||
@@ -145,6 +149,7 @@ std::string THeartbeatThread::GenerateCall() {
|
||||
<< "&clientversion=" << std::to_string(Application::ClientMajorVersion()) + ".0" // FIXME: Wtf.
|
||||
<< "&name=" << Application::Settings.getAsString(Settings::Key::General_Name)
|
||||
<< "&tags=" << Application::Settings.getAsString(Settings::Key::General_Tags)
|
||||
<< "&guests=" << (Application::Settings.AllowGuests ? "true" : "false")
|
||||
<< "&modlist=" << mResourceManager.TrimmedList()
|
||||
<< "&modstotalsize=" << mResourceManager.MaxModSize()
|
||||
<< "&modstotal=" << mResourceManager.ModsLoaded()
|
||||
|
||||
@@ -22,11 +22,13 @@
|
||||
#include "CustomAssert.h"
|
||||
#include "Http.h"
|
||||
#include "LuaAPI.h"
|
||||
#include "Profiling.h"
|
||||
#include "TLuaPlugin.h"
|
||||
#include "sol/object.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <fmt/core.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
@@ -64,6 +66,7 @@ void TLuaEngine::operator()() {
|
||||
RegisterThread("LuaEngine");
|
||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::Good);
|
||||
// lua engine main thread
|
||||
beammp_infof("Lua v{}.{}.{}", LUA_VERSION_MAJOR, LUA_VERSION_MINOR, LUA_VERSION_RELEASE);
|
||||
CollectAndInitPlugins();
|
||||
// now call all onInit's
|
||||
auto Futures = TriggerEvent("onInit", "");
|
||||
@@ -266,7 +269,7 @@ std::vector<std::string> TLuaEngine::StateThreadData::GetStateTableKeys(const st
|
||||
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
auto obj = current.get<sol::object>(keys.at(i));
|
||||
if (obj.get_type() == sol::type::nil) {
|
||||
if (obj.get_type() == sol::type::lua_nil) {
|
||||
// error
|
||||
break;
|
||||
} else if (i == keys.size() - 1) {
|
||||
@@ -351,7 +354,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const
|
||||
return mLuaStates.at(StateID)->EnqueueScript(Script);
|
||||
}
|
||||
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args) {
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaValue>& Args) {
|
||||
std::unique_lock Lock(mLuaStatesMutex);
|
||||
return mLuaStates.at(StateID)->EnqueueFunctionCall(FunctionName, Args);
|
||||
}
|
||||
@@ -360,17 +363,30 @@ void TLuaEngine::CollectAndInitPlugins() {
|
||||
if (!fs::exists(mResourceServerPath)) {
|
||||
fs::create_directories(mResourceServerPath);
|
||||
}
|
||||
for (const auto& Dir : fs::directory_iterator(mResourceServerPath)) {
|
||||
auto Path = Dir.path();
|
||||
Path = fs::relative(Path);
|
||||
if (!Dir.is_directory()) {
|
||||
beammp_error("\"" + Dir.path().string() + "\" is not a directory, skipping");
|
||||
|
||||
std::vector<fs::path> PluginsEntries;
|
||||
for (const auto& Entry : fs::directory_iterator(mResourceServerPath)) {
|
||||
if (Entry.is_directory()) {
|
||||
PluginsEntries.push_back(Entry);
|
||||
} else {
|
||||
TLuaPluginConfig Config { Path.stem().string() };
|
||||
FindAndParseConfig(Path, Config);
|
||||
InitializePlugin(Path, Config);
|
||||
beammp_error("\"" + Entry.path().string() + "\" is not a directory, skipping");
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(PluginsEntries.begin(), PluginsEntries.end(), [](const fs::path& first, const fs::path& second) {
|
||||
auto firstStr = first.string();
|
||||
auto secondStr = second.string();
|
||||
std::transform(firstStr.begin(), firstStr.end(), firstStr.begin(), ::tolower);
|
||||
std::transform(secondStr.begin(), secondStr.end(), secondStr.begin(), ::tolower);
|
||||
return firstStr < secondStr;
|
||||
});
|
||||
|
||||
for (const auto& Dir : PluginsEntries) {
|
||||
auto Path = fs::relative(Dir);
|
||||
TLuaPluginConfig Config { Path.stem().string() };
|
||||
FindAndParseConfig(Path, Config);
|
||||
InitializePlugin(Path, Config);
|
||||
}
|
||||
}
|
||||
|
||||
void TLuaEngine::InitializePlugin(const fs::path& Folder, const TLuaPluginConfig& Config) {
|
||||
@@ -431,13 +447,52 @@ std::set<std::string> TLuaEngine::GetEventHandlersForState(const std::string& Ev
|
||||
return mLuaEvents[EventName][StateId];
|
||||
}
|
||||
|
||||
std::vector<sol::object> TLuaEngine::StateThreadData::JsonStringToArray(JsonString Str) {
|
||||
auto LocalTable = Lua_JsonDecode(Str.value).as<std::vector<sol::object>>();
|
||||
for (auto& value : LocalTable) {
|
||||
if (value.is<std::string>() && value.as<std::string>() == BEAMMP_INTERNAL_NIL) {
|
||||
value = sol::object {};
|
||||
}
|
||||
}
|
||||
return LocalTable;
|
||||
}
|
||||
|
||||
sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
|
||||
auto Return = mEngine->TriggerEvent(EventName, mStateId, EventArgs);
|
||||
auto Table = mStateView.create_table();
|
||||
int i = 1;
|
||||
for (auto Arg : EventArgs) {
|
||||
switch (Arg.get_type()) {
|
||||
case sol::type::none:
|
||||
case sol::type::userdata:
|
||||
case sol::type::lightuserdata:
|
||||
case sol::type::thread:
|
||||
case sol::type::function:
|
||||
case sol::type::poly:
|
||||
Table.set(i, BEAMMP_INTERNAL_NIL);
|
||||
beammp_warnf("Passed a value of type '{}' to TriggerGlobalEvent(\"{}\", ...). This type can not be serialized, and cannot be passed between states. It will arrive as <nil> in handlers.", sol::type_name(EventArgs.lua_state(), Arg.get_type()), EventName);
|
||||
break;
|
||||
case sol::type::lua_nil:
|
||||
Table.set(i, BEAMMP_INTERNAL_NIL);
|
||||
break;
|
||||
case sol::type::string:
|
||||
case sol::type::number:
|
||||
case sol::type::boolean:
|
||||
case sol::type::table:
|
||||
Table.set(i, Arg);
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
JsonString Str { LuaAPI::MP::JsonEncode(Table) };
|
||||
beammp_debugf("json: {}", Str.value);
|
||||
auto Return = mEngine->TriggerEvent(EventName, mStateId, Str);
|
||||
auto MyHandlers = mEngine->GetEventHandlersForState(EventName, mStateId);
|
||||
|
||||
sol::variadic_results LocalArgs = JsonStringToArray(Str);
|
||||
for (const auto& Handler : MyHandlers) {
|
||||
auto Fn = mStateView[Handler];
|
||||
if (Fn.valid()) {
|
||||
auto LuaResult = Fn(EventArgs);
|
||||
auto LuaResult = Fn(LocalArgs);
|
||||
auto Result = std::make_shared<TLuaResult>();
|
||||
if (LuaResult.valid()) {
|
||||
Result->Error = false;
|
||||
@@ -468,11 +523,13 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
|
||||
sol::state_view StateView(mState);
|
||||
sol::table Result = StateView.create_table();
|
||||
auto Vector = Self.get<std::vector<std::shared_ptr<TLuaResult>>>("ReturnValueImpl");
|
||||
int i = 1;
|
||||
for (const auto& Value : Vector) {
|
||||
if (!Value->Ready) {
|
||||
return sol::lua_nil;
|
||||
}
|
||||
Result.add(Value->Result);
|
||||
Result.set(i, Value->Result);
|
||||
++i;
|
||||
}
|
||||
return Result;
|
||||
});
|
||||
@@ -482,12 +539,14 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
|
||||
sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
|
||||
// TODO: make asynchronous?
|
||||
sol::table Result = mStateView.create_table();
|
||||
int i = 1;
|
||||
for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) {
|
||||
auto Fn = mStateView[Handler];
|
||||
if (Fn.valid() && Fn.get_type() == sol::type::function) {
|
||||
auto FnRet = Fn(EventArgs);
|
||||
if (FnRet.valid()) {
|
||||
Result.add(FnRet);
|
||||
Result.set(i, FnRet);
|
||||
++i;
|
||||
} else {
|
||||
sol::error Err = FnRet;
|
||||
beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what());
|
||||
@@ -659,6 +718,7 @@ static void AddToTable(sol::table& table, const std::string& left, const T& valu
|
||||
static void JsonDecodeRecursive(sol::state_view& StateView, sol::table& table, const std::string& left, const nlohmann::json& right) {
|
||||
switch (right.type()) {
|
||||
case nlohmann::detail::value_t::null:
|
||||
AddToTable(table, left, sol::lua_nil_t {});
|
||||
return;
|
||||
case nlohmann::detail::value_t::object: {
|
||||
auto value = table.create();
|
||||
@@ -882,6 +942,30 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
||||
UtilTable.set_function("RandomIntRange", [this](int64_t min, int64_t max) -> int64_t {
|
||||
return std::uniform_int_distribution(min, max)(mMersenneTwister);
|
||||
});
|
||||
UtilTable.set_function("DebugExecutionTime", [this]() -> sol::table {
|
||||
sol::state_view StateView(mState);
|
||||
sol::table Result = StateView.create_table();
|
||||
auto stats = mProfile.all_stats();
|
||||
for (const auto& [name, stat] : stats) {
|
||||
Result[name] = StateView.create_table();
|
||||
Result[name]["mean"] = stat.mean;
|
||||
Result[name]["stdev"] = stat.stdev;
|
||||
Result[name]["min"] = stat.min;
|
||||
Result[name]["max"] = stat.max;
|
||||
Result[name]["n"] = stat.n;
|
||||
}
|
||||
return Result;
|
||||
});
|
||||
UtilTable.set_function("DebugStartProfile", [this](const std::string& name) {
|
||||
mProfileStarts[name] = prof::now();
|
||||
});
|
||||
UtilTable.set_function("DebugStopProfile", [this](const std::string& name) {
|
||||
if (!mProfileStarts.contains(name)) {
|
||||
beammp_lua_errorf("DebugStopProfile('{}') failed, because a profile for '{}' wasn't started", name, name);
|
||||
return;
|
||||
}
|
||||
mProfile.add_sample(name, prof::duration(mProfileStarts.at(name), prof::now()));
|
||||
});
|
||||
|
||||
auto HttpTable = StateView.create_named_table("Http");
|
||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||
@@ -929,7 +1013,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueScript(const TLu
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy) {
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaValue>& Args, const std::string& EventName, CallStrategy Strategy) {
|
||||
// TODO: Document all this
|
||||
decltype(mStateFunctionQueue)::iterator Iter = mStateFunctionQueue.end();
|
||||
if (Strategy == CallStrategy::BestEffort) {
|
||||
@@ -951,7 +1035,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFrom
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args) {
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaValue>& Args) {
|
||||
auto Result = std::make_shared<TLuaResult>();
|
||||
Result->StateId = mStateId;
|
||||
Result->Function = FunctionName;
|
||||
@@ -1019,6 +1103,7 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
std::chrono::milliseconds(500),
|
||||
[&]() -> bool { return !mStateFunctionQueue.empty(); });
|
||||
if (NotExpired) {
|
||||
auto ProfStart = prof::now();
|
||||
auto TheQueuedFunction = std::move(mStateFunctionQueue.front());
|
||||
mStateFunctionQueue.erase(mStateFunctionQueue.begin());
|
||||
Lock.unlock();
|
||||
@@ -1036,19 +1121,21 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
continue;
|
||||
}
|
||||
switch (Arg.index()) {
|
||||
case TLuaArgTypes_String:
|
||||
case TLuaType::String:
|
||||
LuaArgs.push_back(sol::make_object(StateView, std::get<std::string>(Arg)));
|
||||
break;
|
||||
case TLuaArgTypes_Int:
|
||||
case TLuaType::Int:
|
||||
LuaArgs.push_back(sol::make_object(StateView, std::get<int>(Arg)));
|
||||
break;
|
||||
case TLuaArgTypes_VariadicArgs:
|
||||
LuaArgs.push_back(sol::make_object(StateView, std::get<sol::variadic_args>(Arg)));
|
||||
case TLuaType::Json: {
|
||||
auto LocalArgs = JsonStringToArray(std::get<JsonString>(Arg));
|
||||
LuaArgs.insert(LuaArgs.end(), LocalArgs.begin(), LocalArgs.end());
|
||||
break;
|
||||
case TLuaArgTypes_Bool:
|
||||
}
|
||||
case TLuaType::Bool:
|
||||
LuaArgs.push_back(sol::make_object(StateView, std::get<bool>(Arg)));
|
||||
break;
|
||||
case TLuaArgTypes_StringStringMap: {
|
||||
case TLuaType::StringStringMap: {
|
||||
auto Map = std::get<std::unordered_map<std::string, std::string>>(Arg);
|
||||
auto Table = StateView.create_table();
|
||||
for (const auto& [k, v] : Map) {
|
||||
@@ -1077,6 +1164,9 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
Result->ErrorMessage = BeamMPFnNotFoundError; // special error kind that we can ignore later
|
||||
Result->MarkAsReady();
|
||||
}
|
||||
auto ProfEnd = prof::now();
|
||||
auto ProfDuration = prof::duration(ProfStart, ProfEnd);
|
||||
mProfile.add_sample(FnName, ProfDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ void TNetwork::Identify(TConnection&& RawConnection) {
|
||||
} else {
|
||||
beammp_errorf("Invalid code got in Identify: '{}'", Code);
|
||||
}
|
||||
} catch(const std::exception& e) {
|
||||
} catch (const std::exception& e) {
|
||||
beammp_errorf("Error during handling of code {} - client left in invalid state, closing socket", Code);
|
||||
boost::system::error_code ec;
|
||||
RawConnection.Socket.shutdown(socket_base::shutdown_both, ec);
|
||||
@@ -278,7 +278,7 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!TCPSend(*Client, StringToVector("A"))) { //changed to A for Accepted version
|
||||
if (!TCPSend(*Client, StringToVector("A"))) { // changed to A for Accepted version
|
||||
// TODO: handle
|
||||
}
|
||||
|
||||
@@ -289,16 +289,21 @@ 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{};
|
||||
std::string Key(reinterpret_cast<const char*>(Data.data()), Data.size());
|
||||
std::string AuthKey = Application::Settings.Key;
|
||||
std::string ClientIp = Client->GetIdentifiers().at("ip");
|
||||
|
||||
nlohmann::json AuthReq {};
|
||||
std::string AuthResStr {};
|
||||
try {
|
||||
AuthReq = nlohmann::json {
|
||||
{ "key", key }
|
||||
{ "key", Key },
|
||||
{ "auth_key", AuthKey },
|
||||
{ "client_ip", ClientIp }
|
||||
};
|
||||
|
||||
auto Target = "/pkToUser";
|
||||
|
||||
unsigned int ResponseCode = 0;
|
||||
AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode);
|
||||
|
||||
@@ -368,6 +373,11 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
||||
return false;
|
||||
});
|
||||
|
||||
if (!NotAllowedWithReason && !Application::Settings.AllowGuests && Client->IsGuest()) { //!NotAllowedWithReason because this message has the lowest priority
|
||||
NotAllowedWithReason = true;
|
||||
Reason = "No guests are allowed on this server! To join, sign up at: forum.beammp.com.";
|
||||
}
|
||||
|
||||
if (NotAllowed) {
|
||||
ClientKick(*Client, "you are not allowed on the server!");
|
||||
return {};
|
||||
|
||||
@@ -76,7 +76,7 @@ void TPPSMonitor::operator()() {
|
||||
return true;
|
||||
});
|
||||
for (auto& ClientToKick : TimedOutClients) {
|
||||
Network().ClientKick(*ClientToKick, "Timeout (no ping for way too long)");
|
||||
ClientToKick->Disconnect("Timeout");
|
||||
}
|
||||
TimedOutClients.clear();
|
||||
if (C == 0 || mInternalPPS == 0) {
|
||||
|
||||
Reference in New Issue
Block a user