summaryrefslogtreecommitdiffhomepage
path: root/serialize/savegame.cpp
blob: 5eee17f2af47f79c3d13fc9e878b69919c0e8fde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include "binary-writer.inl"
#include "compat/defs.hpp"
#include "compat/strerror.hpp"
#include "compat/int-hash.hpp"
#include "loader/loader.hpp"
#include "src/world.hpp"

#include "atlas-type.hpp"
#include "src/anim-atlas.hpp"
#include "src/ground-atlas.hpp"
#include "src/wall-atlas.hpp"

#include "src/scenery.hpp"
#include "src/critter.hpp"
#include "src/light.hpp"

#include <cstring>
#include <compare>
#include <memory>
#include <vector>
#include <algorithm>
#include <Corrade/Utility/Path.h>
#include <tsl/robin_map.h>

#if 1
#ifdef __CLION_IDE__
#undef fm_assert
#define fm_assert(...) (void)(__VA_ARGS__)
#endif
#endif

namespace floormat::Serialize {

namespace {

using tilemeta = uint8_t;
using atlasid  = uint32_t;
using chunksiz = uint32_t;
using proto_t  = uint32_t;

constexpr inline proto_t proto_version      = 20;
constexpr inline auto file_magic            = ".floormat.save"_s;
constexpr inline auto chunk_magic           = (uint16_t)0xdead;
constexpr inline auto object_magic          = (uint16_t)0xb00b;
constexpr inline auto atlas_magic           = (uint16_t)0xbeef;
constexpr inline auto null_atlas            = (atlasid)-1;

struct FILE_raii final
{
    FILE_raii(FILE* s) noexcept : s{s} {}
    ~FILE_raii() noexcept { close(); }
    operator FILE*() noexcept { return s; }
    void close() noexcept { if (s) std::fclose(s); s = nullptr; }
private:
    FILE* s;
};

using floormat::Hash::fnvhash_buf;

template<typename T> T& non_const(const T& value) { return const_cast<T&>(value); }
template<typename T> T& non_const(T& value) = delete;
template<typename T> T& non_const(T&& value) = delete;
template<typename T> T& non_const(const T&& value) = delete;

constexpr size_t vector_initial_size = 128, hash_initial_size = vector_initial_size*2;

struct buffer
{
    std::unique_ptr<char[]> data;
    size_t size;

    bool empty() const { return size == 0; }
    buffer() : data{nullptr}, size{0} {}
    buffer(size_t len) : // todo use allocator
        data{std::make_unique<char[]>(len)},
        size{len}
    {
#if !fm_ASAN
        std::memset(&data[0], 0xfe, size);
#endif
    }
};

struct serialized_atlas
{
    buffer buf;
    const void* atlas;
    atlas_type type;
};

struct serialized_chunk
{
    buffer buf{};
    chunk* c;
};

template<typename Derived>
struct visitor_
{
    template<typename T, typename F>
    CORRADE_ALWAYS_INLINE void do_visit_nonconst(const T& value, F&& fun)
    {
        do_visit(non_const(value), fun);
    }

    template<typename T, typename F>
    CORRADE_ALWAYS_INLINE void do_visit(T&& value, F&& fun)
    {
        static_cast<Derived&>(*this).visit(value, fun);
    }

    template<typename T, typename F>
    requires (std::is_arithmetic_v<T> && std::is_fundamental_v<T>)
    static void visit(T& x, F&& f)
    {
        f(x);
    }

    template<typename T, size_t N, typename F>
    void visit(Math::Vector<N, T>& x, F&& f)
    {
        for (uint32_t i = 0; i < N; i++)
            do_visit(x.data()[i], f);
    }

    template<typename E, typename F>
    requires std::is_enum_v<E>
    void visit(E& x, F&& f)
    {
        auto* ptr = const_cast<std::underlying_type_t<E>*>(reinterpret_cast<const std::underlying_type_t<E>*>(&x));
        do_visit(*ptr, f);
    }

