Launcher plugins ​

A launcher plugin is a prebuilt program inside your addon that the launcher runs as a subprocess and talks to in newline-delimited JSON over stdin and stdout. It can add:

  • pages in the launcher's sidebar;
  • an importer for files dropped on the window;
  • asset ops contributed when a profile builds;
  • swofty subcommands;
  • a line on the profile card.

Any language that reads and writes a line of JSON works. The examples use templates/launcher-addon, a Rust helper that depends only on serde_json.

A plugin in full ​

This plugin adds one sidebar page with a switch per item, handles the switch's event, and keeps the switches in the profile with two host calls. It is templates/launcher-addon trimmed to those three parts.

json
{
  "schema_version": 1,
  "id": "example.launcher-addon",
  "name": "Example Launcher Addon",
  "version": "0.1.0",
  "author": "Your Name",
  "compat": { "game": "f1_25" },
  "launcher": {
    "program": { "windows": "bin/example-helper.exe", "linux": "bin/example-helper" },
    "sections": [{ "id": "items", "title": "Example", "icon": "package" }]
  }
}
rust
use std::io::{BufRead, Lines, StdinLock, StdoutLock, Write};
use std::path::PathBuf;

use serde_json::{Value, json};

const API: u64 = 1;

struct Host {
    lines: Lines<StdinLock<'static>>,
    out: StdoutLock<'static>,
    next: u64, // ids of the plugin's own host calls
}

impl Host {
    fn send(&mut self, message: &Value) {
        // one JSON object on one line, flushed
        writeln!(self.out, "{message}").and_then(|()| self.out.flush()).ok();
    }

    // A host call: a request with an id; the launcher answers it before anything else.
    fn call(&mut self, method: &str, params: Value) -> Result<Value, String> {
        let id = self.next;
        self.next += 1;
        self.send(&json!({ "id": id, "method": method, "params": params }));
        let line = match self.lines.next() {
            Some(Ok(line)) => line,
            _ => std::process::exit(0), // stdin closed: the launcher is gone
        };
        let reply: Value = serde_json::from_str(&line).map_err(|e| e.to_string())?;
        match reply.get("error") {
            Some(e) => Err(e.to_string()),
            None => Ok(reply["result"].clone()),
        }
    }
}

struct Addon {
    data_dir: PathBuf, // from hello; shared by every profile
    vanilla: bool,     // the untouched profile refuses set_state
}

impl Addon {
    fn items(&self) -> Vec<String> {
        let mut names: Vec<String> = std::fs::read_dir(self.data_dir.join("items"))
            .into_iter()
            .flatten()
            .filter_map(|e| e.ok()?.file_name().into_string().ok())
            .collect();
        names.sort();
        names
    }

    // get_state: this addon's entry in the active profile's state, or null
    fn enabled(&self, host: &mut Host) -> Result<Vec<String>, String> {
        let state = host.call("get_state", Value::Null)?;
        Ok(serde_json::from_value(state["on"].clone()).unwrap_or_default())
    }

    // view: the page, rebuilt on every call
    fn view(&self, host: &mut Host) -> Result<Value, String> {
        let on = self.enabled(host)?;
        let rows: Vec<Value> = self.items().iter().map(|name| json!({
            "id": name,
            "title": name,
            "subtitle": "Example item",
            "trailing": { "switch": { "id": name, "on": on.contains(name), "enabled": !self.vanilla } }
        })).collect();
        Ok(json!({
            "subtitle": format!("{} items, {} on", rows.len(), on.len()),
            "body": { "list": { "rows": rows, "reorder": false } }
        }))
    }

    // event: a switch flipped; store the new set, then ask for the page again
    fn event(&self, host: &mut Host, event: &Value) -> Result<Value, String> {
        if let Some(toggle) = event.get("toggle") {
            let name = toggle["id"].as_str().unwrap_or_default().to_owned();
            let mut on = self.enabled(host)?;
            on.retain(|n| *n != name);
            if toggle["on"].as_bool() == Some(true) {
                on.push(name);
            }
            host.call("set_state", json!({ "on": on }))?; // replaces the whole state value
            return Ok(json!("reload"));
        }
        Ok(json!("none"))
    }
}

