From 7a6290774bf721681f685d0fd9bde9e110624caf Mon Sep 17 00:00:00 2001 From: Disquse Date: Thu, 11 Jan 2024 15:25:51 +0300 Subject: [PATCH 1/2] fix(gta-game/five): update `CNetGamePlayer` accessors to support b3095 This was forgotten during the initial work on 3095 support for FiveM. Also did some minor code cleanup. --- .../gta-game-five/include/NetworkPlayerMgr.h | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/code/components/gta-game-five/include/NetworkPlayerMgr.h b/code/components/gta-game-five/include/NetworkPlayerMgr.h index 28e6161b52..d20fc46163 100644 --- a/code/components/gta-game-five/include/NetworkPlayerMgr.h +++ b/code/components/gta-game-five/include/NetworkPlayerMgr.h @@ -12,13 +12,13 @@ #include #define DECLARE_ACCESSOR(x) \ - decltype(impl.m2372.x)& x() \ - { \ - return (xbr::IsGameBuildOrGreater<2372>()) ? impl.m2372.x : (xbr::IsGameBuildOrGreater<2060>()) ? impl.m2060.x : impl.m1604.x; \ + decltype(impl.m3095.x)& x() \ + { \ + return (xbr::IsGameBuildOrGreater<3095>()) ? impl.m3095.x : (xbr::IsGameBuildOrGreater<2372>()) ? impl.m2372.x : (xbr::IsGameBuildOrGreater<2060>()) ? impl.m2060.x : impl.m1604.x; \ } \ - const decltype(impl.m2372.x)& x() const \ - { \ - return (xbr::IsGameBuildOrGreater<2372>()) ? impl.m2372.x : (xbr::IsGameBuildOrGreater<2060>()) ? impl.m2060.x : impl.m1604.x; \ + const decltype(impl.m3095.x)& x() const \ + { \ + return (xbr::IsGameBuildOrGreater<3095>()) ? impl.m3095.x : (xbr::IsGameBuildOrGreater<2372>()) ? impl.m2372.x : (xbr::IsGameBuildOrGreater<2060>()) ? impl.m2060.x : impl.m1604.x; \ } #ifdef COMPILING_GTA_GAME_FIVE @@ -85,6 +85,7 @@ class CNetGamePlayer : public rage::netPlayer char end[EndPad]; }; + // Do not forget to update `DECLARE_ACCESSOR` define when adding new impl! union { Impl<12, 0, 28> m1604; @@ -96,14 +97,14 @@ class CNetGamePlayer : public rage::netPlayer public: void* GetPlayerInfo() { - return (xbr::IsGameBuildOrGreater<3095>()) ? impl.m3095.playerInfo : (xbr::IsGameBuildOrGreater<2372>()) ? impl.m2372.playerInfo : (xbr::IsGameBuildOrGreater<2060>()) ? impl.m2060.playerInfo : impl.m1604.playerInfo; + return playerInfo(); } public: - DECLARE_ACCESSOR(nonPhysicalPlayerData); - DECLARE_ACCESSOR(activePlayerIndex); - DECLARE_ACCESSOR(physicalPlayerIndex); - DECLARE_ACCESSOR(playerInfo); + DECLARE_ACCESSOR(nonPhysicalPlayerData) + DECLARE_ACCESSOR(activePlayerIndex) + DECLARE_ACCESSOR(physicalPlayerIndex) + DECLARE_ACCESSOR(playerInfo) }; class CNetworkPlayerMgr @@ -111,3 +112,5 @@ class CNetworkPlayerMgr public: static GTA_GAME_EXPORT CNetGamePlayer* GetPlayer(int playerIndex); }; + +#undef DECLARE_ACCESSOR From ddcd4664b70b8518228fccda3e8e954545c10345 Mon Sep 17 00:00:00 2001 From: Disquse Date: Thu, 11 Jan 2024 15:37:03 +0300 Subject: [PATCH 2/2] tweak(extra-natives/five): minor tweaks in player extra natives - Use `GetPlayerInfo` method instead of raw `playerInfo` accessor. - Remove unnecessary float checks + other minor code cleanups. --- .../extra-natives-five/src/PlayerNatives.cpp | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/code/components/extra-natives-five/src/PlayerNatives.cpp b/code/components/extra-natives-five/src/PlayerNatives.cpp index 526a7846e5..e00ebe7660 100644 --- a/code/components/extra-natives-five/src/PlayerNatives.cpp +++ b/code/components/extra-natives-five/src/PlayerNatives.cpp @@ -1,11 +1,11 @@ #include -#include +#include #include #include #include - #include +#include "EntitySystem.h" static int(*netInterface_GetNumPhysicalPlayers)(); static CNetGamePlayer** (*netInterface_GetAllPhysicalPlayers)(); @@ -29,7 +29,7 @@ static void* getAndCheckPlayerInfo(fx::ScriptContext& context) return nullptr; } - return player->playerInfo(); + return player->GetPlayerInfo(); } template @@ -119,15 +119,14 @@ static HookFunction hookFunction([]() { bool result = false; - void* playerInfo = getAndCheckPlayerInfo(context); - - if (playerInfo) + if (void* playerInfo = getAndCheckPlayerInfo(context)) { float newStamina = context.GetArgument(1); - float maxStamina = *((float*)((char*)playerInfo + PlayerMaxStaminaOffset)); - if (newStamina && newStamina <= maxStamina) + float maxStamina = *(float*)((char*)playerInfo + PlayerMaxStaminaOffset); + + if (newStamina <= maxStamina) { - *((float*)((char*)playerInfo + PlayerStaminaOffset)) = newStamina; + *(float*)((char*)playerInfo + PlayerStaminaOffset) = newStamina; result = true; } } @@ -139,14 +138,12 @@ static HookFunction hookFunction([]() { bool result = false; - void* playerInfo = getAndCheckPlayerInfo(context); - - if (playerInfo) + if (void* playerInfo = getAndCheckPlayerInfo(context)) { float newMaxStamina = context.GetArgument(1); - if (newMaxStamina && newMaxStamina > 0.0) + if (newMaxStamina > 0.0) { - *((float*)((char*)playerInfo + PlayerMaxStaminaOffset)) = newMaxStamina; + *(float*)((char*)playerInfo + PlayerMaxStaminaOffset) = newMaxStamina; result = true; } }