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 staticC# extension method, declared in apublic staticclass outside anyKRPCService. 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. AKRPCStructis 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. 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 GameScene parameter defaults to the game scenes of the class
being extended.
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, in place of the
string-keyed SpaceCenter.Module API. Declare a KRPCClass wrapping the module, and
add a nullable extension property on SpaceCenter.Part that returns it. The wrapper names the
module with a KRPC.SpaceCenter.ModuleRef, as SpaceCenter.Module does:
[KRPCClass (Service = "TestFlight")]
public class FailureModule
: Equatable<FailureModule>, IGameObjectState {
// The part the module is on, and which of the part's modules it is
readonly Part part;
ModuleRef reference;
internal FailureModule (Part part, ModuleRef reference)
{
this.part = part;
this.reference = reference;
}
// The module, which every member reaches the game through
TestFlightFailure InternalModule {
get {
return (TestFlightFailure)reference.Get (part.InternalPart);
}
}
/// <summary>
/// The state of the module, which is its part's until the part is
/// there to look in.
/// </summary>
public GameObjectState GameObjectState {
get { return reference.StateOn (part); }
}
/// <summary>Returns true if the objects are equal.</summary>
public override bool Equals (FailureModule other)
{
return !ReferenceEquals (other, null) && part == other.part &&
reference == other.reference;
}
/// <summary>Hash code for the object.</summary>
public override int GetHashCode ()
{
return Hash.Of (part).And (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);
}
}
The server keeps the object in a store keyed on the object itself, so the rules the SpaceCenter
classes follow apply here too. A ModuleRef names one of a part’s modules by its class and its
position among the part’s modules of that class, which outlives any one of them. The wrapper holds
the part and the reference, and finds the module again on each access.
Equals and GetHashCode read that identity alone, never a resolved game object, and build the
hash with KRPC.Utils.Hash. The extension property builds a wrapper on each call, so value
identity is what keeps the store to one entry for the module.
ModuleRef.Get raises KRPC.Service.KRPC.ObjectDestroyedException once the part no longer has
the module. ModuleRef.StateOn gives the module the state of its part, which is what lets the
store drop the object once the game destroys the part.
The reference records where it last found the module, so the field holding one is declared without
readonly. A readonly field is copied on each access, and the record is written to the copy.
Clients reach the module as part.failure_module.failed.