summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-01-19 11:07:48 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-01-19 11:07:48 -0500
commitba590b20a6581323ac13fc63d380524789d49c97 (patch)
treeb530efe47e72ebe3bc07ccf0503f74eadfca27ec /src/StardewModdingAPI
parent4fe123350c8946ea9da4269063c30a41aaca87e3 (diff)
downloadSMAPI-ba590b20a6581323ac13fc63d380524789d49c97.tar.gz
SMAPI-ba590b20a6581323ac13fc63d380524789d49c97.tar.bz2
SMAPI-ba590b20a6581323ac13fc63d380524789d49c97.zip
add public mod registry (#220)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Framework/ModRegistry.cs29
-rw-r--r--src/StardewModdingAPI/IModHelper.cs3
-rw-r--r--src/StardewModdingAPI/IModRegistry.cs20
-rw-r--r--src/StardewModdingAPI/ModHelper.cs13
-rw-r--r--src/StardewModdingAPI/Program.cs2
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
6 files changed, 64 insertions, 4 deletions
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
{
/// <summary>Tracks the installed mods.</summary>
- internal class ModRegistry
+ internal class ModRegistry : IModRegistry
{
/*********
** Properties
@@ -22,6 +22,33 @@ namespace StardewModdingAPI.Framework
/*********
** Public methods
*********/
+ /****
+ ** IModRegistry
+ ****/
+ /// <summary>Get metadata for all loaded mods.</summary>
+ public IEnumerable<IManifest> GetAll()
+ {
+ return this.Mods.Select(p => p.ModManifest);
+ }
+
+ /// <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.GetAll().FirstOrDefault(p => p.UniqueID == 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.GetAll().Any(p => p.UniqueID == uniqueID);
+ }
+
+ /****
+ ** Internal methods
+ ****/
/// <summary>Register a mod as a possible source of deprecation warnings.</summary>
/// <param name="mod">The mod instance.</param>
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 @@
/// <summary>Simplifies access to private game code.</summary>
IReflectionHelper Reflection { get; }
+ /// <summary>Metadata about loaded mods.</summary>
+ 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
+{
+ /// <summary>Provides metadata about loaded mods.</summary>
+ public interface IModRegistry
+ {
+ /// <summary>Get metadata for all loaded mods.</summary>
+ IEnumerable<IManifest> 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>
+ IManifest Get(string uniqueID);
+
+ /// <summary>Get whether a mod has been loaded.</summary>
+ /// <param name="uniqueID">The mod's unique ID.</param>
+ 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
/// <summary>Simplifies access to private game code.</summary>
public IReflectionHelper Reflection { get; } = new ReflectionHelper();
+ /// <summary>Metadata about loaded mods.</summary>
+ public IModRegistry ModRegistry { get; }
+
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="modDirectory">The mod directory path.</param>
- public ModHelper(string modDirectory)
+ /// <param name="modRegistry">Metadata about loaded mods.</param>
+ /// <exception cref="ArgumentException">An argument is null or invalid.</exception>
+ /// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception>
+ 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 @@
<Compile Include="Events\EventArgsStringChanged.cs" />
<Compile Include="Events\GameEvents.cs" />
<Compile Include="Events\GraphicsEvents.cs" />
+ <Compile Include="IModRegistry.cs" />
<Compile Include="Events\LocationEvents.cs" />
<Compile Include="Events\MenuEvents.cs" />
<Compile Include="Events\MineEvents.cs" />