Skip to content

Commit

Permalink
improve profile saving with file swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed May 25, 2024
1 parent a2b340b commit 7d29ea1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
36 changes: 19 additions & 17 deletions Project-Aurora/Project-Aurora/Profiles/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,15 @@ public void DeleteProfile(ApplicationProfile? profile)
if (profile.Equals(Profile))
SwitchToProfile(Profiles[Math.Min(profileIndex, Profiles.Count - 1)]);

if (File.Exists(profile.ProfileFilepath))
if (!File.Exists(profile.ProfileFilepath)) return;
try
{
try
{
File.Delete(profile.ProfileFilepath);
}
catch (Exception exc)
{
Global.logger.Error(exc, "Could not delete profile with path \"{ProfileFilepath}\"", profile.ProfileFilepath);
}
File.Delete(profile.ProfileFilepath);
}
catch (Exception exc)
{
Global.logger.Error(exc, "Could not delete profile with path \"{ProfileFilepath}\"", profile.ProfileFilepath);
}

SaveProfiles();
}

private string GetValidFilename(string filename)
Expand Down Expand Up @@ -599,20 +595,26 @@ private async Task LoadProfileFile(string profilePath, bool selectedProfile)
{
var profileDir = Path.GetDirectoryName(profilePath);
var profileFileName = Path.GetFileName(profilePath);
// find auto-backup save files also ending with .json.bkX
var saveProfileFile = Directory.EnumerateFiles(profileDir, profileFileName + "*")
.Where(path => path.EndsWith(".json") || path.Contains(".json.bk"))
.Where(path => !path.EndsWith(".corrupted"))
.OrderBy(File.GetLastWriteTime)
.Order()
.Last();
var profileSettings = await LoadProfile(saveProfileFile);

// means save with .bkX extension is found. Load that file
if (profilePath != saveProfileFile)
{
File.Delete(profilePath);
File.Move(saveProfileFile, profilePath);
foreach (var extraFailedSaves in Directory.EnumerateFiles(profileDir, profileFileName + "*")
.Where(path => !path.EndsWith(".corrupted")))
{
File.Delete(extraFailedSaves);
}
}

// find extra auto-backup save files ending with .bkX and delete them
foreach (var extraFailedSaves in Directory.EnumerateFiles(profileDir, profileFileName + ".json.bk*")
.Where(path => !path.EndsWith(".corrupted")))
{
File.Delete(extraFailedSaves);
}

if (profileSettings == null) return;
Expand Down
5 changes: 4 additions & 1 deletion Project-Aurora/Project-Aurora/Settings/ApplicationProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ private async Task SaveProfile()
Directory.CreateDirectory(Path.GetDirectoryName(_savePath!));
if (File.Exists(_savePath!))
{
var backupFile = _savePath! + ".backup" + _backupFileNumber++;
var backupFile = _savePath! + ".bk" + _backupFileNumber++;
// move current save to preserve it
File.Move(_savePath!, backupFile);
// write new save to supposed location
await File.WriteAllTextAsync(_savePath!, content, Encoding.UTF8);
// write successful, delete backup
File.Delete(backupFile);
}
else
Expand Down

0 comments on commit 7d29ea1

Please sign in to comment.