diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-13 00:13:29 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-13 00:13:29 -0500 |
commit | d1080a8b2b54c777a446f08d9ecd5b76b4b2561a (patch) | |
tree | 751ecea0d496058220c2d05f64f8233576ec3dd5 /src/StardewModdingAPI | |
parent | 8b0db49f12ffe1e715317a9145913949c8da0ec2 (diff) | |
download | SMAPI-d1080a8b2b54c777a446f08d9ecd5b76b4b2561a.tar.gz SMAPI-d1080a8b2b54c777a446f08d9ecd5b76b4b2561a.tar.bz2 SMAPI-d1080a8b2b54c777a446f08d9ecd5b76b4b2561a.zip |
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.
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Framework/ModHelper.cs | 59 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs | 73 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 12 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 1 |
4 files changed, 96 insertions, 49 deletions
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 *********/ - /// <summary>The JSON settings to use when serialising and deserialising files.</summary> - private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection<T> values are duplicated each time the config is loaded - }; + /// <summary>Encapsulates SMAPI's JSON file parsing.</summary> + private readonly JsonHelper JsonHelper; /********* @@ -38,20 +33,24 @@ namespace StardewModdingAPI.Framework *********/ /// <summary>Construct an instance.</summary> /// <param name="modDirectory">The mod directory path.</param> + /// <param name="jsonHelper">Encapsulate SMAPI's JSON parsing.</param> /// <param name="modRegistry">Metadata about loaded mods.</param> - /// <exception cref="ArgumentException">An argument is null or invalid.</exception> + /// <exception cref="ArgumentNullException">An argument is null or empty.</exception> /// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception> - 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<TModel>(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<TModel>(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<TModel>(path, this); } /// <summary>Save to a JSON file.</summary> @@ -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 +{ + /// <summary>Encapsulates SMAPI's JSON file parsing.</summary> + public class JsonHelper + { + /********* + ** Accessors + *********/ + /// <summary>The JSON settings to use when serialising and deserialising files.</summary> + private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection<T> values are duplicated each time the config is loaded + }; + + + /********* + ** Public methods + *********/ + /// <summary>Read a JSON file.</summary> + /// <typeparam name="TModel">The model type.</typeparam> + /// <param name="fullPath">The absolete file path.</param> + /// <param name="modHelper">The mod helper to inject for <see cref="IConfigFile"/> instances.</param> + /// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns> + public TModel ReadJsonFile<TModel>(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<TModel>(json, this.JsonSettings); + if (model is IConfigFile) + { + var wrapper = (IConfigFile)model; + wrapper.ModHelper = modHelper; + wrapper.FilePath = fullPath; + } + + return model; + } + + /// <summary>Save to a JSON file.</summary> + /// <typeparam name="TModel">The model type.</typeparam> + /// <param name="fullPath">The absolete file path.</param> + /// <param name="model">The model to save.</param> + public void WriteJsonFile<TModel>(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>("manifest.json"); + manifest = jsonHelper.ReadJsonFile<Manifest>(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 @@ <Compile Include="Framework\Logging\ConsoleInterceptionManager.cs" /> <Compile Include="Framework\Logging\InterceptingTextWriter.cs" /> <Compile Include="Framework\Reflection\PrivateProperty.cs" /> + <Compile Include="Framework\Serialisation\JsonHelper.cs" /> <Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" /> <Compile Include="IModRegistry.cs" /> <Compile Include="Events\LocationEvents.cs" /> |