Files
ReshadePluginsCore/libs/UEngine/UETools.cpp

90 lines
3.6 KiB
C++

#include "UETools.hpp"
#include "Engine_classes.hpp"
std::shared_ptr<spdlog::logger> g_logger;
// UE settings
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;
}
// UE objects
SDK::APawn* GetPawnFromWorld(SDK::UWorld* world) {
if (!world) world = SDK::UWorld::GetWorld();
if (!world) return nullptr;
SDK::UGameInstance* gameInstance = world->OwningGameInstance;
if (!gameInstance || gameInstance->LocalPlayers.Num() == 0) return nullptr;
SDK::ULocalPlayer* localPlayer = gameInstance->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) {
g_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();
}