Skip to content

2.1 Accessing the API

Joseph Korinek edited this page May 20, 2015 · 17 revisions

Accessing the API

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

  1. Including CLSInterfaces.dll in your distribution. This file is also located in the ConnectedLivingSpace\Plugins folder of the CLS distribution. In the CLS download, there is a Dev folder containing the dll and a snippet of code that can be used to integrate in. Including this dll in your distribution allow you to establish a reference and create a namespace reference in your classes for easy reference and less verbose class and method references.

  2. Bind to the CLSInterfaces.dll through Reflection. This method negates the need for the existance of the dll in your distribution, as you are detecting the presense of it at load. however, you will need to ensure any code accessing the library is segragated, and includes full namespace resolution, making your code more verbose.

Create a reference to the dll, and add the reference to your build.

Alternatively, you can simply add a reference to the dll in your build and add conditional logic to use reflection to determine if the dll exists, and therefore if CLS exists in the KSP game that is currently running.

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).

Clone this wiki locally