Skip to content

fragment_children: raw child-drawable census; closes the levels.rpf fragment gap (ASSET-261) - #3756

Open
kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:asset-261-levels-fragments
Open

kapitche wants to merge 2 commits into
ItsNotRexed:mainfrom
kapitche:asset-261-levels-fragments

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: ASSET-261

AI-assisted.

What and why

docs/character_models.md said the 50,798 levels.rpf fragments were "not raw-walked for child arrays; that is the one bounded gap". This closes it.

tools/fragment_children.py reads any plain RPF9 archive in place (same carve as skin_streams.py, no manifest, no TOC) and counts, per fragment, the child drawables. Measured layout: the u16 at fragment +0x30 is the drawable count and the records are 0x110 bytes apart starting at the main drawable, so records 1 and up are children. It also scans every other 16-byte offset of the system pages for drawable bodies, so a child outside the array would show up as a "stray". It handles page directories that sit past the first system page, takes --jobs/--start/--end, and can write a local per-fragment records file for a re-count.

Counts (the same tool on the three archives, same package, 1.0.2.1):

archive fragments single drawable with a child array child drawables strays
packs.rpf 2,865 2,865 0 0 0
data.rpf 1,758 1,758 0 0 0
levels.rpf 50,802 50,068 734 735 0

The packs.rpf row reproduces the existing "2,865, no child arrays" result, which is the control. In levels.rpf every declared child (a - 1, 735) is found at the stride and nothing else is found. The tool counts 50,802 fragments against the 50,798 in the issue (the exporter's manifest); I did not chase the 4. Also recorded: 10,097 levels fragments have a null +0x10, +0x20 or +0x28, which skin_streams.resource_kind rejects today (#2379), and 6 have a main drawable that fails the embedded header test.

Not reproduced: the bus2_frag_hi figures already in the doc ("14 model structs, 11 children") come from a different walk. This one does not count them, and the doc now says so.

Tested

  • python -I tests/test_fragment_children.py (19 tests, synthetic pages and archives only): Python 3.12.16 on Linux (WSL2) and Python 3.13.1 on Windows.
  • python -m compileall -q make_game.py tools tests, tests/test_docs_list_items.py, tests/test_text_write_line_endings.py, tests/test_skin_streams.py: pass on Linux 3.12.16.
  • Negative controls (tool edited, tests rerun): stride 0x110 -> 0x100 fails 6 tests; removing the bounds check fails test_non_finite_bounds_are_not_a_body; removing the model geometry-count check fails test_model_with_mismatched_geometry_counts_is_not_a_body.
  • Ran on the real archives read-only: levels.rpf took 22 min with 12 workers over a WSL mount. Nothing from the archives is in the repo.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No translator rule.

… levels.rpf result (ASSET-261)

Walks the fragment drawable array (u16 at +0x30 records, 0x110-byte stride) and scans the system pages for
other drawable bodies, over any plain RPF9 archive, with --jobs/--start/--end. Synthetic tests only.
docs/character_models.md: levels.rpf and data.rpf counts next to the packs.rpf ones.
@leoleahy

Copy link
Copy Markdown
Contributor

Independent CPU review at f3072c31 on Windows (CPython 3.14.8, no GPU, no game files): pass.

  • python -I tests/test_fragment_children.py: 19 OK.
  • Repo-wide invariants all pass: tests/test_tool_stdout_encoding.py (2 OK; verified tools/fragment_children.py includes sys.stdout.reconfigure(errors="replace") in __main__), tests/test_text_write_line_endings.py (2 OK), tests/test_tool_startup.py (2 OK).
  • python -m compileall -q tools/fragment_children.py tests/test_fragment_children.py: clean; LF line endings preserved on all 3 files.

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested f3072c3 on Windows, CPython 3.14.7, CPU only (no GPU, no game files).

python -I tests/test_fragment_children.py gives 19 tests, OK. The tool is new, thus there is no merge-base control.

I found one defect. The archive scan does not find a drawable at the last aligned offset of a fragment.

Run this script from the repository root. It uses only synthetic bytes:

import runpy
t = runpy.run_path("tests/test_fragment_children.py")
for padding in (0, 16):
    b = t["fragment"](strays=(0x1f50,)) + bytes(padding)
    print(len(b), t["fc"].drawable_body(b, 0x1f50), t["TestFragmentChildren"]().scan(t["archive"](b))["stray_drawables"])

Actual result:

8192 True 0
8208 True 1

Expected result: the count is 1 in the two cases. The body check accepts the same bytes in the two cases.
Cause: in stray_bodies, range(HEADER, n - 176, 16) does not include the last candidate offset.
Possible correction: include that offset in the range, and add the case without padding to the tests.

AI-assisted.

@sds-228 sds-228 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review of f3072c3: no issues found. The census tool and its synthetic tests stay at count level, and the docs update matches the measured totals. Targeted suite: 19 tests OK; compileall and diff check clean.

AI-assisted (OpenCode).

@kapitche

Copy link
Copy Markdown
Contributor Author

Pushed d84e735: thanks @shdann, stray_bodies now scans up to the last aligned offset (range(HEADER, n - 176 + 1, 16)), and a new test puts a stray at 0x1f50 on an 0x2000 page with 0 and 16 bytes of padding (both count 1). Tests: tests/test_fragment_children.py 20 OK, test_tool_stdout_encoding/test_text_write_line_endings/test_tool_startup OK (Linux, CPython 3.12.16). Negative control: with the old range the new test fails (1 failure).

@ricardotanaka

Copy link
Copy Markdown

Tested the current head d84e7351 (with the last-offset scan fix) on my own copy of the 1.0.2.1 package, natively on Windows 11 (CPython 3.12, --jobs 6): same counts as the table, and still no strays after the fix.

archive fragments multi-drawable child arrays declared / found children strays
packs.rpf 2,865 0 0 0 / 0 0
data.rpf 1,758 0 0 0 / 0 0
levels.rpf 50,802 734 734 735 / 735 0

Also as in the description: levels.rpf has 10,097 fragments with a null optional pointer and 6 whose main drawable fails the header test; 0 unreadable in all three. Run times 51 s, 2 s and 151 s. Counts only, no game data. AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified merged with main on Linux: tests/test_fragment_children.py passes (19/19) and the compliance suites (text_write_line_endings, docs_python_deps, docs_repo_paths, docs_cli_flags, docs_links, tool_startup) are green on the merged tree. The tool is stdlib-only, pins newline="\n" on every write, and all test bytes are synthetic. The archive census figures need a game copy and were not re-measured here. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants