summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs
blob: 694c563dd4741086d596da19c1101a603fa1eede (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
using System.Reflection;
using System.Reflection.Emit;
using Nanoray.Pintail;

namespace StardewModdingAPI.Framework.Reflection
{
    /// <inheritdoc />
    internal class InterfaceProxyFactory : IInterfaceProxyFactory
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying proxy type builder.</summary>
        private readonly IProxyManager<string> ProxyManager;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public InterfaceProxyFactory()
        {
            AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName($"StardewModdingAPI.Proxies, Version={this.GetType().Assembly.GetName().Version}, Culture=neutral"), AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("StardewModdingAPI.Proxies");
            this.ProxyManager = new ProxyManager<string>(moduleBuilder, new ProxyManagerConfiguration<string>(
                proxyPrepareBehavior: ProxyManagerProxyPrepareBehavior.Eager,
                proxyObjectInterfaceMarking: ProxyObjectInterfaceMarking.Disabled
            ));
        }

        /// <inheritdoc />
        public TInterface CreateProxy<TInterface>(object instance, string sourceModID, string targetModID)
            where TInterface : class
        {
            return this.ProxyManager.ObtainProxy<string, TInterface>(instance, targetContext: targetModID, proxyContext: sourceModID);
        }
    }
}