simplify SelectiveStringEnumConverter implementation
This commit is contained in:
parent
2ff9373971
commit
51a2c3991f
|
@ -1,9 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using StardewModdingAPI.Utilities;
|
|
||||||
|
|
||||||
namespace StardewModdingAPI.Framework.Serialisation
|
namespace StardewModdingAPI.Framework.Serialisation
|
||||||
{
|
{
|
||||||
|
@ -20,7 +19,9 @@ namespace StardewModdingAPI.Framework.Serialisation
|
||||||
ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection<T> values are duplicated each time the config is loaded
|
ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection<T> values are duplicated each time the config is loaded
|
||||||
Converters = new List<JsonConverter>
|
Converters = new List<JsonConverter>
|
||||||
{
|
{
|
||||||
new SelectiveStringEnumConverter(typeof(Buttons), typeof(Keys), typeof(SButton))
|
new SelectiveStringEnumConverter<Buttons>(),
|
||||||
|
new SelectiveStringEnumConverter<Keys>(),
|
||||||
|
new SelectiveStringEnumConverter<SButton>()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,37 +1,22 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
namespace StardewModdingAPI.Framework.Serialisation
|
namespace StardewModdingAPI.Framework.Serialisation
|
||||||
{
|
{
|
||||||
/// <summary>A variant of <see cref="StringEnumConverter"/> which only converts certain enums.</summary>
|
/// <summary>A variant of <see cref="StringEnumConverter"/> which only converts a specified enum.</summary>
|
||||||
internal class SelectiveStringEnumConverter : StringEnumConverter
|
/// <typeparam name="T">The enum type.</typeparam>
|
||||||
|
internal class SelectiveStringEnumConverter<T> : StringEnumConverter
|
||||||
{
|
{
|
||||||
/*********
|
|
||||||
** Properties
|
|
||||||
*********/
|
|
||||||
/// <summary>The enum type names to convert.</summary>
|
|
||||||
private readonly HashSet<string> Types;
|
|
||||||
|
|
||||||
|
|
||||||
/*********
|
/*********
|
||||||
** Public methods
|
** Public methods
|
||||||
*********/
|
*********/
|
||||||
/// <summary>Construct an instance.</summary>
|
|
||||||
/// <param name="types">The enum types to convert.</param>
|
|
||||||
public SelectiveStringEnumConverter(params Type[] types)
|
|
||||||
{
|
|
||||||
this.Types = new HashSet<string>(types.Select(p => p.FullName));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Get whether this instance can convert the specified object type.</summary>
|
/// <summary>Get whether this instance can convert the specified object type.</summary>
|
||||||
/// <param name="type">The object type.</param>
|
/// <param name="type">The object type.</param>
|
||||||
public override bool CanConvert(Type type)
|
public override bool CanConvert(Type type)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
base.CanConvert(type)
|
base.CanConvert(type)
|
||||||
&& this.Types.Contains((Nullable.GetUnderlyingType(type) ?? type).FullName);
|
&& (Nullable.GetUnderlyingType(type) ?? type) == typeof(T);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue