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,6 +32,10 @@ enum
PATCH_JUMP PATCH_JUMP
}; };
namespace Memory
{
namespace internal
{
inline signed char* GetVer() inline signed char* GetVer()
{ {
static signed char bVer = -1; static signed char bVer = -1;
@ -44,10 +48,12 @@ inline bool* GetEuropean()
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,6 +64,10 @@ inline AT DynBaseAddress(AT address)
#if defined _GTA_III #if defined _GTA_III
namespace Memory
{
namespace internal
{
inline void InitializeVersions() inline void InitializeVersions()
{ {
signed char* bVer = GetVer(); signed char* bVer = GetVer();
@ -71,8 +81,7 @@ inline void InitializeVersions()
} }
// 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();
@ -84,22 +93,28 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
#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
namespace Memory
{
namespace internal
{
inline void InitializeVersions() inline void InitializeVersions()
{ {
signed char* bVer = GetVer(); signed char* bVer = GetVer();
@ -113,8 +128,7 @@ inline void InitializeVersions()
} }
// 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();
@ -126,29 +140,36 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
#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
namespace Memory
{
namespace internal
{
inline void InitializeVersions() 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
@ -203,11 +224,12 @@ inline void InitializeVersions()
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;
@ -229,11 +251,12 @@ inline void InitializeRegion_10()
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;
@ -253,14 +276,12 @@ inline void InitializeRegion_11()
} }
} }
// 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 )
{ {
@ -270,66 +291,8 @@ inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t ad
#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,34 +302,34 @@ 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);
@ -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