openOMSI GitHub Discord

Docs / Plugins

OMSI plugins (plugins/.opl + DLL)

6 min readEdit this pagedocs/PLUGINS.md

On this page
  1. The original
  2. openOMSI
  3. Script textures (openOMSI only)
  4. The mouse on those textures
  5. Passengers' tickets (openOMSI only)
  6. Building the host
  7. Tests

What OMSI does with plugins, and how openOMSI does the same (crates/omsi-plugin, driven from crates/omsi-app/src/plugins.rs).

The original#

  • Finding them: every *.opl under <OMSI>\plugins, recursively (FindFilesRecursive). Tags: [dll] (a path relative to plugins\), [varlist], [stringvarlist], [systemvarlist], [triggers] - each a count, then that many names.

  • Loading: LoadLibrary, then GetProcAddress for PluginStart and PluginFinalize (required - "Could not load plugin …: procedure … not found!") and AccessVariable, AccessTrigger, AccessSystemVariable, AccessStringVariable (optional - "Loading plugin …: procedure … not found!"). Then PluginStart(AOwner). PluginFinalize runs when the game ends.

  • Every frame, plugin by plugin:

    1. each listed system variable: AccessSystemVariable(index: Word; var value: Single; var write: Boolean); written back when write is true;
    2. with a player vehicle: each listed vehicle variable (AccessVariable, same shape);
    3. each string variable: AccessStringVariable(index: Word; text: PWideChar; var write: Boolean) - a buffer of length + 1 wide characters with the text and its terminating zero; read back when write is true;
    4. each trigger: AccessTrigger(index: Word; var active: Boolean), active false before the call. A change from the last frame is a key event: down fires the trigger, up fires <trigger>_off.

    All stdcall; index is the position in the plugin's own list. Names the vehicle does not have are skipped.