    template<typename F>
    void visit(object& obj, F&& f)
    {
        auto type = obj.type();

        do_visit(obj.id, f);
        do_visit(type, f);
        fm_assert(obj.atlas);
        do_visit(*obj.atlas, f);
        //do_visit(*obj.c, f);
        do_visit(obj.coord.local(), f);
        do_visit_nonconst(obj.offset, f);
        do_visit_nonconst(obj.bbox_offset, f);
        do_visit_nonconst(obj.bbox_size, f);
        do_visit_nonconst(obj.delta, f);
        do_visit_nonconst(obj.frame, f);
        do_visit_nonconst(obj.r, f);
        do_visit_nonconst(obj.pass, f);

        switch (type)
        {
        case object_type::critter: do_visit(static_cast<critter&>(obj), f); return;
        case object_type::scenery: do_visit(static_cast<scenery&>(obj), f); return;
        case object_type::light:   do_visit(static_cast<light&>(obj), f); return;
        //case object_type::door:   do_visit(static_cast<door&>(obj), f); return;
        case object_type::COUNT:
        case object_type::none:
            break;
        }
        fm_abort("invalid object type '%d'", (int)obj.type());
    }

    template<typename F>
    void visit(tile_ref c, F&& f)
    {
        do_visit(c.ground(), f);
        do_visit(c.wall_north(), f);
        do_visit(c.wall_west(), f);
    }
};

struct string_hasher
{
    inline size_t operator()(StringView s) const
    {
        return fnvhash_buf(s.data(), s.size());
    }
};

struct writer final : visitor_<writer>
{
    const world& w;

    std::vector<StringView> string_array{};
    tsl::robin_map<StringView, uint32_t, string_hasher> string_map{hash_initial_size};

    std::vector<serialized_atlas> atlas_array{};
    tsl::robin_map<const void*, uint32_t> atlas_map{hash_initial_size};

    std::vector<serialized_chunk> chunk_array{};

    buffer header_buf{}, string_buf{};

    writer(const world& w) : w{w} { } // added to avoid spurious warning until GCC 14: warning: missing initializer for member ::<anonymous>

    struct size_counter
    {
        size_t& size;

        template<typename T>
        requires (std::is_arithmetic_v<T> && std::is_fundamental_v<T>)
        void operator()(T) { size += sizeof(T); }
    };

    struct byte_writer
    {
        binary_writer<char*>& s;

        template<typename T>
        requires (std::is_fundamental_v<T> && std::is_arithmetic_v<T>)
        void operator()(T value)
        {
            s << value;
        }
    };

    using visitor_<writer>::visit;

    template<typename F>
    void visit(critter& obj, F&& f)
    {
        uint8_t flags = 0;
        flags |= (1 << 0) * obj.playable;
        do_visit(flags, f);
        do_visit(obj.name, f);
        do_visit(obj.offset_frac, f);
    }

    template<typename F>
    void visit(scenery& obj, F&& f)
    {
        auto sc_type = obj.sc_type;
        do_visit(sc_type, f);
        uint8_t flags = 0;
        flags |= obj.active      * (1 << 0);
        flags |= obj.closing     * (1 << 1);
        flags |= obj.interactive * (1 << 2);
        do_visit(flags, f);
    }

    template<typename F>
    void visit(light& obj, F&& f)
    {
        do_visit(obj.max_distance, f);
        do_visit(obj.color, f);
        auto falloff = obj.falloff;
        do_visit(falloff, f);
        uint8_t flags = 0;
        flags |= obj.enabled * (1 << 0);
        do_visit(flags, f);
    }

    template<typename F>
    void intern_atlas_(void* atlas, atlas_type type, F&& f)
    {
        do_visit(atlas_magic, f);
        do_visit(type, f);

        StringView name;

        switch (type)
        {
        case atlas_type::ground: name = reinterpret_cast<const ground_atlas*>(atlas)->name(); goto ok;
        case atlas_type::wall:   name = reinterpret_cast<const wall_atlas*>(atlas)->name(); goto ok;
        case atlas_type::object: name = reinterpret_cast<const anim_atlas*>(atlas)->name(); goto ok;
        case atlas_type::none: break;
        }
        fm_abort("invalid atlas type '%d'", (int)type);
ok:
    do_visit(intern_string(name), f);
    }

