Skip to content

Commit

Permalink
Merge pull request #860 from dremin/settings-enum-defined
Browse files Browse the repository at this point in the history
Better validate that an enum value is defined
  • Loading branch information
dremin authored Jun 25, 2024
2 parents 8605def + 7af8cce commit 58c9d99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
8 changes: 2 additions & 6 deletions RetroBar/Utilities/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,20 @@ protected void Set<T>(ref T field, T value, [CallerMemberName] string propertyNa
{
if (!field.Equals(value))
{
// TODO: Should we log setting change?

field = value;
OnPropertyChanged(propertyName);
}
}

protected void SetEnum<T>(ref T field, T value, [CallerMemberName] string propertyName = "") where T : struct, Enum
protected void SetEnum<T>(ref T field, T value, [CallerMemberName] string propertyName = "") where T : Enum
{
if (!field.Equals(value))
{
if (Convert.ToInt32(value) < 0)
if (!Enum.IsDefined(typeof(T), value))
{
return;
}

// TODO: Should we log setting change?

field = value;
OnPropertyChanged(propertyName);
}
Expand Down
14 changes: 7 additions & 7 deletions RetroBar/Utilities/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class SettingsManager<T> : INotifyPropertyChanged where T : IMigratableSe
{
public event PropertyChangedEventHandler PropertyChanged;

private static JsonSerializerOptions options = new()
{
IgnoreReadOnlyProperties = true,
WriteIndented = true,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};

private string _fileName;

private T _settings;
Expand Down Expand Up @@ -66,13 +73,6 @@ private bool LoadFromFile()

private void SaveToFile()
{
JsonSerializerOptions options = new()
{
IgnoreReadOnlyProperties = true,
WriteIndented = true,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};

try
{
Directory.CreateDirectory(Path.GetDirectoryName(_fileName));
Expand Down

0 comments on commit 58c9d99

Please sign in to comment.