Skip to content

Commit

Permalink
Merge pull request #23 from jxy-s/user-settings-clean-exit
Browse files Browse the repository at this point in the history
handle corrupt user settings cleanly
  • Loading branch information
jxy-s authored Oct 27, 2022
2 parents 21c6e1a + b5648c8 commit 7ce21d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
24 changes: 23 additions & 1 deletion VMPlex/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;

namespace VMPlex
{
Expand All @@ -9,9 +10,30 @@ public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
LoadUserSettings();
Utility.TryExtractHVIntegrate();
base.OnStartup(e);
Utility.CreateSelfJob();
}

private void LoadUserSettings()
{
try
{
UserSettings.Instance.Load();
}
catch (Exception exc)
{
MessageBox.Show(
$"Failed to load settings file \"{UserSettings.Instance.UserSettingsFile}\"\n{exc.Message}",
"VMPlex Fatal Settings Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
//
// Unable to parse settings. Error already displayed. Quietly exit.
//
Environment.Exit(1);
}
}
}
}
18 changes: 2 additions & 16 deletions VMPlex/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public enum AudioRedirectionModeSetting
public class UserSettings : INotifyPropertyChanged
{
public Settings Settings { get { lock (Lock) { return ActiveSettings; } } }
public string UserSettingsFile { get; private set; } = Path.GetFullPath("vmplex-settings.json");

public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyChange(string Name = null)
Expand Down Expand Up @@ -221,20 +222,6 @@ public UserSettings()
SettingsFileWatcher.Deleted += OnChanged;
SettingsFileWatcher.Created += OnChanged;
SettingsFileWatcher.Renamed += OnRenamed;

try
{
Load();
}
catch (Exception exc)
{
MessageBox.Show(
$"Failed to load settings file \"{UserSettingsFile}\"\n{exc.Message}",
"VMPlex Fatal Settings Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
throw exc;
}
}

public void OpenInEditor()
Expand Down Expand Up @@ -275,7 +262,7 @@ public Settings Mutate(Func<Settings, Settings> Mutator)
return settings;
}

private void Load()
public void Load()
{
lock (Lock)
{
Expand Down Expand Up @@ -353,7 +340,6 @@ private void ReloadOnChange()
private object Lock = new object();
private Settings ActiveSettings = new Settings();
private FileSystemWatcher SettingsFileWatcher;
private string UserSettingsFile = Path.GetFullPath("vmplex-settings.json");
private JsonSerializerOptions JsonSerializeOpts = new JsonSerializerOptions
{
WriteIndented = true,
Expand Down

0 comments on commit 7ce21d5

Please sign in to comment.