openOMSI GitHub Discord
Writing plugins

Docs / Plugins / Writing plugins

Full access

6 min readEdit this pagedocs/PLUGINS.md

On this page
  1. Asking for full access
  2. Giving a plugin full access
  3. What a plugin with full access gets
  4. Native plugins

A plugin can do anything a program on the computer can, once the player allows it: read and write any file, connect to any address, run any command, load C modules and native code. The game API stays the same; the sandbox and its limits go.

Asking for full access#

A plugin stays in the sandbox unless it asks, and asking is not having.

Plugin How it asks
Lua file or folder a line --@full_access among the first 30 lines of its entry file, or a call of omsi.plugin.request_full_access()
compiled .oop the native permission in its manifest
native library always (it is native code)

Scripts a vehicle or a scenery object brings can never have full access: they come with content, possibly from another player's game. A server can never give it either.

Giving a plugin full access#

Only the player gives it, in one of two places:

  • In the game. When a plugin asks, the game shows its own window (not a plugin panel, which could fake it): the plugin's name, its entry file under Details, the fingerprint of its code and what full access means. Allow once gives it until the game ends, Always allow keeps it, Deny keeps the plugin in the sandbox for this session. Esc denies; Enter never allows. The plugin starts again with full access once allowed.
  • In the launcher. The Plugins page marks each plugin that asks. Allow full access (after a warning) gives it for the code the last game checked; a plugin the game has not loaded yet cannot be given it there.

The consent is bound to the plugin's code. It covers the entry file and, for a folder plugin, the code in its folder (Lua modules, native libraries, package files), as a SHA-256 fingerprint. A plugin whose code changes, an update from the catalog included, asks again.

For a one-file plugin in the top-level plugins folder the fingerprint covers the other Lua files there too, because require can load them: put a plugin that asks for full access in a folder of its own.

"Always allow" is kept in ~/.openomsi/native-plugins.txt, never in the plugins folder, which downloads and catalog installs write into. A line names the plugin and the fingerprint of the code that was allowed:

# Plugins with full access to this computer ...
dispatcher/ sha256:4f1c...e9a0
notes.lua sha256:77b2...0c41

To take access away, use Take full access away on the Plugins page, or remove the line. A line without a fingerprint gives nothing. A plugin can read whether it has full access with omsi.plugin.full_access().

For developers. While you work on a plugin its fingerprint changes with every save. Set the environment variable OMSI_PLUGIN_FULL_ACCESS_DEV to its name (as the launcher lists it, for example myplugin/; several separated by commas) to skip the question for that name on your machine. The game logs a warning for each such plugin. Never set it for plugins you did not write.

Only give full access to plugins you trust as much as a program you install: they run with your account's rights.

What a plugin with full access gets#

Part What it can do
Lua the whole standard library: io, all of os (execute, remove, getenv), debug, dofile, loadfile, binary chunks, and require of Lua and C modules from the plugin's folder (also lib/) and from Lua's own search paths
Limits no 50 ms limit per call and no memory limit (an endless loop now hangs the game)
omsi.fs read, write, list, copy, move and delete any file or folder; ~/ is the home folder, a relative path starts in the plugin's folder
omsi.net TCP clients and servers, UDP to and from any address (broadcast too), two-way WebSockets with ws:// and wss://
omsi.http HTTP and HTTPS requests with any method, headers and body, on a thread (http.request) or waiting (http.fetch)
omsi.system run commands (system.exec, system.spawn), open files and web pages, the environment, the system's name and the wall clock
WebAssembly the same functions, and no limit on a call's work

Changing the game itself needs no full access: the game's own permissions cover it (see The game's insides).

Everything that waits on the network or a program runs on its own thread: a call returns a handle at once, and the result comes to the function you gave in the next frames.

Lua
-- a small HTTP server: open http://localhost:8080 in a browser
omsi.net.listen(8080, function(e)
  if e.type == "accept" then
    local body = omsi.json.encode({ speed = omsi.speed(), next_stop = omsi.duty.next_stop() })
    omsi.net.send(e.handle, "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n" .. body)
    omsi.net.close(e.handle)
  end
end)

-- report each trip to a company server
omsi.on("trip_done", function()
  omsi.http.request({ url = "https://example.org/trips", method = "POST", body = omsi.json.encode(omsi.duty.get()) },
    function(r) if not r.ok then omsi.warn("upload failed: " .. (r.error or r.status)) end end)
end)

-- any Lua C module, for example LuaSocket next to the plugin
local socket = require("socket")

The full list is in the API reference.

Native plugins#

A native plugin is a shared library built for the player's system, loaded into the game. It has full access and can use its own threads, files and network as any program does, and it reaches the whole plugin API by name, as a WebAssembly plugin does.

It is loaded only once the player allowed it, in the game's window or on the launcher's Plugins page; until then not one byte of it runs.

Where Name
the plugins folder <Name>.native.dll (Windows), .so (Linux, Android), .dylib (macOS)
a plugin's own folder <Name>/plugin.native.dll, .so or .dylib

Its data folder is <Name>.data/ (or <Name>/data/), and its own folder is where relative paths start.

A Rust plugin is a cdylib. The repository's example, crates/omsi-plugin/native-demo, has a small SDK (sdk.rs) to copy:

Rust
mod sdk;
use serde_json::json;

native_plugin!(|| {
    sdk::on("stop_arrive", |args| sdk::log(&format!("arrived at {}", args[0])));
    sdk::every(1.0, || {
        let kmh = sdk::call("speed", json!([])).ok();
        let _ = sdk::call("message", json!([format!("{kmh:?} km/h"), 1]));
    });
    // std works as in any program
    std::thread::spawn(|| { /* a server, a serial port, a database */ });
});
shell
cargo build -p omsi-native-demo-plugin --release

Copy target/release/libomsi_native_demo_plugin.dylib (or .so, or omsi_native_demo_plugin.dll) to the plugins folder as NativeDemo.native.dylib and start the game: it asks whether the plugin may load.

The C interface, for other languages:

c
typedef void (*openomsi_out)(void *user, const char *text, size_t len);
typedef struct {
    uint32_t abi;  /* 1 */
    void *ctx;
    int32_t (*call)(void *ctx, const char *name, size_t name_len,
                    const char *args, size_t args_len, openomsi_out out, void *user);
    void (*log)(void *ctx, int32_t level, const char *text, size_t len);
} openomsi_host;

uint32_t openomsi_native_abi(void);                         /* return 1 */
int32_t  openomsi_native_start(const openomsi_host *host);  /* 0: started */
void     openomsi_native_callback(const openomsi_host *host, uint64_t id,
                                  const char *args, size_t len);  /* optional */
void     openomsi_native_stop(const openomsi_host *host);   /* optional */
  • call runs the API function name ("var", "ui.set", "http.fetch") with a JSON array of arguments. It returns 0 and gives the result's JSON to out, or 1 and the error's text. The text is only valid during out.
  • Call it only from the thread the game calls the plugin on, during start, callback or stop. Other threads hand their results over to it, for example through a queue read in a frame handler.
  • A function argument is {"$cb": id} with an id the plugin chooses. The game calls it back through openomsi_native_callback with the arguments as a JSON array. Bytes are {"$b64": "..."}.
  • A callback that comes while the plugin is in a call of its own runs right after that call returns.
  • A crash in a native plugin is a crash of the game. Catch panics at the boundary, as the SDK does.

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open