From 9bac936124114547265a453e6891c62481aa99ed Mon Sep 17 00:00:00 2001 From: Disquse Date: Sat, 7 Oct 2023 19:53:35 +0300 Subject: [PATCH] tweak(net/five): shared method to get net object from entity Should decrease hard-to-read code duplication and ensure consistency. --- .../gta-net-five/src/CloneExperiments.cpp | 26 +++++++++++-------- .../gta-net-five/src/MumbleVoice.cpp | 8 +++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/code/components/gta-net-five/src/CloneExperiments.cpp b/code/components/gta-net-five/src/CloneExperiments.cpp index 74369710c8..fafd6c9923 100644 --- a/code/components/gta-net-five/src/CloneExperiments.cpp +++ b/code/components/gta-net-five/src/CloneExperiments.cpp @@ -437,20 +437,24 @@ static hook::cdecl_stub getPlayerPedForNetPlayer([]() }); #ifdef GTA_FIVE -static const uint32_t g_entityNetObjOffset = 208; +static constexpr uint32_t g_entityNetObjOffset = 208; #elif IS_RDR3 -static const uint32_t g_entityNetObjOffset = 224; +static constexpr uint32_t g_entityNetObjOffset = 224; #endif +rage::netObject* GetNetObjectFromEntity(void* entity) +{ + // technically must be at least CDynamicEntity + return *(rage::netObject**)((char*)entity + g_entityNetObjOffset); +} + rage::netObject* GetLocalPlayerPedNetObject() { auto ped = getPlayerPedForNetPlayer(GetLocalPlayer()); if (ped) { - auto netObj = *(rage::netObject**)((char*)ped + g_entityNetObjOffset); - - return netObj; + return GetNetObjectFromEntity(ped); } return nullptr; @@ -489,7 +493,7 @@ void HandleClientDrop(const NetLibraryClientInfo& info) if (ped) { - auto netObj = *(rage::netObject**)((char*)ped + g_entityNetObjOffset); + auto netObj = GetNetObjectFromEntity(ped); if (netObj) { @@ -4702,7 +4706,7 @@ static InitFunction initFunction([]() return; } - auto netObj = *(rage::netObject**)(entity + g_entityNetObjOffset); + auto netObj = GetNetObjectFromEntity(entity); static char blah[90000]; @@ -4743,7 +4747,7 @@ static InitFunction initFunction([]() return; } - auto netObj = *(rage::netObject**)(entity + g_entityNetObjOffset); + auto netObj = GetNetObjectFromEntity(entity); static char blah[90000]; @@ -4931,7 +4935,7 @@ static InitFunction initFunction([]() return; } - auto obj = *(rage::netObject**)(entity + g_entityNetObjOffset); + auto obj = GetNetObjectFromEntity(entity); auto data = context.GetArgument(1); @@ -5003,7 +5007,7 @@ static InitFunction initFunction([]() return; } - auto obj = *(rage::netObject**)(entity + g_entityNetObjOffset); + auto obj = GetNetObjectFromEntity(entity); //obj->GetBlender()->m_30(); obj->GetBlender()->m_58(); }); @@ -5011,7 +5015,7 @@ static InitFunction initFunction([]() static ConsoleCommand saveCloneCmd("save_clone", [](const std::string& address) { uintptr_t addressPtr = _strtoui64(address.c_str(), nullptr, 16); - auto netObj = *(rage::netObject**)(addressPtr + g_entityNetObjOffset); + auto netObj = GetNetObjectFromEntity((void*)addressPtr); static char blah[90000]; diff --git a/code/components/gta-net-five/src/MumbleVoice.cpp b/code/components/gta-net-five/src/MumbleVoice.cpp index 649ad5abc5..6330336427 100644 --- a/code/components/gta-net-five/src/MumbleVoice.cpp +++ b/code/components/gta-net-five/src/MumbleVoice.cpp @@ -701,6 +701,8 @@ static bool(*g_origIsPlayerTalking)(void*, void*); extern CNetGamePlayer* netObject__GetPlayerOwner(rage::netObject* object); +extern rage::netObject* GetNetObjectFromEntity(void* entity); + static bool _isPlayerTalking(void* mgr, char* playerData) { if (g_origIsPlayerTalking(mgr, playerData)) @@ -730,11 +732,7 @@ static bool _isPlayerTalking(void* mgr, char* playerData) if (ped) { -#ifdef GTA_FIVE - auto netObj = *(rage::netObject**)(ped + 208); -#elif IS_RDR3 - auto netObj = *(rage::netObject**)(ped + 224); -#endif + auto netObj = GetNetObjectFromEntity(ped); if (netObj) {