summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.SaveBackup
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2023-01-09 12:27:49 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2023-01-09 12:27:49 -0500
commitb4e95a92b33c541d36379d69d3650c5c22ea324c (patch)
treede020a6dc7e065f725d68ea43b4a3a57eb7ccf8c /src/SMAPI.Mods.SaveBackup
parent368b25b5411683192f4398616abed61441457799 (diff)
parent25b8e13ba827a0512f5089d3bd22e8ed1a15e7ba (diff)
downloadSMAPI-b4e95a92b33c541d36379d69d3650c5c22ea324c.tar.gz
SMAPI-b4e95a92b33c541d36379d69d3650c5c22ea324c.tar.bz2
SMAPI-b4e95a92b33c541d36379d69d3650c5c22ea324c.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.SaveBackup')
-rw-r--r--src/SMAPI.Mods.SaveBackup/ModEntry.cs57
-rw-r--r--src/SMAPI.Mods.SaveBackup/manifest.json4
2 files changed, 7 insertions, 54 deletions
diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs
index a79c092f..8a22a5f3 100644
--- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs
+++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs
@@ -1,10 +1,8 @@
using System;
-using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Linq;
-using System.Reflection;
using System.Threading.Tasks;
using StardewValley;
@@ -81,7 +79,7 @@ namespace StardewModdingAPI.Mods.SaveBackup
}
// compress backup if possible
- if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception? compressError))
+ if (!this.TryCompressDir(fallbackDir.FullName, targetFile, out Exception? compressError))
{
this.Monitor.Log(Constants.TargetPlatform != GamePlatform.Android
? $"Backed up to {fallbackDir.FullName}." // expected to fail on Android
@@ -136,19 +134,16 @@ namespace StardewModdingAPI.Mods.SaveBackup
}
}
- /// <summary>Create a zip using the best available method.</summary>
- /// <param name="sourcePath">The file or directory path to zip.</param>
+ /// <summary>Try to create a compressed zip file for a directory.</summary>
+ /// <param name="sourcePath">The directory path to zip.</param>
/// <param name="destination">The destination file to create.</param>
/// <param name="error">The error which occurred trying to compress, if applicable. This is <see cref="NotSupportedException"/> if compression isn't supported on this platform.</param>
/// <returns>Returns whether compression succeeded.</returns>
- private bool TryCompress(string sourcePath, FileInfo destination, [NotNullWhen(false)] out Exception? error)
+ private bool TryCompressDir(string sourcePath, FileInfo destination, [NotNullWhen(false)] out Exception? error)
{
try
{
- if (Constants.TargetPlatform == GamePlatform.Mac)
- this.CompressUsingMacProcess(sourcePath, destination); // due to limitations with the bundled Mono on macOS, we can't reference System.IO.Compression
- else
- this.CompressUsingNetFramework(sourcePath, destination);
+ ZipFile.CreateFromDirectory(sourcePath, destination.FullName, CompressionLevel.Fastest, false);
error = null;
return true;
@@ -160,48 +155,6 @@ namespace StardewModdingAPI.Mods.SaveBackup
}
}
- /// <summary>Create a zip using the .NET compression library.</summary>
- /// <param name="sourcePath">The file or directory path to zip.</param>
- /// <param name="destination">The destination file to create.</param>
- /// <exception cref="NotSupportedException">The compression libraries aren't available on this system.</exception>
- private void CompressUsingNetFramework(string sourcePath, FileInfo destination)
- {
- // get compress method
- MethodInfo createFromDirectory;
- try
- {
- // create compressed backup
- Assembly coreAssembly = Assembly.Load("System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
- Assembly fsAssembly = Assembly.Load("System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
- 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.");
- createFromDirectory = zipFileType.GetMethod("CreateFromDirectory", new[] { typeof(string), typeof(string), compressionLevelType, typeof(bool) }) ?? throw new InvalidOperationException("Can't load ZipFile.CreateFromDirectory method.");
- }
- catch (Exception ex)
- {
- throw new NotSupportedException("Couldn't load the .NET compression libraries on this system.", ex);
- }
-
- // compress file
- createFromDirectory.Invoke(null, new object[] { sourcePath, destination.FullName, CompressionLevel.Fastest, false });
- }
-
- /// <summary>Create a zip using a process command on macOS.</summary>
- /// <param name="sourcePath">The file or directory path to zip.</param>
- /// <param name="destination">The destination file to create.</param>
- private void CompressUsingMacProcess(string sourcePath, FileInfo destination)
- {
- DirectoryInfo saveFolder = new(sourcePath);
- ProcessStartInfo startInfo = new()
- {
- FileName = "zip",
- Arguments = $"-rq \"{destination.FullName}\" \"{saveFolder.Name}\" -x \"*.DS_Store\" -x \"__MACOSX\"",
- WorkingDirectory = $"{saveFolder.FullName}/../",
- CreateNoWindow = true
- };
- new Process { StartInfo = startInfo }.Start();
- }
-
/// <summary>Recursively copy a directory or file.</summary>
/// <param name="source">The file or folder to copy.</param>
/// <param name="targetFolder">The folder to copy into.</param>
diff --git a/src/SMAPI.Mods.SaveBackup/manifest.json b/src/SMAPI.Mods.SaveBackup/manifest.json
index 9a587a2b..e29a3ed3 100644
--- a/src/SMAPI.Mods.SaveBackup/manifest.json
+++ b/src/SMAPI.Mods.SaveBackup/manifest.json
@@ -1,9 +1,9 @@
{
"Name": "Save Backup",
"Author": "SMAPI",
- "Version": "3.18.1",
+ "Version": "3.18.2",
"Description": "Automatically backs up all your saves once per day into its folder.",
"UniqueID": "SMAPI.SaveBackup",
"EntryDll": "SaveBackup.dll",
- "MinimumApiVersion": "3.18.1"
+ "MinimumApiVersion": "3.18.2"
}