Skip to content

Commit 9162169

Browse files
authored
feat: implement new clone registry method (#163)
1 parent 18f70e2 commit 9162169

File tree

15 files changed

+339
-209
lines changed

15 files changed

+339
-209
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ ecsact_build_recipe(
157157
# core
158158
"ecsact_execute_systems",
159159
"ecsact_create_registry",
160+
"ecsact_clone_registry",
160161
"ecsact_destroy_registry",
161162
"ecsact_clear_registry",
162163
"ecsact_create_entity",

MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ module(
77
bazel_dep(name = "rules_cc", version = "0.0.17")
88
bazel_dep(name = "bazel_skylib", version = "1.6.1")
99
bazel_dep(name = "rules_ecsact", version = "0.5.8")
10-
bazel_dep(name = "ecsact_runtime", version = "0.7.0")
10+
bazel_dep(name = "ecsact_runtime", version = "0.7.1")
1111
bazel_dep(name = "ecsact_lang_cpp", version = "0.4.10")
1212
bazel_dep(name = "boost.mp11", version = "1.83.0.bzl.1")
1313
bazel_dep(name = "entt", version = "3.12.2")
1414
bazel_dep(name = "ecsact_codegen", version = "0.4.3")
15-
bazel_dep(name = "ecsact_cli", version = "0.3.19")
15+
bazel_dep(name = "ecsact_cli", version = "0.3.22")
1616
bazel_dep(name = "xxhash", version = "0.8.2")
1717
bazel_dep(name = "googletest", version = "1.14.0.bcr.1")
1818
bazel_dep(name = "boost.dll", version = "1.83.0.bzl.2")
1919
bazel_dep(name = "boost.process", version = "1.83.0.bzl.2")
2020
bazel_dep(name = "tracy", version = "0.11.1.edr.2")
21+
bazel_dep(name = "docopt.cpp", version = "0.6.2")
2122

2223
bazel_dep(name = "toolchains_llvm", version = "1.2.0", dev_dependency = True)
2324
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
25+
2426
git_override(
2527
module_name = "hedron_compile_commands",
2628
commit = "204aa593e002cbd177d30f11f54cff3559110bb9",

bazel/common.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ common --registry=https://raw.githubusercontent.com/ecsact-dev/bazel_registry/ma
44
common --registry=https://raw.githubusercontent.com/zaucy/bazel-central-registry/add-curl-config2 # temporary
55
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main
66
common --registry=https://bcr.bazel.build
7+
common --@docopt.cpp//:use_boost_regex
78
build --enable_platform_specific_config
89
build --incompatible_enable_cc_toolchain_resolution
910
build --incompatible_strict_action_env

ecsact/entt/registry_util.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ inline auto create_registry()
3535

3636
auto ecsact_init_registry_storage(::entt::registry& registry) -> void;
3737

38+
auto copy_components( //
39+
const ::entt::registry& src,
40+
::entt::registry& dst
41+
) -> void;
42+
3843
} // namespace ecsact::entt

rt_entt_codegen/core/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ _CORE_CODEGEN_METHODS = {
1515
"//rt_entt_codegen/shared:parallel",
1616
"//rt_entt_codegen/shared:system_variant",
1717
],
18-
"create_registry": [],
1918
"entity_matches": [
2019
"//rt_entt_codegen/shared:sorting",
2120
],
@@ -52,6 +51,7 @@ _CORE_CODEGEN_METHODS = {
5251
],
5352
"system_notify": ["//rt_entt_codegen/shared:system_util"],
5453
"update_beforechange": [],
54+
"copy_components": [],
5555
}
5656

5757
[cc_library(
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "core.hh"
2+
3+
#include "ecsact/lang-support/lang-cc.hh"
4+
#include "ecsact/cpp_codegen_plugin_util.hh"
5+
#include "rt_entt_codegen/shared/util.hh"
6+
7+
auto ecsact::rt_entt_codegen::core::print_copy_components(
8+
ecsact::codegen_plugin_context& ctx,
9+
const ecsact::rt_entt_codegen::ecsact_entt_details& details
10+
) -> void {
11+
using ecsact::cc_lang_support::cpp_identifier;
12+
using ecsact::cpp_codegen_plugin_util::block;
13+
using ecsact::meta::decl_full_name;
14+
using ecsact::rt_entt_codegen::util::method_printer;
15+
16+
auto printer = //
17+
method_printer{ctx, "ecsact::entt::copy_components"}
18+
.parameter("const ::entt::registry&", "src")
19+
.parameter("::entt::registry&", "dst")
20+
.return_type("void");
21+
22+
for(auto comp_id : details.all_components) {
23+
const auto cpp_comp_name = cpp_identifier(decl_full_name(comp_id));
24+
const auto is_tag = ecsact::meta::get_field_ids(comp_id).empty();
25+
26+
if(is_tag) {
27+
block(
28+
ctx,
29+
std::format("for(auto entity : src.view<{}>())", cpp_comp_name),
30+
[&] { ctx.writef("dst.emplace<{}>(entity);\n", cpp_comp_name); }
31+
);
32+
} else {
33+
block(
34+
ctx,
35+
std::format(
36+
"for(auto&& [entity, comp] : src.view<{}>().each())",
37+
cpp_comp_name
38+
),
39+
[&] { ctx.writef("dst.emplace<{}>(entity, comp);\n", cpp_comp_name); }
40+
);
41+
}
42+
}
43+
44+
for(auto sys_id : details.all_systems) {
45+
const auto system_name = cpp_identifier(decl_full_name(sys_id));
46+
const auto pending_lazy_exec_struct = std::format(
47+
"::ecsact::entt::detail::pending_lazy_execution<::{}>",
48+
system_name
49+
);
50+
block(
51+
ctx,
52+
std::format(
53+
"for(auto entity : src.view<{}>())",
54+
pending_lazy_exec_struct
55+
),
56+
[&] {
57+
ctx.writef("dst.emplace<{}>(entity);\n", pending_lazy_exec_struct);
58+
}
59+
);
60+
}
61+
}

rt_entt_codegen/core/core.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ auto print_execution_options( //
2121
const ecsact_entt_details& details
2222
) -> void;
2323

24-
auto print_create_registry( //
25-
codegen_plugin_context& ctx,
26-
const ecsact_entt_details& details
27-
) -> void;
28-
2924
auto print_init_registry_storage( //
3025
ecsact::codegen_plugin_context& ctx,
3126
const ecsact::rt_entt_codegen::ecsact_entt_details& details
@@ -96,4 +91,9 @@ auto print_apply_streaming_data(
9691
const ecsact_entt_details& details
9792
) -> void;
9893

94+
auto print_copy_components(
95+
codegen_plugin_context& ctx,
96+
const ecsact_entt_details& details
97+
) -> void;
98+
9999
} // namespace ecsact::rt_entt_codegen::core

rt_entt_codegen/core/create_registry.cc

Lines changed: 0 additions & 23 deletions
This file was deleted.

rt_entt_codegen/rt_entt_codegen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void ecsact_codegen_plugin(
350350
core::print_check_error_template_specializations(ctx, details);
351351
core::print_execute_system_like_template_specializations(ctx, details);
352352
core::print_init_registry_storage(ctx, details);
353-
core::print_create_registry(ctx, details);
353+
core::print_copy_components(ctx, details);
354354
core::print_apply_streaming_data(ctx, details);
355355
core::print_trigger_ecsact_events_minimal(ctx, details);
356356
core::print_trigger_ecsact_events_all(ctx, details);

runtime/ecsact_rt_entt_core.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ void ecsact_destroy_registry(ecsact_registry_id reg_id) {
1717
assert(reg.template storage<entt::entity>().in_use() == 0);
1818
}
1919

20+
auto ecsact_create_registry(const char* registry_name) -> ecsact_registry_id {
21+
auto&& [registry_id, reg] = ecsact::entt::create_registry();
22+
ecsact::entt::ecsact_init_registry_storage(reg);
23+
return registry_id;
24+
}
25+
26+
auto ecsact_clone_registry( //
27+
ecsact_registry_id reg_id,
28+
const char* name
29+
) -> ecsact_registry_id {
30+
auto& reg = ecsact::entt::get_registry(reg_id);
31+
auto cloned_reg_id = ecsact_create_registry(name);
32+
auto& cloned_reg = ecsact::entt::get_registry(cloned_reg_id);
33+
for(auto&& [entity_id] : reg.template storage<entt::entity>().each()) {
34+
[[maybe_unused]] auto cloned_entity_id = cloned_reg.create(entity_id);
35+
assert(cloned_entity_id == entity_id);
36+
}
37+
38+
ecsact::entt::copy_components(reg, cloned_reg);
39+
40+
return cloned_reg_id;
41+
}
42+
2043
void ecsact_clear_registry(ecsact_registry_id reg_id) {
2144
auto& reg = ecsact::entt::get_registry(reg_id);
2245
reg = {};

0 commit comments

Comments
 (0)