From eb07b1aba2de8f557ce63dda47fecc5cd4961ae7 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Fri, 18 Oct 2024 14:40:32 -0700 Subject: [PATCH] Fix JSON serialization for auto_override/toggle components Differential Revision: D64616203 Pull Request resolved: https://github.com/SanderMertens/flecs/pull/1407 --- distr/flecs.c | 17 +- src/addons/json/json.c | 12 ++ src/addons/json/serialize_iter_result_table.c | 5 +- test/meta/project.json | 11 +- test/meta/src/SerializeEntityToJson.c | 165 ++++++++++++++++++ test/meta/src/main.c | 37 +++- 6 files changed, 242 insertions(+), 5 deletions(-) diff --git a/distr/flecs.c b/distr/flecs.c index cfdd858798..398d8ce999 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -42273,6 +42273,18 @@ void flecs_json_id_member( ecs_id_t id, bool fullpath) { + ecs_id_t flags = id & ECS_ID_FLAGS_MASK; + + if (flags & ECS_AUTO_OVERRIDE) { + ecs_strbuf_appendlit(buf, "auto_override|"); + id &= ~ECS_AUTO_OVERRIDE; + } + + if (flags & ECS_TOGGLE) { + ecs_strbuf_appendlit(buf, "toggle|"); + id &= ~ECS_TOGGLE; + } + if (fullpath) { flecs_json_id_member_fullpath(buf, world, id); return; @@ -43822,8 +43834,11 @@ bool flecs_json_serialize_table_tags( } flecs_json_next(buf); - flecs_json_path_or_label(buf, world, id, + + ecs_strbuf_appendlit(buf, "\""); + flecs_json_id_member(buf, world, id, desc ? desc->serialize_full_paths : true); + ecs_strbuf_appendlit(buf, "\""); tag_count ++; } diff --git a/src/addons/json/json.c b/src/addons/json/json.c index f409d79929..e18bcd01fb 100644 --- a/src/addons/json/json.c +++ b/src/addons/json/json.c @@ -651,6 +651,18 @@ void flecs_json_id_member( ecs_id_t id, bool fullpath) { + ecs_id_t flags = id & ECS_ID_FLAGS_MASK; + + if (flags & ECS_AUTO_OVERRIDE) { + ecs_strbuf_appendlit(buf, "auto_override|"); + id &= ~ECS_AUTO_OVERRIDE; + } + + if (flags & ECS_TOGGLE) { + ecs_strbuf_appendlit(buf, "toggle|"); + id &= ~ECS_TOGGLE; + } + if (fullpath) { flecs_json_id_member_fullpath(buf, world, id); return; diff --git a/src/addons/json/serialize_iter_result_table.c b/src/addons/json/serialize_iter_result_table.c index af00c0e9dd..f514f62e2c 100644 --- a/src/addons/json/serialize_iter_result_table.c +++ b/src/addons/json/serialize_iter_result_table.c @@ -115,8 +115,11 @@ bool flecs_json_serialize_table_tags( } flecs_json_next(buf); - flecs_json_path_or_label(buf, world, id, + + ecs_strbuf_appendlit(buf, "\""); + flecs_json_id_member(buf, world, id, desc ? desc->serialize_full_paths : true); + ecs_strbuf_appendlit(buf, "\""); tag_count ++; } diff --git a/test/meta/project.json b/test/meta/project.json index 47871f8079..a1097134fa 100644 --- a/test/meta/project.json +++ b/test/meta/project.json @@ -136,7 +136,7 @@ "vector_of_struct_with_strings", "vector_of_arrays_of_strings", "vector_of_opaque" - ] + ] }, { "id": "StructTypes", "testcases": [ @@ -720,7 +720,14 @@ "serialize_sparse_mixed", "serialize_sparse_inherited", "serialize_sparse_inherited_pair", - "serialize_sparse_inherited_mixed" + "serialize_sparse_inherited_mixed", + "serialize_auto_override_w_inherited", + "serialize_auto_override", + "serialize_auto_override_pair", + "serialize_auto_override_fullpath", + "serialize_auto_override_pair_fullpath", + "serialize_toggle", + "serialize_toggle_pair" ] }, { "id": "SerializeIterToJson", diff --git a/test/meta/src/SerializeEntityToJson.c b/test/meta/src/SerializeEntityToJson.c index 76b923455d..6317755189 100644 --- a/test/meta/src/SerializeEntityToJson.c +++ b/test/meta/src/SerializeEntityToJson.c @@ -1778,3 +1778,168 @@ void SerializeEntityToJson_serialize_sparse_inherited_mixed(void) { ecs_fini(world); } + +void SerializeEntityToJson_serialize_auto_override_w_inherited(void) { + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, Position); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_auto_override(world, e, Position); + + ecs_entity_to_json_desc_t desc = { + .serialize_inherited = true + }; + + char *json = ecs_entity_to_json(world, e, &desc); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|Position\"]}"); + ecs_os_free(json); + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_auto_override(void) { + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, Position); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_auto_override(world, e, Position); + + char *json = ecs_entity_to_json(world, e, NULL); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|Position\"]}"); + ecs_os_free(json); + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_auto_override_pair(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Rel); + ECS_TAG(world, Tgt); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_auto_override_pair(world, e, Rel, Tgt); + + char *json = ecs_entity_to_json(world, e, NULL); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(Rel,Tgt)\"]}"); + ecs_os_free(json); + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_auto_override_fullpath(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Rel); + ECS_TAG(world, Tgt); + + ecs_entity_t tag = ecs_entity(world, { .name = "tags.foo"}); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_auto_override_id(world, e, tag); + + { + ecs_entity_to_json_desc_t desc = { + .serialize_full_paths = true + }; + + char *json = ecs_entity_to_json(world, e, &desc); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|tags.foo\"]}"); + ecs_os_free(json); + } + + { + ecs_entity_to_json_desc_t desc = { + .serialize_full_paths = false + }; + + char *json = ecs_entity_to_json(world, e, &desc); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|foo\"]}"); + ecs_os_free(json); + } + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_auto_override_pair_fullpath(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Rel); + ECS_TAG(world, Tgt); + + ecs_entity_t rel = ecs_entity(world, { .name = "tags.rel"}); + ecs_entity_t tgt = ecs_entity(world, { .name = "tags.tgt"}); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_auto_override_pair(world, e, rel, tgt); + + { + ecs_entity_to_json_desc_t desc = { + .serialize_full_paths = true + }; + + char *json = ecs_entity_to_json(world, e, &desc); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(tags.rel,tags.tgt)\"]}"); + ecs_os_free(json); + } + + { + ecs_entity_to_json_desc_t desc = { + .serialize_full_paths = false + }; + + char *json = ecs_entity_to_json(world, e, &desc); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(rel,tgt)\"]}"); + ecs_os_free(json); + } + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_toggle(void) { + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, Position); + + ecs_add_id(world, ecs_id(Position), EcsCanToggle); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_add(world, e, Position); + ecs_enable_component(world, e, Position, false); + + char *json = ecs_entity_to_json(world, e, NULL); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"toggle|Position\"], \"components\":{\"Position\":null}}"); + ecs_os_free(json); + + ecs_fini(world); +} + +void SerializeEntityToJson_serialize_toggle_pair(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Rel); + ECS_TAG(world, Tgt); + + ecs_add_id(world, Rel, EcsCanToggle); + + ecs_entity_t e = ecs_entity(world, { .name = "e"}); + ecs_add_pair(world, e, Rel, Tgt); + ecs_enable_pair(world, e, Rel, Tgt, false); + + char *json = ecs_entity_to_json(world, e, NULL); + test_assert(json != NULL); + test_json(json, "{\"name\":\"e\", \"tags\":[\"toggle|(Rel,Tgt)\"],\"pairs\":{\"Rel\":[\"Tgt\"]}}"); + ecs_os_free(json); + + ecs_fini(world); +} diff --git a/test/meta/src/main.c b/test/meta/src/main.c index b22d883cb8..2090fbc2fa 100644 --- a/test/meta/src/main.c +++ b/test/meta/src/main.c @@ -690,6 +690,13 @@ void SerializeEntityToJson_serialize_sparse_mixed(void); void SerializeEntityToJson_serialize_sparse_inherited(void); void SerializeEntityToJson_serialize_sparse_inherited_pair(void); void SerializeEntityToJson_serialize_sparse_inherited_mixed(void); +void SerializeEntityToJson_serialize_auto_override_w_inherited(void); +void SerializeEntityToJson_serialize_auto_override(void); +void SerializeEntityToJson_serialize_auto_override_pair(void); +void SerializeEntityToJson_serialize_auto_override_fullpath(void); +void SerializeEntityToJson_serialize_auto_override_pair_fullpath(void); +void SerializeEntityToJson_serialize_toggle(void); +void SerializeEntityToJson_serialize_toggle_pair(void); // Testsuite 'SerializeIterToJson' void SerializeIterToJson_serialize_1_comps_empty(void); @@ -3646,6 +3653,34 @@ bake_test_case SerializeEntityToJson_testcases[] = { { "serialize_sparse_inherited_mixed", SerializeEntityToJson_serialize_sparse_inherited_mixed + }, + { + "serialize_auto_override_w_inherited", + SerializeEntityToJson_serialize_auto_override_w_inherited + }, + { + "serialize_auto_override", + SerializeEntityToJson_serialize_auto_override + }, + { + "serialize_auto_override_pair", + SerializeEntityToJson_serialize_auto_override_pair + }, + { + "serialize_auto_override_fullpath", + SerializeEntityToJson_serialize_auto_override_fullpath + }, + { + "serialize_auto_override_pair_fullpath", + SerializeEntityToJson_serialize_auto_override_pair_fullpath + }, + { + "serialize_toggle", + SerializeEntityToJson_serialize_toggle + }, + { + "serialize_toggle_pair", + SerializeEntityToJson_serialize_toggle_pair } }; @@ -4895,7 +4930,7 @@ static bake_test_suite suites[] = { "SerializeEntityToJson", NULL, NULL, - 62, + 69, SerializeEntityToJson_testcases }, {