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>
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>
@ -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>
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>

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
namespace StardewModdingAPI.Framework.ModHelpers
{
@ -27,7 +28,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <summary>Get metadata for all loaded mods.</summary>
public IEnumerable<IManifest> GetAll()
{
return this.Registry.GetAll();
return this.Registry.GetAll().Select(p => p.Manifest);
}
/// <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>
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>
/// <param name="uniqueID">The mod's unique ID.</param>
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>
private readonly List<IModMetadata> Mods = new List<IModMetadata>();
/// <summary>The friendly mod names treated as deprecation warning sources (assembly full name => mod name).</summary>
private readonly IDictionary<string, string> ModNamesByAssembly = new Dictionary<string, string>();
/// <summary>An assembly full name => mod lookup.</summary>
private readonly IDictionary<string, IModMetadata> ModNamesByAssembly = new Dictionary<string, IModMetadata>();
/*********
** Public methods
*********/
/****
** Basic metadata
****/
/// <summary>Get metadata for all loaded mods.</summary>
public IEnumerable<IManifest> GetAll()
/// <summary>Register a mod as a possible source of deprecation warnings.</summary>
/// <param name="metadata">The mod metadata.</param>
public void Add(IModMetadata metadata)
{
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>
/// <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)
public IModMetadata Get(string uniqueID)
{
// normalise search ID
if (string.IsNullOrWhiteSpace(uniqueID))
@ -42,37 +47,13 @@ namespace StardewModdingAPI.Framework
uniqueID = uniqueID.Trim();
// 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>
/// <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>
/// <summary>Get the mod metadata from one of its assemblies.</summary>
/// <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>
public string GetModFrom(Type type)
public IModMetadata GetFrom(Type 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>
/// <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
StackTrace stack = new StackTrace();
@ -101,9 +82,9 @@ namespace StardewModdingAPI.Framework
foreach (StackFrame frame in frames)
{
MethodBase method = frame.GetMethod();
string name = this.GetModFrom(method.ReflectedType);
if (name != null)
return name;
IModMetadata mod = this.GetFrom(method.ReflectedType);
if (mod != null)
return mod;
}
// no known assembly found

View File

@ -247,7 +247,7 @@ namespace StardewModdingAPI
this.IsDisposed = true;
// dispose mod data
foreach (IModMetadata mod in this.ModRegistry.GetMods())
foreach (IModMetadata mod in this.ModRegistry.GetAll())
{
try
{
@ -374,7 +374,7 @@ namespace StardewModdingAPI
}
// 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";
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();
// 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);
}
@ -753,7 +753,7 @@ namespace StardewModdingAPI
}
}
}
IModMetadata[] loadedMods = this.ModRegistry.GetMods().ToArray();
IModMetadata[] loadedMods = this.ModRegistry.GetAll().ToArray();
// log skipped mods
this.Monitor.Newline();
@ -858,7 +858,7 @@ namespace StardewModdingAPI
private void ReloadTranslations()
{
JsonHelper jsonHelper = new JsonHelper();
foreach (IModMetadata metadata in this.ModRegistry.GetMods())
foreach (IModMetadata metadata in this.ModRegistry.GetAll())
{
// read translation files
IDictionary<string, IDictionary<string, string>> translations = new Dictionary<string, IDictionary<string, string>>();