CLEO Redux SDK
SDK provides a way to create new script commands for any game that CLEO Redux supports. It is agnostic to the game title and the underlying runtime (CS or JS). At this moment CLEO provides SDK for the C++ and Rust languages.
- SDK Version
- Platforms Support
- Plugin Lifetime
- Plugin Callbacks
- Path Resolution Convention
- Getting a Directory Path
- Lookup of Game Symbols
- String Arguments
- Developing New Commands
- Developing File Loaders
SDK Version
The current version is 8. Changes to SDK advance this number by one.
Platforms Support
CLEO Redux provides SDK for both 32-bit and 64-bit games. There is one notable change between the two: on the 32-bit platform the SDK functions GetIntParam and SetIntParam operate on signed 32-bit numbers, whereas on the 64-bit platform they operate on signed 64-bit numbers (declared as the type isize).
It's recommended for 64-bit plugins to have 64 in their names (e.g. myplugin64.cleo).
Plugin Lifetime
Each plugin is a dynamic-link library (DLL) with the .cleo extension that must be placed in cleo\cleo_plugins. CLEO Redux scans this directory on startup and loads all .cleo files using WinAPI's function LoadLibrary.
Plugin Callbacks
Besides commands and file loaders a plugin can subscribe to engine events with callback functions:
| Method | When it runs | Since |
|---|---|---|
OnBeforeScripts / OnAfterScripts | on each main loop iteration, before and after scripts are executed | v4 |
OnBeforeGameProcess / OnAfterGameProcess | on every game loop iteration, before and after the game processes a frame. Unlike the script callbacks they also run on frames where no script is executed, e.g. in the pause menu | v8 |
OnRuntimeInit | on each runtime init: a new game, a saved game load, or a call to RuntimeInit | v4 |
OnShowTextBox | on a ShowTextBox call; providing a callback shadows the built-in implementation | v5 |
Availability depends on the host. OnBeforeGameProcess and OnAfterGameProcess are fired only for the supported games. On a manifest or unknown host they never fire: that host drives the runtime itself with RuntimeInit and RuntimeNextTick (see Embedding), and OnBeforeScripts, OnAfterScripts, OnRuntimeInit and OnShowTextBox keep working there. The is_paused and is_restarting flags are derived from the game's own pause timer (CTimer), so they are meaningful only where that game loop hook exists; a host driven through the SDK has to track the pause state itself.
OnBeforeScripts, OnAfterScripts, OnBeforeGameProcess and OnAfterGameProcess receive the current time in milliseconds and the time step (the value added to the TIMERA/TIMERB variables). The game process callbacks also receive is_paused and is_restarting flags. is_paused mirrors CTimer::m_UserPause || CTimer::m_CodePause; is_restarting is true while the runtime is being (re)initialized.
void OnAfterGameProcessTick(unsigned int time, int time_step, bool is_paused, bool is_restarting) {
if (!is_paused) {
// the game has processed this frame
}
}
class MyPlugin {
public:
MyPlugin() {
OnAfterGameProcess(&OnAfterGameProcessTick);
}
} MyPlugin;
#![allow(unused)] fn main() { extern "C" fn on_after_game_process(time: u32, time_step: i32, is_paused: bool, is_restarting: bool) { // ... } #[ctor] fn init() { cleo_redux_sdk::on_after_game_process(on_after_game_process); } }
Path Resolution Convention
String arguments representing a path to the directory or file must be normalized using SDK's function ResolvePath. This function takes a path and returns the absolute path resolved according to the following rules:
-
an absolute path gets resolved as is
-
a path starting with
CLEO/orCLEO\\gets resolved relative to the CLEO directory which is either{game}\cleoor{user}\AppData\Roaming\CLEO Redux\cleo
-
a path starting with
./or../gets resolved relative to the script directory.What is the script directory?
For CS scripts it is always the CLEO directory.
For JS scripts it could be the CLEO directory (if the JS file is located there). Or if the script is located in a subfolder inside the CLEO directory, then the script directory is the one that contains its
index.jsfile.It is different from the resolution algorithm used by the
importstatement. Theimportstatement resolves paths relative to the current file's directory (that can be nested many levels deep), whereas theResolvePathdoes not go deeper than theindex.jsdirectory. You can use the__dirnamevariable to get the directory of the current file:DynamicLibrary.Load(__dirname + "\\mylib.dll"); -
all other paths get resolved relative to the current working directory (the game directory)
Getting a Directory Path
GetDirectoryPath(dir, dest) writes an absolute path to the buffer dest as a null-terminated UTF-8 string. The buffer needs to be large enough to hold the path (for example MAX_PATH bytes). dir is one of the Directory values:
| Value | Path |
|---|---|
Directory::CLEO | the CLEO directory |
Directory::CONFIG | cleo\.config |
Directory::TEXT | cleo\cleo_text |
Directory::PLUGINS | cleo\cleo_plugins |
Directory::CWD | the current working directory (normally the game directory) |
Directory::HOST | the host root directory |
Directory::ASI | the directory cleo_redux.asi / cleo_redux64.asi was loaded from (since SDK v8). An ASI loader may put the file into a subfolder, so it may differ from HOST. |
char asi[MAX_PATH] = {0};
GetDirectoryPath(Directory::ASI, asi);
In Rust use cleo_redux_sdk::get_directory_path(Directory::ASI) which returns a PathBuf.
Lookup of Game Symbols
GetSymbolAddress(name) returns the address of a symbol from the built-in symbol table or from the host's symbol tables. It returns 0 when the symbol is not found.
The name follows the same rules as Memory.Translate (Finding Memory Addresses): a plain name resolves to the absolute address of the symbol, whereas a name with the * prefix is a pointer symbol that resolves to the value stored at that address.
auto addr = GetSymbolAddress("CGame::Process");
String Arguments
Strings passed in and out of the SDK methods are UTF-8 encoded.
If the script uses an integer value where a string is expected SDK treats this number as a pointer to a null-terminated UTF-8 character sequence to read, or to a large enough buffer to store the result to:
IniFile.WriteString(0xdeadbeef, "my.ini", "section", "key");
SDK will read a string from the address 0xDEADBEEF and write it to the ini file.
0AF4: read_string_from_ini_file 'my.ini' section 'section' key 'key' store_to 0xDEADBEEF
SDK will read a string from the ini file and write it at the address 0xDEADBEEF.
Developing New Commands
To register custom command handlers the plugin must call RegisterCommand in the DllMain function. Once a user script encounters this command CLEO Redux invokes the handler with one argument, which is a pointer to the current context. This pointer must be used for calling other SDK methods. See C++ SDK guide or Rust SDK guide for examples.
Unsafe commands
New commands that use low-level WinAPI and can potentially damage user environment must be explicitly registered with a permission token (third argument to the RegisterCommand). User can disallow usage of unsafe commands in the scripts using permission config. At the moment three permission tokens are used: mem, fs, and dll. They mark commands operating with the host process, user files and external libraries.
Command interface
CLEO Redux uses Sanny Builder Library to know an interface of any command. For a new command to become available in the scripts, the JSON file (gta3.json, vc.json, sa.json) must have the command definition, including the name that matches with the value that the plugin uses RegisterCommand with. E.g. if the plugin registers SHOW_MESSAGE command, the JSON file must have a command with the name property set to SHOW_MESSAGE. The number and order of the input and output parameters in the definition must match the order of methods used by the plugin (i.e. GetXXXParam for each input argument and SetXXXParam for each output argument).
Claiming Opcodes
Opcodes get assigned to new commands in Sanny Builder Library based on the availability, similarity with existing commands in other games, and other factors. To claim an opcode reach out to Sanny Builder Library maintainers on GitHub.
Why use command names and not an id for the command lookup?
One of the common issues with CLEO Library plugins was that commands authored by different people often had id collisions. If two plugins add commands with the same id, it is impossible to use them both. Using string names minimizes the collisions with custom plugins as well as with native opcodes. The library's definitions ensure each command claims only an available id. Also it helps to track and document plugins in a single place.
Custom command backends
Available since SDK v8. RegisterJitBackend(host_name, backend) registers a custom backend responsible for translating input commands into the host's expected format. It suits hosts that CLEO does not support out-of-the-box. See the example backend for The Witcher 3.
- the backend owns every command of that host and takes precedence over commands registered with
RegisterCommand host_nameis the name of the host the backend serves- register the backend once, early in the plugin's lifetime; the
CleoJitBackendstruct must stay valid for the lifetime of the process abi_versionmust match the value the running build expects (currently1)
The backend is a vtable of callbacks. CLEO calls create for every command invocation and destroy once that invocation is over:
| Callback | Purpose |
|---|---|
create(command) | returns the state for this invocation. The CleoCommand pointer is borrowed and valid only for the duration of this call |
destroy(state) | releases the state |
input_int, input_float, input_string, input_vector, input_global_var | receive the input values of the command |
before_alloc, alloc_int, alloc_float, alloc_string, alloc_vector | announce the result slots before execute, one alloc_* per declared output, with the declared type (a vector output also passes its size) |
end_of_arguments | terminates a variadic argument list. CLEO calls it only for commands with variadic inputs or outputs, after the result slots are announced |
execute(state, command_name) | runs the command and returns a HandlerResult |
get_int, get_float, get_string, get_handle | return a result by its index; get_string copies the value into dest (at most max_len bytes) |
create receives a CleoCommand with the command name, the member it maps to, the is_static and is_condition flags, and the declared inputs and outputs. This information is sourced from the command definition file.
The exact contract of every callback is described by the comments in cleo_redux_sdk.h.
Developing File Loaders
A plugin can associate itself with a particular glob pattern (or in other words a file name with wildcard characters in it, e.g. *.txt) and provide a handler to be called when a script imports a file matching the pattern.
The glob pattern can be used to match an arbitrary file name using wildcards to represent any characters (*.pak* would match 1.pak1, 2.pak1, 3.pak3, etc) or a specific file name (gta.dat would match only a gta.dat file). Be mindful about using wildcards to avoid matching unnecessary files. The plugin can associate itself with many globs (for example TextLoader uses *.txt and *.text), but it's recommended to support only related formats in one plugin.
The handler gets a full path to the file as an input and returns a pointer to the buffer with the content of the file that has been serialized into JSON. The serialized JSON content should be a valid null-terminated UTF-8 string.
To allocate a memory buffer for the serialized string the plugin MUST call SDK's method
AllocMem. This memory will be released by CLEO Redux once it reads the data from it. Allocating the memory inside the plugin will lead to an error.
If the serialization has failed or the file does not have the expected format (for example due to a glob matching too many files), the handler should return a null pointer. In this case CLEO proceeds to another loader that could handle this file.
See examples of the file loaders implemented for Text files (in C++) and IDE files (in Rust).