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); } } }