"""The game versions this port knows, keyed by the build id of exefs/main.
Why: each update of Mario Kart World is a different executable (other addresses, another sdk build, 1.8.0 adds
subsdk1, the nnEcli module), so everything tied to one build (the patch sites, the RomFS size the setup checks, the
number the runtime switches its fixed addresses on) is chosen here, from the dump itself, in one place. The runtime
keeps the matching rows for its own fixed addresses in runtime/core/boot/version.c, keyed by `number`.
Usage: from versions import detect; v = detect(exefs) -> Version, or ValueError for a build not listed.
uv run recomp/versions.py [EXEFS] prints the version of a dump.
"""
import sys
from dataclasses import dataclass
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
@dataclass(frozen=True)
class Version:
name: str
number: int
main_build_id: str
romfs_files: int
patches: Path
VERSIONS = (
Version("1.0.0", 100, "9e244438c3d8ef1c", 205_660, ROOT / "recomp/patches-mkw.txt"),
Version("1.8.0", 180, "d00e4f399e411d06", 206_202, ROOT / "recomp/patches-mkw-180.txt"),
)
def build_id_of(head: bytes, where: str = "exefs/main") -> str:
"""The build id of exefs/main from its first 0x48 bytes (a folder's file or a .nup package's entry)."""
if head[:4] != b"NSO0":
raise ValueError(f"{where} is not an NSO: decrypted exefs needed")
return head[0x40:0x48].hex()
def main_build_id(exefs: Path) -> str:
return build_id_of((Path(exefs) / "main").read_bytes()[:0x48], str(Path(exefs) / "main"))
def detect(exefs: Path) -> Version:
return detect_id(main_build_id(exefs))
def detect_id(got: str) -> Version:
for v in VERSIONS:
if v.main_build_id == got:
return v
names = ", ".join(v.name for v in VERSIONS)
raise ValueError(f"exefs/main is not a known version of Mario Kart World ({names}; build id {got})")
ROMFS_PRODUCT = "System/Resource/ResourceSizeTable.Product."
def romfs_products(names) -> set[int]:
"""The game data version numbers RomFS file paths (relative, '/'-separated) name, empty when none does."""
found = set()
for name in names:
if name.startswith(ROMFS_PRODUCT):
number = name[len(ROMFS_PRODUCT) :].split(".", 1)[0]
if number.isdigit():
found.add(int(number))
return found
def romfs_mismatch(v: Version, names) -> str:
"""Why the RomFS whose file paths are `names` does not belong to v ("" when it does or names no version)."""
found = romfs_products(names)
if not found or v.number in found:
return ""
known = {x.number: x.name for x in VERSIONS}
got = ", ".join(known.get(n, f"Product.{n}") for n in sorted(found))
return f"RomFS {got}, exefs/main {v.name}"
def modules(exefs: Path) -> list[str]:
"""The game modules present in the ExeFS, in load order (recomp/layout.py ORDER)."""
sys.path.insert(0, str(ROOT / "recomp"))
from layout import ORDER
return [m for m in ORDER if (Path(exefs) / m).is_file()]
if __name__ == "__main__":
exefs = Path(sys.argv[1] if len(sys.argv) > 1 else "game/exefs")
v = detect(exefs)
print(f"Mario Kart World {v.name} (version {v.number}), modules {', '.join(modules(exefs))}, patches {v.patches.name}")