SMAPI - Stardew Modding API

SMAPI - Stardew Modding API

971k Downloads

Problem with calling an API using an interface as a return value or parameter

Xebeth opened this issue ยท 5 comments

commented

Describe the bug
While trying to implement the API integration with ModTabSettings I ran into a problem with the proxy class generated to bind the API interface to the method in the ModTabSettings mod. The method used in the API uses an interface as a return value (ISettingsTabApi) so it might be partly on @GilarF's side too. I have tried returning the ISettingsTabApi object as an out parameter but that results into a different error: The ModUsingAPI.IModAPI interface defines method GetThing which doesn't exist in the API. (probably a separate issue).

To Reproduce
I created a repository to illustrate the problem. It contains a mod providing an API and a mod using the API. This results in an exception (see the full log):
Failed loading type: 'StardewModdingAPI.Proxies.From<Blackclaw.ModUsingAPI_ModUsingAPI.IModAPI>_To<Blackclaw.ModWithAPI_ModWithAPI.ModAPI>': System.TypeLoadException: Method 'GetThing' in type 'StardewModdingAPI.Proxies.From<Blackclaw.ModUsingAPI_ModUsingAPI.IModAPI>_To<Blackclaw.ModWithAPI_ModWithAPI.ModAPI>' from assembly 'StardewModdingAPI.Proxies, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

Log file
Log file

commented

I tried referencing the ModTabSettings.dll and it didn't help, though. If I understand the issue correctly, @GilarF should expose his API through an interface which I would need to use, right?

commented

The mod has this example on its mod page:

ITabApi api = helper.ModRegistry.GetApi<ISettingsTabApi>("GilarF.ModSettingsTab").GetMod("YourModUniqueId");

The example seems wrong though; its API doesn't implement ISettingsTabApi, and ISettingsTabApi doesn't have a GetMod method.

I suggest opening an issue in the author's repo to clarify.

commented

If the IThing interface is replaced with a bool everything works as expected.

commented

In my original mod, referencing the ModSettingsTab.dll assembly doesn't seem to improve anything.

commented

That's a known limitation when mapping the API to a custom interface; only the main API interface is proxied by SMAPI. I expanded the known limitations in the docs to clarify that. (Pull requests are welcome if someone wants to rework the API proxying!)

The solution in this case is to reference ModSettingsTab.dll, remove your custom interface, and use the one provided by the mod directly.