summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-27 17:27:56 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-27 17:27:56 +0100
commitbeb6685f605b89ce4b2dd99f87b9319e4f7f4d94 (patch)
treeb4db6babfa2a7c5518db59e1678ef4e434ae0d70 /serialize
parentd2fcaad5425acb4a50138e793e3dcecc6600e4d3 (diff)
more WIP
Diffstat (limited to 'serialize')
-rw-r--r--serialize/binary-serializer.hpp7
-rw-r--r--serialize/scenery.cpp52
-rw-r--r--serialize/scenery.hpp23
3 files changed, 50 insertions, 32 deletions
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 <concepts>
#include <type_traits>
-namespace Corrade::Containers {
-
-template<typename T> class BasicStringView;
-using StringView = BasicStringView<const char>;
-
-} // 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<scenery_proto>::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<scenery_proto>::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 <Corrade/Containers/StringView.h>
#include <vector>
+#include <Corrade/Containers/StringView.h>
#include <nlohmann/json_fwd.hpp>
+namespace floormat::Serialize {
+
+struct serialized_scenery final {
+ StringView name, descr;
+ scenery_proto proto;
+
+ static std::vector<serialized_scenery> deserialize(StringView filename);
+};
+
+} // namespace floormat::Serialize
+
namespace nlohmann {
template<> struct adl_serializer<floormat::scenery_type> {
@@ -19,13 +30,11 @@ template<> struct adl_serializer<floormat::rotation> {
template<> struct adl_serializer<floormat::scenery_proto> {
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<item> deserialize_list(Corrade::Containers::StringView file);
+template<> struct adl_serializer<floormat::Serialize::serialized_scenery> {
+ 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