using Mono.Cecil;
using Mono.Cecil.Cil;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// Finds CIL instructions that reference a given method.
public class MethodFinder : IInstructionFinder
{
/*********
** Properties
*********/
/// The full type name for which to find references.
private readonly string FullTypeName;
/// The method name for which to find references.
private readonly string MethodName;
/*********
** Accessors
*********/
/// A brief noun phrase indicating what the instruction finder matches.
public string NounPhrase { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The full type name for which to find references.
/// The method name for which to find references.
/// A brief noun phrase indicating what the instruction finder matches (or null to generate one).
public MethodFinder(string fullTypeName, string methodName, string nounPhrase = null)
{
this.FullTypeName = fullTypeName;
this.MethodName = methodName;
this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method";
}
/*********
** Protected methods
*********/
/// Get whether a CIL instruction matches.
/// The IL instruction.
/// Whether the mod was compiled on a different platform.
public bool IsMatch(Instruction instruction, bool platformChanged)
{
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
return
methodRef != null
&& methodRef.DeclaringType.FullName == this.FullTypeName
&& methodRef.Name == this.MethodName;
}
}
}