.. _service-api-extension-members: Extending Other Services ======================== A mod can add methods and properties to a class belonging to another service, as :cs:attr:`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: .. literalinclude:: /scripts/extending/extending-other-services/ExtensionMethod.lib.cs :language: csharp :start-after: [docs] :end-before: [/docs] :dedent: 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 :ref:`service-api-clientgen`. C# has no extension properties, so a property is a pair of extension methods named ``GetX`` and ``SetX``, each carrying :cs:attr:`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. .. literalinclude:: /scripts/extending/extending-other-services/ExtensionProperty.lib.cs :language: csharp :start-after: [docs] :end-before: [/docs] :dedent: 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 :cs:attr:`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 :cs:attr:`KRPCClass`. A :cs:attr:`KRPCStruct` is a value with a fixed set of fields, and cannot be extended. * The name of the member must be a valid :ref:`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 :ref:`service-api-errors` describes. * The parameter types and return type must be :ref:`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 :cs:attr:`KRPCNullable`. The game scenes and required mods of an extension member are found as :ref:`service-api-availability` 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 :cs:attr:`KRPCClass` deriving from ``KRPC.SpaceCenter.Services.Parts.Module``, where ``T`` is the type of the part module. An extension property on ``SpaceCenter.Part`` can return it: .. literalinclude:: /scripts/extending/extending-other-services/PartModuleClass.lib.cs :language: csharp :start-after: [docs] :end-before: [/docs] :dedent: 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`` lists the names of the module types, as a part's ``.cfg`` file names them: .. literalinclude:: /scripts/extending/extending-other-services/PartModuleClass.lib.cs :language: csharp :start-after: [docs-named] :end-before: [/docs-named] :dedent: A ``Module`` class takes precedence over a ``NamedModule`` 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``.