summaryrefslogtreecommitdiffhomepage
path: root/editor/save.cpp
blob: c00506a770e667af792ad7e27a527dc9b91eb51d (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
#include "app.hpp"
#include "floormat/main.hpp"
#include "src/world.hpp"
#include "loader/loader.hpp"
#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))
        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);
    fputs("quicksave... ", stderr); fflush(stderr);
    world.serialize(tmp);
    Path::move(tmp, file);
    fputs("done\n", stderr); 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;
    }
    fputs("quickload... ", stderr); fflush(stderr);
    reset_world(world::deserialize(file));
    fputs("done\n", stderr); fflush(stderr);
}

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

} // namespace floormat