diff options
-rw-r--r-- | serialize/world-reader.cpp | 10 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 30 |
2 files changed, 27 insertions, 13 deletions
diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index dc698765..df845732 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -33,7 +33,7 @@ private: std::vector<scenery_proto> sceneries; std::vector<std::shared_ptr<tile_atlas>> atlases; world* _world; - std::uint16_t PROTO = (std::uint16_t)-1; + std::uint16_t PROTO = 0; }; reader_state::reader_state(world& world) noexcept : _world{&world} {} @@ -204,8 +204,11 @@ void reader_state::read_chunks(reader_t& s) case entity_type::character: { character_proto proto; std::uint8_t id; id << s; - proto.frame = read_entity_flags(s, proto) ? s.read<std::uint8_t>() : s.read<std::uint16_t>(); proto.r = rotation(id >> sizeof(id)*8-1-rotation_BITS & rotation_MASK); + if (read_entity_flags(s, proto)) + proto.frame = s.read<std::uint8_t>(); + else + proto.frame << s; Vector2s offset_frac; offset_frac[0] << s; offset_frac[1] << s; @@ -246,6 +249,8 @@ void reader_state::read_chunks(reader_t& s) c.sort_entities(); + const auto nbytes = s.bytes_read(); + (void)nbytes; fm_assert(c.is_scenery_modified()); fm_assert(c.is_passability_modified()); } @@ -306,6 +311,7 @@ void reader_state::deserialize_world(ArrayView<const char> buf) fm_throw("bad proto version '{}' (should be between '{}' and '{}')"_cf, (std::size_t)proto, (std::size_t)min_proto_version, (std::size_t)proto_version); PROTO = proto; + fm_assert(PROTO > 0); std::uint64_t entity_counter = 0; read_atlases(s); if (PROTO >= 3) [[likely]] diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index f3af1c19..799e386f 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -351,8 +351,9 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) } } - s << (std::uint32_t)c.entities().size(); - fm_assert((std::uint32_t)c.entities().size() == c.entities().size()); + const auto entity_count = (std::uint32_t)c.entities().size(); + s << entity_count; + fm_assert(entity_count == c.entities().size()); for (const auto& e_ : c.entities()) { const auto& e = *e_; @@ -480,22 +481,27 @@ ArrayView<const char> writer_state::serialize_world() using proto_t = std::decay_t<decltype(proto_version)>; fm_assert(_world->size() <= int_max<chunksiz>); - std::size_t len = 0; - len += std::size(file_magic)-1; - len += sizeof(proto_t); - len += sizeof(std::uint64_t); - len += sizeof(chunksiz); - for (const auto& buf : chunk_bufs) - len += buf.size(); - len += atlas_buf.size(); - len += scenery_buf.size(); + const auto len = fm_begin( + auto len = 0_uz; + len += std::size(file_magic)-1; + len += sizeof(proto_t); + len += atlas_buf.size(); + len += scenery_buf.size(); + len += sizeof(std::uint64_t); + len += sizeof(chunksiz); + for (const auto& buf : chunk_bufs) + len += buf.size(); + return len; + ); file_buf.resize(len); + auto bytes_written = 0_uz; auto it = file_buf.begin(); const auto copy = [&](const auto& in) { auto len1 = std::distance(std::cbegin(in), std::cend(in)), len2 = std::distance(it, file_buf.end()); fm_assert(len1 <= len2); it = std::copy(std::cbegin(in), std::cend(in), it); + bytes_written += (std::size_t)len1; }; const auto copy_int = [&]<typename T>(const T& value) { union { T x; char bytes[sizeof x]; } c = {.x = maybe_byteswap(value)}; @@ -509,6 +515,8 @@ ArrayView<const char> writer_state::serialize_world() copy_int((chunksiz)_world->size()); for (const auto& buf : chunk_bufs) copy(buf); + fm_assert(file_buf.size() == bytes_written); + fm_assert(len == bytes_written); _world = nullptr; return {file_buf.data(), file_buf.size()}; } |