diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-02-25 23:40:52 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-02-25 23:40:52 -0500 |
commit | 0f987c0578b69d57c01502bc44b43b4e9619e658 (patch) | |
tree | 8753e87737990929864d931c725ef8eefefaaf33 /src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs | |
parent | 512c2b9fb761838a852fb4c8fe8dff40bab5f31e (diff) | |
download | SMAPI-0f987c0578b69d57c01502bc44b43b4e9619e658.tar.gz SMAPI-0f987c0578b69d57c01502bc44b43b4e9619e658.tar.bz2 SMAPI-0f987c0578b69d57c01502bc44b43b4e9619e658.zip |
restore InterfaceProxyFactory to encapsulate Pintail (#830)
Diffstat (limited to 'src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs')
-rw-r--r-- | src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs | 42 |
1 files changed, 42 insertions, 0 deletions
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 +{ + /// <summary>Generates proxy classes to access mod APIs through an arbitrary interface.</summary> + internal class InterfaceProxyFactory + { + /********* + ** 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 + )); + } + + /// <summary>Create an API proxy.</summary> + /// <typeparam name="TInterface">The interface through which to access the API.</typeparam> + /// <param name="instance">The API instance to access.</param> + /// <param name="sourceModID">The unique ID of the mod consuming the API.</param> + /// <param name="targetModID">The unique ID of the mod providing the API.</param> + public TInterface CreateProxy<TInterface>(object instance, string sourceModID, string targetModID) + where TInterface : class + { + return this.ProxyManager.ObtainProxy<string, TInterface>(instance, targetContext: targetModID, proxyContext: sourceModID); + } + } +} |