Skip to content

Commit

Permalink
chore: Push changes to work on another device
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 10, 2024
1 parent b2b1499 commit 82e2246
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 20 deletions.
16 changes: 8 additions & 8 deletions Source/Ecsact/Ecsact.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,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
15 changes: 8 additions & 7 deletions Source/Ecsact/Public/EcsactUnreal/EcsactAsyncRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
#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 Down Expand Up @@ -134,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 Down
3 changes: 2 additions & 1 deletion Source/Ecsact/Public/EcsactUnreal/EcsactExecution.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "EcsactUnreal/Ecsact.h"
#include "UObject/WeakObjectPtrTemplates.h"

class EcsactUnrealExecution {
class ECSACT_API EcsactUnrealExecution {
friend class UEcsactSyncRunner;
friend class UEcsactAsyncRunner;
static float DeltaTime_;
Expand Down
51 changes: 51 additions & 0 deletions Source/Ecsact/Public/EcsactUnreal/EcsactRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "EcsactUnreal/EcsactRunner.h"
#include "EcsactUnreal/EcsactUnrealExecutionOptions.h"
#include "UObject/ObjectMacros.h"
#include "UObject/UObjectIterator.h"
#include "EcsactUnreal/EcsactRunnerSubsystem.h"
#include "EcsactUnreal/Ecsact.h"
#include "ecsact/runtime/common.h"

UEcsactRunner::UEcsactRunner() : EventsCollector{} {
ExecutionOptions = CreateDefaultSubobject<UEcsactUnrealExecutionOptions>( //
TEXT("ExecutionOptions")
);

EventsCollector.init_callback_user_data = this;
EventsCollector.update_callback_user_data = this;
EventsCollector.remove_callback_user_data = this;
Expand Down Expand Up @@ -47,6 +52,18 @@ auto UEcsactRunner::IsTickable() const -> bool {
return !IsTemplate() && !bIsStopped;
}

auto UEcsactRunner::CreateEntity() -> EcsactRunnerCreateEntityBuilder {
return {this, GeneratePlaceholderId()};
}

auto UEcsactRunner::GeneratePlaceholderId() -> ecsact_placeholder_entity_id {
static ecsact_placeholder_entity_id LastPlaceholderId = {};
using ref_t = std::add_lvalue_reference_t<
std::underlying_type_t<decltype(LastPlaceholderId)>>;
reinterpret_cast<ref_t>(LastPlaceholderId) += 1;
return LastPlaceholderId;
}

auto UEcsactRunner::InitRunnerSubsystems() -> void {
auto subsystem_types = TArray<UClass*>{};
for(auto it = TObjectIterator<UClass>{}; it; ++it) {
Expand Down Expand Up @@ -139,6 +156,14 @@ auto UEcsactRunner::OnEntityCreatedRaw(
void* callback_user_data
) -> void {
auto self = static_cast<ThisClass*>(callback_user_data);

auto create_callback =
self->CreateEntityCallbacks.Find(placeholder_entity_id);
if(create_callback) {
create_callback->Execute(entity_id);
self->CreateEntityCallbacks.Remove(placeholder_entity_id);
}

for(auto subsystem : self->RunnerSubsystems) {
subsystem->EntityCreated(static_cast<int32>(entity_id));
}
Expand All @@ -155,3 +180,29 @@ auto UEcsactRunner::OnEntityDestroyedRaw(
subsystem->EntityDestroyed(static_cast<int32>(entity_id));
}
}

UEcsactRunner::EcsactRunnerCreateEntityBuilder::EcsactRunnerCreateEntityBuilder(
UEcsactRunner* Owner,
ecsact_placeholder_entity_id PlaceholderId
)
: Owner{Owner}
, PlaceholderId{PlaceholderId}
, Builder{Owner->ExecutionOptions->CreateEntity(PlaceholderId)} {
}

UEcsactRunner::EcsactRunnerCreateEntityBuilder::
EcsactRunnerCreateEntityBuilder(EcsactRunnerCreateEntityBuilder&&) = default;

UEcsactRunner::EcsactRunnerCreateEntityBuilder::
~EcsactRunnerCreateEntityBuilder() = default;

auto UEcsactRunner::EcsactRunnerCreateEntityBuilder::Finish() -> void {
Builder.Finish();
}

auto UEcsactRunner::EcsactRunnerCreateEntityBuilder::OnCreate(
TDelegate<void(ecsact_entity_id)> Callback
) && -> EcsactRunnerCreateEntityBuilder {
Owner->CreateEntityCallbacks.Add(PlaceholderId, Callback);
return std::move(*this);
}
56 changes: 55 additions & 1 deletion Source/Ecsact/Public/EcsactUnreal/EcsactRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

UCLASS(Abstract)

class UEcsactRunner : public UObject, public FTickableGameObject {
class ECSACT_API UEcsactRunner : public UObject, public FTickableGameObject {
GENERATED_BODY() // NOLINT

TArray<class UEcsactRunnerSubsystem*> RunnerSubsystems;
ecsact_execution_events_collector EventsCollector;
bool bIsStopped = false;

TMap<ecsact_placeholder_entity_id, TDelegate<void(ecsact_entity_id)>>
CreateEntityCallbacks;

static auto OnInitComponentRaw(
ecsact_event event,
ecsact_entity_id entity_id,
Expand Down Expand Up @@ -62,7 +65,11 @@ class UEcsactRunner : public UObject, public FTickableGameObject {
virtual auto InitRunnerSubsystems() -> void;
virtual auto ShutdownRunnerSubsystems() -> void;

virtual auto GeneratePlaceholderId() -> ecsact_placeholder_entity_id;

public:
class EcsactRunnerCreateEntityBuilder;

UEcsactRunner();

virtual auto Start() -> void;
Expand All @@ -73,6 +80,13 @@ class UEcsactRunner : public UObject, public FTickableGameObject {
auto GetStatId() const -> TStatId override;
auto IsTickable() const -> bool override;

/**
* Returns a builder for creating a new entity. This builder is 'runner aware'
* meaning that any lifecycle hooks that the builder exposes is provided by
* the runners execution
*/
auto CreateEntity() -> EcsactRunnerCreateEntityBuilder;

template<typename A>
auto PushAction(const A& Action) -> void {
return ExecutionOptions->PushAction<A>(Action);
Expand All @@ -93,3 +107,43 @@ class UEcsactRunner : public UObject, public FTickableGameObject {
return ExecutionOptions->RemoveComponent<C>(Entity);
}
};

class ECSACT_API UEcsactRunner::EcsactRunnerCreateEntityBuilder {
friend UEcsactRunner;

UEcsactRunner* Owner;
ecsact_placeholder_entity_id PlaceholderId;

UEcsactUnrealExecutionOptions::CreateEntityBuilder Builder;

EcsactRunnerCreateEntityBuilder(
UEcsactRunner* Owner,
ecsact_placeholder_entity_id PlacerholderId
);

public:
EcsactRunnerCreateEntityBuilder(EcsactRunnerCreateEntityBuilder&&);
~EcsactRunnerCreateEntityBuilder();

template<typename C>
auto AddComponent( //
const C& Component
) && -> EcsactRunnerCreateEntityBuilder {
static_cast<UEcsactUnrealExecutionOptions::CreateEntityBuilder&&>(Builder)
.AddComponent<C>(Component);
return std::move(*this);
}

/**
* Listens for when the entity is created.
*/
auto OnCreate( //
TDelegate<void(ecsact_entity_id)> Callback
) && -> EcsactRunnerCreateEntityBuilder;

/**
* This is automatically called by the destructor, but can be called manually
* to 'finish' building your entity.
*/
auto Finish() -> void;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "EcsactUnreal/EcsactUnrealExecutionOptions.h"
#include "ecsact/runtime/common.h"

using CreateEntityBuilder = UEcsactUnrealExecutionOptions::CreateEntityBuilder;

UEcsactUnrealExecutionOptions::UEcsactUnrealExecutionOptions() : ExecOpts({}) {
}
Expand All @@ -12,7 +15,8 @@ auto UEcsactUnrealExecutionOptions::IsNotEmpty() const -> bool {
ExecOpts.add_components_length > 0 ||
ExecOpts.destroy_entities_length > 0 ||
ExecOpts.update_components_length > 0 ||
ExecOpts.remove_components_length > 0;
ExecOpts.remove_components_length > 0 ||
ExecOpts.create_entities_length > 0;
}

auto UEcsactUnrealExecutionOptions::Clear() -> void {
Expand All @@ -29,5 +33,70 @@ auto UEcsactUnrealExecutionOptions::Clear() -> void {
AddComponentList.Empty();
UpdateComponentList.Empty();
RemoveComponentList.Empty();
DestroyEntityList.Empty();
CreateEntityList.Empty();
CreateEntityComponentsList.Empty();
CreateEntityComponentsListData.Empty();
CreateEntityComponentsListNums.Empty();
ExecOpts = {};
}

auto UEcsactUnrealExecutionOptions::CreateEntity(
ecsact_placeholder_entity_id PlaceholderId
) -> CreateEntityBuilder {
return {this, PlaceholderId};
}

CreateEntityBuilder::CreateEntityBuilder(
UEcsactUnrealExecutionOptions* Owner,
ecsact_placeholder_entity_id PlacerholderId
)
: bValid(true), Owner(Owner), PlaceholderId(PlaceholderId) {
}

CreateEntityBuilder::CreateEntityBuilder(CreateEntityBuilder&& Other) {
bValid = Other.bValid;
Owner = Other.Owner;
ComponentList = std::move(Other.ComponentList);

Other.bValid = false;
Other.Owner = nullptr;
Other.ComponentList = {};
}

CreateEntityBuilder::~CreateEntityBuilder() {
if(bValid && Owner) {
Finish();
}
}

auto CreateEntityBuilder::Finish() -> void {
if(!bValid || !Owner) {
return;
}

UE_LOG(
LogTemp,
Warning,
TEXT("CreateEntityBuilder::Finish() after valid check")
);

Owner->CreateEntityList.Add(PlaceholderId);
Owner->CreateEntityComponentsListNums.Add(ComponentList.Num());
Owner->CreateEntityComponentsList.Push(std::move(ComponentList));
Owner->CreateEntityComponentsListData.Empty();
for(auto& list : Owner->CreateEntityComponentsList) {
Owner->CreateEntityComponentsListData.Add(list.GetData());
}

Owner->ExecOpts.create_entities_length = Owner->CreateEntityList.Num();
Owner->ExecOpts.create_entities = Owner->CreateEntityList.GetData();
Owner->ExecOpts.create_entities_components_length =
Owner->CreateEntityComponentsListNums.GetData();
Owner->ExecOpts.create_entities_components =
Owner->CreateEntityComponentsListData.GetData();

bValid = false;
Owner = nullptr;
ComponentList = {};
}
Loading

0 comments on commit 82e2246

Please sign in to comment.