fn main() {
    let stdin = std::io::stdin();
    let mut host = Host { lines: stdin.lock().lines(), out: std::io::stdout().lock(), next: 0 };
    let mut addon = Addon { data_dir: PathBuf::new(), vanilla: false };
    while let Some(Ok(line)) = host.lines.next() {
        let Ok(message) = serde_json::from_str::<Value>(&line) else {
            eprintln!("not JSON: {line}"); // stderr goes to helper.log
            continue;
        };
        let params = &message["params"];
        let method = message["method"].as_str().unwrap_or_default();
        let Some(id) = message["id"].as_u64() else {
            // a notification: the active profile changed
            if method == "profile" {
                addon.vanilla = params["vanilla"].as_bool().unwrap_or(false);
            }
            continue;
        };
        let result = match method {
            "hello" => {
                addon.data_dir = PathBuf::from(params["data_dir"].as_str().unwrap_or_default());
                addon.vanilla = params["profile"]["vanilla"].as_bool().unwrap_or(false);
                Ok(json!({ "api": API })) // any other number stops the plugin
            }
            "view" => addon.view(&mut host),
            "event" => addon.event(&mut host, &params["event"]),
            other => Err(format!("unknown method {other}")),
        };
        let reply = match result {
            Ok(result) => json!({ "id": id, "result": result }),
            Err(error) => json!({ "id": id, "error": error }), // fails this call only
        };
        host.send(&reply);
    }
}

The session when the page opens and a switch is flipped:

text
launcher  {"id":0,"method":"hello","params":{"api":1,"addon":"example.launcher-addon","data_dir":"/home/you/.local/share/swofty/addon-data/example.launcher-addon","cache_dir":"/home/you/.local/share/swofty/cache/addons/example.launcher-addon","profile":{"name":"default","vanilla":false,"enabled":true},"launcher":"1.9.29","host_calls":["addons","install_addon","set_addon_enabled","remove_addon","move_addon","tasks","profile_heads","save_dir"]}}
plugin    {"id":0,"result":{"api":1}}
launcher  {"id":1,"method":"view","params":{"section":"items"}}
plugin    {"id":0,"method":"get_state","params":null}
launcher  {"id":0,"result":{"on":["okayama.exmod"]}}
plugin    {"id":1,"result":{"subtitle":"2 items, 1 on","body":{"list":{"rows":[…],"reorder":false}}}}
launcher  {"id":2,"method":"event","params":{"section":"items","event":{"toggle":{"id":"suzuka.exmod","on":true}}}}
plugin    {"id":1,"method":"get_state","params":null}
launcher  {"id":1,"result":{"on":["okayama.exmod"]}}
plugin    {"id":2,"method":"set_state","params":{"on":["okayama.exmod","suzuka.exmod"]}}
launcher  {"id":2,"result":null}
plugin    {"id":2,"result":"reload"}
launcher  {"id":3,"method":"view","params":{"section":"items"}}

The full template also has a dashboard page, an importer, a build hook, a command and a summary. Its Overview section uses every dashboard block, a form, an editable table, tick and invalidate.

Build, install and open it ​

sh
cp -r templates/launcher-addon my-addon        # 1. copy the template (or the smaller helper in Your first addon)
cd my-addon
cargo build --release                          # 2. build one binary per OS
mkdir -p bin
cp target/release/example-helper bin/          #    for Windows, build example-helper.exe and copy it to bin/
swofty addons install .                        # 3. install it
swofty addons enable example.launcher-addon
  • Ship one binary per OS you support, at the path in program. If players use both platforms, ship both.
  • Enable the addon in a modded profile: its Example section shows in the sidebar, under the addon's name. Opening the section starts the plugin and calls view.
  • When something fails, read $DATA/cache/addons/<id>/helper.log (Running and failures).
  • Ship the binaries in the .swoftyaddon (Packaging and publishing).

The smaller helper is in Your first addon.

The launcher block ​

launcher in swofty.json names the program and lists what it answers. The launcher reads the block without starting the plugin.

