Skip to content

Commit

Permalink
feat: ecsact package subsystems (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored Sep 11, 2024
1 parent e454e94 commit 77b4a4d
Show file tree
Hide file tree
Showing 17 changed files with 1,181 additions and 69 deletions.
17 changes: 8 additions & 9 deletions Source/Ecsact/Ecsact.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public Ecsact(ReadOnlyTargetRules Target) : base(Target) {
"Engine",
"Slate",
"SlateCore",
"Kismet",
});

DynamicallyLoadedModuleNames.AddRange(new string[] {
Expand Down Expand Up @@ -80,14 +79,14 @@ public Ecsact(ReadOnlyTargetRules Target) : base(Target) {
"--plugin=cpp_systems_source"
};

// if(!File.Exists(EcsactUnrealCodegenPluginPath)) {
// Console.WriteLine(
// "warning: EcsactUnrealCodegenPlugin was not built. It should have "
// + "been shipped with the Ecsact Unreal integration plugin."
// );
// } else {
// CodegenArgs.Add($"--plugin={EcsactUnrealCodegenPluginPath}");
// }
if(!File.Exists(EcsactUnrealCodegenPluginPath)) {
Console.WriteLine(
"warning: EcsactUnrealCodegenPlugin was not built. It should have " +
"been shipped with the Ecsact Unreal integration plugin."
);
} else {
CodegenArgs.Add($"--plugin={EcsactUnrealCodegenPluginPath}");
}

CodegenArgs.AddRange(EcsactSources);
ExecEcsactCli(CodegenArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ auto UEcsactAsyncConnectBlueprintAction::Activate() -> void {
TEXT("Cannot use Ecsact async blueprint api with runner that does not "
"implement IEcsactAsyncRunnerEvents")
);
OnError.Broadcast(EAsyncConnectError::AsyncRunnerEventsUnavailable);
OnDone.Broadcast({});
return;
}

auto req_id = ecsact_async_connect(Utf8ConnectionString.c_str());
UE_LOG(Ecsact, Warning, TEXT("async connect request id=%i"), req_id);

if(req_id == ECSACT_INVALID_ID(async_request)) {
UE_LOG(Ecsact, Error, TEXT("Invalid Request ID"));
OnError.Broadcast(EAsyncConnectError::InvalidRequestId);
OnDone.Broadcast({});
return;
}

async_events->OnRequestDone(
req_id,
Expand All @@ -46,6 +56,7 @@ auto UEcsactAsyncConnectBlueprintAction::Activate() -> void {
}

auto UEcsactAsyncConnectBlueprintAction::OnRequestDone() -> void {
UE_LOG(Ecsact, Error, TEXT("OnRequestDone??"));
if(!bConnectFailed) {
OnSuccess.Broadcast({});
}
Expand All @@ -55,12 +66,13 @@ auto UEcsactAsyncConnectBlueprintAction::OnRequestDone() -> void {
auto UEcsactAsyncConnectBlueprintAction::OnRequestError( //
ecsact_async_error Error
) -> void {
UE_LOG(Ecsact, Error, TEXT("OnRequestError??"));
switch(Error) {
case ECSACT_ASYNC_ERR_PERMISSION_DENIED:
OnError.Broadcast(EAsyncConnectError::PermissionDenied);
break;
case ECSACT_ASYNC_INVALID_CONNECTION_STRING:
OnError.Broadcast(EAsyncConnectError::PermissionDenied);
OnError.Broadcast(EAsyncConnectError::InvalidConnectionString);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
UENUM()
enum class EAsyncConnectError : uint8 {
NoError,
AsyncRunnerEventsUnavailable,
InvalidRequestId,
PermissionDenied,
InvalidConnectionString,
};
Expand Down Expand Up @@ -54,7 +56,7 @@ class ECSACT_API UEcsactAsyncConnectBlueprintAction
FAsyncConnectDoneCallback OnDone;

/**
* Async request is done and no errors occured.
* Async request is done and no errors occurred.
*/
UPROPERTY(BlueprintAssignable)
FAsyncConnectDoneCallback OnSuccess;
Expand Down
27 changes: 21 additions & 6 deletions Source/Ecsact/Public/EcsactUnreal/Ecsact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ FOR_EACH_ECSACT_API_FN(INIT_ECSACT_API_FN, UNUSED_PARAM);
FEcsactModule* FEcsactModule::Self = nullptr;

auto FEcsactModule::Get() -> FEcsactModule& {
check(Self != nullptr);
return *Self;
if(GIsEditor) {
return FModuleManager::Get().GetModuleChecked<FEcsactModule>("Ecsact");
} else {
check(Self != nullptr);
return *Self;
}
}

auto FEcsactModule::Abort() -> void {
Expand Down Expand Up @@ -84,13 +88,14 @@ auto FEcsactModule::UnloadEcsactRuntime() -> void {
}

auto FEcsactModule::StartupModule() -> void {
UE_LOG(Ecsact, Warning, TEXT("Ecsact Startup Module"));
Self = this;
if(!GIsEditor) {
LoadEcsactRuntime();
}
#if WITH_EDITOR
FEditorDelegates::PreBeginPIE.AddRaw(this, &FEcsactModule::OnPreBeginPIE);
FEditorDelegates::EndPIE.AddRaw(this, &FEcsactModule::OnEndPIE);
FEditorDelegates::EndPIE.AddRaw(this, &FEcsactModule::OnPrePIEEnded);
#endif
}

Expand All @@ -103,14 +108,15 @@ auto FEcsactModule::ShutdownModule() -> void {
FEditorDelegates::PreBeginPIE.RemoveAll(this);
FEditorDelegates::EndPIE.RemoveAll(this);
#endif
UE_LOG(Ecsact, Warning, TEXT("Ecsact Shutdown Module"));
Self = nullptr;
}

auto FEcsactModule::OnPreBeginPIE(bool _) -> void {
LoadEcsactRuntime();
}

auto FEcsactModule::OnEndPIE(bool _) -> void {
auto FEcsactModule::OnPrePIEEnded(bool _) -> void {
UnloadEcsactRuntime();
}

Expand Down Expand Up @@ -149,17 +155,26 @@ auto FEcsactModule::StartRunner() -> void {
UE_LOG(
Ecsact,
Log,
TEXT("Using ecsact runner: %s"),
TEXT("Starting ecsact runner: %s"),
*Runner->GetClass()->GetName()
);
Runner->AddToRoot();
Runner->Start();
}
}

auto FEcsactModule::StopRunner() -> void {
if(Runner != nullptr) {
UE_LOG(
Ecsact,
Log,
TEXT("Stopping ecsact runner: %s"),
*Runner->GetClass()->GetName()
);
Runner->Stop();
Runner->RemoveFromRoot();
Runner = nullptr;
Runner->MarkAsGarbage();
Runner.Reset();
}
}

Expand Down
9 changes: 5 additions & 4 deletions Source/Ecsact/Public/EcsactUnreal/Ecsact.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
#include "UObject/WeakObjectPtr.h"

DECLARE_LOG_CATEGORY_EXTERN(Ecsact, Log, All);

class FEcsactModule : public IModuleInterface {
friend class EcsactUnrealExecution;

static FEcsactModule* Self;
void* EcsactRuntimeHandle;
class UEcsactRunner* Runner;
static FEcsactModule* Self;
void* EcsactRuntimeHandle;
TWeakObjectPtr<class UEcsactRunner> Runner;

auto LoadEcsactRuntime() -> void;
auto UnloadEcsactRuntime() -> void;
auto Abort() -> void;
auto OnPreBeginPIE(bool bIsSimulating) -> void;
auto OnEndPIE(const bool bIsSimulating) -> void;
auto OnPrePIEEnded(const bool bIsSimulating) -> void;

auto StartRunner() -> void;
auto StopRunner() -> void;
Expand Down
77 changes: 67 additions & 10 deletions Source/Ecsact/Public/EcsactUnreal/EcsactAsyncRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,27 @@ auto UEcsactAsyncRunner::OnAsyncErrorRaw(

for(auto req_id : request_ids) {
auto cbs = self->RequestErrorCallbacks.Find(req_id);
if(cbs) {

UE_LOG(Ecsact, Warning, TEXT("async request error id=%i"), req_id);

if(cbs && !cbs->IsEmpty()) {
for(auto& cb : *cbs) {
cb.ExecuteIfBound(async_err);
if(!cb.ExecuteIfBound(async_err)) {
UE_LOG(
Ecsact,
Warning,
TEXT("Unbound async error callback for request %i"),
req_id
);
}
}
} else {
UE_LOG(
Ecsact,
Warning,
TEXT("No async error callbacks for request %i"),
req_id
);
}
}
}
Expand All @@ -54,27 +71,46 @@ auto UEcsactAsyncRunner::OnAsyncRequestDoneRaw(

for(auto req_id : request_ids) {
auto cbs = self->RequestDoneCallbacks.Find(req_id);
if(cbs) {

UE_LOG(Ecsact, Warning, TEXT("async request done id=%i"), req_id);

if(cbs && !cbs->IsEmpty()) {
for(auto& cb : *cbs) {
cb.ExecuteIfBound();
if(!cb.ExecuteIfBound()) {
UE_LOG(
Ecsact,
Warning,
TEXT("Unbound async done callback for request %i (self=%i)"),
req_id,
(intptr_t)self
);
}
}

cbs->Empty();
} else {
UE_LOG(
Ecsact,
Warning,
TEXT("No async done callbacks for request %i (self=%i)"),
req_id,
(intptr_t)self
);
}
}
}

auto UEcsactAsyncRunner::Tick(float DeltaTime) -> void {
if(IsStopped()) {
return;
}

EnqueueExecutionOptions();

if(ecsact_async_flush_events == nullptr) {
UE_LOG(Ecsact, Error, TEXT("ecsact_async_flush_events is unavailable"));
} else {
ecsact_execution_events_collector* evc_c = nullptr;
if(EventsCollector != nullptr) {
evc_c = EventsCollector->GetCEVC();
}
ecsact_async_flush_events(evc_c, &async_evc);
ecsact_async_flush_events(GetEventsCollector(), &async_evc);
}
}

Expand All @@ -92,7 +128,14 @@ auto UEcsactAsyncRunner::EnqueueExecutionOptions() -> void {
}

if(ExecutionOptions->IsNotEmpty()) {
ecsact_async_enqueue_execution_options(*ExecutionOptions->GetCPtr());
auto req_id =
ecsact_async_enqueue_execution_options(*ExecutionOptions->GetCPtr());
UE_LOG(
Ecsact,
Warning,
TEXT("Actually enqueueing some options! (req_id=%i)"),
(int)req_id
);
ExecutionOptions->Clear();
}
}
Expand All @@ -109,6 +152,13 @@ auto UEcsactAsyncRunner::OnRequestDone(
FAsyncRequestDoneCallback Callback
) -> void {
check(RequestId != ECSACT_INVALID_ID(async_request));
UE_LOG(
Ecsact,
Error,
TEXT("Adding on request done callback (req_id=%i self=%i)"),
RequestId,
(intptr_t)this
);

RequestDoneCallbacks.FindOrAdd(RequestId).Add(Callback);
}
Expand All @@ -118,6 +168,13 @@ auto UEcsactAsyncRunner::OnRequestError(
FAsyncRequestErrorCallback Callback
) -> void {
check(RequestId != ECSACT_INVALID_ID(async_request));
UE_LOG(
Ecsact,
Error,
TEXT("Adding on request error callback (req_id=%i self=%i)"),
RequestId,
(intptr_t)this
);

RequestErrorCallbacks.FindOrAdd(RequestId).Add(Callback);
}
2 changes: 1 addition & 1 deletion Source/Ecsact/Public/EcsactUnreal/EcsactExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ auto EcsactUnrealExecution::DeltaTime() -> float {
return DeltaTime_;
}

auto EcsactUnrealExecution::Runner() -> class UEcsactRunner* {
auto EcsactUnrealExecution::Runner() -> TWeakObjectPtr<class UEcsactRunner> {
return FEcsactModule::Get().Runner;
}
7 changes: 5 additions & 2 deletions Source/Ecsact/Public/EcsactUnreal/EcsactExecution.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

class EcsactUnrealExecution {
#include "EcsactUnreal/Ecsact.h"
#include "UObject/WeakObjectPtrTemplates.h"

class ECSACT_API EcsactUnrealExecution {
friend class UEcsactSyncRunner;
friend class UEcsactAsyncRunner;
static float DeltaTime_;
Expand All @@ -18,5 +21,5 @@ class EcsactUnrealExecution {
/**
*
*/
static auto Runner() -> class UEcsactRunner*;
static auto Runner() -> TWeakObjectPtr<class UEcsactRunner>;
};
Loading

0 comments on commit 77b4a4d

Please sign in to comment.