Refactor so GameModeBase is the runnable

This commit is contained in:
2023-06-22 15:17:49 -04:00
parent c6558ac0b4
commit 9a85092afb
7 changed files with 83 additions and 91 deletions

View File

@@ -8,7 +8,7 @@ public class Integration : ModuleRules
{ {
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" }); PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Sockets", "Networking" });
PrivateDependencyModuleNames.AddRange(new string[] { }); PrivateDependencyModuleNames.AddRange(new string[] { });

View File

@@ -24,13 +24,18 @@ EngineWrapper AIntegrationGameModeBase::Luprex;
AIntegrationGameModeBase::AIntegrationGameModeBase() AIntegrationGameModeBase::AIntegrationGameModeBase()
{ {
Worker = nullptr;
Thread = nullptr; Thread = nullptr;
ThreadStopRequested = false;
//PrimaryActorTick.bCanEverTick = true; // Probably wrong //PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong //PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong //PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
SetActorTickEnabled(true); // Probably wrong SetActorTickEnabled(true);
SetActorTickInterval(1.0f); // Probably wrong SetActorTickInterval(1.0f);
}
AIntegrationGameModeBase::~AIntegrationGameModeBase()
{
WaitForThread();
} }
void AIntegrationGameModeBase::HandleConsoleOutput() void AIntegrationGameModeBase::HandleConsoleOutput()
@@ -46,14 +51,44 @@ void AIntegrationGameModeBase::HandleConsoleOutput()
ConsoleOutput.Append(fs); ConsoleOutput.Append(fs);
} }
void AIntegrationGameModeBase::WaitForWorkerThread() void AIntegrationGameModeBase::LaunchThread()
{
if (Thread == nullptr) {
ThreadStopRequested = false;
Thread = FRunnableThread::Create(this, TEXT("Worker Thread"));
}
}
// Init routine called by the worker thread.
bool AIntegrationGameModeBase::Init()
{
engineutil::DPrint("WorkerRunnable::Init");
return true;
}
// Run routine called by the worker thread.
uint32 AIntegrationGameModeBase::Run()
{
while (!ThreadStopRequested)
{
engineutil::DPrint("WorkerRunnable::Run");
FPlatformProcess::Sleep(1.0);
}
engineutil::DPrint("WorkerRunnable Done");
return 0;
}
void AIntegrationGameModeBase::Stop()
{
ThreadStopRequested = true;
}
#pragma optimize( "", off )
void AIntegrationGameModeBase::WaitForThread()
{ {
if (Thread == nullptr) return; if (Thread == nullptr) return;
//Worker->Stop(); Stop(); // Notifies the thread to clean up and exit.
//Thread->WaitForCompletion(); delete Thread; // This waits for the thread to complete.
delete Thread;
delete Worker;
Worker = nullptr;
Thread = nullptr; Thread = nullptr;
} }
@@ -68,10 +103,10 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
EngineSeconds += DeltaSeconds; EngineSeconds += DeltaSeconds;
Luprex.play_invoke_event_update(&Luprex, EngineSeconds); Luprex.play_invoke_event_update(&Luprex, EngineSeconds);
} }
for (const FString& fs : engineutil::DPrintGetStored()) { TArray<FString> prints = engineutil::DPrintGetStored();
for (const FString& fs : prints) {
ConsoleOutput.AppendLine(fs); ConsoleOutput.AppendLine(fs);
} }
engineutil::DPrintClearStored();
if (ConsoleOutput.IsDirty()) { if (ConsoleOutput.IsDirty()) {
ConsoleSetOutput(ConsoleOutput.Get()); ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty(); ConsoleOutput.ClearDirty();
@@ -94,7 +129,7 @@ void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
void AIntegrationGameModeBase::BeginPlay() void AIntegrationGameModeBase::BeginPlay()
{ {
engineutil::RawPrint("In BeginPlay"); engineutil::DPrint("In BeginPlay");
Super::BeginPlay(); Super::BeginPlay();
if (!luprex_initialized()) { if (!luprex_initialized()) {
engineutil::init_wrapper(&Luprex); engineutil::init_wrapper(&Luprex);
@@ -122,16 +157,12 @@ void AIntegrationGameModeBase::BeginPlay()
ConsoleOutput.AppendLine(FString("Initialize Luprex Success")); ConsoleOutput.AppendLine(FString("Initialize Luprex Success"));
} }
EngineSeconds = 0; EngineSeconds = 0;
LaunchThread();
if (Thread == nullptr) {
Worker = new FWorkerRunnable();
Thread = FRunnableThread::Create(Worker, TEXT("Worker Thread"));
}
} }
void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason) { void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason) {
engineutil::RawPrint("In EndPlay"); engineutil::DPrint("In EndPlay");
WaitForWorkerThread(); WaitForThread();
} }

View File

