Skip to content

2.1 Accessing the API

Joseph Korinek edited this page Nov 24, 2021 · 17 revisions

Home || Prev || Next

Accessing the API

As a mod developer, in order to access CLS, you have 3 choices to establish connectivity with CLS.

  1. Create a "hard" dependency to CLS. This is accomplished by adding a reference to CLSInterfaces.dll contained in the Gamedata\ConnectedLivingSpace\Plugins folder in your build, and then directly calling the namespace "ConnectedLivingSpace" in your code. If the .dll does not exist at run time, then a namespace error will occur. This is not the recommended approach, but is doable.

  2. Including a renamed copy of CLSInterfaces.dll in your distribution. This is the recommended approach, both for its fault tolerance, and its ease of use. This file is also located in the Gamedata\ConnectedLivingSpace\Plugins folder. In the CLS download, there is also a Dev folder containing the dll and a snippet of code (shown below in sample 1) that can be used to integrate into your plugin. Including this dll in your distribution allows you to establish a reference and create a namespace reference in your classes for easy reference and less verbose class and method references. You also need not worry about namespace resolution when CLS is not installed.

NOTE: As of KSP 1.12, there is a DLL loading conflict error if duplicate copies of CLSInterfaces.dll are found. Therefore, please be sure to rename this file to be unique. Ex: "MyMod_CLSInterface.dll"

  1. Bind directly to the CLSInterfaces.dll through Reflection. This method negates the need for the existence of the dll in your distribution, as you are detecting the presence of the CLS installation at load. however, you will need to ensure any code accessing the library is segregated, and includes full namespace resolution, making your code more verbose. If CLS is NOT installed, any calls to the CLS namespace will fail, and cause errors in your plugin. An example of this method is when accessing RemoteTech's API, as no stand alone interface library is available for RemoteTech.

Adding CLS to your build

Create a reference to the dll, and add it to your build. Then determine if CLS is installed by attempting to access a core class.

Sample1: Detecting a CLS installation With CLSInterfaces.dll included in your distribution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace YourModNameSpaceHere
{
    class CLSClient
    {
        private static ConnectedLivingSpace.ICLSAddon _CLS = null;
        private static bool? _CLSAvailable = null;

        public static ConnectedLivingSpace.ICLSAddon GetCLS() {
            Type CLSAddonType = AssemblyLoader.loadedAssemblies.SelectMany(a => a.assembly.GetExportedTypes()).SingleOrDefault(t => t.FullName == "ConnectedLivingSpace.CLSAddon");
            if (CLSAddonType != null) {
                object realCLSAddon = CLSAddonType.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
                _CLS =   (ConnectedLivingSpace.ICLSAddon)realCLSAddon;
            }
            return _CLS;
        }

        public static bool CLSInstalled {
            get {
                if (_CLSAvailable == null)
                    _CLSAvailable = GetCLS() != null;
                return (bool)_CLSAvailable;
            }
        }
    }
}

Sample2: Detecting a CLS installation using reflection (Soft Dependency).

    internal static bool clsChecked = false;
    private bool _isClsInstalled = false;
    internal static bool IsCLSInstalled
    {
        get
        {
            if (!_clsChecked)
            {
                string assemblyName = "ConnectedLivingSpace";
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                var assembly = (from a in assemblies
                                where a.FullName.Contains(assemblyName)
                                select a).SingleOrDefault();
                if (assembly != null)
                    _isClsInstalled = true;
                else
                    _isClsInstalled = false;
                _clsChecked = true;
            }
            return _isClsInstalled;
        }
    }

This method checks for the existence of the assembly and returns a bool denoting its existence. Once you know the assembly is present, you can call directly to the namespace using full namespace calls.