Scene scripts¶
The disc's 14 mission modules (HIDEOUT, M00 to M13, no M07) hold the level logic: 693 named scripts, 27,894 functions, 2,115,685 EE instructions. Their source is checked in as 450 C files — 416 per-level files under src/scripts/<LEVEL>/ and 34 shared Alllevels_*.c scripts under src/scripts/Alllevels/, mapped to levels by src/scripts/shared-scripts.json and configured per level by src/scripts/<LEVEL>/shared-scripts.h — compiled into 707 objects that rebuild the modules byte for byte with ee-gcc 3.2 040921: 8,462,740 code bytes, 1,321,096 data bytes and 25,245 metadata records.
make match-scripts # verify all 14 modules and write the rebuilt images
python3 tools/port/match_scripts.py match M00 --data --image # one module
python3 tools/port/match_scripts.py match M00 M00_Entrancebirdsgroundbox # one script
python3 tools/port/match_scripts.py match M00 SCRIPT --list # differing instructions per function
python3 tools/port/match_scripts.py match M00 SCRIPT --diff FUNCTION # one function side by side
Outputs (objects/, match.json, data-match.json, image-match.json, <LEVEL>.rebuilt.PS2) go under build/scripts/. Verification needs Docker for ee-gcc and the Python venv of make helpers for rabbitizer.
The port compiles the same files for the host and runs them as native code; native-scripts.md describes that build and how a run is checked. script-sdk.md describes the generated headers, script-thunks.md the engine interfaces, and ../formats/script-modules.md the module format.
What a script file contains¶
Each named script is one C file built against openblood/decompiled.h (with -Isrc/scripts/<LEVEL> before -Isrc/scripts, so shared files in src/scripts/Alllevels/ pick up each level's shared-scripts.h and local headers). module-support.c holds the level's script table, the shared runtime, the saved variables, and functions and controllers that belong to no single script.
- Tables: the script's
SCRIPTCREATOR, states, function controllers, functions table (M00_Chemist_functions), virtual states (M00_Chemist_virtual_states), properties and saved statics, as initializers with the original field values. States are named for what the actor does in them (M00_Chemist_KnockOnDoorState), and a state's functions, controllers, handlers and structs carry its name (M00_Chemist_KnockOnDoorState_run,..._run_fc,..._on_m00_enterroom). A state with no name yet keeps its number in tree order (M00_Entrancebirdsgroundbox_state1). - Functions: every function the script owns. Most functions are named for what they do (
Alllevels_Basefunc_PlayAnimationSegmentAndWait,M00_Chemist_KnockOnDoor); a derived script's override of a virtual slot takes the slot's name with its own script as prefix. A function with no name yet is named after the base-most script whose table holds it and its slot (Alllevels_Useable_Useable_Object_F5), or after its controller's offset (function_0x5ca0). Shared scripts live once insrc/scripts/Alllevels/and compile into every level that lists them insrc/scripts/shared-scripts.json; every shared function has one name across all levels, andsrc/scripts/<LEVEL>/shared-scripts.hdefines only helper-ownership flags saying which translation unit defines a helper in that level. A message handler is named after the state or script whose switch calls it and the message (M00_Chemist_WaitForEnterRoomCountdownState_on_actordamage,..._on_otherfor the call after the switch). Engine calls go through the typedScriptImportstable and the service tableSF; a literal message ID has its SDK name where one is known (MSG_m10_activateexit). The runtime appears as__ob_rt_frame,__ob_rt_calland so on. - Variables: each script's variable block is a struct,
M00_Chemist_Variables. A derived script's block begins with its base's, so each struct is written as a field-list macro (ALLLEVELS_CIVILIAN_VARIABLES) that starts with the base's macro: ee-gcc 3.2 has no anonymous members of a named struct type, and the macro keeps inherited fields one->away. The shared scripts' structs are insrc/scripts/Alllevels/<Name>.h, a level's base scripts' insrc/scripts/<LEVEL>/<Script>.h, and every other script's in its own file. A word of one-bit flags is a union of the word and its named bits, declared asu8bitfields like the decompiler's bit view (m2c_bits32), because a struct ofu8fields may alias any access. A bit with no name yet, or one a frame reads through a byte, keeps the view and names the word it lives in (((m2c_bits32 *)&vars->actorFlags)->b9). A field nobody has named yet isunknown<offset>, and bytes no code reads areu8 unknown<offset>[n]. - Frames: a function's arguments and locals are a
<Function>_Framestruct after the 0x14-byteLocalVarEntryheader. A virtual slot's arguments are a field-list macro of the function that introduces the slot (ALLLEVELS_BASEFUNC_PLAYANIMATIONSEGMENTANDWAIT_ARGUMENTS), which every override's frame begins with. A function reads its own frame throughvframe, aconst volatileview, and writes it throughframe, because the original reads frame variables as volatile and stores them plainly. A caller fills a callee's arguments through its next-frame pointer typed as the callee's frame, and keeps the original's volatile stores by casting that pointer to avolatileframe (((volatile Alllevels_Human_AimWeaponAtObject_Frame *)pFrame->m_pNextVariables)->target = ...). A state change stores its target through the SDK'sScriptStateChangeArguments. The frames ofmodule-support.cfunctions that other scripts call are insrc/scripts/<LEVEL>/module-support.h, and a module-support function called only from scripts that share a base takes that base's variables type. A state found through the creator's virtual state table indexes it by slot ((s32)virtualStates[16]). - State variables and payloads: each state's view of the state variable block is
<State>_Variables(M00_M00_Levelcontrol_MissionBriefingState_Variables), defined in the script's file. A message handler reads its payload throughpayload, typed asMSG_<message>_Payloadfromsrc/scripts/payloads.h. A saved global is read by name, and the saved difficulty settings through the fields of the SDK'ssDifficultySettings(Alllevels_Perceptionconverter_savedDifficultySettings.faimspeed). - Other names: another script's states, controllers and functions are external. A saved static is named after the script whose save descriptor lists it (
Alllevels_Civilian_savedLockpickingWarner): the matcher's typed rules name some,SAVED_NAMESintools/port/script_saved_storage.pynames the rest by script and descriptor index, so a shared script's statics have the same names in every level, and one nobody has named yet is<script>_savedStatic<index>. Names chosen by hand are intools/config/script-names.json, by image offset per level; the matcher reads them, so a renamed function needs its entry there.
openblood/decompiled.h is generated by tools/port/script_sdk.py headers and committed (headers --check verifies it). It declares ScriptImports as a struct of typed function pointers with the EE ABI spelled out, so the C calls the engine exactly as the original code does:
- a struct result travels through a pointer passed first;
v3andsCoverarguments travel as pointers to the caller's copy;ZSC_EVENTandsGQCTasktravel as one 64-bit value.
A struct member reached through a pointer may be reordered with a load or store of a scalar global, such as a saved variable, where an access by offset may not, so the original wrote some accesses by offset. Those are written as a dereference of the field's address cast to the original access type (*(s32 *)&next->playbackMode = 5;, *(const volatile s32 *)&frame->result): GCC 3.2 does not fold that back into a member access, so it is the same access as the offset form, and the field keeps its name. A byte or half of a flag word is read through a view of the named word (((volatile u8 *)&next->flags14)[1]), the same address arithmetic. Accesses whose struct or field is not known yet stay by offset (M2C_FIELD(pFrame, s32 *, 0x14)).
What the verifier checks¶
match compiles the files and compares each function's instruction stream with the original. Where the instructions agree outside their relocated fields, tools/port/script_relocations.py checks the relocation kinds and paired addends, the named call and data targets, literal bytes and each destination of a switch table. A wrong or unresolved operand counts as a difference. The exit status requires every script creator on the disc, every function in its compiled object, and an exact C implementation of each.
--data compares the compiled metadata with the module: record lengths, scalar bytes and 32-bit pointer relocations of the script registry and creator names, creator and state records, function controllers, virtual tables, property lists, saved-static descriptors and string fixup lists.
--image places compiled code and data at their original image offsets, re-encodes the relocation targets against the original symbol indices and compares the raw bytes. The header, symbol, import and relocation records are serialized from the reference layout. <LEVEL>.rebuilt.PS2 is written only when every image byte is placed from compiler output, all function and metadata checks pass, and the complete file equals the reference.
The shape of the original source¶
The modules were built with -Os -G0 -mno-split-addresses and type-based aliasing on. They are the output of IOI's script translator, and the matching functions show what its C looked like:
- Resumable functions are one switch on the resume index,
switch ((u16)(pFrame->m_lFunctionIndex & 0x7FFF))(the root's run function switches on the field itself), with acaseat every resume point, inside loops too. Case 0 falls into case 1. Where the code goes back to the switch (a state's run function afterSF.CheckTimeout()), it isgoto resume;to a label before the switch. - Message functions are a switch on the message. Each case calls its handler with
(pSS, msg, pData)as a tail call; the default handler is called, then a dead store keeps the call from being a tail call, as the original's is not. - Variables: the script's and the state's variable pointers are read once before the switch. Frame variables, the function's own and the next frame's, are read from memory at every use, as through a
volatilepointer, but stored as ordinary memory. A call's arguments go into the next frame in the order of their offsets. One-bit fields of the variable blocks are bitfields. - Returns: each path has its own
return, where GCC merged identical ends. - Float constants are literals of the source (
0.5f), not loads from module data. A constant the source wrote as an expression that ee-gcc folds by truncation goes throughSC_FOLDED(source, value). - Early exits: where one arm of a test returns and the other goes on to a resume point, the test is an early exit (
if (__ob_rt_check_timeout(pSS, 2)) return 0.0f;). - Parent handlers: a message no case takes is handed to the parent scripts' handlers by
while (pSS->m_pMessageHandler != NULL) { ... }. - Types the instructions do not show. A word is loaded and stored by the same instructions whatever its type, but ee-gcc orders the loads and stores of a block by what may alias what: a store to an enumeration, a pointer or a float does not hold back the load of an int, a store to a struct member does not hold back the load of a global, and two volatile reads keep their order. So the order of the original's loads and stores says what the variables are.
Traces and deterministic runs¶
OPENBLOOD_DETERMINISTIC=1runs the game on a virtual clock: each frame is exactly one frame long, each read of the cycle counter on the main thread moves it on by a fixed step, movies play on the virtual clock, and there is no frame pacing and no sound. Two runs with the same input are then alike.OPENBLOOD_FRAMES=Nquits after N frames, andOPENBLOOD_INPUT_SCRIPT=FILEplays scripted pad input.OPENBLOOD_TRACE_SCRIPT_CALLS=1logs each engine interface call with the running object, its arguments and the script, and each script service that steers what happens next (script sf <object> <service> <values>): timeout checks with their result, messages with their target and a hash of their payload, allocations and priorities.OPENBLOOD_TRACE_SCRIPT_RUNS=1logs every script function the engine runs with the float it returns.OPENBLOOD_DUMP_SCRIPT_VARS_AT=Ndumps the running script's variables at traced call N.