Skip to content

Commit

Permalink
Save Version Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFeline committed Jul 16, 2024
1 parent 22c4180 commit a1ae05b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
4 changes: 1 addition & 3 deletions Models/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ [JsonIgnore] public List<Entry> Database
m_Database = JsonConvert.DeserializeObject<List<Entry>>(content) ?? new List<Entry>();
} 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<Entry>();
}

Expand Down
35 changes: 17 additions & 18 deletions Models/SaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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; }

Expand Down Expand Up @@ -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();
Expand All @@ -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))
{
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -320,6 +318,7 @@ public static SaveData Import()
}
#pragma warning restore CS0612

data.Export();
return data;
}

Expand Down
17 changes: 17 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1ae05b

Please sign in to comment.