@@ -6,23 +6,33 @@
#include "GameFramework/GameModeBase.h" #include "GameFramework/GameModeBase.h"
#include "enginewrapper.hpp" #include "enginewrapper.hpp"
#include "engineutil.hpp" #include "engineutil.hpp"
#include "WorkerRunnable.hpp"
#include "IntegrationGameModeBase.generated.h" #include "IntegrationGameModeBase.generated.h"
/** /**
* *
*/ */
UCLASS() UCLASS()
class INTEGRATION_API AIntegrationGameModeBase : public AGameModeBase class INTEGRATION_API AIntegrationGameModeBase : public AGameModeBase, public FRunnable
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
AIntegrationGameModeBase(); AIntegrationGameModeBase();
~AIntegrationGameModeBase();
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void Tick(float) override; virtual void Tick(float) override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason); virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
// Thread start and shutdown.
void LaunchThread();
void WaitForThread();
// Methods of FRunnable, for the thread to use.
virtual bool Init() override;
virtual uint32 Run() override;
virtual void Stop() override;
// Return true if luprex was successfully initialized.
inline bool luprex_initialized() { inline bool luprex_initialized() {
return Luprex.play_initialize != nullptr; return Luprex.play_initialize != nullptr;
} }
@@ -31,16 +41,17 @@ public:
UFUNCTION(BlueprintImplementableEvent) UFUNCTION(BlueprintImplementableEvent)
void ConsoleSetOutput(const FString& text); void ConsoleSetOutput(const FString& text);
// This is called by the GUI whenever the user hits enter.
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void ConsoleSendInput(const FString& text); void ConsoleSendInput(const FString& text);
void HandleConsoleOutput(); void HandleConsoleOutput();
void WaitForWorkerThread();
// Refresh the console output. // The worker thread is responsible for networking and event_update
FWorkerRunnable* Worker;
FRunnableThread* Thread; FRunnableThread* Thread;
bool ThreadStopRequested;
engineutil::ConsoleOutput ConsoleOutput; engineutil::ConsoleOutput ConsoleOutput;
static EngineWrapper Luprex; static EngineWrapper Luprex;
float EngineSeconds; float EngineSeconds;

View File

@@ -1,26 +0,0 @@
#include "WorkerRunnable.hpp"
#include "engineutil.hpp"
bool FWorkerRunnable::Init()
{
engineutil::RawPrint("WorkerRunnable::Init");
return true;
}
uint32 FWorkerRunnable::Run()
{
while (!StopRequested)
{
engineutil::RawPrint("WorkerRunnable::Run");
FPlatformProcess::Sleep(1.0);
}
engineutil::RawPrint("WorkerRunnable Done");
return 0;
}
void FWorkerRunnable::Stop()
{
StopRequested = true;
}

View File

@@ -1,15 +0,0 @@
#pragma once
#include "CoreMinimal.h"
class FWorkerRunnable : public FRunnable
{
public:
FWorkerRunnable() : StopRequested(false) {}
virtual bool Init() override;
virtual uint32 Run() override;
virtual void Stop() override;
private:
bool StopRequested;
};

View File

@@ -14,34 +14,31 @@ void init_wrapper(EngineWrapper* w) {
} }
} }
// Print text using GEngine->Add... // The DPrint array. This stores the dprints
void RawPrint(const char* text) { // until they can be collected by the console implementation.
if (GEngine) static TArray<FString> dprint_array;
{ static FCriticalSection dprint_mutex;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString(text));
}
}
static TArray<FString> dprints;
void DPrint(const FString& fs) { void DPrint(const FString& fs) {
dprints.Emplace(fs); FScopeLock lk(&dprint_mutex);
dprint_array.Emplace(fs);
} }
void DPrint(const char* msg) { void DPrint(const char* msg) {
dprints.Emplace(msg); FScopeLock lk(&dprint_mutex);
dprint_array.Emplace(msg);
} }
void DPrintHook(const char* msg, size_t len) { void DPrintHook(const char* msg, size_t len) {
dprints.Emplace(len, (const UTF8CHAR*)msg); FScopeLock lk(&dprint_mutex);
dprint_array.Emplace(len, (const UTF8CHAR*)msg);
} }
const TArray<FString>& DPrintGetStored() { TArray<FString> DPrintGetStored() {
return dprints; FScopeLock lk(&dprint_mutex);
} TArray<FString> result = std::move(dprint_array);
dprint_array.Empty();
void DPrintClearStored() { return result;
dprints.Reset();
} }
void ConsoleOutput::Append(const FString& text) { void ConsoleOutput::Append(const FString& text) {

View File

@@ -6,9 +6,6 @@ namespace engineutil {
// Load the DLL and initialize the wrapper, if possible. // Load the DLL and initialize the wrapper, if possible.
void init_wrapper(EngineWrapper* w); void init_wrapper(EngineWrapper* w);
// Print text using GEngine->Add...
void RawPrint(const char* text);
// Print text on the console. // Print text on the console.
void DPrint(const FString& fs); void DPrint(const FString& fs);
@@ -19,10 +16,7 @@ void DPrint(const char* msg);
void DPrintHook(const char* msg, size_t len); void DPrintHook(const char* msg, size_t len);
// Get all the stored dprints. // Get all the stored dprints.
const TArray<FString>& DPrintGetStored(); TArray<FString> DPrintGetStored();
// Clear the stored dprints.
void DPrintClearStored();
class ConsoleOutput { class ConsoleOutput {
private: private: