From 95a93a05b39d2b27b538ecdb0e6a18f28096c5c2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 7 Feb 2017 20:50:41 -0500 Subject: remove oldest deprecated code (#231) Since Stardew Valley 1.2 breaks most mods anyway, this commits removes the oldest deprecations and fixes the issues that are easiest for mods to update. See documentation for details. --- .../Serialisation/SemanticVersionConverter.cs | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/StardewModdingAPI/Framework/Serialisation/SemanticVersionConverter.cs (limited to 'src/StardewModdingAPI/Framework/Serialisation') diff --git a/src/StardewModdingAPI/Framework/Serialisation/SemanticVersionConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SemanticVersionConverter.cs new file mode 100644 index 00000000..52ec999e --- /dev/null +++ b/src/StardewModdingAPI/Framework/Serialisation/SemanticVersionConverter.cs @@ -0,0 +1,51 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace StardewModdingAPI.Framework.Serialisation +{ + /// Overrides how SMAPI reads and writes . + internal class SemanticVersionConverter : JsonConverter + { + /********* + ** Accessors + *********/ + /// Whether this converter can write JSON. + public override bool CanWrite => false; + + + /********* + ** Public methods + *********/ + /// Get whether this instance can convert the specified object type. + /// The object type. + public override bool CanConvert(Type objectType) + { + return objectType == typeof(ISemanticVersion); + } + + /// Reads the JSON representation of the object. + /// The JSON reader. + /// The object type. + /// The object being read. + /// The calling serializer. + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + JObject obj = JObject.Load(reader); + int major = obj.Value("MajorVersion"); + int minor = obj.Value("MinorVersion"); + int patch = obj.Value("PatchVersion"); + string build = obj.Value("Build"); + return new SemanticVersion(major, minor, patch, build); + } + + /// Writes the JSON representation of the object. + /// The JSON writer. + /// The value. + /// The calling serializer. + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new InvalidOperationException("This converter does not write JSON."); + } + } +} -- cgit From d1080a8b2b54c777a446f08d9ecd5b76b4b2561a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 13 Feb 2017 00:13:29 -0500 Subject: move core JSON logic out of mod helper (#199) This lets SMAPI parse manifest.json files without a mod helper, so we can pass the mod name into the helper. --- src/StardewModdingAPI/Framework/ModHelper.cs | 59 +++++------------ .../Framework/Serialisation/JsonHelper.cs | 73 ++++++++++++++++++++++ src/StardewModdingAPI/Program.cs | 12 ++-- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 4 files changed, 96 insertions(+), 49 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs (limited to 'src/StardewModdingAPI/Framework/Serialisation') diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index 2b562b4f..4a3d5ed5 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -1,8 +1,7 @@ using System; using System.IO; -using Newtonsoft.Json; -using StardewModdingAPI.Advanced; using StardewModdingAPI.Framework.Reflection; +using StardewModdingAPI.Framework.Serialisation; namespace StardewModdingAPI.Framework { @@ -12,12 +11,8 @@ namespace StardewModdingAPI.Framework /********* ** Properties *********/ - /// The JSON settings to use when serialising and deserialising files. - 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 - }; + /// Encapsulates SMAPI's JSON file parsing. + private readonly JsonHelper JsonHelper; /********* @@ -38,20 +33,24 @@ namespace StardewModdingAPI.Framework *********/ /// Construct an instance. /// The mod directory path. + /// Encapsulate SMAPI's JSON parsing. /// Metadata about loaded mods. - /// An argument is null or invalid. + /// An argument is null or empty. /// The path does not exist on disk. - public ModHelper(string modDirectory, IModRegistry modRegistry) + public ModHelper(string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry) { // validate - if (modRegistry == null) - throw new ArgumentException("The mod registry cannot be null."); if (string.IsNullOrWhiteSpace(modDirectory)) - throw new ArgumentException("The mod directory cannot be empty."); + throw new ArgumentNullException(nameof(modDirectory)); + if (jsonHelper == null) + throw new ArgumentNullException(nameof(jsonHelper)); + if (modRegistry == null) + throw new ArgumentNullException(nameof(modRegistry)); if (!Directory.Exists(modDirectory)) throw new InvalidOperationException("The specified mod directory does not exist."); // initialise + this.JsonHelper = jsonHelper; this.DirectoryPath = modDirectory; this.ModRegistry = modRegistry; } @@ -88,28 +87,8 @@ namespace StardewModdingAPI.Framework public TModel ReadJsonFile(string path) where TModel : class { - // read file - string fullPath = Path.Combine(this.DirectoryPath, path); - string json; - try - { - json = File.ReadAllText(fullPath); - } - catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException) - { - return null; - } - - // deserialise model - TModel model = JsonConvert.DeserializeObject(json, this.JsonSettings); - if (model is IConfigFile) - { - var wrapper = (IConfigFile)model; - wrapper.ModHelper = this; - wrapper.FilePath = path; - } - - return model; + path = Path.Combine(this.DirectoryPath, path); + return this.JsonHelper.ReadJsonFile(path, this); } /// Save to a JSON file. @@ -120,15 +99,7 @@ namespace StardewModdingAPI.Framework where TModel : class { path = Path.Combine(this.DirectoryPath, path); - - // create directory if needed - string dir = Path.GetDirectoryName(path); - if (!Directory.Exists(dir)) - Directory.CreateDirectory(dir); - - // write file - string json = JsonConvert.SerializeObject(model, this.JsonSettings); - File.WriteAllText(path, json); + this.JsonHelper.WriteJsonFile(path, model); } } } diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs new file mode 100644 index 00000000..3809666f --- /dev/null +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -0,0 +1,73 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using StardewModdingAPI.Advanced; + +namespace StardewModdingAPI.Framework.Serialisation +{ + /// Encapsulates SMAPI's JSON file parsing. + public class JsonHelper + { + /********* + ** Accessors + *********/ + /// The JSON settings to use when serialising and deserialising files. + 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 + }; + + + /********* + ** Public methods + *********/ + /// Read a JSON file. + /// The model type. + /// The absolete file path. + /// The mod helper to inject for instances. + /// Returns the deserialised model, or null if the file doesn't exist or is empty. + public TModel ReadJsonFile(string fullPath, IModHelper modHelper) + where TModel : class + { + // read file + string json; + try + { + json = File.ReadAllText(fullPath); + } + catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException) + { + return null; + } + + // deserialise model + TModel model = JsonConvert.DeserializeObject(json, this.JsonSettings); + if (model is IConfigFile) + { + var wrapper = (IConfigFile)model; + wrapper.ModHelper = modHelper; + wrapper.FilePath = fullPath; + } + + return model; + } + + /// Save to a JSON file. + /// The model type. + /// The absolete file path. + /// The model to save. + public void WriteJsonFile(string fullPath, TModel model) + where TModel : class + { + // create directory if needed + string dir = Path.GetDirectoryName(fullPath); + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + // write file + string json = JsonConvert.SerializeObject(model, this.JsonSettings); + File.WriteAllText(fullPath, json); + } + } +} diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index f4518e21..b86e186f 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -15,6 +15,7 @@ using StardewModdingAPI.Events; using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Logging; using StardewModdingAPI.Framework.Models; +using StardewModdingAPI.Framework.Serialisation; using StardewValley; using Monitor = StardewModdingAPI.Framework.Monitor; @@ -319,6 +320,9 @@ namespace StardewModdingAPI { Program.Monitor.Log("Loading mods..."); + // get JSON helper + JsonHelper jsonHelper = new JsonHelper(); + // get assembly loader AssemblyLoader modAssemblyLoader = new AssemblyLoader(Program.TargetPlatform, Program.Monitor); AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => modAssemblyLoader.ResolveAssembly(e.Name); @@ -353,9 +357,6 @@ namespace StardewModdingAPI return; } - // get helper - IModHelper helper = new ModHelper(directory.FullName, Program.ModRegistry); - // get manifest path string manifestPath = Path.Combine(directory.FullName, "manifest.json"); if (!File.Exists(manifestPath)) @@ -378,7 +379,7 @@ namespace StardewModdingAPI } // deserialise manifest - manifest = helper.ReadJsonFile("manifest.json"); + manifest = jsonHelper.ReadJsonFile(Path.Combine(directory.FullName, "manifest.json"), null); if (manifest == null) { Program.Monitor.Log($"{errorPrefix}: the manifest file does not exist.", LogLevel.Error); @@ -503,8 +504,9 @@ namespace StardewModdingAPI } // inject data + // get helper mod.ModManifest = manifest; - mod.Helper = helper; + mod.Helper = new ModHelper(directory.FullName, jsonHelper, Program.ModRegistry); mod.Monitor = Program.GetSecondaryMonitor(manifest.Name); mod.PathOnDisk = directory.FullName; diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index ae08012d..add6ec40 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -148,6 +148,7 @@ + -- cgit From 548cbcecc41e88413885b1fcce7504a627640826 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Feb 2017 13:07:30 -0500 Subject: mark two internal classes internal --- src/StardewModdingAPI/Framework/Manifest.cs | 39 ++++++++++++++++++++++ .../Framework/Serialisation/JsonHelper.cs | 2 +- src/StardewModdingAPI/Manifest.cs | 39 ---------------------- src/StardewModdingAPI/Mod.cs | 6 ++-- src/StardewModdingAPI/StardewModdingAPI.csproj | 2 +- 5 files changed, 44 insertions(+), 44 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/Manifest.cs delete mode 100644 src/StardewModdingAPI/Manifest.cs (limited to 'src/StardewModdingAPI/Framework/Serialisation') diff --git a/src/StardewModdingAPI/Framework/Manifest.cs b/src/StardewModdingAPI/Framework/Manifest.cs new file mode 100644 index 00000000..189da9a8 --- /dev/null +++ b/src/StardewModdingAPI/Framework/Manifest.cs @@ -0,0 +1,39 @@ +using System; +using Newtonsoft.Json; +using StardewModdingAPI.Framework.Serialisation; + +namespace StardewModdingAPI.Framework +{ + /// A manifest which describes a mod for SMAPI. + internal class Manifest : IManifest + { + /********* + ** Accessors + *********/ + /// The mod name. + public string Name { get; set; } + + /// A brief description of the mod. + public string Description { get; set; } + + /// The mod author's name. + public string Author { get; set; } + + /// The mod version. + [JsonConverter(typeof(SemanticVersionConverter))] + public ISemanticVersion Version { get; set; } + + /// The minimum SMAPI version required by this mod, if any. + public string MinimumApiVersion { get; set; } + + /// The name of the DLL in the directory that has the method. + public string EntryDll { get; set; } + + /// The unique mod ID. + public string UniqueID { get; set; } + + /// Whether the mod uses per-save config files. + [Obsolete("Use " + nameof(Mod) + "." + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadConfig) + " instead")] + public bool PerSaveConfigs { get; set; } + } +} diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs index 3809666f..26d937a5 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -6,7 +6,7 @@ using StardewModdingAPI.Advanced; namespace StardewModdingAPI.Framework.Serialisation { /// Encapsulates SMAPI's JSON file parsing. - public class JsonHelper + internal class JsonHelper { /********* ** Accessors diff --git a/src/StardewModdingAPI/Manifest.cs b/src/StardewModdingAPI/Manifest.cs deleted file mode 100644 index baacf954..00000000 --- a/src/StardewModdingAPI/Manifest.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using Newtonsoft.Json; -using StardewModdingAPI.Framework.Serialisation; - -namespace StardewModdingAPI -{ - /// A manifest which describes a mod for SMAPI. - public class Manifest : IManifest - { - /********* - ** Accessors - *********/ - /// The mod name. - public string Name { get; set; } - - /// A brief description of the mod. - public string Description { get; set; } - - /// The mod author's name. - public string Author { get; set; } - - /// The mod version. - [JsonConverter(typeof(SemanticVersionConverter))] - public ISemanticVersion Version { get; set; } - - /// The minimum SMAPI version required by this mod, if any. - public string MinimumApiVersion { get; set; } - - /// The name of the DLL in the directory that has the method. - public string EntryDll { get; set; } - - /// The unique mod ID. - public string UniqueID { get; set; } - - /// Whether the mod uses per-save config files. - [Obsolete("Use " + nameof(Mod) + "." + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadConfig) + " instead")] - public bool PerSaveConfigs { get; set; } - } -} diff --git a/src/StardewModdingAPI/Mod.cs b/src/StardewModdingAPI/Mod.cs index 45534e16..c8456a29 100644 --- a/src/StardewModdingAPI/Mod.cs +++ b/src/StardewModdingAPI/Mod.cs @@ -50,11 +50,11 @@ namespace StardewModdingAPI } } - /// The full path to the per-save configs folder (if is true). + /// The full path to the per-save configs folder (if is true). [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadJsonFile) + " instead")] public string PerSaveConfigFolder => this.GetPerSaveConfigFolder(); - /// The full path to the per-save configuration file for the current save (if is true). + /// The full path to the per-save configuration file for the current save (if is true). [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadJsonFile) + " instead")] public string PerSaveConfigPath { @@ -82,7 +82,7 @@ namespace StardewModdingAPI /********* ** Private methods *********/ - /// Get the full path to the per-save configuration file for the current save (if is true). + /// Get the full path to the per-save configuration file for the current save (if is true). [Obsolete] private string GetPerSaveConfigFolder() { diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index ffeb8e2e..1e896d4b 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -186,7 +186,7 @@ - + -- cgit From 41ee8990f81a63c686953b2c28e7af8627fd8098 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 17 Feb 2017 11:33:22 -0500 Subject: 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 ++++- .../Serialisation/SelectiveStringEnumConverter.cs | 35 ++++++++++++++++++++++ src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs (limited to 'src/StardewModdingAPI/Framework/Serialisation') 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 @@ + -- cgit From e321362378eaacd0081db44f0db3ef457ef97368 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 17 Feb 2017 13:59:31 -0500 Subject: fix nullable enums not being written to JSON as string --- .../Framework/Serialisation/SelectiveStringEnumConverter.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/StardewModdingAPI/Framework/Serialisation') diff --git a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs index e9c5496d..37108556 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs @@ -29,7 +29,9 @@ namespace StardewModdingAPI.Framework.Serialisation /// The object type. public override bool CanConvert(Type type) { - return base.CanConvert(type) && this.Types.Contains(type.FullName); + return + base.CanConvert(type) + && this.Types.Contains((Nullable.GetUnderlyingType(type) ?? type).FullName); } } } -- cgit From 183fb9ff6e66e519ee9c0e3a3357504e8caf662a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 20:12:47 -0400 Subject: remove unused IConfigFile (#238) --- src/StardewModdingAPI/Advanced/ConfigFile.cs | 37 ---------------------- src/StardewModdingAPI/Advanced/IConfigFile.cs | 28 ---------------- src/StardewModdingAPI/Constants.cs | 2 ++ src/StardewModdingAPI/Framework/ModHelper.cs | 15 +-------- .../Framework/Serialisation/JsonHelper.cs | 14 ++------ src/StardewModdingAPI/Program.cs | 3 +- src/StardewModdingAPI/StardewModdingAPI.csproj | 2 -- 7 files changed, 6 insertions(+), 95 deletions(-) delete mode 100644 src/StardewModdingAPI/Advanced/ConfigFile.cs delete mode 100644 src/StardewModdingAPI/Advanced/IConfigFile.cs (limited to 'src/StardewModdingAPI/Framework/Serialisation') diff --git a/src/StardewModdingAPI/Advanced/ConfigFile.cs b/src/StardewModdingAPI/Advanced/ConfigFile.cs deleted file mode 100644 index 78cad26a..00000000 --- a/src/StardewModdingAPI/Advanced/ConfigFile.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.IO; -using Newtonsoft.Json; - -namespace StardewModdingAPI.Advanced -{ - /// Wraps a configuration file with IO methods for convenience. - [Obsolete] - public abstract class ConfigFile : IConfigFile - { - /********* - ** Accessors - *********/ - /// Provides simplified APIs for writing mods. - public IModHelper ModHelper { get; set; } - - /// The file path from which the model was loaded, relative to the mod directory. - public string FilePath { get; set; } - - - /********* - ** Public methods - *********/ - /// Reparse the underlying file and update this model. - public void Reload() - { - string json = File.ReadAllText(Path.Combine(this.ModHelper.DirectoryPath, this.FilePath)); - JsonConvert.PopulateObject(json, this); - } - - /// Save this model to the underlying file. - public void Save() - { - this.ModHelper.WriteJsonFile(this.FilePath, this); - } - } -} \ No newline at end of file diff --git a/src/StardewModdingAPI/Advanced/IConfigFile.cs b/src/StardewModdingAPI/Advanced/IConfigFile.cs deleted file mode 100644 index 1b424ace..00000000 --- a/src/StardewModdingAPI/Advanced/IConfigFile.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -namespace StardewModdingAPI.Advanced -{ - /// Wraps a configuration file with IO methods for convenience. - [Obsolete] - public interface IConfigFile - { - /********* - ** Accessors - *********/ - /// Provides simplified APIs for writing mods. - IModHelper ModHelper { get; set; } - - /// The file path from which the model was loaded, relative to the mod directory. - string FilePath { get; set; } - - - /********* - ** Methods - *********/ - /// Reparse the underlying file and update this model. - void Reload(); - - /// Save this model to the underlying file. - void Save(); - } -} diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 8949bc55..ae2fbe87 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -147,6 +147,8 @@ namespace StardewModdingAPI new GenericFieldFinder("StardewValley.Item", "set_Name", isStatic: false), // APIs removed in SMAPI 1.9 + new GenericTypeFinder("StardewModdingAPI.Advanced.ConfigFile"), + new GenericTypeFinder("StardewModdingAPI.Advanced.IConfigFile"), new GenericTypeFinder("StardewModdingAPI.Entities.SPlayer"), new GenericTypeFinder("StardewModdingAPI.Extensions"), new GenericTypeFinder("StardewModdingAPI.Inheritance.ItemStackChange"), diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index 0d50201b..c8c44dba 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using StardewModdingAPI.Advanced; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Serialisation; @@ -15,9 +14,6 @@ namespace StardewModdingAPI.Framework /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; - /// Manages deprecation warnings. - private static DeprecationManager DeprecationManager; - /********* ** Accessors @@ -65,13 +61,6 @@ namespace StardewModdingAPI.Framework this.ConsoleCommands = new CommandHelper(modName, commandManager); } - /// Injects types required for backwards compatibility. - /// Manages deprecation warnings. - internal static void Shim(DeprecationManager deprecationManager) - { - ModHelper.DeprecationManager = deprecationManager; - } - /**** ** Mod config file ****/ @@ -81,8 +70,6 @@ namespace StardewModdingAPI.Framework where TConfig : class, new() { TConfig config = this.ReadJsonFile("config.json") ?? new TConfig(); - if (config is IConfigFile) - ModHelper.DeprecationManager.Warn($"{nameof(IConfigFile)}", "1.9", DeprecationLevel.Info); this.WriteConfig(config); // create file or fill in missing fields return config; } @@ -107,7 +94,7 @@ namespace StardewModdingAPI.Framework where TModel : class { path = Path.Combine(this.DirectoryPath, path); - return this.JsonHelper.ReadJsonFile(path, this); + return this.JsonHelper.ReadJsonFile(path); } /// Save to a JSON file. diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs index d5f5bfd0..bd15c7bb 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework.Input; using Newtonsoft.Json; -using StardewModdingAPI.Advanced; namespace StardewModdingAPI.Framework.Serialisation { @@ -31,9 +30,8 @@ namespace StardewModdingAPI.Framework.Serialisation /// Read a JSON file. /// The model type. /// The absolete file path. - /// The mod helper to inject for instances. /// Returns the deserialised model, or null if the file doesn't exist or is empty. - public TModel ReadJsonFile(string fullPath, IModHelper modHelper) + public TModel ReadJsonFile(string fullPath) where TModel : class { // read file @@ -48,15 +46,7 @@ namespace StardewModdingAPI.Framework.Serialisation } // deserialise model - TModel model = JsonConvert.DeserializeObject(json, this.JsonSettings); - if (model is IConfigFile) - { - var wrapper = (IConfigFile)model; - wrapper.ModHelper = modHelper; - wrapper.FilePath = fullPath; - } - - return model; + return JsonConvert.DeserializeObject(json, this.JsonSettings); } /// Save to a JSON file. diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index f0cc00c7..db7a3df6 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -97,7 +97,6 @@ namespace StardewModdingAPI InternalExtensions.Shim(this.ModRegistry); Log.Shim(this.DeprecationManager, this.GetSecondaryMonitor("legacy mod"), this.ModRegistry); Mod.Shim(this.DeprecationManager); - ModHelper.Shim(this.DeprecationManager); ContentEvents.Shim(this.ModRegistry, this.Monitor); PlayerEvents.Shim(this.DeprecationManager); TimeEvents.Shim(this.DeprecationManager); @@ -344,7 +343,7 @@ namespace StardewModdingAPI } // deserialise manifest - manifest = jsonHelper.ReadJsonFile(Path.Combine(directory.FullName, "manifest.json"), null); + manifest = jsonHelper.ReadJsonFile(Path.Combine(directory.FullName, "manifest.json")); if (manifest == null) { this.Monitor.Log($"{skippedPrefix} because its manifest is invalid.", LogLevel.Error); diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 92726ca0..dceae74e 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -115,8 +115,6 @@ Properties\GlobalAssemblyInfo.cs - - -- cgit