From 5583e707b217eb36e71ccae2fe894efbd599a8db Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 7 Jul 2017 12:17:22 -0400 Subject: split helper out of mod registry, add mod ID, refactor ModHelper constructor (#318) --- .../Framework/ModHelpers/ModHelper.cs | 36 ++++++++-------- .../Framework/ModHelpers/ModRegistryHelper.cs | 48 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs (limited to 'src/StardewModdingAPI/Framework/ModHelpers') diff --git a/src/StardewModdingAPI/Framework/ModHelpers/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelpers/ModHelper.cs index 20d891a1..665b9cf4 100644 --- a/src/StardewModdingAPI/Framework/ModHelpers/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelpers/ModHelper.cs @@ -23,16 +23,16 @@ namespace StardewModdingAPI.Framework.ModHelpers /// An API for loading content assets. public IContentHelper Content { get; } - /// Simplifies access to private game code. + /// An API for accessing private game code. public IReflectionHelper Reflection { get; } - /// Metadata about loaded mods. + /// an API for fetching metadata about loaded mods. public IModRegistry ModRegistry { get; } /// An API for managing console commands. public ICommandHelper ConsoleCommands { get; } - /// Provides 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). + /// 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; } @@ -41,36 +41,32 @@ namespace StardewModdingAPI.Framework.ModHelpers *********/ /// Construct an instance. /// The mod's unique ID. - /// The mod's display name. /// 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 API for loading content assets. + /// An API for managing console commands. + /// an API for fetching metadata about loaded mods. + /// An API for accessing private game code. + /// 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 displayName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager, IReflectionHelper reflection) + public ModHelper(string modID, string modDirectory, JsonHelper jsonHelper, IContentHelper contentHelper, ICommandHelper commandHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, ITranslationHelper translationHelper) : base(modID) { - // validate + // validate directory 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, modID, displayName); - this.ModRegistry = modRegistry; - this.ConsoleCommands = new CommandHelper(modID, displayName, commandManager); - this.Reflection = reflection; - this.Translation = new TranslationHelper(modID, displayName, contentManager.GetLocale(), contentManager.GetCurrentLanguage()); + this.JsonHelper = jsonHelper ?? throw new ArgumentNullException(nameof(jsonHelper)); + this.Content = contentHelper ?? throw new ArgumentNullException(nameof(contentHelper)); + 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.Translation = translationHelper ?? throw new ArgumentNullException(nameof(translationHelper)); } /**** diff --git a/src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs b/src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs new file mode 100644 index 00000000..9e824694 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; + +namespace StardewModdingAPI.Framework.ModHelpers +{ + /// Provides metadata about installed mods. + internal class ModRegistryHelper : BaseHelper, IModRegistry + { + /********* + ** Properties + *********/ + /// The underlying mod registry. + private readonly ModRegistry Registry; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The unique ID of the relevant mod. + /// The underlying mod registry. + public ModRegistryHelper(string modID, ModRegistry registry) + : base(modID) + { + this.Registry = registry; + } + + /// Get metadata for all loaded mods. + public IEnumerable GetAll() + { + return this.Registry.GetAll(); + } + + /// Get metadata for a loaded mod. + /// The mod's unique ID. + /// Returns the matching mod's metadata, or null if not found. + public IManifest Get(string uniqueID) + { + return this.Registry.Get(uniqueID); + } + + /// Get whether a mod has been loaded. + /// The mod's unique ID. + public bool IsLoaded(string uniqueID) + { + return this.Registry.IsLoaded(uniqueID); + } + } +} -- cgit