extremely aggressive scenedit autosave

This commit is contained in:
2025-08-28 18:20:55 -05:00
parent 9dc75f0f94
commit 327ca54b60
6 changed files with 61 additions and 24 deletions

View File

@@ -33,7 +33,7 @@ bool mac_is_intel(){
}
return _mac_is_intel;
}
fs::path progDir, tempDir, scenDir, replayDir, saveDir;
fs::path progDir, tempDir, scenDir, replayDir, saveDir, edAutoDir;
// This is here to avoid unnecessarily duplicating it in platform-specific files.
cursor_type Cursor::current = sword_curs;
@@ -84,6 +84,9 @@ void init_directories(const char* exec_path) {
saveDir = tempDir/"Saves";
fs::create_directories(saveDir);
edAutoDir = tempDir/"EditorAuto";
fs::create_directories(edAutoDir);
add_resmgr_paths(tempDir/"data");
tempDir /= "Temporary Files";

View File

@@ -1101,32 +1101,52 @@ struct overrides_sheet {
}
};
const int MAX_ED_AUTOSAVE_DEFAULT = 10; // TODO make a pref
static fs::path next_autosave_path() {
extern fs::path edAutoDir;
auto ed_autosaves = sorted_file_mtimes(edAutoDir, {".boes"});
if(ed_autosaves.size() >= MAX_ED_AUTOSAVE_DEFAULT){
return ed_autosaves.back().first; // Reuse oldest auto slot
}else if(ed_autosaves.empty()){
return edAutoDir / "auto0.boes";
}else{
fs::path newest_auto = ed_autosaves[0].first;
int num = std::stoi(newest_auto.stem().string().substr(4)) + 1;
std::string temp = "auto" + std::to_string(num) + ".boes";
return edAutoDir / temp;
}
}
extern std::string scenario_temp_dir_name;
extern fs::path scenDir;
void save_scenario(bool rename) {
void save_scenario(bool rename, bool autosave) {
fs::path toFile = scenario.scen_file;
if(rename || toFile.empty()) {
fs::path def = scenario.scen_file;
if(def.empty())
def = scenDir/"myscenario.boes";
toFile = nav_put_scenario(def);
if(toFile.empty()) return;
}
if(fs::is_directory(toFile)) {
// Unpacked scenario
set_pref("LastScenario", (toFile / "header.exs").string());
}else{
set_pref("LastScenario", toFile.string());
}
save_prefs();
if(!autosave){
if(rename || toFile.empty()) {
fs::path def = scenario.scen_file;
if(def.empty())
def = scenDir/"myscenario.boes";
toFile = nav_put_scenario(def);
if(toFile.empty()) return;
}
extern cUndoList undo_list;
undo_list.save();
if(scenario.is_legacy && cChoiceDlog("save-legacy-scen", {"update", "cancel"}).show() == "update")
scenario.is_legacy = false;
if(fs::is_directory(toFile)) {
// Unpacked scenario
set_pref("LastScenario", (toFile / "header.exs").string());
}else{
set_pref("LastScenario", toFile.string());
}
save_prefs();
extern cUndoList undo_list;
undo_list.save();
if(scenario.is_legacy && cChoiceDlog("save-legacy-scen", {"update", "cancel"}).show() == "update")
scenario.is_legacy = false;
}
scenario.reset_version();
tarball scen_file;
{
@@ -1197,7 +1217,11 @@ void save_scenario(bool rename) {
writeDialogueToXml(ticpp::Printer("talk.xml", town_talk), scenario.towns[i]->talking, i);
}
change_made = false;
if(autosave){
toFile = next_autosave_path();
}else{
change_made = false;
}
// Alright. At this point, check to see if the scenario was unpacked.
if(fs::is_directory(toFile)) {

View File

@@ -1,5 +1,5 @@
void save_scenario(bool rename = false);
void save_scenario(bool rename = false, bool autosave = false);
void start_data_dump();
void scen_text_dump();

View File

@@ -194,6 +194,10 @@ static void launch_scenario(eLaunchType type) {
//Changed to ISO C specified argument and return type.
int main(int argc, char* argv[]) {
extern cUndoList undo_list;
undo_list.onChange = []() -> void {
save_scenario(false, true);
};
try {
init_scened(argc, argv);

View File

@@ -43,12 +43,14 @@ void cUndoList::undo(){
if(noUndo()) return;
(*cur)->undo();
cur++;
if(onChange) onChange();
}
void cUndoList::redo(){
if(noRedo()) return;
cur--;
(*cur)->redo();
if(onChange) onChange();
}
std::string cUndoList::undoName() const {
@@ -91,4 +93,6 @@ void cUndoList::add(action_ptr what){
theList.pop_back();
}
cur = theList.begin();
if(onChange) onChange();
}

View File

@@ -93,6 +93,8 @@ public:
/// dropped from the front of the list.
/// Actions are only dropped when adding new actions.
static size_t maxUndoSize;
/// Called every time the list is updated either by performing something new, or and undo/redo call (but not on clear)
std::function<void()> onChange;
};
// As a special convention, I will prefix non-abstract action classes with 'a' instead of 'c'