using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.AssemblyRewriters.Framework;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// Finds CIL instructions that reference a given field.
public sealed class GenericFieldFinder : BaseFieldFinder
{
/*********
** Properties
*********/
/// The full type name for which to find references.
private readonly string FullTypeName;
/// The field name for which to find references.
private readonly string FieldName;
/// Whether the field to match is static.
private readonly bool IsStatic;
/*********
** 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 field name for which to find references.
/// Whether the field to match is static.
public GenericFieldFinder(string fullTypeName, string fieldName, bool isStatic)
{
this.FullTypeName = fullTypeName;
this.FieldName = fieldName;
this.IsStatic = isStatic;
this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field";
}
/*********
** Protected methods
*********/
/// Get whether a field reference should be rewritten.
/// The IL instruction.
/// The field reference.
/// Whether the mod was compiled on a different platform.
protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{
return
this.IsStaticField(instruction) == this.IsStatic
&& fieldRef.DeclaringType.FullName == this.FullTypeName
&& fieldRef.Name == this.FieldName;
}
}
}