Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Fixes hang when settings is corrupt.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonBlade committed Oct 19, 2019
1 parent 88ade09 commit 4825c9b
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 72 deletions.
28 changes: 27 additions & 1 deletion PaisleyPark/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;

namespace PaisleyPark.Models
{
Expand Down Expand Up @@ -62,19 +63,36 @@ public class Settings : INotifyPropertyChanged
/// </summary>
public ObservableCollection<Preset> Presets { get; set; } = new ObservableCollection<Preset>();

/// <summary>
/// Class logger.
/// </summary>
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

/// <summary>
/// Saves the settings to the file.
/// </summary>
/// <param name="settings">Settings to save to the user file.</param>
public static void Save(Settings settings)
{
if (settings == null)
{
logger.Error("Settings save is null!");
MessageBox.Show("Cannot save settings as it would become corrupt!", "Paisley Park", MessageBoxButton.OK, MessageBoxImage.Error);
throw new Exception("Settings passed is null");
}

// Does the settings folder exist? If not, create Settings folder.
if (!Directory.Exists(SETTINGS_FOLDER))
Directory.CreateDirectory(SETTINGS_FOLDER);

// Get the full path to the settings file.
var fullPath = Path.Combine(SETTINGS_FOLDER, SETTINGS_FILE);

// Store three backups
File.Copy(fullPath + "2.bak", fullPath + "3.bak");
File.Copy(fullPath + "1.bak", fullPath + "2.bak");
File.Copy(fullPath, fullPath + "1.bak");

// Create StreamWriter instance to save file contents into full path.
using (var text = File.CreateText(fullPath))
{
Expand All @@ -90,7 +108,7 @@ public static void Save(Settings settings)
public static Settings Load()
{
// Create the return value.
Settings settings = new Settings();
var settings = new Settings();

// Does the settings folder exist? If not, create Settings folder.
if (!Directory.Exists(SETTINGS_FOLDER))
Expand All @@ -103,6 +121,14 @@ public static Settings Load()
if (File.Exists(fullPath))
settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(fullPath));

if (settings == null)
{
logger.Error("Current settings file is corrupt!");
File.Copy(fullPath, fullPath + ".bak");
MessageBox.Show("Your settings file is corrupt, a new settings file is being created. You may attempt to restore settings from backups. Please ask in Discord if you have questions.", "Paisley Park", MessageBoxButton.OK, MessageBoxImage.Warning);
settings = new Settings();
}

// Return our settings.
return settings;
}
Expand Down
Loading

0 comments on commit 4825c9b

Please sign in to comment.