From f1653e48886ae0ddad49aa778f7f5bc5e2b0645f Mon Sep 17 00:00:00 2001 From: pankern <128001411+pankern@users.noreply.github.com> Date: Thu, 6 Apr 2023 21:40:53 -0700 Subject: [PATCH] Store the mainsave paths alongside autosave paths This is so that when we open an autosave from the recents menu, we can still correctly set the current project path to be the main save file instead of the autosave file, and the user can continue to press Ctrl+S to have changes saved in the expected location. --- source/creator/io/autosave.d | 41 +++++++++++++++++++++++-------- source/creator/widgets/mainmenu.d | 22 ++++++++--------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/source/creator/io/autosave.d b/source/creator/io/autosave.d index d85254c94..6af5d2798 100644 --- a/source/creator/io/autosave.d +++ b/source/creator/io/autosave.d @@ -55,28 +55,49 @@ void incSetAutosaveEnabled(bool enabled) { incSettingsSet("AutosaveEnabled", enabled); } -string[] incGetPrevAutosaves() { - return incSettingsGet!(string[])("prev_autosaves"); +struct AutosaveRecord { + string autosavePath; + string mainsavePath; +} + +AutosaveRecord[] incGetPrevAutosaves() { + AutosaveRecord[] saveRecords; + string[] autosavePaths = incSettingsGet!(string[])("prev_autosaves"); + string[] mainsavePaths = incSettingsGet!(string[])("prev_autosave_mainpaths"); + foreach (i, autosavePath; autosavePaths) { + string mainsavePath = ""; + if (i < mainsavePaths.length) { + mainsavePath = mainsavePaths[i]; + } + saveRecords ~= AutosaveRecord(autosavePath, mainsavePath); + } + return saveRecords; } -void incAddPrevAutosave(string path) { +void incAddPrevAutosave(string autosavePath) { import std.algorithm.searching : countUntil; import std.algorithm.mutation : remove; - string[] autosaves = incGetPrevAutosaves(); + AutosaveRecord[] saveRecords = incGetPrevAutosaves(); - ptrdiff_t idx = autosaves.countUntil(path); + ptrdiff_t idx = saveRecords.countUntil!"a.autosavePath == b"(autosavePath); if (idx >= 0) { - autosaves = autosaves.remove(idx); + saveRecords = saveRecords.remove(idx); } // Put project to the start of the "previous" list and // limit to 10 elements - autosaves = path.dup ~ autosaves; - //(currProjectPath) - if(autosaves.length > 10) autosaves.length = 10; + saveRecords = AutosaveRecord(autosavePath, incProjectPath()) ~ saveRecords; + if(saveRecords.length > 10) saveRecords.length = 10; // Then save. - incSettingsSet("prev_autosaves", autosaves); + string[] autosavePaths; + string[] mainsavePaths; + foreach (saveRecord; saveRecords) { + autosavePaths ~= saveRecord.autosavePath; + mainsavePaths ~= saveRecord.mainsavePath; + } + incSettingsSet("prev_autosaves", autosavePaths); + incSettingsSet("prev_autosave_mainpaths", mainsavePaths); incSettingsSave(); } diff --git a/source/creator/widgets/mainmenu.d b/source/creator/widgets/mainmenu.d index d32b69c74..75f7ef806 100644 --- a/source/creator/widgets/mainmenu.d +++ b/source/creator/widgets/mainmenu.d @@ -134,22 +134,20 @@ void incMainMenu() { } string[] prevProjects = incGetPrevProjects(); - string[] prevAutosaves = incGetPrevAutosaves(); + AutosaveRecord[] prevAutosaves = incGetPrevAutosaves(); if (igBeginMenu(__("Recent"), prevProjects.length > 0)) { import std.path : baseName; if (igBeginMenu(__("Autosaves"), prevAutosaves.length > 0)) { - foreach(autosave; prevAutosaves) { - if (igMenuItem(autosave.baseName.toStringz, "", false, true)) { - /** - TODO: we want to open autosaves a special way. - A way that opens that file up, - but sets the main project file properly. - To do this, we first need to store - autosaves as a tuple to contain the original - main project path. - */ + foreach(saveRecord; prevAutosaves) { + auto autosavePath = saveRecord.autosavePath.baseName.toStringz; + if (igMenuItem(autosavePath, "", false, true)) { + incPopWelcomeWindow(); + incOpenProject( + saveRecord.mainsavePath, + saveRecord.autosavePath + ); } - incTooltip(autosave); + incTooltip(saveRecord.autosavePath); } igEndMenu(); }