summaryrefslogtreecommitdiffhomepage
path: root/editor/save.cpp
blob: 35cdf9e79b9494926d9c02dfc80c30187429267d (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
#include "app.hpp"
#include "floormat/main.hpp"
#include "src/world.hpp"

#include <filesystem>
#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"

namespace Path = Corrade::Utility::Path;
using std::filesystem::path;

static bool ensure_save_directory()
{
    if (Path::make(save_dir))
        return true;
    else
    {
        fm_warn("failed to create save directory '%s'", save_dir);
        return false;
    }
}

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

void app::do_quickload()
{
    if (!ensure_save_directory())
        return;
    if (!Path::exists(quicksave_file))
    {
        fm_warn("no quicksave");
        return;
    }
    fputs("quickload... ", stderr); fflush(stderr);
    M->world() = world::deserialize(quicksave_file);
    fputs("done\n", stderr); fflush(stderr);
}

} // namespace floormat