Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
fix 4gb patch for not "GeometryDash.exe" files
Browse files Browse the repository at this point in the history
  • Loading branch information
Prevter committed Feb 25, 2024
1 parent 6081d17 commit 64d63d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v.2.0.1
* [Hotfix] Fixed a crash during the game launch if you've unfocused the game window
* Fixed a crash during the game launch if you've unfocused the game window
* Fixed a crash for GDPS users, if executable name is not "GeometryDash.exe"

## v2.0.0
### Full project rewrite for better stability and code quality
Expand Down
30 changes: 22 additions & 8 deletions src/shared/platform/win32/4gb_patch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <Windows.h>

namespace openhack::win32::four_gb {
/// @brief Whether `isPatched` has been called.
inline static bool HAS_CHECKED = false;
/// @brief Whether the game is already patched to use 4GB of memory.
inline static bool IS_PATCHED = false;

/// @brief Gets the path of the running executable.
/// @return The path of the running executable.
inline std::string getExecutablePath() {
char buffer[MAX_PATH];
GetModuleFileNameA(nullptr, buffer, MAX_PATH);
return buffer;
}

/// @brief Check whether the game is already patched to use 4GB of memory.
/// @return Whether the game is already patched to use 4GB of memory.
inline bool isPatched() {
// Check if we already know the result
if (HAS_CHECKED) return IS_PATCHED;

// Read 'GeometryDash.exe' into memory
std::ifstream file("GeometryDash.exe", std::ios::binary);
auto exePath = getExecutablePath();

// Try reading the game executable
std::ifstream file(exePath, std::ios::binary);
if (!file.is_open()) return false;

// Read the file into memory
file.seekg(0, std::ios::end);
auto size = file.tellg();
file.seekg(0, std::ios::beg);
Expand All @@ -44,8 +57,9 @@ namespace openhack::win32::four_gb {
/// @brief Patch the game to use 4GB of memory.
/// @return Whether the patch was successful.
inline bool patch() {
// Try reading "GeometryDash.exe"
std::ifstream file("GeometryDash.exe", std::ios::binary);
// Try reading the game executable
auto exePath = getExecutablePath();
std::ifstream file(exePath, std::ios::binary);
if (!file.is_open()) return false;

// Read the file into memory
Expand All @@ -62,14 +76,14 @@ namespace openhack::win32::four_gb {
peHeader->FileHeader.Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;

// Move the original file to a backup
auto backupPath = "GeometryDash.exe.bak";
if (std::rename("GeometryDash.exe", backupPath) != 0) {
auto backupPath = exePath + ".bak";
if (std::rename(exePath.c_str(), backupPath.c_str()) != 0) {
delete[] buffer;
return false;
}

// Write the modified file
std::ofstream newFile("GeometryDash.exe", std::ios::binary);
std::ofstream newFile(exePath, std::ios::binary);
newFile.write(buffer, size);
newFile.close();

Expand Down

0 comments on commit 64d63d3

Please sign in to comment.