"""Decode the MoVE (version 19) state-machine and motion-tree containers in anim.rpf (ASSET-86). python -I tools/anim_move.py \\anim.rpf \\anim-move.json Each MoVE resource has a fixed 32-byte header (`MoVE`, u32 version = 19, three zero u32s, u32 network hash at +0x14, u32 body_size at +0x18, one zero u32 at +0x1C), followed at +0x20 by a variable preamble: - u32 N_req + N_req × u32 request hashes - u32 N_flag + N_flag × u32 flag hashes - u32 preamble_mode (0, 1 or 2) - u32 N_desc + N_desc × 5-byte `{u32 hash; u8 kind}` descriptors - 10-byte tail `{u16 sub_flag; u32 ref_hash_0; u32 ref_hash_1}` (zero when preamble_mode is 0 or 1) `body_size` at +0x18 excludes the 46 fixed/count/tail preamble bytes outside it and the `5 * N_desc` descriptor table, so every complete MoVE v19 resource satisfies: total_bytes == 46 + 5 * N_desc + body_size One resource in anim.rpf (at 0x105a7d000, 71,099 bytes) exceeds RPF9's 64 KiB stream ceiling and spans two consecutive 16-byte-aligned zlib streams (65,536 + 5,563 bytes). After the 10-byte preamble tail sits the root state container (`Type 1` = StateMachine, `Type 3` = InlinedStateMachine, `Type 22` = MotionTree), whose child states, transitions and length-prefixed parameter blocks recurse through signed 32-bit self-relative offsets. Usage: `python -I tools/anim_move.py --help` lists the options. """ import argparse import collections import json import os import re import struct import sys import tempfile import zlib sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import anim_index # noqa: E402 from file_mode import published_mode # noqa: E402 from replace_retry import replace as replace_retry # noqa: E402 from report_paths import same_report_path # noqa: E402 MAGIC = b"MoVE" VERSION = 19 TYPE_STATE_MACHINE = 1 TYPE_INLINED_STATE_MACHINE = 3 TYPE_MOTION_TREE = 22 CONTAINER_TYPES = { TYPE_STATE_MACHINE: "state_machine", TYPE_INLINED_STATE_MACHINE: "inlined_state_machine", TYPE_MOTION_TREE: "motion_tree", } KNOWN_SCHEMA_HASHES = { anim_index.joaat32("statemachine"): "statemachine", anim_index.joaat32("motiontree"): "motiontree", } MAX_STANDALONE_FILE_BYTES = 128 * 1024 * 1024 class DecodeError(ValueError): pass def is_move_container(data): return len(data) >= 32 and data[:4] == MAGIC def expected_total_size(data): """Return `46 + 5 * N_desc + body_size` once the preamble up to `N_desc` is available, or None if truncated.""" if not is_move_container(data) or len(data) < 48: return None body_size = struct.unpack_from(" len(data): return None n_flag = struct.unpack_from(" len(data): return None n_desc = struct.unpack_from("= 0x4C`).""" if start < 0 or end < start or end > len(data): raise DecodeError(f"transition span 0x{start:x}..0x{end:x} outside resource") out, p = [], start for _ in range(count): if p + 0x4C > end: raise DecodeError(f"truncated transition header at 0x{p:x}") tr_size = struct.unpack_from(" end: raise DecodeError(f"invalid transition size 0x{tr_size:x} at 0x{p:x}") target_rel = struct.unpack_from("= 0x50 else 0 out.append({ "offset": p, "size": tr_size, "target": p + 0x48 + target_rel, "condition_kind": cond_kind, "condition_bytes": max(0, tr_size - 0x50), }) p += tr_size if p != end: raise DecodeError(f"transition block at 0x{start:x} ended at 0x{p:x}, expected 0x{end:x}") return out def _parse_default_transitions(data, start, end, count): """Parse `count` default/any-state transitions in `data[start:end]`: `{u16 size; u16 cond_count; i32 target_rel}`.""" if start < 0 or end < start or end > len(data): raise DecodeError(f"default-transition span 0x{start:x}..0x{end:x} outside resource") out, p = [], start for _ in range(count): if p + 16 > end: raise DecodeError(f"truncated default transition at 0x{p:x}") size, cond_count, target_rel = struct.unpack_from(" end: raise DecodeError(f"invalid default-transition size {size} at 0x{p:x}") out.append({ "offset": p, "size": size, "conditions": cond_count, "target": p + 4 + target_rel, }) p += size if p != end: raise DecodeError(f"default-transition block at 0x{start:x} ended at 0x{p:x}, expected 0x{end:x}") return out def _parse_param_blocks(data, start, end_limit, count): """Parse `count` length-prefixed MotionTree parameter blocks starting at `start`. Every block satisfies: block_size == 12 + 16 * (b1 + b2 + b3 + b4) + 8 * b5 + sum(sub_size for _ in range(b6)) with `b7 == 0`. Returns `(blocks, end_offset)`. """ if start < 0 or start > end_limit: raise DecodeError(f"parameter block start 0x{start:x} outside container") blocks, p = [], start for _ in range(count): if p + 12 > end_limit: raise DecodeError(f"truncated parameter block header at 0x{p:x}") bsz = struct.unpack_from(" end_limit: raise DecodeError(f"invalid parameter block size {bsz} at 0x{p:x}") b0, b1, b2, b3, b4, b5, b6, b7 = data[p + 4:p + 12] if b7 != 0: raise DecodeError(f"non-zero parameter block reserved byte b7={b7} at 0x{p:x}") n16 = b1 + b2 + b3 + b4 q = p + 12 + 16 * n16 + 8 * b5 if q > p + bsz: raise DecodeError(f"parameter block table overflow at 0x{p:x}") sub_sizes = [] for _ in range(b6): if q + 6 > p + bsz: raise DecodeError(f"truncated curve sub-record at 0x{q:x}") sub_sz = struct.unpack_from(" p + bsz: raise DecodeError(f"invalid curve sub-record size {sub_sz} at 0x{q:x}") sub_sizes.append(sub_sz) q += sub_sz if q != p + bsz: raise DecodeError(f"parameter block size mismatch at 0x{p:x}: walked {q - p}, header {bsz}") blocks.append({ "offset": p, "size": bsz, "index": b0, "bindings_16b": n16, "binding_counts": (b1, b2, b3, b4), "outputs_8b": b5, "curves": b6, "curve_bytes": sum(sub_sizes), }) p += bsz return blocks, p def _parse_container(data, off, end_limit, stats): """Recursively decode a `Type 1` (`StateMachine`), `Type 3` (`InlinedStateMachine`), or `Type 22` (`MotionTree`).""" if off + 0x24 > end_limit or end_limit > len(data): raise DecodeError(f"truncated state container at 0x{off:x}") (ntype, nidx, name_hash, init_rel, flags_0c, b10, b11, n_children, n_transitions) = struct.unpack_from(" end_limit: raise DecodeError(f"truncated {kind} header at 0x{off:x}") tr_rel = struct.unpack_from(" end_limit: raise DecodeError(f"truncated inlined_state_machine input table at 0x{off:x}") for i in range(n_inputs): ipos = off + 0x5C + 4 * i inp_target = ipos + struct.unpack_from(" 0 else child_table_end if n_transitions > 0 and tr_start != child_table_end: raise DecodeError( f"{kind} at 0x{off:x}: transition start 0x{tr_start:x} != child table end 0x{child_table_end:x}" ) child_entries = [] for i in range(n_children): epos = child_table_off + 8 * i ch_hash, ch_rel = struct.unpack_from(" end_limit: raise DecodeError(f"truncated motion_tree header at 0x{off:x}") tr_rel, blk_rel, n_param_blocks = struct.unpack_from(" len(data): raise DecodeError("truncated MoVE preamble at request count") n_req = struct.unpack_from(" len(data): raise DecodeError("truncated MoVE request table") requests = list(struct.unpack_from(f"<{n_req}I", data, pos)) if n_req else [] pos += 4 * n_req n_flag = struct.unpack_from(" len(data): raise DecodeError("truncated MoVE flag table") flags = list(struct.unpack_from(f"<{n_flag}I", data, pos)) if n_flag else [] pos += 4 * n_flag preamble_mode, n_desc = struct.unpack_from("<2I", data, pos) pos += 8 if preamble_mode not in (0, 1, 2): raise DecodeError(f"unexpected MoVE preamble_mode {preamble_mode}") if pos + 5 * n_desc + 10 > len(data): raise DecodeError("truncated MoVE 5-byte descriptor table or 10-byte preamble tail") descriptors = [] for _ in range(n_desc): h, kind = struct.unpack_from(" MAX_STANDALONE_FILE_BYTES: continue m = re.search(r"0x([0-9a-fA-F]+)|([0-9a-fA-F]{6,})", name) offset = int(m.group(1) or m.group(2), 16) if m else len(entries) with open(full, "rb") as fh: data = fh.read() entries.append((offset, name, data)) entries.sort(key=lambda e: (e[0], e[1])) items = [] i = 0 while i < len(entries): offset, _name, data = entries[i] if not is_move_container(data): i += 1 continue blob = bytearray(data) stream_count = 1 want = expected_total_size(blob) j = i + 1 while (want is not None and len(blob) < want and j < len(entries) and len(entries[j - 1][2]) == anim_index.MAX_CHUNK and not is_move_container(entries[j][2])): blob += entries[j][2] stream_count += 1 j += 1 items.append((offset, stream_count, len(blob), bytes(blob))) i = j return items, len(entries) def scan(path, start=None, limit=None): """Scan an RPF9 archive or a directory of carved streams and decode every MoVE v19 container.""" if os.path.isdir(path): if start is not None or limit is not None: raise ValueError("--start and --limit apply only to an RPF archive") raw_items, total_streams = _collect_move_streams_dir(path) else: raw_items, total_streams = _collect_move_streams_rpf(path, start, limit) stats = collections.Counter() resources = [] total_stored = 0 total_inflated = 0 total_move_streams = 0 for offset, stream_count, stored_bytes, blob in raw_items: total_stored += stored_bytes total_inflated += len(blob) total_move_streams += stream_count try: parsed = parse_move(blob) except (DecodeError, struct.error) as e: stats["move_resources_unparsed"] += 1 resources.append({ "offset": offset, "streams": stream_count, "stored_bytes": stored_bytes, "bytes": len(blob), "error": str(e)[:160], }) continue parsed["offset"] = offset parsed["streams"] = stream_count parsed["stored_bytes"] = stored_bytes resources.append(parsed) stats["move_resources"] += 1 stats["move_streams"] += stream_count if stream_count > 1: stats["multi_stream_resources"] += 1 stats[f"version:{parsed['version']}"] += 1 p = parsed["preamble"] stats[f"preamble_mode:{p['mode']}"] += 1 if p["requests"]: stats["resources_with_requests"] += 1 stats["requests"] += len(p["requests"]) if p["flags"]: stats["resources_with_flags"] += 1 stats["flags"] += len(p["flags"]) if p["descriptors_5b"]: stats["resources_with_descriptors_5b"] += 1 stats["descriptors_5b"] += len(p["descriptors_5b"]) for d in p["descriptors_5b"]: stats[f"descriptor_5b_kind:{d['kind']}"] += 1 root = parsed["root"] stats[f"root_container:{root['kind']}"] += 1 for k, v in parsed["counts"].items(): stats[k] += v summary = { "streams": total_streams, "move_resources": len(raw_items), "move_streams": total_move_streams, "stored_bytes": total_stored, "inflated_bytes": total_inflated, "counts": dict(sorted(stats.items())), } return resources, summary def _summary_row(r): """Build a game-data-free summary row for JSON output (counts and structural metadata only, no raw hash arrays).""" if "error" in r: return r p = r["preamble"] root = r["root"] c = r["counts"] return { "offset": r["offset"], "streams": r["streams"], "stored_bytes": r["stored_bytes"], "bytes": r["bytes"], "version": r["version"], "body_size": r["body_size"], "node_start": r["node_start"], "graph_bytes": r["graph_bytes"], "preamble_mode": p["mode"], "requests": len(p["requests"]), "flags": len(p["flags"]), "descriptors_5b": len(p["descriptors_5b"]), "root_type": root["type"], "root_kind": root["kind"], "containers": c.get("containers", 0), "state_machines": c.get("container:state_machine", 0), "inlined_state_machines": c.get("container:inlined_state_machine", 0), "motion_trees": c.get("container:motion_tree", 0), "transitions": c.get("transitions", 0), "default_transitions": c.get("default_transitions", 0), "param_blocks": c.get("param_blocks", 0), } def _write_json_atomic(path, doc): """Publish complete JSON, or leave the previous report intact on replace failure.""" destination = os.path.abspath(path) parent = os.path.dirname(destination) or "." os.makedirs(parent, exist_ok=True) try: mode = os.stat(destination).st_mode except FileNotFoundError: mode = None except OSError: # Non-regular destinations (pipes/devices) keep a direct write. with open(destination, "w", encoding="utf-8", newline="\n") as f: json.dump(doc, f, indent=1) return if mode is not None: # Retain the direct writer's permission check without truncating the report. os.close(os.open(destination, os.O_WRONLY)) staged = tempfile.NamedTemporaryFile( mode="w", encoding="utf-8", newline="\n", delete=False, dir=parent, prefix=".anim_move_", suffix=".tmp", ) try: with staged: json.dump(doc, staged, indent=1) os.chmod(staged.name, published_mode(destination)) replace_retry(staged.name, destination) finally: try: os.chmod(staged.name, 0o600) except OSError: pass try: os.unlink(staged.name) except FileNotFoundError: pass def main(argv=None): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("archive", help="anim.rpf or a directory of carved MoVE streams") ap.add_argument("out", help="output .json (counts and one row per MoVE resource)") ap.add_argument("--start", type=anim_index.offset, help="first byte to scan (default: the RPF data start)") ap.add_argument("--limit", type=anim_index.byte_count, help="number of bytes to scan") a = ap.parse_args(argv) if not os.path.exists(a.archive): sys.exit(f"{a.archive}: No such file or directory") if os.path.isdir(a.archive) and (a.start is not None or a.limit is not None): ap.error("--start and --limit cannot be used when the input is a directory") if os.path.isdir(a.out): sys.exit(f"{a.out}: cannot write: Is a directory") inputs = [full for _name, full in _iter_dir_stream_paths(a.archive)] if os.path.isdir(a.archive) else [a.archive] if any(same_report_path(path, a.out) for path in inputs): sys.exit(f"{a.out}: refuses to overwrite the input archive") try: resources, summary = scan(a.archive, a.start, a.limit) except ValueError as e: sys.exit(f"{a.archive}: {e}") except OSError as e: sys.exit(f"{e.filename or a.archive}: {e.strerror or e}") rows = [_summary_row(r) for r in resources] try: _write_json_atomic(a.out, {"summary": summary, "move_resources": rows}) except OSError as e: sys.exit(f"{a.out}: cannot write: {e.strerror or e}") c = summary["counts"] print(json.dumps({ k: c.get(k, 0) for k in ( "move_resources", "move_streams", "multi_stream_resources", "containers", "container:state_machine", "container:inlined_state_machine", "container:motion_tree", "transitions", "default_transitions", "param_blocks", ) })) return 0 if __name__ == "__main__": try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass sys.exit(main())