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.Linq;
using StardewModdingAPI;
using StardewModdingAPI.Events;
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");
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
static string pre_play_saves = Path.Combine(saved_backups, "Pre_Play_Saves");
static string sleeping_saves = Path.Combine(saved_backups, "Nightly_InGame_Saves");
static string backup_path = Path.Combine(stardew_path, "Backed_Up_Saves");
/*********
** 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");
public static string output;
static int pre_iterator = 0;
static int sleep_iterator =0;
static string pre_iterator_string = pre_iterator.ToString();
static string sleep_iterator_string = sleep_iterator.ToString();
/// <summary>The folder path containing the game's saves.</summary>
private static readonly string SavesPath = Path.Combine(SaveBackup.AppDataPath, "Saves");
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)
{
DataLoader();
MyWritter();
Save_Backup(); //Yup I wrote it all in one function.... kinda
this.LoadConfig();
this.WriteConfig();
StardewModdingAPI.Events.TimeEvents.DayOfMonthChanged += Sleep_Save_Backup;
this.BackupSaves(SaveBackup.PrePlayBackupsPath);
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.
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());
this.BackupSaves(SaveBackup.NightlyBackupsPath);
}
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.
string mydatastring = "Nightly_Backup";
string back_up_savefile = Path.Combine(pre_play_saves, mydatastring);
back_up_savefile = Path.Combine(sleeping_saves, mydatastring);
// back up saves
Directory.CreateDirectory(folderPath);
ZipFile.CreateFromDirectory(SaveBackup.SavesPath, Path.Combine(folderPath, $"backup-{DateTime.Now:yyyyMMdd'-'HHmmss}.zip"));
//This shouldn't run, but just incase...
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");
}
//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());
// delete old backups
new DirectoryInfo(folderPath)
.EnumerateFiles()
.OrderByDescending(f => f.CreationTime)
.Skip(this.SaveCount)
.ToList()
.ForEach(file => file.Delete());
}
void DataLoader()
/// <summary>Load the configuration settings.</summary>
private void LoadConfig()
{
//loads the data to the variables upon loading the game.
var mylocation = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
if (!File.Exists(mylocation)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
var path = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
if (File.Exists(path))
{
Console.WriteLine("The config file for AutoSpeed was not found, guess I'll create it...");
}
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]);
string[] text = File.ReadAllLines(path);
this.SaveCount = Convert.ToInt32(text[3]);
}
}
void MyWritter()
/// <summary>Save the configuration settings.</summary>
private void WriteConfig()
{
//saves the BuildEndurance_data at the end of a new day;
var mylocation = Path.Combine(Helper.DirectoryPath, "AutoBackup_data.txt");
string[] mystring3 = new string[20];
if (!File.Exists(mylocation))
{
Console.WriteLine("The data file for AutoBackup was not found, guess I'll create it when you sleep.");
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();
mystring3[0] = "Player: AutoBackup Config:";
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);
}
File.WriteAllLines(path, text);
}
}
}

View File

@ -11,4 +11,4 @@
"UniqueID": "4be88c18-b6f3-49b0-ba96-f94b1a5be890",
"PerSaveConfigs": false,
"EntryDll": "SaveBackup.dll"
}
}