summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-12-01 19:26:59 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-12-01 19:34:38 +0100
commit03917e76108e3d45e73a93905ffc05423dd672df (patch)
tree9b640e19c8b9cd893d10cd439b141ea574f67b8e /serialize
parentacecc8744c17dc266f9369ab99df2ced084abc9a (diff)
serialize, loader: simplify loading sceneries a bit
Diffstat (limited to 'serialize')
-rw-r--r--serialize/binary-reader.inl1
-rw-r--r--serialize/world-reader.cpp51
2 files changed, 26 insertions, 26 deletions
diff --git a/serialize/binary-reader.inl b/serialize/binary-reader.inl
index bb1f8cf7..d1abcec9 100644
--- a/serialize/binary-reader.inl
+++ b/serialize/binary-reader.inl
@@ -70,6 +70,7 @@ constexpr auto binary_reader<It>::read_asciiz_string() noexcept
struct fixed_string final {
char buf[MAX];
std::size_t len;
+ operator StringView() const noexcept { return { buf, len, StringViewFlag::NullTerminated }; }
};
fixed_string ret;
diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp
index 2fff13b7..d95f81b5 100644
--- a/serialize/world-reader.cpp
+++ b/serialize/world-reader.cpp
@@ -7,7 +7,6 @@
#include "src/tile-atlas.hpp"
#include "src/anim-atlas.hpp"
#include <cstring>
-#include <Corrade/Containers/StringStlHash.h>
namespace {
@@ -21,38 +20,31 @@ struct reader_state final {
private:
using reader_t = binary_reader<decltype(ArrayView<const char>{}.cbegin())>;
- void load_sceneries();
- std::shared_ptr<tile_atlas> lookup_atlas(atlasid id);
+ const std::shared_ptr<tile_atlas>& lookup_atlas(atlasid id);
+ const scenery_proto& lookup_scenery(atlasid id);
void read_atlases(reader_t& reader);
void read_sceneries(reader_t& reader);
void read_chunks(reader_t& reader);
- std::unordered_map<atlasid, std::shared_ptr<tile_atlas>> atlases;
- std::unordered_map<StringView, const serialized_scenery*> default_sceneries;
std::vector<scenery_proto> sceneries;
+ std::vector<std::shared_ptr<tile_atlas>> atlases;
world* _world;
std::uint16_t PROTO = (std::uint16_t)-1;
};
reader_state::reader_state(world& world) noexcept : _world{&world} {}
-void reader_state::load_sceneries()
-{
- for (const serialized_scenery& s : loader.sceneries())
- default_sceneries[s.name] = &s;
-}
-
void reader_state::read_atlases(reader_t& s)
{
const auto N = s.read<atlasid>();
- atlases.reserve(N * 2);
+ atlases.reserve(N);
for (atlasid i = 0; i < N; i++)
{
Vector2ub size;
size[0] << s;
size[1] << s;
const auto& [buf, len] = s.read_asciiz_string<atlas_name_max>();
- atlases[i] = loader.tile_atlas({buf, len}, size);
+ atlases.push_back(loader.tile_atlas({buf, len}, size));
}
}
@@ -70,6 +62,8 @@ bool read_scenery_flags(binary_reader<T>& s, scenery& sc)
void reader_state::read_sceneries(reader_t& s)
{
+ (void)loader.sceneries();
+
std::uint16_t magic; magic << s;
if (magic != scenery_magic)
fm_abort("bad scenery magic");
@@ -83,14 +77,13 @@ void reader_state::read_sceneries(reader_t& s)
std::uint8_t num; num << s;
fm_assert(num > 0);
auto str = s.read_asciiz_string<atlas_name_max>();
- auto it = default_sceneries.find(StringView{str.buf, str.len});
- if (it == default_sceneries.end())
- fm_abort("can't find scenery '%s'", str.buf);
+ const auto sc_ = loader.scenery(str);
for (std::size_t n = 0; n < num; n++)
{
atlasid id; id << s;
fm_assert(id < sz);
- scenery_proto sc = it->second->proto;
+ fm_assert(!sceneries[id]);
+ scenery_proto sc = sc_;
bool short_frame = read_scenery_flags(s, sc.frame);
fm_debug_assert(sc.atlas != nullptr);
if (short_frame)
@@ -103,16 +96,24 @@ void reader_state::read_sceneries(reader_t& s)
i += num;
}
fm_assert(i == sz);
- for (const scenery_proto& x : sceneries)
- fm_assert(x.atlas != nullptr);
+ for (const scenery_proto& sc : sceneries)
+ fm_assert(sc);
+}
+
+const std::shared_ptr<tile_atlas>& reader_state::lookup_atlas(atlasid id)
+{
+ if (id < atlases.size())
+ return atlases[id];
+ else
+ fm_abort("no such atlas: '%zu'", (std::size_t)id);
}
-std::shared_ptr<tile_atlas> reader_state::lookup_atlas(atlasid id)
+const scenery_proto& reader_state::lookup_scenery(atlasid id)
{
- if (auto it = atlases.find(id); it != atlases.end())
- return it->second;
+ if (id < sceneries.size())
+ return sceneries[id];
else
- fm_abort("not such atlas: '%zu'", (std::size_t)id);
+ fm_abort("no such scenery: '%zu'", (std::size_t)id);
}
void reader_state::read_chunks(reader_t& s)
@@ -162,8 +163,7 @@ void reader_state::read_chunks(reader_t& 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;
- fm_assert(id < sceneries.size());
- auto sc = sceneries[id];
+ auto sc = lookup_scenery(id);
(void)sc.atlas->group(r);
sc.frame.r = r;
if (!exact)
@@ -203,7 +203,6 @@ void reader_state::deserialize_world(ArrayView<const char> buf)
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);
PROTO = proto;
- load_sceneries();
read_atlases(s);
if (PROTO >= 3)
read_sceneries(s);