openOMSI GitHub Discord
Writing plugins

Docs / Plugins / Writing plugins

OMSI 2 DLL plugins

6 min readEdit this pagedocs/PLUGINS.md

On this page
  1. The .opl file
  2. The DLL interface
  3. Differences in openOMSI
  4. Game values as variables
  5. Script textures
  6. The mouse on script textures
  7. Passengers' tickets in DLL plugins
  8. Building the 32-bit helper

openOMSI loads the original game's plugins: every *.opl under the plugins folder of each content root, searched recursively. The first content root's copy of an .opl wins.

The .opl file#

Tag Contents
[dll] the DLL, relative to plugins
[varlist] a count, then that many vehicle variable names
[stringvarlist] a count, then string variable names
[systemvarlist] a count, then system variable names
[triggers] a count, then trigger names
[scripttexturelist] openOMSI only: a count, then script texture numbers

The DLL interface#

The game loads the DLL and looks up its procedures, all stdcall:

Procedure Required
PluginStart(AOwner) yes
PluginFinalize yes, runs when the game ends
AccessVariable, AccessStringVariable, AccessSystemVariable, AccessTrigger no
AccessScriptTexture, AccessScriptTexturePointer, AccessTicketRequest no, openOMSI only

A missing required procedure gives "Could not load plugin …: procedure … not found!". A missing optional one gives "Loading plugin …: procedure … not found!".

Every frame, plugin by plugin:

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

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

Differences in openOMSI#

  • A DLL the game can load directly (same system and architecture, for example a 64-bit DLL in the 64-bit game) runs in-process.
  • Otherwise it runs in omsi-plugin-host32.exe, a 32-bit Windows helper that loads the DLL and talks to the game once per frame. On Windows it runs directly; on macOS and Linux through Wine (wine on the PATH, or the OMSI_WINE variable). The game looks for the helper next to itself; OMSI_PLUGIN_HOST32 overrides the path.
  • A plugin's writes to system variables are not applied: the clock, weather and input stay the game's.
  • PluginStart gets a nil owner, since there is no Delphi application object. Plugins that open windows do so without a parent.
  • A plugin whose helper stops answering is left out for the rest of the session.
  • The launcher's Disable plugins setting, or OMSI_NO_PLUGINS=1, leaves out every plugin.

Game values as variables#

Many OMSI 2 plugins read the bus's position, its type or the map from fixed memory addresses of Omsi.exe, which cannot work in openOMSI. Instead, list openomsi_<key> in [varlist] or [stringvarlist], where <key> is any key of omsi.info(): openomsi_tile_x, openomsi_tile_pos_x, openomsi_heading, openomsi_map_path, and so on. OMSI 2 has no variables by these names and skips them, so one .opl serves both games.

  • Values are available while the player drives a bus. Numbers and booleans come as variables, texts as string variables.
  • Names are matched without regard to case. A real vehicle script variable of the same name takes precedence.
  • The values are read-only.
  • Booleans are 0 or 1. A text requested as a number (or the reverse), an unknown key, or a number that is not a finite 32-bit float is unavailable.
  • destination is the selected HOF terminus's texture identifier (empty for an all-exit terminus). passengers is zero when no passenger simulation runs. version is the game's displayed version.

Script textures#

OMSI 2 gives plugins no way into the vehicle's [scripttexture]s; tools such as OmsiHook reach them in Omsi.exe's memory. openOMSI adds a tag and a procedure for it. OMSI 2 skips both.

[scripttexturelist]
2
0
3

This lists the player 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 this 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, not the texture's number.
  • pixels is width × height × 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 the entry and keeps what the plugin drew last, so it can 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 from the next picture on. Leave it false when nothing changed: a written picture is copied, and through the 32-bit helper it travels over a pipe (a 512 × 256 texture is 512 KiB).
  • If a script also draws into the texture (STLock … STUnlock), the last to finish is shown. Give the plugin textures the scripts leave alone.
  • Without a player vehicle, or for a number the vehicle has no texture for, the entry is skipped.

A 64-bit DLL loads in-process in the 64-bit game, without the pipe. Textures go over the pipe only for a library that exports AccessScriptTexture. An older omsi-plugin-host32.exe without this still serves every other plugin.

The mouse on script textures#

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

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

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

A press on such a texture belongs to the plugin: a [mouseevent] mesh under it does not fire. Events are not kept while the game is paused.

Passengers' tickets in DLL plugins#

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 passenger_ticket event). It may change what they ask for; they wait in the doorway for that frame.

  • person: the passenger's id (as omsi.people knows it); age in years.
  • stop, destination: the timetable names of the boarding stop and the stop they ride to ("" when unknown). 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. ticket: the pack's ticket from 1 (0: none fits their age, and the desk 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: a buffer of 256 wide characters (the terminating zero included) with 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 apply the changes. Only what differs is applied, the kind only while the passenger is not yet in the bus. The DLL's name is kept as who changed it.

An older omsi-plugin-host32.exe without this procedure does not pass it on, and the plugin then does not hear of passengers.

Building the 32-bit helper#

shell
scripts/build-plugin-host.sh

It 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. See Building from source for building openOMSI itself.

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

↑↓ to move↵ to open