#!/usr/bin/env python3 """Extract a .unitypackage (gzipped tar of GUID dirs) into real paths. Copies the raw `asset` bytes of every FBX/texture/metadata entry to raw/. Prints a summary by extension. """ import sys import tarfile import shutil from pathlib import Path from collections import Counter KEEP = {".fbx", ".png", ".tga", ".json", ".txt", ".sk", ".asset"} def main(pkg: str, out_root: str) -> None: out = Path(out_root) out.mkdir(parents=True, exist_ok=True) counts: Counter = Counter() with tarfile.open(pkg, "r:gz") as tar: members = {m.name: m for m in tar.getmembers()} guids = sorted({name.split("/")[0] for name in members if "/" in name}) for guid in guids: pn = members.get(f"{guid}/pathname") asset = members.get(f"{guid}/asset") if pn is None or asset is None: continue pathname = tar.extractfile(pn).read().decode("utf-8").splitlines()[0].strip() ext = Path(pathname).suffix.lower() if ext not in KEEP: counts[f"(skipped {ext or 'dir'})"] += 1 continue dest = out / pathname dest.parent.mkdir(parents=True, exist_ok=True) with tar.extractfile(asset) as src, open(dest, "wb") as dst: shutil.copyfileobj(src, dst) counts[ext] += 1 for ext, n in sorted(counts.items()): print(f"{ext}\t{n}") if __name__ == "__main__": main(sys.argv[1], sys.argv[2])