Skip to content

Commit

Permalink
UI: Try to better recover from unexpected errors while opening the ma…
Browse files Browse the repository at this point in the history
…in window
  • Loading branch information
SourMesen committed Jan 5, 2025
1 parent ea657c1 commit b3fd159
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
9 changes: 8 additions & 1 deletion UI/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public override void OnFrameworkInitializationCompleted()
new PreferencesConfig().InitializeFontDefaults();
desktop.MainWindow = new SetupWizardWindow();
} else {
desktop.MainWindow = new MainWindow();
try {
desktop.MainWindow = new MainWindow();
} catch {
//Something broke when trying to load the main window, the settings file might be invalid/broken, try to reset them
Configuration.BackupSettings(ConfigManager.ConfigFile);
ConfigManager.ResetSettings(false);
desktop.MainWindow = new MainWindow();
}
}
}
base.OnFrameworkInitializationCompleted();
Expand Down
12 changes: 9 additions & 3 deletions UI/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,19 @@ public static Configuration Config
}
}

public static void ResetSettings()
public static void ResetSettings(bool initDefaults = true)
{
DefaultKeyMappingType defaultMappings = Config.DefaultKeyMappings;
if(defaultMappings == DefaultKeyMappingType.None) {
defaultMappings = DefaultKeyMappingType.Xbox | DefaultKeyMappingType.ArrowKeys;
}

_config = Configuration.CreateConfig();
Config.DefaultKeyMappings = defaultMappings;
Config.InitializeDefaults();
Config.ConfigUpgrade = (int)ConfigUpgradeHint.NextValue - 1;
if(initDefaults) {
Config.InitializeDefaults();
Config.ConfigUpgrade = (int)ConfigUpgradeHint.NextValue - 1;
}
Config.Save();
Config.ApplyConfig();
}
Expand Down
14 changes: 10 additions & 4 deletions UI/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ public static Configuration Deserialize(string configFile)
} catch {
try {
//File exists but couldn't be loaded, make a backup of the old settings before we overwrite them
string? folder = Path.GetDirectoryName(configFile);
if(folder != null) {
File.Copy(configFile, Path.Combine(folder, "settings." + DateTime.Now.ToString("yyyy-M-dd_HH-mm-ss") + ".bak"), true);
}
BackupSettings(configFile);
} catch { }

config = Configuration.CreateConfig();
Expand All @@ -271,6 +268,15 @@ public static Configuration Deserialize(string configFile)
return config;
}

public static void BackupSettings(string configFile)
{
//File exists but couldn't be loaded, make a backup of the old settings before we overwrite them
string? folder = Path.GetDirectoryName(configFile);
if(folder != null) {
File.Copy(configFile, Path.Combine(folder, "settings." + DateTime.Now.ToString("yyyy-M-dd_HH-mm-ss") + ".bak"), true);
}
}

public void Serialize(string configFile)
{
try {
Expand Down
7 changes: 4 additions & 3 deletions UI/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MainWindow : MesenWindow
private NotificationListener? _listener = null;
private ShortcutHandler _shortcutHandler;

private MouseManager _mouseManager;
private MouseManager? _mouseManager = null;
private ContentControl _audioPlayer;
private MainMenuView _mainMenu;
private CommandLineHelper? _cmdLine;
Expand Down Expand Up @@ -106,7 +106,6 @@ public MainWindow()
_softwareRenderer = this.GetControl<SoftwareRendererView>("SoftwareRenderer");
_audioPlayer = this.GetControl<ContentControl>("AudioPlayer");
_mainMenu = this.GetControl<MainMenuView>("MainMenu");
_mouseManager = new MouseManager(this, _usesSoftwareRenderer ? _softwareRenderer : _renderer, _mainMenu, _usesSoftwareRenderer);
ConfigManager.Config.MainWindow.LoadWindowSettings(this);

#if DEBUG
Expand Down Expand Up @@ -180,7 +179,7 @@ private async void ValidateExit()
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_mouseManager.Dispose();
_mouseManager?.Dispose();
}

private void OnDrop(object? sender, DragEventArgs e)
Expand All @@ -204,6 +203,8 @@ protected override void OnOpened(EventArgs e)
return;
}

_mouseManager = new MouseManager(this, _usesSoftwareRenderer ? _softwareRenderer : _renderer, _mainMenu, _usesSoftwareRenderer);

ConfigManager.Config.InitializeFontDefaults();
ConfigManager.Config.Preferences.ApplyFontOptions();
ConfigManager.Config.Debug.Fonts.ApplyConfig();
Expand Down

0 comments on commit b3fd159

Please sign in to comment.