Thin templates for Memory

This commit is contained in:
Silent 2017-12-23 18:25:58 +01:00
parent b7cf823ee7
commit 1e9d526c7c
2 changed files with 316 additions and 317 deletions

View file

@ -32,22 +32,28 @@ enum
PATCH_JUMP PATCH_JUMP
}; };
inline signed char* GetVer() namespace Memory
{ {
static signed char bVer = -1; namespace internal
return &bVer; {
} inline signed char* GetVer()
{
static signed char bVer = -1;
return &bVer;
}
inline bool* GetEuropean() inline bool* GetEuropean()
{ {
static bool bEuropean; static bool bEuropean;
return &bEuropean; return &bEuropean;
} }
inline void* GetDummy() inline uintptr_t GetDummy()
{ {
static uintptr_t dwDummy; static uintptr_t dwDummy;
return &dwDummy; return reinterpret_cast<uintptr_t>(&dwDummy);
}
}
} }
template<typename AT> template<typename AT>
@ -58,365 +64,358 @@ inline AT DynBaseAddress(AT address)
#if defined _GTA_III #if defined _GTA_III
inline void InitializeVersions() namespace Memory
{ {
signed char* bVer = GetVer(); namespace internal
if ( *bVer == -1 )
{ {
if (*(uint32_t*)0x5C1E70 == 0x53E58955) *bVer = 0; inline void InitializeVersions()
else if (*(uint32_t*)0x5C2130 == 0x53E58955) *bVer = 1; {
else if (*(uint32_t*)0x5C6FD0 == 0x53E58955) *bVer = 2; signed char* bVer = GetVer();
}
}
// This function initially detects III version then chooses the address basing on game version if ( *bVer == -1 )
template<typename T> {
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam) if (*(uint32_t*)0x5C1E70 == 0x53E58955) *bVer = 0;
{ else if (*(uint32_t*)0x5C2130 == 0x53E58955) *bVer = 1;
InitializeVersions(); else if (*(uint32_t*)0x5C6FD0 == 0x53E58955) *bVer = 2;
}
}
signed char bVer = *GetVer(); // This function initially detects III version then chooses the address basing on game version
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
switch ( bVer ) signed char bVer = *GetVer();
{
case 1: switch ( bVer )
#ifdef assert {
assert(address11); case 1:
#endif #ifdef assert
return (T)address11; assert(address11);
case 2: #endif
#ifdef assert return address11;
assert(addressSteam); case 2:
#endif #ifdef assert
return (T)addressSteam; assert(addressSteam);
default: #endif
#ifdef assert return addressSteam;
assert(address10); default:
#endif #ifdef assert
return (T)address10; assert(address10);
#endif
return address10;
}
}
} }
} }
#elif defined _GTA_VC #elif defined _GTA_VC
inline void InitializeVersions() namespace Memory
{ {
signed char* bVer = GetVer(); namespace internal
if ( *bVer == -1 )
{ {
if (*(uint32_t*)0x667BF0 == 0x53E58955) *bVer = 0; inline void InitializeVersions()
else if (*(uint32_t*)0x667C40 == 0x53E58955) *bVer = 1; {
else if (*(uint32_t*)0x666BA0 == 0x53E58955) *bVer = 2; signed char* bVer = GetVer();
}
}
// This function initially detects VC version then chooses the address basing on game version if ( *bVer == -1 )
template<typename T> {
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam) if (*(uint32_t*)0x667BF0 == 0x53E58955) *bVer = 0;
{ else if (*(uint32_t*)0x667C40 == 0x53E58955) *bVer = 1;
InitializeVersions(); else if (*(uint32_t*)0x666BA0 == 0x53E58955) *bVer = 2;
}
}
signed char bVer = *GetVer(); // This function initially detects VC version then chooses the address basing on game version
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
switch ( bVer ) signed char bVer = *GetVer();
{
case 1: switch ( bVer )
#ifdef assert {
assert(address11); case 1:
#endif #ifdef assert
return (T)address11; assert(address11);
case 2: #endif
#ifdef assert return address11;
assert(addressSteam); case 2:
#endif #ifdef assert
return (T)addressSteam; assert(addressSteam);
default: #endif
#ifdef assert return addressSteam;
assert(address10); default:
#endif #ifdef assert
return (T)address10; assert(address10);
#endif
return address10;
}
}
} }
} }
#elif defined _GTA_SA #elif defined _GTA_SA
inline void InitializeVersions() namespace Memory
{ {
signed char* bVer = GetVer(); namespace internal
bool* bEuropean = GetEuropean();
if ( *bVer == -1 )
{ {
if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF ) inline void InitializeVersions()
{ {
// 1.0 US signed char* bVer = GetVer();
*bVer = 0;
*bEuropean = false; if ( *bVer == -1 )
} {
else if ( *(uint32_t*)DynBaseAddress(0x8245BC) == 0x94BF ) bool* bEuropean = GetEuropean();
{
// 1.0 EU if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF )
*bVer = 0; {
*bEuropean = true; // 1.0 US
} *bVer = 0;
else if ( *(uint32_t*)DynBaseAddress(0x8252FC) == 0x94BF ) *bEuropean = false;
{ }
// 1.01 US else if ( *(uint32_t*)DynBaseAddress(0x8245BC) == 0x94BF )
*bVer = 1; {
*bEuropean = false; // 1.0 EU
} *bVer = 0;
else if ( *(uint32_t*)DynBaseAddress(0x82533C) == 0x94BF ) *bEuropean = true;
{ }
// 1.01 EU else if ( *(uint32_t*)DynBaseAddress(0x8252FC) == 0x94BF )
*bVer = 1; {
*bEuropean = true; // 1.01 US
} *bVer = 1;
else if (*(uint32_t*)DynBaseAddress(0x85EC4A) == 0x94BF ) *bEuropean = false;
{ }
// 3.0 else if ( *(uint32_t*)DynBaseAddress(0x82533C) == 0x94BF )
*bVer = 2; {
*bEuropean = false; // 1.01 EU
*bVer = 1;
*bEuropean = true;
}
else if (*(uint32_t*)DynBaseAddress(0x85EC4A) == 0x94BF )
{
// 3.0
*bVer = 2;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x858D21) == 0x3539F633 )
{
// newsteam r1
*bVer = 3;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x858D51) == 0x3539F633 )
{
// newsteam r2
*bVer = 4;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x858C61) == 0x3539F633 )
{
// newsteam r2 lv
*bVer = 5;
*bEuropean = false;
}
}
} }
else if ( *(uint32_t*)DynBaseAddress(0x858D21) == 0x3539F633 ) inline void InitializeRegion_10()
{ {
// newsteam r1 signed char* bVer = GetVer();
*bVer = 3;
*bEuropean = false; if ( *bVer == -1 )
{
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)0x82457C == 0x94BF )
{
*bVer = 0;
*bEuropean = false;
}
else if ( *(uint32_t*)0x8245BC == 0x94BF )
{
*bVer = 0;
*bEuropean = true;
}
else
{
#ifdef assert
assert(!"AddressByRegion_10 on non-1.0 EXE!");
#endif
}
}
} }
else if ( *(uint32_t*)DynBaseAddress(0x858D51) == 0x3539F633 )
inline void InitializeRegion_11()
{ {
// newsteam r2 signed char* bVer = GetVer();
*bVer = 4;
*bEuropean = false; if ( *bVer == -1 )
{
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)0x8252FC == 0x94BF )
{
*bVer = 1;
*bEuropean = false;
}
else if ( *(uint32_t*)0x82533C == 0x94BF )
{
*bVer = 1;
*bEuropean = true;
}
else
{
#ifdef assert
assert(!"AddressByRegion_11 on non-1.01 EXE!");
#endif
}
}
} }
else if ( *(uint32_t*)DynBaseAddress(0x858C61) == 0x3539F633 )
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV)
{ {
// newsteam r2 lv InitializeVersions();
*bVer = 5;
*bEuropean = false; signed char bVer = *GetVer();
const bool bEuropean = *GetEuropean();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( address11 == 0 )
return GetDummy();
// Adjust to US if needed
if ( bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( addressSteam == 0 )
return GetDummy();
return addressSteam;
case 3:
return GetDummy();
case 4:
#ifdef assert
assert(addressNewsteamR2);
#endif
if ( addressNewsteamR2 == 0 )
return GetDummy();
return DynBaseAddress(addressNewsteamR2);
case 5:
#ifdef assert
assert(addressNewsteamR2_LV);
#endif
if ( addressNewsteamR2_LV == 0 )
return GetDummy();
return DynBaseAddress(addressNewsteamR2_LV);
default:
#ifdef assert
assert(address10);
#endif
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return address10;
}
}
inline uintptr_t AddressByRegion_10(uintptr_t address10)
{
InitializeRegion_10();
const bool bEuropean = *GetEuropean();
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return address10;
}
inline uintptr_t AddressByRegion_11(uintptr_t address11)
{
InitializeRegion_11();
const bool bEuropean = *GetEuropean();
// Adjust to US if needed
if ( !bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return address11;
} }
} }
} }
inline void InitializeRegion_10()
{
bool* bEuropean = GetEuropean();
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if ( *(uint32_t*)0x82457C == 0x94BF )
{
*bVer = 0;
*bEuropean = false;
}
else if ( *(uint32_t*)0x8245BC == 0x94BF )
{
*bVer = 0;
*bEuropean = true;
}
else
{
#ifdef assert
assert(!"AddressByRegion_10 on non-1.0 EXE!");
#endif #endif
}
}
}
inline void InitializeRegion_11() #if defined _GTA_III || defined _GTA_VC
{
bool* bEuropean = GetEuropean();
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if ( *(uint32_t*)0x8252FC == 0x94BF )
{
*bVer = 1;
*bEuropean = false;
}
else if ( *(uint32_t*)0x82533C == 0x94BF )
{
*bVer = 1;
*bEuropean = true;
}
else
{
#ifdef assert
assert(!"AddressByRegion_11 on non-1.01 EXE!");
#endif
}
}
}
// This function initially detects SA version then chooses the address basing on game version
template<typename T> template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam) inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{ {
InitializeVersions(); return T(Memory::internal::AddressByVersion( address10, address11, addressSteam ));
}
signed char bVer = *GetVer(); #elif defined _GTA_SA
bool bEuropean = *GetEuropean();
switch ( bVer ) template<typename T>
{ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
case 1: {
#ifdef assert return T(Memory::internal::AddressByVersion( address10, address11, addressSteam, 0, 0 ));
assert(address11);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( !address11 )
return (T)GetDummy();
// Adjust to US if needed
if ( !bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return (T)address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( !addressSteam )
return (T)GetDummy();
return (T)addressSteam;
case 3:
case 4:
case 5:
// TODO: DO
return (T)GetDummy();
default:
#ifdef assert
assert(address10);
#endif
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return (T)address10;
}
} }
template<typename T> template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV) inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV)
{ {
InitializeVersions(); return T(Memory::internal::AddressByVersion( address10, address11, addressSteam, addressNewsteamR2, addressNewsteamR2_LV ));
signed char bVer = *GetVer();
bool bEuropean = *GetEuropean();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( !address11 )
return (T)GetDummy();
// Adjust to US if needed
if ( bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return (T)address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
// Safety measures - if null, return dummy var pointer to prevent a crash
if ( !addressSteam )
return (T)GetDummy();
return (T)addressSteam;
case 3:
return (T)GetDummy();
case 4:
#ifdef assert
assert(addressNewsteamR2);
#endif
if ( !addressNewsteamR2 )
return (T)GetDummy();
return (T)DynBaseAddress(addressNewsteamR2);
case 5:
#ifdef assert
assert(addressNewsteamR2_LV);
#endif
if ( !addressNewsteamR2_LV )
return (T)GetDummy();
return (T)DynBaseAddress(addressNewsteamR2_LV);
default:
#ifdef assert
assert(address10);
#endif
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return (T)address10;
}
} }
template<typename T> template<typename T>
inline T AddressByRegion_10(uintptr_t address10) inline T AddressByRegion_10(uintptr_t address10)
{ {
InitializeRegion_10(); return T(Memory::internal::AddressByRegion_10(address10));
bool bEuropean = *GetEuropean();
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return (T)address10;
} }
template<typename T> template<typename T>
inline T AddressByRegion_11(uintptr_t address11) inline T AddressByRegion_11(uintptr_t address11)
{ {
InitializeRegion_11(); return T(Memory::internal::AddressByRegion_11(address11));
bool bEuropean = *GetEuropean();
// Adjust to US if needed
if ( !bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return (T)address11;
} }
#endif #endif

View file

@ -715,7 +715,7 @@ void DrawRect_HalfPixel_Steam(CRect& rect, const CRGBA& rgba)
char* GetMyDocumentsPathSA() char* GetMyDocumentsPathSA()
{ {
static char cUserFilesPath[MAX_PATH]; static char cUserFilesPath[MAX_PATH];
static char* const ppTempBufPtr = *GetVer() == 0 ? *AddressByRegion_10<char**>(0x744FE5) : cUserFilesPath; static char* const ppTempBufPtr = *Memory::internal::GetVer() == 0 ? *AddressByRegion_10<char**>(0x744FE5) : cUserFilesPath;
static bool initPath = [&] () { static bool initPath = [&] () {
char** const ppUserFilesDir = AddressByVersion<char**>(0x74503F, 0x74586F, 0x77EE50, 0x77902B, 0x778F1B); char** const ppUserFilesDir = AddressByVersion<char**>(0x74503F, 0x74586F, 0x77EE50, 0x77902B, 0x778F1B);
@ -1707,7 +1707,7 @@ void __declspec(naked) TrailerDoubleRWheelsFix2_Steam()
} }
} }
static void* LoadFLAC_JumpBack = AddressByVersion<void*>(0x4F3743, *GetVer() == 1 ? (*(BYTE*)0x4F3A50 == 0x6A ? 0x4F3BA3 : 0x5B6B81) : 0, 0x4FFC3F); static void* LoadFLAC_JumpBack = AddressByVersion<void*>(0x4F3743, *Memory::internal::GetVer() == 1 ? (*(BYTE*)0x4F3A50 == 0x6A ? 0x4F3BA3 : 0x5B6B81) : 0, 0x4FFC3F);
void __declspec(naked) LoadFLAC() void __declspec(naked) LoadFLAC()
{ {
_asm _asm