openOMSI#

  • omsi_plugin::Plugins::load reads the plugins folder of every content root (the first root's copy of an .opl wins) and loads each library:
    • in-process when the running program can load it (same system and architecture - a plugin built for openOMSI, or a 32-bit DLL in a 32-bit Windows build);
    • otherwise in omsi-plugin-host32.exe, omsi-plugin-host built for 32-bit Windows, which loads the DLL and answers over stdin/stdout (one round trip per frame). On Windows it runs directly; on macOS and Linux through Wine (wine on the PATH, or OMSI_WINE). The host is found next to the game (OMSI_PLUGIN_HOST32 overrides).
  • The system variables are the scripts' (omsi_script::SysVar); a plugin's writes to them are not applied (the clock, weather and input stay the game's).
  • openomsi_<key> in a [varlist] or [stringvarlist] reads the value <key> of omsi.info() (see above): numbers and booleans as variables, texts as string variables, while the player drives a bus.

    A plugin that reads the bus's place, its type or the map out of Omsi.exe's memory at fixed addresses - which cannot work here - lists openomsi_tile_x, openomsi_tile_pos_x, openomsi_heading, openomsi_map_path... instead. OMSI has no variables of these names and skips them, so one .opl serves both games.

    Names are matched case-insensitively. Vehicle script variables take precedence over this fallback. Game values are read-only: writing them does not change the game. Boolean values are 0 or 1; a text requested as a number (or the reverse), an unknown key, or a number not representable as a finite f32 is unavailable.

    destination is the selected HOF terminus's texture identifier (empty for an all-exit terminus); passengers is zero when no passenger simulation is active. version is the game's displayed version, rather than the Cargo package version.

  • OMSI_NO_PLUGINS=1 leaves every plugin out. A plugin whose host stops answering is left out for the rest of the session.
  • PluginStart gets a nil owner: there is no Delphi application object. Plugins that open their own windows do so without a parent.

Script textures (openOMSI only)#

OMSI 2 gives plugins no way into the vehicle's [scripttexture]s; tools such as OmsiHook reach them in Omsi.exe's memory, which does not exist here. openOMSI adds one optional procedure and one .opl tag for it. OMSI skips both, so one DLL and .opl serve both games.

OMSI file
[scripttexturelist]
2
0
3

lists the player's vehicle's script textures the plugin draws, by number (0-based, as [useScriptTexture] and \S:n count them). Every frame, after the triggers, the game calls for each entry the vehicle has:

Pascal
procedure AccessScriptTexture(index: Word; width, height: Integer; pixels: Pointer;
  var write: Boolean); stdcall;
  • index is the entry's position in the list (as for the other Access* procedures), not the texture's number.
  • pixels is width x height x 4 bytes: RGBA, top row first, rows width * 4 bytes apart, straight (not premultiplied) alpha. The size is the texture's own (its [scripttexture] entry, or what STLoadTex made it).
  • The buffer belongs to that entry and keeps what the plugin drew last, so a plugin may redraw only what changed; it starts zeroed and is zeroed again when the texture's size changes. It is valid only during the call.
  • Set write to show the buffer: the texture shows it from the next picture on. Leave it false while nothing changed: a written picture is copied, and through omsi-plugin-host32.exe it travels over a pipe (a 512 x 256 texture is 512 KiB).
  • A script drawing into the same texture (STLock ... STUnlock) and the plugin take turns: the last to let go is what shows. Give the plugin textures the scripts leave alone.
  • No player vehicle, or a number the vehicle has no texture for: the entry is skipped.

The mouse on those textures#

Pascal
procedure AccessScriptTexturePointer(index: Word; kind: Integer; x, y: Single); stdcall;

A library that also exports this hears the mouse on its [scripttexturelist] textures. The game finds where a click lands on the bus's meshes the way it does for [htmltexture] pages (the nearest mesh slot whose material shows the texture with [useScriptTexture]) and calls it in the next frame, before AccessScriptTexture, so the picture drawn then can already answer:

  • kind 0: the button went down on the texture; 1: the cursor moved while it is held (at most one a frame, the latest); 2: it came up (off the texture: where it was last on it).
  • x, y: the place in the texture's pixels, from its top left (0..width, 0..height, fractional).
  • index is the entry's position in the list, as for AccessScriptTexture.

A press on such a texture is the plugin's: a [mouseevent] mesh under it does not fire, as for a page. Events are not kept while the game is paused.

A 64-bit DLL loads in-process in the 64-bit game, without the pipe; a 32-bit one runs in the host, which keeps the buffers on its side. The textures go over the pipe only for a library that exports AccessScriptTexture; an older omsi-plugin-host32.exe without this procedure still serves every other plugin.

Passengers' tickets (openOMSI only)#

Pascal
procedure AccessTicketRequest(person: Cardinal; age: Single;
  stop, destination: PWideChar; stopNumber, destinationNumber: Integer;
  var kind, ticket: Integer; var price: Single; name, voice: PWideChar;
  var write: Boolean); stdcall;

A library that exports this hears every passenger boarding the player's bus once, in the frame after they decided about their ticket (the Lua plugins' passenger_ticket), and may change what they ask for at the cash desk - they wait in the doorway for that frame:

  • person: the passenger's id (as the Lua plugins' omsi.people know it); age in years.
  • stop, destination: the timetable's names of the stop they board at and the one they ride to ("" when not known); stopNumber, destinationNumber: where those are in the duty's trip, from 1 (0: not in it).
  • kind: 0 nothing to do (a pass), 1 validates a ticket, 2 buys one at the cash desk; ticket the ticket pack's (from 1; 0 none fits their age, and the desk then asks for the pack's first, as in OMSI), price the price asked. A buyer keeps a ticket of the pack: 0 is not taken for them.
  • name is a buffer of 256 wide characters (the terminating zero included) holding the ticket's name; voice one as long, empty: a line of the passenger's voice folder said at the desk instead of Ticket_<n>_<k> (Zone_4001 for Zone_4001.wav).
  • Set write to take what the plugin changed of them: only what differs is applied, the kind only while the passenger is not in the bus yet. The plugin's name (the DLL's) is kept as who changed it.

omsi-plugin-host32.exe passes the call through; an older host without it does not, and the plugin then does not hear of the passengers.

Building the host#

shell
scripts/build-plugin-host.sh

needs rustup target add i686-pc-windows-gnu and MinGW (brew install mingw-w64, or apt install mingw-w64); copy dist/omsi-plugin-host32.exe next to the game. The 32-bit build uses panic=abort and a stand-in _Unwind_Resume (Homebrew's i686 MinGW links no unwinder the prebuilt standard library can use).

Tests#

cargo test -p omsi-plugin builds crates/omsi-plugin/demo (a plugin with the OMSI interface) and drives it in-process and through the host.

With OMSI_TEST_WINE_DIR=target/i686-pc-windows-gnu/release (after building the host and the demo for i686-pc-windows-gnu) the test windows_dll_under_wine runs the real chain: a 32-bit Windows DLL with Delphi-style undecorated stdcall exports, in the 32-bit host, under Wine.

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

↑↓ to move↵ to open