|
| 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_hash_registry( |
| 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::hash_registry"} |
| 18 | + .parameter("const ::entt::registry&", "reg") |
| 19 | + .return_type("std::uint64_t"); |
| 20 | + |
| 21 | + ctx.writef("auto* state = XXH3_createState();\n"); |
| 22 | + ctx.writef("if(!state) {{ return 0; }}\n"); |
| 23 | + ctx.writef("XXH3_64bits_reset(state);\n"); |
| 24 | + |
| 25 | + // XXH3_64bits_update(state, buffer, count); |
| 26 | + |
| 27 | + block( |
| 28 | + ctx, |
| 29 | + "for(auto&& [entity] : reg.template storage<::entt::entity>()->each())", |
| 30 | + [&] { |
| 31 | + ctx.writef("XXH3_64bits_update(state, &entity, sizeof(entity));\n"); |
| 32 | + ctx.writef( |
| 33 | + "auto has_states = std::array<bool, {}>{{\n", |
| 34 | + details.all_components.size() + details.all_systems.size() |
| 35 | + ); |
| 36 | + for(auto comp_id : details.all_components) { |
| 37 | + const auto cpp_comp_name = cpp_identifier(decl_full_name(comp_id)); |
| 38 | + ctx.writef("\treg.all_of<{}>(entity),\n", cpp_comp_name); |
| 39 | + } |
| 40 | + |
| 41 | + for(auto sys_id : details.all_systems) { |
| 42 | + const auto system_name = cpp_identifier(decl_full_name(sys_id)); |
| 43 | + const auto pending_lazy_exec_struct = std::format( |
| 44 | + "::ecsact::entt::detail::pending_lazy_execution<::{}>", |
| 45 | + system_name |
| 46 | + ); |
| 47 | + ctx.writef("\treg.all_of<{}>(entity),\n", pending_lazy_exec_struct); |
| 48 | + } |
| 49 | + |
| 50 | + ctx.writef("\n}};\n"); |
| 51 | + ctx.writef( |
| 52 | + "XXH3_64bits_update(state, has_states.data(), sizeof(bool) * " |
| 53 | + "has_states.size());\n" |
| 54 | + ); |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + ctx.writef("\n"); |
| 59 | + |
| 60 | + for(auto comp_id : details.all_components) { |
| 61 | + const auto cpp_comp_name = cpp_identifier(decl_full_name(comp_id)); |
| 62 | + if(ecsact::meta::get_field_ids(comp_id).empty()) { |
| 63 | + // tags are covered in the `has_states` above already |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + block( |
| 68 | + ctx, |
| 69 | + std::format( |
| 70 | + "for(auto&& [entity, comp] : reg.view<{}>().each())", |
| 71 | + cpp_comp_name |
| 72 | + ), |
| 73 | + [&] { |
| 74 | + auto field_ids = ecsact::meta::get_field_ids(comp_id); |
| 75 | + for(auto field_id : field_ids) { |
| 76 | + auto field_name = ecsact::meta::field_name(comp_id, field_id); |
| 77 | + ctx.writef( |
| 78 | + "XXH3_64bits_update(state, &comp.{0}, " |
| 79 | + "sizeof(decltype(comp.{0})));\n", |
| 80 | + field_name |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + ); |
| 85 | + ctx.writef("\n"); |
| 86 | + } |
| 87 | + |
| 88 | + ctx.writef("auto result = XXH3_64bits_digest(state);\n"); |
| 89 | + ctx.writef("XXH3_freeState(state);\n"); |
| 90 | + ctx.writef("return result;\n"); |
| 91 | +} |
0 commit comments