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
};
inline signed char* GetVer()
namespace Memory
{
static signed char bVer = -1;
return &bVer;
}
namespace internal
{
inline signed char* GetVer()
{
static signed char bVer = -1;
return &bVer;
}
inline bool* GetEuropean()
{
static bool bEuropean;
return &bEuropean;
}
inline bool* GetEuropean()
{
static bool bEuropean;
return &bEuropean;
}
inline void* GetDummy()
{
static uintptr_t dwDummy;
return &dwDummy;
inline uintptr_t GetDummy()
{
static uintptr_t dwDummy;
return reinterpret_cast<uintptr_t>(&dwDummy);
}
}
}
template<typename AT>
@ -58,365 +64,358 @@ inline AT DynBaseAddress(AT address)
#if defined _GTA_III
inline void InitializeVersions()
namespace Memory
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
namespace internal
{
if (*(uint32_t*)0x5C1E70 == 0x53E58955) *bVer = 0;
else if (*(uint32_t*)0x5C2130 == 0x53E58955) *bVer = 1;
else if (*(uint32_t*)0x5C6FD0 == 0x53E58955) *bVer = 2;
}
}
inline void InitializeVersions()
{
signed char* bVer = GetVer();
// This function initially detects III version then chooses the address basing on game version
template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
if ( *bVer == -1 )
{
if (*(uint32_t*)0x5C1E70 == 0x53E58955) *bVer = 0;
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
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
return (T)address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
return (T)addressSteam;
default:
#ifdef assert
assert(address10);
#endif
return (T)address10;
signed char bVer = *GetVer();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
return address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
return addressSteam;
default:
#ifdef assert
assert(address10);
#endif
return address10;
}
}
}
}
#elif defined _GTA_VC
inline void InitializeVersions()
namespace Memory
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
namespace internal
{
if (*(uint32_t*)0x667BF0 == 0x53E58955) *bVer = 0;
else if (*(uint32_t*)0x667C40 == 0x53E58955) *bVer = 1;
else if (*(uint32_t*)0x666BA0 == 0x53E58955) *bVer = 2;
}
}
inline void InitializeVersions()
{
signed char* bVer = GetVer();
// This function initially detects VC version then chooses the address basing on game version
template<typename T>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
if ( *bVer == -1 )
{
if (*(uint32_t*)0x667BF0 == 0x53E58955) *bVer = 0;
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
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
return (T)address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
return (T)addressSteam;
default:
#ifdef assert
assert(address10);
#endif
return (T)address10;
signed char bVer = *GetVer();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
return address11;
case 2:
#ifdef assert
assert(addressSteam);
#endif
return addressSteam;
default:
#ifdef assert
assert(address10);
#endif
return address10;
}
}
}
}
#elif defined _GTA_SA
inline void InitializeVersions()
namespace Memory
{
signed char* bVer = GetVer();
bool* bEuropean = GetEuropean();
if ( *bVer == -1 )
namespace internal
{
if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF )
inline void InitializeVersions()
{
// 1.0 US
*bVer = 0;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x8245BC) == 0x94BF )
{
// 1.0 EU
*bVer = 0;
*bEuropean = true;
}
else if ( *(uint32_t*)DynBaseAddress(0x8252FC) == 0x94BF )
{
// 1.01 US
*bVer = 1;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x82533C) == 0x94BF )
{
// 1.01 EU
*bVer = 1;
*bEuropean = true;
}
else if (*(uint32_t*)DynBaseAddress(0x85EC4A) == 0x94BF )
{
// 3.0
*bVer = 2;
*bEuropean = false;
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
bool* bEuropean = GetEuropean();
if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF )
{
// 1.0 US
*bVer = 0;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x8245BC) == 0x94BF )
{
// 1.0 EU
*bVer = 0;
*bEuropean = true;
}
else if ( *(uint32_t*)DynBaseAddress(0x8252FC) == 0x94BF )
{
// 1.01 US
*bVer = 1;
*bEuropean = false;
}
else if ( *(uint32_t*)DynBaseAddress(0x82533C) == 0x94BF )
{
// 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
*bVer = 3;
*bEuropean = false;
signed char* bVer = GetVer();
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
*bVer = 4;
*bEuropean = false;
signed char* bVer = GetVer();
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
*bVer = 5;
*bEuropean = false;
InitializeVersions();
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
}
}
}
inline void InitializeRegion_11()
{
bool* bEuropean = GetEuropean();
signed char* bVer = GetVer();
#if defined _GTA_III || defined _GTA_VC
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>
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();
bool bEuropean = *GetEuropean();
#elif defined _GTA_SA
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:
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)
{
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)
{
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
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;
}
return T(Memory::internal::AddressByVersion( address10, address11, addressSteam, addressNewsteamR2, addressNewsteamR2_LV ));
}
template<typename T>
inline T AddressByRegion_10(uintptr_t address10)
{
InitializeRegion_10();
bool bEuropean = *GetEuropean();
// Adjust to EU if needed
if ( bEuropean && address10 > 0x7466D0 )
{
if ( address10 < 0x7BA940 )
address10 += 0x50;
else
address10 += 0x40;
}
return (T)address10;
return T(Memory::internal::AddressByRegion_10(address10));
}
template<typename T>
inline T AddressByRegion_11(uintptr_t address11)
{
InitializeRegion_11();
bool bEuropean = *GetEuropean();
// Adjust to US if needed
if ( !bEuropean && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return (T)address11;
return T(Memory::internal::AddressByRegion_11(address11));
}
#endif

View file

@ -715,7 +715,7 @@ void DrawRect_HalfPixel_Steam(CRect& rect, const CRGBA& rgba)
char* GetMyDocumentsPathSA()
{
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 = [&] () {
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()
{
_asm