using Mono.Cecil; using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Finders { /// Finds CIL instructions that reference a given field. public class FieldFinder : IInstructionFinder { /********* ** 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; /********* ** 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 field name for which to find references. /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). public FieldFinder(string fullTypeName, string fieldName, string nounPhrase = null) { this.FullTypeName = fullTypeName; this.FieldName = fieldName; this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field"; } /********* ** 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) { FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); return fieldRef != null && fieldRef.DeclaringType.FullName == this.FullTypeName && fieldRef.Name == this.FieldName; } } }