using System; using System.Collections.Generic; using System.Reflection; namespace StardewModdingAPI.Framework { /// Manages deprecation warnings. internal class DeprecationManager { /********* ** Properties *********/ /// 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 *********/ /// Whether -level deprecation messages should be shown in the console. public bool SendNoticesToConsole { get; set; } /********* ** Public methods *********/ /// Construct an instance. /// Tracks the installed mods. public DeprecationManager(ModRegistry modRegistry) { this.ModRegistry = modRegistry; } /// Log a deprecation warning. /// A noun phrase describing what is deprecated. /// The SMAPI version which deprecated it. /// How deprecated the code is. public void Warn(string nounPhrase, string version, DeprecationLevel severity) { this.Warn(this.ModRegistry.GetModFromStack(), nounPhrase, version, severity); } /// Log a deprecation warning. /// The friendly mod name which used the deprecated code. /// A noun phrase describing what is deprecated. /// The SMAPI version which deprecated it. /// How deprecated the code is. public void Warn(string source, string nounPhrase, string version, DeprecationLevel severity) { // ignore if already warned if (source != null && !this.MarkWarned(source, nounPhrase, version)) return; // build message string message = source != null ? $"{source} used {nounPhrase}, which is deprecated since SMAPI {version}." : $"An unknown mod used {nounPhrase}, which is deprecated since SMAPI {version}."; message += severity != DeprecationLevel.PendingRemoval ? " It will work fine for now, but may be removed in a future version of SMAPI." : " It will be removed soon, so the mod will break if it's not updated."; if (source == null) message += $"{Environment.NewLine}{Environment.StackTrace}"; // log message switch (severity) { case DeprecationLevel.Notice: if (this.SendNoticesToConsole) Log.Debug($"[DEV] {message}"); else Log.LogToFile(message); break; case DeprecationLevel.Info: Log.Debug(message); break; case DeprecationLevel.PendingRemoval: Log.Warning(message); break; default: throw new NotImplementedException($"Unknown deprecation level '{severity}'"); } } /// Mark a deprecation warning as already logged. /// A noun phrase describing what is deprecated (e.g. "the Extensions.AsInt32 method"). /// The SMAPI version which deprecated it. /// 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.ModRegistry.GetModFromStack(), nounPhrase, version); } /// Mark a deprecation warning as already logged. /// The friendly name of the assembly which used the deprecated code. /// A noun phrase describing what is deprecated (e.g. "the Extensions.AsInt32 method"). /// The SMAPI version which deprecated it. /// Returns whether the deprecation was successfully marked as warned. Returns false if it was already marked. public bool MarkWarned(string source, string nounPhrase, string version) { if (string.IsNullOrWhiteSpace(source)) throw new InvalidOperationException("The deprecation source cannot be empty."); string key = $"{source}::{nounPhrase}::{version}"; if (this.LoggedDeprecations.Contains(key)) return false; this.LoggedDeprecations.Add(key); return true; } /// Get whether a type implements the given virtual method. /// The type to check. /// The base type which declares the virtual method. /// The method name. public bool IsVirtualMethodImplemented(Type subtype, Type baseType, string name) { MethodInfo method = subtype.GetMethod(nameof(Mod.Entry), new[] { typeof(object[]) }); return method.DeclaringType != baseType; } } }