2018-12-30 18:00:05 +08:00
|
|
|
using System;
|
2017-02-22 15:29:00 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Linq;
|
2020-02-17 23:26:39 +08:00
|
|
|
using System.Reflection;
|
2017-08-06 03:51:44 +08:00
|
|
|
using Omegasis.SaveBackup.Framework;
|
2017-02-22 15:29:00 +08:00
|
|
|
using StardewModdingAPI;
|
2017-07-30 08:53:26 +08:00
|
|
|
using StardewModdingAPI.Events;
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2017-07-28 08:28:39 +08:00
|
|
|
namespace Omegasis.SaveBackup
|
2017-02-22 15:29:00 +08:00
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <summary>The mod entry point.</summary>
|
|
|
|
public class SaveBackup : Mod
|
2017-02-22 15:29:00 +08:00
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
/*********
|
2019-01-06 15:23:07 +08:00
|
|
|
** Fields
|
2017-07-30 08:53:26 +08:00
|
|
|
*********/
|
|
|
|
/// <summary>The folder path containing the game's app data.</summary>
|
2020-02-17 20:57:04 +08:00
|
|
|
private static string AppDataPath => Constants.TargetPlatform != GamePlatform.Android ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley") : Constants.DataPath;
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <summary>The folder path containing the game's saves.</summary>
|
2020-02-17 20:57:04 +08:00
|
|
|
private static string SavesPath => Constants.TargetPlatform != GamePlatform.Android ? Path.Combine(SaveBackup.AppDataPath, "Saves") : Constants.CurrentSavePath;
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <summary>The folder path containing backups of the save before the player starts playing.</summary>
|
|
|
|
private static readonly string PrePlayBackupsPath = Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", "Pre_Play_Saves");
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <summary>The folder path containing nightly backups of the save.</summary>
|
2020-02-17 20:57:04 +08:00
|
|
|
private static string NightlyBackupsPath => Constants.TargetPlatform != GamePlatform.Android ? Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", "Nightly_InGame_Saves") : Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", Constants.SaveFolderName, "Nightly_InGame_Saves");
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
/// <summary>The mod configuration.</summary>
|
|
|
|
private ModConfig Config;
|
2017-02-22 15:29:00 +08:00
|
|
|
|
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/*********
|
|
|
|
** 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)
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
this.Config = helper.ReadConfig<ModConfig>();
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2020-02-17 20:57:04 +08:00
|
|
|
if(Constants.TargetPlatform != GamePlatform.Android)
|
|
|
|
this.BackupSaves(SaveBackup.PrePlayBackupsPath);
|
2017-02-22 15:29:00 +08:00
|
|
|
|
2019-01-06 15:21:06 +08:00
|
|
|
helper.Events.GameLoop.Saving += this.OnSaving;
|
2017-07-30 08:53:26 +08:00
|
|
|
}
|
2017-02-22 15:29:00 +08:00
|
|
|
|
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/*********
|
|
|
|
** Private methods
|
|
|
|
*********/
|
2019-01-06 15:21:06 +08:00
|
|
|
/// <summary>Raised before the game begins writes data to the save file (except the initial save creation).</summary>
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <param name="sender">The event sender.</param>
|
2019-01-06 15:21:06 +08:00
|
|
|
/// <param name="e">The event arguments.</param>
|
|
|
|
private void OnSaving(object sender, SavingEventArgs e)
|
2017-07-30 08:53:26 +08:00
|
|
|
{
|
|
|
|
this.BackupSaves(SaveBackup.NightlyBackupsPath);
|
2017-02-22 15:29:00 +08:00
|
|
|
}
|
|
|
|
|
2020-02-17 23:26:39 +08:00
|
|
|
/// <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>
|
|
|
|
/// <param name="copyRoot">Whether to copy the root folder itself, or <c>false</c> to only copy its contents.</param>
|
|
|
|
/// <param name="filter">A filter which matches the files or directories to copy, or <c>null</c> to copy everything.</param>
|
|
|
|
/// <remarks>Derived from the SMAPI installer code.</remarks>
|
|
|
|
/// <returns>Returns whether any files were copied.</returns>
|
|
|
|
private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, bool copyRoot = true)
|
|
|
|
{
|
|
|
|
if (!source.Exists)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool anyCopied = false;
|
|
|
|
|
|
|
|
switch (source)
|
|
|
|
{
|
|
|
|
case FileInfo sourceFile:
|
|
|
|
targetFolder.Create();
|
|
|
|
sourceFile.CopyTo(Path.Combine(targetFolder.FullName, sourceFile.Name));
|
|
|
|
anyCopied = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DirectoryInfo sourceDir:
|
|
|
|
DirectoryInfo targetSubfolder = copyRoot ? new DirectoryInfo(Path.Combine(targetFolder.FullName, sourceDir.Name)) : targetFolder;
|
|
|
|
foreach (var entry in sourceDir.EnumerateFileSystemInfos())
|
|
|
|
anyCopied = this.RecursiveCopy(entry, targetSubfolder) || anyCopied;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new NotSupportedException($"Unknown filesystem info type '{source.GetType().FullName}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return anyCopied;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OriginCompressLogic(string folderPath)
|
|
|
|
{
|
|
|
|
ZipFile.CreateFromDirectory(SaveBackup.SavesPath, Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}.zip"));
|
|
|
|
}
|
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
/// <summary>Back up saves to the specified folder.</summary>
|
|
|
|
/// <param name="folderPath">The folder path in which to generate saves.</param>
|
|
|
|
private void BackupSaves(string folderPath)
|
2017-02-22 15:29:00 +08:00
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
// back up saves
|
|
|
|
Directory.CreateDirectory(folderPath);
|
2020-02-17 23:26:39 +08:00
|
|
|
if(Constants.TargetPlatform == GamePlatform.Android)
|
|
|
|
{
|
|
|
|
FileInfo targetFile = new FileInfo(Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}.zip"));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.CompressUsingNetFramework(folderPath, targetFile);
|
|
|
|
}
|
|
|
|
catch (NotSupportedException)
|
|
|
|
{
|
|
|
|
this.RecursiveCopy(new DirectoryInfo(SaveBackup.SavesPath), new DirectoryInfo(Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}")), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.OriginCompressLogic(folderPath);
|
|
|
|
}
|
2017-07-30 08:53:26 +08:00
|
|
|
|
|
|
|
// delete old backups
|
|
|
|
new DirectoryInfo(folderPath)
|
2020-02-17 23:26:39 +08:00
|
|
|
.GetFileSystemInfos()
|
2017-07-30 08:53:26 +08:00
|
|
|
.OrderByDescending(f => f.CreationTime)
|
2017-08-06 03:51:44 +08:00
|
|
|
.Skip(this.Config.SaveCount)
|
2017-07-30 08:53:26 +08:00
|
|
|
.ToList()
|
2020-02-17 23:26:39 +08:00
|
|
|
.ForEach(file =>
|
|
|
|
{
|
|
|
|
if (file is DirectoryInfo folder)
|
|
|
|
folder.Delete(recursive: true);
|
|
|
|
else
|
|
|
|
file.Delete();
|
|
|
|
});
|
2017-02-22 15:29:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|