openOMSI GitHub Discord
Plugin API reference

Docs / Plugins / Plugin API reference

Full access

6 min readEdit this pagedocs/PLUGIN_API.md

On this page
  1. omsi.fs.copy
  2. omsi.fs.exists
  3. omsi.fs.list
  4. omsi.fs.mkdir
  5. omsi.fs.paths
  6. omsi.fs.read
  7. omsi.fs.remove
  8. omsi.fs.rename
  9. omsi.fs.stat
  10. omsi.fs.write
  11. omsi.net.close
  12. omsi.net.info
  13. omsi.net.listen
  14. omsi.net.lookup
  15. omsi.net.poll
  16. omsi.net.send
  17. omsi.net.tcp
  18. omsi.net.udp
  19. omsi.net.websocket
  20. omsi.http.fetch
  21. omsi.http.poll
  22. omsi.http.request
  23. omsi.system.env
  24. omsi.system.exec
  25. omsi.system.getenv
  26. omsi.system.info
  27. omsi.system.open
  28. omsi.system.setenv
  29. omsi.system.sleep
  30. omsi.system.spawn
  31. omsi.system.time

omsi.fs.*, omsi.net.*, omsi.http.* and omsi.system.* reach the whole computer: any file, any network address, any program. They need the native permission: the plugin asks for it, and only the player gives it, in the game's own window or on the launcher's Plugins page, for one build of the plugin's code. See Full access.

A plugin with full access also gets the whole Lua standard library (io, os, debug, C modules through require and package.loadlib) and has no time or memory limit.

Calls that wait for the network or a program return a handle at once. What happens comes to the function given (on_event, on_done) in the plugin's next frames, or waits for net.poll and http.poll. http.fetch and system.exec wait for their answer, and the game waits with them.

omsi.fs.copy#

omsi.fs.copy(from, to) → true, or false and the reason

Copies a file, or a folder with everything in it.

Parameters: from string; to string.

Since unreleased. Permission: native.

omsi.fs.exists#

omsi.fs.exists(path) → boolean

Whether anything is at path.

Parameters: path string.

Since unreleased. Permission: native.

omsi.fs.list#

omsi.fs.list(path) → a list of {name, path, dir, size, modified}, or nil and the reason

The entries of any folder, sorted by name.

Parameters: path string.

Since unreleased. Permission: native.

omsi.fs.mkdir#

omsi.fs.mkdir(path) → true, or false and the reason

Makes a folder and the folders above it.

Parameters: path string.

Since unreleased. Permission: native.

omsi.fs.paths#

omsi.fs.paths() → {plugin, data, game, home, temp, exe}

Where things are: the plugin's own folder, its data folder, the game's working folder, the home folder, the temporary folder and the game's program.

Since unreleased. Permission: native.

omsi.fs.read#

omsi.fs.read(path) → the file's content, or nil and the reason

Reads any file of the computer. A relative path is from the plugin's own folder, ~/ the home folder. Binary files come back whole (a Lua string; {"$b64": ...} in JSON).

Parameters: path string.

Since unreleased. Permission: native.

Lua
local text, why = omsi.fs.read("~/Documents/timetable.csv")

omsi.fs.remove#

omsi.fs.remove(path, [recursive]) → true, or false and the reason

Deletes a file or an empty folder; with recursive, a folder and everything in it.

Parameters: path string; recursive bool, optional.

Since unreleased. Permission: native.

omsi.fs.rename#

omsi.fs.rename(from, to) → true, or false and the reason

Moves or renames a file or folder.

Parameters: from string; to string.

Since unreleased. Permission: native.

omsi.fs.stat#

omsi.fs.stat(path) → {dir, file, size, modified, created, readonly, link}, or nil and the reason

What is at path (times in seconds since 1970).

Parameters: path string.

Since unreleased. Permission: native.

omsi.fs.write#

omsi.fs.write(path, data, [append]) → true, or false and the reason

Writes (or with append adds to) any file of the computer; its folders are made.

Parameters: path string; data string; append bool, optional.

Since unreleased. Permission: native.

omsi.net.close#

omsi.net.close(handle) → boolean

Closes a connection, server or socket.

Parameters: handle integer.

Since unreleased. Permission: native.

omsi.net.info#

omsi.net.info(handle) → {kind, state, local_host, local_port, peer_host, peer_port} or nil

What a connection is (tcp, listener, udp, websocket) and does (connecting, open, listening, closed, failed), and its addresses.

Parameters: handle integer.

Since unreleased. Permission: native.

omsi.net.listen#

omsi.net.listen(port, [on_event], [host]) → a handle, or nil and the reason

A TCP server on port (0: any free one, see net.info), on every address unless host names one. Each connection that comes is an accept event with its own handle, which then has its own data and closed events.

Parameters: port integer; on_event function, optional; host string, optional.

Since unreleased. Permission: native.

Lua
omsi.net.listen(8080, function(e)
  if e.type == "accept" then
    omsi.net.send(e.handle, "HTTP/1.0 200 OK\r\n\r\n" .. omsi.json.encode({ speed = omsi.speed() }))
    omsi.net.close(e.handle)
  end
end)

omsi.net.lookup#

omsi.net.lookup(host) → a list of addresses, or nil and the reason

The IP addresses of a host name (this waits for the answer).

Parameters: host string.

Since unreleased. Permission: native.

