From 8f836be9972876ed74a545b1384c325ca39db75d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Jul 2017 20:53:26 -0400 Subject: [PATCH] refactor SaveBackup This commit formats/documents/simplifies code, standardises naming conventions, etc. --- GeneralMods/SaveBackup/Class1.cs | 237 ++++++++------------------- GeneralMods/SaveBackup/manifest.json | 2 +- 2 files changed, 67 insertions(+), 172 deletions(-) diff --git a/GeneralMods/SaveBackup/Class1.cs b/GeneralMods/SaveBackup/Class1.cs index 28966710..2df899da 100644 --- a/GeneralMods/SaveBackup/Class1.cs +++ b/GeneralMods/SaveBackup/Class1.cs @@ -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 + /// The mod entry point. + 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 + *********/ + /// The folder path containing the game's app data. + 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(); + /// The folder path containing the game's saves. + private static readonly string SavesPath = Path.Combine(SaveBackup.AppDataPath, "Saves"); - static int save_num = 30; + /// The folder path containing backups of the save before the player starts playing. + private static readonly string PrePlayBackupsPath = Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", "Pre_Play_Saves"); + /// The folder path containing nightly backups of the save. + private static readonly string NightlyBackupsPath = Path.Combine(SaveBackup.AppDataPath, "Backed_Up_Saves", "Nightly_InGame_Saves"); + + /// The number of save backups to keep for each type. + private int SaveCount = 30; + + + /********* + ** Public methods + *********/ + /// The mod entry point, called after the mod is first loaded. + /// Provides simplified APIs for writing mods. 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 + *********/ + /// The method invoked when changes. + /// The event sender. + /// The event data. + 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) + /// Back up saves to the specified folder. + /// The folder path in which to generate saves. + 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() + /// Load the configuration settings. + 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() + /// Save the configuration settings. + 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); } - - } - - - } diff --git a/GeneralMods/SaveBackup/manifest.json b/GeneralMods/SaveBackup/manifest.json index d1ab9cfd..54a57e77 100644 --- a/GeneralMods/SaveBackup/manifest.json +++ b/GeneralMods/SaveBackup/manifest.json @@ -11,4 +11,4 @@ "UniqueID": "4be88c18-b6f3-49b0-ba96-f94b1a5be890", "PerSaveConfigs": false, "EntryDll": "SaveBackup.dll" -} \ No newline at end of file +}