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;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Sockets", "Networking" });
PrivateDependencyModuleNames.AddRange(new string[] { });

View File

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

View File

@@ -6,23 +6,33 @@
#include "GameFramework/GameModeBase.h"
#include "enginewrapper.hpp"
#include "engineutil.hpp"
#include "WorkerRunnable.hpp"
#include "IntegrationGameModeBase.generated.h"
/**
*
*/
UCLASS()
class INTEGRATION_API AIntegrationGameModeBase : public AGameModeBase
class INTEGRATION_API AIntegrationGameModeBase : public AGameModeBase, public FRunnable
{
GENERATED_BODY()
public:
AIntegrationGameModeBase();
~AIntegrationGameModeBase();
virtual void BeginPlay() override;
virtual void Tick(float) override;
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() {
return Luprex.play_initialize != nullptr;
}
@@ -31,16 +41,17 @@ public:
UFUNCTION(BlueprintImplementableEvent)
void ConsoleSetOutput(const FString& text);
// This is called by the GUI whenever the user hits enter.
UFUNCTION(BlueprintCallable)
void ConsoleSendInput(const FString& text);
void HandleConsoleOutput();
void WaitForWorkerThread();
// Refresh the console output.
FWorkerRunnable* Worker;
// The worker thread is responsible for networking and event_update
FRunnableThread* Thread;
bool ThreadStopRequested;
engineutil::ConsoleOutput ConsoleOutput;
static EngineWrapper Luprex;
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...
void RawPrint(const char* text) {
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString(text));
}
}
static TArray<FString> dprints;
// The DPrint array. This stores the dprints
// until they can be collected by the console implementation.
static TArray<FString> dprint_array;
static FCriticalSection dprint_mutex;
void DPrint(const FString& fs) {
dprints.Emplace(fs);
FScopeLock lk(&dprint_mutex);
dprint_array.Emplace(fs);
}
void DPrint(const char* msg) {
dprints.Emplace(msg);
FScopeLock lk(&dprint_mutex);
dprint_array.Emplace(msg);
}
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() {
return dprints;
}
void DPrintClearStored() {
dprints.Reset();
TArray<FString> DPrintGetStored() {
FScopeLock lk(&dprint_mutex);
TArray<FString> result = std::move(dprint_array);
dprint_array.Empty();
return result;
}
void ConsoleOutput::Append(const FString& text) {

View File

@@ -6,9 +6,6 @@ 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);
@@ -19,10 +16,7 @@ void DPrint(const char* msg);
void DPrintHook(const char* msg, size_t len);
// Get all the stored dprints.
const TArray<FString>& DPrintGetStored();
// Clear the stored dprints.
void DPrintClearStored();
TArray<FString> DPrintGetStored();
class ConsoleOutput {
private: