reorganise SaveBackup code a bit (#522)

This commit is contained in:
Jesse Plamondon-Willard 2018-05-23 00:50:46 -04:00
parent b942c89dcf
commit fda2ac7485
1 changed files with 35 additions and 32 deletions

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
@ -27,36 +26,15 @@ namespace StardewModdingAPI.Mods.SaveBackup
{ {
try try
{ {
// read config
ModConfig config = this.Helper.ReadConfig<ModConfig>(); ModConfig config = this.Helper.ReadConfig<ModConfig>();
// init backup folder // init backup folder
DirectoryInfo folder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups")); DirectoryInfo backupFolder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups"));
folder.Create(); backupFolder.Create();
// back up saves // back up saves
{ this.CreateBackup(backupFolder);
FileInfo file = new FileInfo(Path.Combine(folder.FullName, this.FileName)); this.PruneBackups(backupFolder, config.BackupsToKeep);
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}");
}
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -68,15 +46,40 @@ namespace StardewModdingAPI.Mods.SaveBackup
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Get backups ordered by creation date.</summary> /// <summary>Back up the current saves.</summary>
/// <param name="folder">The folder to search.</param> /// <param name="backupFolder">The folder containing save backups.</param>
/// <param name="skip">The number of backups to skip.</param> private void CreateBackup(DirectoryInfo backupFolder)
private IEnumerable<FileInfo> GetOldBackups(DirectoryInfo folder, int skip)
{ {
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);
}
}
/// <summary>Remove old backups if we've exceeded the limit.</summary>
/// <param name="backupFolder">The folder containing save backups.</param>
/// <param name="backupsToKeep">The number of backups to keep.</param>
private void PruneBackups(DirectoryInfo backupFolder, int backupsToKeep)
{
var oldBackups = backupFolder
.GetFiles() .GetFiles()
.OrderByDescending(p => p.CreationTimeUtc) .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}");
}
}
} }
} }
} }