OMSI plugins (plugins/.opl + DLL)
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
*.oplunder<OMSI>\plugins, recursively (FindFilesRecursive). Tags:[dll](a path relative toplugins\),[varlist],[stringvarlist],[systemvarlist],[triggers]- each a count, then that many names. -
Loading:
LoadLibrary, thenGetProcAddressforPluginStartandPluginFinalize(required - "Could not load plugin …: procedure … not found!") andAccessVariable,AccessTrigger,AccessSystemVariable,AccessStringVariable(optional - "Loading plugin …: procedure … not found!"). ThenPluginStart(AOwner).PluginFinalizeruns when the game ends. -
Every frame, plugin by plugin:
- each listed system variable:
AccessSystemVariable(index: Word; var value: Single; var write: Boolean); written back whenwriteis true; - with a player vehicle: each listed vehicle variable (
AccessVariable, same shape); - 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 whenwriteis true; - each trigger:
AccessTrigger(index: Word; var active: Boolean),activefalse before the call. A change from the last frame is a key event: down fires the trigger, up fires<trigger>_off.
All
stdcall;indexis the position in the plugin's own list. Names the vehicle does not have are skipped. - each listed system variable:
openOMSI#
omsi_plugin::Plugins::loadreads thepluginsfolder of every content root (the first root's copy of an.oplwins) 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-hostbuilt 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 (wineon thePATH, orOMSI_WINE). The host is found next to the game (OMSI_PLUGIN_HOST32overrides).
- 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>ofomsi.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.oplserves 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
0or1; a text requested as a number (or the reverse), an unknown key, or a number not representable as a finitef32is unavailable.destinationis the selected HOF terminus's texture identifier (empty for an all-exit terminus);passengersis zero when no passenger simulation is active.versionis the game's displayed version, rather than the Cargo package version.OMSI_NO_PLUGINS=1leaves every plugin out. A plugin whose host stops answering is left out for the rest of the session.PluginStartgets 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.
[scripttexturelist]
2
0
3lists 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:
procedure AccessScriptTexture(index: Word; width, height: Integer; pixels: Pointer;
var write: Boolean); stdcall;indexis the entry's position in the list (as for the otherAccess*procedures), not the texture's number.pixelsiswidthxheightx 4 bytes: RGBA, top row first, rowswidth * 4bytes apart, straight (not premultiplied) alpha. The size is the texture's own (its[scripttexture]entry, or whatSTLoadTexmade 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
writeto show the buffer: the texture shows it from the next picture on. Leave it false while nothing changed: a written picture is copied, and throughomsi-plugin-host32.exeit 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#
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:
kind0: 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).indexis the entry's position in the list, as forAccessScriptTexture.
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)#
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.peopleknow it);agein 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;ticketthe ticket pack's (from 1; 0 none fits their age, and the desk then asks for the pack's first, as in OMSI),pricethe price asked. A buyer keeps a ticket of the pack: 0 is not taken for them.nameis a buffer of 256 wide characters (the terminating zero included) holding the ticket's name;voiceone as long, empty: a line of the passenger's voice folder said at the desk instead ofTicket_<n>_<k>(Zone_4001forZone_4001.wav).- Set
writeto 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#
scripts/build-plugin-host.shneeds 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.