From daa16769f087b60cc67517171d942db967b2949e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 1 Dec 2022 16:33:18 +0100 Subject: serialize: add binary_reader::operator<< --- serialize/binary-reader.hpp | 3 +++ serialize/binary-reader.inl | 6 ++++++ serialize/world-reader.cpp | 32 ++++++++++++++++---------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/serialize/binary-reader.hpp b/serialize/binary-reader.hpp index bf7e578e..530d9f0c 100644 --- a/serialize/binary-reader.hpp +++ b/serialize/binary-reader.hpp @@ -43,6 +43,9 @@ private: It it, end; }; +template +void operator<<(T& x, binary_reader& reader) noexcept; + template binary_reader& operator>>(binary_reader& reader, T& x) noexcept; diff --git a/serialize/binary-reader.inl b/serialize/binary-reader.inl index 2230dff6..bb1f8cf7 100644 --- a/serialize/binary-reader.inl +++ b/serialize/binary-reader.inl @@ -55,6 +55,12 @@ binary_reader& operator>>(binary_reader& reader, T& x) noexcept return reader; } +template +void operator<<(T& x, binary_reader& reader) noexcept +{ + x = reader.template read(); +} + template template constexpr auto binary_reader::read_asciiz_string() noexcept diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index eb775e77..2fff13b7 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -49,8 +49,8 @@ void reader_state::read_atlases(reader_t& s) for (atlasid i = 0; i < N; i++) { Vector2ub size; - s >> size[0]; - s >> size[1]; + size[0] << s; + size[1] << s; const auto& [buf, len] = s.read_asciiz_string(); atlases[i] = loader.tile_atlas({buf, len}, size); } @@ -59,7 +59,7 @@ void reader_state::read_atlases(reader_t& s) template bool read_scenery_flags(binary_reader& s, scenery& sc) { - std::uint8_t flags; s >> flags; + std::uint8_t flags; flags << s; sc.passable = !!(flags & 1 << 0); sc.blocks_view = !!(flags & 1 << 1); sc.active = !!(flags & 1 << 2); @@ -70,17 +70,17 @@ bool read_scenery_flags(binary_reader& s, scenery& sc) void reader_state::read_sceneries(reader_t& s) { - std::uint16_t magic; s >> magic; + std::uint16_t magic; magic << s; if (magic != scenery_magic) fm_abort("bad scenery magic"); - atlasid sz; s >> sz; + atlasid sz; sz << s; fm_assert(sz < scenery_id_max); sceneries.resize(sz); std::size_t i = 0; while (i < sz) { - std::uint8_t num; s >> num; + std::uint8_t num; num << s; fm_assert(num > 0); auto str = s.read_asciiz_string(); auto it = default_sceneries.find(StringView{str.buf, str.len}); @@ -88,7 +88,7 @@ void reader_state::read_sceneries(reader_t& s) fm_abort("can't find scenery '%s'", str.buf); for (std::size_t n = 0; n < num; n++) { - atlasid id; s >> id; + atlasid id; id << s; fm_assert(id < sz); scenery_proto sc = it->second->proto; bool short_frame = read_scenery_flags(s, sc.frame); @@ -96,7 +96,7 @@ void reader_state::read_sceneries(reader_t& s) if (short_frame) sc.frame.frame = s.read(); else - s >> sc.frame.frame; + sc.frame.frame << s; fm_assert(sc.frame.frame < sc.atlas->info().nframes); sceneries[id] = sc; } @@ -122,12 +122,12 @@ void reader_state::read_chunks(reader_t& s) for (std::size_t k = 0; k < N; k++) { std::decay_t magic; - s >> magic; + magic << s; if (magic != chunk_magic) fm_abort("bad chunk magic"); chunk_coords coord; - s >> coord.x; - s >> coord.y; + coord.x << s; + coord.y << s; auto& chunk = (*_world)[coord]; for (std::size_t i = 0; i < TILE_COUNT; i++) { @@ -138,7 +138,7 @@ void reader_state::read_chunks(reader_t& s) auto id = flags & meta_short_atlasid ? atlasid{s.read()} : s.read(); variant_t v; if (PROTO >= 2) [[likely]] - s >> v; + v << s; else v = flags & meta_short_variant_ ? s.read() @@ -158,7 +158,7 @@ void reader_state::read_chunks(reader_t& s) if (PROTO >= 3) [[likely]] if (flags & meta_scenery) { - atlasid id; s >> id; + atlasid id; id << s; const bool exact = id & meta_long_scenery_bit; const auto r = rotation(id >> sizeof(id)*8-1-rotation_BITS & rotation_MASK); id &= ~scenery_id_flag_mask; @@ -171,9 +171,9 @@ void reader_state::read_chunks(reader_t& s) if (read_scenery_flags(s, sc.frame)) sc.frame.frame = s.read(); else - s >> sc.frame.frame; + sc.frame.frame << s; if (sc.frame.active) - s >> sc.frame.delta; + sc.frame.delta << s; } t.scenery() = sc; } @@ -198,7 +198,7 @@ void reader_state::deserialize_world(ArrayView buf) if (!!::memcmp(s.read().data(), file_magic, std::size(file_magic)-1)) fm_abort("bad magic"); proto_t proto; - s >> proto; + proto << s; if (!(proto >= min_proto_version && proto <= proto_version)) fm_abort("bad proto version '%zu' (should be between '%zu' and '%zu')", (std::size_t)proto, (std::size_t)min_proto_version, (std::size_t)proto_version); -- cgit v1.2.3