generalise internal mod registry (#409)

This commit is contained in:
Jesse Plamondon-Willard 2017-12-11 22:29:56 -05:00
parent 69c9ab0ecd
commit 971aff8368
4 changed files with 33 additions and 51 deletions

View File

@ -37,7 +37,7 @@ namespace StardewModdingAPI.Framework
/// <param name="severity">How deprecated the code is.</param> /// <param name="severity">How deprecated the code is.</param>
public void Warn(string nounPhrase, string version, DeprecationLevel severity) public void Warn(string nounPhrase, string version, DeprecationLevel severity)
{ {
this.Warn(this.ModRegistry.GetModFromStack(), nounPhrase, version, severity); this.Warn(this.ModRegistry.GetFromStack()?.DisplayName, nounPhrase, version, severity);
} }
/// <summary>Log a deprecation warning.</summary> /// <summary>Log a deprecation warning.</summary>
@ -82,7 +82,7 @@ namespace StardewModdingAPI.Framework
/// <returns>Returns whether the deprecation was successfully marked as warned. Returns <c>false</c> if it was already marked.</returns> /// <returns>Returns whether the deprecation was successfully marked as warned. Returns <c>false</c> if it was already marked.</returns>
public bool MarkWarned(string nounPhrase, string version) public bool MarkWarned(string nounPhrase, string version)
{ {
return this.MarkWarned(this.ModRegistry.GetModFromStack(), nounPhrase, version); return this.MarkWarned(this.ModRegistry.GetFromStack()?.DisplayName, nounPhrase, version);
} }
/// <summary>Mark a deprecation warning as already logged.</summary> /// <summary>Mark a deprecation warning as already logged.</summary>

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace StardewModdingAPI.Framework.ModHelpers namespace StardewModdingAPI.Framework.ModHelpers
{ {
@ -27,7 +28,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <summary>Get metadata for all loaded mods.</summary> /// <summary>Get metadata for all loaded mods.</summary>
public IEnumerable<IManifest> GetAll() public IEnumerable<IManifest> GetAll()
{ {
return this.Registry.GetAll(); return this.Registry.GetAll().Select(p => p.Manifest);
} }
/// <summary>Get metadata for a loaded mod.</summary> /// <summary>Get metadata for a loaded mod.</summary>
@ -35,14 +36,14 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <returns>Returns the matching mod's metadata, or <c>null</c> if not found.</returns> /// <returns>Returns the matching mod's metadata, or <c>null</c> if not found.</returns>
public IManifest Get(string uniqueID) public IManifest Get(string uniqueID)
{ {
return this.Registry.Get(uniqueID); return this.Registry.Get(uniqueID)?.Manifest;
} }
/// <summary>Get whether a mod has been loaded.</summary> /// <summary>Get whether a mod has been loaded.</summary>
/// <param name="uniqueID">The mod's unique ID.</param> /// <param name="uniqueID">The mod's unique ID.</param>
public bool IsLoaded(string uniqueID) public bool IsLoaded(string uniqueID)
{ {
return this.Registry.IsLoaded(uniqueID); return this.Registry.Get(uniqueID) != null;
} }
} }
} }

View File

