refactor SaveBackup

This commit formats/documents/simplifies code, standardises naming conventions, etc.
This commit is contained in:
Jesse Plamondon-Willard 2017-07-29 20:53:26 -04:00
parent db9cda9e8b
commit 8f836be997
2 changed files with 67 additions and 172 deletions

View File

@ -3,203 +3,98 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using StardewModdingAPI; using StardewModdingAPI;
using StardewModdingAPI.Events;
namespace Omegasis.SaveBackup namespace Omegasis.SaveBackup
{ {
public class Class1 : Mod /// <summary>The mod entry point.</summary>
public class SaveBackup : Mod
{ {
static string app_path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); /*********
static string stardew_path = Path.Combine(app_path, "StardewValley"); ** Properties
static string stardew_save_path = Path.Combine(stardew_path, "Saves"); *********/
static string saved_backups = Path.Combine(stardew_path, "Backed_Up_Saves"); //name of exported file /// <summary>The folder path containing the game's app data.</summary>
static string pre_play_saves = Path.Combine(saved_backups, "Pre_Play_Saves"); private static readonly string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley");
static string sleeping_saves = Path.Combine(saved_backups, "Nightly_InGame_Saves");
static string backup_path = Path.Combine(stardew_path, "Backed_Up_Saves");
public static string output; /// <summary>The folder path containing the game's saves.</summary>
static int pre_iterator = 0; private static readonly string SavesPath = Path.Combine(SaveBackup.AppDataPath, "Saves");
static int sleep_iterator =0;
static string pre_iterator_string = pre_iterator.ToString();
static string sleep_iterator_string = sleep_iterator.ToString();
static int save_num = 30; /// <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");
/// <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");
/// <summary>The number of save backups to keep for each type.</summary>
private int SaveCount = 30;
/*********
** 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) public override void Entry(IModHelper helper)
{ {
DataLoader(); this.LoadConfig();
MyWritter(); this.WriteConfig();
Save_Backup(); //Yup I wrote it all in one function.... kinda this.BackupSaves(SaveBackup.PrePlayBackupsPath);
StardewModdingAPI.Events.TimeEvents.DayOfMonthChanged += Sleep_Save_Backup; TimeEvents.DayOfMonthChanged += this.TimeEvents_DayOfMonthChanged;
} }
public void Save_Backup()
/*********
** Private methods
*********/
/// <summary>The method invoked when <see cref="StardewValley.Game1.dayOfMonth"/> changes.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void TimeEvents_DayOfMonthChanged(object sender, EventArgs e)
{ {
//insert data read and data writer functions if I want better future optimization. this.BackupSaves(SaveBackup.NightlyBackupsPath);
string mydatastring = "Pre_Play_Backup";
string back_up_savefile = Path.Combine(pre_play_saves, mydatastring);
back_up_savefile = Path.Combine(pre_play_saves, mydatastring);
if (!Directory.Exists(saved_backups))
{
Directory.CreateDirectory(saved_backups);
Console.WriteLine("Making Backup Directory");
}
if (!Directory.Exists(pre_play_saves))
{
Directory.CreateDirectory(pre_play_saves);
Console.WriteLine("Making Backup Directory");
}
if (!Directory.Exists(sleeping_saves))
{
Directory.CreateDirectory(sleeping_saves);
Console.WriteLine("Making Backup Directory");
}
while (true)
{
pre_iterator++; //initial iterations
pre_iterator_string = pre_iterator.ToString(); //string conversion
if (File.Exists(back_up_savefile + pre_iterator_string +".zip")) continue; //if my file exists, go back to the top!
if (!File.Exists(back_up_savefile + pre_iterator_string + ".zip")) //if my file doesnt exist, make it!
{
pre_iterator_string = pre_iterator.ToString();
string newbackup = back_up_savefile + pre_iterator_string;
output = newbackup + ".zip";
ZipFile.CreateFromDirectory(stardew_save_path, output);
break;
}
}
var files = new DirectoryInfo(pre_play_saves).EnumerateFiles()
.OrderByDescending(f => f.CreationTime)
.Skip(save_num)
.ToList();
files.ForEach(f => f.Delete());
} }
static void Sleep_Save_Backup(object sender, EventArgs e) /// <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)
{ {
//insert data read and data writer functions if I want better future optimization. // back up saves
string mydatastring = "Nightly_Backup"; Directory.CreateDirectory(folderPath);
string back_up_savefile = Path.Combine(pre_play_saves, mydatastring); ZipFile.CreateFromDirectory(SaveBackup.SavesPath, Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}.zip"));
back_up_savefile = Path.Combine(sleeping_saves, mydatastring);
// delete old backups
//This shouldn't run, but just incase... new DirectoryInfo(folderPath)
if (!Directory.Exists(saved_backups)) .EnumerateFiles()
{ .OrderByDescending(f => f.CreationTime)
Directory.CreateDirectory(saved_backups); .Skip(this.SaveCount)
Console.WriteLine("Making Backup Directory"); .ToList()
} .ForEach(file => file.Delete());
if (!Directory.Exists(pre_play_saves))
{
Directory.CreateDirectory(pre_play_saves);
Console.WriteLine("Making Backup Directory");
}
if (!Directory.Exists(sleeping_saves))
{
Directory.CreateDirectory(sleeping_saves);
Console.WriteLine("Making Backup Directory");
}
//string backup_path = Path.Combine(stardew_path, "Backed_Up_Saves");
while (true)
{
sleep_iterator++; //initial iterations
sleep_iterator_string = sleep_iterator.ToString(); //string conversion
if (File.Exists(back_up_savefile + sleep_iterator_string + ".zip")) continue; //if my file exists, go back to the top!
if (!File.Exists(back_up_savefile + sleep_iterator_string + ".zip")) //if my file doesnt exist, make it!
{
sleep_iterator_string = sleep_iterator.ToString();
string newbackup = back_up_savefile + sleep_iterator_string;
output = newbackup + ".zip";
ZipFile.CreateFromDirectory(stardew_save_path, output);
break;
}
}
var files = new DirectoryInfo(sleeping_saves).EnumerateFiles()
.OrderByDescending(f => f.CreationTime)
.Skip(save_num)
.ToList();
files.ForEach(f => f.Delete());
} }
/// <summary>Load the configuration settings.</summary>
void DataLoader() private void LoadConfig()
{ {
//loads the data to the variables upon loading the game. var path = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
var mylocation = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt"); if (File.Exists(path))
if (!File.Exists(mylocation)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{ {
Console.WriteLine("The config file for AutoSpeed was not found, guess I'll create it..."); string[] text = File.ReadAllLines(path);
this.SaveCount = Convert.ToInt32(text[3]);
}
else
{
// Console.WriteLine("HEY THERE IM LOADING DATA");
//loads the BuildEndurance_data upon loading the mod
string[] readtext = File.ReadAllLines(mylocation);
save_num = Convert.ToInt32(readtext[3]);
} }
} }
void MyWritter() /// <summary>Save the configuration settings.</summary>
private void WriteConfig()
{ {
//saves the BuildEndurance_data at the end of a new day; string path = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
var mylocation = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt"); string[] text = new string[20];
string[] mystring3 = new string[20]; text[0] = "Player: AutoBackup Config:";
if (!File.Exists(mylocation)) text[1] = "====================================================================================";
{ text[2] = "Number of Backups to Keep:";
Console.WriteLine("The data file for AutoBackup was not found, guess I'll create it when you sleep."); text[3] = this.SaveCount.ToString();
mystring3[0] = "Player: AutoBackup Config:"; File.WriteAllLines(path, text);
mystring3[1] = "====================================================================================";
mystring3[2] = "Number of Backups to Keep:";
mystring3[3] = save_num.ToString();
File.WriteAllLines(mylocation, mystring3);
}
else
{
// Console.WriteLine("HEY IM SAVING DATA");
//write out the info to a text file upon loading
mystring3[0] = "Player: AutoBackup Config:";
mystring3[1] = "====================================================================================";
mystring3[2] = "Number of Backups to Keep:";
mystring3[3] = save_num.ToString();
File.WriteAllLines(mylocation, mystring3);
}
} }
} }
} }