Skip to content

Commit

Permalink
Merge RedM net events v3 (mr-559)
Browse files Browse the repository at this point in the history
ac5d3db - fix(onesync): rdr3 net events v3
  • Loading branch information
prikolium-cfx committed Oct 21, 2024
2 parents 9a57c0f + ac5d3db commit 5d79501
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
43 changes: 28 additions & 15 deletions code/components/gta-net-five/src/netGameEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static std::unordered_set<uint16_t> g_eventBlacklist;
static std::unordered_set<uint32_t> g_eventBlacklistV2;

#ifdef IS_RDR3
static std::unordered_map<uint16_t, const char*> g_eventNames;
static std::unordered_map<uint16_t, std::string> g_eventNames;
#endif

static void (*g_origAddEvent)(void*, rage::netGameEvent*);
Expand Down Expand Up @@ -286,9 +286,17 @@ static uint16_t netEventMgr_MapEventId(uint16_t type, bool isSend)
return type;
}

static std::vector<uint32_t> netEventMgr_GetIdentsToEventHash()
static std::vector<uint32_t>& netEventMgr_GetIdentsToEventHash(bool invalidate = false)
{
std::vector<uint32_t> eventIdents;
static std::vector<uint32_t> eventIdents {};
static bool init {true};

if (!init && !invalidate)
{
return eventIdents;
}

eventIdents.clear();

#ifdef GTA_FIVE
auto eventMgr = *(char**)g_netEventMgr;
Expand All @@ -305,11 +313,17 @@ static std::vector<uint32_t> 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;
}

Expand Down Expand Up @@ -400,7 +414,7 @@ static void SendGameEventRaw(uint16_t eventId, rage::netGameEvent* ev)

if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4))
{
static std::vector<uint32_t> eventIdentsToHash = netEventMgr_GetIdentsToEventHash();
std::vector<uint32_t>& eventIdentsToHash = netEventMgr_GetIdentsToEventHash();

if (ev->eventType >= eventIdentsToHash.size())
{
Expand Down Expand Up @@ -611,12 +625,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<uint32_t, uint16_t> eventIdents = netEventMgr_GetEventHashIdents();

const auto eventIdentRes = eventIdents.find(eventNameHash);
Expand All @@ -627,7 +635,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));
Expand Down Expand Up @@ -659,6 +671,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
Expand Down Expand Up @@ -872,7 +885,7 @@ static void EventMgr_AddEvent(void* eventMgr, rage::netGameEvent* ev)

if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4))
{
static std::vector<uint32_t> eventIdentsToHash = netEventMgr_GetIdentsToEventHash();
std::vector<uint32_t>& eventIdentsToHash = netEventMgr_GetIdentsToEventHash();
if (ev->eventType >= eventIdentsToHash.size())
{
// invalid event id
Expand Down Expand Up @@ -932,7 +945,7 @@ static void DecideNetGameEvent(rage::netGameEvent* ev, CNetGamePlayer* player, C

if (icgi->IsNetVersionOrHigher(net::NetBitVersion::netVersion4))
{
static std::vector<uint32_t> eventIdentsToHash = netEventMgr_GetIdentsToEventHash();
std::vector<uint32_t>& eventIdentsToHash = netEventMgr_GetIdentsToEventHash();

if (ev->eventType >= eventIdentsToHash.size())
{
Expand Down Expand Up @@ -1212,7 +1225,7 @@ const char* rage::netGameEvent::GetName()
return "UNKNOWN_EVENT";
}

return findEvent->second;
return findEvent->second.c_str();
}
#endif

Expand Down
23 changes: 23 additions & 0 deletions code/tests/server/TestServerEventComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <StdInc.h>

#include <catch_amalgamated.hpp>

#include "ClientRegistry.h"
#include "ServerEventComponent.h"
#include "ServerEventComponentInstance.h"
#include "ServerInstance.h"

TEST_CASE("Principal test")
{
fwRefContainer<fx::ServerInstanceBase> serverInstance = ServerInstance::Create();
serverInstance->SetComponent(new fx::ClientRegistry());
const fx::ClientSharedPtr client = serverInstance->GetComponent<fx::ClientRegistry>()->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);
}

0 comments on commit 5d79501

Please sign in to comment.