Wrapping Another Mod#

A service that wraps another mod binds to it by name as the game starts, and so runs whether or not the mod is installed. A subclass of KRPC.Utils.ModAPI holds the binding, and RequiresMod names it on the service:

sealed class WindAPI : ModAPI
{
    public WindAPI () : base ("Wind", "WindMod")
    {
    }

    public static Func<Vessel, double> Speed;
    static Func<object> manager;

    protected override void Bind (ModBinder mod)
    {
        Speed = mod.Type ("WindMod.API")
            .Method<Func<Vessel, double>> ("GetSpeed");
        manager = mod.Type ("WindMod.WindManager")
            .StaticGetter<object> ("Instance");
    }

    protected override string NotReady ()
    {
        return manager () == null ? "not running in this scene" : null;
    }
}

[KRPCService (GameScene = GameScene.Flight)]
[RequiresMod (typeof (WindAPI))]
public static class Wind
{
    [KRPCProperty (GameScene = GameScene.All)]
    public static bool Available {
        get { return ModAPI.Get<WindAPI> ().IsAvailable; }
    }

    [KRPCProperty]
    public static double Speed {
        get { return WindAPI.Speed (FlightGlobals.ActiveVessel); }
    }
}

The constructor gives the mod’s name and the name of its main assembly. The server keeps one instance of each subclass. It calls Bind once, as the game finishes loading, when the game has loaded the mod’s assembly. The constructor only names the mod, as it can run before the game has loaded.

Bind sets static fields through the ModBinder. Its Type method finds a type of the mod by its full name, and binds a getter, setter, field, method or constructor of it. ReadOnlyField binds a field that the mod declares read-only and writes through reflection. A type or member missing from the installed version binds a delegate that throws when called. Only the procedures that use it fail. Bind through mod.Require to fail the whole integration instead. An exception from Bind does the same.

A missing member is a warning in the log. An integration that fails to load is an error in the log, and the kRPC Problems window names it.

NotReady gives the reason the mod cannot serve a call yet, such as before it creates its instance in a scene, or null once it can. Each call checks it once the integration is available.

RequiresMod (params Type[] Types)#

The ModAPI types a service, class, method or property needs, which must all be available. A call fails while a mod it needs is not installed, its integration failed to load or the mod is not ready, with the reason, such as “Wind is not available: not installed”. The check comes before the game scene check.

Several attributes on one member mean any one of them will do, for a member that works with either of two mods.

A class inherits the attributes of its service, and a method or property those of its class. The nearest attributes apply. An extension member takes them from the member, then from the class that declares it, then from the class it extends.

A static property of the service named Available is exempt, as it reports whether the mod can be used. Give it GameScene = GameScene.All to report this in every game scene.

A member with a fallback for when the mod is missing calls ModAPI.Get<WindAPI> ().CheckInstalled () in place of the attribute. It returns false when the mod is not installed, and throws when the mod is installed but its integration failed to load.