summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-01-22 14:48:36 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-01-22 14:48:36 +0100
commit49f455887a29a2a879e330dca9e050296e617720 (patch)
treee52c2cba90adbae6b2868576e03919f437b6a649 /serialize
parentb35f469d1a22d410fe0013edf865e2cb6e6de916 (diff)
w
Diffstat (limited to 'serialize')
-rw-r--r--serialize/savegame.cpp120
1 files changed, 61 insertions, 59 deletions
diff --git a/serialize/savegame.cpp b/serialize/savegame.cpp
index 5a86bebd..af7e223a 100644
--- a/serialize/savegame.cpp
+++ b/serialize/savegame.cpp
@@ -33,7 +33,13 @@
#endif
#endif
-namespace floormat::Serialize {
+namespace floormat {
+
+using floormat::Serialize::binary_reader;
+using floormat::Serialize::binary_writer;
+using floormat::Serialize::atlas_type;
+using floormat::Serialize::maybe_byteswap;
+using floormat::Hash::fnvhash_buf;
namespace {
@@ -47,8 +53,6 @@ private:
FILE* s;
};
-using floormat::Hash::fnvhash_buf;
-
struct string_hasher
{
inline size_t operator()(StringView s) const
@@ -512,56 +516,6 @@ ok:
}
};
-struct reader final : visitor_<reader>
-{
- std::vector<StringView> strings;
- proto_t PROTO = (proto_t)-1;
- uint32_t nstrings = 0, natlases = 0, nchunks = 0;
-
- class world& w;
- reader(class world& w) : w{w} {}
-
- void deserialize_header_(binary_reader<const char*>& s)
- {
- fm_assert(PROTO == (proto_t)-1);
- auto magic = s.read<file_magic.size()>();
- fm_soft_assert(StringView{magic.data(), magic.size()} == file_magic);
- PROTO << s;
- fm_soft_assert(PROTO >= proto_version_min);
- fm_soft_assert(PROTO <= proto_version);
- nstrings << s;
- natlases << s;
- nchunks << s;
- }
-
- StringView get_string(atlasid id)
- {
- fm_soft_assert(id < strings.size());
- return strings[id];
- }
-
- void deserialize_strings_(binary_reader<const char*>& s)
- {
- fm_assert(strings.empty());
- strings.reserve(nstrings);
- for (uint32_t i = 0; i < nstrings; i++)
- {
- auto str = s.read_asciiz_string_();
- strings.emplace_back(str);
- }
- }
-
- void deserialize_world(ArrayView<const char> buf)
- {
- binary_reader s{buf.data(), buf.data() + buf.size()};
- deserialize_header_(s);
- deserialize_strings_(s);
- // atlases
- // chunks
- // assert end
- }
-};
-
void my_fwrite(FILE_raii& f, const buffer& buf, char(&errbuf)[128])
{
auto len = std::fwrite(&buf.data[0], buf.size, 1, f);
@@ -572,12 +526,6 @@ void my_fwrite(FILE_raii& f, const buffer& buf, char(&errbuf)[128])
} // namespace
-} // namespace floormat::Serialize
-
-namespace floormat {
-
-using namespace floormat::Serialize;
-
void world::serialize(StringView filename)
{
collect(true);
@@ -626,6 +574,60 @@ void world::serialize(StringView filename)
}
}
+namespace {
+
+struct reader final : visitor_<reader>
+{
+ std::vector<StringView> strings;
+ proto_t PROTO = (proto_t)-1;
+ uint32_t nstrings = 0, natlases = 0, nchunks = 0;
+
+ class world& w;
+ reader(class world& w) : w{w} {}
+
+ void deserialize_header_(binary_reader<const char*>& s)
+ {
+ fm_assert(PROTO == (proto_t)-1);
+ auto magic = s.read<file_magic.size()>();
+ fm_soft_assert(StringView{magic.data(), magic.size()} == file_magic);
+ PROTO << s;
+ fm_soft_assert(PROTO >= proto_version_min);
+ fm_soft_assert(PROTO <= proto_version);
+ nstrings << s;
+ natlases << s;
+ nchunks << s;
+ }
+
+ StringView get_string(atlasid id)
+ {
+ fm_soft_assert(id < strings.size());
+ return strings[id];
+ }
+
+ void deserialize_strings_(binary_reader<const char*>& s)
+ {
+ fm_assert(strings.empty());
+ strings.reserve(nstrings);
+ for (uint32_t i = 0; i < nstrings; i++)
+ {
+ auto str = s.read_asciiz_string_();
+ strings.emplace_back(str);
+ }
+ }
+
+ void deserialize_world(ArrayView<const char> buf)
+ {
+ binary_reader s{buf.data(), buf.data() + buf.size()};
+ deserialize_header_(s);
+ deserialize_strings_(s);
+ // atlases
+ // chunks
+ // assert end
+ }
+};
+
+} // namespace
+
class world world::deserialize(StringView filename) noexcept(false)
{
char errbuf[128];