diff --git a/SilentHillF/dllmain.cpp b/SilentHillF/dllmain.cpp index 0cf7b35..eba69c2 100644 --- a/SilentHillF/dllmain.cpp +++ b/SilentHillF/dllmain.cpp @@ -57,7 +57,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; -static bool g_Console_Enabled = false; // AOB Scan pointers static uint8_t* FOVaddress = nullptr; @@ -279,64 +278,41 @@ static void CameraFixEnabled() { } } +static UExponentialHeightFogComponent* fogComponent = nullptr; static void FogFixEnabled() { - if (g_fix_enabled && g_Fog_fix_enabled && FogDensityEngineaddress && FogOpacityEngineaddress) { + if (FogDensityEngineaddress && FogOpacityEngineaddress) { if (!FogDensityHook) FogDensityHook = safetyhook::create_mid(FogDensityEngineaddress + 0x7, // Hook for fog density [](SafetyHookContext& ctx) { - ctx.xmm1.f32[0] = g_FogDensity; - ctx.rflags &= ~(1ULL << 6); + if (ctx.rcx) fogComponent = reinterpret_cast(ctx.rcx); + ctx.xmm1.f32[0] = g_fix_enabled && g_Fog_fix_enabled ? g_FogDensity : ctx.xmm1.f32[0]; }); - else FogDensityHook.enable(); if (!FogMaxOpacityHook) FogMaxOpacityHook = safetyhook::create_mid(FogOpacityEngineaddress + 0x7, // Hook for fog opacity [](SafetyHookContext& ctx) { - ctx.xmm1.f32[0] = g_FogMaxOpacity; - ctx.rflags &= ~(1ULL << 6); + ctx.xmm1.f32[0] = g_fix_enabled && g_Fog_fix_enabled ? g_FogMaxOpacity : ctx.xmm1.f32[0]; }); - else FogMaxOpacityHook.enable(); + } + if (g_fix_enabled && g_Fog_fix_enabled && FogDensityEngineaddress && FogOpacityEngineaddress) logger->info("Fog fix enabled"); - } - if (!(g_fix_enabled && g_Fog_fix_enabled)) { - if (FogDensityHook) FogDensityHook.disable(); - if (FogMaxOpacityHook) FogMaxOpacityHook.disable(); - logger->info("Fog fix disabled"); - } - if (g_fix_enabled && g_Fog_fix_enabled && Fogaddress) { + if (!(g_fix_enabled && g_Fog_fix_enabled) && FogDensityEngineaddress && FogOpacityEngineaddress) + logger->info("Fog fix disabled"); + + if (Fogaddress) { if (!FogHook) FogHook = safetyhook::create_mid(Fogaddress + 0x5f, // Generic hook for fogs fixes [](SafetyHookContext& ctx) { - UWorld* World = UWorld::GetWorld(); - if (!World) return; - - if (World != previousWorld) { - previousWorld = World; - previousLevel = World->PersistentLevel; - previousFogActor = nullptr; + UWorld* world = UWorld::GetWorld(); + if (world != previousWorld) { + previousWorld = world; + fogComponent = nullptr; g_DefaultFogDensity = -1.f; g_DefaultFogMaxOpacity = -1.f; } - - if (!previousFogActor || !previousFogActor->Component) { // Dynamic search for fog actor if none is valid - if (previousLevel) { - for (AActor* Actor : previousLevel->Actors) { - if (!Actor || !Actor->Class) continue; - if (Actor->IsA(AExponentialHeightFog::StaticClass())) { - previousFogActor = reinterpret_cast(Actor); - // Save default values - g_DefaultFogDensity = previousFogActor->Component->FogDensity; - g_DefaultFogMaxOpacity = previousFogActor->Component->FogMaxOpacity; - break; - } - } - } - } - if (previousFogActor && previousFogActor->Component) { - // Get default engine values - if (g_DefaultFogDensity == -1.f) g_DefaultFogDensity = previousFogActor->Component->FogDensity; - if (g_DefaultFogMaxOpacity == -1.f) g_DefaultFogMaxOpacity = previousFogActor->Component->FogMaxOpacity; - // Force fix values - previousFogActor->Component->SetFogDensity(g_fix_enabled && g_Fog_fix_enabled ? g_FogDensity : g_DefaultFogDensity); - previousFogActor->Component->SetFogMaxOpacity(g_fix_enabled && g_Fog_fix_enabled ? g_FogMaxOpacity : g_DefaultFogMaxOpacity); + if (fogComponent) { + if (g_DefaultFogDensity == -1.f) g_DefaultFogDensity = fogComponent->FogDensity; + if (g_DefaultFogMaxOpacity == -1.f) g_DefaultFogMaxOpacity = fogComponent->FogMaxOpacity; + fogComponent->SetFogDensity(g_fix_enabled && g_Fog_fix_enabled ? g_FogDensity : g_DefaultFogDensity); + fogComponent->SetFogMaxOpacity(g_fix_enabled && g_Fog_fix_enabled ? g_FogMaxOpacity : g_DefaultFogMaxOpacity); } }); } @@ -569,49 +545,7 @@ static void EnableConsole() { } logger->info("-------------- Console re-enabling --------------"); - std::thread([&]() { - auto start = std::chrono::high_resolution_clock::now(); // Measure the time to renable console - UEngine* Engine = nullptr; - - for (int i = 0; i < 10; ++i) { // gives 10 seconds to find UE Engine - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - Engine = UEngine::GetEngine(); - if (Engine && Engine->ConsoleClass && Engine->GameViewport) - break; - } - - if (!Engine || !Engine->ConsoleClass || !Engine->GameViewport) { - logger->error("Console could not be found in engine."); - return; - } - logger->info("Console found in engine"); - - /* Creates a new UObject of class-type specified by Engine->ConsoleClass */ - UObject* NewObject = UGameplayStatics::SpawnObject(Engine->ConsoleClass, Engine->GameViewport); - if (NewObject) - { - logger->info("Successfully spawned console object"); - // Set the console viewport so that it will be displayed - Engine->GameViewport->ViewportConsole = static_cast(NewObject); - // Compute console enabling duration - auto end = std::chrono::high_resolution_clock::now(); - std::chrono::duration elapsed = end - start; - - // Set the all the console shortkey to F2 - for (int i = 0; i < UInputSettings::GetDefaultObj()->ConsoleKeys.Num(); i++) - { - UInputSettings::GetDefaultObj()->ConsoleKeys[i].KeyName = UKismetStringLibrary::Conv_StringToName(L"F2"); - } - logger->info("Console fully reactivated in {:.3f}s and bound to key F2", elapsed.count()); - logger->info("------------------ User inputs ------------------"); - - g_Console_Enabled = true; - } - else { - logger->error("Could not spawn console object"); - logger->info("------------------ User inputs ------------------"); - } - }).detach(); + ReactivateDevConsole(logger); } static void InitializeLogger() {