Add HUD & UI scaling. Code refactoring

This commit is contained in:
2026-02-13 16:51:32 +01:00
parent e960d60cdf
commit 8146a75cc0

View File

@@ -1,7 +1,8 @@
#include "CommonHeaders.h"
#include "CommonHeaders.h"
#include "UETools.hpp"
#include "UEngine.hpp"
#include "UEVars.hpp"
#include "Logger.hpp"
#include "SDK/Basic.hpp"
#include "SDK/Engine_classes.hpp"
#include "SDK/WBP_Cutscene_classes.hpp"
@@ -12,8 +13,6 @@
using namespace SDK;
// Unreal Engine variables
static UWorld* previousWorld = nullptr;
static ULevel* previousLevel = nullptr;
static AExponentialHeightFog* previousFogActor = nullptr;
// Constants
const std::string PLUGIN_NAME = "SilentHillf";
@@ -23,6 +22,8 @@ constexpr ULONGLONG DEFAULT_ACTORS_SCAN_BETWEEN_TICKS = 300; // Used for enemies
// Logger
std::shared_ptr<spdlog::logger> logger;
// Screen informations
static int screenWidth = GetSystemMetrics(SM_CXSCREEN);
// Plugin states
static bool AOBScanDone = false;
static bool g_fix_enabled = false;
@@ -37,6 +38,7 @@ static bool g_Fog_Max_Opacity_fix_enabled = false;
static bool g_Camera_fix_enabled = false;
static bool g_Cutscenes_fix_enabled = false;
static bool g_Cutscenes_FPS_fix_enabled = false;
static bool g_HUD_fix_enabled = false;
static bool g_SkipIntro_fix_enabled = false;
static bool g_TimeDilation_fix_enabled = false;
static bool g_StealthMode_fix_enabled = false;
@@ -47,9 +49,9 @@ static float g_FogDensity = 0.4f;
static float g_FogMaxOpacity = 0.4f;
static float g_WorldTimeDilationValue = 1.f;
static float g_AITimeDilationValue = 1.f;
static int g_HUDOffsets = 0.f;
static bool g_Console = false;
static bool user_inputs_logged = false;
// Shared values
static float g_FOV_In = 65.f;
static float g_FOV_Out = 65.f;
@@ -57,7 +59,6 @@ static float g_Camera_In = 230.f;
static float g_Camera_Out = 230.f;
static float g_DefaultFogDensity = -1.f;
static float g_DefaultFogMaxOpacity = -1.f;
// AOB Scan pointers
static uint8_t* FOVaddress = nullptr;
static uint8_t* DOFaddress = nullptr;
@@ -72,7 +73,6 @@ static uint8_t* CutscenesFPSaddress = nullptr;
static uint8_t* ConstrainAspectRatioaddress = nullptr;
static uint8_t* AspectRatioAxisConstraintaddress = nullptr;
static uint8_t* WorldTimedilationaddress = nullptr;
// Hooking
static SafetyHookMid FOVHook{};
static SafetyHookMid PEHook{};
@@ -100,6 +100,7 @@ static void CutscenesUltrawideFixEnabled();
static void CutscenesFPSFixEnabled();
static void ProcessEvent();
static void EnableCheats(Cheat cheat);
static void HUDFixEnabled(UUserWidget* widget, bool writeLog = false);
extern "C" __declspec(dllexport) void SetFixEnabled(bool enabled, bool init) {
g_fix_enabled = enabled;
@@ -190,6 +191,7 @@ extern "C" __declspec(dllexport) void SetFixEnabled(bool enabled, bool init) {
extern "C" __declspec(dllexport) void SetFixesEnabled(GameFixes fix, bool enabled) {
if (fix == GameFixes::DevConsole) { g_Console = enabled; EnableConsole(); };
if (fix == GameFixes::FOV) { g_fov_fix_enabled = enabled; FOVFixEnabled(); }
if (fix == GameFixes::HUD) { g_HUD_fix_enabled = enabled; HUDFixEnabled(nullptr, true); }
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(); }
@@ -204,31 +206,27 @@ extern "C" __declspec(dllexport) void SetFixesEnabled(GameFixes fix, bool enable
if (fix == GameFixes::GodMode) { g_GodMode_fix_enabled = enabled; EnableCheats(Cheat::GodMode); }
}
// UUserWidget pointers
static UUserWidget* g_Notifications = nullptr;
static UUserWidget* g_PauseMenu = nullptr;
static UUserWidget* g_Quest = nullptr;
static UUserWidget* g_PickupMessage = nullptr;
static UUserWidget* g_CommonGuide = nullptr;
// Setters for Reshade addon call
extern "C" __declspec(dllexport) void SetFOV(int fov) {
g_AdditionalFOVValue = fov;
extern "C" __declspec(dllexport) void SetValues(GameSetting setting, float value) {
if (setting == GameSetting::FOV) g_AdditionalFOVValue = (int)(value);
if (setting == GameSetting::CameraDistance) g_CameraDistance = value;
if (setting == GameSetting::FogDensity) g_FogDensity = value;
if (setting == GameSetting::FogMaxOpacity) g_FogMaxOpacity = value;
if (setting == GameSetting::WorldTimeDilation) g_WorldTimeDilationValue = value;
if (setting == GameSetting::AITimeDilation) g_AITimeDilationValue = value;
if (setting == GameSetting::HUD) {
g_HUDOffsets = ((int)value * screenWidth) / 100;
HUDFixEnabled(g_Notifications);
HUDFixEnabled(g_PauseMenu);
}
}
extern "C" __declspec(dllexport) void SetCameraDistance(float multiplier) {
g_CameraDistance = multiplier;
}
extern "C" __declspec(dllexport) void SetFogDensity(float fogDensity) {
g_FogDensity = fogDensity;
}
extern "C" __declspec(dllexport) void SetFogMaxOpacity(float fogMaxOpacity) {
g_FogMaxOpacity = fogMaxOpacity;
}
extern "C" __declspec(dllexport) void SetWorldTimeDilation(float timeDilation) {
g_WorldTimeDilationValue = timeDilation;
}
extern "C" __declspec(dllexport) void SetAITimeDilation(float timeDilation) {
g_AITimeDilationValue = timeDilation;
}
// Getters for Reshade addon call
extern "C" __declspec(dllexport) void GetGameInfos(GameInfos* infos) {
if (!infos) return;
@@ -243,22 +241,120 @@ extern "C" __declspec(dllexport) void GetGameInfos(GameInfos* infos) {
}
// Code injection functions
static void ProcessEvent() {
if (!PEHook && ProcessEventaddress) {
PEHook = safetyhook::create_mid(ProcessEventaddress + 0xc,
[](SafetyHookContext& ctx) {
UObject* object = (UObject*)ctx.rcx;
UFunction* func = (UFunction*)ctx.rdx;
if (object && func) {
std::string funcName = func->GetName();
std::string objectName = object->GetName();
static bool bLastState = false;
if (funcName == "Construct") {
if (objectName.contains("WBP_CommonGuide_C")) g_CommonGuide = (UUserWidget*)object;
if (objectName.contains("WBP_Quest")) g_Quest = (UUserWidget*)object;
if (objectName.contains("WBP_PickMessage_C")) g_PickupMessage = (UUserWidget*)object;
if (objectName.contains("WBP_Notification_C")) g_Notifications = (UUserWidget*)object;
HUDFixEnabled(g_Notifications);
}
if (funcName == "Destruct") {
if (objectName.contains("WBP_CommonGuide_C")) g_CommonGuide = nullptr;
if (objectName.contains("WBP_Quest")) g_Quest = nullptr;
if (objectName.contains("WBP_PickMessage_C")) g_PickupMessage= nullptr;
if (objectName.contains("WBP_Notification_C")) g_Notifications = nullptr;
}
// WBP_SaveLoad_C Title MainList
if (objectName.contains("WBP_HUD_C") || objectName.contains("WBP_Mainmenu_C") || objectName.contains("WBP_Pause_C")) {
if (funcName == "Construct" || funcName.contains("Tick")) {
auto* widget = (UUserWidget*)object;
if (objectName.contains("WBP_Pause_C")) g_PauseMenu = widget;
HUDFixEnabled(widget);
}
if (funcName == "Destruct") g_PauseMenu = nullptr;
}
if (object->GetName().contains("TitleFlow")) { // Intro Skip
UWBP_TitleFlow_C* titleFlow = static_cast<UWBP_TitleFlow_C*>(object);
if (titleFlow && titleFlow->IsA(UWBP_TitleFlow_C::StaticClass()) && g_SkipIntro_fix_enabled) {
if (titleFlow->WBP_DevTitleFlow_Logo) titleFlow->WBP_DevTitleFlow_Logo->ShowNextLogo();
if (titleFlow->WBP_TitleFlow_Warning) titleFlow->WBP_TitleFlow_Warning->ShowNextWarning();
}
}
bool bEnable = g_fix_enabled && g_Cutscenes_fix_enabled;
if (object->GetName().contains("WBP_Cutscene_C")) { // Black bars widgets removal
bLastState = !(funcName == "Destruct" || funcName == "Construct");
auto cutscene = static_cast<UWBP_Cutscene_C*>(object);
if (bEnable != bLastState) {
float opacity = bEnable ? 0.f : 1.f;
cutscene->LeftGroup_1->SetRenderOpacity(opacity);
cutscene->LeftGroup_2->SetRenderOpacity(opacity);
cutscene->RightGroup_1->SetRenderOpacity(opacity);
cutscene->RightGroup_2->SetRenderOpacity(opacity);
bLastState = bEnable;
}
// Unlock FPS in cinematics ALevelSequenceActor -> ULevelSequencePlayer -> UMovieSceneSequencePlayer -> SetFrameRate
if (cutscene->SequenceActor && cutscene->SequenceActor->SequencePlayer) {
FFrameRate framerate = { g_Cutscenes_FPS_fix_enabled ? 240 : 30 , 1 };
cutscene->SequenceActor->SequencePlayer->SetFrameRate(framerate);
}
}
}
});
}
if (g_fix_enabled && g_Cutscenes_fix_enabled && !ConstrainAspectRatioHook)
ConstrainAspectRatioHook = safetyhook::create_mid(ConstrainAspectRatioaddress,
[](SafetyHookContext& ctx) {
ctx.rcx = 0; // bConstrainAspectRatio
});
if (g_fix_enabled && g_Cutscenes_fix_enabled && !AspectRatioAxisConstraintHook)
AspectRatioAxisConstraintHook = safetyhook::create_mid(AspectRatioAxisConstraintaddress,
[](SafetyHookContext& ctx) {
ctx.rdx = 0; // AspectRatioAxisConstraint Y
});
}
static void HUDFixEnabled(UUserWidget* widget, bool writeLog) {
auto ApplyTranslation = [&](UUserWidget* widget, float targetX) {
if (!widget) return;
if (widget->RenderTransform.Translation.X != targetX) // avoid spam every tick
widget->SetRenderTranslation(FVector2D(targetX, 0));
};
ApplyTranslation(g_CommonGuide, g_fix_enabled && g_HUD_fix_enabled ? -g_HUDOffsets : 0);
ApplyTranslation(g_Quest, g_fix_enabled && g_HUD_fix_enabled ? g_HUDOffsets : 0);
ApplyTranslation(g_PickupMessage, g_fix_enabled && g_HUD_fix_enabled ? -g_HUDOffsets : 0);
if (widget && widget->WidgetTree && widget->WidgetTree->RootWidget) {
float targetOffset = g_fix_enabled && g_HUD_fix_enabled ? g_HUDOffsets : 0.f;
ApplyOffsetsRecursive(widget->WidgetTree->RootWidget, targetOffset, targetOffset);
}
if (writeLog) logger->info("HUD fix {}", g_fix_enabled && g_HUD_fix_enabled ? "enabled" : "disabled");
}
static void FOVFixEnabled() {
if (!FOVaddress) return;
if (g_fix_enabled && g_fov_fix_enabled && FOVaddress) {
if (!FOVHook) FOVHook = safetyhook::create_mid(FOVaddress, // Hook only once
[](SafetyHookContext& ctx) { // SDK based FOV -> AdvancedThirdPersonCamera.ATPCFOVSettings:CameraFOV
// ATPC.CameraMode.FOVSettings.CameraFOV
g_FOV_In = ctx.xmm6.f32[0];
ctx.xmm6.f32[0] += (g_fix_enabled && g_fov_fix_enabled ? g_AdditionalFOVValue : g_FOV_In);
ctx.xmm6.f32[0] += (g_fix_enabled && g_fov_fix_enabled ? g_AdditionalFOVValue : 0);
g_FOV_Out = ctx.xmm6.f32[0];
});
else FOVHook.enable();
logger->info("FOV fix enabled");
}
if (!(g_fix_enabled && g_fov_fix_enabled) && FOVaddress) {
if (!(g_fix_enabled && g_fov_fix_enabled) && FOVaddress)
if (FOVHook) FOVHook.disable();
logger->info("FOV fix disabled");
}
logger->info("FOV fix {}", g_fix_enabled && g_fov_fix_enabled ? "enabled" : "disabled");
}
static void CameraFixEnabled() {
@@ -323,8 +419,8 @@ static void VolumetricFogFixEnabled() {
if (g_fix_enabled && g_Volumetric_Fog_fix_enabled && VolumetricFogaddress) {
bVolumetricFog = Memory::GetAddressFromOpcode(VolumetricFogaddress, 2, 7); // Get adress where is stored r.VolumetricFog
if (!VolumetricFogHook) VolumetricFogHook = safetyhook::create_mid(VolumetricFogaddress +0x7,
[](SafetyHookContext& ctx) { *bVolumetricFog = 0;}); // r.VolumetricFog = 0
if (!VolumetricFogHook) VolumetricFogHook = safetyhook::create_mid(VolumetricFogaddress + 0x7,
[](SafetyHookContext& ctx) { *bVolumetricFog = 0; }); // r.VolumetricFog = 0
else VolumetricFogHook.enable();
logger->info("Volumetric fog fix enabled");
}
@@ -335,62 +431,6 @@ static void VolumetricFogFixEnabled() {
}
}
static void ProcessEvent() {
if (!PEHook && ProcessEventaddress) {
PEHook = safetyhook::create_mid(ProcessEventaddress + 0xc,
[](SafetyHookContext& ctx) {
UObject* object = (UObject*)ctx.rcx;
UFunction* func = (UFunction*)ctx.rdx;
if (object && func) {
std::string funcName = func->GetName();
std::string objectName = object->GetName();
static bool bLastState = false;
if (object->GetName().contains("TitleFlow")) { // Intro Skip
UWBP_TitleFlow_C* titleFlow = static_cast<UWBP_TitleFlow_C*>(object);
if (titleFlow && titleFlow->IsA(UWBP_TitleFlow_C::StaticClass()) && g_SkipIntro_fix_enabled) {
if (titleFlow->WBP_DevTitleFlow_Logo) titleFlow->WBP_DevTitleFlow_Logo->ShowNextLogo();
if (titleFlow->WBP_TitleFlow_Warning) titleFlow->WBP_TitleFlow_Warning->ShowNextWarning();
}
}
bool bEnable = g_fix_enabled && g_Cutscenes_fix_enabled;
if (object->GetName().contains("WBP_Cutscene_C")) { // Black bars widgets removal
bLastState = !(funcName == "Destruct" || funcName == "Construct");
auto cutscene = static_cast<UWBP_Cutscene_C*>(object);
if (bEnable != bLastState) {
float opacity = bEnable ? 0.f : 1.f;
cutscene->LeftGroup_1->SetRenderOpacity(opacity);
cutscene->LeftGroup_2->SetRenderOpacity(opacity);
cutscene->RightGroup_1->SetRenderOpacity(opacity);
cutscene->RightGroup_2->SetRenderOpacity(opacity);
bLastState = bEnable;
}
// Unlock FPS in cinematics ALevelSequenceActor -> ULevelSequencePlayer -> UMovieSceneSequencePlayer -> SetFrameRate
if (cutscene->SequenceActor && cutscene->SequenceActor->SequencePlayer) {
FFrameRate framerate = { g_Cutscenes_FPS_fix_enabled ? 240 : 30 , 1 };
cutscene->SequenceActor->SequencePlayer->SetFrameRate(framerate);
}
}
}
});
}
if (g_fix_enabled && g_Cutscenes_fix_enabled && !ConstrainAspectRatioHook) {
if (!ConstrainAspectRatioHook) ConstrainAspectRatioHook = safetyhook::create_mid(ConstrainAspectRatioaddress,
[](SafetyHookContext& ctx) {
ctx.rcx = 0; // bConstrainAspectRatio
});
}
if (g_fix_enabled && g_Cutscenes_fix_enabled && !AspectRatioAxisConstraintHook)
if(!AspectRatioAxisConstraintHook) AspectRatioAxisConstraintHook = safetyhook::create_mid(AspectRatioAxisConstraintaddress,
[](SafetyHookContext& ctx) {
ctx.rdx = 0; // AspectRatioAxisConstraint Y
});
}
static void CutscenesUltrawideFixEnabled() {
logger->info("Cutscenes ultrawide fix {}", g_fix_enabled && g_Cutscenes_fix_enabled ? "enabled" : "disabled");
}
@@ -548,25 +588,10 @@ static void EnableConsole() {
ReactivateDevConsole(logger);
}
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>("Silent Hill f", std::make_shared<spdlog::sinks::rotating_file_sink_st>(PLUGIN_LOG, 10 * 1024 * 1024, 1));
logger->set_level(spdlog::level::debug);
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) {
InitializeLogger();
logger = InitializeLogger("Silent Hill f", PLUGIN_LOG);
logger->info("Plugin {} loaded.", PLUGIN_NAME);
}
else if (reason == DLL_PROCESS_DETACH) {