Moved Unreal Engine tools to libs. Add Console enabling
This commit is contained in:
63
libs/UEngine/UETools.cpp
Normal file
63
libs/UEngine/UETools.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "UETools.hpp"
|
||||
#include "Engine_classes.hpp"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user