Parse the moby per-class part table

A moby class span does not start with its geometry. The first u32 is the
offset of a part table, and the table is 16-byte records of vertex
offset, vertex count, an unestablished second count, the next part offset
and flags.

Parts tile: vertex_offset + 16 * vertex_count == next_part. The stride is
never assumed anywhere -- it falls out of that identity, which is what
pins the record size and the field boundaries. The identity holds for
43,467 of 43,467 part records across every moby class on all 27 levels,
and the parser rejects any span that violates it rather than returning a
plausible wrong answer.

Two terminations are needed. A zero vertex_offset ends the run, but 172
records on the disc have a zero vertex_offset and a zero next_part while
still carrying a vertex count. Stopping only on the first leaves 99.6%
instead of 100% and quietly includes a second table as if it were parts.

1,270,802 vertices over 4,560 class spans; 116 spans carry no geometry.

Vertices are 16 bytes of VIF-packed half-float quads cycling through four
distinct words. Handed back raw -- unpacking them is separate work from
finding them, and second_count is recorded but not guessed at.
This commit is contained in:
2026-10-11 02:29:57 +02:00
parent 1f96ef09b9
commit bbb10c678d
5 changed files with 369 additions and 5 deletions
+93
View File
@@ -1526,3 +1526,96 @@ fn collision_framing_holds_on_every_level() {
);
eprintln!("collision: {payload_bytes} payload bytes across {levels} levels, {empty} empty");
}
/// Moby model geometry: the per-class part table.
///
/// The invariant is `vertex_offset + 16 * vertex_count == next_part`, which is
/// what makes the 16-byte record size and the field boundaries fall out of the
/// data instead of being assumed. The parser rejects any span that violates it,
/// so this test is really checking that no span on the disc does.
#[test]
fn moby_parts_tile_on_every_level() {
require_disc!();
let data = std::env::var_os("GC_EXTRACTED")
.map(PathBuf::from)
.unwrap_or_else(|| {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
PathBuf::from(home).join("Documents/gc-extracted")
});
if !data.exists() {
eprintln!("skipped: unpack the disc first");
return;
}
use gc_formats::level_core::LevelCoreHeader;
use gc_formats::moby::parse_moby_geometry;
let mut levels = 0usize;
let mut classes = 0usize;
let mut parts = 0usize;
let mut verts = 0usize;
let mut empties = 0usize;
for n in 0..27 {
let dir = data.join(format!("level{n}"));
let (Ok(core), Ok(index)) = (
std::fs::read(dir.join("core_data.bin")),
std::fs::read(dir.join("core_index.bin")),
) else {
continue;
};
let h = LevelCoreHeader::parse(&index).expect("core header");
let range = h.moby_classes;
assert!(
range.count > 0 && range.offset > 0,
"level{n}: no moby class table"
);
// Class spans tile between consecutive geometry offsets.
let stride = 0x20usize;
let mut offsets: Vec<usize> = (0..range.count as usize)
.map(|i| {
let at = range.offset as usize + i * stride;
let e = gc_formats::level_core::ClassEntry::parse(&index[at..at + stride])
.unwrap_or_else(|e| panic!("level{n}: class {i}: {e}"));
e.offset_in_asset_wad
})
.map(|o| o.max(0) as usize)
.filter(|&o| o > 0)
.collect();
offsets.sort_unstable();
assert!(!offsets.is_empty(), "level{n}: no moby geometry offsets");
for i in 0..offsets.len() {
let start = offsets[i];
let end = offsets.get(i + 1).copied().unwrap_or(core.len());
if end <= start {
continue;
}
let g = parse_moby_geometry(&core[start..end])
.unwrap_or_else(|e| panic!("level{n}: moby class at {start}: {e}"));
if g.is_empty() {
empties += 1;
}
// Every part's vertices are readable and inside the span.
for p in &g.parts {
assert!(
g.vertices(p).is_some(),
"level{n}: part vertices past the end of the class span"
);
}
parts += g.parts.len();
verts += g.vertex_count();
classes += 1;
}
levels += 1;
}
assert_eq!(levels, 27, "expected all 27 levels, got {levels}");
assert!(classes > 4_000, "only {classes} moby class spans");
assert!(parts > 40_000, "only {parts} moby parts");
assert!(verts > 1_200_000, "only {verts} moby vertices");
eprintln!(
"moby: {parts} parts tiling exactly, {verts} vertices, {classes} class spans \
({empties} with no geometry) across {levels} levels"
);
}