From 4444be0af989be0f035b0c01153f2d29acb4b183 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 15:50:42 +0200 Subject: [PATCH 1/6] add postVehicleSpawn event --- src/TServer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/TServer.cpp b/src/TServer.cpp index 22e7c538..8f279914 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -20,6 +20,7 @@ #include "Client.h" #include "Common.h" #include "CustomAssert.h" +#include "TLuaEngine.h" #include "TNetwork.h" #include "TPPSMonitor.h" #include @@ -314,9 +315,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ return !Result->Error && Result->Result.is() && Result->Result.as() != 0; }); + bool SpawnConfirmed = false; if (ShouldSpawn(c, CarJson, CarID) && !ShouldntSpawn) { c.AddNewCar(CarID, Packet); Network.SendToAll(nullptr, StringToVector(Packet), true, true); + SpawnConfirmed = true; } else { if (!Network.Respond(c, StringToVector(Packet), true)) { // TODO: handle @@ -326,7 +329,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ // TODO: handle } beammp_debugf("{} (force : car limit/lua) removed ID {}", c.GetName(), CarID); + SpawnConfirmed = false; } + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postVehicleSpawn", "", SpawnConfirmed, c.GetID(), CarID, Packet.substr(3)); + // the post event is not cancellable so we dont wait for it + LuaAPI::MP::Engine->ReportErrors(PostFutures); } return; case 'c': { From afa5a04043a4dcbadd9cab93f0e533ada8c2e6c9 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 16:08:00 +0200 Subject: [PATCH 2/6] add postPlayerAuth --- src/TNetwork.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index e08d78d6..8edb50b4 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -423,6 +423,18 @@ std::shared_ptr TNetwork::Authentication(TConnection&& RawConnection) { Reason = "No guests are allowed on this server! To join, sign up at: forum.beammp.com."; } + bool Allowed = true; + if (NotAllowed) { + Allowed = false; + } + if (NotAllowedWithReason) { + Allowed = false; + } + + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postPlayerAuth", "", Allowed, Client->GetName(), Client->GetRoles(), Client->IsGuest(), Client->GetIdentifiers()); + // the post event is not cancellable so we dont wait for it + LuaAPI::MP::Engine->ReportErrors(PostFutures); + if (NotAllowed) { ClientKick(*Client, "you are not allowed on the server!"); return {}; From 8f9db10474c45e67089e9e6254bc21780d02085e Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 16:09:21 +0200 Subject: [PATCH 3/6] move postPlayerAuth to after kick --- src/TNetwork.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 8edb50b4..e6d0163b 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -431,15 +431,17 @@ std::shared_ptr TNetwork::Authentication(TConnection&& RawConnection) { Allowed = false; } - auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postPlayerAuth", "", Allowed, Client->GetName(), Client->GetRoles(), Client->IsGuest(), Client->GetIdentifiers()); - // the post event is not cancellable so we dont wait for it - LuaAPI::MP::Engine->ReportErrors(PostFutures); - if (NotAllowed) { ClientKick(*Client, "you are not allowed on the server!"); - return {}; } else if (NotAllowedWithReason) { ClientKick(*Client, Reason); + } + + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postPlayerAuth", "", Allowed, Client->GetName(), Client->GetRoles(), Client->IsGuest(), Client->GetIdentifiers()); + // the post event is not cancellable so we dont wait for it + LuaAPI::MP::Engine->ReportErrors(PostFutures); + + if (!Allowed) { return {}; } From 86b37e8ae155b2743c43d59ea90ebd09c2e4a207 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 16:23:50 +0200 Subject: [PATCH 4/6] move postPlayerAuth later again, after client insert --- src/TNetwork.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index e6d0163b..986f5bb7 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -437,15 +437,10 @@ std::shared_ptr TNetwork::Authentication(TConnection&& RawConnection) { ClientKick(*Client, Reason); } - auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postPlayerAuth", "", Allowed, Client->GetName(), Client->GetRoles(), Client->IsGuest(), Client->GetIdentifiers()); - // the post event is not cancellable so we dont wait for it - LuaAPI::MP::Engine->ReportErrors(PostFutures); if (!Allowed) { return {}; - } - - if (mServer.ClientCount() < size_t(Application::Settings.getAsInt(Settings::Key::General_MaxPlayers))) { + } else if (mServer.ClientCount() < size_t(Application::Settings.getAsInt(Settings::Key::General_MaxPlayers))) { beammp_info("Identification success"); mServer.InsertClient(Client); TCPClient(Client); @@ -453,6 +448,10 @@ std::shared_ptr TNetwork::Authentication(TConnection&& RawConnection) { ClientKick(*Client, "Server full!"); } + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postPlayerAuth", "", Allowed, Client->GetName(), Client->GetRoles(), Client->IsGuest(), Client->GetIdentifiers()); + // the post event is not cancellable so we dont wait for it + LuaAPI::MP::Engine->ReportErrors(PostFutures); + return Client; } From 94768c916d617cc0f1e4eece1b149c8349b8d28b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 16:30:49 +0200 Subject: [PATCH 5/6] add postChatMessage --- src/TServer.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/TServer.cpp b/src/TServer.cpp index 8f279914..caa70f31 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -224,16 +224,18 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::vectorTriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Message); TLuaEngine::WaitForAll(Futures); LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), PacketAsString.substr(PacketAsString.find(':', 3) + 1)); - if (std::any_of(Futures.begin(), Futures.end(), - [](const std::shared_ptr& Elem) { - return !Elem->Error - && Elem->Result.is() - && bool(Elem->Result.as()); - })) { - break; + bool Rejected = std::any_of(Futures.begin(), Futures.end(), + [](const std::shared_ptr& Elem) { + return !Elem->Error + && Elem->Result.is() + && bool(Elem->Result.as()); + }); + if (!Rejected) { + std::string SanitizedPacket = fmt::format("C:{}: {}", LockedClient->GetName(), Message); + Network.SendToAll(nullptr, StringToVector(SanitizedPacket), true, true); } - std::string SanitizedPacket = fmt::format("C:{}: {}", LockedClient->GetName(), Message); - Network.SendToAll(nullptr, StringToVector(SanitizedPacket), true, true); + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postChatMessage", "", !Rejected, LockedClient->GetID(), LockedClient->GetName(), Message); + LuaAPI::MP::Engine->ReportErrors(PostFutures); return; } case 'E': From f70514a0213ad2f6daf685d5ed667be86f2f7223 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 18 Sep 2024 16:34:56 +0200 Subject: [PATCH 6/6] add postVehicleEdited why the fuck is it in past tense --- src/TServer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/TServer.cpp b/src/TServer.cpp index caa70f31..3b65c7dd 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -354,10 +354,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ auto FoundPos = Packet.find('{'); FoundPos = FoundPos == std::string::npos ? 0 : FoundPos; // attempt at sanitizing this + bool Allowed = false; if ((c.GetUnicycleID() != VID || IsUnicycle(c, Packet.substr(FoundPos))) && !ShouldntAllow) { Network.SendToAll(&c, StringToVector(Packet), false, true); Apply(c, VID, Packet); + Allowed = true; } else { if (c.GetUnicycleID() == VID) { c.SetUnicycleID(-1); @@ -365,7 +367,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ std::string Destroy = "Od:" + std::to_string(c.GetID()) + "-" + std::to_string(VID); Network.SendToAll(nullptr, StringToVector(Destroy), true, true); c.DeleteCar(VID); + Allowed = false; } + + auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postVehicleEdited", "", Allowed, c.GetID(), VID, Packet.substr(3)); + // the post event is not cancellable so we dont wait for it + LuaAPI::MP::Engine->ReportErrors(PostFutures); } return; }