summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/ReflectionHelper.cs
blob: 648d6742493b87ec2ed03289ed3faa35e8990eba (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Reflection;
using StardewModdingAPI.Framework.Reflection;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <summary>Provides helper methods for accessing private game code.</summary>
    /// <remarks>This implementation searches up the type hierarchy, and caches the reflected fields and methods with a sliding expiry (to optimise performance without unnecessary memory usage).</remarks>
    internal class ReflectionHelper : BaseHelper, IReflectionHelper
    {
        /*********
        ** Properties
        *********/
        /// <summary>The underlying reflection helper.</summary>
        private readonly Reflector Reflector;

        /// <summary>The mod name for error messages.</summary>
        private readonly string ModName;

        /// <summary>Manages deprecation warnings.</summary>
        private readonly DeprecationManager DeprecationManager;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="modID">The unique ID of the relevant mod.</param>
        /// <param name="modName">The mod name for error messages.</param>
        /// <param name="reflector">The underlying reflection helper.</param>
        /// <param name="deprecationManager">Manages deprecation warnings.</param>
        public ReflectionHelper(string modID, string modName, Reflector reflector, DeprecationManager deprecationManager)
            : base(modID)
        {
            this.ModName = modName;
            this.Reflector = reflector;
            this.DeprecationManager = deprecationManager;
        }

        /// <summary>Get an instance field.</summary>
        /// <typeparam name="TValue">The field type.</typeparam>
        /// <param name="obj">The object which has the field.</param>
        /// <param name="name">The field name.</param>
        /// <param name="required">Whether to throw an exception if the field is not found.</param>
        public IReflectedField<TValue> GetField<TValue>(object obj, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetField<TValue>(obj, name, required)
            );
        }

        /// <summary>Get a static field.</summary>
        /// <typeparam name="TValue">The field type.</typeparam>
        /// <param name="type">The type which has the field.</param>
        /// <param name="name">The field name.</param>
        /// <param name="required">Whether to throw an exception if the field is not found.</param>
        public IReflectedField<TValue> GetField<TValue>(Type type, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetField<TValue>(type, name, required)
            );
        }

        /// <summary>Get an instance property.</summary>
        /// <typeparam name="TValue">The property type.</typeparam>
        /// <param name="obj">The object which has the property.</param>
        /// <param name="name">The property name.</param>
        /// <param name="required">Whether to throw an exception if the property is not found.</param>
        public IReflectedProperty<TValue> GetProperty<TValue>(object obj, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetProperty<TValue>(obj, name, required)
            );
        }

        /// <summary>Get a static property.</summary>
        /// <typeparam name="TValue">The property type.</typeparam>
        /// <param name="type">The type which has the property.</param>
        /// <param name="name">The property name.</param>
        /// <param name="required">Whether to throw an exception if the property is not found.</param>
        public IReflectedProperty<TValue> GetProperty<TValue>(Type type, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetProperty<TValue>(type, name, required)
            );
        }

        /// <summary>Get an instance method.</summary>
        /// <param name="obj">The object which has the method.</param>
        /// <param name="name">The field name.</param>
        /// <param name="required">Whether to throw an exception if the field is not found.</param>
        public IReflectedMethod GetMethod(object obj, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetMethod(obj, name, required)
            );
        }

        /// <summary>Get a static method.</summary>
        /// <param name="type">The type which has the method.</param>
        /// <param name="name">The field name.</param>
        /// <param name="required">Whether to throw an exception if the field is not found.</param>
        public IReflectedMethod GetMethod(Type type, string name, bool required = true)
        {
            return this.AssertAccessAllowed(
                this.Reflector.GetMethod(type, name, required)
            );
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
        /// <typeparam name="T">The field value type.</typeparam>
        /// <param name="field">The field being accessed.</param>
        /// <returns>Returns the same field instance for convenience.</returns>
        private IReflectedField<T> AssertAccessAllowed<T>(IReflectedField<T> field)
        {
            this.AssertAccessAllowed(field?.FieldInfo);
            return field;
        }

        /// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
        /// <typeparam name="T">The property value type.</typeparam>
        /// <param name="property">The property being accessed.</param>
        /// <returns>Returns the same property instance for convenience.</returns>
        private IReflectedProperty<T> AssertAccessAllowed<T>(IReflectedProperty<T> property)
        {
            this.AssertAccessAllowed(property?.PropertyInfo);
            return property;
        }

        /// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
        /// <param name="method">The method being accessed.</param>
        /// <returns>Returns the same method instance for convenience.</returns>
        private IReflectedMethod AssertAccessAllowed(IReflectedMethod method)
        {
            this.AssertAccessAllowed(method?.MethodInfo);
            return method;
        }

        /// <summary>Assert that mods can use the reflection helper to access the given member.</summary>
        /// <param name="member">The member being accessed.</param>
        private void AssertAccessAllowed(MemberInfo member)
        {
            if (member == null)
                return;

            // get type which defines the member
            Type declaringType = member.DeclaringType;
            if (declaringType == null)
                throw new InvalidOperationException($"Can't validate access to {member.MemberType} {member.Name} because it has no declaring type."); // should never happen

            // validate access
            string rootNamespace = typeof(Program).Namespace;
            if (declaringType.Namespace == rootNamespace || declaringType.Namespace?.StartsWith(rootNamespace + ".") == true)
                throw new InvalidOperationException($"SMAPI blocked access by {this.ModName} to its internals through the reflection API. Accessing the SMAPI internals is strongly discouraged since they're subject to change, which means the mod can break without warning. (Detected access to {declaringType.FullName}.{member.Name}.)");
        }
    }
}