@ -15,26 +15,31 @@ namespace StardewModdingAPI.Framework
/// <summary>The registered mod data.</summary> /// <summary>The registered mod data.</summary>
private readonly List<IModMetadata> Mods = new List<IModMetadata>(); private readonly List<IModMetadata> Mods = new List<IModMetadata>();
/// <summary>The friendly mod names treated as deprecation warning sources (assembly full name => mod name).</summary> /// <summary>An assembly full name => mod lookup.</summary>
private readonly IDictionary<string, string> ModNamesByAssembly = new Dictionary<string, string>(); private readonly IDictionary<string, IModMetadata> ModNamesByAssembly = new Dictionary<string, IModMetadata>();
/********* /*********
** Public methods ** Public methods
*********/ *********/
/**** /// <summary>Register a mod as a possible source of deprecation warnings.</summary>
** Basic metadata /// <param name="metadata">The mod metadata.</param>
****/ public void Add(IModMetadata metadata)
/// <summary>Get metadata for all loaded mods.</summary>
public IEnumerable<IManifest> GetAll()
{ {
return this.Mods.Select(p => p.Manifest); this.Mods.Add(metadata);
this.ModNamesByAssembly[metadata.Mod.GetType().Assembly.FullName] = metadata;
}
/// <summary>Get metadata for all loaded mods.</summary>
public IEnumerable<IModMetadata> GetAll()
{
return this.Mods.Select(p => p);
} }
/// <summary>Get metadata for a loaded mod.</summary> /// <summary>Get metadata for a loaded mod.</summary>
/// <param name="uniqueID">The mod's unique ID.</param> /// <param name="uniqueID">The mod's unique ID.</param>
/// <returns>Returns the matching mod's metadata, or <c>null</c> if not found.</returns> /// <returns>Returns the matching mod's metadata, or <c>null</c> if not found.</returns>
public IManifest Get(string uniqueID) public IModMetadata Get(string uniqueID)
{ {
// normalise search ID // normalise search ID
if (string.IsNullOrWhiteSpace(uniqueID)) if (string.IsNullOrWhiteSpace(uniqueID))
@ -42,37 +47,13 @@ namespace StardewModdingAPI.Framework
uniqueID = uniqueID.Trim(); uniqueID = uniqueID.Trim();
// find match // find match
return this.GetAll().FirstOrDefault(p => p.UniqueID.Trim().Equals(uniqueID, StringComparison.InvariantCultureIgnoreCase)); return this.GetAll().FirstOrDefault(p => p.Manifest.UniqueID.Trim().Equals(uniqueID, StringComparison.InvariantCultureIgnoreCase));
} }
/// <summary>Get whether a mod has been loaded.</summary> /// <summary>Get the mod metadata from one of its assemblies.</summary>
/// <param name="uniqueID">The mod's unique ID.</param>
public bool IsLoaded(string uniqueID)
{
return this.Get(uniqueID) != null;
}
/****
** Mod data
****/
/// <summary>Register a mod as a possible source of deprecation warnings.</summary>
/// <param name="metadata">The mod metadata.</param>
public void Add(IModMetadata metadata)
{
this.Mods.Add(metadata);
this.ModNamesByAssembly[metadata.Mod.GetType().Assembly.FullName] = metadata.DisplayName;
}
/// <summary>Get all enabled mods.</summary>
public IEnumerable<IModMetadata> GetMods()
{
return (from mod in this.Mods select mod);
}
/// <summary>Get the friendly mod name which defines a type.</summary>
/// <param name="type">The type to check.</param> /// <param name="type">The type to check.</param>
/// <returns>Returns the mod name, or <c>null</c> if the type isn't part of a known mod.</returns> /// <returns>Returns the mod name, or <c>null</c> if the type isn't part of a known mod.</returns>
public string GetModFrom(Type type) public IModMetadata GetFrom(Type type)
{ {
// null // null
if (type == null) if (type == null)
@ -89,7 +70,7 @@ namespace StardewModdingAPI.Framework
/// <summary>Get the friendly name for the closest assembly registered as a source of deprecation warnings.</summary> /// <summary>Get the friendly name for the closest assembly registered as a source of deprecation warnings.</summary>
/// <returns>Returns the source name, or <c>null</c> if no registered assemblies were found.</returns> /// <returns>Returns the source name, or <c>null</c> if no registered assemblies were found.</returns>
public string GetModFromStack() public IModMetadata GetFromStack()
{ {
// get stack frames // get stack frames
StackTrace stack = new StackTrace(); StackTrace stack = new StackTrace();
@ -101,9 +82,9 @@ namespace StardewModdingAPI.Framework
foreach (StackFrame frame in frames) foreach (StackFrame frame in frames)
{ {
MethodBase method = frame.GetMethod(); MethodBase method = frame.GetMethod();
string name = this.GetModFrom(method.ReflectedType); IModMetadata mod = this.GetFrom(method.ReflectedType);
if (name != null) if (mod != null)
return name; return mod;
} }
// no known assembly found // no known assembly found

View File

@ -247,7 +247,7 @@ namespace StardewModdingAPI
this.IsDisposed = true; this.IsDisposed = true;
// dispose mod data // dispose mod data
foreach (IModMetadata mod in this.ModRegistry.GetMods()) foreach (IModMetadata mod in this.ModRegistry.GetAll())
{ {
try try
{ {
@ -374,7 +374,7 @@ namespace StardewModdingAPI
} }
// update window titles // update window titles
int modsLoaded = this.ModRegistry.GetMods().Count(); int modsLoaded = this.ModRegistry.GetAll().Count();
this.GameInstance.Window.Title = $"Stardew Valley {Constants.GameVersion} - running SMAPI {Constants.ApiVersion} with {modsLoaded} mods"; this.GameInstance.Window.Title = $"Stardew Valley {Constants.GameVersion} - running SMAPI {Constants.ApiVersion} with {modsLoaded} mods";
Console.Title = $"SMAPI {Constants.ApiVersion} - running Stardew Valley {Constants.GameVersion} with {modsLoaded} mods"; Console.Title = $"SMAPI {Constants.ApiVersion} - running Stardew Valley {Constants.GameVersion} with {modsLoaded} mods";
@ -390,7 +390,7 @@ namespace StardewModdingAPI
LocalizedContentManager.LanguageCode languageCode = this.ContentManager.GetCurrentLanguage(); LocalizedContentManager.LanguageCode languageCode = this.ContentManager.GetCurrentLanguage();
// update mod translation helpers // update mod translation helpers
foreach (IModMetadata mod in this.ModRegistry.GetMods()) foreach (IModMetadata mod in this.ModRegistry.GetAll())
(mod.Mod.Helper.Translation as TranslationHelper)?.SetLocale(locale, languageCode); (mod.Mod.Helper.Translation as TranslationHelper)?.SetLocale(locale, languageCode);
} }
@ -753,7 +753,7 @@ namespace StardewModdingAPI
} }
} }
} }
IModMetadata[] loadedMods = this.ModRegistry.GetMods().ToArray(); IModMetadata[] loadedMods = this.ModRegistry.GetAll().ToArray();
// log skipped mods // log skipped mods
this.Monitor.Newline(); this.Monitor.Newline();
@ -858,7 +858,7 @@ namespace StardewModdingAPI
private void ReloadTranslations() private void ReloadTranslations()
{ {
JsonHelper jsonHelper = new JsonHelper(); JsonHelper jsonHelper = new JsonHelper();
foreach (IModMetadata metadata in this.ModRegistry.GetMods()) foreach (IModMetadata metadata in this.ModRegistry.GetAll())
{ {
// read translation files // read translation files
IDictionary<string, IDictionary<string, string>> translations = new Dictionary<string, IDictionary<string, string>>(); IDictionary<string, IDictionary<string, string>> translations = new Dictionary<string, IDictionary<string, string>>();