using System; using System.IO; using StardewModdingAPI.Events; using StardewModdingAPI.Framework.Input; using StardewModdingAPI.Toolkit.Serialisation; namespace StardewModdingAPI.Framework.ModHelpers { /// Provides simplified APIs for writing mods. internal class ModHelper : BaseHelper, IModHelper, IDisposable { /********* ** Accessors *********/ /// The full path to the mod's folder. public string DirectoryPath { get; } /// Manages access to events raised by SMAPI, which let your mod react when something happens in the game. public IModEvents Events { get; } /// An API for loading content assets. public IContentHelper Content { get; } /// An API for managing content packs. public IContentPackHelper ContentPacks { get; } /// An API for reading and writing persistent mod data. public IDataHelper Data { get; } /// An API for checking and changing input state. public IInputHelper Input { get; } /// An API for accessing private game code. public IReflectionHelper Reflection { get; } /// an API for fetching metadata about loaded mods. public IModRegistry ModRegistry { get; } /// An API for managing console commands. public ICommandHelper ConsoleCommands { get; } /// Provides multiplayer utilities. public IMultiplayerHelper Multiplayer { get; } /// An API for reading translations stored in the mod's i18n folder, with one file per locale (like en.json) containing a flat key => value structure. Translations are fetched with locale fallback, so missing translations are filled in from broader locales (like pt-BR.json < pt.json < default.json). public ITranslationHelper Translation { get; } /********* ** Public methods *********/ /// Construct an instance. /// The mod's unique ID. /// The full path to the mod's folder. /// Manages the game's input state. /// Manages access to events raised by SMAPI. /// An API for loading content assets. /// An API for managing content packs. /// An API for managing console commands. /// An API for reading and writing persistent mod data. /// an API for fetching metadata about loaded mods. /// An API for accessing private game code. /// Provides multiplayer utilities. /// An API for reading translations stored in the mod's i18n folder. /// An argument is null or empty. /// The path does not exist on disk. public ModHelper(string modID, string modDirectory, SInputState inputState, IModEvents events, IContentHelper contentHelper, IContentPackHelper contentPackHelper, ICommandHelper commandHelper, IDataHelper dataHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, IMultiplayerHelper multiplayer, ITranslationHelper translationHelper) : base(modID) { // validate directory if (string.IsNullOrWhiteSpace(modDirectory)) throw new ArgumentNullException(nameof(modDirectory)); if (!Directory.Exists(modDirectory)) throw new InvalidOperationException("The specified mod directory does not exist."); // initialise this.DirectoryPath = modDirectory; this.Content = contentHelper ?? throw new ArgumentNullException(nameof(contentHelper)); this.ContentPacks = contentPackHelper ?? throw new ArgumentNullException(nameof(contentPackHelper)); this.Data = dataHelper ?? throw new ArgumentNullException(nameof(dataHelper)); this.Input = new InputHelper(modID, inputState); this.ModRegistry = modRegistry ?? throw new ArgumentNullException(nameof(modRegistry)); this.ConsoleCommands = commandHelper ?? throw new ArgumentNullException(nameof(commandHelper)); this.Reflection = reflectionHelper ?? throw new ArgumentNullException(nameof(reflectionHelper)); this.Multiplayer = multiplayer ?? throw new ArgumentNullException(nameof(multiplayer)); this.Translation = translationHelper ?? throw new ArgumentNullException(nameof(translationHelper)); this.Events = events; } /**** ** 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.Data.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.Data.WriteJsonFile("config.json", config); } /**** ** Disposal ****/ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { // nothing to dispose yet } } }