Skip to content

Commit 842fd50

Browse files
committed
Fixed tests, ready for review
1 parent b2ebe17 commit 842fd50

File tree

5 files changed

+241
-98
lines changed

5 files changed

+241
-98
lines changed

reference/async_reference/callbacks/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ cc_library(
88
copts = copts,
99
local_defines = [
1010
"ECSACT_CORE_API=",
11+
"ECSACT_SERIALIZE_API=",
1112
],
1213
visibility = ["//visibility:public"],
1314
deps = [
1415
"//:core",
1516
"//reference/async_reference/util:types",
17+
"//reference/serialize_reference",
1618
],
1719
)
1820

reference/async_reference/callbacks/execution_callbacks.cc

Lines changed: 104 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,67 @@ void execution_callbacks::invoke(
4141

4242
lk.unlock();
4343

44-
for(auto& component_info : init_callbacks) {
45-
const void* component_data = ecsact_get_component(
46-
registry_id,
47-
component_info.entity_id,
48-
component_info.component_id
49-
);
50-
51-
execution_events->init_callback(
52-
component_info.event,
53-
component_info.entity_id,
54-
component_info.component_id,
55-
component_data,
56-
execution_events->init_callback_user_data
57-
);
44+
if(execution_events->init_callback != nullptr) {
45+
for(auto& component_info : init_callbacks) {
46+
const void* component_data = ecsact_get_component(
47+
registry_id,
48+
component_info.entity_id,
49+
component_info.component_id
50+
);
51+
52+
execution_events->init_callback(
53+
component_info.event,
54+
component_info.entity_id,
55+
component_info.component_id,
56+
component_data,
57+
execution_events->init_callback_user_data
58+
);
59+
}
5860
}
5961

60-
for(auto& component_info : update_callbacks) {
61-
const void* component_data = ecsact_get_component(
62-
registry_id,
63-
component_info.entity_id,
64-
component_info.component_id
65-
);
66-
67-
execution_events->update_callback(
68-
component_info.event,
69-
component_info.entity_id,
70-
component_info.component_id,
71-
component_data,
72-
execution_events->update_callback_user_data
73-
);
62+
if(execution_events->update_callback != nullptr) {
63+
for(auto& component_info : update_callbacks) {
64+
const void* component_data = ecsact_get_component(
65+
registry_id,
66+
component_info.entity_id,
67+
component_info.component_id
68+
);
69+
70+
execution_events->update_callback(
71+
component_info.event,
72+
component_info.entity_id,
73+
component_info.component_id,
74+
component_data,
75+
execution_events->update_callback_user_data
76+
);
77+
}
7478
}
7579

76-
for(auto& component_info : remove_callbacks) {
77-
const void* component_data = ecsact_get_component(
78-
registry_id,
79-
component_info.entity_id,
80-
component_info.component_id
81-
);
82-
83-
execution_events->remove_callback(
84-
component_info.event,
85-
component_info.entity_id,
86-
component_info.component_id,
87-
component_data,
88-
execution_events->remove_callback_user_data
89-
);
80+
if(execution_events->remove_callback != nullptr) {
81+
for(auto& component_info : remove_callbacks) {
82+
for(auto itr = removed_execute_components.begin();
83+
itr != removed_execute_components.end();) {
84+
auto& execute_component = *itr;
85+
86+
if(execute_component.entity_id != component_info.entity_id && execute_component._id != component_info.component_id) {
87+
++itr;
88+
continue;
89+
}
90+
91+
auto deserialized_component =
92+
ecsact::deserialize(execute_component._id, execute_component.data);
93+
94+
execution_events->remove_callback(
95+
component_info.event,
96+
component_info.entity_id,
97+
component_info.component_id,
98+
deserialized_component.data(),
99+
execution_events->remove_callback_user_data
100+
);
101+
102+
itr = removed_execute_components.erase(itr);
103+
}
104+
}
90105
}
91106
}
92107

@@ -98,6 +113,29 @@ void execution_callbacks::init_callback(
98113
void* callback_user_data
99114
) {
100115
auto self = static_cast<execution_callbacks*>(callback_user_data);
116+
117+
auto result =
118+
std::erase_if(self->remove_callbacks_info, [&](auto& remove_cb_info) {
119+
return remove_cb_info.component_id == component_id &&
120+
remove_cb_info.entity_id == entity_id;
121+
});
122+
123+
std::erase_if(self->update_callbacks_info, [&](auto& update_cb_info) {
124+
return update_cb_info.component_id == component_id &&
125+
update_cb_info.entity_id == entity_id;
126+
});
127+
128+
if(result > 0) {
129+
for(int i = 0; i < self->removed_execute_components.size(); ++i) {
130+
auto& execute_component = self->removed_execute_components[i];
131+
if(execute_component._id == component_id) {
132+
self->removed_execute_components.erase(
133+
self->removed_execute_components.begin() + i
134+
);
135+
}
136+
}
137+
}
138+
101139
auto info = types::callback_info{};
102140

103141
info.event = event;
@@ -114,6 +152,7 @@ void execution_callbacks::update_callback(
114152
void* callback_user_data
115153
) {
116154
auto self = static_cast<execution_callbacks*>(callback_user_data);
155+
117156
auto info = types::callback_info{};
118157

119158
info.event = event;
@@ -132,12 +171,34 @@ void execution_callbacks::remove_callback(
132171
auto self = static_cast<execution_callbacks*>(callback_user_data);
133172

134173
std::erase_if(self->init_callbacks_info, [&](auto& init_cb_info) {
135-
return init_cb_info.component_id == component_id;
174+
return init_cb_info.component_id == component_id &&
175+
init_cb_info.entity_id == entity_id;
176+
});
177+
178+
// Maybe I can store data from here?
179+
std::erase_if(self->update_callbacks_info, [&](auto& update_cb_info) {
180+
return update_cb_info.component_id == component_id &&
181+
update_cb_info.entity_id == entity_id;
136182
});
137183

138184
auto info = types::callback_info{};
139185
info.event = event;
140186
info.entity_id = entity_id;
141187
info.component_id = component_id;
188+
189+
auto component_to_serialize = ecsact_component{
190+
.component_id = component_id,
191+
.component_data = component_data};
192+
193+
auto serialized_component = ecsact::serialize(component_to_serialize);
194+
195+
self->removed_execute_components.push_back(types::cpp_execution_component{
196+
.entity_id = entity_id,
197+
._id = component_id,
198+
.data = serialized_component,
199+
});
200+
201+
// Same for remove, I could store data here
202+
// maybe...?
142203
self->remove_callbacks_info.push_back(info);
143204
}

reference/async_reference/callbacks/execution_callbacks.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
#include <vector>
44
#include <mutex>
5+
#include <map>
56

67
#include "ecsact/runtime/core.hh"
8+
#include "ecsact/runtime/serialize.hh"
79
#include "reference/async_reference/util/types.hh"
810

911
class execution_callbacks {
@@ -25,6 +27,8 @@ private:
2527
ecsact_execution_events_collector collector;
2628
std::mutex execution_m;
2729

30+
std::vector<types::cpp_execution_component> removed_execute_components;
31+
2832
std::vector<types::callback_info> init_callbacks_info;
2933
std::vector<types::callback_info> update_callbacks_info;
3034
std::vector<types::callback_info> remove_callbacks_info;

0 commit comments

Comments
 (0)