summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs
blob: 056422a4132dd649b8f19945851b6d4cb11e7650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.AssemblyRewriters.Framework;

namespace StardewModdingAPI.AssemblyRewriters.Finders
{
    /// <summary>Finds CIL instructions that reference a given field.</summary>
    public sealed class GenericFieldFinder : BaseFieldFinder
    {
        /*********
        ** Properties
        *********/
        /// <summary>The full type name for which to find references.</summary>
        private readonly string FullTypeName;

        /// <summary>The field name for which to find references.</summary>
        private readonly string FieldName;

        /// <summary>Whether the field to match is static.</summary>
        private readonly bool IsStatic;


        /*********
        ** Accessors
        *********/
        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
        public override string NounPhrase { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="fullTypeName">The full type name for which to find references.</param>
        /// <param name="fieldName">The field name for which to find references.</param>
        /// <param name="isStatic">Whether the field to match is static.</param>
        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
        *********/
        /// <summary>Get whether a field reference should be rewritten.</summary>
        /// <param name="instruction">The IL instruction.</param>
        /// <param name="fieldRef">The field reference.</param>
        /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
        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;
        }
    }
}