using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace StardewModdingAPI.Framework
{
/// Tracks the installed mods.
internal class ModRegistry
{
/*********
** Properties
*********/
/// The registered mod data.
private readonly List Mods = new List();
/// An assembly full name => mod lookup.
private readonly IDictionary ModNamesByAssembly = new Dictionary();
/// Whether all mod assemblies have been loaded.
public bool AreAllModsLoaded { get; set; }
/// Whether all mods have been initialised and their method called.
public bool AreAllModsInitialised { get; set; }
/*********
** Public methods
*********/
/// Register a mod.
/// The mod metadata.
public void Add(IModMetadata metadata)
{
this.Mods.Add(metadata);
if (!metadata.IsContentPack)
this.ModNamesByAssembly[metadata.Mod.GetType().Assembly.FullName] = metadata;
}
/// Get metadata for all loaded mods.
/// Whether to include SMAPI mods.
/// Whether to include content pack mods.
public IEnumerable GetAll(bool assemblyMods = true, bool contentPacks = true)
{
IEnumerable query = this.Mods;
if (!assemblyMods)
query = query.Where(p => p.IsContentPack);
if (!contentPacks)
query = query.Where(p => !p.IsContentPack);
return query;
}
/// Get metadata for a loaded mod.
/// The mod's unique ID.
/// Returns the matching mod's metadata, or null if not found.
public IModMetadata Get(string uniqueID)
{
// normalise search ID
if (string.IsNullOrWhiteSpace(uniqueID))
return null;
uniqueID = uniqueID.Trim();
// find match
return this.GetAll().FirstOrDefault(p => p.HasID(uniqueID));
}
/// Get the mod metadata from one of its assemblies.
/// The type to check.
/// Returns the mod name, or null if the type isn't part of a known mod.
public IModMetadata GetFrom(Type type)
{
// null
if (type == null)
return null;
// known type
string assemblyName = type.Assembly.FullName;
if (this.ModNamesByAssembly.ContainsKey(assemblyName))
return this.ModNamesByAssembly[assemblyName];
// not found
return null;
}
/// Get the friendly name for the closest assembly registered as a source of deprecation warnings.
/// Returns the source name, or null if no registered assemblies were found.
public IModMetadata GetFromStack()
{
// get stack frames
StackTrace stack = new StackTrace();
StackFrame[] frames = stack.GetFrames();
if (frames == null)
return null;
// search stack for a source assembly
foreach (StackFrame frame in frames)
{
MethodBase method = frame.GetMethod();
IModMetadata mod = this.GetFrom(method.ReflectedType);
if (mod != null)
return mod;
}
// no known assembly found
return null;
}
}
}