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 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's metadata, 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 mod metadata from a stack frame, if any. /// The stack frame to check. /// Returns the mod's metadata, or null if the frame isn't part of a known mod. public IModMetadata? GetFrom(StackFrame frame) { MethodBase? method = frame.GetMethod(); return this.GetFrom(method?.ReflectedType); } /// Get the mod metadata from the closest assembly registered as a source of deprecation warnings. /// The call stack to analyze. /// Returns the mod's metadata, or null if no registered assemblies were found. public IModMetadata? GetFromStack(StackFrame[] frames) { foreach (StackFrame frame in frames) { IModMetadata? mod = this.GetFrom(frame); if (mod != null) return mod; } return null; } } }