Fix level extents read as bytes, and extract the level geometry
A defect in the Phase 1 extractor, found by reading the data extent's contents: level extents are declared in sector counts, the generic G/ pair table uses byte counts, and level containers were being routed through the generic path. So LEVEL0.WAD's data extent -- 6,484 sectors -- was taken as 6,484 bytes. Every test still passed, because a truncated file is still a file. The extent's own header gave it away: it declares an uncompressed total of 13,277,920 bytes, and 13,277,920 / 6,484 is 2047.7. A ratio landing on 2048 is a unit error, not a compression ratio. Across 27 levels this had been discarding roughly 300 MB of level content. Level containers now go through the typed LevelHeader instead. Level 0 yields: data.bin 13,279,232 the data extent core_data.wad 9,738,678 the geometry, compressed core_data.bin 16,969,296 the geometry, decompressed core_index.bin 31,600 the geometry directory gameplay.bin 659,456 sound_bank.bin 1,503,232 core_data is a WAD LZ77 stream -- the format already implemented for the game -- and it decompresses cleanly. The first real level content this project has recovered. New: LevelDataHeader parses the data extent's own header, a table of byte ranges naming the level overlay executable, the geometry directory, the GS RAM image, the HUD banks and the geometry. Note this is a third meaning for a "size" field on the same disc, after sector counts in the level header and byte counts in the generic table; all three are documented together in level-data.md so the next parser does not rediscover this the hard way. The geometry directory's offsets were checked against the geometry rather than taken on trust: all twelve land inside the 16.9 MB of decompressed core_data, which is what confirms the header is being read correctly. The class tables themselves do not yet decode under the documented layout, and that is recorded as open work in plan/level-assets.md rather than papered over. 75 unit tests, 9 integration tests against the real disc. Hash table regenerated at 5,159 files and 5,671,915,799 bytes -- hashes only, no content. Extraction now writes to ~/Documents/gc-extracted rather than /tmp, which is 16 GB and did not have room for a 5.3 GB output.
This commit is contained in:
@@ -22,6 +22,9 @@ fn main() -> ExitCode {
|
||||
Some("identify") => run_identify(&args[1..]),
|
||||
Some("unpack") => run_unpack(&args[1..]),
|
||||
Some("verify") => run_verify(&args[1..]),
|
||||
// Scratch helper used while reversing the level formats: decompress a
|
||||
// byte range of an already-unpacked file with the WAD decoder.
|
||||
Some("wad") => run_wad(&args[1..]),
|
||||
Some("--help") | Some("-h") | Some("help") | None => {
|
||||
usage();
|
||||
ExitCode::SUCCESS
|
||||
@@ -187,6 +190,56 @@ fn run_unpack(args: &[String]) -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_wad(args: &[String]) -> ExitCode {
|
||||
if args.len() < 4 {
|
||||
eprintln!("usage: gc-extract wad <file> <offset> <size> <out>");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
let data = match std::fs::read(&args[0]) {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
eprintln!("error: {}", e);
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
let off: usize = match args[1].parse() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("error: bad offset: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
let sz: usize = match args[2].parse() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("error: bad size: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
if off + sz > data.len() {
|
||||
eprintln!(
|
||||
"error: range {off}..{} is outside the {} byte file",
|
||||
off + sz,
|
||||
data.len()
|
||||
);
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
match gc_formats::wad::decompress(&data[off..off + sz]) {
|
||||
Ok(out) => {
|
||||
if let Err(e) = std::fs::write(&args[3], &out) {
|
||||
eprintln!("error: cannot write {}: {e}", args[3]);
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
println!("{} bytes -> {} bytes -> {}", sz, out.len(), args[3]);
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("error: {e}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_verify(args: &[String]) -> ExitCode {
|
||||
if args.len() < 2 {
|
||||
eprintln!("usage: gc-extract verify <out-dir> <table.tsv>");
|
||||
|
||||
@@ -13,6 +13,8 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use gc_formats::global_wad::{GlobalWad, RangeKind};
|
||||
use gc_formats::iso9660::IsoImage;
|
||||
use gc_formats::level_data::{LevelDataHeader, LevelDataPart};
|
||||
use gc_formats::level_wad::LevelHeader;
|
||||
|
||||
/// One unpacked asset, with the identity we record for verification.
|
||||
pub struct Extracted {
|
||||
@@ -76,6 +78,106 @@ pub fn unpack(iso_path: &Path, out_dir: &Path) -> Result<Vec<Extracted>, String>
|
||||
continue;
|
||||
}
|
||||
|
||||
// Level containers have a typed header whose extents use sector counts,
|
||||
// not the byte counts of the generic `G/` pair table. Reading them
|
||||
// through the generic path would take a 6,484-sector extent as 6,484
|
||||
// bytes and silently lose 13 MB of every level, so they go through the
|
||||
// typed parser.
|
||||
if stem.starts_with("level")
|
||||
&& stem
|
||||
.trim_start_matches("level")
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_digit())
|
||||
{
|
||||
match LevelHeader::parse(&raw) {
|
||||
Ok(header) => {
|
||||
let mut parts = 0usize;
|
||||
for (part, range) in header.ordered_extents() {
|
||||
let Some(offset) = range.byte_offset() else {
|
||||
continue;
|
||||
};
|
||||
let len = range.byte_len() as usize;
|
||||
if offset + len as u64 > raw.len() as u64 {
|
||||
eprintln!(
|
||||
" {stem}/{}: runs past the end of the container; skipped",
|
||||
part.name()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
write_file(
|
||||
out_dir,
|
||||
&format!("{stem}/{}.bin", part.name()),
|
||||
&raw[offset as usize..offset as usize + len],
|
||||
&mut written,
|
||||
);
|
||||
parts += 1;
|
||||
}
|
||||
// The `data` extent is itself a container of byte ranges.
|
||||
// Split out the geometry directory and the geometry, so the
|
||||
// latter is usable without re-deriving the layout later.
|
||||
// `data_extent` is the `data` slice; `raw` is the whole
|
||||
// level container and its offsets are unrelated.
|
||||
let data_extent: &[u8] = &raw[header.data.offset as usize * 2048
|
||||
..(header.data.offset as usize + header.data.size as usize) * 2048];
|
||||
match LevelDataHeader::parse(data_extent) {
|
||||
Ok(data_header) => {
|
||||
let core = data_header.part(LevelDataPart::CoreData);
|
||||
let index = data_header.part(LevelDataPart::CoreIndex);
|
||||
if let Ok(bytes) = core.slice(data_extent, data_extent.len()) {
|
||||
// `core_data` is a WAD stream: keep both the
|
||||
// compressed original and the decoded form,
|
||||
// because the hash table covers the original.
|
||||
write_file(
|
||||
out_dir,
|
||||
&format!("{stem}/core_data.wad"),
|
||||
bytes,
|
||||
&mut written,
|
||||
);
|
||||
match gc_formats::wad::decompress(bytes) {
|
||||
Ok(decoded) => {
|
||||
write_file(
|
||||
out_dir,
|
||||
&format!("{stem}/core_data.bin"),
|
||||
&decoded,
|
||||
&mut written,
|
||||
);
|
||||
}
|
||||
Err(e) => eprintln!(
|
||||
" {stem}: core_data did not decompress ({e}); the \
|
||||
compressed form was kept"
|
||||
),
|
||||
}
|
||||
parts += 1;
|
||||
}
|
||||
if let Ok(bytes) = index.slice(data_extent, data_extent.len()) {
|
||||
write_file(
|
||||
out_dir,
|
||||
&format!("{stem}/core_index.bin"),
|
||||
bytes,
|
||||
&mut written,
|
||||
);
|
||||
parts += 1;
|
||||
}
|
||||
}
|
||||
Err(e) => eprintln!(" {stem}: data extent header did not parse: {e}"),
|
||||
}
|
||||
|
||||
println!(
|
||||
" {:<16} {} bytes, level id {} -> {} parts",
|
||||
stem,
|
||||
raw.len(),
|
||||
header.id,
|
||||
parts
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
skipped.push((relative.clone(), e.to_string()));
|
||||
write_file(out_dir, &relative, &raw, &mut written);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let container = match GlobalWad::parse_with(&raw, RangeKind::for_container(&relative)) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user