Skip to content

Commit

Permalink
Add json config and avx
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Dec 14, 2020
1 parent 57326cd commit 7ccbdc5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 24 deletions.
44 changes: 44 additions & 0 deletions cyberpunk_amd_patch/src/Options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "Options.h"
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <fstream>

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<spdlog::sinks::rotating_file_sink_mt>((Path / "performance_overhaul.log").string(), 1048576 * 5, 3);

const auto logger = std::make_shared<spdlog::logger>("", 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;
}
}
15 changes: 15 additions & 0 deletions cyberpunk_amd_patch/src/Options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <windows.h>
#include <filesystem>

struct Image;
struct Options
{
Options(HMODULE aModule);

bool PatchSpectre { true };
bool PatchSMT{ true };
bool PatchAVX{ false };
std::filesystem::path Path;
};
30 changes: 15 additions & 15 deletions cyberpunk_amd_patch/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
#include <windows.h>
#include <DbgHelp.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <array>
#include <filesystem>


#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<spdlog::sinks::rotating_file_sink_mt>("performance_overhaul/performance_overhaul.log", 1048576 * 5, 3);
if(options.PatchSMT)
PatchAmd(&image);

auto logger = std::make_shared<spdlog::logger>("", 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)
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion cyberpunk_amd_patch/src/hook_spin.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <windows.h>

#include "Image.h"
#include <utility>
#include <cstring>
#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions cyberpunk_amd_patch/src/hot_patch_fix.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include <windows.h>

#include "Image.h"
#include <utility>
#include <cstring>
#include <spdlog/spdlog.h>

void HotPatchFix_1_4(Image* apImage);
Expand All @@ -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");
}
9 changes: 7 additions & 2 deletions cyberpunk_amd_patch/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 7ccbdc5

Please sign in to comment.