Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle corrupt user settings cleanly #23

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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