SMAPI - Stardew Modding API

SMAPI - Stardew Modding API

971k Downloads

Different Usage time requirements for GetApi vs GetApi<>

ClxS opened this issue ยท 1 comments

commented

Describe the bug
public TInterface GetApi<TInterface>(string uniqueID) where TInterface : class
The above method performs the following check:

if (!this.Registry.AreAllModsInitialized)
{
    this.Monitor.Log("Tried to access a mod-provided API before all mods were initialized.", LogLevel.Error);
    return null;
}

The non-Generic alternative performs no such check, leading to differences in behaviour. I'd argue that since the concept of dependencies exists then there's no need for the AreAllModsInitialized check in the generic version, or that behaviour should be consistent across both.

To Reproduce
Works. diApiObj is set to a value.

var diApiObj = helper.ModRegistry.GetApi(Constants.ModId);
if (!(diApiObj is IDependencyApi diApi))
{
    throw new Exception(
        $"{nameof(LibArcanium)} relies on {nameof(LibClxSDI)} but could not access API using mod key {Constants.ModId}.");
}

Does not work. diApiObj is null and logs error: Tried to access a mod-provided API before all mods were initialised.

var diApi = helper.ModRegistry.GetApi<IDependencyApi>(Constants.ModId);
if (diApi == null)
{
    throw new Exception(
        $"{nameof(LibArcanium)} relies on {nameof(LibClxSDI)} but could not access API using mod key {Constants.ModId}.");
}
commented

Thanks! Fixed in develop for the upcoming SMAPI 3.0.