summaryrefslogtreecommitdiffhomepage
path: root/editor/save.cpp
blob: 2c073e014716c538081cd827c58cf4a01cbddc6f (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
#include "app.hpp"
#include "floormat/main.hpp"
#include "src/world.hpp"
#include "loader/loader.hpp"
#include <cstdio>
#include <Corrade/Containers/String.h>
#include <Corrade/Utility/Path.h>

namespace floormat {

#define save_dir "save"
#define quicksave_file save_dir "/" "quicksave.dat"
#define quicksave_tmp save_dir "/" "quicksave.tmp"

static bool ensure_save_directory()
{
    auto dir = Path::join(loader.TEMP_PATH, save_dir);
    if (Path::make(save_dir))
    {
        fm_assert(Path::exists(Path::join(loader.TEMP_PATH, "CMakeCache.txt"_s)));
        return true;
    }
    else
    {
        fm_warn("failed to create save directory '%s'", save_dir);
        return false;
    }
}

void app::do_quicksave()
{
    auto file = Path::join(loader.TEMP_PATH, quicksave_file);
    auto tmp = Path::join(loader.TEMP_PATH, quicksave_tmp);
    if (!ensure_save_directory())
        return;
    auto& world = M->world();
    world.collect(true);
    if (Path::exists(tmp))
        Path::remove(tmp);
    std::fputs("quicksave... ", stderr); std::fflush(stderr);
    world.serialize(tmp);
    Path::move(tmp, file);
    std::fputs("done\n", stderr); std::fflush(stderr);
}

void app::do_quickload()
{
    auto file = Path::join(loader.TEMP_PATH, quicksave_file);
    if (!ensure_save_directory())
        return;
    if (!Path::exists(file))
    {
        fm_warn("no quicksave");
        return;
    }
    std::fputs("quickload... ", stderr); std::fflush(stderr);
    reset_world(world::deserialize(file, loader_policy::warn));
    std::fputs("done\n", stderr); std::fflush(stderr);
}

void app::do_new_file()
{
    reset_world();
    auto& w = M->world();
    maybe_initialize_chunk_(chunk_coords_{}, w[chunk_coords_{}]);
}

} // namespace floormat