summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--editor/json.cpp21
-rw-r--r--serialize/json-helper.cpp19
-rw-r--r--serialize/json-helper.hpp12
-rw-r--r--serialize/tile.cpp7
-rw-r--r--serialize/tile.hpp12
-rw-r--r--serialize/world.cpp46
-rw-r--r--serialize/world.hpp7
-rw-r--r--test/json.cpp5
8 files changed, 27 insertions, 102 deletions
diff --git a/editor/json.cpp b/editor/json.cpp
index 7a64b207..f3317d5e 100644
--- a/editor/json.cpp
+++ b/editor/json.cpp
@@ -8,8 +8,15 @@
namespace floormat {
-#define save_dir "../save"
+#define FM_SAVE_BINARY
+
+#ifdef FM_SAVE_BINARY
+#define quicksave_file save_dir "/" "quicksave.dat"
+#else
#define quicksave_file save_dir "/" "quicksave.json"
+#endif
+
+#define save_dir "../save"
#define quicksave_tmp quicksave_file ".tmp"
namespace Path = Corrade::Utility::Path;
@@ -35,7 +42,13 @@ void app::do_quicksave()
if (Path::exists(quicksave_tmp))
Path::remove(quicksave_tmp);
fputs("quicksave...", stderr); fflush(stderr);
+#if 0
+#ifdef FM_SAVE_BINARY
+ json_helper::to_binary(world, quicksave_tmp);
+#else
json_helper::to_json(world, quicksave_tmp);
+#endif
+#endif
Path::move(quicksave_tmp, quicksave_file);
fputs(" done\n", stderr); fflush(stderr);
}
@@ -50,7 +63,13 @@ void app::do_quickload()
}
auto& world = M->world();
fputs("quickload...", stderr); fflush(stderr);
+#if 0
+#ifdef FM_SAVE_BINARY
+ world = json_helper::from_binary<struct world>(quicksave_file);
+#else
world = json_helper::from_json<struct world>(quicksave_file);
+#endif
+#endif
fputs(" done\n", stderr); fflush(stderr);
}
diff --git a/serialize/json-helper.cpp b/serialize/json-helper.cpp
index 8738065f..dae3ad96 100644
--- a/serialize/json-helper.cpp
+++ b/serialize/json-helper.cpp
@@ -25,23 +25,4 @@ void json_helper::to_json_(const json& j, const fspath& pathname, int indent)
(open_stream<std::ofstream, fspath, std::ios_base::out>(pathname) << j.dump(indent, '\t') << '\n').flush();
}
-#define FORMAT cbor
-
-#define JOIN2(prefix, fmt) prefix ## fmt
-#define JOIN(prefix, fmt) JOIN2(prefix, fmt)
-#define FROM JOIN(from_, FORMAT)
-#define TO JOIN(to_, FORMAT)
-
-auto json_helper::from_binary_(const fspath& pathname) -> json
-{
- return json::FROM(open_stream<std::ifstream, fspath, std::ios_base::in>(pathname));
-}
-
-void json_helper::to_binary_(const json& j, const fspath& pathname)
-{
- auto s = open_stream<std::ofstream, fspath, std::ios_base::out>(pathname);
- json::TO(j, s);
- s.flush();
-}
-
} // namespace floormat
diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp
index b6fe1da2..9c9d208d 100644
--- a/serialize/json-helper.hpp
+++ b/serialize/json-helper.hpp
@@ -10,20 +10,12 @@ struct json_helper final {
using fspath = std::filesystem::path;
template<typename T> static T from_json(const fspath& pathname);
- template<typename T, int indent = 1> static void to_json(const T& self, const fspath& pathname);
+ template<typename T> static void to_json(const T& self, const fspath& pathname, int indent = 1);
static json from_json_(const fspath& pathname);
static void to_json_(const json& j, const fspath& pathname, int indent);
-
- template<typename T> static T from_binary(const fspath& pathname);
- template<typename T> static void to_binary(const T& self, const fspath& pathname);
- static json from_binary_(const fspath& pathname);
- static void to_binary_(const json& j, const fspath& pathname);
};
template<typename T> T json_helper::from_json(const fspath& pathname) { return from_json_(pathname); }
-template<typename T, int indent> void json_helper::to_json(const T& self, const fspath& pathname) { to_json_(json(self), pathname, indent); }
-
-template<typename T> T json_helper::from_binary(const fspath& pathname) { return from_binary_(pathname); }
-template<typename T> void json_helper::to_binary(const T& self, const fspath& pathname) { to_binary_(json(self), pathname); }
+template<typename T> void json_helper::to_json(const T& self, const fspath& pathname, int indent) { to_json_(json(self), pathname, indent); }
} // namespace floormat
diff --git a/serialize/tile.cpp b/serialize/tile.cpp
index 8a5374ab..629ce52d 100644
--- a/serialize/tile.cpp
+++ b/serialize/tile.cpp
@@ -19,13 +19,8 @@ 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; if (j.is_null()) val = {}; else from_json(j, val); }
-void adl_serializer<tile>::to_json(json& j, const tile& val) { using nlohmann::to_json; to_json(j, val); }
-void adl_serializer<tile>::from_json(const json& j, tile& val) { using nlohmann::from_json; from_json(j, val); }
-
-void adl_serializer<chunk>::to_json(json& j, const chunk& val) { using nlohmann::to_json; to_json(j, val.tiles()); }
-void adl_serializer<chunk>::from_json(const json& j, chunk& val) { using nlohmann::from_json; from_json(j, val.tiles()); }
-
void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { using nlohmann::from_json; from_json(j, val); }
} // namespace nlohmann
+
diff --git a/serialize/tile.hpp b/serialize/tile.hpp
index 09c23f9a..01673b2b 100644
--- a/serialize/tile.hpp
+++ b/serialize/tile.hpp
@@ -19,18 +19,6 @@ struct adl_serializer<floormat::tile_image> {
};
template<>
-struct adl_serializer<floormat::tile> {
- static void to_json(json& j, const floormat::tile& val);
- static void from_json(const json& j, floormat::tile& val);
-};
-
-template<>
-struct adl_serializer<floormat::chunk> {
- static void to_json(json& j, const floormat::chunk& val);
- static void from_json(const json& j, floormat::chunk& val);
-};
-
-template<>
struct adl_serializer<floormat::local_coords> {
static void to_json(json& j, const floormat::local_coords& val);
static void from_json(const json& j, floormat::local_coords& val);
diff --git a/serialize/world.cpp b/serialize/world.cpp
index 7bdf8ba6..cd6ebd6b 100644
--- a/serialize/world.cpp
+++ b/serialize/world.cpp
@@ -1,10 +1,6 @@
#include "world.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 "serialize/tile.hpp"
#include <nlohmann/json.hpp>
#if defined _MSC_VER
@@ -30,49 +26,11 @@ 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)
-{
- using nlohmann::to_json;
- if (!val)
- j = nullptr;
- else
- j = *val;
-}
-
-void adl_serializer<std::shared_ptr<chunk>>::from_json(const json& j, std::shared_ptr<chunk>& val)
-{
- using nlohmann::from_json;
- if (j.is_null())
- val = nullptr;
- else
- *(val = std::make_shared<chunk>()) = 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); }
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
+
diff --git a/serialize/world.hpp b/serialize/world.hpp
index dd7f5078..39abaaca 100644
--- a/serialize/world.hpp
+++ b/serialize/world.hpp
@@ -24,10 +24,5 @@ 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
+
diff --git a/test/json.cpp b/test/json.cpp
index 198f2d48..8ea2bd09 100644
--- a/test/json.cpp
+++ b/test/json.cpp
@@ -11,6 +11,7 @@
namespace floormat {
+[[maybe_unused]]
static chunk make_test_chunk()
{
auto metal1 = loader.tile_atlas("metal1", {2, 2}),
@@ -47,10 +48,6 @@ bool floormat::test_json() // NOLINT(readability-convert-member-functions-to-sta
Magnum::Math::Vector3 vec{0.f/zero, -1.f/zero, 123.f};
json_helper::to_json(vec, output_dir/"vec3_inf.json");
}
- {
- const auto chunk = make_test_chunk();
- json_helper::to_json(chunk, output_dir/"zzz_chunk-1.json");
- }
return true;
}