From b009a37f35d4a59b71913a5bfe66c0df071af8b3 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 13:59:11 +0200 Subject: [PATCH 01/13] update asio to 1-23-0 --- deps/asio | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/asio b/deps/asio index d038fb3..4915cfd 160000 --- a/deps/asio +++ b/deps/asio @@ -1 +1 @@ -Subproject commit d038fb3c2fb56fb91ff1d17b0715cff7887aa09e +Subproject commit 4915cfd8a1653c157a1480162ae5601318553eb8 From 1caa5e3517e4ef61c7fed06cc480bbd2c4a68a8d Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 13:59:54 +0200 Subject: [PATCH 02/13] update commandline --- deps/commandline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/commandline b/deps/commandline index d6b1c32..7b9f51d 160000 --- a/deps/commandline +++ b/deps/commandline @@ -1 +1 @@ -Subproject commit d6b1c32c8af6ad5306f9f001305b3be9928ae4bb +Subproject commit 7b9f51d6a0402500a8f84a2fb37223ac4309f2d7 From e634b4b6b780b8aecfeeecfaef2cb75e63fb9950 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 14:00:26 +0200 Subject: [PATCH 03/13] update doctest --- deps/doctest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/doctest b/deps/doctest index 7b98851..b7c21ec 160000 --- a/deps/doctest +++ b/deps/doctest @@ -1 +1 @@ -Subproject commit 7b9885133108ae301ddd16e2651320f54cafeba7 +Subproject commit b7c21ec5ceeadb4951b00396fc1e4642dd347e5f From 60dd1e24726bda897363231fe2380f6367173198 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 14:01:04 +0200 Subject: [PATCH 04/13] update json --- deps/json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/json b/deps/json index ede6667..69d7448 160000 --- a/deps/json +++ b/deps/json @@ -1 +1 @@ -Subproject commit ede66678580596028bcd6e18871a35a54bac01d7 +Subproject commit 69d744867f8847c91a126fa25e9a6a3d67b3be41 From 398f8d3fa40b4e0a15b549695546d2abebc3f791 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 14:01:40 +0200 Subject: [PATCH 05/13] update sol2 --- deps/sol2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/sol2 b/deps/sol2 index c068aef..eba8662 160000 --- a/deps/sol2 +++ b/deps/sol2 @@ -1 +1 @@ -Subproject commit c068aefbeddb3dd1f1fd38d42843ecb49a3b4cdb +Subproject commit eba86625b707e3c8c99bbfc4624e51f42dc9e561 From 76a8f231ac04921f375fc045093268f6a6bb0b6e Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 14:02:11 +0200 Subject: [PATCH 06/13] update httplib --- deps/cpp-httplib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/cpp-httplib b/deps/cpp-httplib index 47044c0..d92c314 160000 --- a/deps/cpp-httplib +++ b/deps/cpp-httplib @@ -1 +1 @@ -Subproject commit 47044c05a8587dff86ab90526daabfef61079490 +Subproject commit d92c31446687cfa336a6332b1015b4fe289fbdec From b443bec72ef7ec7e7a2615d142328e1b65562eb5 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 18:13:25 +0200 Subject: [PATCH 07/13] update fmt --- deps/fmt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/fmt b/deps/fmt index ce246aa..c4ee726 160000 --- a/deps/fmt +++ b/deps/fmt @@ -1 +1 @@ -Subproject commit ce246aaf74cb7616777497da988804fa4c02bf2d +Subproject commit c4ee726532178e556d923372f29163bd206d7732 From e4db66782e3de2677bf1314b135b43dc6d2f3785 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 18:13:49 +0200 Subject: [PATCH 08/13] update libzip --- deps/libzip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/libzip b/deps/libzip index 76df02f..5532f9b 160000 --- a/deps/libzip +++ b/deps/libzip @@ -1 +1 @@ -Subproject commit 76df02f86b9746e139fd9fc934a70e3a21bbc557 +Subproject commit 5532f9baa0c44cc5435ad135686a4ea009075b9a From e4cbba59ef126d1a8468a6a22287b8d71a1ae9cd Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 18:14:19 +0200 Subject: [PATCH 09/13] update sentry-native --- deps/sentry-native | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/sentry-native b/deps/sentry-native index 90966cc..87e67ad 160000 --- a/deps/sentry-native +++ b/deps/sentry-native @@ -1 +1 @@ -Subproject commit 90966cc1022b8155681b6899539b35466baccf2c +Subproject commit 87e67ad783a7ec4476b0eb4742bd40fe5a1e2435 From 54e02abad1e6cc1b20b8a7c335c908a3e8afa75f Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 3 Aug 2022 18:16:56 +0200 Subject: [PATCH 10/13] update toml11 --- deps/toml11 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/toml11 b/deps/toml11 index 1400dd2..c7627ff 160000 --- a/deps/toml11 +++ b/deps/toml11 @@ -1 +1 @@ -Subproject commit 1400dd223fb4297337266fcb5d04b700338aea71 +Subproject commit c7627ff6a1eb6f34fbd98369990a9442e2836c25 From 44b94c9e58838c3a49a877246f30df71c11622fd Mon Sep 17 00:00:00 2001 From: 20dka Date: Wed, 14 Sep 2022 01:36:16 +0200 Subject: [PATCH 11/13] add MP.GetPositionRaw(pid, vid) fix vehicles sometimes not deleting for all players --- Changelog.md | 2 + include/Client.h | 4 ++ include/Common.h | 4 ++ include/TLuaEngine.h | 1 + include/TServer.h | 1 + src/Client.cpp | 17 ++++++ src/TLuaEngine.cpp | 32 +++++++++++ src/TServer.cpp | 127 ++++++++++++++++++++++++++++++++++--------- 8 files changed, 163 insertions(+), 25 deletions(-) diff --git a/Changelog.md b/Changelog.md index e8f3a6a..f719f10 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ - ADDED MP.JsonEncode() and MP.JsonDecode(), which turn lua tables into json and vice-versa - ADDED FS.ListFiles and FS.ListDirectories - ADDED onFileChanged event, triggered when a server plugin file changes +- ADDED MP.GetPositionRaw(), which can be used to retrieve the latest position packet per player, per vehicle - FIXED `ip` in MP.GetIdentifiers - FIXED issue with client->server events which contain ':' - FIXED a fatal exception on LuaEngine startup if Resources/Server is a symlink @@ -13,6 +14,7 @@ - FIXED incorrect timing calculation of Lua EventTimer loop - FIXED bug which caused hot-reload not to report syntax errors - FIXED missing error messages on some event handler calls +- FIXED vehicles not deleting for all players if an edit was cancelled by Lua # v3.0.2 diff --git a/include/Client.h b/include/Client.h index d513659..114ff3e 100644 --- a/include/Client.h +++ b/include/Client.h @@ -39,11 +39,13 @@ public: void AddNewCar(int Ident, const std::string& Data); void SetCarData(int Ident, const std::string& Data); + void SetCarPosition(int Ident, const std::string& Data); TVehicleDataLockPair GetAllCars(); void SetName(const std::string& Name) { mName = Name; } void SetRoles(const std::string& Role) { mRole = Role; } void SetIdentifier(const std::string& key, const std::string& value) { mIdentifiers[key] = value; } std::string GetCarData(int Ident); + std::string GetCarPositionRaw(int Ident); void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; } void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; } void SetTCPSock(SOCKET CSock) { mSocket[0] = CSock; } @@ -93,7 +95,9 @@ private: std::unordered_map mIdentifiers; bool mIsGuest = false; mutable std::mutex mVehicleDataMutex; + mutable std::mutex mVehiclePositionMutex; TSetOfVehicleData mVehicleData; + SparseArray mVehiclePosition; std::string mName = "Unknown Client"; SOCKET mSocket[2] { SOCKET(0), SOCKET(0) }; sockaddr_in mUDPAddress {}; // is this initialization OK? yes it is diff --git a/include/Common.h b/include/Common.h index c77ffd1..37c891f 100644 --- a/include/Common.h +++ b/include/Common.h @@ -4,6 +4,7 @@ extern TSentry Sentry; #include +#include #include #include #include @@ -33,6 +34,9 @@ struct Version { std::string AsString(); }; +template +using SparseArray = std::unordered_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 diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 2041687..b3cf883 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -211,6 +211,7 @@ private: sol::table Lua_GetPlayers(); std::string Lua_GetPlayerName(int ID); sol::table Lua_GetPlayerVehicles(int ID); + std::pair Lua_GetPositionRaw(int PID, int VID); sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port); sol::table Lua_JsonDecode(const std::string& str); int Lua_GetPlayerIDByName(const std::string& Name); diff --git a/include/TServer.h b/include/TServer.h index 2e51e12..698835a 100644 --- a/include/TServer.h +++ b/include/TServer.h @@ -38,4 +38,5 @@ private: static bool ShouldSpawn(TClient& c, const std::string& CarJson, int ID); static bool IsUnicycle(TClient& c, const std::string& CarJson); static void Apply(TClient& c, int VID, const std::string& pckt); + static void HandlePosition(TClient& c, std::string Packet); }; diff --git a/src/Client.cpp b/src/Client.cpp index 410d966..f93f825 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -47,6 +47,23 @@ TClient::TVehicleDataLockPair TClient::GetAllCars() { return { &mVehicleData, std::unique_lock(mVehicleDataMutex) }; } +std::string TClient::GetCarPositionRaw(int Ident) { + std::unique_lock lock(mVehiclePositionMutex); + try + { + return mVehiclePosition.at(Ident); + } + catch (const std::out_of_range& oor) { + return ""; + } + return ""; +} + +void TClient::SetCarPosition(int Ident, const std::string& Data) { + std::unique_lock lock(mVehiclePositionMutex); + mVehiclePosition[Ident] = Data; +} + std::string TClient::GetCarData(int Ident) { { // lock std::unique_lock lock(mVehicleDataMutex); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 1ac7fe3..303f8bf 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -517,6 +517,35 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) { return sol::lua_nil; } +std::pair TLuaEngine::StateThreadData::Lua_GetPositionRaw(int PID, int VID) { + std::pair Result; + auto MaybeClient = GetClient(mEngine->Server(), PID); + if (MaybeClient && !MaybeClient.value().expired()) { + auto Client = MaybeClient.value().lock(); + std::string VehiclePos = Client->GetCarPositionRaw(VID); + + if (VehiclePos.empty()) { + //return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Vehicle not found")); + Result.second = "Vehicle not found"; + return Result; + } + + sol::table t = Lua_JsonDecode(VehiclePos); + if (t == sol::lua_nil){ + Result.second = "Packet decode failed"; + } + //return std::make_tuple(Result, sol::make_object(StateView, sol::lua_nil)); + Result.first = t; + return Result; + } + else { + //return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Client expired")); + Result.second = "Client expired"; + return Result; + } +} + + sol::table TLuaEngine::StateThreadData::Lua_HttpCreateConnection(const std::string& host, uint16_t port) { auto table = mStateView.create_table(); constexpr const char* InternalClient = "__InternalClient"; @@ -673,6 +702,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI MPTable.set_function("GetPlayerVehicles", [&](int ID) -> sol::table { return Lua_GetPlayerVehicles(ID); }); + MPTable.set_function("GetPositionRaw", [&](int PID, int VID) -> std::pair { + return Lua_GetPositionRaw(PID, VID); + }); MPTable.set_function("SendChatMessage", &LuaAPI::MP::SendChatMessage); MPTable.set_function("GetPlayers", [&]() -> sol::table { return Lua_GetPlayers(); diff --git a/src/TServer.cpp b/src/TServer.cpp index 48b2bbb..92e9061 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -15,6 +15,67 @@ #include "Json.h" +static std::optional> GetPidVid(std::string str) { + auto IDSep = str.find('-'); + std::string pid = str.substr(0, IDSep); + std::string vid = str.substr(IDSep + 1); + + if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { + try { + int PID = stoi(pid); + int VID = stoi(vid); + return {{ PID, VID }}; + } catch(const std::exception&) { + return std::nullopt; + } + } + return std::nullopt; +} + +TEST_CASE("GetPidVid") { + SUBCASE("Valid singledigit") { + const auto MaybePidVid = GetPidVid("0-1"); + CHECK(MaybePidVid); + auto [pid, vid] = MaybePidVid.value(); + + CHECK_EQ(pid, 0); + CHECK_EQ(vid, 1); + } + SUBCASE("Valid doubledigit") { + const auto MaybePidVid = GetPidVid("10-12"); + CHECK(MaybePidVid); + auto [pid, vid] = MaybePidVid.value(); + + CHECK_EQ(pid, 10); + CHECK_EQ(vid, 12); + } + SUBCASE("Empty string") { + const auto MaybePidVid = GetPidVid(""); + CHECK(!MaybePidVid); + } + SUBCASE("Invalid separator") { + const auto MaybePidVid = GetPidVid("0x0"); + CHECK(!MaybePidVid); + } + SUBCASE("Missing pid") { + const auto MaybePidVid = GetPidVid("-0"); + CHECK(!MaybePidVid); + } + SUBCASE("Missing vid") { + const auto MaybePidVid = GetPidVid("0-"); + CHECK(!MaybePidVid); + } + SUBCASE("Invalid pid") { + const auto MaybePidVid = GetPidVid("x-0"); + CHECK(!MaybePidVid); + } + SUBCASE("Invalid vid") { + const auto MaybePidVid = GetPidVid("0-x"); + CHECK(!MaybePidVid); + } +} + + TServer::TServer(const std::vector& Arguments) { beammp_info("BeamMP Server v" + Application::ServerVersionString()); Application::SetSubsystemStatus("Server", Application::Status::Starting); @@ -86,8 +147,8 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac std::any Res; char Code = Packet.at(0); - // V to Z - if (Code <= 90 && Code >= 86) { + // V to Y + if (Code <= 89 && Code >= 86) { PPSMonitor.IncrementInternalPPS(); Network.SendToAll(LockedClient.get(), Packet, false, false); return; @@ -145,6 +206,11 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac beammp_trace("got 'N' packet (" + std::to_string(Packet.size()) + ")"); Network.SendToAll(LockedClient.get(), Packet, false, true); return; + case 'Z': // position packet + PPSMonitor.IncrementInternalPPS(); + Network.SendToAll(LockedClient.get(), Packet, false, false); + + HandlePosition(*LockedClient, Packet); default: return; } @@ -223,13 +289,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ } } return; - case 'c': + case 'c': { beammp_trace(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - pid = Data.substr(0, Data.find('-')); - vid = Data.substr(Data.find('-') + 1, Data.find(':', 1) - Data.find('-') - 1); - if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { - PID = stoi(pid); - VID = stoi(vid); + auto MaybePidVid = GetPidVid(Data.substr(0, Data.find(':', 1))); + if (MaybePidVid) { + std::tie(PID, VID) = MaybePidVid.value(); } if (PID != -1 && VID != -1 && PID == c.GetID()) { auto Futures = LuaAPI::MP::Engine->TriggerEvent("onVehicleEdited", "", c.GetID(), VID, Packet.substr(3)); @@ -250,20 +314,17 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ c.SetUnicycleID(-1); } std::string Destroy = "Od:" + std::to_string(c.GetID()) + "-" + std::to_string(VID); - if (!Network.Respond(c, Destroy, true)) { - // TODO: handle - } + Network.SendToAll(nullptr, Destroy, true, true); c.DeleteCar(VID); } } return; - case 'd': + } + case 'd': { beammp_trace(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - pid = Data.substr(0, Data.find('-')); - vid = Data.substr(Data.find('-') + 1); - if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { - PID = stoi(pid); - VID = stoi(vid); + auto MaybePidVid = GetPidVid(Data); + if (MaybePidVid) { + std::tie(PID, VID) = MaybePidVid.value(); } if (PID != -1 && VID != -1 && PID == c.GetID()) { if (c.GetUnicycleID() == VID) { @@ -276,15 +337,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ beammp_debug(c.GetName() + (" deleted car with ID ") + std::to_string(VID)); } return; - case 'r': + } + case 'r': { beammp_trace(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - Pos = int(Data.find('-')); - pid = Data.substr(0, Pos++); - vid = Data.substr(Pos, Data.find(':') - Pos); - - if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { - PID = stoi(pid); - VID = stoi(vid); + auto MaybePidVid = GetPidVid(Data); + if (MaybePidVid) { + std::tie(PID, VID) = MaybePidVid.value(); } if (PID != -1 && VID != -1 && PID == c.GetID()) { @@ -293,6 +351,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ Network.SendToAll(&c, Packet, false, true); } return; + } case 't': beammp_trace(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); Network.SendToAll(&c, Packet, false, true); @@ -365,3 +424,21 @@ void TServer::InsertClient(const std::shared_ptr& NewClient) { WriteLock Lock(mClientsMutex); // TODO why is there 30+ threads locked here (void)mClients.insert(NewClient); } + +void TServer::HandlePosition(TClient& c, std::string Packet) { + // Zp:serverVehicleID:data + std::string withoutCode = Packet.substr(3); + auto NameDataSep = withoutCode.find(':', 2); + std::string ServerVehicleID = withoutCode.substr(2, NameDataSep - 2); + std::string Data = withoutCode.substr(NameDataSep + 1); + + // parse veh ID + auto MaybePidVid = GetPidVid(ServerVehicleID); + if (MaybePidVid) { + int PID = -1; + int VID = -1; + std::tie(PID, VID) = MaybePidVid.value(); + + c.SetCarPosition(VID, Data); + } +} From a84d042a8abb34d23f813e8e9b3a3ec394a4cd8f Mon Sep 17 00:00:00 2001 From: 20dka Date: Wed, 14 Sep 2022 12:36:54 +0200 Subject: [PATCH 12/13] add error messages to some lua events --- Changelog.md | 1 + include/LuaAPI.h | 10 ++++----- src/LuaAPI.cpp | 57 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/Changelog.md b/Changelog.md index f719f10..4b5e126 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ - ADDED FS.ListFiles and FS.ListDirectories - ADDED onFileChanged event, triggered when a server plugin file changes - ADDED MP.GetPositionRaw(), which can be used to retrieve the latest position packet per player, per vehicle +- ADDED error messages to some lua functions - FIXED `ip` in MP.GetIdentifiers - FIXED issue with client->server events which contain ':' - FIXED a fatal exception on LuaEngine startup if Resources/Server is a symlink diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 4871a0b..6d2af31 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -12,12 +12,12 @@ namespace MP { std::string GetOSName(); std::tuple GetServerVersion(); - bool TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& Data); - bool TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data); + std::pair TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& Data); + std::pair TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data); inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); } - void DropPlayer(int ID, std::optional MaybeReason); - void SendChatMessage(int ID, const std::string& Message); - void RemoveVehicle(int PlayerID, int VehicleID); + std::pair DropPlayer(int ID, std::optional MaybeReason); + std::pair SendChatMessage(int ID, const std::string& Message); + std::pair RemoveVehicle(int PlayerID, int VehicleID); void Set(int ConfigID, sol::object NewValue); bool IsPlayerGuest(int ID); bool IsPlayerConnected(int ID); diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 48b77d8..e26a1eb 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -110,72 +110,93 @@ TEST_CASE("LuaAPI::MP::GetServerVersion") { CHECK(pa == real.patch); } -static inline bool InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) { +static inline std::pair InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) { std::string Packet = "E:" + EventName + ":" + Data; - if (PlayerID == -1) + if (PlayerID == -1) { LuaAPI::MP::Engine->Network().SendToAll(nullptr, Packet, true, true); - else { + return {true, ""}; + } else { auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), PlayerID); if (!MaybeClient || MaybeClient.value().expired()) { - beammp_lua_error("TriggerClientEvent invalid Player ID"); - return false; + beammp_lua_errorf("TriggerClientEvent invalid Player ID '{}'", PlayerID); + return {false, "Invalid Player ID"}; } auto c = MaybeClient.value().lock(); if (!LuaAPI::MP::Engine->Network().Respond(*c, Packet, true)) { - beammp_lua_error("Respond failed, dropping client " + std::to_string(PlayerID)); + beammp_lua_errorf("Respond failed, dropping client {}", PlayerID); LuaAPI::MP::Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets"); - return false; + return {false, "Respond failed, dropping client"}; + } + return {true, ""}; } - return true; } -bool LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& DataObj) { +std::pair LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& DataObj) { std::string Data = DataObj.as(); return InternalTriggerClientEvent(PlayerID, EventName, Data); } -void LuaAPI::MP::DropPlayer(int ID, std::optional MaybeReason) { +std::pair LuaAPI::MP::DropPlayer(int ID, std::optional MaybeReason) { auto MaybeClient = GetClient(Engine->Server(), ID); if (!MaybeClient || MaybeClient.value().expired()) { - beammp_lua_error("Tried to drop client with id " + std::to_string(ID) + ", who doesn't exist"); - return; + beammp_lua_errorf("Tried to drop client with id {}, who doesn't exist", ID); + return {false, "Player does not exist"}; } auto c = MaybeClient.value().lock(); LuaAPI::MP::Engine->Network().ClientKick(*c, MaybeReason.value_or("No reason")); + return {true, ""}; } -void LuaAPI::MP::SendChatMessage(int ID, const std::string& Message) { +std::pair LuaAPI::MP::SendChatMessage(int ID, const std::string& Message) { + std::pair Result; std::string Packet = "C:Server: " + Message; if (ID == -1) { LogChatMessage(" (to everyone) ", -1, Message); Engine->Network().SendToAll(nullptr, Packet, true, true); + Result.first = true; } else { auto MaybeClient = GetClient(Engine->Server(), ID); if (MaybeClient && !MaybeClient.value().expired()) { auto c = MaybeClient.value().lock(); - if (!c->IsSynced()) - return; + if (!c->IsSynced()) { + Result.first = false; + Result.second = "Player still syncing data"; + return Result; + } LogChatMessage(" (to \"" + c->GetName() + "\")", -1, Message); Engine->Network().Respond(*c, Packet, true); + Result.first = true; } else { beammp_lua_error("SendChatMessage invalid argument [1] invalid ID"); + Result.first = false; + Result.second = "Invalid Player ID"; } + return Result; } + return Result; } -void LuaAPI::MP::RemoveVehicle(int PID, int VID) { +std::pair LuaAPI::MP::RemoveVehicle(int PID, int VID) { + std::pair Result; auto MaybeClient = GetClient(Engine->Server(), PID); if (!MaybeClient || MaybeClient.value().expired()) { beammp_lua_error("RemoveVehicle invalid Player ID"); - return; + Result.first = false; + Result.second = "Invalid Player ID"; + return Result; } auto c = MaybeClient.value().lock(); if (!c->GetCarData(VID).empty()) { std::string Destroy = "Od:" + std::to_string(PID) + "-" + std::to_string(VID); Engine->Network().SendToAll(nullptr, Destroy, true, true); c->DeleteCar(VID); + Result.first = true; + } else { + Result.first = false; + Result.second = "Vehicle does not exist"; } + return Result; } void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) { @@ -645,6 +666,6 @@ std::string LuaAPI::MP::JsonUnflatten(const std::string& json) { return nlohmann::json::parse(json).unflatten().dump(-1); } -bool LuaAPI::MP::TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data) { +std::pair LuaAPI::MP::TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data) { return InternalTriggerClientEvent(PlayerID, EventName, JsonEncode(Data)); } From 4b242c26fca9e0b899519e4abe62291e1261ff35 Mon Sep 17 00:00:00 2001 From: 20dka Date: Wed, 14 Sep 2022 18:53:44 +0200 Subject: [PATCH 13/13] optimize function argument passing in GetPidVid, TServer::HandlePosition --- src/TServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TServer.cpp b/src/TServer.cpp index 92e9061..b9d7d2d 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -15,7 +15,7 @@ #include "Json.h" -static std::optional> GetPidVid(std::string str) { +static std::optional> GetPidVid(const std::string& str) { auto IDSep = str.find('-'); std::string pid = str.substr(0, IDSep); std::string vid = str.substr(IDSep + 1); @@ -425,7 +425,7 @@ void TServer::InsertClient(const std::shared_ptr& NewClient) { (void)mClients.insert(NewClient); } -void TServer::HandlePosition(TClient& c, std::string Packet) { +void TServer::HandlePosition(TClient& c, const std::string& Packet) { // Zp:serverVehicleID:data std::string withoutCode = Packet.substr(3); auto NameDataSep = withoutCode.find(':', 2);