Script interface thunks¶
Scene scripts call the engine through ScriptImports, a table of 716 function pointers (struct ScriptImportsTable in decompiled.h). The port fills each level library's table when it loads the level (ScriptLevelAttach). Entry n is ScriptAdapter_<Name> in port/engine/ScriptInterfaces/_sdl/scriptthunkssdl.cpp:
- The adapter has the script-side C signature:
REFis auint32, pointers are host pointers,ZSC_EVENTandsGQCTaskcome by value. - It fills a
ScriptCallrecord (port/engine/zstdlib/_sdl/scriptcallsdl.h): 16 integer slots, 16 pointer slots, 8 float slots and the result. - It calls
ScriptDispatchwith the interface'sScriptInterfaceentry. That counts the call, writes thescript calltrace line whenOPENBLOOD_TRACE_SCRIPT_CALLSis set, and offers the call to the Lua hook (g_pScriptCallHook), which may answer it instead of the engine. - Otherwise
ScriptThunk_<Name>reads the slots, calls the C++ function thatScriptInterfaces[n]points at, and stores the result, which the adapter returns.
Each ScriptInterface entry also carries m_pszSignature, the interface's types as letters for the Lua mod runtime: the result (v void, w integer, enum or REF, f float, m a struct written through a hidden pointer), then one letter per script parameter (w, f, s a string, p anything passed by address or as a pair, such as a vector, a reference, an event or a task, and v for variadic arguments). obm.api calls only interfaces whose parameters are w, f and s, and obm.scripts.signature(name) returns the letters (docs/modding/README.md).
The file is generated from src/engine/ScriptInterfaces/scriptinterfaces.h:
tools/third_party/venv/bin/python tools/port/script_thunks.py
tools/third_party/venv/bin/python tools/port/script_thunks.py --check
The generator copies the header's includes and prototypes (not the table, which zscriptc.cpp defines) and stops with the function's name on any argument or return type it has no rule for. tools/tests/test_native_scripts.py checks that the generated file is current and that every adapter's parameters match the declaration in decompiled.h.
Calling convention¶
ScriptCall numbers its slots as the EE convention numbers registers, so a thunk reads argument n from the slot the original read it from. Every rule below was read from the original functions in HITMAN3.ELF.
| Rule | Evidence |
|---|---|
Integer arguments go in $a0 to $t3 ($4 to $11), single floats in $f12 to $f19, and the two are counted separately. |
Actorcommunication__Sendradiomessagetoneedeforce (0x40fd6c): channel in $a1, force in $f12, the event in $a2. Zlink__Playaniminterpolated (0x26c728): two v3 pointers in $a2/$a3, four floats in $f12 to $f15, mode in $t0. |
The callee narrows unsigned char and anim itself, so the thunk casts the slot the same way. |
andi 0xff on $t2/$t3 in Dialog__Startdialogex (0x410934), sll 16; sra 16 on $a1 in Zlink__Canplayanimsegment (0x26c658). |
Integer arguments after the eighth are 8-byte slots at the caller's $sp + 0, + 8 and so on. |
Dialog__Startdialogex has a 0x80 frame and reads bPlayActorAnimA with addiu $t6, $sp, 0x80; lbu 0($t6) and bPlayActorAnimB with lbu 0x88($sp). It is the only interface with stack arguments. |
A struct larger than 8 bytes passed by value (v3, sCover) arrives as a pointer to the caller's copy in the next integer slot. |
Math__Vdot (0x26dae8) loads both vectors through $a0 and $a1; Coverlist__Getintermediateposition takes sCover as a pointer in $a2. |
A struct of 8 bytes or less passed by value (ZSC_EVENT, sGQCTask) travels whole in one 64-bit register, first field in the low half. This is GCC's EABI rule with 8-byte words. |
Actorcommunication__Sendglobalevent (0x40fe4c): sd $a1, 0($sp), then lhu 0 for zmsg and lw 4 for pData. Guardquartercontroller__Delegatetask (0x4103bc): sd $a1, lw 0 for type, lw 4 for rtarget. |
A returned struct (v3, sCover, sDifficultySettings) goes through a hidden pointer in $a0, the other integer arguments move up one slot, and the callee returns the pointer in $v0. |
All 21 such interfaces copy into the saved $a0 and set $v0 from it, for example Coverlist__Getbestcoverfrompos (0x40f530, 0x24 bytes) and Silevelcontrol__Getdifficultysettings (0x40dbf4, 0xa4 bytes). |
Integers, enums, REF and unsigned char return in $v0; anim returns sign-extended; float returns in $f0. |
Zlink__Getanim (0x26c3fc) returns an lh result, -1 when not found. Math__Vdot ends with madd.s $f0. |
References (v3 &, int &, sCover &, REF &) are pointers in the next integer slot. A REF & slot is 4 bytes. |
Cigs__Getcig (0x412900) clears the slot with sw $zero, 0($s0). |
--check tests the generated slot assignment against the reference for all 716 interfaces. It compares the header's return type and parameter count with each debug record, decodes the original with rabbitizer, and lists the argument registers the function reads before writing them. It reports:
716 interfaces: 825 argument registers read at entry by the original, 509 never read,
639 placed by the debug record, 66 recorded after a move to a later register, 0 problems
No original reads an argument register that the record leaves unset. The 66 moved records are parameters the function copies to the next register to make room for this (Pathfollower__Ispathfollowervalid records Ref in $a1); the check counts them only when the original also reads the expected register at entry. The 509 unread slots are unused parameters, parameters passed straight through to a callee (Math__Vneg tail-jumps to vneg with $a0 untouched), or registers read by an instruction rabbitizer does not decode, such as the R5900 sqrt.s $f0, $f12 in Math__Sqrt.
Thunks checked by hand against the disassembly: Zlink__Playaniminterpolated, Zlink__Canplayanimsegment, Actorcommunication__Sendradiomessagetoneedeforce, Actorcommunication__Sendglobalevent, Guardquartercontroller__Delegatetask, Coverlist__Getbestcoverfrompos, Silevelcontrol__Getdifficultysettings, Pathfinder__Getsearchposaroundpos, Zlink__Getanim, Math__Vdot, Cigs__Getcig, Zhm3Actor__Isaimingattarget, Dialog__Startdialogex and the four varargs functions.
Struct conversions¶
v3 (0xc), sCover (0x24) and sDifficultySettings (0xa4) hold only floats, ints, one enum and two unsigned chars, so the scripts' layout equals the engine's; the generated file checks the three sizes. A by-value argument is passed to the adapter as a pointer to the script's copy, a reference points into the script's memory, and a returned struct is written through slot 0.
ZSC_EVENT (zmsg, pData) has the same fields on both sides, and ScriptEventFromCall rebuilds it from the integer and pointer slots. pData is the script's payload in the scripts' layout, so each of the 11 thunks that take an event converts it with ScriptPayloadFromScript before the call and copies it back with ScriptPayloadBackToScript after, as SF.SendCommand does (docs/internals/scripts/payload-layouts.md). All 11 deliver the event during the call. sGQCTask (type, rtarget) travels packed in one 64-bit integer slot and ScriptTaskFromCall unpacks it.
A REF & is copied into a native REF, passed to the engine, and narrowed back into the script's 4-byte slot after the call. Three of them are arrays sized by a capacity argument: Zhm3Actor__Getvisibleprojectileweapons, Scene__Getgeomsinsidebox and Scene__Getactorsinbox. Their thunks use a native array of up to 256 REFs, copy capacity entries in and out, and abort when capacity is larger. Only the first is called by the shipped modules.
Every REF going back to a script, as a return value or through a REF &, passes through ScriptNarrowRef, which aborts with the interface name if the value does not fit 32 bits.
One engine function could hand back such a value from a script's own mistake. Zlist__Getref passes the index to REFTAB::GetRefNr, which returns 0 at or past the end but reads before the first entry for a negative index: inside the block header (TabBlk), whose _Pad word is never written, or the allocation before it. M06's socializing tourists ask for entry spot - base (M06_M06_Socializingtourist.c lines 2109 and 2550), which can be negative. On the PS2 the read returns a stray word; in the port the header and the entries are eight bytes wide and the read once returned 0xffffffe0ffffffe0, which aborted M06 at frame 1 (25 November 2026, PC-data mode, one run in four). The port's Zlist__Getref (port/engine/ScriptInterfaces/_sdl/listoverridesdl.cpp, overrides) returns 0 for a negative index, as for a missing list or an index past the end; the tourist script treats 0 as no spot.
Varargs¶
Print, Error, Warning and Debugfunctions__Addpermanentscripttext take .... Their adapters format the text with vsnprintf into a buffer of SCRIPT_TEXT_SIZE (1024) bytes and pass it as one string; the thunk calls the engine function with "%s" and that string. Print, Error and Warning also log it as script: .... In the shipped binary all four discard the text: Print, Error and Warning are empty, and Addpermanentscripttext formats through IOIformat, an empty 8-byte function, then returns color.
No shipped module calls Print, Error or Warning. One calls Addpermanentscripttext (index 597): M08's Socializingtouristlistscript draws a count with "%d". Over the 14 modules, tools/port/script_explorer.py finds 34,280 ScriptImports call sites to 614 of the 716 entries (docs/internals/formats/script-modules.md).
Engine-side notes¶
- The 43 interfaces that take
const char *get a pointer into the level library's data or the script's memory, valid while the level is loaded. Where an engine object keeps the text, the code one call down copies it:ZHM3DialogControl::StartDialogintoZRTStrings,ChangeObjectiveTextandChangeObjectivePathwithstrcpy,ZOSD::SetSubtitlethroughSetText. No interface implementation stores an argument pointer in a member. - No interface returns a pointer.
v3 &,int &andsCover &results are written in place during the call. - The
SFservices are a separate table, filled byScriptInstallFunctionsinscriptfunctionssdl.cpp(docs/development/port/native-scripts.md).