From 92bfa6fa5b37e0f0c3f1967520d01a1947bf01bc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 12 May 2018 18:16:40 -0400 Subject: add bundled save backup mod (#253) --- src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs | 9 +++ src/SMAPI.Mods.SaveBackup/ModEntry.cs | 82 ++++++++++++++++++++++ .../Properties/AssemblyInfo.cs | 4 ++ .../StardewModdingAPI.Mods.SaveBackup.csproj | 58 +++++++++++++++ src/SMAPI.Mods.SaveBackup/manifest.json | 8 +++ 5 files changed, 161 insertions(+) create mode 100644 src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs create mode 100644 src/SMAPI.Mods.SaveBackup/ModEntry.cs create mode 100644 src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs create mode 100644 src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj create mode 100644 src/SMAPI.Mods.SaveBackup/manifest.json (limited to 'src/SMAPI.Mods.SaveBackup') diff --git a/src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs b/src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs new file mode 100644 index 00000000..c9dcb216 --- /dev/null +++ b/src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs @@ -0,0 +1,9 @@ +namespace StardewModdingAPI.Mods.SaveBackup.Framework +{ + /// The mod configuration. + internal class ModConfig + { + /// The number of backups to keep. + public int BackupsToKeep { get; set; } = 10; + } +} diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs new file mode 100644 index 00000000..e9e62752 --- /dev/null +++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using StardewModdingAPI.Mods.SaveBackup.Framework; +using StardewValley; + +namespace StardewModdingAPI.Mods.SaveBackup +{ + /// The main entry point for the mod. + public class ModEntry : Mod + { + /********* + ** Properties + *********/ + /// The name of the save archive to create. + private readonly string FileName = $"{DateTime.UtcNow:yyyy-MM-dd} - SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version}.zip"; + + + /********* + ** Public methods + *********/ + /// The mod entry point, called after the mod is first loaded. + /// Provides simplified APIs for writing mods. + public override void Entry(IModHelper helper) + { + try + { + // read config + ModConfig config = this.Helper.ReadConfig(); + + // init backup folder + DirectoryInfo folder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups")); + folder.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}"); + } + } + } + catch (Exception ex) + { + this.Monitor.Log($"Error backing up saves: {ex}"); + } + } + + + /********* + ** 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) + { + return folder + .GetFiles() + .OrderByDescending(p => p.CreationTimeUtc) + .Skip(skip); + } + } +} diff --git a/src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs b/src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..fc6b26fa --- /dev/null +++ b/src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Reflection; + +[assembly: AssemblyTitle("StardewModdingAPI.Mods.SaveBackup")] +[assembly: AssemblyDescription("")] diff --git a/src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj b/src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj new file mode 100644 index 00000000..b322bf3a --- /dev/null +++ b/src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj @@ -0,0 +1,58 @@ + + + + + Debug + x86 + {E272EB5D-8C57-417E-8E60-C1079D3F53C4} + Library + Properties + StardewModdingAPI.Mods.SaveBackup + SaveBackup + v4.5 + 512 + + + true + full + false + $(SolutionDir)\..\bin\Debug\Mods\SaveBackup\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + $(SolutionDir)\..\bin\Release\Mods\SaveBackup\ + TRACE + prompt + 4 + + + + + + + + + Properties\GlobalAssemblyInfo.cs + + + + + + + + PreserveNewest + + + + + {f1a573b0-f436-472c-ae29-0b91ea6b9f8f} + StardewModdingAPI + + + + + \ No newline at end of file diff --git a/src/SMAPI.Mods.SaveBackup/manifest.json b/src/SMAPI.Mods.SaveBackup/manifest.json new file mode 100644 index 00000000..5246f65d --- /dev/null +++ b/src/SMAPI.Mods.SaveBackup/manifest.json @@ -0,0 +1,8 @@ +{ + "Name": "Save Backup", + "Author": "SMAPI", + "Version": "2.6.0-beta.12", + "Description": "Automatically backs up all your saves once per day into its folder.", + "UniqueID": "SMAPI.SaveBackup", + "EntryDll": "SaveBackup.dll" +} -- cgit