diff --git a/cyberpunk_amd_patch/src/Options.cpp b/cyberpunk_amd_patch/src/Options.cpp new file mode 100644 index 00000000..58041560 --- /dev/null +++ b/cyberpunk_amd_patch/src/Options.cpp @@ -0,0 +1,44 @@ +#include "Options.h" +#include +#include +#include +#include + +Options::Options(HMODULE aModule) +{ + char path[2048 + 1] = { 0 }; + GetModuleFileNameA(aModule, path, std::size(path) - 1); + + Path = path; + Path = Path.parent_path().parent_path(); + Path /= "performance_overhaul/"; + + std::error_code ec; + create_directories(Path, ec); + + const auto rotatingLogger = std::make_shared((Path / "performance_overhaul.log").string(), 1048576 * 5, 3); + + const auto logger = std::make_shared("", spdlog::sinks_init_list{ rotatingLogger }); + set_default_logger(logger); + + const auto configPath = Path / "config.json"; + + std::ifstream configFile(configPath); + if(configFile) + { + auto config = nlohmann::json::parse(configFile); + this->PatchAVX = config.value("avx", this->PatchAVX); + this->PatchSMT = config.value("smt", this->PatchSMT); + this->PatchSpectre = config.value("spectre", this->PatchSpectre); + } + 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; + } +} diff --git a/cyberpunk_amd_patch/src/Options.h b/cyberpunk_amd_patch/src/Options.h new file mode 100644 index 00000000..ec4ba138 --- /dev/null +++ b/cyberpunk_amd_patch/src/Options.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +struct Image; +struct Options +{ + Options(HMODULE aModule); + + bool PatchSpectre { true }; + bool PatchSMT{ true }; + bool PatchAVX{ false }; + std::filesystem::path Path; +}; \ No newline at end of file diff --git a/cyberpunk_amd_patch/src/dllmain.cpp b/cyberpunk_amd_patch/src/dllmain.cpp index 6fe9b40b..ce4808cb 100644 --- a/cyberpunk_amd_patch/src/dllmain.cpp +++ b/cyberpunk_amd_patch/src/dllmain.cpp @@ -4,37 +4,37 @@ #include #include #include -#include -#include #include #include "Image.h" +#include "Options.h" + #pragma comment( lib, "dbghelp.lib" ) #pragma comment(linker, "/DLL") void PatchAmd(Image* apImage); +void PatchAvx(Image* apImage); void HotPatchFix(Image* apImage); void StringInitializerFix(Image* apImage); void PatchSpin(Image* apImage); -void Initialize() +void Initialize(HMODULE mod) { - std::error_code ec; - std::filesystem::create_directory("performance_overhaul", ec); + Options options(mod); + + Image image; - auto rotatingLogger = std::make_shared("performance_overhaul/performance_overhaul.log", 1048576 * 5, 3); + if(options.PatchSMT) + PatchAmd(&image); - auto logger = std::make_shared("", spdlog::sinks_init_list{ rotatingLogger }); - set_default_logger(logger); + if(options.PatchSpectre) + HotPatchFix(&image); - Image image; - PatchAmd(&image); - //StringInitializerFix(&image); - //PatchSpin(&image); - HotPatchFix(&image); + if (options.PatchAVX) + PatchAvx(&image); - logger->flush(); + spdlog::default_logger()->flush(); } void PatchAmd(Image* apImage) @@ -99,7 +99,7 @@ void PatchAvx(Image* apImage) BOOL APIENTRY DllMain(HMODULE mod, DWORD ul_reason_for_call, LPVOID) { switch(ul_reason_for_call) { case DLL_PROCESS_ATTACH: - Initialize(); + Initialize(mod); break; case DLL_PROCESS_DETACH: diff --git a/cyberpunk_amd_patch/src/hook_spin.cpp b/cyberpunk_amd_patch/src/hook_spin.cpp index 63f49fce..e449796d 100644 --- a/cyberpunk_amd_patch/src/hook_spin.cpp +++ b/cyberpunk_amd_patch/src/hook_spin.cpp @@ -1,7 +1,6 @@ #include #include "Image.h" -#include #include #include @@ -55,9 +54,13 @@ void PatchSpin(Image* apImage) auto addr = (uint8_t*)(0x2AEEC70 + apImage->base_address); DWORD oldProtect = 0; VirtualProtect(addr, 32, PAGE_EXECUTE_WRITECOPY, &oldProtect); + + // mov rax, HookSpin addr[0] = 0x48; addr[1] = 0xB8; std::memcpy(addr + 2, &pFuncPtr, 8); + + // jmp rax addr[10] = 0xFF; addr[11] = 0xE0; VirtualProtect(addr, 32, oldProtect, nullptr); diff --git a/cyberpunk_amd_patch/src/hot_patch_fix.cpp b/cyberpunk_amd_patch/src/hot_patch_fix.cpp index 12504ac6..5a712e13 100644 --- a/cyberpunk_amd_patch/src/hot_patch_fix.cpp +++ b/cyberpunk_amd_patch/src/hot_patch_fix.cpp @@ -1,8 +1,4 @@ -#include - #include "Image.h" -#include -#include #include void HotPatchFix_1_4(Image* apImage); @@ -13,9 +9,9 @@ void HotPatchFix(Image* apImage) HotPatchFix_1_4(apImage); else { - spdlog::warn("\tHot patch removal: failed, unknown version"); + spdlog::warn("\tSpectre patch: failed, unknown version"); return; } - spdlog::info("\tHot patch removal: success"); + spdlog::info("\tSpectre patch: success"); } diff --git a/cyberpunk_amd_patch/xmake.lua b/cyberpunk_amd_patch/xmake.lua index 7e5ae67c..a4b04e1e 100644 --- a/cyberpunk_amd_patch/xmake.lua +++ b/cyberpunk_amd_patch/xmake.lua @@ -1,12 +1,17 @@ set_languages("cxx17") -add_requires("zlib", "spdlog") +add_requires("zlib", "spdlog", "nlohmann_json") + +add_rules("mode.debug", "mode.release") +add_cxflags("-flto") +add_ldflags("-flto") set_optimize("fastest") + target("performance_overhaul") set_kind("shared") add_files("src/**.cpp") add_includedirs("src/") add_syslinks("User32") - add_packages("zlib", "spdlog") + add_packages("zlib", "spdlog", "nlohmann_json")