add TryProxy for any objects
This commit is contained in:
parent
467375a7a3
commit
a54d58d064
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue