Skip to content

Commit

Permalink
Fixed crashes when user edits preference file
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Mar 24, 2021
1 parent a442edc commit 57307fa
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion PixiEditor/Models/UserPreferences/PreferencesSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,22 @@ public void AddCallback(string setting, Action<object> action)
try
{
return Preferences.ContainsKey(name)
<<<<<<< HEAD
? (T)Convert.ChangeType(Preferences[name], typeof(T))
: fallbackValue;
}
catch (InvalidCastException)
{
=======
? (T)Preferences[name]
: fallbackValue;
}
catch (InvalidCastException)
{
Preferences.Remove(name);
Save();

>>>>>>> ab87ace (Fixed crashes when user edits preference file)
return fallbackValue;
}
}
Expand All @@ -146,11 +157,21 @@ public void AddCallback(string setting, Action<object> action)
try
{
return LocalPreferences.ContainsKey(name)
<<<<<<< HEAD
? (T)Convert.ChangeType(LocalPreferences[name], typeof(T))
=======
? (T)LocalPreferences[name]
>>>>>>> ab87ace (Fixed crashes when user edits preference file)
: fallbackValue;
}
catch (InvalidCastException)
{
<<<<<<< HEAD
=======
LocalPreferences.Remove(name);
Save();

>>>>>>> ab87ace (Fixed crashes when user edits preference file)
return fallbackValue;
}
}
Expand Down Expand Up @@ -181,7 +202,13 @@ private static string GetPathToSettings(Environment.SpecialFolder folder, string
else
{
string json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

// dictionary is null if the user deletes the content of the preference file.
if (dictionary != null)
{
return dictionary;
}
}

return new Dictionary<string, object>();
Expand Down

0 comments on commit 57307fa

Please sign in to comment.