rework Save Backup to support Android

This commit is contained in:
Jesse Plamondon-Willard 2019-10-07 15:05:01 -04:00
parent 175ebf9071
commit b4a8c1c2ac
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 78 additions and 39 deletions

View File

@ -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;
// 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);
}
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;
// 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
}
}
/// <summary>Create a zip using the best available method.</summary>
/// <param name="sourcePath">The file or 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, 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;
}
}
/// <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") ?? 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 });
}
/// <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 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();
}
/// <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>