summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-22 16:03:10 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-22 16:03:10 +0200
commitb1165d784c446dc505a100d861cb5151bebdda15 (patch)
tree0186179580aa8585f6fb3f3ad9c0ca7207f97607 /serialize
parentaa03952cb2a889f8d81a70ed4bd0b4ae69e5ab8d (diff)
serializer work
Diffstat (limited to 'serialize')
-rw-r--r--serialize/tile-atlas.cpp16
-rw-r--r--serialize/tile-atlas.hpp2
-rw-r--r--serialize/tile.cpp4
-rw-r--r--serialize/world.cpp53
-rw-r--r--serialize/world.hpp8
5 files changed, 67 insertions, 16 deletions
diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp
index 4c80a212..d5350851 100644
--- a/serialize/tile-atlas.cpp
+++ b/serialize/tile-atlas.cpp
@@ -2,29 +2,25 @@
#include "serialize/tile-atlas.hpp"
#include "serialize/magnum-vector2i.hpp"
#include "loader.hpp"
+#include "compat/assert.hpp"
#include <tuple>
#include <nlohmann/json.hpp>
-using namespace Magnum;
using namespace floormat;
namespace nlohmann {
using proxy_atlas = std::tuple<std::string, Vector2ub>;
-using shared_atlas = std::shared_ptr<floormat::tile_atlas>;
-void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x)
+void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::shared_ptr<const tile_atlas>& x)
{
- if (!x)
- j = nullptr;
- else {
- using nlohmann::to_json;
- to_json(j, proxy_atlas{x->name(), x->num_tiles2()});
- }
+ fm_assert(x);
+ using nlohmann::to_json;
+ to_json(j, proxy_atlas{x->name(), x->num_tiles2()});
}
-void adl_serializer<shared_atlas>::from_json(const json& j, shared_atlas& x)
+void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std::shared_ptr<tile_atlas>& x)
{
proxy_atlas proxy = j;
const auto& [name, num_tiles] = proxy;
diff --git a/serialize/tile-atlas.hpp b/serialize/tile-atlas.hpp
index f4549d7c..02e5b966 100644
--- a/serialize/tile-atlas.hpp
+++ b/serialize/tile-atlas.hpp
@@ -8,7 +8,7 @@ namespace nlohmann {
template<>
struct adl_serializer<std::shared_ptr<floormat::tile_atlas>> final {
- static void to_json(json& j, const std::shared_ptr<floormat::tile_atlas>& x);
+ static void to_json(json& j, const std::shared_ptr<const floormat::tile_atlas>& x);
static void from_json(const json& j, std::shared_ptr<floormat::tile_atlas>& x);
};
diff --git a/serialize/tile.cpp b/serialize/tile.cpp
index 2e94bfb8..11473548 100644
--- a/serialize/tile.cpp
+++ b/serialize/tile.cpp
@@ -12,10 +12,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords, x, y)
} // namespace floormat
-namespace nlohmann {
-
using namespace floormat;
+namespace nlohmann {
+
void adl_serializer<tile_image>::to_json(json& j, const tile_image& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; }
void adl_serializer<tile_image>::from_json(const json& j, tile_image& val) { using nlohmann::from_json; from_json(j, val); }
diff --git a/serialize/world.cpp b/serialize/world.cpp
index b52fe3b9..835a80f8 100644
--- a/serialize/world.cpp
+++ b/serialize/world.cpp
@@ -1,8 +1,16 @@
#include "world.hpp"
-#include "tile.hpp"
-#include "global-coords.hpp"
+#include "serialize/tile.hpp"
+#include "serialize/tile-atlas.hpp"
+#include "src/global-coords.hpp"
+#include "src/chunk.hpp"
+#include "src/world.hpp"
+#include <memory>
#include <nlohmann/json.hpp>
+#ifdef __GNUG__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
namespace floormat {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y)
@@ -16,9 +24,29 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_coords_, chunk, local)
} // namespace floormat
+using namespace floormat;
+
namespace nlohmann {
-using namespace floormat;
+template<>
+struct adl_serializer<std::shared_ptr<chunk>> final {
+ static void to_json(json& j, const std::shared_ptr<const chunk>& x);
+ static void from_json(const json& j, std::shared_ptr<chunk>& x);
+};
+
+void adl_serializer<std::shared_ptr<chunk>>::to_json(json& j, const std::shared_ptr<const chunk>& val)
+{
+ fm_assert(val);
+ using nlohmann::to_json;
+ j = *val;
+}
+
+void adl_serializer<std::shared_ptr<chunk>>::from_json(const json& j, std::shared_ptr<chunk>& val)
+{
+ val = std::make_shared<chunk>();
+ using nlohmann::from_json;
+ *val = j;
+}
void adl_serializer<chunk_coords>::to_json(json& j, const chunk_coords& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<chunk_coords>::from_json(const json& j, chunk_coords& val) { using nlohmann::from_json; from_json(j, val); }
@@ -26,4 +54,23 @@ void adl_serializer<chunk_coords>::from_json(const json& j, chunk_coords& val) {
void adl_serializer<global_coords>::to_json(json& j, const global_coords& val) { using nlohmann::to_json; to_json(j, global_coords_{val.chunk(), val.local()}); }
void adl_serializer<global_coords>::from_json(const json& j, global_coords& val) { using nlohmann::from_json; global_coords_ x; from_json(j, x); val = {x.chunk, x.local}; }
+void adl_serializer<world>::to_json(json& j, const world& val)
+{
+ using nlohmann::to_json;
+ to_json(j, val.chunks());
+}
+
+void adl_serializer<world>::from_json(const json& j, world& val)
+{
+ using T = std::remove_cvref_t<decltype(val.chunks())>;
+ T x{};
+ using nlohmann::from_json;
+ from_json(j, x);
+ val = world{std::move(x)};
+}
+
} // namespace nlohmann
+
+#ifdef __GNUG__
+#pragma GCC diagnostic pop
+#endif
diff --git a/serialize/world.hpp b/serialize/world.hpp
index 05c88b40..dd7f5078 100644
--- a/serialize/world.hpp
+++ b/serialize/world.hpp
@@ -5,6 +5,8 @@ namespace floormat {
struct chunk_coords;
struct global_coords;
+struct chunk;
+struct world;
} // namespace floormat
@@ -22,4 +24,10 @@ struct adl_serializer<floormat::global_coords> {
static void from_json(const json& j, floormat::global_coords& val);
};
+template<>
+struct adl_serializer<floormat::world> {
+ static void to_json(json& j, const floormat::world& val);
+ static void from_json(const json& j, floormat::world& val);
+};
+
} // namespace nlohmann