MCPcopy Create free account
hub / github.com/crownengine/crown / modify_unit_components

Function modify_unit_components

src/resource/unit_compiler.cpp:852–944  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

850 }
851
852 s32 modify_unit_components(Unit *unit, const char *unit_json, CompileOptions &opts)
853 {
854 TempAllocator4096 ta;
855 JsonObject obj(ta);
856 RETURN_IF_ERROR(sjson::parse(obj, unit_json));
857
858 if (json_object::has(obj, "components")) {
859 JsonArray components(ta);
860 RETURN_IF_ERROR(sjson::parse_array(components, obj["components"]));
861
862 // Add components.
863 for (u32 cc = 0; cc < array::size(components); ++cc) {
864 FlatJsonObject flat_comp(default_allocator());
865 DynamicString empty(default_allocator());
866 to_flat(flat_comp, components[cc], empty);
867
868 array::push_back(unit->_merged_components, components[cc]);
869 vector::push_back(unit->_flattened_components, flat_comp);
870 }
871 }
872
873 if (json_object::has(obj, "deleted_components")) {
874 JsonObject deleted_components(ta);
875 RETURN_IF_ERROR(sjson::parse_object(deleted_components, obj["deleted_components"]));
876
877 // Delete components.
878 auto cur = json_object::begin(deleted_components);
879 auto end = json_object::end(deleted_components);
880 for (; cur != end; ++cur) {
881 JSON_OBJECT_SKIP_HOLE(deleted_components, cur);
882
883 auto key = cur->first;
884
885 // Extract GUID from key "#xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
886 char guid[37];
887 strncpy(guid, key.data() + 1, sizeof(guid) - 1);
888 guid[36] = '\0';
889 Guid component_id = guid::parse(guid);
890
891 u32 comp_idx = object_index(unit->_merged_components, component_id, opts);
892 if (comp_idx != UINT32_MAX) {
893 u32 comp_last = array::size(unit->_merged_components) - 1;
894 if (comp_idx != comp_last) {
895 unit->_merged_components[comp_idx] = unit->_merged_components[comp_last];
896 unit->_flattened_components[comp_idx] = unit->_flattened_components[comp_last];
897 }
898 array::pop_back(unit->_merged_components);
899 vector::pop_back(unit->_flattened_components);
900 } else {
901 char buf[GUID_BUF_LEN];
902 RETURN_IF_FALSE(UNIT_COMPILER, false
903 , opts
904 , "Deletion of unexisting component ID: %s"
905 , guid::to_string(buf, sizeof(buf), component_id)
906 );
907 }
908 }
909 }

Callers 1

parse_unit_internalFunction · 0.85

Calls 12

to_flatFunction · 0.85
strncpyFunction · 0.85
object_indexFunction · 0.85
parseFunction · 0.70
hasFunction · 0.50
parse_arrayFunction · 0.50
sizeFunction · 0.50
parse_objectFunction · 0.50
beginFunction · 0.50
endFunction · 0.50
to_stringFunction · 0.50
dataMethod · 0.45

Tested by

no test coverage detected