From 64a72c45e3c4b504c7a1b61a539f1cbc9b9d23cc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 15 Jan 2017 19:21:26 -0500 Subject: deprecate events replaced by save events (#215) --- src/StardewModdingAPI/Framework/ModRegistry.cs | 40 ++++++++++++++++++++------ 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src/StardewModdingAPI/Framework/ModRegistry.cs') diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs index b593142d..51ec7123 100644 --- a/src/StardewModdingAPI/Framework/ModRegistry.cs +++ b/src/StardewModdingAPI/Framework/ModRegistry.cs @@ -36,6 +36,34 @@ namespace StardewModdingAPI.Framework 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() @@ -49,16 +77,10 @@ namespace StardewModdingAPI.Framework // 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]; + string name = this.GetModFrom(method.ReflectedType); + if (name != null) + return name; } // no known assembly found -- cgit