Skip to content

Commit

Permalink
Merge branch 'master' into TheNormalnij/img_utf_path
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij authored Sep 10, 2023
2 parents 9cbfde5 + 56d81d4 commit 51a3741
Show file tree
Hide file tree
Showing 30 changed files with 496 additions and 221 deletions.
118 changes: 47 additions & 71 deletions Client/game_sa/CGameSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,27 +548,6 @@ void CGameSA::SetMinuteDuration(unsigned long ulTime)

bool CGameSA::IsCheatEnabled(const char* szCheatName)
{
if (!strcmp(szCheatName, PROP_RANDOM_FOLIAGE))
return IsRandomFoliageEnabled();

if (!strcmp(szCheatName, PROP_SNIPER_MOON))
return IsMoonEasterEggEnabled();

if (!strcmp(szCheatName, PROP_EXTRA_AIR_RESISTANCE))
return IsExtraAirResistanceEnabled();

if (!strcmp(szCheatName, PROP_UNDERWORLD_WARP))
return IsUnderWorldWarpEnabled();

if (!strcmp(szCheatName, PROP_VEHICLE_SUNGLARE))
return IsVehicleSunGlareEnabled();

if (!strcmp(szCheatName, PROP_CORONA_ZTEST))
return IsCoronaZTestEnabled();

if (!strcmp(szCheatName, PROP_WATER_CREATURES))
return IsWaterCreaturesEnabled();

std::map<std::string, SCheatSA*>::iterator it = m_Cheats.find(szCheatName);
if (it == m_Cheats.end())
return false;
Expand All @@ -577,48 +556,6 @@ bool CGameSA::IsCheatEnabled(const char* szCheatName)

bool CGameSA::SetCheatEnabled(const char* szCheatName, bool bEnable)
{
if (!strcmp(szCheatName, PROP_RANDOM_FOLIAGE))
{
SetRandomFoliageEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_SNIPER_MOON))
{
SetMoonEasterEggEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_EXTRA_AIR_RESISTANCE))
{
SetExtraAirResistanceEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_UNDERWORLD_WARP))
{
SetUnderWorldWarpEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_VEHICLE_SUNGLARE))
{
SetVehicleSunGlareEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_CORONA_ZTEST))
{
SetCoronaZTestEnabled(bEnable);
return true;
}

if (!strcmp(szCheatName, PROP_WATER_CREATURES))
{
SetWaterCreaturesEnabled(bEnable);
return true;
}

std::map<std::string, SCheatSA*>::iterator it = m_Cheats.find(szCheatName);
if (it == m_Cheats.end())
return false;
Expand All @@ -631,14 +568,6 @@ bool CGameSA::SetCheatEnabled(const char* szCheatName, bool bEnable)

