Skip to content

Commit

Permalink
Add memory pool patch
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Dec 14, 2020
1 parent 7ccbdc5 commit a72e907
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
18 changes: 9 additions & 9 deletions cyberpunk_amd_patch/src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ Options::Options(HMODULE aModule)
this->PatchAVX = config.value("avx", this->PatchAVX);
this->PatchSMT = config.value("smt", this->PatchSMT);
this->PatchSpectre = config.value("spectre", this->PatchSpectre);
this->PatchMemoryPool = config.value("memory_pool", true);
}
else
{
nlohmann::json config;
config["avx"] = this->PatchAVX;
config["smt"] = this->PatchSMT;
config["spectre"] = this->PatchSpectre;

std::ofstream o(configPath);
o << config.dump(4) << std::endl;
}
nlohmann::json config;
config["avx"] = this->PatchAVX;
config["smt"] = this->PatchSMT;
config["spectre"] = this->PatchSpectre;
config["memory_pool"] = this->PatchMemoryPool;

std::ofstream o(configPath);
o << config.dump(4) << std::endl;
}
1 change: 1 addition & 0 deletions cyberpunk_amd_patch/src/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ struct Options
bool PatchSpectre { true };
bool PatchSMT{ true };
bool PatchAVX{ false };
bool PatchMemoryPool{ true };
std::filesystem::path Path;
};
4 changes: 4 additions & 0 deletions cyberpunk_amd_patch/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma comment( lib, "dbghelp.lib" )
#pragma comment(linker, "/DLL")

void PoolPatch(Image* apImage);
void PatchAmd(Image* apImage);
void PatchAvx(Image* apImage);
void HotPatchFix(Image* apImage);
Expand All @@ -34,6 +35,9 @@ void Initialize(HMODULE mod)
if (options.PatchAVX)
PatchAvx(&image);

if(options.PatchMemoryPool)
PoolPatch(&image);

spdlog::default_logger()->flush();
}

Expand Down
47 changes: 47 additions & 0 deletions cyberpunk_amd_patch/src/pool_patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "Image.h"
#include <spdlog/spdlog.h>
#include <mhook-lib/mhook.h>

using TRegisterPoolOptions = void(void*, const char*, uint64_t);
TRegisterPoolOptions* RealRegisterPoolOptions = nullptr;

void RegisterPoolOptions(void* apThis, const char* acpName, uint64_t aSize)
{
if (strcmp(acpName, "PoolCPU") == 0)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);

if (statex.ullTotalPhys)
{
const auto gigsInstalled = statex.ullTotalPhys >> 30;
aSize = (gigsInstalled - 4) << 30;

spdlog::info("\t\tDetected RAM: {}GB, using {}GB", gigsInstalled, aSize >> 30);
}
}
else if (strcmp(acpName, "PoolGPU") == 0)
{
const auto fourGigs = 4ull << 30; // Assume at least 4 gigs of vram is available
aSize = aSize > fourGigs ? aSize : fourGigs;

spdlog::info("\t\tUsing {}GB of VRAM", aSize >> 30);
}

RealRegisterPoolOptions(apThis, acpName, aSize);
}

void PoolPatch(Image* apImage)
{
if (apImage->version == Image::MakeVersion(1, 4))
RealRegisterPoolOptions = reinterpret_cast<TRegisterPoolOptions*>(0x1AD0F0 + apImage->base_address);

if (RealRegisterPoolOptions)
{
auto result = Mhook_SetHook(reinterpret_cast<PVOID*>(&RealRegisterPoolOptions), &RegisterPoolOptions);
spdlog::info("\tPool patch: {}", result ? "success":"error");
}
else
spdlog::info("\tPool patch: failed");
}
4 changes: 2 additions & 2 deletions cyberpunk_amd_patch/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set_languages("cxx17")

add_requires("zlib", "spdlog", "nlohmann_json")
add_requires("zlib", "spdlog", "nlohmann_json", "mhook")

add_rules("mode.debug", "mode.release")
add_cxflags("-flto")
Expand All @@ -14,4 +14,4 @@ target("performance_overhaul")
add_files("src/**.cpp")
add_includedirs("src/")
add_syslinks("User32")
add_packages("zlib", "spdlog", "nlohmann_json")
add_packages("zlib", "spdlog", "nlohmann_json", "mhook")

0 comments on commit a72e907

Please sign in to comment.