From 5bab1bc6afb566adc1b159ac662981f062f2e75f Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 16 Sep 2023 12:18:32 +0200 Subject: [PATCH] Move CClient pointer adjuster to separate function All process methods have a shifted CClient pointer, so this will be used more often. --- r5dev/engine/client/client.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/r5dev/engine/client/client.cpp b/r5dev/engine/client/client.cpp index 28604d29..e35a3e61 100644 --- a/r5dev/engine/client/client.cpp +++ b/r5dev/engine/client/client.cpp @@ -148,6 +148,25 @@ void* CClient::VSendSnapshot(CClient* pClient, CClientFrame* pFrame, int nTick, return v_CClient_SendSnapshot(pClient, pFrame, nTick, nTickAck); } +//--------------------------------------------------------------------------------- +// Purpose: some versions of the binary have an optimization that shifts the 'this' +// pointer of the CClient structure by 8 bytes to avoid having to cache the vftable +// pointer if it never get used. Here we shift it back so it aligns again. +//--------------------------------------------------------------------------------- +CClient* AdjustShiftedThisPointer(CClient* shiftedPointer) +{ +#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) + return shiftedPointer; +#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3) + /* Original function called method "CClient::ExecuteStringCommand" with an optimization + * that shifted the 'this' pointer with 8 bytes. + * Since this has been inlined with "CClient::ProcessStringCmd" as of S2, the shifting + * happens directly to anything calling this function. */ + char* pShifted = reinterpret_cast(shiftedPointer) - 8; + return reinterpret_cast(pShifted); +#endif // !GAMEDLL_S0 || !GAMEDLL_S1 +} + //--------------------------------------------------------------------------------- // Purpose: process string commands (kicking anyone attempting to DOS) // Input : *pClient - (ADJ) @@ -157,16 +176,7 @@ void* CClient::VSendSnapshot(CClient* pClient, CClientFrame* pFrame, int nTick, bool CClient::VProcessStringCmd(CClient* pClient, NET_StringCmd* pMsg) { #ifndef CLIENT_DLL -#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) - CClient* pClient_Adj = pClient; -#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3) - /* Original function called method "CClient::ExecuteStringCommand" with an optimization - * that shifted the 'this' pointer with 8 bytes. - * Since this has been inlined with "CClient::ProcessStringCmd" as of S2, the shifting - * happens directly to anything calling this function. */ - char* pShifted = reinterpret_cast(pClient) - 8; - CClient* pClient_Adj = reinterpret_cast(pShifted); -#endif // !GAMEDLL_S0 || !GAMEDLL_S1 + CClient* pClient_Adj = AdjustShiftedThisPointer(pClient); // Jettison the cmd if the client isn't active. if (!pClient_Adj->IsActive())