write XNA input enums to JSON as strings automatically
Mods often reference Json.NET to do this, so this lets many mods remove Json.NET as a dependency.
This commit is contained in:
parent
1dfedd2d1a
commit
41ee8990f8
|
@ -15,6 +15,7 @@ For mod developers:
|
||||||
* Added `SaveEvents.AfterReturnToTitle` and `TimeEvents.AfterDayStarted` events.
|
* Added `SaveEvents.AfterReturnToTitle` and `TimeEvents.AfterDayStarted` events.
|
||||||
* Added a simpler API for console commands (see `helper.ConsoleCommands`).
|
* Added a simpler API for console commands (see `helper.ConsoleCommands`).
|
||||||
* Added `GetPrivateProperty` reflection helper.
|
* Added `GetPrivateProperty` reflection helper.
|
||||||
|
* SMAPI now writes XNA input enums (`Buttons` and `Keys`) to JSON as strings, so mods no longer need to add a `StringEnumConverter` themselves for those.
|
||||||
* Log files now always use `\r\n` to simplify crossplatform viewing.
|
* Log files now always use `\r\n` to simplify crossplatform viewing.
|
||||||
* Several obsolete APIs have been removed (see [deprecation guide](http://canimod.com/guides/updating-a-smapi-mod)),
|
* Several obsolete APIs have been removed (see [deprecation guide](http://canimod.com/guides/updating-a-smapi-mod)),
|
||||||
and all _notice_-level deprecations have been increased to _info_.
|
and all _notice_-level deprecations have been increased to _info_.
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using Microsoft.Xna.Framework.Input;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using StardewModdingAPI.Advanced;
|
using StardewModdingAPI.Advanced;
|
||||||
|
|
||||||
|
@ -15,7 +17,11 @@ namespace StardewModdingAPI.Framework.Serialisation
|
||||||
private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
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>
|
||||||
|
{
|
||||||
|
new SelectiveStringEnumConverter(typeof(Buttons), typeof(Keys))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace StardewModdingAPI.Framework.Serialisation
|
||||||
|
{
|
||||||
|
/// <summary>A variant of <see cref="StringEnumConverter"/> which only converts certain enums.</summary>
|
||||||
|
internal class SelectiveStringEnumConverter : StringEnumConverter
|
||||||
|
{
|
||||||
|
/*********
|
||||||
|
** Properties
|
||||||
|
*********/
|
||||||
|
/// <summary>The enum type names to convert.</summary>
|
||||||
|
private readonly HashSet<string> Types;
|
||||||
|
|
||||||
|
|
||||||
|
/*********
|
||||||
|
** 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>
|
||||||
|
/// <param name="type">The object type.</param>
|
||||||
|
public override bool CanConvert(Type type)
|
||||||
|
{
|
||||||
|
return base.CanConvert(type) && this.Types.Contains(type.FullName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -153,6 +153,7 @@
|
||||||
<Compile Include="Framework\Reflection\PrivateProperty.cs" />
|
<Compile Include="Framework\Reflection\PrivateProperty.cs" />
|
||||||
<Compile Include="Framework\RequestExitDelegate.cs" />
|
<Compile Include="Framework\RequestExitDelegate.cs" />
|
||||||
<Compile Include="Framework\Serialisation\JsonHelper.cs" />
|
<Compile Include="Framework\Serialisation\JsonHelper.cs" />
|
||||||
|
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
|
||||||
<Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />
|
<Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />
|
||||||
<Compile Include="ICommandHelper.cs" />
|
<Compile Include="ICommandHelper.cs" />
|
||||||
<Compile Include="IModRegistry.cs" />
|
<Compile Include="IModRegistry.cs" />
|
||||||
|
|
Loading…
Reference in New Issue