summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs')
-rw-r--r--src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs26
1 files changed, 26 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;
+ }
+ }
}
}