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;
|
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
|
|
|
/*********
|
|
|
|
** Properties
|
|
|
|
*********/
|
|
|
|
/// <summary>The folder path containing the game's app data.</summary>
|
|
|
|
private static readonly string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley");
|
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>
|
|
|
|
private static readonly string SavesPath = Path.Combine(SaveBackup.AppDataPath, "Saves");
|
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>
|
|
|
|
private static readonly string NightlyBackupsPath = Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", "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
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
ZipFile.CreateFromDirectory(SaveBackup.SavesPath, Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}.zip"));
|
|
|
|
|
|
|
|
// delete old backups
|
|
|
|
new DirectoryInfo(folderPath)
|
|
|
|
.EnumerateFiles()
|
|
|
|
.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()
|
|
|
|
.ForEach(file => file.Delete());
|
2017-02-22 15:29:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|