Skip to content

Commit ac95f7b

Browse files
authored
fix: execute systems from other packages (#44)
1 parent fbf0ddd commit ac95f7b

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

ecsact/entt/detail/meta_util.hh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,26 @@ void mp_for_each_available_action(Callback&& cb) {
2626

2727
mp_for_each<actions>([&]<typename A>(const A& a) { cb(a); });
2828
mp_for_each<dependencies>([&]<typename D>(const D& d) {
29-
mp_for_each_available_components<D>(cb);
29+
mp_for_each_available_action<D>(cb);
3030
});
3131
}
3232

33+
template<typename Package, typename Callback>
34+
void mp_for_each_available_system(Callback&& cb) {
35+
using boost::mp11::mp_for_each;
36+
using systems = typename Package::systems;
37+
using dependencies = typename Package::dependencies;
38+
39+
mp_for_each<systems>([&]<typename S>(const S& s) { cb(s); });
40+
mp_for_each<dependencies>([&]<typename D>(const D& d) {
41+
mp_for_each_available_system<D>(cb);
42+
});
43+
}
44+
45+
template<typename Package, typename Callback>
46+
void mp_for_each_available_system_like(Callback&& cb) {
47+
mp_for_each_available_system<Package>(cb);
48+
mp_for_each_available_action<Package>(cb);
49+
}
50+
3351
} // namespace ecsact::entt::detail

ecsact/entt/runtime.hh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,10 +961,16 @@ private:
961961
}
962962
}
963963

964-
void _execute_systems(registry_info& info, actions_span_t& actions) {
964+
template<typename TargetPackage>
965+
void _execute_package_systems(registry_info& info, actions_span_t& actions) {
965966
using boost::mp11::mp_for_each;
967+
using dependencies = typename TargetPackage::dependencies;
968+
969+
mp_for_each<dependencies>([&]<typename D>(const D&) {
970+
_execute_package_systems<D>(info, actions);
971+
});
966972

967-
mp_for_each<typename package::execution_order>(
973+
mp_for_each<typename TargetPackage::execution_order>(
968974
[&]<typename SystemPair>(SystemPair) {
969975
using boost::mp11::mp_first;
970976
using boost::mp11::mp_second;
@@ -976,6 +982,10 @@ private:
976982
);
977983
}
978984

985+
void _execute_systems(registry_info& info, actions_span_t& actions) {
986+
_execute_package_systems<package>(info, actions);
987+
}
988+
979989
template<typename C>
980990
requires(!std::is_empty_v<C>)
981991
void _pre_exec_add_component(

runtime/dynamic.template.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <boost/mp11.hpp>
22
#include "ecsact/runtime/dynamic.h"
33
#include "ecsact/entt/detail/system_execution_context.hh"
4+
#include "ecsact/entt/detail/meta_util.hh"
45

56
#include "common.template.hh"
67

@@ -12,15 +13,9 @@ using package = typename decltype(ecsact_entt_rt::runtime)::package;
1213

1314
template<typename Fn>
1415
static void cast_and_use_ctx(ecsact_system_execution_context* ctx, Fn&& fn) {
15-
using boost::mp11::mp_flatten;
16-
using boost::mp11::mp_for_each;
17-
using boost::mp11::mp_push_back;
18-
using boost::mp11::mp_transform;
19-
20-
using all_systems = mp_flatten<
21-
mp_push_back<typename package::actions, typename package::systems>>;
16+
using ecsact::entt::detail::mp_for_each_available_system_like;
2217

23-
mp_for_each<all_systems>([&]<typename S>(S) {
18+
mp_for_each_available_system_like<package>([&]<typename S>(S) {
2419
if(ecsact_id_cast<ecsact_system_like_id>(S::id) == ctx->system_id) {
2520
using boost::mp11::mp_size;
2621
using caps_info = ecsact::system_capabilities_info<S>;

runtime/test/imported_pkg.ecsact

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ component ImportedComponent {
44
i32 num;
55
}
66

7+
component SomeLocalComponent {
8+
i32 local_num;
9+
}
10+
11+
system ImportedSystem {
12+
readwrite SomeLocalComponent;
13+
}
14+

runtime/test/runtime_test.cc

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "gtest/gtest.h"
22

33
#include <array>
4+
#include <set>
5+
#include <typeindex>
46
#include <unordered_set>
57
#include <version>
68
#include <ranges>
@@ -56,6 +58,12 @@ void runtime_test::SimpleIncrementImportedComp::impl(context& ctx) {
5658
ctx.update(comp);
5759
}
5860

61+
void imported::test_pkg::ImportedSystem::impl(context& ctx) {
62+
auto comp = ctx.get<SomeLocalComponent>();
63+
comp.local_num += 1;
64+
ctx.update(comp);
65+
}
66+
5967
static std::atomic_bool AssocTestAction_ran = false;
6068

6169
void runtime_test::AssocTestAction::impl(context& ctx) {
@@ -742,22 +750,50 @@ TEST(Core, CreateAndDestroyEntity) {
742750

743751
TEST(Core, MultiPkgUpdate) {
744752
using imported::test_pkg::ImportedComponent;
753+
using imported::test_pkg::SomeLocalComponent;
745754

746-
ecsact_set_system_execution_impl(
755+
ASSERT_TRUE(ecsact_set_system_execution_impl(
747756
ecsact_id_cast<ecsact_system_like_id>(
748757
runtime_test::SimpleIncrementImportedComp::id
749758
),
750759
&runtime_test__SimpleIncrementImportedComp
751-
);
760+
));
761+
762+
ASSERT_TRUE(ecsact_set_system_execution_impl(
763+
ecsact_id_cast<ecsact_system_like_id>(imported::test_pkg::ImportedSystem::id
764+
),
765+
&imported__test_pkg__ImportedSystem
766+
));
752767

753768
auto reg = ecsact::core::registry("MultiPkgUpdate");
754769
auto test_entity = reg.create_entity();
755770
reg.add_component(test_entity, ImportedComponent{});
771+
reg.add_component(test_entity, SomeLocalComponent{});
756772

757773
for(int i = 0; 10 > i; ++i) {
758-
reg.execute_systems();
759-
auto c = reg.get_component<ImportedComponent>(test_entity);
760-
EXPECT_EQ(c.num, i + 1);
774+
auto event_happened = std::set<std::type_index>{};
775+
auto evc = ecsact::core::execution_events_collector<>{};
776+
evc.set_update_callback<ImportedComponent>([&](auto entity, auto comp) {
777+
event_happened.insert(typeid(ImportedComponent));
778+
EXPECT_EQ(comp.num, i + 1);
779+
});
780+
evc.set_update_callback<SomeLocalComponent>([&](auto entity, auto comp) {
781+
event_happened.insert(typeid(SomeLocalComponent));
782+
EXPECT_EQ(comp.local_num, i + 1);
783+
});
784+
785+
reg.execute_systems(1, evc);
786+
{
787+
auto c = reg.get_component<ImportedComponent>(test_entity);
788+
EXPECT_EQ(c.num, i + 1);
789+
EXPECT_TRUE(event_happened.contains(typeid(ImportedComponent)));
790+
}
791+
792+
{
793+
auto c = reg.get_component<SomeLocalComponent>(test_entity);
794+
EXPECT_EQ(c.local_num, i + 1);
795+
EXPECT_TRUE(event_happened.contains(typeid(SomeLocalComponent)));
796+
}
761797
}
762798
}
763799

0 commit comments

Comments
 (0)