using System; using System.IO; using StardewModdingAPI.Framework.Serialisation; namespace StardewModdingAPI.Framework { /// Provides simplified APIs for writing mods. internal class ModHelper : IModHelper, IDisposable { /********* ** Properties *********/ /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; /********* ** Accessors *********/ /// The full path to the mod's folder. public string DirectoryPath { get; } /// An API for loading content assets. public IContentHelper Content { get; } /// Simplifies access to private game code. public IReflectionHelper Reflection { get; } /// Metadata about loaded mods. public IModRegistry ModRegistry { get; } /// An API for managing console commands. public ICommandHelper ConsoleCommands { get; } /********* ** Public methods *********/ /// Construct an instance. /// The manifest for the associated mod. /// The full path to the mod's folder. /// Encapsulate SMAPI's JSON parsing. /// Metadata about loaded mods. /// Manages console commands. /// The content manager which loads content assets. /// Simplifies access to private game code. /// An argument is null or empty. /// The path does not exist on disk. public ModHelper(IManifest manifest, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager, IReflectionHelper reflection) { // 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.DirectoryPath = modDirectory; this.JsonHelper = jsonHelper; this.Content = new ContentHelper(contentManager, modDirectory, manifest.Name); this.ModRegistry = modRegistry; this.ConsoleCommands = new CommandHelper(manifest.Name, commandManager); this.Reflection = reflection; } /**** ** Mod config file ****/ /// Read the mod's configuration file (and create it if needed). /// The config class type. This should be a plain class that has public properties for the settings you want. These can be complex types. public TConfig ReadConfig() where TConfig : class, new() { TConfig config = this.ReadJsonFile("config.json") ?? new TConfig(); this.WriteConfig(config); // create file or fill in missing fields return config; } /// Save to the mod's configuration file. /// The config class type. /// The config settings to save. public void WriteConfig(TConfig config) where TConfig : class, new() { this.WriteJsonFile("config.json", config); } /**** ** Generic JSON files ****/ /// Read a JSON file. /// The model type. /// The file path relative to the mod directory. /// Returns the deserialised model, or null if the file doesn't exist or is empty. public TModel ReadJsonFile(string path) where TModel : class { path = Path.Combine(this.DirectoryPath, path); return this.JsonHelper.ReadJsonFile(path); } /// Save to a JSON file. /// The model type. /// The file path relative to the mod directory. /// The model to save. public void WriteJsonFile(string path, TModel model) where TModel : class { path = Path.Combine(this.DirectoryPath, path); this.JsonHelper.WriteJsonFile(path, model); } /**** ** Disposal ****/ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { // nothing to dispose yet } } }