-
Notifications
You must be signed in to change notification settings - Fork 36
2.1 Accessing the API
Accessing the API
As a mod developer, in order to access CLS, you have 3 choices to establish connectivity with CLS.
-
Create a "hard" dependency to CLS. This is accomplished by adding a reference to CLSInterfaces.dll containe 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 in your code, then a namespace error will occur. This is not the recommended approach, but is doable.
-
Including CLSInterfaces.dll in your distribution. This is the most recommended approach, for its fault tolearance, and its ease of use. 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 (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.
-
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. Any calls to the namespace will fail, and cause errors in your plugin. Accessing RemoteTech's API is accomplished using this method, 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).