From be9d62411221146bba8edd3504c709f14da7c2a0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 13 Nov 2016 12:17:05 -0500 Subject: split mod registry out of deprecation manager for reuse (#168) --- .../Framework/DeprecationManager.cs | 53 ++++---------------- src/StardewModdingAPI/Framework/ModRegistry.cs | 58 ++++++++++++++++++++++ src/StardewModdingAPI/Program.cs | 12 +++-- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 4 files changed, 76 insertions(+), 48 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/ModRegistry.cs (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/DeprecationManager.cs b/src/StardewModdingAPI/Framework/DeprecationManager.cs index 0c5a49f9..2d25db09 100644 --- a/src/StardewModdingAPI/Framework/DeprecationManager.cs +++ b/src/StardewModdingAPI/Framework/DeprecationManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Reflection; namespace StardewModdingAPI.Framework @@ -11,12 +10,12 @@ namespace StardewModdingAPI.Framework /********* ** Properties *********/ - /// The friendly mod names treated as deprecation warning sources (assembly full name => mod name). - private readonly IDictionary ModNamesByAssembly = new Dictionary(); - /// The deprecations which have already been logged (as 'mod name::noun phrase::version'). private readonly HashSet LoggedDeprecations = new HashSet(StringComparer.InvariantCultureIgnoreCase); + /// Tracks the installed mods. + private readonly ModRegistry ModRegistry; + /********* ** Accessors @@ -28,12 +27,11 @@ namespace StardewModdingAPI.Framework /********* ** Public methods *********/ - /// Register a mod as a possible source of deprecation warnings. - /// The mod assembly. - /// The mod's friendly name. - public void AddMod(Assembly assembly, string name) + /// Construct an instance. + /// Tracks the installed mods. + public DeprecationManager(ModRegistry modRegistry) { - this.ModNamesByAssembly[assembly.FullName] = name; + this.ModRegistry = modRegistry; } /// Log a deprecation warning. @@ -42,7 +40,7 @@ namespace StardewModdingAPI.Framework /// How deprecated the code is. public void Warn(string nounPhrase, string version, DeprecationLevel severity) { - this.Warn(this.GetSourceNameFromStack(), nounPhrase, version, severity); + this.Warn(this.ModRegistry.GetModFromStack(), nounPhrase, version, severity); } /// Log a deprecation warning. @@ -95,7 +93,7 @@ namespace StardewModdingAPI.Framework /// Returns whether the deprecation was successfully marked as warned. Returns false if it was already marked. public bool MarkWarned(string nounPhrase, string version) { - return this.MarkWarned(this.GetSourceNameFromStack(), nounPhrase, version); + return this.MarkWarned(this.ModRegistry.GetModFromStack(), nounPhrase, version); } /// Mark a deprecation warning as already logged. @@ -124,38 +122,5 @@ namespace StardewModdingAPI.Framework MethodInfo method = subtype.GetMethod(nameof(Mod.Entry), new[] { typeof(object[]) }); return method.DeclaringType != baseType; } - - - /********* - ** Private methods - *********/ - /// 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. - private string GetSourceNameFromStack() - { - // 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; - } } } diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs new file mode 100644 index 00000000..ba56a447 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModRegistry.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; + +namespace StardewModdingAPI.Framework +{ + /// Tracks the installed mods. + internal class ModRegistry + { + /********* + ** Properties + *********/ + /// 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 manifest. + /// The mod assembly. + public void Add(Manifest manifest, Assembly assembly) + { + this.ModNamesByAssembly[assembly.FullName] = manifest.Name; + } + + /// 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; + } + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 177db8f2..55953a8f 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -57,8 +57,12 @@ namespace StardewModdingAPI /// The game's build type (i.e. GOG vs Steam). public static int BuildType => (int)Program.StardewProgramType.GetField("buildType", BindingFlags.Public | BindingFlags.Static).GetValue(null); + /// Tracks the installed mods. + internal static readonly ModRegistry ModRegistry = new ModRegistry(); + /// Manages deprecation warnings. - internal static readonly DeprecationManager DeprecationManager = new DeprecationManager(); + internal static readonly DeprecationManager DeprecationManager = new DeprecationManager(Program.ModRegistry); + /********* ** Public methods @@ -334,13 +338,13 @@ namespace StardewModdingAPI Mod modEntry = (Mod)modAssembly.CreateInstance(modEntryType.ToString()); if (modEntry != null) { - // add as possible source of deprecation warnings - Program.DeprecationManager.AddMod(modAssembly, manifest.Name); + // track mod + Program.ModRegistry.Add(manifest, modAssembly); // hook up mod + modEntry.Manifest = manifest; modEntry.Helper = helper; modEntry.PathOnDisk = directory; - modEntry.Manifest = manifest; Log.Info($"Loaded mod: {modEntry.Manifest.Name} by {modEntry.Manifest.Author}, v{modEntry.Manifest.Version} | {modEntry.Manifest.Description}"); Program.ModsLoaded += 1; modEntry.Entry(); // deprecated diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index bd1fd022..ce302b95 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -195,6 +195,7 @@ + -- cgit