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(); /// The friendly mod names treated as deprecation warning sources (assembly full name => mod name). private readonly IDictionary ModNamesByAssembly = new Dictionary(); /********* ** Public 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 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) { // get assembly name MethodBase method = frame.GetMethod(); Type type = method.ReflectedType; if (type == null) continue; string assemblyName = type.Assembly.FullName; // get name if it's a registered source if (this.ModNamesByAssembly.ContainsKey(assemblyName)) return this.ModNamesByAssembly[assemblyName]; } // no known assembly found return null; } } }