From 053c0577eccef3db3397a935863af79b30a0282f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 7 Jul 2017 11:44:18 -0400 Subject: add mod ID to mod helpers (#318) --- src/StardewModdingAPI/IReflectionHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/StardewModdingAPI/IReflectionHelper.cs') diff --git a/src/StardewModdingAPI/IReflectionHelper.cs b/src/StardewModdingAPI/IReflectionHelper.cs index 77943c6c..f66e3a31 100644 --- a/src/StardewModdingAPI/IReflectionHelper.cs +++ b/src/StardewModdingAPI/IReflectionHelper.cs @@ -3,7 +3,7 @@ namespace StardewModdingAPI { /// Simplifies access to private game code. - public interface IReflectionHelper + public interface IReflectionHelper : IModLinked { /********* ** Public methods -- cgit 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 ++++++++++++++++++++++ src/StardewModdingAPI/Framework/ModRegistry.cs | 6 +-- src/StardewModdingAPI/IModRegistry.cs | 4 +- src/StardewModdingAPI/IReflectionHelper.cs | 2 +- src/StardewModdingAPI/Program.cs | 21 ++++++---- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 7 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs (limited to 'src/StardewModdingAPI/IReflectionHelper.cs') 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); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs index f9d3cfbf..a427bdb7 100644 --- a/src/StardewModdingAPI/Framework/ModRegistry.cs +++ b/src/StardewModdingAPI/Framework/ModRegistry.cs @@ -7,7 +7,7 @@ using System.Reflection; namespace StardewModdingAPI.Framework { /// Tracks the installed mods. - internal class ModRegistry : IModRegistry + internal class ModRegistry { /********* ** Properties @@ -23,7 +23,7 @@ namespace StardewModdingAPI.Framework ** Public methods *********/ /**** - ** IModRegistry + ** Basic metadata ****/ /// Get metadata for all loaded mods. public IEnumerable GetAll() @@ -47,7 +47,7 @@ namespace StardewModdingAPI.Framework } /**** - ** Internal methods + ** Mod data ****/ /// Register a mod as a possible source of deprecation warnings. /// The mod metadata. diff --git a/src/StardewModdingAPI/IModRegistry.cs b/src/StardewModdingAPI/IModRegistry.cs index 676c9734..5ef3fd65 100644 --- a/src/StardewModdingAPI/IModRegistry.cs +++ b/src/StardewModdingAPI/IModRegistry.cs @@ -2,8 +2,8 @@ namespace StardewModdingAPI { - /// Provides metadata about loaded mods. - public interface IModRegistry + /// Provides an API for fetching metadata about loaded mods. + public interface IModRegistry : IModLinked { /// Get metadata for all loaded mods. IEnumerable GetAll(); diff --git a/src/StardewModdingAPI/IReflectionHelper.cs b/src/StardewModdingAPI/IReflectionHelper.cs index f66e3a31..fb2c7861 100644 --- a/src/StardewModdingAPI/IReflectionHelper.cs +++ b/src/StardewModdingAPI/IReflectionHelper.cs @@ -2,7 +2,7 @@ namespace StardewModdingAPI { - /// Simplifies access to private game code. + /// Provides an API for accessing private game code. public interface IReflectionHelper : IModLinked { /********* diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 66ed0a85..97e18322 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -422,7 +422,7 @@ namespace StardewModdingAPI mod.SetStatus(ModMetadataStatus.Failed, $"it requires per-save configuration files ('psconfigs') which couldn't be created: {ex.GetLogSummary()}"); } } - } + } #endif // process dependencies @@ -604,7 +604,7 @@ namespace StardewModdingAPI /// The JSON helper with which to read mods' JSON files. /// The content manager to use for mod content. #if !SMAPI_2_0 -/// A list to populate with any deprecation warnings. + /// A list to populate with any deprecation warnings. private void LoadMods(IModMetadata[] mods, JsonHelper jsonHelper, SContentManager contentManager, IList deprecationWarnings) #else private void LoadMods(IModMetadata[] mods, JsonHelper jsonHelper, SContentManager contentManager) @@ -702,13 +702,20 @@ namespace StardewModdingAPI #endif // inject data - mod.ModManifest = manifest; - var reflectionHelper = new ReflectionHelper(manifest.UniqueID, this.Reflection); - mod.Helper = new ModHelper(manifest.UniqueID, metadata.DisplayName, metadata.DirectoryPath, jsonHelper, this.ModRegistry, this.CommandManager, contentManager, reflectionHelper); - mod.Monitor = this.GetSecondaryMonitor(metadata.DisplayName); + { + ICommandHelper commandHelper = new CommandHelper(manifest.UniqueID, metadata.DisplayName, this.CommandManager); + IContentHelper contentHelper = new ContentHelper(contentManager, metadata.DirectoryPath, manifest.UniqueID, metadata.DisplayName); + IReflectionHelper reflectionHelper = new ReflectionHelper(manifest.UniqueID, this.Reflection); + IModRegistry modRegistryHelper = new ModRegistryHelper(manifest.UniqueID, this.ModRegistry); + ITranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, manifest.Name, contentManager.GetLocale(), contentManager.GetCurrentLanguage()); + + mod.ModManifest = manifest; + mod.Helper = new ModHelper(manifest.UniqueID, metadata.DirectoryPath, jsonHelper, contentHelper, commandHelper, modRegistryHelper, reflectionHelper, translationHelper); + mod.Monitor = this.GetSecondaryMonitor(metadata.DisplayName); #if !SMAPI_2_0 - mod.PathOnDisk = metadata.DirectoryPath; + mod.PathOnDisk = metadata.DirectoryPath; #endif + } // track mod metadata.SetMod(mod); diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 93d55b0a..03f810d1 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -132,6 +132,7 @@ + -- cgit