API version 1
NitroLeaps has built-in plugins (Rust, compiled into the launcher) and installed plugins (WebAssembly files). Every enabled plugin has a tab of its own in the top bar and draws all of it. The Plugins tab lists the plugins, switches each on or off, installs and uninstalls.
Overview
An installed plugin is one .wasm file: the same file runs on Linux, Windows and macOS, on any processor. Plugins → Install plugin… checks the file and copies it to <config>/plugin-packages/<id>.wasm; nothing of it runs while installing. An installed plugin is switched on at once (its tab appears and it starts); switch it off on the Plugins page. The file dialog opens in the folder the last plugin was picked in (plugin_folder in gui.toml). Installing a plugin whose id is already installed is refused: uninstall it first. A plugin keeps its settings in <config>/plugins/<id>/; uninstalling leaves them, and leaves the games it downloaded.
The plugin runs sandboxed in an interpreter. It sees only its own memory and one host function (nlps.log). Each call may run a bounded number of instructions, more for a larger message (4 billion and 2000 for each byte of the message; a plugin that loops is stopped and reported, with a Restart button) and memory is limited to 512 MiB. Everything else is a request the launcher checks first.
Manifest
The manifest is JSON in the module's custom section nlps.manifest:
{
"id": "homebrew-catalogue",
"name": "Homebrew",
"version": "1.0.0",
"description": "Free homebrew from a catalogue you choose",
"api": 1,
"icon": "grid",
"permissions": ["network"]
}
id is [a-z0-9-]+. icon is one of folder grid list controller joystick trophy search chip display settings package. Unknown keys are refused. A plugin's own logo is a PNG, JPEG or WebP picture (at most 256 KiB, shown at 20 points) in the custom section nlps.logo; it replaces icon on the tab and shows on the Plugins page. A logo drawn in white on a transparent background takes the colour of the text around it, as the built-in icons do. The Plugins tab shows the permissions: network (https requests, downloads, pictures and pages from the web) and, added to it, local_network (plain http requests to this computer and the local network, for a server the user runs at home) and torbox (calls to the user's TorBox account).
Exports and calls
The module exports memory, nlps_alloc(len: i32) -> i32 and nlps_handle(ptr: i32, len: i32) -> i64. To send a message the launcher calls nlps_alloc, writes the message (UTF-8 JSON) there and calls nlps_handle, which returns its reply as ptr << 32 | len (0: no reply). The reply must stay valid until the next call. It may import only nlps.log(ptr: i32, len: i32), which writes a line to the launcher's log.
A reply is {"view": {...}, "requests": [...]}; both are optional, and a missing view keeps the current one.
Messages to the plugin
{"type":"context", "api":1, "games":[…], "library_roots":[…], "games_dir":"…", "download_dir":"…", "scanning":false, "torbox":false, "download_page":false, "delete":true}at start and whenever the library changes (torbox: the plugin has the permission and the user set a key;download_page: the launcher can show a download page, see below;delete: the launcher has thedeleterequest). A game is{"key","title","title_id","version","path","dir","size","package","can_run"}.{"type":"event", "id":"…", "value":…, "submit":true, "fields":{…}}when the user clicks or changes a widget;fieldsholds every field's current value.{"type":"http", "task":"…", "ok":true, "body":"…"}or{"ok":false, "error":"…"}. The answer to anhttprequest also has"status"and"headers"(the answer's headers, names in lower case), and isokwhatever the status;okis false only when no answer came.{"type":"download", "task":"…", "state":"progress|done|failed|cancelled", "received":…, "total":…, "path":"…", "error":"…"}.{"type":"download_page", "task":"…", "ok":true, "url":"https://…", "file":"…"}when a download page started a file download (file: the name the host gives it, or empty), or{"ok":false, "error":"…"}("cancelled": the user closed the window before any download started).{"type":"sign_in", "task":"…", "ok":true, "redirect":"http://127.0.0.1:…/callback", "query":{…}}when the browser came back from a sign-in (query: the parameters it brought), or{"ok":false, "error":"…"}("cancelled", or nobody signed in in time).{"type":"extract", "task":"…", "state":"progress|done|failed|cancelled", "written":…, "files":…, "path":"…", "error":"…"}.{"type":"torbox", "task":"…", "ok":true, "status":200, "body":"…"}(TorBox's answer as it is, whatever the status) or{"ok":false, "error":"…"}when no answer came or no key is set.{"type":"state", "name":"…", "text":"…"|null}.{"type":"timer", "task":"…"}when a timer ends.
Requests
| Request | Does |
|---|---|
{"type":"status","text"} |
a status bar line |
{"type":"rescan"} |
scans the library again |
{"type":"launch","key"} / {"type":"show_game","key"} |
starts / shows a library game |
{"type":"open","target"} |
opens an https page (network) or a folder the plugin may read |
{"type":"add_library_folder","path"} |
asks the user whether to add a folder (a full path) to the library; added only on their yes |
{"type":"install_package","path","title"} |
installs a package from a library or download folder into the games folder |
{"type":"http_get","task","url","limit"} |
https GET, at most 16 MiB (network) |
{"type":"http","task","url","method","headers","body","limit"} |
one request: method is GET (default), POST, PUT, PATCH or DELETE, headers an object of texts, body a text; the answer is at most 16 MiB; redirects are not followed (network). The URL is https, or plain http to localhost or a literal loopback, private or link-local address (local_network) |
{"type":"download","task","url","file","dir"} |
https download into the shared download folder, or into the library folder dir names (one of library_roots or games_dir, as the context gives it), as file (network); resumes a partial file; the library rescans when it ends |
{"type":"download_page","task","url","title"} |
shows an https page of a file host in a window of its own (network; where the context says download_page). The user does there what the page asks for; when the page starts a file download the window closes and a download_page message brings the file's address, which a download request then fetches (the launcher sends the page's cookies with it; the plugin never sees them). Nothing is saved by the page itself, and a link to another site that would open a new window is dropped. cancel closes the window |
{"type":"sign_in","task","url","path","port","timeout"} |
a sign-in in the user's browser (network), see below |
{"type":"extract","task","path","folder","password","dir"} |
extracts an archive from a folder the plugin may read into the new folder folder of the download folder (or of the library folder dir names, as for download), see below |
{"type":"torbox","task","method","path","query","form","file","json","limit"} |
one call to the TorBox API on the user's account (torbox), see below |
{"type":"delete","file","dir"} |
deletes the file a download made under that name in that folder (file and dir as for download), and what is left of one that did not end. Only a plain file right in the folder goes; no answer comes |
{"type":"cancel","task"} |
stops a download, an extraction or a sign-in |
{"type":"timer","task","ms"} |
a timer message after ms milliseconds (250 to 60000; at most 16 waiting), for a plugin that looks again at something that changes |
{"type":"read_state","name"} / {"type":"write_state","name","text"} |
files in the plugin's state folder |
The download folder is the first library folder, else the games folder, so downloads appear in the library without a folder of their own. File and state names are plain names (no folders). A plugin reads files only inside the library folders, the games and download folders and its state folder.
Sign-in in the browser
For a service the user signs in to on its own web page (OAuth and the like). The launcher listens on 127.0.0.1 (port: 1024 to 65535 for a service that wants a registered address; left out: any free port), replaces {redirect} in url (https) with the address it listens on, percent-encoded (http://127.0.0.1:<port><path>; path is /callback unless given), and opens the page in the user's browser. When the service sends the browser back there, the plugin gets a sign_in message with the parameters of that request: its query, the fields of a form post, or what stood after #. The browser shows a line saying the window can be closed. The launcher waits timeout seconds (30 to 900, 300 unless given) for one return, then stops listening. Any program on the computer can open that address while the launcher waits, so send a random state with the sign-in and check that it comes back. The plugin exchanges what it got (a code) with its own http requests and keeps tokens in its state folder.
Archives
extract unpacks zip, 7z, rar, tar and their compressed forms with the system's bsdtar (libarchive; part of macOS and Windows, a package on Linux; without it the request fails saying so). The files come out into a hidden folder that becomes folder when all are out, so the library never sees half an archive; folder must not exist yet. Entries that would land outside the folder are refused and links in the archive are dropped. password opens an encrypted zip. progress messages carry the bytes written so far, done the folder's path, its files and bytes; the library rescans when it ends. An archive split into several volumes is not read.
TorBox
The user pastes their TorBox API key once on the Plugins page (it is kept in <config>/torbox.key); a plugin with the torbox permission then calls the API without ever holding the key. path is the endpoint after https://api.torbox.app/v1/api/ (torrents/mylist, torrents/createtorrent, usenet/requestdl, …), method is GET unless given, query an object of texts, numbers or true/false. A body is either form (an object sent as a multipart form, with file, a readable file of at most 16 MiB such as a .torrent in the download folder, as its file field) or json (any value). The launcher adds the Authorization header, and the token parameter to requestdl calls; a call that names a token itself is refused. The answer comes back as TorBox sent it:
{"type":"torbox","task":"add","method":"POST","path":"torrents/createtorrent","form":{"magnet":"magnet:?xt=…"}}
{"type":"torbox","task":"link","path":"torrents/requestdl","query":{"torrent_id":7,"file_id":0}}
The link in the answer of the second goes to a download request, and an archive that arrives to extract.
Widgets
A view is {"widgets":[…]}. Each widget is an object with "type":
title {text, subtitle}, heading {text}, text {text, style: normal|strong|weak|mono|error|small}, button {id, text, enabled, primary, hint}, link {id, text}, input {id, value, hint, label, secret, width}, checkbox {id, text, value}, select {id, label, value, options:[{value,label}]}, progress {value 0..1, text}, image {src, width, height, id} (a readable file, or an https URL with the network permission; PNG, JPEG, WebP; loaded when it comes on screen), row {children} (wraps), column {children}, card {children, width, id, selected} (with id, a click anywhere on the panel that no widget inside takes is an event; selected draws its border in the accent colour), grid {id, columns, children}, list {id, row_height, rows, height, more, item_width} (only visible rows are drawn; with more, an event with value "end" when the last row comes into view, once per number of rows, for a list that loads as the user scrolls; with item_width, the rows are items of that width laid side by side, as many on a line as the window's width takes, each line row_height high), scroll {id, children, height}, separator, space {size}.
Text a user types stays until the plugin sends a different value for that field.
