From beb6685f605b89ce4b2dd99f87b9319e4f7f4d94 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 27 Nov 2022 17:27:56 +0100 Subject: more WIP --- serialize/binary-serializer.hpp | 7 ------ serialize/scenery.cpp | 52 +++++++++++++++++++++++++++-------------- serialize/scenery.hpp | 23 ++++++++++++------ 3 files changed, 50 insertions(+), 32 deletions(-) (limited to 'serialize') diff --git a/serialize/binary-serializer.hpp b/serialize/binary-serializer.hpp index 3d39880c..057b3c4d 100644 --- a/serialize/binary-serializer.hpp +++ b/serialize/binary-serializer.hpp @@ -6,13 +6,6 @@ #include #include -namespace Corrade::Containers { - -template class BasicStringView; -using StringView = BasicStringView; - -} // namespace Corrade::Containers - namespace floormat::Serialize { static_assert(std::endian::native == std::endian::big || std::endian::native == std::endian::little); diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index b2490739..8bff3d69 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -18,7 +18,7 @@ constexpr struct { { scenery_type::none, "none"_s }, { scenery_type::generic, "generic"_s }, { scenery_type::door, "door"_s }, - { scenery_type::object, "object"_s }, + //{ scenery_type::object, "object"_s }, }; constexpr struct { @@ -87,34 +87,50 @@ void adl_serializer::to_json(json& j, const scenery_proto& val) j["type"] = f.type; fm_assert(val.atlas); j["atlas-name"] = val.atlas->name(); - fm_assert(f.frame != scenery::NO_FRAME); j["frame"] = f.frame; j["rotation"] = f.r; j["passable"] = f.passable; j["blocks-view"] = f.blocks_view; j["active"] = f.active; + j["animated"] = f.animated; + j["interactive"] = f.interactive; } void adl_serializer::from_json(const json& j, scenery_proto& val) { - auto& f = val.frame; - f.type = j["type"]; + const auto get = [&](const StringView& name, auto& value) + { + auto s = std::string_view{name.data(), name.size()}; + if (j.contains(s)) + value = j[s]; + }; + StringView atlas_name = j["atlas-name"]; - val.atlas = loader.anim_atlas(atlas_name); + fm_assert(!atlas_name.isEmpty()); + auto atlas = loader.anim_atlas(atlas_name); + auto& f = val.frame; f = {}; - if (j.contains("frame")) - f.frame = j["frame"]; - if (j.contains("animated")) - f.animated = j["animated"]; - fm_assert(f.animated == (f.frame == scenery::NO_FRAME)); - if (j.contains("rotation")) - f.r = j["rotation"]; - if (j.contains("passable")) - f.passable = j["passable"]; - if (j.contains("blocks-view")) - f.blocks_view = j["blocks-view"]; - if (j.contains("active")) - f.active = j["active"]; + + auto type = scenery_type::generic; get("type", type); + auto frame = f.frame; get("frame", frame); + auto r = f.r; get("rotation", r); + bool passable = f.passable; get("passable", passable); + bool blocks_view = f.blocks_view; get("blocks-view", blocks_view); + bool active = f.active; get("active", active); + bool animated = f.animated; get("animated", animated); + + switch (f.type) + { + default: + fm_abort("unhandled scenery type '%hhu'", f.type); + case scenery_type::generic: + f = { scenery::generic, *atlas, r, frame, passable, blocks_view, animated, active }; + break; + case scenery_type::door: + f = { scenery::door, *atlas, r, false }; + } } + + } // namespace nlohmann diff --git a/serialize/scenery.hpp b/serialize/scenery.hpp index e7946913..190a4d2a 100644 --- a/serialize/scenery.hpp +++ b/serialize/scenery.hpp @@ -1,9 +1,20 @@ #pragma once #include "src/scenery.hpp" -#include #include +#include #include +namespace floormat::Serialize { + +struct serialized_scenery final { + StringView name, descr; + scenery_proto proto; + + static std::vector deserialize(StringView filename); +}; + +} // namespace floormat::Serialize + namespace nlohmann { template<> struct adl_serializer { @@ -19,13 +30,11 @@ template<> struct adl_serializer { template<> struct adl_serializer { static void to_json(json& j, const floormat::scenery_proto& val); static void from_json(const json& j, floormat::scenery_proto& val); +}; - struct item { - Corrade::Containers::StringView name, description; - floormat::scenery_proto proto; - }; - - static std::vector deserialize_list(Corrade::Containers::StringView file); +template<> struct adl_serializer { + static void to_json(json& j, const floormat::Serialize::serialized_scenery& val); + static void from_json(const json& j, floormat::Serialize::serialized_scenery& val); }; } // namespace nlohmann -- cgit v1.2.3