Skip to content

Commit

Permalink
feat: added runner and events collector
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Aug 15, 2024
1 parent 39501c8 commit 272d969
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Source/Ecsact/Private/Ecsact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "CoreGlobals.h"
#include "EcsactSettings.h"
#include "HAL/PlatformProcess.h"
#include "Logging/LogVerbosity.h"
#include "Misc/Paths.h"
#include "EcsactAsyncRunner.h"
#include "ecsact/runtime.h"
Expand Down Expand Up @@ -35,6 +36,7 @@ auto FEcsactModule::LoadEcsactRuntime() -> void {
FPaths::ProjectDir(),
TEXT("Binaries/Win64/EcsactRuntime.dll")
);
UE_LOG(Ecsact, Log, TEXT("Loading ecsact runtime %s"), *ecsact_runtime_path);

EcsactRuntimeHandle = FPlatformProcess::GetDllHandle(*ecsact_runtime_path);

Expand Down Expand Up @@ -65,6 +67,8 @@ auto FEcsactModule::LoadEcsactRuntime() -> void {
}

auto FEcsactModule::UnloadEcsactRuntime() -> void {
UE_LOG(Ecsact, Log, TEXT("Unloading ecsact runtime"));

StopRunner();
if(EcsactRuntimeHandle) {
FPlatformProcess::FreeDllHandle(EcsactRuntimeHandle);
Expand Down
8 changes: 7 additions & 1 deletion Source/Ecsact/Public/EcsactAsyncRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "EcsactAsyncRunner.h"
#include "Ecsact.h"
#include "EcsactUnrealEventsCollector.h"
#include "ecsact/runtime/async.h"
#include "ecsact/runtime/common.h"

auto UEcsactAsyncRunner::Tick(float DeltaTime) -> void {
if(ecsact_async_flush_events == nullptr) {
UE_LOG(Ecsact, Error, TEXT("ecsact_async_flush_events is unavailable"));
} else {
ecsact_async_flush_events(nullptr, nullptr);
ecsact_execution_events_collector* evc_c = nullptr;
if(EventsCollector != nullptr) {
evc_c = EventsCollector->GetCEVC();
}
ecsact_async_flush_events(evc_c, nullptr);
}
}
3 changes: 3 additions & 0 deletions Source/Ecsact/Public/EcsactAsyncRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ UCLASS(NotBlueprintable)
class UEcsactAsyncRunner : public UEcsactRunner {
GENERATED_BODY()
public:
UPROPERTY()
class UEcsactUnrealEventsCollector* EventsCollector;

auto Tick(float DeltaTime) -> void override;
};
20 changes: 18 additions & 2 deletions Source/Ecsact/Public/EcsactSyncRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#include "Ecsact.h"
#include "EcsactSyncRunner.h"
#include "Ecsact.h"
#include "EcsactExecution.h"
#include "ecsact/runtime/core.h"

auto UEcsactSyncRunner::Tick(float DeltaTime) -> void {
if(ecsact_execute_systems == nullptr) {
UE_LOG(Ecsact, Error, TEXT("ecsact_execute_systems is unavailable"));
return;
}

EcsactUnrealExecution::DeltaTime_ = DeltaTime;
auto err = ecsact_execute_systems({}, 1, nullptr, nullptr);

if(registry_id == ECSACT_INVALID_ID(registry)) {
UE_LOG(
Ecsact,
Warning,
TEXT("UEcsactSyncRunner register_id is unset. Creating one for you. We "
"recommend creating your own instead.")
);
registry_id = ecsact_create_registry("Default Registry");
}

auto err = ecsact_execute_systems(registry_id, 1, nullptr, nullptr);
if(err != ECSACT_EXEC_SYS_OK) {
UE_LOG(Ecsact, Error, TEXT("Ecsact execution failed"));
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Ecsact/Public/EcsactSyncRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#include "Tickable.h"
#include "UObject/NoExportTypes.h"
#include "EcsactRunner.h"
#include "ecsact/runtime/common.h"
#include "EcsactSyncRunner.generated.h"

UCLASS(NotBlueprintable)

class UEcsactSyncRunner : public UEcsactRunner {
GENERATED_BODY()
public:
// TODO: Put this somewhere good.
ecsact_registry_id registry_id = ECSACT_INVALID_ID(registry);

auto Tick(float DeltaTime) -> void override;
};
117 changes: 117 additions & 0 deletions Source/Ecsact/Public/EcsactUnrealEventsCollector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "EcsactUnrealEventsCollector.h"
#include "Ecsact.h"

UEcsactUnrealEventsCollector::UEcsactUnrealEventsCollector() : evc{} {
evc.init_callback_user_data = this;
evc.update_callback_user_data = this;
evc.remove_callback_user_data = this;
evc.entity_created_callback_user_data = this;
evc.entity_destroyed_callback_user_data = this;

evc.init_callback = ThisClass::OnInitComponentRaw;
evc.update_callback = ThisClass::OnUpdateComponentRaw;
evc.remove_callback = ThisClass::OnRemoveComponentRaw;
evc.entity_created_callback = ThisClass::OnEntityCreatedRaw;
evc.entity_destroyed_callback = ThisClass::OnEntityDestroyedRaw;
}

auto UEcsactUnrealEventsCollector::OnInitComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

UE_LOG(
Ecsact,
Log,
TEXT("OnInitComponent (entity=%i, component=%i)"),
static_cast<int32_t>(entity_id),
static_cast<int32_t>(component_id)
);

self->InitComponentRawEvent
.Broadcast(entity_id, component_id, component_data);
}

auto UEcsactUnrealEventsCollector::OnUpdateComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

UE_LOG(
Ecsact,
Log,
TEXT("OnUpdateComponent (entity=%i, component=%i)"),
static_cast<int32_t>(entity_id),
static_cast<int32_t>(component_id)
);

self->UpdateComponentRawEvent
.Broadcast(entity_id, component_id, component_data);
}

auto UEcsactUnrealEventsCollector::OnRemoveComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

UE_LOG(
Ecsact,
Log,
TEXT("OnRemoveComponent (entity=%i, component=%i)"),
static_cast<int32_t>(entity_id),
static_cast<int32_t>(component_id)
);
self->RemoveComponentRawEvent
.Broadcast(entity_id, component_id, component_data);
}

auto UEcsactUnrealEventsCollector::OnEntityCreatedRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_placeholder_entity_id placeholder_entity_id,
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

UE_LOG(
Ecsact,
Log,
TEXT("OnEntityCreated (entity=%i)"),
static_cast<int32_t>(entity_id)
);
self->CreatedEntityRawEvent.Broadcast(entity_id, placeholder_entity_id);
}

