diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-29 00:54:19 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-29 00:54:19 +0200 |
commit | b6a42cc53f808c86342d1bcd400ea95e6e7f5762 (patch) | |
tree | cd65334638415d037e5544c9af5142eab3d6bc7a /serialize | |
parent | 9954b8b4f5fb95470e127a4f24a0c73289dd49a9 (diff) |
serializer work
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/binary-reader.hpp | 3 | ||||
-rw-r--r-- | serialize/binary-reader.inl | 25 | ||||
-rw-r--r-- | serialize/world-reader.cpp | 7 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 5 |
4 files changed, 27 insertions, 13 deletions
diff --git a/serialize/binary-reader.hpp b/serialize/binary-reader.hpp index 7eed2145..4876e0dc 100644 --- a/serialize/binary-reader.hpp +++ b/serialize/binary-reader.hpp @@ -45,7 +45,8 @@ struct binary_reader final { template<typename T> T read() noexcept;
template<std::size_t N> constexpr std::array<char, N> read() noexcept;
constexpr std::size_t bytes_read() const noexcept { return num_bytes_read; }
- constexpr StringView read_asciiz_string() noexcept;
+ template<std::size_t Max>
+ auto read_asciiz_string() noexcept;
private:
std::size_t num_bytes_read = 0;
diff --git a/serialize/binary-reader.inl b/serialize/binary-reader.inl index ad2d59bc..b3d04109 100644 --- a/serialize/binary-reader.inl +++ b/serialize/binary-reader.inl @@ -84,13 +84,26 @@ binary_reader<It>& operator>>(binary_reader<It>& reader, T& x) noexcept }
template<string_input_iterator It>
-constexpr StringView binary_reader<It>::read_asciiz_string() noexcept
+template<std::size_t MAX>
+auto binary_reader<It>::read_asciiz_string() noexcept
{
- const It pos = it;
- while (it != end)
- if (char c = *it++; c == '\0')
- return StringView{pos, (std::size_t)std::distance(pos, end), StringViewFlag::NullTerminated};
- fm_abort("unexpected EOF while reading a string");
+ struct fixed_string final {
+ char buf[MAX];
+ std::size_t len;
+ };
+
+ fixed_string ret;
+ for (std::size_t i = 0; i < MAX-1 && it != end; i++)
+ {
+ const char c = *it++;
+ ret.buf[i] = c;
+ if (c == '\0')
+ {
+ ret.len = i;
+ return ret;
+ }
+ }
+ fm_abort("can't find string terminator");
}
} // namespace floormat::Serialize
diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index 4b5d365c..4c67031e 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -35,7 +35,8 @@ void reader_state::read_atlases(reader_t& s) Vector2ub size;
s >> size[0];
s >> size[1];
- atlases[i] = loader.tile_atlas(s.read_asciiz_string(), size);
+ auto str = s.read_asciiz_string<atlas_name_max>();
+ atlases[i] = loader.tile_atlas({str.buf, str.len}, size);
}
}
@@ -67,9 +68,10 @@ void reader_state::read_chunks(reader_t& s) const auto make_atlas = [&]() -> tile_image {
auto atlas = lookup_atlas(s.read<atlasid>());
auto id = s.read<imgvar>();
+ fm_assert(id < atlas->num_tiles());
return { atlas, id };
};
- tile t;
+ tile& t = chunk[i];
if (flags & meta_ground)
t.ground_image = make_atlas();
if (flags & meta_wall_n)
@@ -86,7 +88,6 @@ void reader_state::read_chunks(reader_t& s) default:
fm_abort("bad pass mode '%zu' for tile %zu", i, (std::size_t)x);
}
- chunk[i] = std::move(t);
}
}
}
diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index faf60849..e1750fb2 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -39,7 +39,7 @@ private: std::unordered_map<const void*, interned_atlas> tile_images;
};
-constexpr auto tile_size = sizeof(tilemeta) + sizeof(atlasid)*3;
+constexpr auto tile_size = sizeof(tilemeta) + (sizeof(atlasid) + sizeof(imgvar))*3;
constexpr auto chunkbuf_size =
sizeof(chunk_magic) + sizeof(chunk_coords) + tile_size * TILE_COUNT;
@@ -88,8 +88,6 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) {
const tile& x = c[i];
- [[maybe_unused]] constexpr auto tile_size = sizeof(tilemeta) + (sizeof(atlasid) + sizeof(imgvar))*3;
-
fm_debug_assert(s.bytes_written() + tile_size <= chunkbuf_size);
auto img_g = maybe_intern_atlas(x.ground_image);
@@ -145,6 +143,7 @@ void writer_state::serialize_atlases() s << sz2[0]; s << sz2[1];
s.write_asciiz_string(name);
}
+ atlas_buf.resize(s.bytes_written());
fm_assert(s.bytes_written() <= atlasbuf_size);
}
|