From ba590b20a6581323ac13fc63d380524789d49c97 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 19 Jan 2017 11:07:48 -0500 Subject: add public mod registry (#220) --- src/StardewModdingAPI/Framework/ModRegistry.cs | 29 +++++++++++++++++++++++++- src/StardewModdingAPI/IModHelper.cs | 3 +++ src/StardewModdingAPI/IModRegistry.cs | 20 ++++++++++++++++++ src/StardewModdingAPI/ModHelper.cs | 13 ++++++++++-- src/StardewModdingAPI/Program.cs | 2 +- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/StardewModdingAPI/IModRegistry.cs (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs index 51ec7123..209f1928 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 + internal class ModRegistry : IModRegistry { /********* ** Properties @@ -22,6 +22,33 @@ namespace StardewModdingAPI.Framework /********* ** Public methods *********/ + /**** + ** IModRegistry + ****/ + /// Get metadata for all loaded mods. + public IEnumerable GetAll() + { + return this.Mods.Select(p => p.ModManifest); + } + + /// 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.GetAll().FirstOrDefault(p => p.UniqueID == uniqueID); + } + + /// Get whether a mod has been loaded. + /// The mod's unique ID. + public bool IsLoaded(string uniqueID) + { + return this.GetAll().Any(p => p.UniqueID == uniqueID); + } + + /**** + ** Internal methods + ****/ /// Register a mod as a possible source of deprecation warnings. /// The mod instance. public void Add(IMod mod) diff --git a/src/StardewModdingAPI/IModHelper.cs b/src/StardewModdingAPI/IModHelper.cs index 183b3b2b..02f9c038 100644 --- a/src/StardewModdingAPI/IModHelper.cs +++ b/src/StardewModdingAPI/IModHelper.cs @@ -12,6 +12,9 @@ /// Simplifies access to private game code. IReflectionHelper Reflection { get; } + /// Metadata about loaded mods. + IModRegistry ModRegistry { get; } + /********* ** Public methods diff --git a/src/StardewModdingAPI/IModRegistry.cs b/src/StardewModdingAPI/IModRegistry.cs new file mode 100644 index 00000000..676c9734 --- /dev/null +++ b/src/StardewModdingAPI/IModRegistry.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace StardewModdingAPI +{ + /// Provides metadata about loaded mods. + public interface IModRegistry + { + /// Get metadata for all loaded mods. + IEnumerable GetAll(); + + /// Get metadata for a loaded mod. + /// The mod's unique ID. + /// Returns the matching mod's metadata, or null if not found. + IManifest Get(string uniqueID); + + /// Get whether a mod has been loaded. + /// The mod's unique ID. + bool IsLoaded(string uniqueID); + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 78b3eefa..c20130cf 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -30,22 +30,31 @@ namespace StardewModdingAPI /// Simplifies access to private game code. public IReflectionHelper Reflection { get; } = new ReflectionHelper(); + /// Metadata about loaded mods. + public IModRegistry ModRegistry { get; } + /********* ** Public methods *********/ /// Construct an instance. /// The mod directory path. - public ModHelper(string modDirectory) + /// Metadata about loaded mods. + /// An argument is null or invalid. + /// The path does not exist on disk. + public ModHelper(string modDirectory, IModRegistry modRegistry) { // validate + if (modRegistry == null) + throw new ArgumentException("The mod registry cannot be null."); if (string.IsNullOrWhiteSpace(modDirectory)) - throw new InvalidOperationException("The mod directory cannot be empty."); + throw new ArgumentException("The mod directory cannot be empty."); if (!Directory.Exists(modDirectory)) throw new InvalidOperationException("The specified mod directory does not exist."); // initialise this.DirectoryPath = modDirectory; + this.ModRegistry = modRegistry; } /**** diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 11578fc4..29f71cb7 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -353,7 +353,7 @@ namespace StardewModdingAPI } // get helper - IModHelper helper = new ModHelper(directory); + IModHelper helper = new ModHelper(directory, Program.ModRegistry); // get manifest path string manifestPath = Path.Combine(directory, "manifest.json"); diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 125f287e..d56b6866 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -148,6 +148,7 @@ + -- cgit