Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CNetGamePlayer accessors to support b3095 in FiveM #2335

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions code/components/extra-natives-five/src/PlayerNatives.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <StdInc.h>
#include <ScriptEngine.h>

#include <ScriptEngine.h>
#include <ScriptSerialization.h>
#include <NetworkPlayerMgr.h>
#include <scrEngine.h>

#include <Hooking.h>
#include "EntitySystem.h"

static int(*netInterface_GetNumPhysicalPlayers)();
static CNetGamePlayer** (*netInterface_GetAllPhysicalPlayers)();
Expand All @@ -29,7 +29,7 @@ static void* getAndCheckPlayerInfo(fx::ScriptContext& context)
return nullptr;
}

return player->playerInfo();
return player->GetPlayerInfo();
}

template<typename T, int* offset>
Expand Down Expand Up @@ -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<float>(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;
}
}
Expand All @@ -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<float>(1);
if (newMaxStamina && newMaxStamina > 0.0)
if (newMaxStamina > 0.0)
{
*((float*)((char*)playerInfo + PlayerMaxStaminaOffset)) = newMaxStamina;
*(float*)((char*)playerInfo + PlayerMaxStaminaOffset) = newMaxStamina;
result = true;
}
}
Expand Down
25 changes: 14 additions & 11 deletions code/components/gta-game-five/include/NetworkPlayerMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
#include <netPeerAddress.h>

#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
Expand Down Expand Up @@ -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;
Expand All @@ -96,18 +97,20 @@ 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
{
public:
static GTA_GAME_EXPORT CNetGamePlayer* GetPlayer(int playerIndex);
};

#undef DECLARE_ACCESSOR
Loading