From fda2ac74857e3bc1129667ef6ba975efd6ca33f6 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 23 May 2018 00:50:46 -0400 Subject: reorganise SaveBackup code a bit (#522) --- src/SMAPI.Mods.SaveBackup/ModEntry.cs | 67 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'src/SMAPI.Mods.SaveBackup') diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs index e9e62752..37793ced 100644 --- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs +++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; @@ -27,36 +26,15 @@ namespace StardewModdingAPI.Mods.SaveBackup { try { - // read config ModConfig config = this.Helper.ReadConfig(); // init backup folder - DirectoryInfo folder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups")); - folder.Create(); + DirectoryInfo backupFolder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups")); + backupFolder.Create(); // back up saves - { - FileInfo file = new FileInfo(Path.Combine(folder.FullName, this.FileName)); - if (!file.Exists) - { - this.Monitor.Log($"Adding {file.Name}...", LogLevel.Trace); - ZipFile.CreateFromDirectory(Constants.SavesPath, file.FullName, CompressionLevel.Fastest, includeBaseDirectory: false); - } - } - - // prune old saves - foreach (FileInfo file in this.GetOldBackups(folder, config.BackupsToKeep)) - { - try - { - this.Monitor.Log($"Deleting {file.Name}...", LogLevel.Trace); - file.Delete(); - } - catch (Exception ex) - { - this.Monitor.Log($"Error deleting old save backup '{file.Name}': {ex}"); - } - } + this.CreateBackup(backupFolder); + this.PruneBackups(backupFolder, config.BackupsToKeep); } catch (Exception ex) { @@ -68,15 +46,40 @@ namespace StardewModdingAPI.Mods.SaveBackup /********* ** Private methods *********/ - /// Get backups ordered by creation date. - /// The folder to search. - /// The number of backups to skip. - private IEnumerable GetOldBackups(DirectoryInfo folder, int skip) + /// Back up the current saves. + /// The folder containing save backups. + private void CreateBackup(DirectoryInfo backupFolder) { - return folder + FileInfo file = new FileInfo(Path.Combine(backupFolder.FullName, this.FileName)); + if (!file.Exists) + { + this.Monitor.Log($"Adding {file.Name}...", LogLevel.Trace); + ZipFile.CreateFromDirectory(Constants.SavesPath, file.FullName, CompressionLevel.Fastest, includeBaseDirectory: false); + } + } + + /// Remove old backups if we've exceeded the limit. + /// The folder containing save backups. + /// The number of backups to keep. + private void PruneBackups(DirectoryInfo backupFolder, int backupsToKeep) + { + var oldBackups = backupFolder .GetFiles() .OrderByDescending(p => p.CreationTimeUtc) - .Skip(skip); + .Skip(backupsToKeep); + + foreach (FileInfo file in oldBackups) + { + try + { + this.Monitor.Log($"Deleting {file.Name}...", LogLevel.Trace); + file.Delete(); + } + catch (Exception ex) + { + this.Monitor.Log($"Error deleting old save backup '{file.Name}': {ex}"); + } + } } } } -- cgit