From 482dd42d3d010d4636e86c551db11c5ee8ac470d Mon Sep 17 00:00:00 2001 From: mytigio Date: Sat, 14 Jan 2017 09:56:21 -0700 Subject: Add a catch for DirectoryNotFoundException in ModHelper.ReadJsonFile method. --- src/StardewModdingAPI/ModHelper.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/StardewModdingAPI/ModHelper.cs') diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 1fcc0182..7989be74 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -80,6 +80,10 @@ namespace StardewModdingAPI { return null; } + catch (DirectoryNotFoundException) + { + return null; + } // deserialise model TModel model = JsonConvert.DeserializeObject(json); -- cgit From 34bf9c5ecabce7500423ba0d1cdd3cfaa25f186b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 12:26:59 -0500 Subject: tweak JSON read error-handling, update release notes --- release-notes.md | 3 +++ src/StardewModdingAPI/ModHelper.cs | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src/StardewModdingAPI/ModHelper.cs') diff --git a/release-notes.md b/release-notes.md index bede8849..1c0409ea 100644 --- a/release-notes.md +++ b/release-notes.md @@ -8,6 +8,9 @@ For players: * Fixed issue where SMAPI couldn't be launched from Steam for some Linux players. * Updated list of incompatible mods. +For mod developers: +* Fixed error when reading a custom JSON file from a directory that doesn't exist. + For SMAPI developers: * Added support for specifying a lower bound in mod incompatibility data. * Added support for custom incompatible-mod-version error text. diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 7989be74..7304c5ea 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -76,11 +76,7 @@ namespace StardewModdingAPI { json = File.ReadAllText(fullPath); } - catch (FileNotFoundException) - { - return null; - } - catch (DirectoryNotFoundException) + catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException) { return null; } -- cgit From 9d1b6a1af2f21dff9904d03daaf6f1ce6409d7f8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 13:03:57 -0500 Subject: fix issue where default ICollection values in config.json were duplicated on each load (#209) --- release-notes.md | 3 ++- src/StardewModdingAPI/Manifest.cs | 1 + src/StardewModdingAPI/ModHelper.cs | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src/StardewModdingAPI/ModHelper.cs') diff --git a/release-notes.md b/release-notes.md index 8b4df6d7..1fb72dd1 100644 --- a/release-notes.md +++ b/release-notes.md @@ -6,7 +6,8 @@ See [log](https://github.com/Pathoschild/SMAPI/compare/stable...develop). For players: * Fixed issue where the installer couldn't find the game for some players on 32-bit Windows. * Fixed issue where SMAPI couldn't be launched from Steam for some Linux players. -* Fixed error loading mods if they have a `.cache` folder created on a different platform. +* Fixed issue where values in `config.json` were duplicated in some cases. +* Fixed error loading mods that were released with a `.cache` folder from a different platform. * Updated list of incompatible mods. For mod developers: diff --git a/src/StardewModdingAPI/Manifest.cs b/src/StardewModdingAPI/Manifest.cs index 018b31ae..07dd3541 100644 --- a/src/StardewModdingAPI/Manifest.cs +++ b/src/StardewModdingAPI/Manifest.cs @@ -8,6 +8,7 @@ namespace StardewModdingAPI internal class ManifestImpl : Manifest, IManifest { /// The mod version. + [JsonProperty("Version", ObjectCreationHandling = ObjectCreationHandling.Auto/* avoids issue where Json.NET can't determine concrete type for interface */)] public new ISemanticVersion Version { get { return base.Version; } diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 7304c5ea..78b3eefa 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -10,6 +10,17 @@ namespace StardewModdingAPI [Obsolete("Use " + nameof(IModHelper) + " instead.")] // only direct mod access to this class is obsolete public class ModHelper : IModHelper { + /********* + ** 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 + }; + + /********* ** Accessors *********/ @@ -82,7 +93,7 @@ namespace StardewModdingAPI } // deserialise model - TModel model = JsonConvert.DeserializeObject(json); + TModel model = JsonConvert.DeserializeObject(json, this.JsonSettings); if (model is IConfigFile) { var wrapper = (IConfigFile)model; @@ -108,7 +119,7 @@ namespace StardewModdingAPI Directory.CreateDirectory(dir); // write file - string json = JsonConvert.SerializeObject(model, Formatting.Indented); + string json = JsonConvert.SerializeObject(model, this.JsonSettings); File.WriteAllText(path, json); } } -- cgit