Skip to content

Commit

Permalink
Fixed (or now notifies user of) errors in stylesheets
Browse files Browse the repository at this point in the history
- Stylesheet errors are now saved to a file, the user is notified and the default file is loaded. Some poor soul has tried to star the program hundreds of times to no avail, due to a single quotation mark missing in their StyleSettings.yaml file.
  • Loading branch information
TCNOco committed Jul 1, 2021
1 parent 61ebde0 commit a866793
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
26 changes: 25 additions & 1 deletion TcNo-Acc-Switcher-Client/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 49 additions & 11 deletions TcNo-Acc-Switcher-Server/Data/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}
}

/// <summary>
/// Load stylesheet settings from stylesheet file.
/// </summary>
public void LoadStylesheetFromFile()
public bool LoadStylesheetFromFile()
{
if (!File.Exists(StylesheetFile))
{
Expand All @@ -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;
Expand Down Expand Up @@ -544,11 +587,6 @@ public void LoadStylesheetFromFile()
}
}
}

// Get name of current stylesheet
GetCurrentStylesheet();
if (WindowsAccent)
SetAccentColor();
}

/// <summary>
Expand Down

0 comments on commit a866793

Please sign in to comment.