2017-02-22 15:29:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
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-07-30 08:53:26 +08:00
|
|
|
|
/// <summary>The number of save backups to keep for each type.</summary>
|
|
|
|
|
private int SaveCount = 30;
|
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)
|
|
|
|
|
{
|
|
|
|
|
this.LoadConfig();
|
|
|
|
|
this.WriteConfig();
|
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
|
|
|
|
|
2017-08-06 03:20:46 +08:00
|
|
|
|
SaveEvents.BeforeSave += this.SaveEvents_BeforeSave;
|
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
|
|
|
|
|
*********/
|
2017-08-06 03:20:46 +08:00
|
|
|
|
/// <summary>The method invoked before the save is updated.</summary>
|
2017-07-30 08:53:26 +08:00
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:20:46 +08:00
|
|
|
|
private void SaveEvents_BeforeSave(object sender, EventArgs 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)
|
|
|
|
|
.Skip(this.SaveCount)
|
|
|
|
|
.ToList()
|
|
|
|
|
.ForEach(file => file.Delete());
|
2017-02-22 15:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
|
/// <summary>Load the configuration settings.</summary>
|
|
|
|
|
private void LoadConfig()
|
2017-02-22 15:29:00 +08:00
|
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
|
var path = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
|
|
|
|
|
if (File.Exists(path))
|
2017-02-22 15:29:00 +08:00
|
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
|
string[] text = File.ReadAllLines(path);
|
|
|
|
|
this.SaveCount = Convert.ToInt32(text[3]);
|
2017-02-22 15:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 08:53:26 +08:00
|
|
|
|
/// <summary>Save the configuration settings.</summary>
|
|
|
|
|
private void WriteConfig()
|
2017-02-22 15:29:00 +08:00
|
|
|
|
{
|
2017-07-30 08:53:26 +08:00
|
|
|
|
string path = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
|
|
|
|
|
string[] text = new string[20];
|
|
|
|
|
text[0] = "Player: AutoBackup Config:";
|
|
|
|
|
text[1] = "====================================================================================";
|
|
|
|
|
text[2] = "Number of Backups to Keep:";
|
|
|
|
|
text[3] = this.SaveCount.ToString();
|
|
|
|
|
|
|
|
|
|
File.WriteAllLines(path, text);
|
2017-02-22 15:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|