#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of a Redump "Nintendo - GameCube" set held as one .rvz per zip. Reads ``$REDUMP_DIR/Nintendo - GameCube - NKit RVZ [zstd-19-128k]`` (or ``--dir``). For each zip it reads the central directory and the first 216 bytes of the one member: the RVZ file header and, inside it, the first 0x80 bytes of the original disc header, which RVZ keeps uncompressed. Nothing is extracted or copied; no disc is decoded; no game byte beyond those 0x80 header bytes is read. The run is cheap (2,014 small reads). The layout used (all big-endian): - RVZ file header: magic ``RVZ\\x01`` at 0, then version fields; the disc struct begins at 0x48 with four 32-bit words (disc type, compression, level, chunk size), and the disc header copy follows at 0x58. Source: Dolphin's ``docs/WiaAndRvz.md`` (the format's description, inside a GPL-2.0-or-later repository; a file format is a fact, nothing of it is fetched or copied here). Checked by every image carrying the GameCube magic below at the offset the layout predicts (OBSERVED, this run). - Disc header (YAGCD chapter 13, "DVD Structure"; DOC, community): game code 0x00 (4 bytes: system, two title letters, region), maker 0x04 (2), disc number 0x06, version 0x07, audio streaming flag 0x08, stream buffer size 0x09, Wii magic 0x18 (``5D1C9EA3``), GameCube magic 0x1C (``C2339F3D``), game name 0x20. The datfile, if given (``--dat``, a Redump Logiqx XML), is compared by name only: an NKit image is not the Redump dump byte for byte, so its SHA-1 cannot be checked without rebuilding the ISO (not done here). REDUMP_DIR=... python -I scripts/census_redump.py [--dat FILE] """ from __future__ import annotations import argparse import collections import os import re import struct import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path SET = "Nintendo - GameCube - NKit RVZ [zstd-19-128k]" GC_MAGIC = 0xC2339F3D WII_MAGIC = 0x5D1C9EA3 HEAD = 0x58 # disc header copy inside the RVZ file SYSTEMS = {"G": "G (game)", "D": "D (demo / not-for-resale)", "U": "U (utility)", "P": "P (promotional / PAL-converted)", "R": "R", "S": "S"} def parens(name: str) -> list[str]: return re.findall(r"\(([^)]*)\)", name) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--dir", help=f"default $REDUMP_DIR/{SET}") parser.add_argument("--dat", type=Path, help="Redump GameCube datfile (Logiqx XML)") args = parser.parse_args() root = Path(args.dir) if args.dir else None if root is None: base = os.environ.get("REDUMP_DIR") if not base: print("set REDUMP_DIR or pass --dir", file=sys.stderr) return 2 root = Path(base) / SET zips = sorted(p for p in root.iterdir() if p.suffix.lower() == ".zip") print(f"set: {SET}") print(f"images (zip files): {len(zips)}") members = collections.Counter() magic = collections.Counter() system = collections.Counter() region = collections.Counter() makers = collections.Counter() streaming = 0 streaming_titles = [] disc2 = 0 versions = collections.Counter() sizes = [] ids: dict[str, list[str]] = collections.defaultdict(list) tags = collections.Counter() regions_by_name = collections.Counter() problems = [] for path in zips: stem = path.stem for group in parens(stem): for part in (p.strip() for p in group.split(",")): if part in ("USA", "Europe", "Japan", "Korea", "Germany", "France", "Italy", "Spain", "UK", "Australia", "Brazil", "Netherlands", "Asia", "Taiwan"): regions_by_name[part] += 1 head = group.split(" ")[0] if head in ("Beta", "Proto", "Demo", "Rev", "Disc", "Kiosk", "Sample", "Unl", "Program", "Alt", "Promo", "Tech"): tags[head] += 1 with zipfile.ZipFile(path) as archive: infos = [i for i in archive.infolist() if not i.is_dir()] for info in infos: members[Path(info.filename).suffix.lower()] += 1 if len(infos) != 1: problems.append(f"{path.name}: {len(infos)} members") continue info = infos[0] sizes.append(info.file_size) with archive.open(info) as handle: data = handle.read(HEAD + 0x80) magic[data[:4]] += 1 disc = data[HEAD:HEAD + 0x80] gc, wii = struct.unpack(">I", disc[0x1C:0x20])[0], struct.unpack(">I", disc[0x18:0x1C])[0] if gc != GC_MAGIC: problems.append(f"{path.name}: no GameCube magic (wii magic {wii:08X})") continue code = disc[0:4].decode("ascii", "replace") maker = disc[4:6].decode("ascii", "replace") system[code[0]] += 1 region[code[3]] += 1 makers[maker] += 1 versions[disc[7]] += 1 if disc[6]: disc2 += 1 if disc[8]: streaming += 1 streaming_titles.append(code) ids[code + maker].append(stem) print(f"zip members by extension: {dict(members)}") print(f"file magics: { {k.decode('latin-1'): v for k, v in magic.items()} }") if sizes: print(f"RVZ sizes: min {min(sizes) / 2**20:.1f} MiB, max {max(sizes) / 2**20:.1f} MiB," f" total {sum(sizes) / 2**40:.2f} TiB") print(f"distinct game IDs (code + maker): {len(ids)}") print(f"images whose disc-number byte is not 0 (second discs): {disc2}") print(f"images with the audio-streaming flag set (disc header 0x08): {streaming}" f" ({len(set(streaming_titles))} distinct game codes)") print("\nsystem letter of the game code:") for key, count in system.most_common(): print(f" {SYSTEMS.get(key, key):28} {count}") print("\nregion letter of the game code:") for key, count in region.most_common(): print(f" {key} {count}") print("\ndisc version byte:") for key, count in sorted(versions.items()): print(f" {key} {count}") print("\nmost common maker codes:") for key, count in makers.most_common(12): print(f" {key} {count}") print("\nregion words in file names (an image counts once per region):") for key, count in regions_by_name.most_common(): print(f" {key:12} {count}") print("\ntags in file names (first word of a parenthesis):") for key, count in tags.most_common(): print(f" {key:8} {count}") if args.dat: tree = ET.parse(args.dat) header = tree.find("header/description") names = {g.get("name") for g in tree.getroot().iter("game")} have = {p.stem for p in zips} print(f"\ndatfile: {header.text if header is not None else args.dat.name}") print(f" entries {len(names)}; matched by name {len(names & have)};" f" in the datfile only {len(names - have)}; in the set only {len(have - names)}") for name in sorted(have - names)[:15]: print(f" set only: {name}") for name in sorted(names - have)[:15]: print(f" datfile only: {name}") if problems: print("\nproblems:") for line in problems: print(f" {line}") return 0 if __name__ == "__main__": sys.exit(main())