Skip to content

Commit

Permalink
Merge branch 'mr-132'
Browse files Browse the repository at this point in the history
fix(gta-net-five): Construct CPlayerInfo for g_player31. (mr-132):
 - dc7aecd fix(gta-net-five): Construct CPlayerInfo for g_player31.
  • Loading branch information
thorium-cfx committed Nov 6, 2023
2 parents d46956b + dc7aecd commit 29e01c1
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions code/components/gta-net-five/src/CloneExperiments.cpp
Expand Up @@ -738,7 +738,22 @@ static hook::cdecl_stub<CNetGamePlayer*(void*)> _netPlayerCtor([]()
#elif IS_RDR3
return hook::get_pattern("E8 ? ? ? ? 33 F6 48 8D 05 ? ? ? ? 48 8D 8B", -0x17);
#endif
});

#ifdef GTA_FIVE
// Offset is usually 0xA0, but can differ on ancient builds
static int g_CNetPlayerOffset_PlayerInfo = 0;
struct CPlayerInfo_Five;
static hook::cdecl_stub<CPlayerInfo_Five*(void*, uint64_t, void*)> _playerInfoCtor([]()
{
return hook::get_pattern("48 83 EC 30 65 4C 8B 0C 25 ? 00 00 00 0F 29 70 C8 45 33 ED", -0x18);
});
// this appears to be a substruct that makes up a good chunk of the beginning of the CPlayerInfo
static hook::cdecl_stub<void*()> _getLocalGamerInfo([]()
{
return hook::get_address<void*>(hook::get_pattern("E8 ? ? ? ? 33 D2 48 8B CB 4C 8B C0 E8 ? ? ? ? 4D 8B CE"), 1, 5);
});
#endif

static CNetGamePlayer*(*g_origAllocateNetPlayer)(void*);

Expand All @@ -760,9 +775,30 @@ static CNetGamePlayer* AllocateNetPlayer(void* mgr)
// RDR3 wants CNetworkPlayerMgr pointer in CNetGamePlayer
#ifdef IS_RDR3
*(rage::netPlayerMgrBase**)((uint64_t)player + 288) = g_playerMgr;
#endif

#endif

return player;
}

// fix: some Events expect the CPlayerInfo to be non-null in the CNetGamePlayer (It is not set in the constructor)
static void Player31_ApplyPlayerInfo(CNetGamePlayer* player)
{
static void* infoMem = nullptr;
if (!infoMem)
{
infoMem = malloc(0x2000);
CPlayerInfo_Five* info = _playerInfoCtor(infoMem, 0, _getLocalGamerInfo());
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = info;
}
else
{
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = (CPlayerInfo_Five*)infoMem;
}
}
// Clear the playerInfo after events
static void Player31_ClearPlayerInfo(CNetGamePlayer* player)
{
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = nullptr;
}

#include <minhook.h>
Expand Down Expand Up @@ -3023,6 +3059,8 @@ static void HandleNetGameEvent(const char* idata, size_t len)

if (ev)
{
Player31_ApplyPlayerInfo(g_player31);

ev->HandleReply(&rlBuffer, player);

#ifdef GTA_FIVE
Expand All @@ -3037,7 +3075,9 @@ static void HandleNetGameEvent(const char* idata, size_t len)
#endif

delete ev;
g_events.erase({ eventType, eventHeader });
g_events.erase({ eventType, eventHeader });

Player31_ClearPlayerInfo(g_player31);
}
}
}
Expand Down Expand Up @@ -3078,7 +3118,7 @@ static void HandleNetGameEvent(const char* idata, size_t len)
}
}
}


static void DecideNetGameEvent(rage::netGameEvent* ev, CNetGamePlayer* player, CNetGamePlayer* unkConn, rage::datBitBuffer* buffer, uint16_t evH)
{
g_lastEventGotRejected = false;
Expand Down Expand Up @@ -4212,7 +4252,13 @@ static HookFunction hookFunctionTime([]()
MH_CreateHook((xbr::IsGameBuildOrGreater<1436>()) ? hook::get_pattern("83 C8 FF 4C 89 77 08 83 FD", -87) : hook::get_pattern("48 89 51 08 41 83 F8 02 44 0F 45 C8", -49), func, (void**)&g_origInitializeTime);
#endif

MH_EnableHook(MH_ALL_HOOKS);
MH_EnableHook(MH_ALL_HOOKS);

#ifdef GTA_FIVE
char* loc = hook::get_pattern<char>("48 8B 81 ? 00 00 00 48 83 C0 20 C3");
loc += 3;
g_CNetPlayerOffset_PlayerInfo = *(int*)loc;
#endif

#ifdef GTA_FIVE
if (xbr::IsGameBuildOrGreater<2372>())
Expand Down

0 comments on commit 29e01c1

Please sign in to comment.