using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using StardewModdingAPI.Framework.Models;
namespace StardewModdingAPI.Framework
{
/// Tracks the installed mods.
internal class ModRegistry : IModRegistry
{
/*********
** Properties
*********/
/// The registered mod data.
private readonly List Mods = new List();
/// The friendly mod names treated as deprecation warning sources (assembly full name => mod name).
private readonly IDictionary ModNamesByAssembly = new Dictionary();
/// Metadata about mods that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code.
private readonly ModCompatibility[] CompatibilityRecords;
/*********
** Public methods
*********/
/// Construct an instance.
/// Metadata about mods that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code.
public ModRegistry(IEnumerable compatibilityRecords)
{
this.CompatibilityRecords = compatibilityRecords.ToArray();
}
/****
** 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)
{
this.Mods.Add(mod);
this.ModNamesByAssembly[mod.GetType().Assembly.FullName] = mod.ModManifest.Name;
}
/// Get all enabled mods.
public IEnumerable GetMods()
{
return (from mod in this.Mods select mod);
}
/// Get the friendly mod name which handles a delegate.
/// The delegate to follow.
/// Returns the mod name, or null if the delegate isn't implemented by a known mod.
public string GetModFrom(Delegate @delegate)
{
return @delegate?.Target != null
? this.GetModFrom(@delegate.Target.GetType())
: null;
}
/// Get the friendly mod name which defines a type.
/// The type to check.
/// Returns the mod name, or null if the type isn't part of a known mod.
public string GetModFrom(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 string GetModFromStack()
{
// 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();
string name = this.GetModFrom(method.ReflectedType);
if (name != null)
return name;
}
// no known assembly found
return null;
}
/// Get metadata that indicates whether SMAPI should assume the mod is compatible or broken, regardless of whether it detects incompatible code.
/// The mod manifest.
/// Returns the incompatibility record if applicable, else null.
internal ModCompatibility GetCompatibilityRecord(IManifest manifest)
{
string key = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll;
return (
from mod in this.CompatibilityRecords
where
mod.ID == key
&& (mod.LowerSemanticVersion == null || !manifest.Version.IsOlderThan(mod.LowerSemanticVersion))
&& !manifest.Version.IsNewerThan(mod.UpperSemanticVersion)
select mod
).FirstOrDefault();
}
}
}