Skip to content

Commit

Permalink
Don't serialize base DontInherit components to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 31, 2024
1 parent be43cc2 commit 881cf27
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 13 deletions.
20 changes: 14 additions & 6 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43859,7 +43859,7 @@ bool flecs_json_serialize_table_tags(
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (!(idr->flags & EcsIdOnInstantiateInherit)) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}
Expand Down Expand Up @@ -43926,7 +43926,7 @@ bool flecs_json_serialize_table_pairs(
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (!(idr->flags & EcsIdOnInstantiateInherit)) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}
Expand Down Expand Up @@ -43999,6 +43999,7 @@ static
int flecs_json_serialize_table_components(
const ecs_world_t *world,
ecs_table_t *table,
const ecs_table_t *src_table,
ecs_strbuf_t *buf,
ecs_json_value_ser_ctx_t *values_ctx,
const ecs_iter_to_json_desc_t *desc,
Expand All @@ -44019,15 +44020,22 @@ int flecs_json_serialize_table_components(
}

void *ptr;
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}

const ecs_type_info_t *ti;
int32_t column_index = table->column_map ? table->column_map[i] : -1;
if (column_index != -1) {
ecs_column_t *column = &table->data.columns[column_index];
ti = column->ti;
ptr = ECS_ELEM(column->data, ti->size, row);
} else {
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
if (!(idr->flags & EcsIdIsSparse)) {
continue;
}
Expand Down Expand Up @@ -44131,7 +44139,7 @@ bool flecs_json_serialize_table_inherited_type(

int32_t component_count = 0;
flecs_json_serialize_table_components(
world, base_table, buf, NULL, desc,
world, base_table, table, buf, NULL, desc,
ECS_RECORD_TO_ROW(base_record->row), &component_count);

if (desc->serialize_type_info) {
Expand Down Expand Up @@ -44263,7 +44271,7 @@ int flecs_json_serialize_iter_result_table(

component_count = 0; /* Each row has the same number of components */
if (flecs_json_serialize_table_components(
world, table, buf, values_ctx, desc, i, &component_count))
world, table, NULL, buf, values_ctx, desc, i, &component_count))
{
result = -1;
break;
Expand Down
20 changes: 14 additions & 6 deletions src/addons/json/serialize_iter_result_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool flecs_json_serialize_table_tags(
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (!(idr->flags & EcsIdOnInstantiateInherit)) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ bool flecs_json_serialize_table_pairs(
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (!(idr->flags & EcsIdOnInstantiateInherit)) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}
Expand Down Expand Up @@ -241,6 +241,7 @@ static
int flecs_json_serialize_table_components(
const ecs_world_t *world,
ecs_table_t *table,
const ecs_table_t *src_table,
ecs_strbuf_t *buf,
ecs_json_value_ser_ctx_t *values_ctx,
const ecs_iter_to_json_desc_t *desc,
Expand All @@ -261,15 +262,22 @@ int flecs_json_serialize_table_components(
}

void *ptr;
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;

if (src_table) {
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}

const ecs_type_info_t *ti;
int32_t column_index = table->column_map ? table->column_map[i] : -1;
if (column_index != -1) {
ecs_column_t *column = &table->data.columns[column_index];
ti = column->ti;
ptr = ECS_ELEM(column->data, ti->size, row);
} else {
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
if (!(idr->flags & EcsIdIsSparse)) {
continue;
}
Expand Down Expand Up @@ -373,7 +381,7 @@ bool flecs_json_serialize_table_inherited_type(

int32_t component_count = 0;
flecs_json_serialize_table_components(
world, base_table, buf, NULL, desc,
world, base_table, table, buf, NULL, desc,
ECS_RECORD_TO_ROW(base_record->row), &component_count);

if (desc->serialize_type_info) {
Expand Down Expand Up @@ -505,7 +513,7 @@ int flecs_json_serialize_iter_result_table(

component_count = 0; /* Each row has the same number of components */
if (flecs_json_serialize_table_components(
world, table, buf, values_ctx, desc, i, &component_count))
world, table, NULL, buf, values_ctx, desc, i, &component_count))
{
result = -1;
break;
Expand Down
3 changes: 3 additions & 0 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@
"serialize_w_name_2_tags",
"serialize_w_name_1_pair",
"serialize_w_base",
"serialize_w_base_dont_inherit_tag",
"serialize_w_base_dont_inherit_component",
"serialize_w_base_dont_inherit_pair",
"serialize_w_2_base",
"serialize_component_w_base",
"serialize_component_w_base_no_reflection_data",
Expand Down
79 changes: 79 additions & 0 deletions test/meta/src/SerializeEntityToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,85 @@ void SerializeEntityToJson_serialize_w_base(void) {
ecs_fini(world);
}

void SerializeEntityToJson_serialize_w_base_dont_inherit_tag(void) {
ecs_world_t *world = ecs_init();

ECS_ENTITY(world, TagA, (OnInstantiate, Inherit));
ECS_ENTITY(world, TagB, (OnInstantiate, DontInherit));

ecs_entity_t base = ecs_entity(world, { .name = "Base" });
ecs_add(world, base, TagA);
ecs_add(world, base, TagB);

ecs_entity_t e = ecs_entity(world, { .name = "Foo" });
ecs_add_pair(world, e, EcsIsA, base);

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\":\"Foo\", \"pairs\":{\"IsA\":\"Base\"},\"inherited\":{\"Base\":{\"tags\":[\"TagA\"]}}}");

ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_w_base_dont_inherit_component(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);
ECS_COMPONENT(world, Velocity);

ecs_add_pair(world, ecs_id(Position), EcsOnInstantiate, EcsInherit);
ecs_add_pair(world, ecs_id(Velocity), EcsOnInstantiate, EcsDontInherit);

ecs_entity_t base = ecs_entity(world, { .name = "Base" });
ecs_add(world, base, Position);
ecs_add(world, base, Velocity);

ecs_entity_t e = ecs_entity(world, { .name = "Foo" });
ecs_add_pair(world, e, EcsIsA, base);

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\":\"Foo\", \"pairs\":{\"IsA\":\"Base\"},\"inherited\":{\"Base\":{\"components\":{\"Position\":null}}}}");

ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_w_base_dont_inherit_pair(void) {
ecs_world_t *world = ecs_init();

ECS_ENTITY(world, RelA, (OnInstantiate, Inherit));
ECS_ENTITY(world, RelB, (OnInstantiate, DontInherit));
ECS_TAG(world, Tgt);

ecs_entity_t base = ecs_entity(world, { .name = "Base" });
ecs_add_pair(world, base, RelA, Tgt);
ecs_add_pair(world, base, RelB, Tgt);

ecs_entity_t e = ecs_entity(world, { .name = "Foo" });
ecs_add_pair(world, e, EcsIsA, base);

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\":\"Foo\", \"pairs\":{\"IsA\":\"Base\"},\"inherited\":{\"Base\":{\"pairs\":{\"RelA\":\"Tgt\"}}}}");

ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_w_2_base(void) {
ecs_world_t *world = ecs_init();

Expand Down
17 changes: 16 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ void SerializeEntityToJson_serialize_w_name_1_tag(void);
void SerializeEntityToJson_serialize_w_name_2_tags(void);
void SerializeEntityToJson_serialize_w_name_1_pair(void);
void SerializeEntityToJson_serialize_w_base(void);
void SerializeEntityToJson_serialize_w_base_dont_inherit_tag(void);
void SerializeEntityToJson_serialize_w_base_dont_inherit_component(void);
void SerializeEntityToJson_serialize_w_base_dont_inherit_pair(void);
void SerializeEntityToJson_serialize_w_2_base(void);
void SerializeEntityToJson_serialize_component_w_base(void);
void SerializeEntityToJson_serialize_component_w_base_no_reflection_data(void);
Expand Down Expand Up @@ -3431,6 +3434,18 @@ bake_test_case SerializeEntityToJson_testcases[] = {
"serialize_w_base",
SerializeEntityToJson_serialize_w_base
},
{
"serialize_w_base_dont_inherit_tag",
SerializeEntityToJson_serialize_w_base_dont_inherit_tag
},
{
"serialize_w_base_dont_inherit_component",
SerializeEntityToJson_serialize_w_base_dont_inherit_component
},
{
"serialize_w_base_dont_inherit_pair",
SerializeEntityToJson_serialize_w_base_dont_inherit_pair
},
{
"serialize_w_2_base",
SerializeEntityToJson_serialize_w_2_base
Expand Down Expand Up @@ -4935,7 +4950,7 @@ static bake_test_suite suites[] = {
"SerializeEntityToJson",
NULL,
NULL,
69,
72,
SerializeEntityToJson_testcases
},
{
Expand Down

0 comments on commit 881cf27

Please sign in to comment.