diff --git a/Source/Integration/IntegrationGameModeBase.cpp b/Source/Integration/IntegrationGameModeBase.cpp index 59fd478c..b260c9d0 100644 --- a/Source/Integration/IntegrationGameModeBase.cpp +++ b/Source/Integration/IntegrationGameModeBase.cpp @@ -3,6 +3,7 @@ #include "IntegrationGameModeBase.h" #include "drvutil.hpp" #include "engineutil.hpp" +#include "WorkerRunnable.hpp" #include #include @@ -21,7 +22,10 @@ EngineWrapper AIntegrationGameModeBase::Luprex; // engw.play_invoke_event_update(&engw, drvutil::get_monotonic_clock()); //} -AIntegrationGameModeBase::AIntegrationGameModeBase() { +AIntegrationGameModeBase::AIntegrationGameModeBase() +{ + Worker = nullptr; + Thread = nullptr; //PrimaryActorTick.bCanEverTick = true; // Probably wrong //PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong //PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong @@ -29,7 +33,8 @@ AIntegrationGameModeBase::AIntegrationGameModeBase() { SetActorTickInterval(1.0f); // Probably wrong } -void AIntegrationGameModeBase::HandleConsoleOutput() { +void AIntegrationGameModeBase::HandleConsoleOutput() +{ uint32_t ndata; const char* data; Luprex.get_outgoing(&Luprex, 0, &ndata, &data); if (ndata == 0) return; @@ -41,7 +46,19 @@ void AIntegrationGameModeBase::HandleConsoleOutput() { ConsoleOutput.Append(fs); } -void AIntegrationGameModeBase::Tick(float DeltaSeconds) { +void AIntegrationGameModeBase::WaitForWorkerThread() +{ + if (Thread == nullptr) return; + //Worker->Stop(); + //Thread->WaitForCompletion(); + delete Thread; + delete Worker; + Worker = nullptr; + Thread = nullptr; +} + +void AIntegrationGameModeBase::Tick(float DeltaSeconds) +{ Super::Tick(DeltaSeconds); if (!luprex_initialized()) { return; @@ -61,7 +78,8 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds) { } } -void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs) { +void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs) +{ if (Luprex.engine) { const TCHAR* fstchar = *fs; if (sizeof(TCHAR) == 2) { @@ -74,7 +92,9 @@ void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs) { } } -void AIntegrationGameModeBase::BeginPlay() { +void AIntegrationGameModeBase::BeginPlay() +{ + engineutil::RawPrint("In BeginPlay"); Super::BeginPlay(); if (!luprex_initialized()) { engineutil::init_wrapper(&Luprex); @@ -102,5 +122,16 @@ void AIntegrationGameModeBase::BeginPlay() { ConsoleOutput.AppendLine(FString("Initialize Luprex Success")); } EngineSeconds = 0; + + if (Thread == nullptr) { + Worker = new FWorkerRunnable(); + Thread = FRunnableThread::Create(Worker, TEXT("Worker Thread")); + } } +void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason) { + engineutil::RawPrint("In EndPlay"); + WaitForWorkerThread(); +} + + diff --git a/Source/Integration/IntegrationGameModeBase.h b/Source/Integration/IntegrationGameModeBase.h index add67c24..dcdf1672 100644 --- a/Source/Integration/IntegrationGameModeBase.h +++ b/Source/Integration/IntegrationGameModeBase.h @@ -6,6 +6,7 @@ #include "GameFramework/GameModeBase.h" #include "enginewrapper.hpp" #include "engineutil.hpp" +#include "WorkerRunnable.hpp" #include "IntegrationGameModeBase.generated.h" /** @@ -20,6 +21,7 @@ public: AIntegrationGameModeBase(); virtual void BeginPlay() override; virtual void Tick(float) override; + virtual void EndPlay(const EEndPlayReason::Type EndPlayReason); inline bool luprex_initialized() { return Luprex.play_initialize != nullptr; @@ -34,7 +36,11 @@ public: void HandleConsoleOutput(); + void WaitForWorkerThread(); + // Refresh the console output. + FWorkerRunnable* Worker; + FRunnableThread* Thread; engineutil::ConsoleOutput ConsoleOutput; static EngineWrapper Luprex; float EngineSeconds; diff --git a/Source/Integration/WorkerRunnable.cpp b/Source/Integration/WorkerRunnable.cpp new file mode 100644 index 00000000..31cb1918 --- /dev/null +++ b/Source/Integration/WorkerRunnable.cpp @@ -0,0 +1,26 @@ + +#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; +} \ No newline at end of file diff --git a/Source/Integration/WorkerRunnable.hpp b/Source/Integration/WorkerRunnable.hpp new file mode 100644 index 00000000..b37eff56 --- /dev/null +++ b/Source/Integration/WorkerRunnable.hpp @@ -0,0 +1,15 @@ +#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; +}; + diff --git a/Source/Integration/engineutil.cpp b/Source/Integration/engineutil.cpp index 90b49067..b60c18de 100644 --- a/Source/Integration/engineutil.cpp +++ b/Source/Integration/engineutil.cpp @@ -14,6 +14,14 @@ void init_wrapper(EngineWrapper* w) { } } +// Print text using GEngine->Add... +void RawPrint(const char* text) { + if (GEngine) + { + GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString(text)); + } +} + static TArray dprints; void DPrint(const FString& fs) { diff --git a/Source/Integration/engineutil.hpp b/Source/Integration/engineutil.hpp index 426a07bf..7b78ac28 100644 --- a/Source/Integration/engineutil.hpp +++ b/Source/Integration/engineutil.hpp @@ -6,6 +6,9 @@ namespace engineutil { // Load the DLL and initialize the wrapper, if possible. void init_wrapper(EngineWrapper* w); +// Print text using GEngine->Add... +void RawPrint(const char* text); + // Print text on the console. void DPrint(const FString& fs);