diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-07 12:17:22 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-07 12:17:22 -0400 |
commit | 5583e707b217eb36e71ccae2fe894efbd599a8db (patch) | |
tree | 7fe3117d19ec39034ce992dbca4992aace11d511 /src/StardewModdingAPI | |
parent | 053c0577eccef3db3397a935863af79b30a0282f (diff) | |
download | SMAPI-5583e707b217eb36e71ccae2fe894efbd599a8db.tar.gz SMAPI-5583e707b217eb36e71ccae2fe894efbd599a8db.tar.bz2 SMAPI-5583e707b217eb36e71ccae2fe894efbd599a8db.zip |
split helper out of mod registry, add mod ID, refactor ModHelper constructor (#318)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Framework/ModHelpers/ModHelper.cs | 36 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/ModHelpers/ModRegistryHelper.cs | 48 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/ModRegistry.cs | 6 | ||||
-rw-r--r-- | src/StardewModdingAPI/IModRegistry.cs | 4 | ||||
-rw-r--r-- | src/StardewModdingAPI/IReflectionHelper.cs | 2 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 21 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 1 |
7 files changed, 85 insertions, 33 deletions
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 /// <summary>An API for loading content assets.</summary> public IContentHelper Content { get; } - /// <summary>Simplifies access to private game code.</summary> + /// <summary>An API for accessing private game code.</summary> public IReflectionHelper Reflection { get; } - /// <summary>Metadata about loaded mods.</summary> + /// <summary>an API for fetching metadata about loaded mods.</summary> public IModRegistry ModRegistry { get; } /// <summary>An API for managing console commands.</summary> public ICommandHelper ConsoleCommands { get; } - /// <summary>Provides translations stored in the mod's <c>i18n</c> folder, with one file per locale (like <c>en.json</c>) containing a flat key => value structure. Translations are fetched with locale fallback, so missing translations are filled in from broader locales (like <c>pt-BR.json</c> < <c>pt.json</c> < <c>default.json</c>).</summary> + /// <summary>An API for reading translations stored in the mod's <c>i18n</c> folder, with one file per locale (like <c>en.json</c>) containing a flat key => value structure. Translations are fetched with locale fallback, so missing translations are filled in from broader locales (like <c>pt-BR.json</c> < <c>pt.json</c> < <c>default.json</c>).</summary> public ITranslationHelper Translation { get; } @@ -41,36 +41,32 @@ namespace StardewModdingAPI.Framework.ModHelpers *********/ /// <summary>Construct an instance.</summary> /// <param name="modID">The mod's unique ID.</param> - /// <param name="displayName">The mod's display name.</param> /// <param name="modDirectory">The full path to the mod's folder.</param> /// <param name="jsonHelper">Encapsulate SMAPI's JSON parsing.</param> - /// <param name="modRegistry">Metadata about loaded mods.</param> - /// <param name="commandManager">Manages console commands.</param> - /// <param name="contentManager">The content manager which loads content assets.</param> - /// <param name="reflection">Simplifies access to private game code.</param> + /// <param name="contentHelper">An API for loading content assets.</param> + /// <param name="commandHelper">An API for managing console commands.</param> + /// <param name="modRegistry">an API for fetching metadata about loaded mods.</param> + /// <param name="reflectionHelper">An API for accessing private game code.</param> + /// <param name="translationHelper">An API for reading translations stored in the mod's <c>i18n</c> folder.</param> /// <exception cref="ArgumentNullException">An argument is null or empty.</exception> /// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception> - 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 +{ + /// <summary>Provides metadata about installed mods.</summary> + internal class ModRegistryHelper : BaseHelper, IModRegistry + { + /********* + ** Properties + *********/ + /// <summary>The underlying mod registry.</summary> + private readonly ModRegistry Registry; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="modID">The unique ID of the relevant mod.</param> + /// <param name="registry">The underlying mod registry.</param> + public ModRegistryHelper(string modID, ModRegistry registry) + : base(modID) + { + this.Registry = registry; + } + + /// <summary>Get metadata for all loaded mods.</summary> + public IEnumerable<IManifest> GetAll() + { + return this.Registry.GetAll(); + } + + /// <summary>Get metadata for a loaded mod.</summary> + /// <param name="uniqueID">The mod's unique ID.</param> + /// <returns>Returns the matching mod's metadata, or <c>null</c> if not found.</returns> + public IManifest Get(string uniqueID) + { + return this.Registry.Get(uniqueID); + } + + /// <summary>Get whether a mod has been loaded.</summary> + /// <param name="uniqueID">The mod's unique ID.</param> + 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 { /// <summary>Tracks the installed mods.</summary> - internal class ModRegistry : IModRegistry + internal class ModRegistry { /********* ** Properties @@ -23,7 +23,7 @@ namespace StardewModdingAPI.Framework ** Public methods *********/ /**** - ** IModRegistry + ** Basic metadata ****/ /// <summary>Get metadata for all loaded mods.</summary> public IEnumerable<IManifest> GetAll() @@ -47,7 +47,7 @@ namespace StardewModdingAPI.Framework } /**** - ** Internal methods + ** Mod data ****/ /// <summary>Register a mod as a possible source of deprecation warnings.</summary> /// <param name="metadata">The mod metadata.</param> 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 { - /// <summary>Provides metadata about loaded mods.</summary> - public interface IModRegistry + /// <summary>Provides an API for fetching metadata about loaded mods.</summary> + public interface IModRegistry : IModLinked { /// <summary>Get metadata for all loaded mods.</summary> IEnumerable<IManifest> 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 { - /// <summary>Simplifies access to private game code.</summary> + /// <summary>Provides an API for accessing private game code.</summary> 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 /// <param name="jsonHelper">The JSON helper with which to read mods' JSON files.</param> /// <param name="contentManager">The content manager to use for mod content.</param> #if !SMAPI_2_0 -/// <param name="deprecationWarnings">A list to populate with any deprecation warnings.</param> + /// <param name="deprecationWarnings">A list to populate with any deprecation warnings.</param> private void LoadMods(IModMetadata[] mods, JsonHelper jsonHelper, SContentManager contentManager, IList<Action> 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 @@ <Compile Include="Framework\ModHelpers\CommandHelper.cs" /> <Compile Include="Framework\ModHelpers\ContentHelper.cs" /> <Compile Include="Framework\ModHelpers\ModHelper.cs" /> + <Compile Include="Framework\ModHelpers\ModRegistryHelper.cs" /> <Compile Include="Framework\ModHelpers\ReflectionHelper.cs" /> <Compile Include="Framework\ModHelpers\TranslationHelper.cs" /> <Compile Include="Framework\ModLoading\InvalidModStateException.cs" /> |