auto UEcsactUnrealEventsCollector::OnEntityDestroyedRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_placeholder_entity_id placeholder_entity_id,
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

UE_LOG(
Ecsact,
Log,
TEXT("OnEntityDestroyed (entity=%i)"),
static_cast<int32_t>(entity_id)
);
self->DestroyedEntityRawEvent.Broadcast(entity_id, placeholder_entity_id);
}

auto UEcsactUnrealEventsCollector::GetCEVC()
-> ecsact_execution_events_collector* {
return &evc;
}
80 changes: 80 additions & 0 deletions Source/Ecsact/Public/EcsactUnrealEventsCollector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include "CoreMinimal.h"
#include "ecsact/runtime/common.h"
#include "EcsactUnrealEventsCollector.generated.h"

UCLASS()

class UEcsactUnrealEventsCollector : public UObject {
GENERATED_BODY()

ecsact_execution_events_collector evc;

static auto OnInitComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void;

static auto OnUpdateComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void;

static auto OnRemoveComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) -> void;

static auto OnEntityCreatedRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_placeholder_entity_id placeholder_entity_id,
void* callback_user_data
) -> void;

static auto OnEntityDestroyedRaw(
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_placeholder_entity_id placeholder_entity_id,
void* callback_user_data
) -> void;

public:
DECLARE_MULTICAST_DELEGATE_ThreeParams( //
FRawComponentEventDelegate,
ecsact_entity_id,
ecsact_component_id,
const void*
);
DECLARE_MULTICAST_DELEGATE_TwoParams( //
FRawEntityEventDelegate,
ecsact_entity_id,
ecsact_placeholder_entity_id
);

FRawComponentEventDelegate InitComponentRawEvent;
FRawComponentEventDelegate UpdateComponentRawEvent;
FRawComponentEventDelegate RemoveComponentRawEvent;

FRawEntityEventDelegate CreatedEntityRawEvent;
FRawEntityEventDelegate DestroyedEntityRawEvent;

UEcsactUnrealEventsCollector();

/**
* Get's the C `ecsact_execution_events_collector` pointer typically passed to
* `ecsact_execute_systems` or `ecsact_async_flush_events`. The lifetime of
* this pointer is the same as the owning `EcsactUnrealEventsCollector`.
*/
auto GetCEVC() -> ecsact_execution_events_collector*;
};

0 comments on commit 272d969

Please sign in to comment.