diff options
-rw-r--r-- | src/StardewModdingAPI/Framework/DeprecationManager.cs | 53 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/ModRegistry.cs | 58 | ||||
-rw-r--r-- | src/StardewModdingAPI/Program.cs | 12 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 1 |
4 files changed, 76 insertions, 48 deletions
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 *********/ - /// <summary>The friendly mod names treated as deprecation warning sources (assembly full name => mod name).</summary> - private readonly IDictionary<string, string> ModNamesByAssembly = new Dictionary<string, string>(); - /// <summary>The deprecations which have already been logged (as 'mod name::noun phrase::version').</summary> private readonly HashSet<string> LoggedDeprecations = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase); + /// <summary>Tracks the installed mods.</summary> + private readonly ModRegistry ModRegistry; + /********* ** Accessors @@ -28,12 +27,11 @@ namespace StardewModdingAPI.Framework /********* ** Public methods *********/ - /// <summary>Register a mod as a possible source of deprecation warnings.</summary> - /// <param name="assembly">The mod assembly.</param> - /// <param name="name">The mod's friendly name.</param> - public void AddMod(Assembly assembly, string name) + /// <summary>Construct an instance.</summary> + /// <param name="modRegistry">Tracks the installed mods.</param> + public DeprecationManager(ModRegistry modRegistry) { - this.ModNamesByAssembly[assembly.FullName] = name; + this.ModRegistry = modRegistry; } /// <summary>Log a deprecation warning.</summary> @@ -42,7 +40,7 @@ namespace StardewModdingAPI.Framework /// <param name="severity">How deprecated the code is.</param> public void Warn(string nounPhrase, string version, DeprecationLevel severity) { - this.Warn(this.GetSourceNameFromStack(), nounPhrase, version, severity); + this.Warn(this.ModRegistry.GetModFromStack(), nounPhrase, version, severity); } /// <summary>Log a deprecation warning.</summary> @@ -95,7 +93,7 @@ namespace StardewModdingAPI.Framework /// <returns>Returns whether the deprecation was successfully marked as warned. Returns <c>false</c> if it was already marked.</returns> public bool MarkWarned(string nounPhrase, string version) { - return this.MarkWarned(this.GetSourceNameFromStack(), nounPhrase, version); + return this.MarkWarned(this.ModRegistry.GetModFromStack(), nounPhrase, version); } /// <summary>Mark a deprecation warning as already logged.</summary> @@ -124,38 +122,5 @@ namespace StardewModdingAPI.Framework MethodInfo method = subtype.GetMethod(nameof(Mod.Entry), new[] { typeof(object[]) }); return method.DeclaringType != baseType; } - - - /********* - ** Private methods - *********/ - /// <summary>Get the friendly name for the closest assembly registered as a source of deprecation warnings.</summary> - /// <returns>Returns the source name, or <c>null</c> if no registered assemblies were found.</returns> - 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 +{ + /// <summary>Tracks the installed mods.</summary> + internal class ModRegistry + { + /********* + ** Properties + *********/ + /// <summary>The friendly mod names treated as deprecation warning sources (assembly full name => mod name).</summary> + private readonly IDictionary<string, string> ModNamesByAssembly = new Dictionary<string, string>(); + + + /********* + ** Public methods + *********/ + /// <summary>Register a mod as a possible source of deprecation warnings.</summary> + /// <param name="manifest">The mod manifest.</param> + /// <param name="assembly">The mod assembly.</param> + public void Add(Manifest manifest, Assembly assembly) + { + this.ModNamesByAssembly[assembly.FullName] = manifest.Name; + } + + /// <summary>Get the friendly name for the closest assembly registered as a source of deprecation warnings.</summary> + /// <returns>Returns the source name, or <c>null</c> if no registered assemblies were found.</returns> + 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 /// <summary>The game's build type (i.e. GOG vs Steam).</summary> public static int BuildType => (int)Program.StardewProgramType.GetField("buildType", BindingFlags.Public | BindingFlags.Static).GetValue(null); + /// <summary>Tracks the installed mods.</summary> + internal static readonly ModRegistry ModRegistry = new ModRegistry(); + /// <summary>Manages deprecation warnings.</summary> - 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 @@ <Compile Include="Extensions.cs" /> <Compile Include="Framework\DeprecationLevel.cs" /> <Compile Include="Framework\DeprecationManager.cs" /> + <Compile Include="Framework\ModRegistry.cs" /> <Compile Include="Framework\UpdateHelper.cs" /> <Compile Include="Framework\GitRelease.cs" /> <Compile Include="Framework\UserSettings.cs" /> |