Extending Other Services#

A mod can add methods and properties to a class belonging to another service, as KRPCClass lets it add a class to one. A TestFlight mod can add vessel.parts.with_failure(...) to SpaceCenter.Parts. The member is written as a C# extension method, and joins the service that owns the type being extended:

public static class TestFlightExtensions {
    /// <summary>The parts that have the named failure.</summary>
    [KRPCMethod]
    public static IList<Part> WithFailure (
        this Parts parts, string failure)
    {
        throw new NotImplementedException ();
    }
}

The containing class needs no kRPC attribute, only public static. A dynamic client, such as Python or Lua, exposes the member as an ordinary member of the class it extends. A static client, such as C#, C++, C-nano or Java, reaches it once its code is regenerated from the mod’s assembly and the assembly declaring the service being extended; see Generating Service Code for Static Clients.

C# has no extension properties, so a property is a pair of extension methods named GetX and SetX, each carrying KRPCProperty. The getter takes only the instance and returns a value. The setter takes the instance and the value to set, and returns nothing. Either one on its own gives a read-only or a write-only property.

[KRPCProperty]
public static bool GetFailed (this Parts parts)
{
    throw new NotImplementedException ();
}

[KRPCProperty]
public static void SetFailed (this Parts parts, bool value)
{
    // ...
}

An extension member must satisfy the following criteria:

  • The method must be a public static C# extension method, declared in a public static class outside any KRPCService. A class that is not public puts its members out of reach, and the server names it in a warning in the log.

  • The type it extends must be a KRPCClass. A KRPCStruct is a value with a fixed set of fields, and cannot be extended.

  • The name of the member must be a valid kRPC identifier, and must not clash with a member the class already has, or with another extension member. This includes the members of the class’s base classes and derived classes. The getter and the setter of an extension property must be declared in the same class. A member that clashes is left out, as Errors and Warnings describes.

  • The parameter types and return type must be types that kRPC can serialize, and may name types from any service.

Deprecation and the Nullable parameter of the attribute work as they do for a member declared in the class. A setter’s value is an ordinary parameter, so mark it nullable with KRPCNullable. The game scenes and required mods of an extension member are found as Game Scenes and Required Mods describes.

Exposing a Part Module as a Class#

A mod that adds a PartModule to a part can expose it as a class of its own, with typed members alongside the string-keyed fields of SpaceCenter.Module. Declare a KRPCClass deriving from KRPC.SpaceCenter.Services.Parts.Module<T>, where T is the type of the part module. An extension property on SpaceCenter.Part can return it:

[KRPCClass (Service = "TestFlight")]
public class FailureModule : Module<TestFlightFailure> {
    internal FailureModule (Part part, ModuleRef reference)
        : base (part, reference)
    {
    }

    /// <summary>Whether the part has failed.</summary>
    [KRPCProperty]
    public bool Failed {
        get { return InternalModule.failed; }
    }
}

public static class TestFlightExtensions {
    /// <summary>
    /// The failure module of the part, or <c>null</c> if it
    /// does not have one.
    /// </summary>
    [KRPCProperty (Nullable = true)]
    public static FailureModule GetFailureModule (this Part part)
    {
        var reference =
            ModuleRef.ForType<TestFlightFailure> (part.InternalPart);
        return reference == ModuleRef.None
            ? null : new FailureModule (part, reference);
    }
}

Clients reach the module as part.failure_module.failed. SpaceCenter.Part.Modules also returns it as a FailureModule, for a module of type T or of a type derived from it.

Clients see SpaceCenter.Module as the base class, so the class inherits members such as Name and FieldList, and passes where a Module is expected. Its own members reach the game through InternalModule, typed as T.

The constructor takes the part and a KRPC.SpaceCenter.ModuleRef, which names one of a part’s modules by its class and its position among the part’s modules of that class. Module holds the two, and finds the module again on each access. It compares objects by the part and the reference, so the extension property can build an object on each call.

A class for the modules of a mod it does not reference derives from KRPC.SpaceCenter.Services.Parts.NamedModule<TNames>. TNames lists the names of the module types, as a part’s .cfg file names them:

public sealed class SensorModules : ModuleNames {
    public SensorModules () : base ("TestFlightSensor")
    {
    }
}

[KRPCClass (Service = "TestFlight")]
public class SensorModule : NamedModule<SensorModules> {
    internal SensorModule (Part part, ModuleRef reference)
        : base (part, reference)
    {
    }
}

A Module<T> class takes precedence over a NamedModule<TNames> class for the same module type. Two classes of one kind for the same module type are an error, as is a class whose constructor does not take the part and a reference. The server reports these errors with those of its scan of the services. A module type with two classes takes the class of its nearest base type, or is a plain Module.