diff --git a/DeathStranding2/dllmain.cpp b/DeathStranding2/dllmain.cpp new file mode 100644 index 0000000..ac6a06e --- /dev/null +++ b/DeathStranding2/dllmain.cpp @@ -0,0 +1,147 @@ +#include "CommonHeaders.h" +#include "inicpp.h" +#include "UEngine.hpp" +#include "Logger.hpp" + +// Constants +const std::string PLUGIN_NAME = "DeathStranding2"; +const std::string PLUGIN_LOG = PLUGIN_NAME + ".log"; +constexpr ULONGLONG DEFAULT_DELAY_BETWEEN_TICK = 500; // Used for retrieving Game resolution + +// Logger +std::shared_ptr logger; + +// Screen informations +static int screenWidth = GetSystemMetrics(SM_CXSCREEN); +static int screenHeight = GetSystemMetrics(SM_CYSCREEN); +static float g_AspectRatio = (float)screenWidth / screenHeight; + +// Plugin states +static bool AOBScanDone = false; +static std::atomic g_fix_enabled = false; +static std::atomic g_fov_fix_enabled = false; +static std::atomic g_ultrawide_fix_enabled = false; +static int g_AdditionalFOVValue = 0; + +// Shared values +static float g_FOV_In = 80.f; +static float g_CompensatedFOV = 80.f; +static float g_FOV_Out = 80.f; + +// AOB Scan pointers +static uint8_t* FOVaddress = nullptr; +static uint8_t* Ultrawideaddress = nullptr; +static uint8_t* Cutscenesaddress = nullptr; + +// Hooking +static SafetyHookMid FOVHook{}; +static SafetyHookMid UWHook{}; + +// Prototypes +static void FOVFixEnabled(); +static void UltraWideFixEnabled(); + +extern "C" __declspec(dllexport) void SetFixEnabled(bool enabled, bool init) { + //return; + g_fix_enabled = enabled; + if (!AOBScanDone) { // Decima Engine + logger->info("--------------- AOB scan started ---------------"); + constexpr auto FOVStringObfuscated = make_obfuscated<0xFA>("C5 F8 ?? ?? ?? ?? C5 F8 ?? ?? ?? ?? C5 78 ?? ?? ?? ?? C5 FA ?? ?? C5 F2 59 ?? ?? ?? ?? ?? 48 83 ?? ?? C3"); // +0x1e + constexpr auto AspectStringObfuscated = make_obfuscated<0x8B>("C5 FA 10 ?? ?? C5 F2 ?? ?? C5 CA ?? ?? E9 ?? ?? ?? ?? C5 FA 10 ?? ?? ?? ?? ?? C5"); + constexpr auto AspectCutscenesStringObfuscated = make_obfuscated<0x9D>("0F 84 ?? ?? ?? ?? 49 8B 9D ?? ?? ?? ?? 49 63 85 ?? ?? ?? ?? 48 6B ?? ?? 48 ?? ?? 48 ?? ?? 0F 84"); + + using AOBScan::Make; + using OffsetScan::Make; + // Prepare all data for scanning + std::vector signatures = { + Make(&FOVaddress, FOVStringObfuscated, "FOV"), + Make(&Ultrawideaddress, AspectStringObfuscated, "Ultrawide"), + Make(&Cutscenesaddress, AspectCutscenesStringObfuscated, "Cutscenes"), + }; + // Scan all signature in a batch + Memory::AOBScanBatch(signatures, logger); + if (FOVaddress && Ultrawideaddress && Cutscenesaddress) + logger->info("All AOB signatures found. Ready to patch..."); + + logger->info("-------------- Fixes initialisation -------------"); + AOBScanDone = true; + } + + if (!init && Ultrawideaddress) FOVFixEnabled(); + if (!init && Ultrawideaddress) UltraWideFixEnabled(); +} + +// Setters for Reshade addon call +extern "C" __declspec(dllexport) void SetFixesEnabled(GameFixes fix, bool enabled) { // Set each fix individually + if (fix == GameFixes::FOV) { g_fov_fix_enabled = enabled; FOVFixEnabled(); } + if (fix == GameFixes::UltraWide) { g_ultrawide_fix_enabled = enabled; UltraWideFixEnabled(); } +} + +extern "C" __declspec(dllexport) void SetValues(GameSetting setting, float value) { + if (setting == GameSetting::FOV) g_AdditionalFOVValue = (int)(value); +} +// Getters for Reshade addon call +extern "C" __declspec(dllexport) void GetGameInfos(GameInfos* infos) { + if (!infos) return; + + infos->FOVIn = g_FOV_In; + infos->FOVOut = g_FOV_Out; + infos->screenWidth = screenWidth; + infos->screenHeight = screenHeight; + infos->aspectRatio = (float)screenWidth / screenHeight; +} + +// Code injection functions +static void FOVFixEnabled() { + if (g_fix_enabled && g_fov_fix_enabled && FOVaddress) { + if (!FOVHook) { // Hook only once + FOVHook = safetyhook::create_mid(FOVaddress + 0x1e, + [](SafetyHookContext& ctx) { + g_FOV_In = ctx.xmm0.f32[0]; + ctx.xmm0.f32[0] += (g_fix_enabled && g_fov_fix_enabled ? g_AdditionalFOVValue : 0); + g_FOV_Out = ctx.xmm0.f32[0]; + }); + } + else FOVHook.enable(); + } + if (!(g_fix_enabled && g_fov_fix_enabled) && FOVaddress) + if (FOVHook) FOVHook.disable(); + + logger->info("FOV fix {}", g_fix_enabled && g_fov_fix_enabled ? "enabled" : "disabled"); +} + +static void UltraWideFixEnabled() { + if (!Ultrawideaddress || !Cutscenesaddress) return; + if (g_fix_enabled && g_ultrawide_fix_enabled) { + if (!UWHook) { + std::thread([]() { + Sleep(5000); + UWHook = safetyhook::create_mid(Ultrawideaddress, + [](SafetyHookContext& ctx) { + ctx.xmm0.f32[0] = g_AspectRatio; + }); + }).detach(); + } + else UWHook.enable(); + Memory::PatchBytes(Cutscenesaddress, "\x90\xE9", 2); + } + else { + if (UWHook) UWHook.disable(); + Memory::RestoreBytes(Cutscenesaddress); + } + + logger->info("Ultrawide fix {}", g_fix_enabled && g_ultrawide_fix_enabled ? "enabled" : "disabled"); +} + +// Standard dll entry +BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) { + if (reason == DLL_PROCESS_ATTACH) { + logger = InitializeLogger("Death Stranding 2: On The Beach", PLUGIN_LOG); + logger->info("Plugin {} loaded.", PLUGIN_NAME); + } + else if (reason == DLL_PROCESS_DETACH) { + logger->info("Plugin {} unloaded.", PLUGIN_NAME); + spdlog::drop_all(); + } + return TRUE; +} \ No newline at end of file