    [[nodiscard]] atlasid intern_atlas(void* atlas, atlas_type type)
    {
        atlas_array.reserve(vector_initial_size);
        fm_assert(atlas != nullptr);
        auto [kv, fresh] = atlas_map.try_emplace(atlas, (uint32_t)-1);
        if (!fresh)
        {
            fm_debug_assert(kv.value() != (uint32_t)-1);
            return kv->second;
        }
        else
        {
            size_t len = 0;
            intern_atlas_(atlas, type, size_counter{len});
            fm_assert(len > 0);

            buffer buf{len};
            binary_writer<char*> s{&buf.data[0], buf.size};
            intern_atlas_(atlas, type, byte_writer{s});
            auto id = (uint32_t)atlas_array.size();
            fm_assert(s.bytes_written() == s.bytes_allocated());
            atlas_array.emplace_back(std::move(buf), atlas, type);
            kv.value() = id;
            fm_assert(id != null_atlas);
            return atlasid{id};
        }
    }

    atlasid maybe_intern_atlas(void* atlas, atlas_type type)
    {
        if (!atlas)
            return null_atlas;
        else
            return intern_atlas(atlas, type);
    }

    atlasid intern_string(StringView str)
    {
        string_array.reserve(vector_initial_size);
        auto [kv, found] = string_map.try_emplace({str}, (uint32_t)-1);
        if (found)
            return kv.value();
        else
        {
            auto id = (uint32_t)string_array.size();
            string_array.emplace_back(str);
            kv.value() = id;
            fm_assert(id != null_atlas);
            return atlasid{id};
        }
    }

    template<typename F>
    void serialize_objects_(chunk& c, F&& f)
    {
        f((uint32_t)c.objects().size());

        for (const std::shared_ptr<object>& obj : c.objects())
        {
            fm_assert(obj != nullptr);
            do_visit(object_magic, f);
            do_visit(*obj, f);
        }
    }

    template<typename F>
    void serialize_tile_(tile_ref t, F&& f)
    {
        auto g = maybe_intern_atlas(t.ground_atlas().get(), atlas_type::ground),
             n = maybe_intern_atlas(t.wall_north_atlas().get(), atlas_type::wall),
             w = maybe_intern_atlas(t.wall_west_atlas().get(), atlas_type::wall);
        do_visit(g, f);
        do_visit(n, f);
        do_visit(w, f);
        if (g != null_atlas) do_visit(t.ground().variant, f);
        if (n != null_atlas) do_visit(t.wall_north().variant, f);
        if (w != null_atlas) do_visit(t.wall_west().variant, f);
    }

    void serialize_chunk_(chunk& c, buffer& buf)
    {
        const auto fn = [this](chunk& c, auto&& f) {
            do_visit(chunk_magic, f);
            do_visit(c.coord(), f);
            for (uint32_t i = 0; i < TILE_COUNT; i++)
                serialize_tile_(c[i], f);
            serialize_objects_(c, f);
        };

        size_t len = 0;
        auto ctr = size_counter{len};
        fn(c, ctr);
        fm_assert(len > 0);

        buf = buffer{len};
        binary_writer<char*> s{&buf.data[0], buf.size};
        byte_writer b{s};
        fn(c, b);
        fm_assert(s.bytes_written() == s.bytes_allocated());
    }

    template<typename F> void serialize_header_(F&& f)
    {
        fm_assert(header_buf.empty());
        for (char c : file_magic)
            f(c);
        f(proto_version);
        auto nstrings = (uint32_t)string_array.size(),
             natlases = (uint32_t)atlas_array.size(),
             nchunks  = (uint32_t)chunk_array.size();
        do_visit(nstrings, f);
        do_visit(natlases, f);
        do_visit(nchunks, f);
    }

