From 53de92c932168d1e44139533f2a4f3b5f1a34114 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 2 Mar 2025 10:27:58 -0600 Subject: [PATCH] autosave loading --- src/game/boe.dlgutil.cpp | 46 ++++++++++++++++++++++++++++++++++------ src/game/boe.dlgutil.hpp | 2 +- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/game/boe.dlgutil.cpp b/src/game/boe.dlgutil.cpp index f6d317c1..ce841b43 100644 --- a/src/game/boe.dlgutil.cpp +++ b/src/game/boe.dlgutil.cpp @@ -1730,7 +1730,7 @@ class cFilePicker { fs::path save_folder; bool picking_auto; bool saving; - cDialog me{*ResMgr::dialogs.get("pick-save")}; + cDialog me; cStack& get_stack() { return dynamic_cast(me["list"]); } std::string template_info_str; @@ -1830,8 +1830,23 @@ class cFilePicker { me["save" + suffix].attachClickHandler(std::bind(&cFilePicker::doSave, this, file)); }else{ me["load" + suffix].attachClickHandler(std::bind(&cFilePicker::doLoad, this, file)); - // TODO check if a newer autosave exists - me["auto" + suffix + "-more-recent"].hide(); + + std::vector> auto_mtimes; + fs::path auto_folder = file; + if(!picking_auto){ + auto_folder.replace_extension(".auto"); + if(fs::is_directory(auto_folder)) auto_mtimes = sorted_file_mtimes(auto_folder); + } + if(auto_mtimes.empty()){ + me["auto" + suffix].hide(); + me["auto" + suffix + "-more-recent"].hide(); + }else{ + // If an autosave is newer than the main save, show an indicator + if(std::difftime(mtime, auto_mtimes.front().second) > 0) + me["auto" + suffix + "-more-recent"].show(); + + me["auto" + suffix].attachClickHandler(std::bind(&cFilePicker::showAuto, this, auto_folder)); + } } } @@ -1866,6 +1881,13 @@ class cFilePicker { ++pages_populated; } + bool showAuto(fs::path auto_folder) { + fs::path autosave = run_autosave_picker(auto_folder, &me); + if(!autosave.empty()) + doLoad(autosave); + return true; + } + bool doLoad(fs::path selected_file) { me.setResult(selected_file); me.toast(false); @@ -1927,7 +1949,8 @@ class cFilePicker { } public: - cFilePicker(fs::path save_folder, bool saving, bool picking_auto = false) : + cFilePicker(fs::path save_folder, bool saving, cDialog* parent = nullptr, bool picking_auto = false) : + me(*ResMgr::dialogs.get("pick-save"), parent), save_folder(save_folder), picking_auto(picking_auto), saving(saving), @@ -1938,11 +1961,22 @@ public: if(saving){ me["title-load"].hide(); + me["title-auto"].hide(); me["file1"].setText(""); // Keep the frame for(int i = 0; i < SLOTS_PER_PAGE; ++i){ me["load" + std::to_string(i+1)].hide(); } }else{ + if(picking_auto){ + me["title-load"].hide(); + std::string title = me["title-auto"].getText(); + fs::path party_name = save_folder.filename(); + party_name.replace_extension(); + boost::replace_first(title, "{Folder}", party_name.string()); + me["title-auto"].setText(title); + }else{ + me["title-auto"].hide(); + } me["title-save"].hide(); me["file1-field"].hide(); me["file1-extension-label"].hide(); @@ -1979,8 +2013,8 @@ static fs::path run_file_picker(fs::path save_folder, bool saving) { return cFilePicker(save_folder, saving).run(); } -fs::path run_autosave_picker(fs::path save_file) { - return ""; +fs::path run_autosave_picker(fs::path auto_folder, cDialog* parent) { + return cFilePicker(auto_folder, false, parent, true).run(); } fs::path run_file_picker(bool saving) { diff --git a/src/game/boe.dlgutil.hpp b/src/game/boe.dlgutil.hpp index e97ee5bf..b7154bae 100644 --- a/src/game/boe.dlgutil.hpp +++ b/src/game/boe.dlgutil.hpp @@ -27,6 +27,6 @@ void tip_of_day(); struct scen_header_type pick_a_scen(); fs::path run_file_picker(bool saving); // Pick from the autosaves made while playing in a given save file -fs::path run_autosave_picker(fs::path save_file); +fs::path run_autosave_picker(fs::path save_file, cDialog* parent = nullptr); #endif