From b4a8c1c2ac9d1b3796a144d267cceadf5f7831ff Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 7 Oct 2019 15:05:01 -0400 Subject: rework Save Backup to support Android --- src/SMAPI.Mods.SaveBackup/ModEntry.cs | 119 ++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs index efc6f39f..3b47759b 100644 --- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs +++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs @@ -68,48 +68,21 @@ namespace StardewModdingAPI.Mods.SaveBackup if (targetFile.Exists || fallbackDir.Exists) return; - // create zip - // due to limitations with the bundled Mono on Mac, we can't reference System.IO.Compression. + // back up saves this.Monitor.Log($"Backing up saves to {targetFile.FullName}...", LogLevel.Trace); - switch (Constants.TargetPlatform) + if (!this.TryCompress(Constants.SavesPath, targetFile, out Exception compressError)) { - case GamePlatform.Android: - case GamePlatform.Linux: - case GamePlatform.Windows: - { - try - { - // create compressed backup - 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 }); - } - catch (Exception ex) when (ex is TypeLoadException || ex.InnerException is TypeLoadException) - { - // create uncompressed backup if compression fails - this.Monitor.Log("Couldn't zip the save backup, creating uncompressed backup instead.", LogLevel.Debug); - this.Monitor.Log(ex.ToString(), LogLevel.Trace); - this.RecursiveCopy(new DirectoryInfo(Constants.SavesPath), fallbackDir, copyRoot: 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; + // log error (expected on Android due to missing compression DLLs) + if (Constants.TargetPlatform == GamePlatform.Android) + this.Monitor.VerboseLog($"Compression isn't supported on Android:\n{compressError}"); + else + { + this.Monitor.Log("Couldn't zip the save backup, creating uncompressed backup instead.", LogLevel.Debug); + this.Monitor.Log(compressError.ToString(), LogLevel.Trace); + } + + // fallback to uncompressed + this.RecursiveCopy(new DirectoryInfo(Constants.SavesPath), fallbackDir, copyRoot: false); } this.Monitor.Log("Backup done!", LogLevel.Trace); } @@ -155,6 +128,72 @@ namespace StardewModdingAPI.Mods.SaveBackup } } + /// Create a zip using the best available method. + /// The file or directory path to zip. + /// The destination file to create. + /// The error which occurred trying to compress, if applicable. This is if compression isn't supported on this platform. + /// Returns whether compression succeeded. + private bool TryCompress(string sourcePath, FileInfo destination, out Exception error) + { + try + { + if (Constants.TargetPlatform == GamePlatform.Mac) + this.CompressUsingMacProcess(sourcePath, destination); // due to limitations with the bundled Mono on Mac, we can't reference System.IO.Compression + else + this.CompressUsingNetFramework(sourcePath, destination); + + error = null; + return true; + } + catch (Exception ex) + { + error = ex; + return false; + } + } + + /// Create a zip using the .NET compression library. + /// The file or directory path to zip. + /// The destination file to create. + /// The compression libraries aren't available on this system. + 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") ?? 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."); + 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 }); + } + + /// Create a zip using a process command on MacOS. + /// The file or directory path to zip. + /// The destination file to create. + private void CompressUsingMacProcess(string sourcePath, FileInfo destination) + { + DirectoryInfo saveFolder = new DirectoryInfo(sourcePath); + ProcessStartInfo startInfo = new ProcessStartInfo + { + FileName = "zip", + Arguments = $"-rq \"{destination.FullName}\" \"{saveFolder.Name}\" -x \"*.DS_Store\" -x \"__MACOSX\"", + WorkingDirectory = $"{saveFolder.FullName}/../", + CreateNoWindow = true + }; + new Process { StartInfo = startInfo }.Start(); + } + /// Recursively copy a directory or file. /// The file or folder to copy. /// The folder to copy into. -- cgit