    void serialize_strings_()
    {
        fm_assert(string_buf.empty());
        size_t len = 0;
        len += sizeof uint32_t{};
        for (const auto& s : string_array)
            len += s.size() + 1;
        buffer buf{len};
        binary_writer b{&buf.data[0], buf.size};
        b << (uint32_t)string_array.size();
        for (const auto& s : string_array)
            b.write_asciiz_string(s);
        fm_assert(b.bytes_written() == b.bytes_allocated());
        string_buf = std::move(buf);
    }

    void serialize_world()
    {
        fm_assert(string_array.empty());
        fm_assert(atlas_array.empty());
        fm_assert(chunk_array.empty());
        fm_assert(header_buf.empty());
        fm_assert(string_buf.empty());

        for (auto& [coord, c] : non_const(w.chunks()))
            chunk_array.push_back({.c = &c });

        std::sort(chunk_array.begin(), chunk_array.end(), [](const auto& c1, const auto& c2) {
            auto a = c1.c->coord(), b = c2.c->coord();
            return std::tuple{a.z, a.y, a.x} <=> std::tuple{b.z, b.y, b.x} == std::strong_ordering::less;
        });

        for (uint32_t i = 0; auto& [coord, c] : chunk_array)
            serialize_chunk_(*c, chunk_array[i++].buf);

        {
            size_t len = 0;
            serialize_header_(size_counter{len});
            fm_assert(len > 0);

            buffer hdr{len};
            binary_writer s{&hdr.data[0], hdr.size};
            serialize_header_(byte_writer{s});
            fm_assert(s.bytes_written() == s.bytes_allocated());
            header_buf = std::move(hdr);
        }

        serialize_strings_();
    }

    template<typename F>
    void visit(anim_atlas& a, F&& f)
    {
        atlasid id = intern_atlas(&a, atlas_type::object);
        do_visit(id, f);
    }

    template<typename F> void visit(const chunk_coords_& coord, F&& f)
    {
        f(coord.x);
        f(coord.y);
        f(coord.z);
    }

    template<typename F> void visit(const local_coords& pt, F&& f)
    {
        f(pt.to_index());
    }

    template<typename F> void visit(StringView name, F&& f)
    {
        f(intern_string(name));
    }
};

void my_fwrite(FILE_raii& f, const buffer& buf, char(&errbuf)[128])
{
    auto len = ::fwrite(&buf.data[0], buf.size, 1, f);
    int error = errno;
    if (len != 1)
        fm_abort("fwrite: %s", get_error_string(errbuf, error).data());
}

} // namespace

} // namespace floormat::Serialize

namespace floormat {

void world::serialize(StringView filename)
{
    using namespace floormat::Serialize;

    collect(true);
    char errbuf[128];
    fm_assert(filename.flags() & StringViewFlag::NullTerminated);
    if (Path::exists(filename))
        Path::remove(filename);
    FILE_raii file = ::fopen(filename.data(), "wb");
    if (!file)
    {
        int error = errno;
        fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), get_error_string(errbuf, error).data());
    }
    {
        struct writer writer{*this};
        const bool is_empty = chunks().empty();
        writer.serialize_world();
        fm_assert(!writer.header_buf.empty());
        if (!is_empty)
        {
            fm_assert(!writer.string_buf.empty());
            fm_assert(!writer.string_array.empty());
            fm_assert(!writer.string_map.empty());
            fm_assert(!writer.atlas_array.empty());
            fm_assert(!writer.atlas_map.empty());
            fm_assert(!writer.chunk_array.empty());
        }
        my_fwrite(file, writer.header_buf, errbuf);
        my_fwrite(file, writer.string_buf, errbuf);
        for (const auto& x : writer.atlas_array)
        {
            fm_assert(!x.buf.empty());
            my_fwrite(file, x.buf, errbuf);
        }
        for (const auto& x : writer.chunk_array)
        {
            fm_assert(!x.buf.empty());
            my_fwrite(file, x.buf, errbuf);
        }
    }

    if (int ret = ::fflush(file); ret != 0)
    {
        int error = errno;
        fm_abort("fflush: %s", get_error_string(errbuf, error).data());
    }
}

} // namespace floormat