diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-26 21:07:33 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-26 21:59:08 +0200 |
commit | 15e25836d4097c0179f6ed18d91611e3a0250664 (patch) | |
tree | a602892b3ec6d527b59596766e00e89fe7ee8051 /editor/json.cpp | |
parent | 07f9e1834243783a8baa51da26869582214c6880 (diff) |
add quicksave and quickload!
Diffstat (limited to 'editor/json.cpp')
-rw-r--r-- | editor/json.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/editor/json.cpp b/editor/json.cpp new file mode 100644 index 00000000..7a64b207 --- /dev/null +++ b/editor/json.cpp @@ -0,0 +1,57 @@ +#include "app.hpp" +#include "floormat/main.hpp" +#include "src/world.hpp" +#include "serialize/json-helper.hpp" +#include "serialize/tile-atlas.hpp" +#include "serialize/world.hpp" +#include <Corrade/Utility/Path.h> + +namespace floormat { + +#define save_dir "../save" +#define quicksave_file save_dir "/" "quicksave.json" +#define quicksave_tmp quicksave_file ".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); + json_helper::to_json(world, quicksave_tmp); + Path::move(quicksave_tmp, quicksave_file); + fputs(" done\n", stderr); fflush(stderr); +} + +void app::do_quickload() +{ + ensure_save_directory(); + if (!Path::exists(quicksave_file)) + { + fm_warn("no quicksave"); + return; + } + auto& world = M->world(); + fputs("quickload...", stderr); fflush(stderr); + world = json_helper::from_json<struct world>(quicksave_file); + fputs(" done\n", stderr); fflush(stderr); +} + +} // namespace floormat |