Skip to content

Scene static buffer (.BUF)

Every scene archive holds one .BUF, the static buffer. ZEngineDataBase::AllocSequence loads it whole (GetStaticData, src/engine/enginedata/enginedatabase.cpp:372, called at line 1040) into m_pStaticBuffer and keeps it until FreeSceneMemory (line 1734). The engine never walks the buffer. Every use is an offset from another member, and IsInStaticBuffer (enginedatabase.h:256) tells such pointers apart from heap copies so they are not freed. A missing .BUF stops AllocSequence with ZDebugBreak (line 1027).

This file was checked against all 71 .BUF members. tools/port/scene/buf.py reads and writes them. python -I tools/port/scene/roundtrip.py --ext BUF rewrites all 71 byte for byte, using the .GMS next to each to find its blocks.

Layout

The buffer is a 16-byte magic followed by blocks, each starting on a 16-byte boundary:

Offset Size Contents
0x0 0x10 0123456789abcdef. Offset 0 means "none" in every reference, so nothing may live there.
0x10 blocks

The packer appended blocks as it compiled the scene. On the disc the order is: the data .PRP properties point at, then the weapon prim table, more property data, the excluded animation names, the geom names and extra init data interleaved in tree order, and the material database last. Block padding is zero after names, and the packer's 0xcd or 0xcc fill elsewhere. Three weapon tables are followed by stale bytes instead of fill.

Who points into the buffer

Block Referenced from Read by
geom name .GMS SCompiledGeom::lOffsetName CreateGeoms (enginedatabase.cpp:682); the name pointer stays in the buffer. Saved games store it as NameIndex (geombuffer.cpp:504).
extra init data .GMS SCompiledGeom::lExData CreateGeoms lines 701 to 711, then ZGEOM::GetChunkData (zgeom.cpp:497)
material description database .GMS m_iMaterialDescOffset ZMaterialDescriptionDB::Create / Init (materialdescriptiondb.cpp:7)
weapon prim table .GMS m_iWeaponPrimsOffset ZHM3GameData::InitWeaponHandles (hm3gamedata.cpp:292)
excluded animation names .GMS m_iExcludedAnimNamesOffset nothing
physics data .GMS m_lPhysicsDataOffset ZDynamicsExtendRuntime::ReadDataBuffer (enginedatabase.cpp:2054)
room exits, neighbour rooms .PRP ZROOM properties ExitsOffset, NeighborsOffset ZROOM::SetExitsOffset / SetNeighborsOffset (zroom.cpp:521, 532)
crowd setup, raw bones, flow data .PRP ZHFlowLnkObj SetupOffset, RawBonesDataOffset; ZHFlow FlowDataOffset humanflow.cpp:676, 691, 851
particle parameter animations .PRP ZParticleEmitter offsets zparticleemitter.cpp:133, 153
matrix/position animation keys the 'KEYS' chunk of a geom's extra init data (SWFCKeys offsets) ZRunMatPosAnim (zrunmatposanim.cpp:81)
shape segments ZSHAPE::m_pShapeSegments zshape.cpp:46

buf.py splits the buffer at every offset the .GMS names and decodes those blocks. Whatever lies between a decoded block (plus its padding) and the next known offset stays an unknown block. These are the property-referenced blocks in the last five rows and the key arrays. Across the disc the buffer bytes split as follows:

Kind Bytes Share
names 3,409,104 50.4%
unknown (.PRP/'KEYS' data) 1,975,552 29.2%
material database 734,992 10.9%
extra init data 530,192 7.8%
weapon prim tables 68,656 1.0%
excluded anim names 42,816 0.6%
physics 2,496 0.0%

Every reference the .GMS makes decodes: 108,803 name blocks, 856 extra-data chunks, 71 material databases, 71 excluded-name lists, 64 weapon tables and 2 physics blocks.

Geom names

A name is a zero-terminated Latin-1 string, padded with zeros to 16 bytes. The packer stores each distinct name once. Geoms with the same name share the block, and the 198,583 geoms use 108,803 names. In tree order, each name's first use comes at a higher offset than the previous one.

Extra init data

An extra init data block is a CHUNKFILE tree (src/engine/zstdlib/chunk.h, described in scene-small.md). Every root on the disc is named 0 and has only children. Code looks the children up with ZGEOM::GetChunkData(name):

Child Shape Read by
'KEYS' (0x4b455953) plain, 40 bytes: SWFCKeys (zkeyspack.h:6): m_lNrFrames, m_lStart, m_lEnd, m_lStep, then BUF offsets m_lOffsetsQ4, m_lTimesQ4, m_lKeysQ4, m_lOffsetsV3, m_lTimesV3, m_lKeysV3 ZMatPosAnim (zmatposanim.cpp:53), doors, body containers
0x80004 children and data, one child ZParamAnim / ZRunParamAnim (zrunparamanim.cpp:30): data is start/end frame. Children PARAM_CAMERA_FOV (0x100001) and PARAM_LIGHT_MULTIPLIER hold float keys.
'CUTS' (0x43555453) data pieces ZCutSequence2 (cutsequence2.cpp:125)

