summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.SaveBackup
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-08-01 11:07:29 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-08-01 11:07:29 -0400
commit60b41195778af33fd609eab66d9ae3f1d1165e8f (patch)
tree7128b906d40e94c56c34ed6058f27bc31c31a08b /src/SMAPI.Mods.SaveBackup
parentb9bc1a6d17cafa0a97b46ffecda432cfc2f23b51 (diff)
parent52cf953f685c65b2b6814e375ec9a5ffa03c440a (diff)
downloadSMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.gz
SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.bz2
SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.SaveBackup')
-rw-r--r--src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs9
-rw-r--r--src/SMAPI.Mods.SaveBackup/ModEntry.cs133
-rw-r--r--src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs4
-rw-r--r--src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj62
-rw-r--r--src/SMAPI.Mods.SaveBackup/manifest.json8
5 files changed, 216 insertions, 0 deletions
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
+{
+ /// <summary>The mod configuration.</summary>
+ internal class ModConfig
+ {
+ /// <summary>The number of backups to keep.</summary>
+ 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..78578c3c
--- /dev/null
+++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.IO.Compression;
+using System.Linq;
+using System.Reflection;
+using StardewModdingAPI.Mods.SaveBackup.Framework;
+using StardewValley;
+
+namespace StardewModdingAPI.Mods.SaveBackup
+{
+ /// <summary>The main entry point for the mod.</summary>
+ public class ModEntry : Mod
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The name of the save archive to create.</summary>
+ private readonly string FileName = $"{DateTime.UtcNow:yyyy-MM-dd} - SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version}.zip";
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>The mod entry point, called after the mod is first loaded.</summary>
+ /// <param name="helper">Provides simplified APIs for writing mods.</param>
+ public override void Entry(IModHelper helper)
+ {
+ try
+ {
+ ModConfig config = this.Helper.ReadConfig<ModConfig>();
+
+ // init backup folder
+ DirectoryInfo backupFolder = new DirectoryInfo(Path.Combine(this.Helper.DirectoryPath, "backups"));
+ backupFolder.Create();
+
+ // back up saves
+ this.CreateBackup(backupFolder);
+ this.PruneBackups(backupFolder, config.BackupsToKeep);
+ }
+ catch (Exception ex)
+ {
+ this.Monitor.Log($"Error backing up saves: {ex}");
+ }
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Back up the current saves.</summary>
+ /// <param name="backupFolder">The folder containing save backups.</param>
+ private void CreateBackup(DirectoryInfo backupFolder)
+ {
+ try
+ {
+ // get target path
+ FileInfo targetFile = new FileInfo(Path.Combine(backupFolder.FullName, this.FileName));
+ if (targetFile.Exists)
+ targetFile.Delete(); //return;
+
+ // create zip
+ // due to limitations with the bundled Mono on Mac, we can't reference System.IO.Compression.
+ this.Monitor.Log($"Adding {targetFile.Name}...", LogLevel.Trace);
+ switch (Constants.TargetPlatform)
+ {
+ case GamePlatform.Linux:
+ case GamePlatform.Windows:
+ {
+ Assembly coreAssembly = Assembly.Load("System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") ?? throw new InvalidOperationException("Can't load System.IO.Compression assembly.");
+ Assembly fsAssembly = Assembly.Load("System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") ?? throw new InvalidOperationException("Can't load System.IO.Compression assembly.");
+ Type compressionLevelType = coreAssembly.GetType("System.IO.Compression.CompressionLevel") ?? throw new InvalidOperationException("Can't load CompressionLevel type.");
+ Type zipFileType = fsAssembly.GetType("System.IO.Compression.ZipFile") ?? throw new InvalidOperationException("Can't load ZipFile type.");
+ MethodInfo createMethod = zipFileType.GetMethod("CreateFromDirectory", new[] { typeof(string), typeof(string), compressionLevelType, typeof(bool) }) ?? throw new InvalidOperationException("Can't load ZipFile.CreateFromDirectory method.");
+ createMethod.Invoke(null, new object[] { Constants.SavesPath, targetFile.FullName, CompressionLevel.Fastest, false });
+ }
+ break;
+
+ case GamePlatform.Mac:
+ {
+ DirectoryInfo saveFolder = new DirectoryInfo(Constants.SavesPath);
+ ProcessStartInfo startInfo = new ProcessStartInfo
+ {
+ FileName = "zip",
+ Arguments = $"-rq \"{targetFile.FullName}\" \"{saveFolder.Name}\" -x \"*.DS_Store\" -x \"__MACOSX\"",
+ WorkingDirectory = $"{Constants.SavesPath}/../",
+ CreateNoWindow = true
+ };
+ new Process { StartInfo = startInfo }.Start();
+ }
+ break;
+ }
+ }
+ catch (Exception ex)
+ {
+ this.Monitor.Log("Couldn't back up save files (see log file for details).", LogLevel.Warn);
+ this.Monitor.Log(ex.ToString(), LogLevel.Trace);
+ }
+ }
+
+ /// <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)
+ {
+ try
+ {
+ var oldBackups = backupFolder
+ .GetFiles()
+ .OrderByDescending(p => p.CreationTimeUtc)
+ .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}");
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ this.Monitor.Log("Couldn't remove old backups (see log file for details).", LogLevel.Warn);
+ this.Monitor.Log(ex.ToString(), LogLevel.Trace);
+ }
+ }
+ }
+}
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..0ccbcc6c
--- /dev/null
+++ b/src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProjectGuid>{E272EB5D-8C57-417E-8E60-C1079D3F53C4}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>StardewModdingAPI.Mods.SaveBackup</RootNamespace>
+ <AssemblyName>SaveBackup</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>$(SolutionDir)\..\bin\Debug\Mods\SaveBackup\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>$(SolutionDir)\..\bin\Release\Mods\SaveBackup\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="..\..\build\GlobalAssemblyInfo.cs">
+ <Link>Properties\GlobalAssemblyInfo.cs</Link>
+ </Compile>
+ <Compile Include="Framework\ModConfig.cs" />
+ <Compile Include="ModEntry.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="manifest.json">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\SMAPI\StardewModdingAPI.csproj">
+ <Project>{f1a573b0-f436-472c-ae29-0b91ea6b9f8f}</Project>
+ <Name>StardewModdingAPI</Name>
+ <Private>False</Private>
+ </ProjectReference>
+ <ProjectReference Include="..\StardewModdingAPI.Toolkit.CoreInterfaces\StardewModdingAPI.Toolkit.CoreInterfaces.csproj">
+ <Project>{d5cfd923-37f1-4bc3-9be8-e506e202ac28}</Project>
+ <Name>StardewModdingAPI.Toolkit.CoreInterfaces</Name>
+ <Private>False</Private>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <Import Project="..\..\build\common.targets" />
+</Project> \ 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..ee0f2abb
--- /dev/null
+++ b/src/SMAPI.Mods.SaveBackup/manifest.json
@@ -0,0 +1,8 @@
+{
+ "Name": "Save Backup",
+ "Author": "SMAPI",
+ "Version": "2.6.0",
+ "Description": "Automatically backs up all your saves once per day into its folder.",
+ "UniqueID": "SMAPI.SaveBackup",
+ "EntryDll": "SaveBackup.dll"
+}