Files
ReshadePluginsCore/libs/UEngine/UETools.cpp

143 lines
5.1 KiB
C++
Raw Normal View History

#pragma once
#include "UETools.hpp"
#include "Engine_classes.hpp"
// Visual effects toggle
void ApplyVisualEffect(GameFixes fix, bool enabled) {
SDK::UWorld* world = SDK::UWorld::GetWorld();
SDK::APawn* pawn = nullptr;
SDK::APlayerController* pc = nullptr;
if (world) {
pawn = GetPawnFromWorld(world);
if (pawn && pawn->Controller)
pc = static_cast<SDK::APlayerController*>(pawn->Controller);
}
SDK::FString cmd = GetCommand(fix, enabled);
if (!cmd) return;
SDK::UKismetSystemLibrary::ExecuteConsoleCommand(world, SDK::FString(cmd), pc);
}
UC::FString GetCommand(GameFixes fix, bool enabled) {
switch (fix) {
case GameFixes::Fog:
return enabled ? L"r.Fog 0" : L"r.Fog 1";
case GameFixes::DOF:
return enabled ? L"r.DepthOfFieldQuality 0" : L"r.DepthOfFieldQuality 1";
case GameFixes::FilmGrain:
return enabled ? L"r.FilmGrain 0" : L"r.FilmGrain 1";
case GameFixes::Vignetting:
return enabled ? L"r.Tonemapper.Quality 0" : L"r.Tonemapper.Quality 5";
case GameFixes::ChromaticAberrations:
return enabled ? L"r.SceneColorFringeQuality 0" : L"r.SceneColorFringeQuality 1";
default:
return SDK::FString();
}
}
// UE settings (resolution ...)
void GetResolution(int& outWidth, int& outHeight, float& outAspectRatio) {
SDK::UEngine* Engine = SDK::UEngine::GetEngine();
if (!Engine || !Engine->GameUserSettings) return;
SDK::UGameUserSettings* Settings = SDK::UGameUserSettings::GetGameUserSettings();
if (!Settings) return;
SDK::FIntPoint Res = Settings->GetScreenResolution();
outWidth = (int)Res.X;
outHeight = (int)Res.Y;
outAspectRatio = (float)outWidth / outHeight;
}
// Objects retrieval functions
SDK::UWorld* GetWorldFromContext(uintptr_t ptr) {
SDK::UObject* obj = reinterpret_cast<SDK::UObject*>(ptr);
while (obj) {
if (obj->IsA(SDK::UWorld::StaticClass())) return (SDK::UWorld*)obj;
obj = obj->Outer;
}
return nullptr;
}
SDK::APawn* GetPawnFromWorld(SDK::UWorld* world) {
if (!world) world = SDK::UWorld::GetWorld();
if (!world) return nullptr;
SDK::UGameInstance* gameInstance = world->OwningGameInstance;
if (!gameInstance) return nullptr;
auto& localPlayers = gameInstance->LocalPlayers;
if (!localPlayers.IsValid() || !localPlayers.IsValidIndex(0)) return nullptr;
SDK::ULocalPlayer* localPlayer = localPlayers[0];
if (!localPlayer) return nullptr;
SDK::APlayerController* pc = localPlayer->PlayerController;
if (!pc) return nullptr;
SDK::APawn* pawn = pc->AcknowledgedPawn;
return pawn;
}
SDK::APawn* GetPawnFromObject(SDK::UObject* object) {
auto* actor = static_cast<SDK::AActor*> (object);
if (!actor || !actor->Class || !actor->IsA(SDK::APawn::StaticClass())) return nullptr;
auto* pawn = static_cast<SDK::APawn*>(actor);
if (!pawn) return nullptr;
auto* controller = pawn->Controller;
if (!controller || !controller->IsA(SDK::APlayerController::StaticClass())) return nullptr;
return pawn;
}
// Console
void ReactivateDevConsole(std::shared_ptr<spdlog::logger> logger) {
std::thread([logger]() {
auto start = std::chrono::high_resolution_clock::now(); // Measure the time to renable console
SDK::UEngine* Engine = nullptr;
for (int i = 0; i < 100; ++i) { // gives 10 seconds to find UE Engine
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Engine = SDK::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 */
SDK::UObject* NewObject = SDK::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<SDK::UConsole*>(NewObject);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
// Set the all the console shortkey to F2
for (int i = 0; i < SDK::UInputSettings::GetDefaultObj()->ConsoleKeys.Num(); i++)
{
SDK::UInputSettings::GetDefaultObj()->ConsoleKeys[i].KeyName = SDK::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.store(true, std::memory_order_release);
return;
}
else {
logger->error("Could not spawn console object");
return;
}
}).detach();
}