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
{ {
namespace internal
{
inline signed char* GetVer()
{
static signed char bVer = -1; static signed char bVer = -1;
return &bVer; 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,8 +64,12 @@ inline AT DynBaseAddress(AT address)
#if defined _GTA_III #if defined _GTA_III
inline void InitializeVersions() namespace Memory
{ {
namespace internal
{
inline void InitializeVersions()
{
signed char* bVer = GetVer(); signed char* bVer = GetVer();
if ( *bVer == -1 ) if ( *bVer == -1 )
@ -68,12 +78,11 @@ inline void InitializeVersions()
else if (*(uint32_t*)0x5C2130 == 0x53E58955) *bVer = 1; else if (*(uint32_t*)0x5C2130 == 0x53E58955) *bVer = 1;
else if (*(uint32_t*)0x5C6FD0 == 0x53E58955) *bVer = 2; else if (*(uint32_t*)0x5C6FD0 == 0x53E58955) *bVer = 2;
} }
} }
// This function initially detects III version then chooses the address basing on game version // This function initially detects III version then chooses the address basing on game version
template<typename T> inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam) {
{
InitializeVersions(); InitializeVersions();
signed char bVer = *GetVer(); signed char bVer = *GetVer();
@ -81,27 +90,33 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
switch ( bVer ) switch ( bVer )
{ {
case 1: case 1:
#ifdef assert #ifdef assert
assert(address11); assert(address11);
#endif #endif
return (T)address11; return address11;
case 2: case 2:
#ifdef assert #ifdef assert
assert(addressSteam); assert(addressSteam);
#endif #endif
return (T)addressSteam; return addressSteam;
default: default:
#ifdef assert #ifdef assert
assert(address10); assert(address10);
#endif #endif
return (T)address10; return address10;
}
}
} }
} }
#elif defined _GTA_VC #elif defined _GTA_VC
inline void InitializeVersions() namespace Memory
{ {
namespace internal
{
inline void InitializeVersions()
{
signed char* bVer = GetVer(); signed char* bVer = GetVer();
if ( *bVer == -1 ) if ( *bVer == -1 )
@ -110,12 +125,11 @@ inline void InitializeVersions()
else if (*(uint32_t*)0x667C40 == 0x53E58955) *bVer = 1; else if (*(uint32_t*)0x667C40 == 0x53E58955) *bVer = 1;
else if (*(uint32_t*)0x666BA0 == 0x53E58955) *bVer = 2; else if (*(uint32_t*)0x666BA0 == 0x53E58955) *bVer = 2;
} }
} }
// This function initially detects VC version then chooses the address basing on game version // This function initially detects VC version then chooses the address basing on game version
template<typename T> inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam) {
{
InitializeVersions(); InitializeVersions();
signed char bVer = *GetVer(); signed char bVer = *GetVer();
@ -123,32 +137,39 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
switch ( bVer ) switch ( bVer )
{ {
case 1: case 1:
#ifdef assert #ifdef assert
assert(address11); assert(address11);
#endif #endif
return (T)address11; return address11;
case 2: case 2:
#ifdef assert #ifdef assert
assert(addressSteam); assert(addressSteam);
#endif #endif
return (T)addressSteam; return addressSteam;
default: default:
#ifdef assert #ifdef assert
assert(address10); assert(address10);
#endif #endif
return (T)address10; return address10;
}
}
} }
} }
#elif defined _GTA_SA #elif defined _GTA_SA
inline void InitializeVersions() namespace Memory
{ {
namespace internal
{
inline void InitializeVersions()
{
signed char* bVer = GetVer(); signed char* bVer = GetVer();
bool* bEuropean = GetEuropean();
if ( *bVer == -1 ) if ( *bVer == -1 )
{ {
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF ) if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF )
{ {
// 1.0 US // 1.0 US
@ -199,15 +220,16 @@ inline void InitializeVersions()
*bEuropean = false; *bEuropean = false;
} }
} }
} }
inline void InitializeRegion_10() inline void InitializeRegion_10()
{ {
bool* bEuropean = GetEuropean();
signed char* bVer = GetVer(); signed char* bVer = GetVer();
if ( *bVer == -1 ) if ( *bVer == -1 )
{ {
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)0x82457C == 0x94BF ) if ( *(uint32_t*)0x82457C == 0x94BF )
{ {
*bVer = 0; *bVer = 0;
@ -220,20 +242,21 @@ inline void InitializeRegion_10()
} }
else else
{ {
#ifdef assert #ifdef assert
assert(!"AddressByRegion_10 on non-1.0 EXE!"); assert(!"AddressByRegion_10 on non-1.0 EXE!");
#endif #endif
}
} }
} }
}
inline void InitializeRegion_11() inline void InitializeRegion_11()
{ {
bool* bEuropean = GetEuropean();
signed char* bVer = GetVer(); signed char* bVer = GetVer();
if ( *bVer == -1 ) if ( *bVer == -1 )
{ {
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)0x8252FC == 0x94BF ) if ( *(uint32_t*)0x8252FC == 0x94BF )
{ {
*bVer = 1; *bVer = 1;
@ -246,90 +269,30 @@ inline void InitializeRegion_11()
} }
else else
{ {
#ifdef assert #ifdef assert
assert(!"AddressByRegion_11 on non-1.01 EXE!"); assert(!"AddressByRegion_11 on non-1.01 EXE!");
#endif #endif
}
} }
} }
}
// This function initially detects SA version then chooses the address basing on game version inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV)
template<typename T> {
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions(); InitializeVersions();
signed char bVer = *GetVer(); signed char bVer = *GetVer();
bool bEuropean = *GetEuropean(); const bool bEuropean = *GetEuropean();
switch ( bVer ) switch ( bVer )
{ {
case 1: case 1:
#ifdef assert #ifdef assert
assert(address11); assert(address11);
#endif #endif
// Safety measures - if null, return dummy var pointer to prevent a crash // Safety measures - if null, return dummy var pointer to prevent a crash
if ( !address11 ) if ( address11 == 0 )
return (T)GetDummy(); return 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>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV)
{
InitializeVersions();
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 // Adjust to US if needed
if ( bEuropean && address11 > 0x746FA0 ) if ( bEuropean && address11 > 0x746FA0 )
@ -339,38 +302,38 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
else else
address11 -= 0x40; address11 -= 0x40;
} }
return (T)address11; return address11;
case 2: case 2:
#ifdef assert #ifdef assert
assert(addressSteam); assert(addressSteam);
#endif #endif
// Safety measures - if null, return dummy var pointer to prevent a crash // Safety measures - if null, return dummy var pointer to prevent a crash
if ( !addressSteam ) if ( addressSteam == 0 )
return (T)GetDummy(); return GetDummy();
return (T)addressSteam; return addressSteam;
case 3: case 3:
return (T)GetDummy(); return GetDummy();
case 4: case 4:
#ifdef assert #ifdef assert
assert(addressNewsteamR2); assert(addressNewsteamR2);
#endif #endif
if ( !addressNewsteamR2 ) if ( addressNewsteamR2 == 0 )
return (T)GetDummy(); return GetDummy();
return (T)DynBaseAddress(addressNewsteamR2); return DynBaseAddress(addressNewsteamR2);
case 5: case 5:
#ifdef assert #ifdef assert
assert(addressNewsteamR2_LV); assert(addressNewsteamR2_LV);
#endif #endif
if ( !addressNewsteamR2_LV ) if ( addressNewsteamR2_LV == 0 )
return (T)GetDummy(); return GetDummy();
return (T)DynBaseAddress(addressNewsteamR2_LV); return DynBaseAddress(addressNewsteamR2_LV);
default: default:
#ifdef assert #ifdef assert
assert(address10); assert(address10);
#endif #endif
// Adjust to EU if needed // Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 ) if ( bEuropean && address10 > 0x7466D0 )
{ {
@ -379,16 +342,15 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
else else
address10 += 0x40; address10 += 0x40;
} }
return (T)address10; return address10;
}
} }
}
template<typename T> inline uintptr_t AddressByRegion_10(uintptr_t address10)
inline T AddressByRegion_10(uintptr_t address10) {
{
InitializeRegion_10(); InitializeRegion_10();
bool bEuropean = *GetEuropean(); const bool bEuropean = *GetEuropean();
// Adjust to EU if needed // Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 ) if ( bEuropean && address10 > 0x7466D0 )
@ -398,15 +360,14 @@ inline T AddressByRegion_10(uintptr_t address10)
else else
address10 += 0x40; address10 += 0x40;
} }
return (T)address10; return address10;
} }
template<typename T> inline uintptr_t AddressByRegion_11(uintptr_t address11)
inline T AddressByRegion_11(uintptr_t address11) {
{
InitializeRegion_11(); InitializeRegion_11();
bool bEuropean = *GetEuropean(); const bool bEuropean = *GetEuropean();
// Adjust to US if needed // Adjust to US if needed
if ( !bEuropean && address11 > 0x746FA0 ) if ( !bEuropean && address11 > 0x746FA0 )
@ -416,7 +377,45 @@ inline T AddressByRegion_11(uintptr_t address11)
else else
address11 -= 0x40; address11 -= 0x40;
} }
return (T)address11; return address11;
}
}
}
#endif
#if defined _GTA_III || defined _GTA_VC
template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
return T(Memory::internal::AddressByVersion( address10, address11, addressSteam ));
}
#elif defined _GTA_SA
template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
return T(Memory::internal::AddressByVersion( address10, address11, addressSteam, 0, 0 ));
}
template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam, uintptr_t addressNewsteamR2, uintptr_t addressNewsteamR2_LV)
{
return T(Memory::internal::AddressByVersion( address10, address11, addressSteam, addressNewsteamR2, addressNewsteamR2_LV ));
}
template<typename T>
inline T AddressByRegion_10(uintptr_t address10)
{
return T(Memory::internal::AddressByRegion_10(address10));
}
template<typename T>
inline T AddressByRegion_11(uintptr_t address11)
{
return T(Memory::internal::AddressByRegion_11(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