jsonc
"launcher": {
  // a path inside the addon folder per OS; keys: linux, windows, macos (at least one)
  "program": { "windows": "bin/example-helper.exe", "linux": "bin/example-helper" },
  // one sidebar page each; the launcher lists them without starting the plugin
  "sections": [
    { "id": "overview", "title": "Overview", "icon": "activity" },
    { "id": "items", "title": "Example", "icon": "package" }
  ],
  "imports": ["exmod"],      // extensions claimed from drops, without the dot
  "claims": false,           // true: the program decides which drops it takes (Importers)
  "build": true,             // the plugin answers build
  "commands": [{ "name": "example", "about": "List the example items" }],
  "startup": false           // 1.3.0: send the first section a startup event once per launcher start
}
  • A sections/<id>.svg in your addon replaces the page's icon in the sidebar and on rows without art. icon is one of the fixed icons.
  • Simplified for Swofty's sections are listed under SIMPLIFIED FOR SWOFTY in this order, its mods section as Importer. Every other addon's sections are under ADDON PANELS.
  • The block takes no other keys: an unknown key fails the manifest.

The map ​

  • The protocol: messages, the session, and how the launcher runs the plugin.
  • Host calls: what the plugin asks the launcher while it handles a call.
    • Progress and state: progress, get_state, set_state.
    • Game files: game_build_key, game_exe_version, game_is_stock, game_original, game_original_lang.
    • Folders: game_dir, addon_game_dir, steam_save_dir (1.0.10), save_dir (1.9.29).
    • Game status: game_status (1.0.10).
    • Profile tracks (1.0.12) and profile heads (1.5.12).
    • Managing addons: addons, install_addon, set_addon_enabled, remove_addon, move_addon (1.3.0), and AddonBuilder.
  • Views: the page a section shows.
    • Bodies: list, empty, blocks.
    • Rows: art, tags, a trailing control, detail and a right-click menu.
    • Buttons: styles, sizes and kinds (confirm, pick, selection).
    • Selection dialogs: checkbox or radio choices inside a popup, gated by view.selection_dialog.
    • Tones and icons.
  • Events and effects: what a control sends, and what the plugin answers.
  • Blocks: the content of a blocks body or a row's detail.
    • Basic blocks: stats, facts, label, text, names, buttons.
    • Dashboard blocks (1.0.10): hero, pills, tiles, meters, card, grid.
    • Image (1.0.12) and drop zone (1.3.0).
    • Tabs (1.0.10).
  • Forms (1.0.10): number, slider, text, select, toggle and colour fields with an Apply button, or live.
  • Tables (1.0.10): sortable columns, editable cells, colours, pictures, switches.
    • Colour cells (1.5.25): a cell that opens the colour picker.
    • Row details (1.5.8): a panel beside the table.
  • Live pages (1.0.10): tick from the launcher, invalidate from the plugin.
  • Task lists (1.3.0): a dialog that ticks off items while an event runs.
  • The build reply: asset ops returned at build, with items and fields.
  • Importers: claiming dropped files and folders.
  • Commands and the profile summary: swofty subcommands and a line on the profile card.
  • Changes at runtime: how build-hook ops reach the game while it boots.

Versions ​

Parts of the protocol need a newer launcher. hello carries the launcher's version from 1.0.10, and each page marks what needs which version. Set the lowest launcher you need in swofty.json:

json
"compat": { "game": "f1_25", "launcher": ">=1.0.10" }

Or check launcher in hello and leave out what that launcher can't show. The template sends its colour field and colour column only to launcher 1.5.25 or later:

rust
const COLOURS: [u64; 3] = [1, 5, 25];

fn at_least(version: &str, wanted: [u64; 3]) -> bool {
    let core = version.split(['-', '+']).next().unwrap_or_default(); // without a pre-release or build suffix
    let parts: Vec<u64> = core.split('.').map(|p| p.parse().unwrap_or(0)).collect();
    parts.len() == 3 && parts[..] >= wanted[..]
}

// in hello
addon.colours = at_least(params["launcher"].as_str().unwrap_or_default(), COLOURS);

An older launcher rejects a view that holds a block it doesn't know, and stops a plugin that sends a host call it doesn't know. Both mark the plugin failed (Isolation and failures).