diff options
-rw-r--r-- | build/common.targets | 12 | ||||
-rw-r--r-- | src/SMAPI.Mods.ConsoleCommands/manifest.json | 2 | ||||
-rw-r--r-- | src/SMAPI.Mods.SaveBackup/Framework/ModConfig.cs | 9 | ||||
-rw-r--r-- | src/SMAPI.Mods.SaveBackup/ModEntry.cs | 82 | ||||
-rw-r--r-- | src/SMAPI.Mods.SaveBackup/Properties/AssemblyInfo.cs | 4 | ||||
-rw-r--r-- | src/SMAPI.Mods.SaveBackup/StardewModdingAPI.Mods.SaveBackup.csproj | 58 | ||||
-rw-r--r-- | src/SMAPI.Mods.SaveBackup/manifest.json | 8 | ||||
-rw-r--r-- | src/SMAPI.sln | 6 | ||||
-rw-r--r-- | src/SMAPI/Program.cs | 3 |
9 files changed, 176 insertions, 8 deletions
diff --git a/build/common.targets b/build/common.targets index 5065a6a6..b382ea54 100644 --- a/build/common.targets +++ b/build/common.targets @@ -30,7 +30,7 @@ <!-- add game references--> <Choose> - <When Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.ConsoleCommands' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Tests'"> + <When Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.ConsoleCommands' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.SaveBackup' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Tests'"> <Choose> <When Condition="$(OS) == 'Windows_NT'"> <ItemGroup> @@ -95,7 +95,7 @@ <!-- copy files into game directory and enable debugging (only in debug mode) --> <Target Name="AfterBuild"> - <CallTarget Targets="CopySMAPI;CopyDefaultMod" Condition="'$(Configuration)' == 'Debug'" /> + <CallTarget Targets="CopySMAPI;CopyDefaultMods" Condition="'$(Configuration)' == 'Debug'" /> </Target> <Target Name="CopySMAPI" Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI'"> <Copy SourceFiles="$(TargetDir)\$(TargetName).exe" DestinationFolder="$(GamePath)" /> @@ -106,10 +106,10 @@ <Copy SourceFiles="$(TargetDir)\Newtonsoft.Json.dll" DestinationFolder="$(GamePath)" /> <Copy SourceFiles="$(TargetDir)\Mono.Cecil.dll" DestinationFolder="$(GamePath)" /> </Target> - <Target Name="CopyDefaultMod" Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.ConsoleCommands'"> - <Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" /> - <Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" Condition="Exists('$(TargetDir)\$(TargetName).pdb')" /> - <Copy SourceFiles="$(TargetDir)\manifest.json" DestinationFolder="$(GamePath)\Mods\ConsoleCommands" /> + <Target Name="CopyDefaultMods" Condition="'$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.ConsoleCommands' OR '$(MSBuildProjectName)' == 'StardewModdingAPI.Mods.SaveBackup'"> + <Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(GamePath)\Mods\$(AssemblyName)" /> + <Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(GamePath)\Mods\$(AssemblyName)" Condition="Exists('$(TargetDir)\$(TargetName).pdb')" /> + <Copy SourceFiles="$(TargetDir)\manifest.json" DestinationFolder="$(GamePath)\Mods\$(AssemblyName)" /> </Target> <!-- launch SMAPI on debug --> diff --git a/src/SMAPI.Mods.ConsoleCommands/manifest.json b/src/SMAPI.Mods.ConsoleCommands/manifest.json index 96d32090..5b3a5324 100644 --- a/src/SMAPI.Mods.ConsoleCommands/manifest.json +++ b/src/SMAPI.Mods.ConsoleCommands/manifest.json @@ -1,7 +1,7 @@ { "Name": "Console Commands", "Author": "SMAPI", - "Version": "2.6.0-beta.8", + "Version": "2.6.0-beta.12", "Description": "Adds SMAPI console commands that let you manipulate the game.", "UniqueID": "SMAPI.ConsoleCommands", "EntryDll": "ConsoleCommands.dll" 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..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 +{ + /// <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 + { + // read config + ModConfig config = this.Helper.ReadConfig<ModConfig>(); + + // 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 + *********/ + /// <summary>Get backups ordered by creation date.</summary> + /// <param name="folder">The folder to search.</param> + /// <param name="skip">The number of backups to skip.</param> + private IEnumerable<FileInfo> 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 @@ +<?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" /> + <Reference Include="System.IO.Compression" /> + <Reference Include="System.IO.Compression.FileSystem" /> + </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> + </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..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" +} diff --git a/src/SMAPI.sln b/src/SMAPI.sln index 1e8ade24..dec26694 100644 --- a/src/SMAPI.sln +++ b/src/SMAPI.sln @@ -58,6 +58,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SMAPI.ModBuildConfig.Analyz EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "StardewModdingAPI.Internal", "SMAPI.Internal\StardewModdingAPI.Internal.shproj", "{85208F8D-6FD1-4531-BE05-7142490F59FE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Mods.SaveBackup", "SMAPI.Mods.SaveBackup\StardewModdingAPI.Mods.SaveBackup.csproj", "{E272EB5D-8C57-417E-8E60-C1079D3F53C4}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution SMAPI.Internal\SMAPI.Internal.projitems*{443ddf81-6aaf-420a-a610-3459f37e5575}*SharedItemsImports = 4 @@ -102,6 +104,10 @@ Global {0CF97929-B0D0-4D73-B7BF-4FF7191035F9}.Debug|x86.Build.0 = Debug|Any CPU {0CF97929-B0D0-4D73-B7BF-4FF7191035F9}.Release|x86.ActiveCfg = Release|Any CPU {0CF97929-B0D0-4D73-B7BF-4FF7191035F9}.Release|x86.Build.0 = Release|Any CPU + {E272EB5D-8C57-417E-8E60-C1079D3F53C4}.Debug|x86.ActiveCfg = Debug|x86 + {E272EB5D-8C57-417E-8E60-C1079D3F53C4}.Debug|x86.Build.0 = Debug|x86 + {E272EB5D-8C57-417E-8E60-C1079D3F53C4}.Release|x86.ActiveCfg = Release|x86 + {E272EB5D-8C57-417E-8E60-C1079D3F53C4}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index e335b68e..f410abc1 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -100,7 +100,8 @@ namespace StardewModdingAPI /// <summary>The mod IDs for which to not show missing update key warnings.</summary> private readonly string[] AllowMissingUpdateKeys = { - "SMAPI.ConsoleCommands" + "SMAPI.ConsoleCommands", + "SMAPI.SaveBackup" }; /// <summary>Encapsulates SMAPI's JSON file parsing.</summary> |