omsi.net.poll#

omsi.net.poll(handle) → a list of events

Takes the events of a connection that came since the last call, oldest first: {type = "open"}, {type = "data", data, host, port}, {type = "accept", handle, host, port} (the new connection's handle), {type = "closed", reason}. A connection with an on_event function gets them there instead, with handle (and, for accept, listener) naming where they came from.

Parameters: handle integer.

Since unreleased. Permission: native.

omsi.net.send#

omsi.net.send(handle, data, [host], [port]) → true, or false and the reason

Sends data on a connection; a UDP socket sends one datagram to host:port.

Parameters: handle integer; data string; host string, optional; port integer, optional.

Since unreleased. Permission: native.

omsi.net.tcp#

omsi.net.tcp(host, port, [on_event], [timeout]) → a handle

Opens a TCP connection to any host. It connects on its own thread: events (open, data, closed) come to on_event(event) in the next frames, or wait for net.poll. Send with net.send.

Parameters: host string; port integer; on_event function, optional; timeout number, optional.

Since unreleased. Permission: native.

Lua
local h = omsi.net.tcp("127.0.0.1", 5000, function(e)
  if e.type == "open" then omsi.net.send(e.handle, "hello\n") end
  if e.type == "data" then omsi.log("got " .. e.data) end
end)

omsi.net.udp#

omsi.net.udp([port], [on_event], [host]) → a handle, or nil and the reason

A UDP socket bound to port (default any free one), able to send to and hear from any address, broadcast too. Each datagram is a data event with the sender's host and port.

Parameters: port integer, optional; on_event function, optional; host string, optional.

Since unreleased. Permission: native.

omsi.net.websocket#

omsi.net.websocket(url, [on_event], [headers]) → a handle

A two-way WebSocket connection to any ws:// or wss:// address, with extra request headers if given. Text messages are data events with a text, binary ones with bytes; net.send sends a text (or bytes, which go as a binary message).

Parameters: url string; on_event function, optional; headers table, optional.

Since unreleased. Permission: native.

omsi.http.fetch#

omsi.http.fetch(request) → {ok, status, url, headers, body} or {ok = false, error}

As http.request, but waits for the answer (the game waits too: keep it for loading, or short requests).

Parameters: request any.

Since unreleased. Permission: native.

omsi.http.poll#

omsi.http.poll(handle) → the answer, or nil while it has not come

The answer of an http.request or system.spawn without a function. Once given, the handle is forgotten.

Parameters: handle integer.

Since unreleased. Permission: native.

omsi.http.request#

omsi.http.request(request, [on_done]) → a handle

An HTTP(S) request on its own thread: request is a URL or {url, method, headers, body, timeout}. The answer {ok, status, url, headers, body} (or {ok = false, error}) comes to on_done(answer) in a later frame, or from http.poll(handle).

Parameters: request any; on_done function, optional.

Since unreleased. Permission: native.

Lua
omsi.http.request({ url = "https://api.example.org/delays", method = "POST",
  headers = { ["Content-Type"] = "application/json" },
  body = omsi.json.encode({ delay = omsi.duty.delay() }) },
  function(r) omsi.log(r.status, r.body) end)

omsi.system.env#

omsi.system.env() → table

All environment variables of the game.

Since unreleased. Permission: native.

omsi.system.exec#

omsi.system.exec(command, [options]) → {ok, code, stdout, stderr} or {ok = false, error}

Runs a command and waits for it: through the shell (sh -c, cmd /C), or with options.args the program itself with those arguments. Options: args, shell, cwd, env (a table of variables), input (its standard input).

Parameters: command string; options table, optional.

Since unreleased. Permission: native.

Lua
local r = omsi.system.exec("git", { args = { "-C", omsi.fs.paths().plugin, "pull" } })
omsi.log(r.code, r.stdout)

omsi.system.getenv#

omsi.system.getenv(name) → string or nil

An environment variable of the game.

Parameters: name string.

Since unreleased. Permission: native.

omsi.system.info#

omsi.system.info() → {os, arch, family, pid, cpus, hostname}

The system the game runs on.

Since unreleased. Permission: native.

omsi.system.open#

omsi.system.open(target) → true, or false and the reason

Opens a file, folder or web address with the system's own program for it (the browser, the file manager).

Parameters: target string.

Since unreleased. Permission: native.

omsi.system.setenv#

omsi.system.setenv(name, [value]) → true

Sets an environment variable of the game (nil removes it); programs it starts afterwards inherit it.

Parameters: name string; value string, optional.

Since unreleased. Permission: native.

omsi.system.sleep#

omsi.system.sleep(seconds) → nil

Waits, and the game with it (at most 10 s). For a helper thread's pace, not the frame's.

Parameters: seconds number.

Since unreleased. Permission: native.

omsi.system.spawn#

omsi.system.spawn(command, [options], [on_done]) → a handle

As system.exec, on its own thread: the result comes to on_done(result) in a later frame, or from http.poll(handle). With options.detach the program is only started and left to run; the result is then {ok, pid} at once.

Parameters: command string; options table, optional; on_done function, optional.

Since unreleased. Permission: native.

omsi.system.time#

omsi.system.time() → number

The wall clock: seconds since 1970, to the microsecond (the game's util.now is the game's time).

Since unreleased. Permission: native.

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

↑↓ to move↵ to open