From ac5d3dba9046c4882278eabb9edc8f7f5e8b61de Mon Sep 17 00:00:00 2001 From: Fabian Terhorst Date: Wed, 16 Oct 2024 18:03:49 +0200 Subject: [PATCH] fix(onesync): rdr3 net events v3 --- .../gta-net-five/src/netGameEvent.cpp | 43 ++++++++++++------- .../tests/server/TestServerEventComponent.cpp | 23 ++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 code/tests/server/TestServerEventComponent.cpp diff --git a/code/components/gta-net-five/src/netGameEvent.cpp b/code/components/gta-net-five/src/netGameEvent.cpp index 6e6e73b06f..aba46d4958 100644 --- a/code/components/gta-net-five/src/netGameEvent.cpp +++ b/code/components/gta-net-five/src/netGameEvent.cpp @@ -95,7 +95,7 @@ static std::unordered_set g_eventBlacklist; static std::unordered_set g_eventBlacklistV2; #ifdef IS_RDR3 -static std::unordered_map g_eventNames; +static std::unordered_map g_eventNames; #endif static void (*g_origAddEvent)(void*, rage::netGameEvent*); @@ -284,9 +284,17 @@ static uint16_t netEventMgr_MapEventId(uint16_t type, bool isSend) return type; } -static std::vector netEventMgr_GetIdentsToEventHash() +static std::vector& netEventMgr_GetIdentsToEventHash(bool invalidate = false) { - std::vector eventIdents; + static std::vector eventIdents {}; + static bool init {true}; + + if (!init && !invalidate) + { + return eventIdents; + } + + eventIdents.clear(); #ifdef GTA_FIVE auto eventMgr = *(char**)g_netEventMgr; @@ -303,11 +311,17 @@ static std::vector netEventMgr_GetIdentsToEventHash() #elif IS_RDR3 for (auto [type, name] : g_eventNames) { - eventIdents.resize(type + 1); + if (eventIdents.size() <= type) + { + eventIdents.resize(type + 1); + } + eventIdents[type] = HashRageString(name); } #endif + init = false; + return eventIdents; } @@ -398,7 +412,7 @@ static void SendGameEventRaw(uint16_t eventId, rage::netGameEvent* ev) if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4)) { - static std::vector eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); + std::vector& eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); if (ev->eventType >= eventIdentsToHash.size()) { @@ -609,12 +623,6 @@ void rage::HandleNetGameEventV2(net::packet::ServerNetGameEventV2& serverNetGame bool rejected = false; - // for all intents and purposes, the player will be 31 - auto lastIndex = player->physicalPlayerIndex(); - player->physicalPlayerIndex() = 31; - - auto eventMgr = *(char**)g_netEventMgr; - static std::unordered_map eventIdents = netEventMgr_GetEventHashIdents(); const auto eventIdentRes = eventIdents.find(eventNameHash); @@ -625,7 +633,11 @@ void rage::HandleNetGameEventV2(net::packet::ServerNetGameEventV2& serverNetGame return; } - if (eventMgr) + // for all intents and purposes, the player will be 31 + auto lastIndex = player->physicalPlayerIndex(); + player->physicalPlayerIndex() = 31; + + if (auto eventMgr = *(char**)g_netEventMgr) { #ifdef GTA_FIVE auto eventHandlerList = (TEventHandlerFn*)(eventMgr + (xbr::IsGameBuildOrGreater<2372>() ? 0x3B3D0 : xbr::IsGameBuildOrGreater<2060>() ? 0x3ABD0 : 0x3AB80)); @@ -657,6 +669,7 @@ void rage::HandleNetGameEventV2(net::packet::ServerNetGameEventV2& serverNetGame static void* RegisterNetGameEvent(void* eventMgr, uint16_t eventId, void* func, const char* name) { g_eventNames.insert({ eventId, name }); + netEventMgr_GetIdentsToEventHash(true); return g_origRegisterNetGameEvent(eventMgr, eventId, func, name); } #endif @@ -870,7 +883,7 @@ static void EventMgr_AddEvent(void* eventMgr, rage::netGameEvent* ev) if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4)) { - static std::vector eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); + std::vector& eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); if (ev->eventType >= eventIdentsToHash.size()) { // invalid event id @@ -930,7 +943,7 @@ static void DecideNetGameEvent(rage::netGameEvent* ev, CNetGamePlayer* player, C if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4)) { - static std::vector eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); + std::vector& eventIdentsToHash = netEventMgr_GetIdentsToEventHash(); if (ev->eventType >= eventIdentsToHash.size()) { @@ -1214,7 +1227,7 @@ const char* rage::netGameEvent::GetName() return "UNKNOWN_EVENT"; } - return findEvent->second; + return findEvent->second.c_str(); } #endif diff --git a/code/tests/server/TestServerEventComponent.cpp b/code/tests/server/TestServerEventComponent.cpp new file mode 100644 index 0000000000..f4abec160b --- /dev/null +++ b/code/tests/server/TestServerEventComponent.cpp @@ -0,0 +1,23 @@ +#include + +#include + +#include "ClientRegistry.h" +#include "ServerEventComponent.h" +#include "ServerEventComponentInstance.h" +#include "ServerInstance.h" + +TEST_CASE("Principal test") +{ + fwRefContainer serverInstance = ServerInstance::Create(); + serverInstance->SetComponent(new fx::ClientRegistry()); + const fx::ClientSharedPtr client = serverInstance->GetComponent()->MakeClient("test"); + const fx::ClientSharedPtr entityClient = client; + const int slotId = 127; + auto sec = fx::ServerEventComponentInstance::Create(); + sec->TriggerClientEvent("onPlayerJoining", fmt::sprintf("%d", client->GetNetId()), entityClient->GetNetId(), entityClient->GetName(), slotId); + REQUIRE(fx::ServerEventComponentInstance::lastClientEvent.has_value() == true); + REQUIRE(fx::ServerEventComponentInstance::lastClientEvent.value().eventName == "onPlayerJoining"); + REQUIRE(fx::ServerEventComponentInstance::lastClientEvent.value().targetSrc == std::to_string(client->GetNetId())); + REQUIRE(fx::ServerEventComponentInstance::lastClientEvent.value().data.size() == 1); +}