Skip to content

restofworld_model_join: handle straddling page-map slots, sort loose hex dirs numerically, and harden index.json load - #7750

Open
narekshaghoyandev wants to merge 1 commit into
ItsNotRexed:mainfrom
narekshaghoyandev:restofworld-model-join-straddling-pagemap-and-dir-order
Open

narekshaghoyandev wants to merge 1 commit into
ItsNotRexed:mainfrom
narekshaghoyandev:restofworld-model-join-straddling-pagemap-and-dir-order

Conversation

@narekshaghoyandev

Copy link
Copy Markdown
Contributor

Fixes three edge-case bugs in tools/restofworld_model_join.py (with zero overlap against #6514, #4021, and #5550):

  1. datResourceMap slot array straddling a 64 KiB system page boundary (_parse_page_map_header, ModelSystemPages.accept, join_streams): join_streams previously stopped fetching system chunks once the 16-byte page-map header fit (p8_off + 16) and immediately called full parse_page_map(sys_probe) before appending the remaining declared system_pages, causing valid multi-page resources whose 8 * (sp + gp) runtime slot array straddles into the next system page to fail prematurely with page-map slot array extends past system segment. Similarly, ModelSystemPages.accept dropped a short (< 64-byte) final system page when the page-map slot array crossed out of page 0. Now _parse_page_map_header validates the 16-byte header and any slot bytes already present in page 0 so remaining declared system pages are fetched first, and full parse_page_map(sys_bytes) validates the complete slot array across all system pages.
  2. Loose carved directory ordering & suffix case (scan_dir): When index.json is absent, scan_dir sorted directory entries lexicographically (sorted(os.listdir(path))) and checked case-sensitive .bin/.bk2 endings, which reversed mixed-width (100010.bin vs 1000010.bin) or mixed-case (0000000a0010.bin vs 0000000B0010.BIN) hex filenames and skipped uppercase extensions. Loose directories are now ordered numerically by their hex offset prefix (preserving exact behavior for equal-width lowercase names) and match .bin/.bk2 case-insensitively.
  3. index.json BOM and deep nesting (_carve_rows): Reads index.json as utf-8-sig so a UTF-8 BOM does not fail JSON parsing, and turns RecursionError from deeply nested JSON into a clean ValueError instead of an unhandled traceback.

Verification

  • python3 -I tests/test_restofworld_model_join.py -v: 12/12 pass (all 3 new tests fail/error against main).
  • python3 -I scripts/check_no_game_data.py tools/restofworld_model_join.py tests/test_restofworld_model_join.py: clean.
  • python3 -m compileall -q tools/restofworld_model_join.py tests/test_restofworld_model_join.py: clean.

Claim: #7694 (comment)
AI-assisted: yes (Gemini)

…hex dirs numerically, and harden index.json load
@hcampbell98

Copy link
Copy Markdown

Tested 3e4d11cde merged with main dde82f27e on Linux (WSL2, CPU-only, Python 3.14.4 & 3.12.13): Pass

  • Diff & repo hygiene: git diff --check dde82f27e...HEAD clean; python3 -I scripts/check_no_game_data.py tools/restofworld_model_join.py tests/test_restofworld_model_join.py clean; python3 -m compileall -q tools tests OK.
  • tests/test_restofworld_model_join.py: 12/12 OK on Python 3.14.4 (-I), 12/12 OK on Python 3.12.13 (-I), and 12 passed under pytest.
  • Negative control: reverting tools/restofworld_model_join.py to dde82f27e yields FAILED (failures=2, errors=1) (test_carve_index_accepts_utf8_bom_and_handles_deep_nesting, test_joins_page_map_slot_array_straddling_system_page_boundary, and test_loose_directory_sorts_hex_offsets_numerically_and_case_insensitively).
  • Edge & CLI probes: synthetic scan_dir and CLI run over a loose directory containing hex-prefixed .bin/.BIN, .bk2, and trailing non-hex .bin exited 0 cleanly (streams=4, joined_resources=1, bink2_files=1, offset 0xa0010); UTF-8 BOM index.json loaded in scan_dir, and in-process deep-nested index.json raised ValueError: index.json nesting is too deep.
  • Limits: CPU-only, no retail archives available on host.

AI-assisted.

@azterixx

Copy link
Copy Markdown

checked: #7750 | with: python -I -m unittest discover -s tests -p test_restofworld_model_join.py | on: Debian 13, Intel i7-8700, CPython 3.13.5, head 3e4d11c, CPU only | result: pass (12 tests; main@dde82f27e has 9)

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 3e4d11c on Windows 11 (26200.9974), Python 3.12.0: pass.

  • python -I tests/test_restofworld_model_join.py -v: 12/12 OK.
  • Negative control (merge-base bf85ff703, tools/restofworld_model_join.py from main, the head's test file): 12 tests, 2 failures + 1 error, exactly the three new tests. The straddle case fails with joined_resources 0 vs 1, the loose-directory case with 1 vs 2, and the BOM case errors in JSON parsing.
  • Code read: the probe now stops at the 16-byte header, ModelSystemPages.accept only consumes a short page when the header is complete or more system pages are declared, and join_streams parses the full slot array after fetching those pages and records a failed row instead of silently joining. A partially present non-zero slot word is rejected under the same error string the test asserts; a non-zero slot beyond system_pages is rejected by the full parse. scan_dir's key orders a 6+ digit hex prefix numerically and falls back to case-folded names; .bin/.bk2 are matched case-insensitively in all four places. utf-8-sig plus the RecursionError -> ValueError conversion leaves main() returning 1 with no output file, as tested.
  • compileall, check_no_game_data.py, git diff --check: pass.

One wording nit only: a non-zero slot beyond the 16-byte header, when those bytes happen to be present, is reported as "in page-map header". Both the old and new tests match that string, so I would not change it now.

Synthetic fixtures only; no game data.

AI-assisted.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 3e4d11c on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_restofworld_model_join.py - all exit 0.

AI-assisted (Claude Code).

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.

5 participants