Skip to content

Commit

Permalink
feat: some logs and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Aug 29, 2024
1 parent ddac3f0 commit b2b1499
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ auto UEcsactAsyncConnectBlueprintAction::Activate() -> void {
}

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;
Expand All @@ -54,6 +56,7 @@ auto UEcsactAsyncConnectBlueprintAction::Activate() -> void {
}

auto UEcsactAsyncConnectBlueprintAction::OnRequestDone() -> void {
UE_LOG(Ecsact, Error, TEXT("OnRequestDone??"));
if(!bConnectFailed) {
OnSuccess.Broadcast({});
}
Expand All @@ -63,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
21 changes: 17 additions & 4 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,6 +88,7 @@ auto FEcsactModule::UnloadEcsactRuntime() -> void {
}

auto FEcsactModule::StartupModule() -> void {
UE_LOG(Ecsact, Warning, TEXT("Ecsact Startup Module"));
Self = this;
if(!GIsEditor) {
LoadEcsactRuntime();
Expand All @@ -103,6 +108,7 @@ auto FEcsactModule::ShutdownModule() -> void {
FEditorDelegates::PreBeginPIE.RemoveAll(this);
FEditorDelegates::EndPIE.RemoveAll(this);
#endif
UE_LOG(Ecsact, Warning, TEXT("Ecsact Shutdown Module"));
Self = nullptr;
}

Expand Down Expand Up @@ -149,7 +155,7 @@ auto FEcsactModule::StartRunner() -> void {
UE_LOG(
Ecsact,
Log,
TEXT("Using ecsact runner: %s"),
TEXT("Starting ecsact runner: %s"),
*Runner->GetClass()->GetName()
);
Runner->AddToRoot();
Expand All @@ -159,9 +165,16 @@ auto FEcsactModule::StartRunner() -> void {

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
7 changes: 4 additions & 3 deletions Source/Ecsact/Public/EcsactUnreal/Ecsact.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

#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;
Expand Down
68 changes: 64 additions & 4 deletions Source/Ecsact/Public/EcsactUnreal/EcsactAsyncRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "ecsact/runtime/common.h"

UEcsactAsyncRunner::UEcsactAsyncRunner() {
UE_LOG(
Ecsact,
Warning,
TEXT("UEcsactAsyncRunner CONSTRUCTOR %i"),
(intptr_t)this
);
async_evc.async_error_callback = ThisClass::OnAsyncErrorRaw;
async_evc.system_error_callback = ThisClass::OnExecuteSysErrorRaw;
async_evc.async_request_done_callback = ThisClass::OnAsyncRequestDoneRaw;
Expand All @@ -28,10 +34,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,17 +77,40 @@ 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) {
Expand Down Expand Up @@ -105,6 +151,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 @@ -114,6 +167,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;
}
4 changes: 3 additions & 1 deletion Source/Ecsact/Public/EcsactUnreal/EcsactExecution.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "UObject/WeakObjectPtrTemplates.h"

class EcsactUnrealExecution {
friend class UEcsactSyncRunner;
friend class UEcsactAsyncRunner;
Expand All @@ -18,5 +20,5 @@ class EcsactUnrealExecution {
/**
*
*/
static auto Runner() -> class UEcsactRunner*;
static auto Runner() -> TWeakObjectPtr<class UEcsactRunner>;
};
2 changes: 1 addition & 1 deletion Source/Ecsact/Public/EcsactUnreal/EcsactRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ auto UEcsactRunner::GetStatId() const -> TStatId {
}

auto UEcsactRunner::IsTickable() const -> bool {
return !IsTemplate();
return !IsTemplate() && !bIsStopped;
}

auto UEcsactRunner::InitRunnerSubsystems() -> void {
Expand Down

0 comments on commit b2b1499

Please sign in to comment.