using System; using System.IO; using StardewModdingAPI.Advanced; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Serialisation; namespace StardewModdingAPI.Framework { /// Provides simplified APIs for writing mods. internal class ModHelper : IModHelper { /********* ** Properties *********/ /// Encapsulates SMAPI's JSON file parsing. private readonly JsonHelper JsonHelper; /// Manages deprecation warnings. private static DeprecationManager DeprecationManager; /********* ** Accessors *********/ /// The mod directory path. public string DirectoryPath { get; } /// Simplifies access to private game code. public IReflectionHelper Reflection { get; } = new ReflectionHelper(); /// Metadata about loaded mods. public IModRegistry ModRegistry { get; } /// An API for managing console commands. public ICommandHelper ConsoleCommands { get; } /********* ** Public methods *********/ /// Construct an instance. /// The friendly mod name. /// The mod directory path. /// Encapsulate SMAPI's JSON parsing. /// Metadata about loaded mods. /// Manages console commands. /// An argument is null or empty. /// The path does not exist on disk. 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); } /// Injects types required for backwards compatibility. /// Manages deprecation warnings. internal static void Shim(DeprecationManager deprecationManager) { ModHelper.DeprecationManager = deprecationManager; } /**** ** 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(); 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; } /// 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, this); } /// 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); } } }