From 0f987c0578b69d57c01502bc44b43b4e9619e658 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 25 Feb 2022 23:40:52 -0500 Subject: restore InterfaceProxyFactory to encapsulate Pintail (#830) --- .../Framework/Reflection/InterfaceProxyFactory.cs | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs (limited to 'src/SMAPI/Framework/Reflection') diff --git a/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs new file mode 100644 index 00000000..40adde8e --- /dev/null +++ b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs @@ -0,0 +1,42 @@ +using System.Reflection; +using System.Reflection.Emit; +using Nanoray.Pintail; + +namespace StardewModdingAPI.Framework.Reflection +{ + /// Generates proxy classes to access mod APIs through an arbitrary interface. + internal class InterfaceProxyFactory + { + /********* + ** Fields + *********/ + /// The underlying proxy type builder. + private readonly IProxyManager ProxyManager; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + 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(moduleBuilder, new ProxyManagerConfiguration( + proxyPrepareBehavior: ProxyManagerProxyPrepareBehavior.Eager, + proxyObjectInterfaceMarking: ProxyObjectInterfaceMarking.Disabled + )); + } + + /// Create an API proxy. + /// The interface through which to access the API. + /// The API instance to access. + /// The unique ID of the mod consuming the API. + /// The unique ID of the mod providing the API. + public TInterface CreateProxy(object instance, string sourceModID, string targetModID) + where TInterface : class + { + return this.ProxyManager.ObtainProxy(instance, targetContext: targetModID, proxyContext: sourceModID); + } + } +} -- cgit