summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/ModHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-04-05 14:55:46 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-04-05 14:55:46 -0400
commitdbb9bd84306830456032778fc11fb9a34dd140c7 (patch)
tree0219cab065bfffbbbb9b048b6a30044f510be2c5 /src/StardewModdingAPI/Framework/ModHelper.cs
parent9c9833c9086b758589dafee10243e3bf47e12d73 (diff)
parent4675da0600edf6781cd740549ad0a175b606fc1e (diff)
downloadSMAPI-dbb9bd84306830456032778fc11fb9a34dd140c7.tar.gz
SMAPI-dbb9bd84306830456032778fc11fb9a34dd140c7.tar.bz2
SMAPI-dbb9bd84306830456032778fc11fb9a34dd140c7.zip
Merge branch 'develop-1.9' into stable
Diffstat (limited to 'src/StardewModdingAPI/Framework/ModHelper.cs')
-rw-r--r--src/StardewModdingAPI/Framework/ModHelper.cs111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs
new file mode 100644
index 00000000..c8c44dba
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/ModHelper.cs
@@ -0,0 +1,111 @@
+using System;
+using System.IO;
+using StardewModdingAPI.Framework.Reflection;
+using StardewModdingAPI.Framework.Serialisation;
+
+namespace StardewModdingAPI.Framework
+{
+ /// <summary>Provides simplified APIs for writing mods.</summary>
+ internal class ModHelper : IModHelper
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>Encapsulates SMAPI's JSON file parsing.</summary>
+ private readonly JsonHelper JsonHelper;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The mod directory path.</summary>
+ public string DirectoryPath { get; }
+
+ /// <summary>Simplifies access to private game code.</summary>
+ public IReflectionHelper Reflection { get; } = new ReflectionHelper();
+
+ /// <summary>Metadata about loaded mods.</summary>
+ public IModRegistry ModRegistry { get; }
+
+ /// <summary>An API for managing console commands.</summary>
+ public ICommandHelper ConsoleCommands { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="modName">The friendly mod name.</param>
+ /// <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>
+ /// <param name="commandManager">Manages console commands.</param>
+ /// <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 modName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager)
+ {
+ // validate
+ if (string.IsNullOrWhiteSpace(modDirectory))
+ 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;
+ this.ConsoleCommands = new CommandHelper(modName, commandManager);
+ }
+
+ /****
+ ** Mod config file
+ ****/
+ /// <summary>Read the mod's configuration file (and create it if needed).</summary>
+ /// <typeparam name="TConfig">The config class type. This should be a plain class that has public properties for the settings you want. These can be complex types.</typeparam>
+ public TConfig ReadConfig<TConfig>()
+ where TConfig : class, new()
+ {
+ TConfig config = this.ReadJsonFile<TConfig>("config.json") ?? new TConfig();
+ this.WriteConfig(config); // create file or fill in missing fields
+ return config;
+ }
+
+ /// <summary>Save to the mod's configuration file.</summary>
+ /// <typeparam name="TConfig">The config class type.</typeparam>
+ /// <param name="config">The config settings to save.</param>
+ public void WriteConfig<TConfig>(TConfig config)
+ where TConfig : class, new()
+ {
+ this.WriteJsonFile("config.json", config);
+ }
+
+ /****
+ ** Generic JSON files
+ ****/
+ /// <summary>Read a JSON file.</summary>
+ /// <typeparam name="TModel">The model type.</typeparam>
+ /// <param name="path">The file path relative to the mod directory.</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 path)
+ where TModel : class
+ {
+ path = Path.Combine(this.DirectoryPath, path);
+ return this.JsonHelper.ReadJsonFile<TModel>(path);
+ }
+
+ /// <summary>Save to a JSON file.</summary>
+ /// <typeparam name="TModel">The model type.</typeparam>
+ /// <param name="path">The file path relative to the mod directory.</param>
+ /// <param name="model">The model to save.</param>
+ public void WriteJsonFile<TModel>(string path, TModel model)
+ where TModel : class
+ {
+ path = Path.Combine(this.DirectoryPath, path);
+ this.JsonHelper.WriteJsonFile(path, model);
+ }
+ }
+}