Skip to content

Commit 54e0c9a

Browse files
committed
Split off entity code and ready for review
1 parent 7660f9c commit 54e0c9a

File tree

10 files changed

+194
-138
lines changed

10 files changed

+194
-138
lines changed

ecsact/runtime/serialize.hh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ std::vector<std::byte> serialize(const T& component_or_action) {
4646
return bytes;
4747
}
4848

49+
inline std::vector<std::byte> serialize(const ecsact_component& component) {
50+
std::vector<std::byte> out_component;
51+
out_component.resize(ecsact_serialize_component_size(component.component_id));
52+
53+
ecsact_serialize_component(
54+
component.component_id,
55+
component.component_data,
56+
reinterpret_cast<uint8_t*>(out_component.data())
57+
);
58+
return out_component;
59+
}
60+
61+
inline std::vector<std::byte> serialize(const ecsact_action& action) {
62+
std::vector<std::byte> out_action;
63+
out_action.resize(ecsact_serialize_action_size(action.action_id));
64+
65+
ecsact_serialize_action(
66+
action.action_id,
67+
action.action_data,
68+
reinterpret_cast<uint8_t*>(out_action.data())
69+
);
70+
return out_action;
71+
}
72+
73+
// NOTE: Add the wrapper for serialize/derserialize here
74+
4975
/**
5076
* Calls `ecsact_deserialize_action` or `ecsact_deserialize_component` based on
5177
* the type of @tp T.
@@ -115,4 +141,40 @@ int deserialize(
115141
::ecsact::deserialize<T>(serialized_component_or_action, read_amount);
116142
return read_amount;
117143
}
144+
145+
inline ecsact_action deserialize(
146+
const ecsact_action_id& id,
147+
std::vector<std::byte>& serialized_action
148+
) {
149+
auto action = ecsact_action{};
150+
151+
ecsact_deserialize_action(
152+
id,
153+
reinterpret_cast<uint8_t*>(serialized_action.data()),
154+
&action
155+
);
156+
return action;
157+
}
158+
159+
inline ecsact_component deserialize(
160+
const ecsact_component_id& id,
161+
std::vector<std::byte>& serialized_component
162+
) {
163+
auto component = ecsact_component{};
164+
165+
std::vector<uint8_t> component_data;
166+
component_data.resize(serialized_component.size());
167+
168+
ecsact_deserialize_component(
169+
id,
170+
reinterpret_cast<uint8_t*>(serialized_component.data()),
171+
component_data.data()
172+
);
173+
174+
component.component_id = id;
175+
component.component_data = component_data.data();
176+
177+
return component;
178+
}
179+
118180
} // namespace ecsact

reference/async_reference/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cc_library(
2020
"//:core",
2121
"//reference/async_reference/callbacks:async_callbacks",
2222
"//reference/async_reference/callbacks:execution_callbacks",
23+
"//reference/async_reference/entity_manager",
2324
"//reference/async_reference/tick_manager",
2425
"//reference/async_reference/util",
2526
"//reference/async_reference/util:types",

reference/async_reference/async_reference.cc

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void async_reference::execute_systems() {
175175
);
176176
}
177177

178-
process_entities();
178+
entity_manager.process_entities(async_callbacks, *registry_id);
179179

180180
auto systems_error =
181181
ecsact_execute_systems(*registry_id, 1, options.get(), &collector);
@@ -216,8 +216,7 @@ ecsact_async_request_id async_reference::create_entity_request() {
216216
return req_id;
217217
}
218218

219-
std::unique_lock lk(pending_m);
220-
pending_entity_requests.insert(pending_entity_requests.end(), req_id);
219+
entity_manager.request_entity(req_id);
221220
return req_id;
222221
}
223222

@@ -237,23 +236,3 @@ ecsact_async_request_id async_reference::next_request_id() {
237236
ecsact_async_request_id async_reference::convert_request_id(int32_t id) {
238237
return static_cast<ecsact_async_request_id>(id);
239238
}
240-
241-
void async_reference::process_entities() {
242-
std::vector<ecsact_async_request_id> pending_entities;
243-
244-
std::unique_lock lk(pending_m);
245-
pending_entities = std::move(pending_entity_requests);
246-
pending_entity_requests.clear();
247-
lk.unlock();
248-
249-
for(auto& entity_request_id : pending_entities) {
250-
auto entity = ecsact_create_entity(*registry_id);
251-
252-
types::entity created_entity{
253-
.entity_id = entity,
254-
.request_id = entity_request_id,
255-
};
256-
257-
async_callbacks.add(created_entity);
258-
}
259-
}

reference/async_reference/async_reference.hh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "reference/async_reference/tick_manager/tick_manager.hh"
1515
#include "reference/async_reference/callbacks/execution_callbacks.hh"
1616
#include "reference/async_reference/callbacks/async_callbacks.hh"
17+
#include "reference/async_reference/entity_manager/entity_manager.hh"
1718

1819
#include "ecsact/runtime/core.hh"
1920
#include "ecsact/runtime/async.h"
@@ -32,7 +33,6 @@ public:
3233
);
3334

3435
ecsact_async_request_id create_entity_request();
35-
3636
ecsact_async_request_id connect(const char* connection_string);
3737

3838
void disconnect();
@@ -42,16 +42,13 @@ private:
4242

4343
std::optional<ecsact_registry_id> registry_id;
4444

45-
void process_entities();
46-
4745
tick_manager tick_manager;
4846
execution_callbacks exec_callbacks;
4947
async_callbacks async_callbacks;
48+
entity_manager entity_manager;
5049

51-
std::vector<ecsact_async_request_id> pending_entity_requests;
50+
std::thread execution_thread;
5251

53-
std::thread execution_thread;
54-
std::mutex pending_m;
5552
std::atomic_bool is_connected = false;
5653
std::atomic_bool is_connected_notified = false;
5754

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load("//bazel:copts.bzl", "copts")
3+
4+
cc_library(
5+
name = "entity_manager",
6+
srcs = ["entity_manager.cc"],
7+
hdrs = ["entity_manager.hh"],
8+
copts = copts,
9+
local_defines = [
10+
"ECSACT_CORE_API=",
11+
],
12+
visibility = ["//visibility:public"],
13+
deps = [
14+
"//:core",
15+
"//reference/async_reference/callbacks:async_callbacks",
16+
"//reference/async_reference/util:types",
17+
],
18+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "entity_manager.hh"
2+
3+
void entity_manager::process_entities(
4+
async_callbacks& callbacks,
5+
const ecsact_registry_id& registry_id
6+
) {
7+
std::vector<ecsact_async_request_id> pending_entities;
8+
9+
std::unique_lock lk(pending_m);
10+
pending_entities = std::move(pending_entity_requests);
11+
pending_entity_requests.clear();
12+
lk.unlock();
13+
14+
for(auto& entity_request_id : pending_entities) {
15+
auto entity = ecsact_create_entity(registry_id);
16+
17+
types::entity created_entity{
18+
.entity_id = entity,
19+
.request_id = entity_request_id,
20+
};
21+
22+
callbacks.add(created_entity);
23+
}
24+
}
25+
26+
void entity_manager::request_entity(const ecsact_async_request_id& req_id) {
27+
std::unique_lock lk(pending_m);
28+
pending_entity_requests.push_back(req_id);
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <mutex>
5+
6+
#include "ecsact/runtime/core.hh"
7+
#include "reference/async_reference/callbacks/async_callbacks.hh"
8+
#include "reference/async_reference/util/types.hh"
9+
10+
class entity_manager {
11+
public:
12+
void process_entities(
13+
async_callbacks& callbacks,
14+
const ecsact_registry_id& registry_id
15+
);
16+
17+
void request_entity(const ecsact_async_request_id& req_id);
18+
19+
private:
20+
std::mutex pending_m;
21+
22+
std::vector<ecsact_async_request_id> pending_entity_requests;
23+
};

reference/async_reference/tick_manager/tick_manager.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void tick_manager::add_pending_options(types::pending_execution_options& pending
1111

1212
std::optional<types::cpp_execution_options>
1313
tick_manager::move_and_increment_tick() {
14-
std::optional<types::cpp_execution_options> cpp_options;
14+
auto cpp_options = std::optional<types::cpp_execution_options>{};
1515

1616
if(validated_tick_map.contains(tick)) {
1717
std::unique_lock lk(tick_m);
@@ -33,7 +33,7 @@ types::async_error tick_manager::validate_pending_options() {
3333
lk.unlock();
3434

3535
for(auto& [key, pending_list] : pending_options) {
36-
types::cpp_execution_options merged_options{};
36+
auto merged_options = types::cpp_execution_options{};
3737

3838
for(int i = 0; i < pending_list.size(); i++) {
3939
merged_options = {};

0 commit comments

Comments
 (0)