From 9107e0ff64fc5788d90c0b44da3f7148cba4f966 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Oct 2022 22:18:01 +0200 Subject: a --- serialize/json-helper.hpp | 10 ++++++--- serialize/magnum-vector.hpp | 35 ++++++++++++++++++------------- serialize/tile-atlas.cpp | 20 ++++++------------ serialize/tile.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++ serialize/tile.hpp | 26 ++++++++++++++++++----- 5 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 serialize/tile.cpp (limited to 'serialize') diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index 345efe54..86c6a16f 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -22,7 +22,12 @@ std::tuple json_helper::from_json(const std::filesystem::path& pathname using Corrade::Utility::Error; std::ifstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - s.open(pathname, std::ios_base::in); + try { + s.open(pathname, std::ios_base::in); + } catch (const std::ios::failure& e) { + Error{} << "failed to open" << pathname << "for reading:" << e.what(); + return {}; + } t ret; json j; s >> j; @@ -41,7 +46,7 @@ bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) try { s.open(pathname, std::ios_base::out | std::ios_base::trunc); } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); + Error{} << "failed to open" << pathname << "for writing:" << e.what(); return false; } s << j.dump(4); @@ -49,4 +54,3 @@ bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) s.flush(); return true; } - diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index eed37708..8ea5e13a 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -11,25 +11,32 @@ namespace nlohmann { template struct adl_serializer> { - static void to_json(json& j, const Magnum::Math::Vector& val) - { - std::array array{}; - for (std::size_t i = 0; i < N; i++) - array[i] = val[i]; - j = array; - } - static void from_json(const json& j, Magnum::Math::Vector& val) - { - std::array array = j; - for (std::size_t i = 0; i < N; i++) - val[i] = array[i]; - } + using vec = Magnum::Math::Vector; + static void to_json(json& j, const vec& val); + static void from_json(const json& j, vec& val); }; -template struct adl_serializer> : adl_serializer> {}; +template +void adl_serializer>::to_json(json& j, const vec& val) +{ + std::array array{}; + for (std::size_t i = 0; i < N; i++) + array[i] = val[i]; + j = array; +} + +template +void adl_serializer>::from_json(const json& j, vec& val) +{ + std::array array = j; + for (std::size_t i = 0; i < N; i++) + val[i] = array[i]; +} + template struct adl_serializer> : adl_serializer> {}; template struct adl_serializer> : adl_serializer> {}; template struct adl_serializer> : adl_serializer> {}; template struct adl_serializer> : adl_serializer> {}; +template struct adl_serializer> : adl_serializer> {}; } // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index c7e52057..8021f1d5 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -2,25 +2,16 @@ #include "serialize/tile-atlas.hpp" #include "serialize/magnum-vector2i.hpp" #include "loader.hpp" +#include #include -namespace Magnum::Examples::Serialize { - -struct proxy_atlas final { - std::string name; - Vector2ui num_tiles; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(proxy_atlas, name, num_tiles) - -} // namespace Magnum::Examples::Serialize +using namespace Magnum; +using namespace Magnum::Examples; namespace nlohmann { -using namespace Magnum::Examples; -using namespace Magnum::Examples::Serialize; - +using proxy_atlas = std::tuple; using shared_atlas = std::shared_ptr; void adl_serializer::to_json(json& j, const shared_atlas& x) @@ -36,7 +27,8 @@ void adl_serializer::to_json(json& j, const shared_atlas& x) void adl_serializer::from_json(const json& j, shared_atlas& x) { proxy_atlas proxy = j; - x = loader.tile_atlas(proxy.name, proxy.num_tiles); + const auto& [name, num_tiles] = proxy; + x = loader.tile_atlas(name, num_tiles); } } // namespace nlohmann diff --git a/serialize/tile.cpp b/serialize/tile.cpp new file mode 100644 index 00000000..2f414e62 --- /dev/null +++ b/serialize/tile.cpp @@ -0,0 +1,50 @@ +#include "src/tile.hpp" +#include "src/chunk.hpp" +#include "serialize/tile.hpp" +#include "serialize/tile-atlas.hpp" +#include +#include + +namespace Magnum::Examples { + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image, atlas, variant) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile, ground_image, wall_north, wall_west, passability) + +} // namespace Magnum::Examples + +namespace nlohmann { + +using namespace Magnum::Examples; + +void adl_serializer::to_json(json& j, const tile_image& val) { + using nlohmann::to_json; + to_json(j, val); +} + +void adl_serializer::from_json(const json& j, tile_image& val) { + using nlohmann::from_json; + from_json(j, val); +} + +void adl_serializer::to_json(json& j, const tile& val) { + using nlohmann::to_json; + to_json(j, val); +} + +void adl_serializer::from_json(const json& j, tile& val) { + using nlohmann::from_json; + from_json(j, val); +} + +void adl_serializer::to_json(json& j, const chunk& val) { + using nlohmann::to_json; + to_json(j, val.tiles()); +} + +void adl_serializer::from_json(const json& j, chunk& val) { + using nlohmann::from_json; + std::remove_cvref_t tiles = {}; + tiles = j; +} + +} // namespace nlohmann diff --git a/serialize/tile.hpp b/serialize/tile.hpp index 4224fa54..d0a2d144 100644 --- a/serialize/tile.hpp +++ b/serialize/tile.hpp @@ -1,14 +1,30 @@ #pragma once -#include +#include namespace Magnum::Examples { - - - +struct tile_image; +struct tile; +struct chunk; } // namespace Magnum::Examples namespace nlohmann { - +template<> +struct adl_serializer { + static void to_json(json& j, const Magnum::Examples::tile_image& val); + static void from_json(const json& j, Magnum::Examples::tile_image& val); +}; + +template<> +struct adl_serializer { + static void to_json(json& j, const Magnum::Examples::tile& val); + static void from_json(const json& j, Magnum::Examples::tile& val); +}; + +template<> +struct adl_serializer { + static void to_json(json& j, const Magnum::Examples::chunk& val); + static void from_json(const json& j, Magnum::Examples::chunk& val); +}; } // namespace nlohmann -- cgit v1.2.3