using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace StardewModdingAPI.Framework
{
/// Manages deprecation warnings.
internal class DeprecationManager
{
/*********
** 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);
/*********
** 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)
{
this.ModNamesByAssembly[assembly.FullName] = name;
}
/// Log a deprecation warning.
/// A noun phrase describing what is deprecated.
/// The SMAPI version which deprecated it.
public void Warn(string nounPhrase, string version)
{
this.Warn(this.GetSourceNameFromStack(), nounPhrase, version);
}
/// 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.
public void Warn(string source, string nounPhrase, string version)
{
if (source != null && !this.MarkWarned(source, nounPhrase, version))
return;
Log.Debug(source != null
? $"NOTE: {source} used {nounPhrase}, which is deprecated since SMAPI {version}. It will work fine for now, but may be removed in a future version of SMAPI."
: $"NOTE: an unknown mod used {nounPhrase}, which is deprecated since SMAPI {version}. It will work fine for now, but may be removed in a future version of SMAPI.\n{Environment.StackTrace}"
);
}
/// 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.GetSourceNameFromStack(), 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;
}
/*********
** 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;
}
}
}