using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.AssemblyRewriters.Framework;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// Finds CIL instructions that reference a given event.
public sealed class GenericEventFinder : BaseMethodFinder
{
/*********
** Properties
*********/
/// The full type name for which to find references.
private readonly string FullTypeName;
/// The event name for which to find references.
private readonly string EventName;
/*********
** Accessors
*********/
/// A brief noun phrase indicating what the instruction finder matches.
public override string NounPhrase { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The full type name for which to find references.
/// The event name for which to find references.
public GenericEventFinder(string fullTypeName, string eventName)
{
this.FullTypeName = fullTypeName;
this.EventName = eventName;
this.NounPhrase = $"obsolete {fullTypeName}.{eventName} event";
}
/*********
** Protected methods
*********/
/// Get whether a method reference should be rewritten.
/// The IL instruction.
/// The method reference.
/// Whether the mod was compiled on a different platform.
protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged)
{
return methodRef.DeclaringType.FullName == this.FullTypeName
&& (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName);
}
}
}