From 41ee8990f81a63c686953b2c28e7af8627fd8098 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 17 Feb 2017 11:33:22 -0500 Subject: [PATCH] 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. --- release-notes.md | 1 + .../Framework/Serialisation/JsonHelper.cs | 8 ++++- .../SelectiveStringEnumConverter.cs | 35 +++++++++++++++++++ .../StardewModdingAPI.csproj | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs diff --git a/release-notes.md b/release-notes.md index 6111776f..a277ee4f 100644 --- a/release-notes.md +++ b/release-notes.md @@ -15,6 +15,7 @@ For mod developers: * Added `SaveEvents.AfterReturnToTitle` and `TimeEvents.AfterDayStarted` events. * Added a simpler API for console commands (see `helper.ConsoleCommands`). * 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. * 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_. diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs index 26d937a5..d5f5bfd0 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.IO; +using Microsoft.Xna.Framework.Input; using Newtonsoft.Json; using StardewModdingAPI.Advanced; @@ -15,7 +17,11 @@ namespace StardewModdingAPI.Framework.Serialisation private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, - ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection values are duplicated each time the config is loaded + ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection values are duplicated each time the config is loaded + Converters = new List + { + new SelectiveStringEnumConverter(typeof(Buttons), typeof(Keys)) + } }; diff --git a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs new file mode 100644 index 00000000..e9c5496d --- /dev/null +++ b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json.Converters; + +namespace StardewModdingAPI.Framework.Serialisation +{ + /// A variant of which only converts certain enums. + internal class SelectiveStringEnumConverter : StringEnumConverter + { + /********* + ** Properties + *********/ + /// The enum type names to convert. + private readonly HashSet Types; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The enum types to convert. + public SelectiveStringEnumConverter(params Type[] types) + { + this.Types = new HashSet(types.Select(p => p.FullName)); + } + + /// Get whether this instance can convert the specified object type. + /// The object type. + public override bool CanConvert(Type type) + { + return base.CanConvert(type) && this.Types.Contains(type.FullName); + } + } +} diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 35dd6513..796980cb 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -153,6 +153,7 @@ +