On the disc, 1,286 references use 'KEYS' alone, 800 use 0x80004 alone, and the rest combine them.

Material description database

The database is a relocatable bytestream (BS_Runtime, materialdescriptiondb.h). Every reference inside it is an offset from the block start. Init reads the SRoot:

Offset Field
0x00 m_MaterialDefinition → ZArray<SMaterialDef>: count, then (name string id, material id)
0x04 m_PropertyDefinition → ZArray<SPropertyDef>: count, then (name string id, EPropertyType, offset in a material record, enum index or -1)
0x08 m_EnumDefinition → ZArray<SEnumDef>: count, then (name string id, offset of a ZArray<SEnumValueDef>: count, then (name string id, index))
0x0c m_MaterialSize: bytes per material record (452 everywhere)
0x10 m_StringDB → ZArray<offset>: count, then the offset of each zero-terminated string
0x14 m_MaterialData → m_MaterialSize bytes per material, in material id order (ids are 1-based)

The parts appear in the order root, materials, properties, enums, material data, each enum's values, string table, strings. Strings are padded to 4 bytes with 0xcd. That order reproduces all 71 databases, so buf.py derives every internal offset.

A property value sits at MaterialSize × (id − 1) + offset (GetProperty_). For an enum property, that word is a stride and value e sits at + 4 + stride × e. All 71 databases have the same 19 materials (Default, Fabric, Stone, Snow, Wood, Flesh, Water, Car, Concrete, Asphalt, Soil, Glass, Grass, Metal, Plastic, Plaster, Cardboard, Gravel, Ceramic) and the same 18 properties. The properties are SolidType, HoleSize, HoleSizeVariation, BulletHole, DebrisEnter, DebrisExit, SoundEnter, SoundExit, FootPrint, ContactMark, ContactDust, ContactDebris, ContactSound, BloodSplatter, BloodStain, BloodTrails, DropSound and SoundMaterial. The two enums are scene:AllLevels/Weapons#/Engine/Objects/Weapons/Ammo (19 ammo types) and scene:AllLevels/Equipment#/Engine/Objects/Contact (no values). Only the values differ.

PT_ResID_Scene (type 4) values are .GMS geom numbers. RemapGeoms (materialdescriptiondb.cpp:19) turns −1 into 0 and any other value n into pRemaps[n − 1]. A copied database must therefore use −1 or geom numbers that exist in the new scene. The errormessages scene's database is all −1, which makes it a safe template. MaterialDB.value(material_id, property, enum_index) returns the raw 4 bytes.

Weapon prim table

The table holds three lists, each a u32 count followed by (prim handle, hash 0, hash 1) triples: total, used and big (InitWeaponHandles). The prim handles are .PRM handles. In _main scenes, PurgePrimBuffer (enginedatabase.cpp:1259) frees every total prim that is not used, not loaded out (the briefing's WeaponHashes match the hash pair) and, under NoHitmanBigWeaponsPurge, not big. 64 scenes have the table, and the header offset is 0 in the other seven.

Excluded animation names

The list is a u32 count followed by length-prefixed strings (a u8 length, then the characters, with no terminator). Nothing reads it. M05_main lists 152 names such as /AI/Check_ID_Guard, and most scenes have count 0.

Physics data

Two scenes have a physics block, which ZDynamicsExtendRuntime::ReadDataBuffer parses. buf.py keeps it as bytes up to the next known block.

What a new scene needs

  • The magic.
  • One name per geom.
  • A material database. Copy errormessages' (all scene references −1), or build one with MaterialDB and keep the 19 materials if .PRP material ids refer to them.
  • Extra init data only for geoms that animate.
  • Optionally a weapon prim table. An excluded-names block is not needed: point the header at any block, for example a zero count.

buf.BufBuilder appends blocks on 16-byte boundaries and returns their offsets for the .GMS and .PRP to use.

Unknown

  • The layout of the property-referenced blocks (ZExit, ZNeighborRoom, ZSetup, ZRawBonesHeader, ZFlowData, ZParticleEmitterAnimationHeader, shape segments, key arrays). Their readers are listed above, but buf.py does not decode them yet. Splitting them needs the .PRP property values and the 'KEYS' offsets.
  • The physics block format (ZDynamicsExtendRuntime::ReadDataBuffer).
  • The stale bytes after three weapon tables, which are packer memory and are kept as padding.