Skip to content

Commit

Permalink
Store the mainsave paths alongside autosave paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pankern committed Apr 7, 2023
1 parent 0f78a2c commit f1653e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
41 changes: 31 additions & 10 deletions source/creator/io/autosave.d
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
22 changes: 10 additions & 12 deletions source/creator/widgets/mainmenu.d
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit f1653e4

Please sign in to comment.