diff options
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs | 26 | ||||
-rw-r--r-- | src/SMAPI/IModRegistry.cs | 7 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs b/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs index ef1ad30c..92c52b00 100644 --- a/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs @@ -98,5 +98,31 @@ namespace StardewModdingAPI.Framework.ModHelpers return castApi; return this.ProxyFactory.CreateProxy<TInterface>(api, this.ModID, uniqueID); } + + /// <inheritdoc /> + public bool TryProxy<TInterface>(string uniqueID, object toProxy, out TInterface proxy) where TInterface : class + { + try + { + foreach (var toProxyInterface in toProxy.GetType().GetInterfaces()) + { + var unproxyBuilder = this.ProxyFactory.ObtainBuilder(typeof(TInterface), toProxyInterface, this.ModID, uniqueID); + if (unproxyBuilder.TryUnproxy(toProxy, out object targetInstance)) + { + proxy = (TInterface)targetInstance; + return true; + } + } + + var proxyBuilder = this.ProxyFactory.ObtainBuilder(toProxy.GetType(), typeof(TInterface), this.ModID, uniqueID); + proxy = (TInterface)proxyBuilder.ObtainInstance(toProxy, this.ProxyFactory); + return true; + } + catch + { + proxy = null; + return false; + } + } } } diff --git a/src/SMAPI/IModRegistry.cs b/src/SMAPI/IModRegistry.cs index 10b3121e..9b99e459 100644 --- a/src/SMAPI/IModRegistry.cs +++ b/src/SMAPI/IModRegistry.cs @@ -25,5 +25,12 @@ namespace StardewModdingAPI /// <typeparam name="TInterface">The interface which matches the properties and methods you intend to access.</typeparam> /// <param name="uniqueID">The mod's unique ID.</param> TInterface GetApi<TInterface>(string uniqueID) where TInterface : class; + + /// <summary>Try to proxy (or unproxy back) the given object to a given interface provided by a mod.</summary> + /// <typeparam name="TInterface">The interface type to proxy (or unproxy) to.</typeparam> + /// <param name="uniqueID">The mod's unique ID.</param> + /// <param name="toProxy">The object to try to proxy (or unproxy back).</param> + /// <param name="proxy">The reference to store the proxied (or unproxied) object back.</param> + bool TryProxy<TInterface>(string uniqueID, object toProxy, out TInterface proxy) where TInterface : class; } } |