Skip to content

Commit

Permalink
Show details about JSON error if theme fails to load
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed May 27, 2020
1 parent e2cafab commit 44ccd6c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
32 changes: 0 additions & 32 deletions src/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ public class AppConfig : INotifyPropertyChanged
public bool fullScreenPause { get; set; }
public bool enableScripts { get; set; }
}

public class ThemeConfig
{
public string themeId { get; set; }
public string displayName { get; set; }
public string imageFilename { get; set; }
public string imageCredits { get; set; }
public int? dayHighlight { get; set; }
public int? nightHighlight { get; set; }
public int[] sunriseImageList { get; set; }
public int[] dayImageList { get; set; }
public int[] sunsetImageList { get; set; }
public int[] nightImageList { get; set; }
}
#nullable restore

class JsonConfig
Expand Down Expand Up @@ -101,24 +87,6 @@ public static void LoadConfig()
autoSaveTimer.Elapsed += OnAutoSaveTimerElapsed;
}

public static ThemeConfig LoadTheme(string name)
{
ThemeConfig theme;
string jsonText = File.ReadAllText(Path.Combine("themes", name, "theme.json"));

try
{
theme = JsonConvert.DeserializeObject<ThemeConfig>(jsonText);
}
catch (JsonException)
{
return null;
}

theme.themeId = name;
return theme;
}

public static void ReloadConfig()
{
restartPending = true;
Expand Down
4 changes: 2 additions & 2 deletions src/ThemeError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public InvalidImageInThemeJSON(string themeId, int imageId, string filename) : b

class InvalidThemeJSON : ThemeError
{
public InvalidThemeJSON(string themeId) : base(themeId)
public InvalidThemeJSON(string themeId, string message) : base(themeId)
{
errorMsg = _("Could not read theme JSON file because its format is invalid");
errorMsg = string.Format(_("Could not read theme JSON file: {0}"), message);
}
}

Expand Down
44 changes: 32 additions & 12 deletions src/ThemeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,53 @@
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace WinDynamicDesktop
{
#nullable disable
public class ThemeConfig
{
public string themeId { get; set; }
public string displayName { get; set; }
public string imageFilename { get; set; }
public string imageCredits { get; set; }
public int? dayHighlight { get; set; }
public int? nightHighlight { get; set; }
public int[] sunriseImageList { get; set; }
public int[] dayImageList { get; set; }
public int[] sunsetImageList { get; set; }
public int[] nightImageList { get; set; }
}
#nullable restore

class ThemeLoader
{
private static readonly Func<string, string> _ = Localization.GetTranslation;
public static IntPtr taskbarHandle = IntPtr.Zero;

public static ThemeResult TryLoad(string themeId)
{
if (!File.Exists(Path.Combine("themes", themeId, "theme.json")))
string jsonPath = Path.Combine("themes", themeId, "theme.json");

if (!File.Exists(jsonPath))
{
return new ThemeResult(new NoThemeJSON(themeId));
}
else
{
ThemeConfig theme = JsonConfig.LoadTheme(themeId);

if (theme == null)
{
return new ThemeResult(new InvalidThemeJSON(themeId));
}
else
{
return ThemeJsonValidator.ValidateQuick(theme);
}
ThemeConfig theme;

try
{
theme = JsonConvert.DeserializeObject<ThemeConfig>(File.ReadAllText(jsonPath));
}
catch (JsonException e)
{
return new ThemeResult(new InvalidThemeJSON(themeId, e.Message));
}

theme.themeId = themeId;
return ThemeJsonValidator.ValidateQuick(theme);
}

public static void HandleError(ThemeError e)
Expand Down

0 comments on commit 44ccd6c

Please sign in to comment.