diff --git a/StyxShardsOFDarkness/dllmain.cpp b/StyxShardsOFDarkness/dllmain.cpp new file mode 100644 index 0000000..28674e8 --- /dev/null +++ b/StyxShardsOFDarkness/dllmain.cpp @@ -0,0 +1,442 @@ +#include "CommonHeaders.h" +#include "UETools.hpp" +#include "UEvars.hpp" +#include "Logger.hpp" +#include "SDK/Basic.hpp" +#include "SDK/Engine_classes.hpp" +#include "SDK/Styx2_classes.hpp" + +using namespace SDK; + +// Constants +const std::string PLUGIN_NAME = "Styx2"; +const std::string PLUGIN_LOG = PLUGIN_NAME + ".log"; +constexpr ULONGLONG DEFAULT_DELAY_BETWEEN_TICK = 500; // Used for enemies time dilation + +// Logger +std::shared_ptr logger; + +// Screen informations +static int screenWidth = GetSystemMetrics(SM_CXSCREEN); +static int screenHeight = GetSystemMetrics(SM_CYSCREEN); +float g_AspectRatio = (float)screenWidth / screenHeight; +float g_BaseAspectRatio = 1.777778; + +// Plugin states +static bool AOBScanDone = false; +static bool g_Console = false; +static std::atomic g_fix_enabled = false; +static std::atomic g_fov_fix_enabled = false; +static std::atomic g_HUD_fix_enabled = false; +static std::atomic g_Camera_fix_enabled = false; +static std::atomic g_DOF_fix_enabled = false; +static std::atomic g_CA_fix_enabled = false; +static std::atomic g_Vignetting_fix_enabled = false; +static std::atomic g_Fog_fix_enabled = false; +static std::atomic g_TimeDilation_fix_enabled = false; +static std::atomic g_GodMode_fix_enabled = false; +static std::atomic g_Amber_fix_enabled = false; +static std::atomic g_Stealth_fix_enabled = false; +static int g_AdditionalFOVValue = 0; +static float g_CameraMultiplier = 1.f; +static float g_CameraHeight = 0.f; +static float g_CameraLateral = 0.f; +static float g_WorldTimeDilationValue = 1.f; +static float g_AITimeDilationValue = 1.f; +static int g_HUDOffsets = 0; +static float g_HUDLeft = 0; +static float g_HUDRight = 0; +static float g_PlayerHealth = 0.f; +static float g_PlayerAmber = 0.f; +static bool user_inputs_logged = false; +// Shared values +static float g_FOV_In = 60.f; +static float g_CompensatedFOV = 60.f; +static float g_FOV_Out = 60.f; +static float g_CameraIn = 250.f; +static float g_CameraOut = 250.f; +// AOB Scan pointers +static uint8_t* FOVaddress = nullptr; +static uint8_t* DOFaddress = nullptr; +static uint8_t* CAaddress = nullptr; +static uint8_t* Vignettingaddress = nullptr; +static uint8_t* Fogaddress = nullptr; +static uint8_t* CameraComponentaddress = nullptr; +static uint8_t* CameraDistanceaddress = nullptr; +static uint8_t* HUD1address = nullptr; +static uint8_t* HUD2address = nullptr; +static uint8_t* WorldTimedilationaddress = nullptr; +static uint8_t* Timedilationaddress = nullptr; +static uint8_t* Stealthaddress = nullptr; +// Hooking +static SafetyHookMid FOVHook{}; +static SafetyHookMid DOFHook{}; +static SafetyHookMid FogHook{}; +static SafetyHookMid CameraDistanceHook{}; +static SafetyHookMid CameraHeightHook{}; +static SafetyHookMid HUD1Hook{}; +static SafetyHookMid HUD2Hook{}; +static SafetyHookMid PEHook{}; +static SafetyHookMid WorldTimeDilationHook{}; +static SafetyHookMid TimeDilationHook{}; +// Prototypes +static void FOVFixEnabled(); +static void UltraWideFixEnabled(); +static void CameraDistanceFixEnabled(); +static void HUDFixEnabled(); +static void DOFFixEnabled(); +static void CAFixEnabled(); +static void VignettingFixEnabled(); +static void FogFixEnabled(); +static void EnableConsole(); +static void EnableCheats(Cheat cheat); +static void ProcessEvent(); + +extern "C" __declspec(dllexport) void SetFixEnabled(bool enabled, bool init) { + g_fix_enabled = enabled; + if (!AOBScanDone) { // Unreal Engine 4.13.1 + logger->info("--------------- AOB scan started ---------------"); + constexpr auto CameraComponentStringObfuscated = make_obfuscated<0xF3>("EB ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ?? 89"); + constexpr auto FOVStringObfuscated = make_obfuscated<0x9C>("F3 0F ?? ?? 48 8B ?? ?? 48 ?? ?? 4C 8D ?? ?? 48"); + constexpr auto CameraDistanceStringObfuscated = make_obfuscated<0x41>("8B ?? ?? 89 ?? ?? F3 0F ?? ?? ?? F3 0F ?? ?? ?? F3 0F ?? ?? ?? F3 41"); // 0x21 + constexpr auto DOFStringObfuscated = make_obfuscated<0x78>("83 78 ?? ?? 66 C7 ?? ?? ?? ?? ?? 0F ?? ?? 32 ?? 84 ?? 0F 84"); + constexpr auto CAStringObfuscated = make_obfuscated<0x42>("7F ?? 89 B7 ?? ?? ?? ?? 42 8B"); + constexpr auto VignettingStringObfuscated = make_obfuscated<0xB7>("8B ?? 83 ?? ?? 7D ?? 89 B7 ?? ?? ?? ?? 83"); + constexpr auto FogStringObfuscated = make_obfuscated<0x78>("83 78 ?? ?? 0F 85 ?? ?? ?? ?? 49 8B ?? ?? 83 BF"); + constexpr auto WorldTimeDilationStringObfuscated = make_obfuscated<0x59>("0F ?? ?? F3 0F 59 ?? ?? ?? ?? ?? 0F ?? ?? 48 ?? ?? 48 ?? ?? F3 0F 59"); + constexpr auto TimeDilationStringObfuscated = make_obfuscated<0x44>("F3 0F ?? ?? 4C ?? ?? 44 8B ?? ?? 49 8B ?? ?? 48 ?? ?? FF 92"); + constexpr auto StealthStringObfuscated = make_obfuscated<0x44>("E8 ?? ?? ?? ?? 84 ?? 74 ?? 84 ?? 0F 84"); + constexpr auto HUD1StringObfuscated = make_obfuscated<0x66>("41 0F ?? ?? 41 0F ?? ?? 0F ?? ?? 66 49 ?? ?? ?? 66 49 ?? ?? ?? E8"); + constexpr auto HUD2StringObfuscated = make_obfuscated<0xFF>("48 ?? ?? 48 ?? ?? 66 48 ?? ?? ?? FF ?? 48 8B ?? ?? ?? 0F ?? ??"); + + using AOBScan::Make; + using OffsetScan::Make; + // Prepare all data for scanning + std::vector signatures = { + Make(&FOVaddress, FOVStringObfuscated, "FOV"), + Make(&CameraComponentaddress, CameraComponentStringObfuscated, "Camera component"), + Make(&CameraDistanceaddress, CameraDistanceStringObfuscated, "Camera distance"), + Make(&HUD1address, HUD1StringObfuscated, "HUD"), + Make(&HUD2address, HUD2StringObfuscated, "HUD"), + Make(&DOFaddress, DOFStringObfuscated, "DOF"), + Make(&CAaddress, CAStringObfuscated, "Chromatic aberrations"), + Make(&Vignettingaddress, VignettingStringObfuscated, "Vignetting"), + Make(&Fogaddress, FogStringObfuscated, "Fog"), + Make(&WorldTimedilationaddress, WorldTimeDilationStringObfuscated, "World time dilation"), + Make(&Timedilationaddress, TimeDilationStringObfuscated, "Actor time dilation"), + Make(&Stealthaddress, StealthStringObfuscated, "Stealth") + }; + Memory::AOBScanBatch(signatures, logger); // Scan all signature in a batch + + if (FOVaddress && CameraComponentaddress && CameraDistanceaddress && HUD1address && HUD2address && DOFaddress && + CAaddress && Vignettingaddress && Fogaddress && WorldTimedilationaddress && Timedilationaddress && Stealthaddress) + logger->info("All AOB signatures found. Ready to patch..."); + + if (!ProcessEventaddress) { + logger->info("------------ UEngine offsets search ------------"); + uint8_t* baseModule = reinterpret_cast(GetModuleHandleA(nullptr)); // Get game base address + + constexpr auto ProcessEventStringObfuscated = make_obfuscated<0x56>("40 ?? 56 57 41 ?? 41 ?? 41 ?? 41 ?? 48 81 ?? ?? ?? ?? ?? 48 8D ?? ?? ?? 48 89 ?? ?? ?? ?? ?? 48 8B ?? ?? ?? ?? ?? 48 ?? ?? 48 89 ?? ?? ?? ?? ?? 48 63"); + // Prepare all data for scanning + std::vector UEoffsetsScans = { + Make(&ProcessEventaddress, ProcessEventStringObfuscated, "ProcessEvent", OffsetCalcType::UE_CalculateOffset, &Offsets::ProcessEvent) + }; + Memory::OffsetScanBatch(UEoffsetsScans, baseModule, logger, ""); // Retrieve all Unreal Engine offsets in a batch + } + logger->info("-------------- Fixes initialisation -------------"); + AOBScanDone = true; + } + + if (!init) { + FOVFixEnabled(); + UltraWideFixEnabled(); + CameraDistanceFixEnabled(); + HUDFixEnabled(); + DOFFixEnabled(); + CAFixEnabled(); + VignettingFixEnabled(); + FogFixEnabled(); + } + ProcessEvent(); +} +// Setters for Reshade addon call +extern "C" __declspec(dllexport) void SetFixesEnabled(GameFixes fix, bool enabled) { // Set each fix individually + if (fix == GameFixes::DevConsole) { g_Console = enabled; EnableConsole(); } + if (fix == GameFixes::FOV) { g_fov_fix_enabled = enabled; FOVFixEnabled(); } + if (fix == GameFixes::UltraWide) { UltraWideFixEnabled(); } + if (fix == GameFixes::Camera) { g_Camera_fix_enabled = enabled; CameraDistanceFixEnabled(); } + if (fix == GameFixes::DOF) { g_DOF_fix_enabled = enabled; DOFFixEnabled(); } + if (fix == GameFixes::ChromaticAberrations) { g_CA_fix_enabled = enabled; CAFixEnabled(); } + if (fix == GameFixes::Vignetting) { g_Vignetting_fix_enabled = enabled; VignettingFixEnabled(); } + if (fix == GameFixes::Fog) { g_Fog_fix_enabled = enabled; FogFixEnabled(); } + if (fix == GameFixes::HUD) { g_HUD_fix_enabled = enabled; HUDFixEnabled(); } + if (fix == GameFixes::TimeDilation) { g_TimeDilation_fix_enabled = enabled; EnableCheats(Cheat::TimeDilation); } + if (fix == GameFixes::GodMode) { g_GodMode_fix_enabled = enabled; EnableCheats(Cheat::GodMode); } + if (fix == GameFixes::Mana) { g_Amber_fix_enabled = enabled; EnableCheats(Cheat::Mana); } + if (fix == GameFixes::Stealth) { g_Stealth_fix_enabled = enabled; EnableCheats(Cheat::Stealth); } +} + +extern "C" __declspec(dllexport) void SetValues(GameSetting setting, float value) { + if (setting == GameSetting::FOV) g_AdditionalFOVValue = (int)(value); + if (setting == GameSetting::CameraDistance) g_CameraMultiplier = value; + if (setting == GameSetting::CameraHeight) g_CameraHeight = value; + if (setting == GameSetting::CameraLateral) g_CameraLateral = value; + if (setting == GameSetting::HUD) { + g_HUDLeft = value / 100; + g_HUDRight = 1 - (value / 100); + g_HUDOffsets = ((int)value * screenWidth) / 100; + } + if (setting == GameSetting::WorldTimeDilation) g_WorldTimeDilationValue = value; + if (setting == GameSetting::AITimeDilation) g_AITimeDilationValue = 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->cameraIn = g_CameraIn; + infos->cameraOut = g_CameraOut; + infos->CompensatedFOV = g_CompensatedFOV; + infos->Health = g_PlayerHealth; + infos->Mana = g_PlayerAmber; + infos->screenWidth = screenWidth; + infos->screenHeight = screenHeight; + infos->aspectRatio = (float)screenWidth / screenHeight; + infos->consoleEnabled = g_Console_Enabled; +} + +// -- Code injection functions -- +static ULONGLONG lastScanTick = 0; // Last time tick was called +static void ProcessEvent() { + if (!PEHook && ProcessEventaddress) { + PEHook = safetyhook::create_mid(ProcessEventaddress + 0xc, + [](SafetyHookContext& ctx) { + ULONGLONG now = GetTickCount64(); + if (now - lastScanTick >= DEFAULT_DELAY_BETWEEN_TICK) { // Delay between each tick to avoid ProcessEvent stuttering + lastScanTick = now; + GetResolution(screenWidth, screenHeight, g_AspectRatio); + } + }); + } +} + +static void FOVFixEnabled() { + if (!FOVaddress) return; + if (!FOVHook) { // Hook only once + FOVHook = safetyhook::create_mid(FOVaddress, + [](SafetyHookContext& ctx) { + g_FOV_In = ctx.xmm0.f32[0]; + ctx.xmm0.f32[0] = Maths::CompensateHorizontalFOV(g_FOV_In, g_BaseAspectRatio, g_AspectRatio); + g_CompensatedFOV = ctx.xmm0.f32[0]; + if (g_fix_enabled && g_fov_fix_enabled) + ctx.xmm0.f32[0] += g_fix_enabled && g_fov_fix_enabled ? g_AdditionalFOVValue : 0.f; + g_FOV_Out = ctx.xmm0.f32[0]; + }); + } + logger->info("FOV fix {}", g_fix_enabled && g_fov_fix_enabled ? "enabled" : "disabled"); +} + +static void CameraDistanceFixEnabled() { + if (!CameraDistanceaddress) return; + if (g_fix_enabled && g_Camera_fix_enabled) { + if (!CameraDistanceHook) { // Hook only once + CameraDistanceHook = safetyhook::create_mid(CameraDistanceaddress + 0x21, + [](SafetyHookContext& ctx) { + g_CameraIn = -ctx.xmm2.f32[0]; + ctx.xmm2.f32[0] *= g_CameraMultiplier; // Set camera back distance + g_CameraOut = -ctx.xmm2.f32[0]; + ctx.xmm1.f32[0] += g_CameraLateral; // Set camera to lateral + }); + } + else CameraDistanceHook.enable(); + + if (!CameraHeightHook) { // Hook only once + CameraHeightHook = safetyhook::create_mid(CameraDistanceaddress + 0x2F, + [](SafetyHookContext& ctx) { + ctx.xmm0.f32[0] += g_CameraHeight; // Set camera height + }); + } + else CameraHeightHook.enable(); + } + else { + if (CameraDistanceHook) CameraDistanceHook.disable(); + if (CameraHeightHook) CameraHeightHook.disable(); + } + + logger->info("Camera distance fix {}", g_fix_enabled && g_Camera_fix_enabled ? "enabled" : "disabled"); +} + +static const std::string HUDClassName = "AIHUD_C"; +static const std::string HUDInteractionClassName = "HUDInteraction_C"; +static void HUDFixEnabled() { + if (!HUD1address || !HUD2address) return; + if (g_fix_enabled && g_HUD_fix_enabled) { + if (!HUD1Hook) { // Hook only once + // Used to reposition health and Amber bar from UStyx2HUDProgressBarSheet -> Material + // From the material pointer we can check who has access to this object using Cheat Engine + HUD1Hook = safetyhook::create_mid(HUD1address, // Used to reposition health and Amber bar from UStyx2HUDProgressBarSheet -> Material + [](SafetyHookContext& ctx) { + ctx.xmm10.f32[0] -= (float)g_HUDOffsets; + }); + } + else HUD1Hook.enable(); + + if (!HUD2Hook) { // Hook only once + // Used to force reposition of UI & main HUD + // This low level asm function has its rcx register as a pointer to the widget being drawn, allowing to filter them properly + HUD2Hook = safetyhook::create_mid(HUD2address + 0x18, // Used to force reposition of UI & main HUD + [](SafetyHookContext& ctx) { + UObject* object = (UObject*)ctx.rcx; + if (!object || !object->Class || !object->Class->SuperStruct) return; + auto className = object->Class->GetName(); // Only using GetName here instead of IsA to avoid CTD + bool isHUDInteraction = className.contains(HUDInteractionClassName); + if (className.contains(HUDClassName)) return; // We don't want AI HUD to be redrawn + + ctx.xmm0.f32[0] = isHUDInteraction ? 0.5f : g_HUDLeft; // Fix interaction buttons shifted by re centering it + ctx.xmm0.f32[1] = 0.f; + ctx.xmm0.f32[2] = g_HUDRight; + ctx.xmm0.f32[3] = 1.0f; + }); + } + else HUD2Hook.enable(); + } + else { + if (HUD1Hook) HUD1Hook.disable(); + if (HUD2Hook) HUD2Hook.disable(); + } +} + +static void DOFFixEnabled() { + if (!DOFaddress) return; + if (g_fix_enabled && g_DOF_fix_enabled) { + if (!DOFHook) { // Hook only once + DOFHook = safetyhook::create_mid(DOFaddress, + [](SafetyHookContext& ctx) { + if (!ctx.rax) return; + *reinterpret_cast(ctx.rax + 0x4) = g_fix_enabled && g_DOF_fix_enabled ? 0 : 1; // r.DepthOfFieldQuality = 0 + }); + } + } + logger->info("Depth of field fix {}", g_fix_enabled && g_DOF_fix_enabled ? "enabled" : "disabled"); +} + +static void FogFixEnabled() { + if (!Fogaddress) return; + if (!FogHook) { // Hook only once + FogHook = safetyhook::create_mid(Fogaddress, + [](SafetyHookContext& ctx) { + if (!ctx.rax) return; + *reinterpret_cast(ctx.rax + 0x4) = g_fix_enabled && g_Fog_fix_enabled ? 0 : 1; // r.Fog = 0 + }); + } + logger->info("Fog fix {}", g_fix_enabled && g_Fog_fix_enabled ? "enabled" : "disabled"); +} + +// -- Cheats -- +static void EnableCheats(Cheat cheat) { + if (WorldTimedilationaddress && !WorldTimeDilationHook) { + WorldTimeDilationHook = safetyhook::create_mid(WorldTimedilationaddress, + [](SafetyHookContext& ctx) { // From AWorldSettings retrieved from world->K2_GetWorldSettings() + ctx.xmm0.f32[0] *= g_TimeDilation_fix_enabled ? g_WorldTimeDilationValue : 1.f; + }); + } + // Enemies time dilation & more + if (Timedilationaddress && !TimeDilationHook) { + TimeDilationHook = safetyhook::create_mid(Timedilationaddress, + [](SafetyHookContext& ctx) { + if (!ctx.rbx) return; + + UObject* object = (UObject*)ctx.rbx; + if (!object || !object->Class) return; + if (object->IsA(AStyx2PlayerCharacter::StaticClass())) { + auto* styx = static_cast(object); + + if (styx && styx->Class) { + auto* controller = styx->Controller; + if (controller && controller->IsA(APlayerController::StaticClass())) { + g_PlayerHealth = styx->GetCurrentHealth(); + g_PlayerAmber = styx->GetCurrentAmber(); + if (g_GodMode_fix_enabled) styx->RestoreLifeOverTime(styx->GameplaySheet->MaxHealth - g_PlayerHealth, 1.f); + if (g_Amber_fix_enabled) styx->RestoreAmberOverTime(styx->GameplaySheet->MaxAmber - g_PlayerAmber, 1.f); + } + } + } + if (object->IsA(AStyx2AICharacter::StaticClass())) { + auto* enemy = static_cast(object); + if (enemy && enemy->Class) { + ctx.xmm1.f32[0] *= g_TimeDilation_fix_enabled ? g_AITimeDilationValue : 1.f; // Set enemy time dilation + + AStyx2AIController* AIController = enemy->GetStyx2AIController(); + if (AIController && AIController->Class && AIController->CognitionComponent && AIController->CognitionComponent->DetectionComponent) { + AIController->CognitionComponent->bVigilant = !g_Stealth_fix_enabled; + AIController->CognitionComponent->bLastVigilant = !g_Stealth_fix_enabled; + AIController->CognitionComponent->bPermaVigilant = !g_Stealth_fix_enabled; + if (g_Stealth_fix_enabled) { // Enemies will forget about their stimuli + AIController->CognitionComponent->DetectionComponent->ResetAllStimuli(); + AIController->CognitionComponent->DetectionComponent->ResetStrangeStimuli(); + } + } + } + } + }); + } + if (Stealthaddress) { + if (g_Stealth_fix_enabled) Memory::PatchBytes(Stealthaddress, "\x90\x90\x90\x90\x90", 5); + else Memory::RestoreBytes(Stealthaddress); + } + // Log cheats status + if (cheat == Cheat::TimeDilation) logger->info("Time dilation cheat {}", g_TimeDilation_fix_enabled ? "enabled" : "disabled"); + if (cheat == Cheat::GodMode) logger->info("God mode cheat {}", g_GodMode_fix_enabled ? "enabled" : "disabled"); + if (cheat == Cheat::Mana) logger->info("Amber cheat {}", g_Amber_fix_enabled ? "enabled" : "disabled"); + if (cheat == Cheat::Stealth) logger->info("Stealth cheat {}", g_Stealth_fix_enabled ? "enabled" : "disabled"); +} + +// Memory patch fixes +static void UltraWideFixEnabled() { + if (!CameraComponentaddress) return; + Memory::PatchBytes(CameraComponentaddress + 0x1b, "\x31\xC0\x90\x90\x90\x90", 6); +} + +static void CAFixEnabled() { + if (!CAaddress) return; + if (g_fix_enabled && g_CA_fix_enabled) + Memory::PatchBytes(CAaddress, "\x90\x90", 2); // NOP x 2 r.SceneColorFringeQuality = 0 + else Memory::RestoreBytes(CAaddress); + + logger->info("Chromatics aberrations fix {}", g_fix_enabled && g_CA_fix_enabled ? "enabled" : "disabled"); +} + +static void VignettingFixEnabled() { + if (!Vignettingaddress) return; + if (g_fix_enabled && g_Vignetting_fix_enabled) + Memory::PatchBytes(Vignettingaddress, "\x31\xC9", 2); // xor ecx,ecx r.Tonemapper.Quality=0 + else Memory::RestoreBytes(Vignettingaddress); + + logger->info("Vignetting fix {}", g_fix_enabled && g_Vignetting_fix_enabled ? "enabled" : "disabled"); +} + +// UE Console creation +static void EnableConsole() { + if (g_Console_Enabled || !g_Console || !ProcessEventaddress) { + if (!g_Console && !user_inputs_logged) { + logger->info("------------------ User inputs ------------------"); + user_inputs_logged = true; + } + return; + } + + logger->info("-------------- Console re-enabling --------------"); + ReactivateDevConsole(logger); +} +// Standard dll entry +BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) { + if (reason == DLL_PROCESS_ATTACH) { + logger = InitializeLogger("Styx: Shards Of Darkness", 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