diff --git a/TcNo-Acc-Switcher-Client/App.xaml.cs b/TcNo-Acc-Switcher-Client/App.xaml.cs index 413cdb722..550293ce3 100644 --- a/TcNo-Acc-Switcher-Client/App.xaml.cs +++ b/TcNo-Acc-Switcher-Client/App.xaml.cs @@ -199,7 +199,31 @@ protected override async void OnStartup(StartupEventArgs e) private static void IsRunningAlready() { - AppSettings.Instance.LoadFromFile(); + if (!AppSettings.Instance.LoadFromFile()) + { + if (File.Exists("StyleSettings_ErrorInfo.txt")) + { + var errorText = File.ReadAllText("StyleSettings_ErrorInfo.txt").Split("\n"); + MessageBox.Show( + "Could not load StyleSettings.json! Error details: " + Environment.NewLine + + errorText[0] + Environment.NewLine + Environment.NewLine + + "The default file will be loaded." + Environment.NewLine + + "See \"StyleSettings_broken.yaml\" for the broken style settings." + Environment.NewLine + + "See \"StyleSettings_ErrorInfo.txt\" for the full error message. Above is only the first line.", "Failed to load Styles", MessageBoxButton.OK, + MessageBoxImage.Error); + try + { + AppSettings.Instance.LoadFromFile(); + } + catch (Exception e) + { + MessageBox.Show( + "Another unexpected error occurred! Error details: " + Environment.NewLine + + e, "Failed to load Styles (2)", MessageBoxButton.OK, MessageBoxImage.Error); + throw; + } + } + } try { // Check if program is running, if not: return. diff --git a/TcNo-Acc-Switcher-Server/Data/AppSettings.cs b/TcNo-Acc-Switcher-Server/Data/AppSettings.cs index 757726917..0e3fdd1c4 100644 --- a/TcNo-Acc-Switcher-Server/Data/AppSettings.cs +++ b/TcNo-Acc-Switcher-Server/Data/AppSettings.cs @@ -417,14 +417,14 @@ public void SetFromJObject(JObject j) CheckShortcuts(); } - public void LoadFromFile() + public bool LoadFromFile() { Globals.DebugWriteLine(@"[Func:Data\AppSettings.LoadFromFile]"); // Main settings if (!File.Exists(SettingsFile)) SaveSettings(); else SetFromJObject(GeneralFuncs.LoadSettings(SettingsFile, GetJObject())); // Stylesheet - LoadStylesheetFromFile(); + return LoadStylesheetFromFile(); } #region STYLESHEET @@ -435,14 +435,22 @@ public void LoadFromFile() public async System.Threading.Tasks.Task SwapStylesheet(string swapTo) { File.Copy($"themes\\{swapTo.Replace(' ', '_')}.yaml", StylesheetFile, true); - LoadStylesheetFromFile(); - await AppData.ReloadPage(); - } + try + { + if (LoadStylesheetFromFile()) await AppData.ReloadPage(); + else throw new Exception(); // Jump to the error display + } + catch (Exception) + { + GeneralInvocableFuncs.ShowToast("error", "Failed to load stylesheet! See documents folder for details.", + "Stylesheet error", "toastarea"); + } + } /// /// Load stylesheet settings from stylesheet file. /// - public void LoadStylesheetFromFile() + public bool LoadStylesheetFromFile() { if (!File.Exists(StylesheetFile)) { @@ -457,6 +465,41 @@ public void LoadStylesheetFromFile() "You can run the Updater in the \"updater\" folder to verify files, and restore these missing files."); } } + + try + { + LoadStylesheet(); + } + catch (YamlDotNet.Core.SyntaxErrorException e) + { + File.Copy(StylesheetFile, "StyleSettings_broken.yaml", true); + if (File.Exists("StyleSettings_ErrorInfo.txt")) File.Delete("StyleSettings_ErrorInfo.txt"); + File.WriteAllText("StyleSettings_ErrorInfo.txt", e.ToString()); + + if (File.Exists("themes\\Default.yaml")) + File.Copy("themes\\Default.yaml", StylesheetFile, true); + else if (File.Exists(Path.Join(Globals.AppDataFolder, "themes\\Default.yaml"))) + File.Copy(Path.Join(Globals.AppDataFolder, "themes\\Default.yaml"), StylesheetFile, true); + else + { + throw new Exception( + "A syntax error was encountered (more details below)! AND:" + Environment.NewLine + + "Could not find \"themes\" folder in TcNo Account Switcher's directory. This (especially Default.yaml) is required for this software to run." + Environment.NewLine + + "You can run the Updater in the \"updater\" folder to verify files, and restore these missing files." + Environment.NewLine + Environment.NewLine + e); + } + return false; + } + + // Get name of current stylesheet + GetCurrentStylesheet(); + if (WindowsAccent) + SetAccentColor(); + + return true; + } + + private void LoadStylesheet() + { // Load new stylesheet var desc = new DeserializerBuilder().WithNamingConvention(HyphenatedNamingConvention.Instance).Build(); var attempts = 0; @@ -544,11 +587,6 @@ public void LoadStylesheetFromFile() } } } - - // Get name of current stylesheet - GetCurrentStylesheet(); - if (WindowsAccent) - SetAccentColor(); } ///