#nullable disable
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
{
/*********
** Fields
*********/
/// The registered mod data.
private readonly List Mods = new();
/// 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 initialized and their method called.
public bool AreAllModsInitialized { get; set; }
/*********
** Public methods
*********/
/// Register a mod.
/// The mod metadata.
public void Add(IModMetadata metadata)
{
this.Mods.Add(metadata);
}
/// Track a mod's assembly for use via .
/// The mod metadata.
/// The mod assembly.
public void TrackAssemblies(IModMetadata metadata, Assembly modAssembly)
{
this.ModNamesByAssembly[modAssembly.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)
{
// normalize 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();
StackFrame[] frames = stack.GetFrames();
// 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;
}
}
}