diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-08 12:05:15 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-08 12:05:15 +0100 |
commit | e70bd100853827b9bafb21103f78ebaa74a2af20 (patch) | |
tree | 5071853cf9774acc23be9b9da203148dd3a0c43c | |
parent | 0ab80a1322dd63ec30abc4170d69aa55a028a8fc (diff) |
apparently we can have nice things
-rw-r--r-- | serialize/anim.cpp | 108 | ||||
-rw-r--r-- | serialize/scenery.cpp | 34 | ||||
-rw-r--r-- | serialize/tile-atlas.cpp | 8 | ||||
-rw-r--r-- | serialize/wall-atlas.cpp | 69 | ||||
-rw-r--r-- | serialize/wall-atlas.hpp | 13 | ||||
-rw-r--r-- | test/wall-atlas.cpp | 28 |
6 files changed, 131 insertions, 129 deletions
diff --git a/serialize/anim.cpp b/serialize/anim.cpp index bea7f3ce..aad40f33 100644 --- a/serialize/anim.cpp +++ b/serialize/anim.cpp @@ -7,7 +7,7 @@ namespace floormat { -using namespace std::string_literals; +using namespace std::string_view_literals; //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_frame, ground, offset, size) //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_group, name, frames, ground, offset) @@ -18,106 +18,106 @@ static void to_json(nlohmann::json& j, const anim_frame& val) constexpr anim_frame def; if (val.ground != def.ground) - j["ground"s] = val.ground; + j["ground"sv] = val.ground; if (val.offset != def.offset) - j["offset"s] = val.offset; + j["offset"sv] = val.offset; if (val.size != def.size) - j["size"s] = val.size; + j["size"sv] = val.size; } static void from_json(const nlohmann::json& j, anim_frame& val) { val = {}; - if (j.contains("ground"s)) - val.ground = j["ground"s]; - if (j.contains("offset"s)) - val.offset = j["offset"s]; - if (j.contains("size"s)) - val.size = j["size"s]; + if (j.contains("ground"sv)) + val.ground = j["ground"sv]; + if (j.contains("offset"sv)) + val.offset = j["offset"sv]; + if (j.contains("size"sv)) + val.size = j["size"sv]; } static void to_json(nlohmann::json& j, const anim_group& val) { const anim_group def{}; - j["name"s] = val.name; + j["name"sv] = val.name; if (val.mirror_from) - j["mirror-from"s] = val.mirror_from; + j["mirror-from"sv] = val.mirror_from; else - j["frames"s] = val.frames; + j["frames"sv] = val.frames; if (val.ground != def.ground) - j["ground"s] = val.ground; + j["ground"sv] = val.ground; if (val.offset != def.offset) - j["offset"s] = val.offset; + j["offset"sv] = val.offset; if (val.z_offset != def.z_offset) - j["z-offset"s] = val.z_offset; + j["z-offset"sv] = val.z_offset; if (val.depth_offset != def.depth_offset) - j["depth-offset"s] = val.depth_offset; + j["depth-offset"sv] = val.depth_offset; } static void from_json(const nlohmann::json& j, anim_group& val) { val = {}; - val.name = j["name"s]; + val.name = j["name"sv]; fm_soft_assert(!val.name.isEmpty()); - if (j.contains("mirror-from"s)) + if (j.contains("mirror-from"sv)) { - fm_soft_assert(!j.contains("frames"s)); - val.mirror_from = j["mirror-from"s]; + fm_soft_assert(!j.contains("frames"sv)); + val.mirror_from = j["mirror-from"sv]; } else - val.frames = j["frames"s]; - if (j.contains("ground"s)) - val.ground = j["ground"s]; - if (j.contains("offset"s)) - val.offset = j["offset"s]; - if (j.contains("z-offset"s)) - val.z_offset = j["z-offset"s]; - if (j.contains("depth-offset"s)) - val.depth_offset = j["depth-offset"s]; + val.frames = j["frames"sv]; + if (j.contains("ground"sv)) + val.ground = j["ground"sv]; + if (j.contains("offset"sv)) + val.offset = j["offset"sv]; + if (j.contains("z-offset"sv)) + val.z_offset = j["z-offset"sv]; + if (j.contains("depth-offset"sv)) + val.depth_offset = j["depth-offset"sv]; } static void to_json(nlohmann::json& j, const anim_def& val) { const anim_def def{}; - j["object_name"s] = val.object_name; + j["object_name"sv] = val.object_name; if (val.anim_name != def.anim_name) - j["anim_name"s] = val.anim_name; + j["anim_name"sv] = val.anim_name; if (val.pixel_size != def.pixel_size) - j["pixel_size"s] = val.pixel_size; + j["pixel_size"sv] = val.pixel_size; if (val.nframes != def.nframes) - j["nframes"s] = val.nframes; + j["nframes"sv] = val.nframes; if (val.action_frame != def.action_frame) - j["action-frame"s] = val.action_frame; + j["action-frame"sv] = val.action_frame; if (val.action_frame2 != def.action_frame2) - j["action-frame-2"s] = val.action_frame2; + j["action-frame-2"sv] = val.action_frame2; if (val.fps != def.fps) - j["fps"s] = val.fps; - j["groups"s] = val.groups; - j["scale"s] = val.scale; + j["fps"sv] = val.fps; + j["groups"sv] = val.groups; + j["scale"sv] = val.scale; } static void from_json(const nlohmann::json& j, anim_def& val) { val = {}; - val.object_name = j["object_name"s]; + val.object_name = j["object_name"sv]; fm_soft_assert(!val.object_name.isEmpty()); - if (j.contains("anim_name"s)) // todo underscore to hyphen - val.anim_name = j["anim_name"s]; - if (j.contains("pixel_size"s)) - val.pixel_size = j["pixel_size"s]; - if (j.contains("nframes"s)) - val.nframes = j["nframes"s]; - if (j.contains("action-frame"s)) - val.action_frame = j["action-frame"s]; - if (j.contains("action-frame-2"s)) - val.action_frame2 = j["action-frame-2"s]; - if (j.contains("fps"s)) - val.fps = j["fps"s]; - val.groups = j["groups"s]; + if (j.contains("anim_name"sv)) // todo underscore to hyphen + val.anim_name = j["anim_name"sv]; + if (j.contains("pixel_size"sv)) + val.pixel_size = j["pixel_size"sv]; + if (j.contains("nframes"sv)) + val.nframes = j["nframes"sv]; + if (j.contains("action-frame"sv)) + val.action_frame = j["action-frame"sv]; + if (j.contains("action-frame-2"sv)) + val.action_frame2 = j["action-frame-2"sv]; + if (j.contains("fps"sv)) + val.fps = j["fps"sv]; + val.groups = j["groups"sv]; fm_soft_assert(!val.groups.empty()); - val.scale = j["scale"s]; + val.scale = j["scale"sv]; fm_soft_assert(val.scale.type != anim_scale_type::invalid); } diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index 820c67af..2994d778 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -13,7 +13,7 @@ namespace { using namespace floormat; -using namespace std::string_literals; +using namespace std::string_view_literals; constexpr struct { scenery_type value = scenery_type::none; @@ -86,24 +86,24 @@ void adl_serializer<scenery_proto>::to_json(json& j, const scenery_proto& f) fm_assert(f.atlas); const scenery_proto default_scenery; if (f.type != default_scenery.type) - j["type"s] = f.type; - j["atlas-name"s] = f.atlas->name(); + j["type"sv] = f.type; + j["atlas-name"sv] = f.atlas->name(); if (f.frame != default_scenery.frame) - j["frame"s] = f.frame; + j["frame"sv] = f.frame; if (f.r != default_scenery.r) - j["rotation"s] = f.r; + j["rotation"sv] = f.r; if (f.pass != default_scenery.pass) - j["pass-mode"s] = f.pass; + j["pass-mode"sv] = f.pass; if (f.active != default_scenery.active) - j["active"s] = f.active; + j["active"sv] = f.active; if (f.interactive != default_scenery.interactive) - j["interactive"s] = f.interactive; + j["interactive"sv] = f.interactive; if (f.offset != default_scenery.offset) - j["offset"s] = Vector2i(f.offset); + j["offset"sv] = Vector2i(f.offset); if (f.bbox_offset != default_scenery.bbox_offset) - j["bbox-offset"s] = Vector2i(f.bbox_offset); + j["bbox-offset"sv] = Vector2i(f.bbox_offset); if (f.bbox_size != default_scenery.bbox_size) - j["bbox-size"s] = Vector2ui(f.bbox_size); + j["bbox-size"sv] = Vector2ui(f.bbox_size); } void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& f) @@ -115,7 +115,7 @@ void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& f) value = j[s]; }; - StringView atlas_name = j["atlas-name"s]; + StringView atlas_name = j["atlas-name"sv]; fm_soft_assert(!atlas_name.isEmpty()); f = {}; f.atlas = loader.anim_atlas(atlas_name, loader_::SCENERY_PATH); @@ -170,17 +170,17 @@ void adl_serializer<serialized_scenery>::to_json(json& j, const serialized_scene fm_soft_assert(val.proto.atlas); j = val.proto; const auto name = !val.name.isEmpty() ? StringView{val.name} : val.proto.atlas->name(); - j["name"s] = name; - j["description"s] = val.descr; + j["name"sv] = name; + j["description"sv] = val.descr; } void adl_serializer<serialized_scenery>::from_json(const json& j, serialized_scenery& val) { val = {}; val.proto = j; - val.name = j["name"s]; - if (j.contains("description"s)) - val.descr = j["description"s]; + val.name = j["name"sv]; + if (j.contains("description"sv)) + val.descr = j["description"sv]; } } // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index 3f619dd1..3918d5b0 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -10,7 +10,7 @@ #include <nlohmann/json.hpp> using namespace floormat; -using namespace std::string_literals; +using namespace std::string_view_literals; namespace { @@ -35,7 +35,7 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::sh { to_json(j, proxy{x->name(), x->num_tiles2(), NullOpt}); if (auto p = x->pass_mode()) - j["pass-mode"s] = *p; + j["pass-mode"sv] = *p; } } @@ -49,8 +49,8 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std:: proxy x; from_json(j, x); Optional<pass_mode> p; - if (j.contains("pass-mode"s)) - p = {InPlaceInit, j["pass-mode"s]}; + if (j.contains("pass-mode"sv)) + p = {InPlaceInit, j["pass-mode"sv]}; val = loader.tile_atlas(x.name, x.size, p); if (auto p2 = val->pass_mode(); p && p2 != p) { diff --git a/serialize/wall-atlas.cpp b/serialize/wall-atlas.cpp index 8a495a5c..2ce2e93c 100644 --- a/serialize/wall-atlas.cpp +++ b/serialize/wall-atlas.cpp @@ -12,8 +12,7 @@ namespace floormat::Wall::detail { -using nlohmann::json; -using namespace std::string_literals; +using namespace std::string_view_literals; namespace { @@ -47,25 +46,25 @@ Group read_group_metadata(const json& jgroup) { int count = 0, index = -1; - bool has_count = jgroup.contains("count"s) && (count = jgroup["count"s]) != 0, - has_index = jgroup.contains("index"s); + bool has_count = jgroup.contains("count"sv) && (count = jgroup["count"sv]) != 0, + has_index = jgroup.contains("index"sv); fm_soft_assert(has_count == has_index); fm_soft_assert(!has_index || index >= 0 && index < 1 << 20); // todo check index within range; } - if (jgroup.contains("pixel-size"s)) - val.pixel_size = jgroup["pixel-size"s]; - if (jgroup.contains("tint-mult"s)) - val.tint_mult = Vector4(jgroup["tint-mult"s]); - if (jgroup.contains("tint-add"s)) - val.tint_add = Vector3(jgroup["tint-add"s]); - if (jgroup.contains("from-rotation"s)) - val.from_rotation = (uint8_t)direction_index_from_name(std::string{ jgroup["from-rotation"s] }); - if (jgroup.contains("mirrored"s)) - val.mirrored = !!jgroup["mirrored"s]; - if (jgroup.contains("default-tint"s)) - val.default_tint = !!jgroup["default-tint"s]; + if (jgroup.contains("pixel-size"sv)) + val.pixel_size = jgroup["pixel-size"sv]; + if (jgroup.contains("tint-mult"sv)) + val.tint_mult = Vector4(jgroup["tint-mult"sv]); + if (jgroup.contains("tint-add"sv)) + val.tint_add = Vector3(jgroup["tint-add"sv]); + if (jgroup.contains("from-rotation"sv)) + val.from_rotation = (uint8_t)direction_index_from_name(std::string{ jgroup["from-rotation"sv] }); + if (jgroup.contains("mirrored"sv)) + val.mirrored = !!jgroup["mirrored"sv]; + if (jgroup.contains("default-tint"sv)) + val.default_tint = !!jgroup["default-tint"sv]; return val; } @@ -92,12 +91,12 @@ Direction read_direction_metadata(const json& jroot, Direction_ dir) Info read_info_header(const json& jroot) { - fm_soft_assert(jroot.contains(("name"s))); + fm_soft_assert(jroot.contains(("name"sv))); fm_soft_assert(jroot.contains(("depth"))); - Info val = {std::string{jroot["name"s]}, {}, jroot["depth"s]}; + Info val = {std::string{jroot["name"sv]}, {}, jroot["depth"sv]}; fm_soft_assert(val.depth > 0); - if (jroot.contains("description"s)) - val.description = std::string{jroot["description"s]}; + if (jroot.contains("description"sv)) + val.description = std::string{jroot["description"sv]}; return val; } @@ -106,14 +105,14 @@ void write_group_metadata(json& jgroup, const Group& val) fm_assert(jgroup.is_object()); fm_assert(jgroup.empty()); - jgroup["index"s] = val.index; - jgroup["count"s] = val.count; - jgroup["pixel-size"s] = val.pixel_size; - jgroup["tint-mult"s] = Vector4(val.tint_mult); - jgroup["tint-add"s] = Vector3(val.tint_add); - jgroup["from-rotation"s] = val.from_rotation; - jgroup["mirrored"s] = val.mirrored; - jgroup["default-tint"s] = val.default_tint; + jgroup["index"sv] = val.index; + jgroup["count"sv] = val.count; + jgroup["pixel-size"sv] = val.pixel_size; + jgroup["tint-mult"sv] = Vector4(val.tint_mult); + jgroup["tint-add"sv] = Vector3(val.tint_add); + jgroup["from-rotation"sv] = val.from_rotation; + jgroup["mirrored"sv] = val.mirrored; + jgroup["default-tint"sv] = val.default_tint; } void write_direction_metadata(json& jdir, const Direction& dir) @@ -126,20 +125,20 @@ void write_direction_metadata(json& jdir, const Direction& dir) const auto& group = dir.*memfn; write_group_metadata(jdir[s], group); } - if (jdir.contains("top"s)) + if (jdir.contains("top"sv)) { - auto& top = jdir["top"s]; - if (top.contains("pixel-size"s)) - top["pixel-size"s] = Vector2i{top["pixel-size"s]}.flipped(); + auto& top = jdir["top"sv]; + if (top.contains("pixel-size"sv)) + top["pixel-size"sv] = Vector2i{top["pixel-size"sv]}.flipped(); } } void write_info_header(json& jroot, const Info& info) { - jroot["name"s] = info.name; + jroot["name"sv] = info.name; if (info.description) - jroot["description"s] = info.description; - jroot["depth"s] = info.depth; + jroot["description"sv] = info.description; + jroot["depth"sv] = info.depth; } } // namespace floormat::Wall::detail diff --git a/serialize/wall-atlas.hpp b/serialize/wall-atlas.hpp index 6c50da6b..17ed5a33 100644 --- a/serialize/wall-atlas.hpp +++ b/serialize/wall-atlas.hpp @@ -16,14 +16,17 @@ struct adl_serializer<std::shared_ptr<floormat::wall_atlas>> namespace floormat::Wall::detail { +using nlohmann::json; + uint8_t direction_index_from_name(StringView s); StringView direction_index_to_name(size_t i); -[[nodiscard]] Group read_group_metadata(const nlohmann::json& jgroup); -[[nodiscard]] Direction read_direction_metadata(const nlohmann::json& jroot, Direction_ dir); -Info read_info_header(const nlohmann::json& jroot); +[[nodiscard]] Group read_group_metadata(const json& jgroup); +[[nodiscard]] Direction read_direction_metadata(const json& jroot, Direction_ dir); +Info read_info_header(const json& jroot); -void write_group_metadata(nlohmann::json& jgroup, const Group& val); -void write_info_header(nlohmann::json& jroot, const Info& info); +void write_group_metadata(json& jgroup, const Group& val); +void write_direction_metadata(json& jdir, const Direction& dir); +void write_info_header(json& jroot, const Info& info); } // namespace floormat::Wall::detail diff --git a/test/wall-atlas.cpp b/test/wall-atlas.cpp index 92439800..2d1e4f85 100644 --- a/test/wall-atlas.cpp +++ b/test/wall-atlas.cpp @@ -8,7 +8,7 @@ namespace floormat::Wall::detail { using nlohmann::json; -using namespace std::string_literals; +using namespace std::string_view_literals; namespace { @@ -37,13 +37,13 @@ void test_read_empty_direction(StringView filename) test_read_header(filename); fm_assert(!jroot.empty()); - fm_assert( jroot.contains("n"s) ); - fm_assert(!jroot.contains("e"s) ); - fm_assert(!jroot.contains("s"s) ); - fm_assert( jroot.contains("w"s) ); + fm_assert( jroot.contains("n"sv) ); + fm_assert(!jroot.contains("e"sv) ); + fm_assert(!jroot.contains("s"sv) ); + fm_assert( jroot.contains("w"sv) ); - fm_assert(jroot["n"s].is_object() && jroot["n"s].empty()); - fm_assert(jroot["w"s].is_object() && jroot["w"s].empty()); + fm_assert(jroot["n"sv].is_object() && jroot["n"sv].empty()); + fm_assert(jroot["w"sv].is_object() && jroot["w"sv].empty()); } void test_read_groups(StringView filename) @@ -52,13 +52,13 @@ void test_read_groups(StringView filename) const auto jroot = json_helper::from_json_(Path::join(json_path(), filename)); read_info_header(jroot); - fm_assert(jroot["depth"s] == 42); - fm_assert( jroot.contains("n"s) ); - fm_assert(!jroot.contains("e"s) ); - fm_assert(!jroot.contains("s"s) ); - fm_assert( jroot.contains("w"s) ); - fm_assert(jroot["n"s].is_object() && jroot["n"s].empty()); - fm_assert(jroot["w"s].is_object() && !jroot["w"s].empty()); + fm_assert(jroot["depth"sv] == 42); + fm_assert( jroot.contains("n"sv) ); + fm_assert(!jroot.contains("e"sv) ); + fm_assert(!jroot.contains("s"sv) ); + fm_assert( jroot.contains("w"sv) ); + fm_assert(jroot["n"sv].is_object() && jroot["n"sv].empty()); + fm_assert(jroot["w"sv].is_object() && !jroot["w"sv].empty()); fm_assert(read_direction_metadata(jroot, Direction_::N).is_empty()); fm_assert(read_direction_metadata(jroot, Direction_::E).is_empty()); fm_assert(read_direction_metadata(jroot, Direction_::S).is_empty()); |