Add post event(s) (#364)

Adds `post*` events which are triggered after the respective `on*` event
has completed and the results have been sent.

They have the same arguments as the `on*` function, with the exception
that another argument is added in the beginning which contains whether
the `on*` variant was cancelled.
This commit is contained in:
Lion 2024-09-19 01:37:17 +02:00 committed by GitHub
commit 63b2a8e4a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 15 deletions

View File

@ -423,15 +423,24 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
Reason = "No guests are allowed on this server! To join, sign up at: forum.beammp.com."; Reason = "No guests are allowed on this server! To join, sign up at: forum.beammp.com.";
} }
bool Allowed = true;
if (NotAllowed) { if (NotAllowed) {
ClientKick(*Client, "you are not allowed on the server!"); Allowed = false;
return {}; }
} else if (NotAllowedWithReason) { if (NotAllowedWithReason) {
ClientKick(*Client, Reason); Allowed = false;
return {};
} }
if (mServer.ClientCount() < size_t(Application::Settings.getAsInt(Settings::Key::General_MaxPlayers))) { if (NotAllowed) {
ClientKick(*Client, "you are not allowed on the server!");
} else if (NotAllowedWithReason) {
ClientKick(*Client, Reason);
}
if (!Allowed) {
return {};
} else if (mServer.ClientCount() < size_t(Application::Settings.getAsInt(Settings::Key::General_MaxPlayers))) {
beammp_info("Identification success"); beammp_info("Identification success");
mServer.InsertClient(Client); mServer.InsertClient(Client);
TCPClient(Client); TCPClient(Client);
@ -439,6 +448,10 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
ClientKick(*Client, "Server full!"); 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; return Client;
} }

View File

@ -20,6 +20,7 @@
#include "Client.h" #include "Client.h"
#include "Common.h" #include "Common.h"
#include "CustomAssert.h" #include "CustomAssert.h"
#include "TLuaEngine.h"
#include "TNetwork.h" #include "TNetwork.h"
#include "TPPSMonitor.h" #include "TPPSMonitor.h"
#include <TLuaPlugin.h> #include <TLuaPlugin.h>
@ -222,16 +223,18 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Message); auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Message);
TLuaEngine::WaitForAll(Futures); TLuaEngine::WaitForAll(Futures);
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), PacketAsString.substr(PacketAsString.find(':', 3) + 1)); LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), PacketAsString.substr(PacketAsString.find(':', 3) + 1));
if (std::any_of(Futures.begin(), Futures.end(), bool Rejected = std::any_of(Futures.begin(), Futures.end(),
[](const std::shared_ptr<TLuaResult>& Elem) { [](const std::shared_ptr<TLuaResult>& Elem) {
return !Elem->Error return !Elem->Error
&& Elem->Result.is<int>() && Elem->Result.is<int>()
&& bool(Elem->Result.as<int>()); && bool(Elem->Result.as<int>());
})) { });
break; 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); auto PostFutures = LuaAPI::MP::Engine->TriggerEvent("postChatMessage", "", !Rejected, LockedClient->GetID(), LockedClient->GetName(), Message);
Network.SendToAll(nullptr, StringToVector(SanitizedPacket), true, true); LuaAPI::MP::Engine->ReportErrors(PostFutures);
return; return;
} }
case 'E': case 'E':
@ -313,9 +316,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
return !Result->Error && Result->Result.is<int>() && Result->Result.as<int>() != 0; return !Result->Error && Result->Result.is<int>() && Result->Result.as<int>() != 0;
}); });
bool SpawnConfirmed = false;
if (ShouldSpawn(c, CarJson, CarID) && !ShouldntSpawn) { if (ShouldSpawn(c, CarJson, CarID) && !ShouldntSpawn) {
c.AddNewCar(CarID, Packet); c.AddNewCar(CarID, Packet);
Network.SendToAll(nullptr, StringToVector(Packet), true, true); Network.SendToAll(nullptr, StringToVector(Packet), true, true);
SpawnConfirmed = true;
} else { } else {
if (!Network.Respond(c, StringToVector(Packet), true)) { if (!Network.Respond(c, StringToVector(Packet), true)) {
// TODO: handle // TODO: handle
@ -326,7 +331,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
// TODO: handle // TODO: handle
} }
beammp_debugf("{} (force : car limit/lua) removed ID {}", c.GetName(), CarID); 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; return;
case 'c': { case 'c': {
@ -345,10 +354,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
auto FoundPos = Packet.find('{'); auto FoundPos = Packet.find('{');
FoundPos = FoundPos == std::string::npos ? 0 : FoundPos; // attempt at sanitizing this FoundPos = FoundPos == std::string::npos ? 0 : FoundPos; // attempt at sanitizing this
bool Allowed = false;
if ((c.GetUnicycleID() != VID || IsUnicycle(c, Packet.substr(FoundPos))) if ((c.GetUnicycleID() != VID || IsUnicycle(c, Packet.substr(FoundPos)))
&& !ShouldntAllow) { && !ShouldntAllow) {
Network.SendToAll(&c, StringToVector(Packet), false, true); Network.SendToAll(&c, StringToVector(Packet), false, true);
Apply(c, VID, Packet); Apply(c, VID, Packet);
Allowed = true;
} else { } else {
if (c.GetUnicycleID() == VID) { if (c.GetUnicycleID() == VID) {
c.SetUnicycleID(-1); c.SetUnicycleID(-1);
@ -357,7 +368,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
Network.SendToAll(nullptr, StringToVector(Destroy), true, true); Network.SendToAll(nullptr, StringToVector(Destroy), true, true);
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onVehicleDeleted", "", c.GetID(), VID)); LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onVehicleDeleted", "", c.GetID(), VID));
c.DeleteCar(VID); 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; return;
} }