void CGameSA::ResetCheats()
{
SetRandomFoliageEnabled(true);
SetMoonEasterEggEnabled(false);
SetExtraAirResistanceEnabled(true);
SetUnderWorldWarpEnabled(true);
SetCoronaZTestEnabled(true);
CVehicleSA::SetVehiclesSunGlareEnabled(false);
SetWaterCreaturesEnabled(true);

std::map<std::string, SCheatSA*>::iterator it;
for (it = m_Cheats.begin(); it != m_Cheats.end(); it++)
{
Expand Down Expand Up @@ -763,6 +692,53 @@ void CGameSA::SetWaterCreaturesEnabled(bool isEnabled)
m_areWaterCreaturesEnabled = isEnabled;
}

void CGameSA::SetBurnFlippedCarsEnabled(bool isEnabled)
{
if (isEnabled == m_isBurnFlippedCarsEnabled)
return;

// CAutomobile::VehicleDamage
if (isEnabled)
{
BYTE originalCodes[6] = {0xD9, 0x9E, 0xC0, 0x04, 0x00, 0x00};
MemCpy((void*)0x6A776B, &originalCodes, 6);
}
else
{
BYTE newCodes[6] = {0xD8, 0xDD, 0x90, 0x90, 0x90, 0x90};
MemCpy((void*)0x6A776B, &newCodes, 6);
}

// CPlayerInfo::Process
if (isEnabled)
{
BYTE originalCodes[6] = {0xD9, 0x99, 0xC0, 0x04, 0x00, 0x00};
MemCpy((void*)0x570E7F, &originalCodes, 6);
}
else
{
BYTE newCodes[6] = {0xD8, 0xDD, 0x90, 0x90, 0x90, 0x90};
MemCpy((void*)0x570E7F, &newCodes, 6);
}

m_isBurnFlippedCarsEnabled = isEnabled;
}

void CGameSA::SetFireballDestructEnabled(bool isEnabled)
{
if (isEnabled)
{
BYTE originalCodes[7] = {0x81, 0x66, 0x1C, 0x7E, 0xFF, 0xFF, 0xFF};
MemCpy((void*)0x6CCE45, &originalCodes, 7); // CPlane::BlowUpCar
MemCpy((void*)0x6C6E01, &originalCodes, 7); // CHeli::BlowUpCar
}
else
{
MemSet((void*)0x6CCE45, 0x90, 7); // CPlane::BlowUpCar
MemSet((void*)0x6C6E01, 0x90, 7); // CHeli::BlowUpCar
}
}

bool CGameSA::PerformChecks()
{
std::map<std::string, SCheatSA*>::iterator it;
Expand Down
24 changes: 12 additions & 12 deletions Client/game_sa/CGameSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ extern unsigned int OBJECTDYNAMICINFO_MAX; // default: 160
#define CHEAT_NEVERWANTED "neverwanted"
#define CHEAT_HEALTARMORMONEY "healtharmormoney"

#define PROP_RANDOM_FOLIAGE "randomfoliage"
#define PROP_SNIPER_MOON "snipermoon"
#define PROP_EXTRA_AIR_RESISTANCE "extraairresistance"
#define PROP_UNDERWORLD_WARP "underworldwarp"
#define PROP_VEHICLE_SUNGLARE "vehiclesunglare"
#define PROP_CORONA_ZTEST "coronaztest"
#define PROP_WATER_CREATURES "watercreatures"

struct SCheatSA
{
BYTE* m_byAddress; // Cheat Address
Expand Down Expand Up @@ -214,11 +206,17 @@ class CGameSA : public CGame
void SetVehicleSunGlareEnabled(bool bEnabled);
bool IsVehicleSunGlareEnabled();

void SetCoronaZTestEnabled(bool isEnabled);
bool IsCoronaZTestEnabled() const noexcept { return m_isCoronaZTestEnabled; }
void SetCoronaZTestEnabled(bool isEnabled) override;
bool IsCoronaZTestEnabled() const noexcept override { return m_isCoronaZTestEnabled; }

bool IsWaterCreaturesEnabled() const noexcept override { return m_areWaterCreaturesEnabled; }
void SetWaterCreaturesEnabled(bool isEnabled) override;

bool IsBurnFlippedCarsEnabled() const noexcept override { return m_isBurnFlippedCarsEnabled; }
void SetBurnFlippedCarsEnabled(bool isEnabled) override;

bool IsWaterCreaturesEnabled() const noexcept { return m_areWaterCreaturesEnabled; }
void SetWaterCreaturesEnabled(bool isEnabled);
bool IsFireballDestructEnabled() const noexcept override { return m_isFireballDestructEnabled; }
void SetFireballDestructEnabled(bool isEnabled) override;

unsigned long GetMinuteDuration();
void SetMinuteDuration(unsigned long ulTime);
Expand Down Expand Up @@ -331,6 +329,8 @@ class CGameSA : public CGame
bool m_bUnderworldWarp;
bool m_isCoronaZTestEnabled{true};
bool m_areWaterCreaturesEnabled{true};
bool m_isBurnFlippedCarsEnabled{true};
bool m_isFireballDestructEnabled{true};

static unsigned int& ClumpOffset;
static unsigned long* VAR_SystemTime;
Expand Down
51 changes: 31 additions & 20 deletions Client/game_sa/CStreamingSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern CCoreInterface* g_pCore;
// count: 26316 in unmodified game
CStreamingInfo (&CStreamingSA::ms_aInfoForModel)[26316] = *(CStreamingInfo(*)[26316])0x8E4CC0;
HANDLE* phStreamingThread = (HANDLE*)0x8E4008;
uint32(&CStreamingSA::ms_streamingHalfOfBufferSize) = *(uint32*)0x8E4CA8;
uint32(&CStreamingSA::ms_streamingHalfOfBufferSizeBlocks) = *(uint32*)0x8E4CA8;
void* (&CStreamingSA::ms_pStreamingBuffer)[2] = *(void* (*)[2])0x8E4CAC;

namespace
Expand Down Expand Up @@ -429,45 +429,56 @@ void CStreamingSA::RemoveArchive(unsigned char ucArchiveID)
m_StreamHandles[uiStreamHandlerID] = NULL;
}

void CStreamingSA::SetStreamingBufferSize(uint32 numBlocks)
bool CStreamingSA::SetStreamingBufferSize(uint32 numBlocks)
{
if (numBlocks == ms_streamingHalfOfBufferSize * 2)
return;
// Check if the size is the same already
if (numBlocks == ms_streamingHalfOfBufferSizeBlocks * 2)
return true;

// First of all, allocate the new buffer
// NOTE: Due to a bug in the `MallocAlign` code the function will just *crash* instead of returning nullptr on alloc. failure :D
typedef void*(__cdecl * Function_CMemoryMgr_MallocAlign)(uint32 uiCount, uint32 uiAlign);
void* pNewBuffer = ((Function_CMemoryMgr_MallocAlign)(0x72F4C0))(numBlocks * 2048, 2048);
if (!pNewBuffer) // ...so this code is useless for now
return false;

int pointer = *(int*)0x8E3FFC;
SGtaStream(&streaming)[5] = *(SGtaStream(*)[5])(pointer);

// Wait while streaming threads ends tasks
while (streaming[0].bInUse && streaming[1].bInUse);
// Wait while streaming thread ends tasks
while (streaming[0].bInUse || streaming[1].bInUse);

// Suspend streaming handle
// Suspend streaming thread [otherwise data might become corrupted]
SuspendThread(*phStreamingThread);

// Create new buffer
if (numBlocks & 1) // Make it be even
if (numBlocks & 1) // Make it be even [Otherwise it can't be split in half properly]
numBlocks++;

typedef void*(__cdecl * Function_CMemoryMgr_MallocAlign)(uint32 uiCount, uint32 uiAlign);
void* pNewBuffer = ((Function_CMemoryMgr_MallocAlign)(0x72F4C0))(numBlocks * 2048, 2048);
// Calculate new buffer pointers
void* const pNewBuff0 = pNewBuffer;
void* const pNewBuff1 = (void*)(reinterpret_cast<uintptr_t>(pNewBuffer) + 2048u * (numBlocks / 2));

// Copy data from old buffer to new buffer
uint uiCopySize = std::min(ms_streamingHalfOfBufferSize, numBlocks / 2); // TODO: This probably won't work as expected
MemCpyFast(pNewBuffer, (void*)ms_pStreamingBuffer[0], uiCopySize);
MemCpyFast((void*)(reinterpret_cast<int>(pNewBuffer) + 1024 * numBlocks), (void*)ms_pStreamingBuffer[1], uiCopySize);
const auto copySizeBytes = std::min(ms_streamingHalfOfBufferSizeBlocks, numBlocks / 2) * 2048;
MemCpyFast(pNewBuff0, ms_pStreamingBuffer[0], copySizeBytes);
MemCpyFast(pNewBuff1, ms_pStreamingBuffer[1], copySizeBytes);

// Now, we can deallocate the old buffer safely
typedef void(__cdecl * Function_CMemoryMgr_FreeAlign)(void* pos);
((Function_CMemoryMgr_FreeAlign)(0x72F4F0))(ms_pStreamingBuffer[0]);

ms_streamingHalfOfBufferSize = numBlocks / 2;
// Update the buffer size now
ms_streamingHalfOfBufferSizeBlocks = numBlocks / 2;

ms_pStreamingBuffer[0] = pNewBuffer;
ms_pStreamingBuffer[1] = (void*)(reinterpret_cast<int>(pNewBuffer) + 1024 * numBlocks);
// Update internal pointers too
streaming[0].pBuffer = ms_pStreamingBuffer[0] = pNewBuff0;
streaming[1].pBuffer = ms_pStreamingBuffer[1] = pNewBuff1;

streaming[0].pBuffer = ms_pStreamingBuffer[0];
streaming[1].pBuffer = ms_pStreamingBuffer[1];

// Well done
// Now we can resume streaming
ResumeThread(*phStreamingThread);

return true;
}

void CStreamingSA::MakeSpaceFor(std::uint32_t memoryToCleanInBytes)
Expand Down
8 changes: 4 additions & 4 deletions Client/game_sa/CStreamingSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class CStreamingSA final : public CStreaming
unsigned char GetUnusedArchive();
unsigned char GetUnusedStreamHandle();
unsigned char AddArchive(const wchar_t* szFilePath);
void RemoveArchive(unsigned char ucArchiveID);
void SetStreamingBufferSize(uint32 uiSize);
uint32 GetStreamingBufferSize() { return ms_streamingHalfOfBufferSize * 2; }; // In bytes
void RemoveArchive(unsigned char ucStreamHandler);
bool SetStreamingBufferSize(uint32 uiSize);
uint32 GetStreamingBufferSize() { return ms_streamingHalfOfBufferSizeBlocks * 2048 * 2; }; // In bytes

void MakeSpaceFor(std::uint32_t memoryToCleanInBytes) override;
std::uint32_t GetMemoryUsed() const override;
Expand All @@ -85,6 +85,6 @@ class CStreamingSA final : public CStreaming
std::vector<SStreamName> m_StreamNames;

static void* (&ms_pStreamingBuffer)[2];
static uint32(&ms_streamingHalfOfBufferSize);
static uint32(&ms_streamingHalfOfBufferSizeBlocks);
static CStreamingInfo (&ms_aInfoForModel)[26316]; // count: 26316 in unmodified game
};
Loading

0 comments on commit 51a3741

Please sign in to comment.