diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/json-helper.hpp | 10 | ||||
-rw-r--r-- | serialize/magnum-vector.hpp | 35 | ||||
-rw-r--r-- | serialize/tile-atlas.cpp | 20 | ||||
-rw-r--r-- | serialize/tile.cpp | 50 | ||||
-rw-r--r-- | serialize/tile.hpp | 26 |
5 files changed, 105 insertions, 36 deletions
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<t, bool> 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<std::size_t N, typename T> struct adl_serializer<Magnum::Math::Vector<N, T>> { - static void to_json(json& j, const Magnum::Math::Vector<N, T>& val) - { - std::array<T, N> 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<N, T>& val) - { - std::array<T, N> array = j; - for (std::size_t i = 0; i < N; i++) - val[i] = array[i]; - } + using vec = Magnum::Math::Vector<N, T>; + static void to_json(json& j, const vec& val); + static void from_json(const json& j, vec& val); }; -template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {}; +template <std::size_t N, typename T> +void adl_serializer<Magnum::Math::Vector<N, T>>::to_json(json& j, const vec& val) +{ + std::array<T, N> array{}; + for (std::size_t i = 0; i < N; i++) + array[i] = val[i]; + j = array; +} + +template <std::size_t N, typename T> +void adl_serializer<Magnum::Math::Vector<N, T>>::from_json(const json& j, vec& val) +{ + std::array<T, N> array = j; + for (std::size_t i = 0; i < N; i++) + val[i] = array[i]; +} + template<typename T> struct adl_serializer<Magnum::Math::Vector3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Vector4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Color3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {}; template<typename T> struct adl_serializer<Magnum::Math::Color4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {}; +template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {}; } // 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 <tuple> #include <nlohmann/json.hpp> -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<std::string, Vector2ui>; using shared_atlas = std::shared_ptr<Magnum::Examples::tile_atlas>; void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x) @@ -36,7 +27,8 @@ void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x) void adl_serializer<shared_atlas>::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 <tuple> +#include <nlohmann/json.hpp> + +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<tile_image>::to_json(json& j, const tile_image& val) { + using nlohmann::to_json; + to_json(j, val); +} + +void adl_serializer<tile_image>::from_json(const json& j, tile_image& val) { + using nlohmann::from_json; + 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; + std::remove_cvref_t<decltype(val.tiles())> 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 <nlohmann/json.hpp> +#include <nlohmann/json_fwd.hpp> namespace Magnum::Examples { - - - +struct tile_image; +struct tile; +struct chunk; } // namespace Magnum::Examples namespace nlohmann { - +template<> +struct adl_serializer<Magnum::Examples::tile_image> { + 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<Magnum::Examples::tile> { + 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<Magnum::Examples::chunk> { + static void to_json(json& j, const Magnum::Examples::chunk& val); + static void from_json(const json& j, Magnum::Examples::chunk& val); +}; } // namespace nlohmann |