From a1ae05bc287330e54c14b2545a22dc76e305c8c4 Mon Sep 17 00:00:00 2001 From: Kittenji <41535779+ChrisFeline@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:42:04 -0400 Subject: [PATCH] Save Version Checks --- Models/History.cs | 4 +--- Models/SaveData.cs | 35 +++++++++++++++++------------------ Program.cs | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Models/History.cs b/Models/History.cs index 92e6583..5036074 100644 --- a/Models/History.cs +++ b/Models/History.cs @@ -53,9 +53,7 @@ [JsonIgnore] public List Database m_Database = JsonConvert.DeserializeObject>(content) ?? new List(); } catch { - if (!string.IsNullOrEmpty(path) && File.Exists(path)) - File.Copy(path, path + $".backup-{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}-{DateTime.Now.Hour}{DateTime.Now.Minute}{DateTime.Now.Second}"); - + Program.CreateFileBackup(path); m_Database = new List(); } diff --git a/Models/SaveData.cs b/Models/SaveData.cs index 33cb42e..e90e421 100644 --- a/Models/SaveData.cs +++ b/Models/SaveData.cs @@ -7,6 +7,8 @@ namespace ToNSaveManager.Models { internal class SaveData { + const int CURRENT_VERSION = 2; + const string LegacyDestination = "data.json"; const string FileName = "SaveData.json"; static string DefaultLocation = Path.Combine(Program.DataLocation, FileName); @@ -47,6 +49,9 @@ public void SetCompleted(string name, bool value) public bool ShouldSerializeObjectives() => false; #endregion + public int Version = 0; + public static SaveData Empty => new SaveData() { Version = CURRENT_VERSION }; + [JsonIgnore] public int Count => Collection.Count; [JsonIgnore] public bool IsDirty { get; private set; } @@ -215,11 +220,7 @@ public static SaveData Import() { MessageBox.Show("Error trying to import your save:\n\n" + ex, "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - try - { - if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) - File.Copy(filePath, filePath + $".backup-{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}-{DateTime.Now.Hour}{DateTime.Now.Minute}{DateTime.Now.Second}"); - } catch + if (!Program.CreateFileBackup(filePath)) { Application.Exit(); return new SaveData(); @@ -229,6 +230,13 @@ public static SaveData Import() if (data == null) data = new SaveData(); + if (data.Version != CURRENT_VERSION) + { + Program.CreateFileBackup(filePath); + data.Version = CURRENT_VERSION; + data.SetDirty(); + } + // Handle old save files if (File.Exists(LegacyDestination)) { @@ -247,7 +255,7 @@ public static SaveData Import() } // Force exporting to new file format - data.Export(true); + data.SetDirty(); // Rename the old file File.Move(LegacyDestination, LegacyDestination + ".old", true); @@ -273,6 +281,7 @@ public static SaveData Import() for (j = 0; j < item.Entries.Count; j++) { entry = item.Entries[j]; + item.Add(entry); int index = History.UniqueEntries.FindIndex(v => v.Timestamp == entry.Timestamp); if (index != -1) @@ -285,20 +294,9 @@ public static SaveData Import() History.UniqueEntries.Add(entry); } } - } - - for (i = 0; i < data.Count; i++) - { - History item = data[i]; - if (item.Entries.Count == 0) continue; - - for (j = 0; j < item.Entries.Count; j++) - { - entry = item.Entries[j]; - item.Add(entry); - } item.Entries.Clear(); + data.SetDirty(); } if (data.Objectives.Count > 0) @@ -320,6 +318,7 @@ public static SaveData Import() } #pragma warning restore CS0612 + data.Export(); return data; } diff --git a/Program.cs b/Program.cs index 9e6e991..d1253f6 100644 --- a/Program.cs +++ b/Program.cs @@ -154,6 +154,23 @@ internal static bool StartCheckForUpdate(bool showUpToDate = false) return false; } + + internal static bool CreateFileBackup(string filePath) + { + Debug.WriteLine("Creating Backup For: " + filePath); + + try + { + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) + File.Copy(filePath, filePath + ".backup_" + DateTimeOffset.UtcNow.ToUnixTimeSeconds()); + + return true; + } + catch + { + return false; + } + } } public partial class MainWindow : Form