Skip to content

Commit 7c76a64

Browse files
committed
Updated tests, rewrote code to fix lifetime issues
1 parent 8fbdd85 commit 7c76a64

File tree

16 files changed

+402
-137
lines changed

16 files changed

+402
-137
lines changed

ecsact/runtime/serialize.hh

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

49+
/**
50+
* Serializes an ecsact_component when the type is unknown.
51+
* @returns serialized action bytes
52+
*/
4953
inline std::vector<std::byte> serialize(const ecsact_component& component) {
5054
std::vector<std::byte> out_component;
5155
out_component.resize(ecsact_serialize_component_size(component.component_id));
@@ -58,6 +62,10 @@ inline std::vector<std::byte> serialize(const ecsact_component& component) {
5862
return out_component;
5963
}
6064

65+
/**
66+
* Serializes an ecsact_action when the type is unknown.
67+
* @returns serialized component bytes
68+
*/
6169
inline std::vector<std::byte> serialize(const ecsact_action& action) {
6270
std::vector<std::byte> out_action;
6371
out_action.resize(ecsact_serialize_action_size(action.action_id));
@@ -140,40 +148,42 @@ int deserialize(
140148
return read_amount;
141149
}
142150

143-
// NOTE: Document functions
144-
inline ecsact_action deserialize(
151+
/**
152+
* Deserializes an ecsact_component when the type is unknown.
153+
* @returns an ecsact_action
154+
*/
155+
inline std::vector<std::byte> deserialize(
145156
const ecsact_action_id& id,
146157
std::vector<std::byte>& serialized_action
147158
) {
148-
auto action = ecsact_action{};
159+
std::vector<std::byte> action_data;
160+
action_data.resize(serialized_action.size());
149161

150162
ecsact_deserialize_action(
151163
id,
152164
reinterpret_cast<uint8_t*>(serialized_action.data()),
153-
&action
165+
action_data.data()
154166
);
155-
return action;
167+
return action_data;
156168
}
157169

158-
inline ecsact_component deserialize(
170+
/**
171+
* Deserializes an ecsact_component when the type is unknown.
172+
* @returns an ecsact_component_id
173+
*/
174+
inline std::vector<std::byte> deserialize(
159175
const ecsact_component_id& id,
160176
std::vector<std::byte>& serialized_component
161177
) {
162-
auto component = ecsact_component{};
163-
164-
std::vector<uint8_t> component_data;
178+
std::vector<std::byte> component_data;
165179
component_data.resize(serialized_component.size());
166180

167181
ecsact_deserialize_component(
168182
id,
169183
reinterpret_cast<uint8_t*>(serialized_component.data()),
170184
component_data.data()
171185
);
172-
173-
component.component_id = id;
174-
component.component_data = component_data.data();
175-
176-
return component;
186+
return component_data;
177187
}
178188

179189
} // 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/detail/c_execution_options",
2324
"//reference/async_reference/entity_manager",
2425
"//reference/async_reference/tick_manager",
2526
"//reference/async_reference/util",

reference/async_reference/async_reference.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,21 @@ void async_reference::execute_systems() {
160160

161161
auto collector = exec_callbacks.get_collector();
162162

163-
std::unique_ptr<ecsact_execution_options> options = nullptr;
163+
detail::c_execution_options c_exec_options;
164164

165165
if(cpp_options) {
166-
options = std::make_unique<ecsact_execution_options>(
167-
util::cpp_to_c_execution_options(*cpp_options, *registry_id)
166+
util::cpp_to_c_execution_options(
167+
c_exec_options,
168+
*cpp_options,
169+
*registry_id
168170
);
169171
}
170-
172+
auto options = c_exec_options.c();
171173
entity_manager.process_entities(async_callbacks, *registry_id);
172174

173175
auto exec_lk = exec_callbacks.lock();
174176
auto systems_error =
175-
ecsact_execute_systems(*registry_id, 1, options.get(), collector);
177+
ecsact_execute_systems(*registry_id, 1, &options, collector);
176178
exec_lk.unlock();
177179

178180
auto end = clock::now();
@@ -227,7 +229,3 @@ ecsact_async_request_id async_reference::next_request_id() {
227229
_last_request_id.fetch_add(1, std::memory_order_relaxed)
228230
);
229231
}
230-
231-
ecsact_async_request_id async_reference::convert_request_id(int32_t id) {
232-
return static_cast<ecsact_async_request_id>(id);
233-
}

reference/async_reference/async_reference.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ecsact/runtime/core.hh"
1212
#include "ecsact/runtime/async.h"
1313

14+
#include "reference/async_reference/detail/c_execution_options/c_execution_options.hh"
1415
#include "reference/async_reference/util/types.hh"
1516
#include "reference/async_reference/util/util.hh"
1617
#include "reference/async_reference/tick_manager/tick_manager.hh"
@@ -55,5 +56,4 @@ private:
5556
std::chrono::milliseconds tick_rate = {};
5657

5758
ecsact_async_request_id next_request_id();
58-
ecsact_async_request_id convert_request_id(int32_t id);
5959
};

reference/async_reference/callbacks/execution_callbacks.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void execution_callbacks::init_callback(
103103
info.event = event;
104104
info.entity_id = entity_id;
105105
info.component_id = component_id;
106-
std::unique_lock lk(self->execution_m);
107106
self->init_callbacks_info.push_back(info);
108107
}
109108

@@ -120,7 +119,6 @@ void execution_callbacks::update_callback(
120119
info.event = event;
121120
info.entity_id = entity_id;
122121
info.component_id = component_id;
123-
std::unique_lock lk(self->execution_m);
124122
self->update_callbacks_info.push_back(info);
125123
}
126124

@@ -133,16 +131,13 @@ void execution_callbacks::remove_callback(
133131
) {
134132
auto self = static_cast<execution_callbacks*>(callback_user_data);
135133

136-
std::unique_lock lk(self->execution_m);
137134
std::erase_if(self->init_callbacks_info, [&](auto& init_cb_info) {
138135
return init_cb_info.component_id == component_id;
139136
});
140-
lk.unlock();
141137

142138
auto info = types::callback_info{};
143139
info.event = event;
144140
info.entity_id = entity_id;
145141
info.component_id = component_id;
146-
lk.lock();
147142
self->remove_callbacks_info.push_back(info);
148143
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load("//bazel:copts.bzl", "copts")
3+
4+
cc_library(
5+
name = "c_execution_options",
6+
srcs = ["c_execution_options.cc"],
7+
hdrs = ["c_execution_options.hh"],
8+
copts = copts,
9+
local_defines = [
10+
"ECSACT_CORE_API=",
11+
],
12+
visibility = ["//visibility:public"],
13+
deps = [
14+
"//:async",
15+
"//:core",
16+
],
17+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "c_execution_options.hh"
2+
3+
ecsact_execution_options detail::c_execution_options::c() {
4+
for(auto& action_info : actions_info) {
5+
ecsact_action action;
6+
action.action_data = action_info.data.data();
7+
action.action_id = action_info.id;
8+
9+
actions.push_back(action);
10+
}
11+
12+
for(auto& add_info : adds_info) {
13+
auto component = ecsact_component{};
14+
15+
component.component_id = add_info.id;
16+
component.component_data = add_info.data.data();
17+
18+
adds.push_back(component);
19+
}
20+
21+
for(auto& update_info : updates_info) {
22+
auto component = ecsact_component{};
23+
24+
component.component_id = update_info.id;
25+
component.component_data = update_info.data.data();
26+
27+
updates.push_back(component);
28+
}
29+
30+
options.actions = actions.data();
31+
options.actions_length = actions.size();
32+
33+
options.add_components = adds.data();
34+
options.add_components_entities = adds_entities.data();
35+
options.add_components_length = adds.size();
36+
37+
options.update_components = updates.data();
38+
options.update_components_entities = updates_entities.data();
39+
options.update_components_length = updates.size();
40+
41+
options.remove_components = remove_ids.data();
42+
options.remove_components_entities = removes_entities.data();
43+
options.remove_components_length = remove_ids.size();
44+
45+
return options;
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <optional>
5+
6+
#include "ecsact/runtime/core.h"
7+
#include "ecsact/runtime/async.h"
8+
9+
namespace detail {
10+
11+
template<typename T>
12+
struct data_info {
13+
T id;
14+
15+
std::vector<std::byte> data;
16+
};
17+
18+
struct c_execution_options {
19+
/**
20+
* Holds the data lifetime used to reconstruct components and actions
21+
* Do NOT use this, it's an implementation detail
22+
*/
23+
std::vector<data_info<ecsact_action_id>> actions_info;
24+
std::vector<data_info<ecsact_component_id>> adds_info;
25+
std::vector<ecsact_entity_id> adds_entities;
26+
std::vector<data_info<ecsact_component_id>> updates_info;
27+
std::vector<ecsact_entity_id> updates_entities;
28+
std::vector<ecsact_component_id> remove_ids;
29+
std::vector<ecsact_entity_id> removes_entities;
30+
31+
ecsact_execution_options c();
32+
33+
private:
34+
ecsact_execution_options options;
35+
36+
std::vector<ecsact_action> actions;
37+
std::vector<ecsact_component> adds;
38+
std::vector<ecsact_component> updates;
39+
};
40+
} // namespace detail

0 commit comments

Comments
 (0)