Files
ReshadePluginsCore/Cronos The New Dawn/dllmain.cpp

338 lines
14 KiB
C++

#include "Memory.hpp";
#include "Maths.hpp";
#include "ObfuscateString.h"
#include <string>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <filesystem>
#include <thread>
#include <safetyhook.hpp>
// Constants
const std::string PLUGIN_NAME = "CronosTND";
const std::string PLUGIN_LOG = PLUGIN_NAME + ".log";
const std::string gameExecutable = "Cronos-Win64-Shipping.exe";
const float baseAspect = 1.7777778;
// Logger
std::shared_ptr<spdlog::logger> logger;
// Screen informations
static int screenWidth = GetSystemMetrics(SM_CXSCREEN);
static int screenHeight = GetSystemMetrics(SM_CYSCREEN);
static float aspectRatio = (float)screenWidth / screenHeight;
// Transform screen width into bytes little-endian
//float floatWidth = static_cast<float>(screenWidth);
//char* bytesWidth = reinterpret_cast<char*>(&floatWidth);
// Plugin states
static bool AOBScanDone = false;
static bool g_fix_enabled = false;
static bool g_fov_fix_enabled = false;
static bool g_aspect_fix_enabled = false;
static bool g_DOF_fix_enabled = false;
static bool g_Vignetting_fix_enabled = false;
static bool g_Fog_fix_enabled = false;
static int g_AdditionalFOVValue = 0;
// Shared values
static float g_FOV_In = 0;
static float g_Compensated_FOV = 0;
static float g_FOV_Out = 0;
// AOB Scan pointers
static uint8_t* FOVaddress = nullptr;
static uint8_t* FPSaddress = nullptr;
static uint8_t* Aspectaddress = nullptr;
static uint8_t* DOFaddress = nullptr;
static uint8_t* Vignettingaddress = nullptr;
static uint8_t* Fogaddress = nullptr;
// Hooking
static SafetyHookMid FOVHook{};
static SafetyHookMid AspectHook{};
static SafetyHookMid FogHook{};
// Prototypes
static void FOVFixEnabled(bool fix_enabled);
static void AspectFixEnabled(bool fix_enabled);
static void DOFFixEnabled(bool fix_enabled);
static void VignettingFixEnabled(bool fix_enabled);
static void FogFixEnabled(bool fix_enabled);
uint8_t* basePtr = reinterpret_cast<uint8_t*>(GetModuleHandleA(nullptr));
extern "C" __declspec(dllexport) void SetFixEnabled(bool enabled, bool init)
{
g_fix_enabled = enabled;
if (g_fix_enabled && !AOBScanDone) {
logger->info("--------------- AOB scan started ---------------");
if (FOVaddress == nullptr) {
constexpr auto FOVStringObfuscated = make_obfuscated<0x4A>("EB ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ?? 89");
FOVaddress = Memory::AOBScan(gameExecutable, FOVStringObfuscated.decrypt(), PAGE_EXECUTE_READ);
//"Cronos-Win64-Shipping.exe" + 36B636C - EB 08 - jmp "Cronos-Win64-Shipping.exe" + 36B6376
//"Cronos-Win64-Shipping.exe" + 36B636E - F3 0F 10 83 30 02 00 00 - movss xmm0, [rbx + 00000230]
//"Cronos-Win64-Shipping.exe" + 36B6376 - F3 0F 11 47 30 - movss[rdi + 30], xmm0
//"Cronos-Win64-Shipping.exe" + 36B637B - 8B 83 54 02 00 00 - mov eax, [rbx + 00000254]
//"Cronos-Win64-Shipping.exe" + 36B6381 - 89 47 5C - mov[rdi + 5C], eax
if (!FOVaddress)
logger->warn("FOV signature not found. Maybe your game has been updated and is no more compatible with this plugin.");
else {
logger->info("FOV signature found at address: 0x{:X}.", reinterpret_cast<uintptr_t>(FOVaddress));
FOVaddress += 0xa;
}
}
if (Aspectaddress == nullptr) {
constexpr auto AspectStringObfuscated = make_obfuscated<0x4A>("C7 43 ?? 39 8E E3 3F C7 87 ?? ?? ?? ?? 39 8E E3 3F 48 83 ?? ?? ?? ?? ?? ?? 75");
Aspectaddress = Memory::AOBScan(gameExecutable, AspectStringObfuscated.decrypt(), PAGE_EXECUTE_READ);
//"Cronos-Win64-Shipping.exe" + 54C4600 - 83 4B 68 01 - or dword ptr[rbx + 68], 01
//"Cronos-Win64-Shipping.exe" + 54C4604 - 80 8F 59 02 00 00 01 - or byte ptr[rdi + 00000259], 01
//"Cronos-Win64-Shipping.exe" + 54C460B - C7 43 5C 39 8E E3 3F - mov[rbx + 5C], 3FE38E39
//"Cronos-Win64-Shipping.exe" + 54C4612 - C7 87 54 02 00 00 398EE33F - mov[rdi + 00000254], 3FE38E39
//"Cronos-Win64-Shipping.exe" + 54C461C - 48 83 BF 98 00 00 00 00 - cmp qword ptr[rdi + 00000098], 00
if (!Aspectaddress)
logger->warn("Aspect ratio signature not found. Maybe your game has been updated and is no more compatible with this plugin.");
else {
logger->info("Aspect ratio signature found at address: 0x{:X}.", reinterpret_cast<uintptr_t>(Aspectaddress));
Aspectaddress += 0x3;
}
}
if (DOFaddress == nullptr) {
constexpr auto DOFStringObfuscated = make_obfuscated<0x4A>("74 ?? 48 ?? ?? 8B ?? ?? 48 ?? ?? E8 ?? ?? ?? ?? 0F");
DOFaddress = Memory::AOBScan(gameExecutable, DOFStringObfuscated.decrypt(), PAGE_EXECUTE_READ);
//"Cronos-Win64-Shipping.exe" + 2712F87 - 74 03 - je "Cronos-Win64-Shipping.exe" + 2712F8C
//"Cronos-Win64-Shipping.exe" + 2712F89 - 48 8B C3 - mov rax, rbx
//"Cronos-Win64-Shipping.exe" + 2712F8C - 8B 34 30 - mov esi, [rax + rsi]
//"Cronos-Win64-Shipping.exe" + 2712F8F - 48 8B CF - mov rcx, rdi
//"Cronos-Win64-Shipping.exe" + 2712F92 - E8 29 8D 6F 01 - call "Cronos-Win64-Shipping.exe" + 3E0BCC0
if (!DOFaddress)
logger->warn("DOF signature not found. Maybe your game has been updated and is no more compatible with this plugin.");
else {
logger->info("DOF signature found at address: 0x{:X}.", reinterpret_cast<uintptr_t>(DOFaddress));
DOFaddress += 0x5;
}
}
if (Vignettingaddress == nullptr) {
constexpr auto VignettingStringObfuscated = make_obfuscated<0x4A>("8B ?? 83 ?? ?? 7D ?? 44 89");
Vignettingaddress = Memory::AOBScan(gameExecutable, VignettingStringObfuscated.decrypt(), PAGE_EXECUTE_READ);
//"Cronos-Win64-Shipping.exe" + 3E079F4 - 8B 08 - mov ecx, [rax]
//"Cronos-Win64-Shipping.exe" + 3E079F6 - 83 F9 02 - cmp ecx, 02
//"Cronos-Win64-Shipping.exe" + 3E079F9 - 7D 09 - jnl "Cronos-Win64-Shipping.exe" + 3E07A04
//"Cronos-Win64-Shipping.exe" + 3E079FB - 44 89 BB C8 22 00 00 - mov[rbx + 000022C8], r15d
//"Cronos-Win64-Shipping.exe" + 3E07A02 - EB 05 - jmp "Cronos-Win64-Shipping.exe" + 3E07A09
if (!Vignettingaddress)
logger->warn("Vignetting signature not found. Maybe your game has been updated and is no more compatible with this plugin.");
else {
logger->info("Vignetting signature found at address: 0x{:X}.", reinterpret_cast<uintptr_t>(Vignettingaddress));
Vignettingaddress += 0x5;
}
}
if (Fogaddress == nullptr) {
constexpr auto FogStringObfuscated = make_obfuscated<0x4A>("F6 ?? ?? ?? ?? 74 ?? 48 8B ?? ?? ?? ?? ?? 83 78 ?? ?? 75 ?? B3");
Fogaddress = Memory::AOBScan(gameExecutable, FogStringObfuscated.decrypt(), PAGE_EXECUTE_READ);
//"Cronos-Win64-Shipping.exe" + 231B60A - 48 8B 05 BF EC AD 08 - mov rax, ["Cronos-Win64-Shipping.exe" + ADFA2D0]
//"Cronos-Win64-Shipping.exe" + 231B611 - 83 78 04 01 - cmp dword ptr[rax + 04], 01
//"Cronos-Win64-Shipping.exe" + 231B615 - 75 04 - jne "Cronos-Win64-Shipping.exe" + 231B61B
//"Cronos-Win64-Shipping.exe" + 231B617 - B3 01 - mov bl, 01
//"Cronos-Win64-Shipping.exe" + 231B619 - EB 02 - jmp "Cronos-Win64-Shipping.exe" + 231B61D
if (!Fogaddress)
logger->warn("Fog signature not found. Maybe your game has been updated and is no more compatible with this plugin.");
else {
logger->info("Fog signature found at address: 0x{:X}.", reinterpret_cast<uintptr_t>(Fogaddress));
Fogaddress += 0x12;
}
}
if (FOVaddress && Aspectaddress && DOFaddress && Vignettingaddress && Fogaddress) {
logger->info("All AOB signatures found. Ready to patch...");
AOBScanDone = true;
}
logger->info("--------------- AOB scan finished ---------------");
}
if (g_fix_enabled) {
if (FOVaddress) FOVFixEnabled(g_fov_fix_enabled || g_aspect_fix_enabled);
if (Aspectaddress) AspectFixEnabled(g_aspect_fix_enabled);
if (DOFaddress) DOFFixEnabled(g_DOF_fix_enabled);
if (Vignettingaddress) VignettingFixEnabled(g_Vignetting_fix_enabled);
if (Fogaddress) FogFixEnabled(g_Fog_fix_enabled);
}
else {
if (FOVaddress) FOVFixEnabled(false);
if (Aspectaddress) AspectFixEnabled(false);
if (DOFaddress) DOFFixEnabled(false);
if (Vignettingaddress) VignettingFixEnabled(false);
if (Fogaddress) FogFixEnabled(false);
logger->info("All fixes disabled.");
}
}
// Setters for Reshade addon calls
extern "C" __declspec(dllexport) void SetFOVFixEnabled(bool enabled, bool init)
{
g_fov_fix_enabled = enabled;
if (!init) FOVFixEnabled(g_fov_fix_enabled || g_aspect_fix_enabled);
}
extern "C" __declspec(dllexport) void SetAspectRatioFixEnabled(bool enabled, bool init)
{
g_aspect_fix_enabled = enabled;
if (!init) AspectFixEnabled(g_aspect_fix_enabled);
}
extern "C" __declspec(dllexport) void SetDOFFixEnabled(bool enabled, bool init)
{
g_DOF_fix_enabled = enabled;
if (!init) DOFFixEnabled(g_DOF_fix_enabled);
}
extern "C" __declspec(dllexport) void SetVignettingFixEnabled(bool enabled, bool init)
{
g_Vignetting_fix_enabled = enabled;
if (!init) VignettingFixEnabled(g_Vignetting_fix_enabled);
}
extern "C" __declspec(dllexport) void SetFogFixEnabled(bool enabled, bool init)
{
g_Fog_fix_enabled = enabled;
if (!init) FogFixEnabled(g_Fog_fix_enabled);
}
extern "C" __declspec(dllexport) void SetFOV(int fov)
{
g_AdditionalFOVValue = fov;
}
// Getters for Reshade addon calls
extern "C" __declspec(dllexport) float GetFOVIn() {
return g_FOV_In;
}
extern "C" __declspec(dllexport) float GetCompensatedFOV() {
return g_Compensated_FOV;
}
extern "C" __declspec(dllexport) float GetFOVOut() {
return g_FOV_Out;
}
// Code injection functions
static void FOVFixEnabled(bool fix_enabled) {
if (g_fix_enabled && fix_enabled && FOVaddress) {
if (!FOVHook) { // Hook only once
FOVHook = safetyhook::create_mid(FOVaddress,
[](SafetyHookContext& ctx) {
g_FOV_In = ctx.xmm0.f32[0];
if (g_aspect_fix_enabled)
g_Compensated_FOV = ctx.xmm0.f32[0] = Maths::CompensateHorizontalFOV(g_FOV_In, baseAspect, aspectRatio);
else
g_Compensated_FOV = ctx.xmm0.f32[0];
g_FOV_Out = ctx.xmm0.f32[0] += (g_fov_fix_enabled ? g_AdditionalFOVValue : 0);
});
}
else FOVHook.enable();
logger->info("FOV fix enabled");
}
if (!fix_enabled) {
if (FOVHook) FOVHook.disable();
logger->info("FOV fix disabled");
}
}
// Memory patch fixes
static void AspectFixEnabled(bool fix_enabled) {
if (g_fix_enabled && fix_enabled && Aspectaddress) {
const char* newAspectRatio = Memory::Float32ToHexBytes(aspectRatio); // Converts new apsect ratio in 4 char bytes
Memory::PatchBytes(Aspectaddress, newAspectRatio, 4); // mov [rdi+00000254],3FE38E39 <= patched
logger->info("Aspect fix enabled");
FOVFixEnabled(fix_enabled);
}
if (!fix_enabled) {
Memory::RestoreBytes(Aspectaddress);
logger->info("Aspect ratio fix disabled");
if (!g_fov_fix_enabled)
FOVFixEnabled(fix_enabled);
}
}
static void DOFFixEnabled(bool fix_enabled) {
if (g_fix_enabled && fix_enabled && DOFaddress) {
Memory::PatchBytes(DOFaddress, "\x31\xF6\x90", 3); // xor esi,esi r.DepthOfFieldQuality = 0
logger->info("Depth of field fix enabled");
}
if (!fix_enabled && DOFaddress) {
Memory::RestoreBytes(DOFaddress);
logger->info("Depth of field fix disabled");
}
}
static void VignettingFixEnabled(bool fix_enabled) {
if (g_fix_enabled && fix_enabled && Vignettingaddress) {
Memory::PatchBytes(Vignettingaddress, "\x31\xC9", 2); // xor ecx,ecx r.Tonemapper.Quality=0
logger->info("Vignetting fix enabled");
}
if (!fix_enabled && Vignettingaddress) {
Memory::RestoreBytes(Vignettingaddress);
logger->info("Vignetting fix disabled");
}
}
static void FogFixEnabled(bool fix_enabled) {
if (g_fix_enabled && fix_enabled && Fogaddress) {
if (!FogHook) { // Hook only once
FogHook = safetyhook::create_mid(Fogaddress,
[](SafetyHookContext& ctx) {
ctx.rflags &= ~0x40; // ZF=0
});
}
logger->info("Fog fix enabled");
}
}
// Logging
static void InitializeLogger()
{
try
{
std::filesystem::path log_path = std::filesystem::absolute(PLUGIN_LOG);
if (std::filesystem::exists(log_path))
std::filesystem::remove(log_path);
logger = std::make_shared<spdlog::logger>("Cronos : The New Dawn", std::make_shared<spdlog::sinks::rotating_file_sink_st>(PLUGIN_LOG, 10 * 1024 * 1024, 1));
logger->flush_on(spdlog::level::debug); // Flush automatically
}
catch (const spdlog::spdlog_ex& ex)
{
std::string plugin_error_message = "Could not open " + PLUGIN_LOG;
MessageBoxA(nullptr, plugin_error_message.c_str(), "Logger Error", MB_ICONERROR | MB_OK);
}
}
// Standard dll entry
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH)
{
if (reason == DLL_PROCESS_ATTACH)
{
InitializeLogger();
logger->info("Plugin {} loaded.", PLUGIN_NAME);
}
}
else if (reason == DLL_PROCESS_DETACH)
{
logger->info("Plugin {} unloaded.", PLUGIN_NAME);
spdlog::drop_all();
}
return TRUE;
}