Commit Graph

22 Commits

Author SHA1 Message Date
692b65cb10 story_index: make the staging file deletable when a replace fails
The staged report carries the destination's bits, so on Windows a failed
os.replace over a read-only report leaves a read-only stage the TemporaryDirectory
cleanup refuses to delete, masking the real error with a cleanup failure. The
cleanup now makes the staged file writable before deleting and lets one delete
failure go, so the caller reports the replace; a successful publish is
unchanged, and the prior report stays byte-for-byte intact on failure. Same
shape as navmesh_index's and bvh_index's staging cleanup.

Regression test in the style of the failed-replace tests there: mocks the
replace as failing over a 0o444 report and fails against the old code on the
made-deletable assertion. Synthetic archives only; no game data.

Claimed in the coordination issue as STORY-INDEX-STAGE-CLEANUP (issue #14).
AI-assisted.
2026-11-25 20:38:26 +01:00
0455bed876 Reserve the carve manifest path for page and story indexes 2026-11-22 19:15:38 -03:00
73bd50e269 Merge pull request #2726 from Rafael 2026-11-22 15:11:30 -05:00
20ac4599cc Merge pull request #2639 from Kacper Serewis 2026-11-22 13:16:43 -05:00
f206d479a6 tools: classify the gap between streams without copying it
Nine archive scanners sliced every gap out of the mmap to test whether it
was all zero (`gap = m[last:s]; gap.count(0) == len(gap)`). The slice is a
copy, so a gap of N bytes allocated N bytes at once; about half of
story000.rpf (~20 GB) is non-zlib, which ends in MemoryError on a 16 GB
machine.

tools/zero_span.py compares the gap with zeros 4 MiB at a time and stops at
the first non-zero window, with the same counts (all of a gap is padding
when every byte is zero, otherwise all of it is unread). rpf9_carve,
rpf9_names, bvh_index, navmesh_index, navmesh_decode, rbf0_index,
anim_index, restofworld_index and story_index use it.

tests/test_zero_span.py checks the counts against the old expression and
runs each scanner on a 48 MiB gap under tracemalloc: 50-51 MiB heap peak
on main, under 24 MiB with this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:07:15 -03:00
27546ddc2b story_index: validate the 0DRP auxiliary table before labelling it clip-stream
story_index gave any stream starting with a u32 of 2 or 3 and the bytes
01 01 00 00 the historical clip-stream signature, reported the count as a
version and called it a 24-byte header, even when only the eight-byte
prefix was present.

Reuse anim_index.aux_table from #2496 (u32 count 2 or 3, count
{u32 tag, u32 payloadBytes} pairs with first tag 0x101, every payload
inside the stream) and report the same record as anim_index: signature
clip-stream, count = entry count, no version, detail with the tags and
payload end. Streams that fail the check fall through to the generic
zero-prefix/text/raw handling. The tool still runs with python -I; the
other classes and the scan, directory and output paths are unchanged.

Synthetic tests cover the record, parity with anim_index, exact fit,
trailing bytes, payload overrun and oversized sizes, the bare and
truncated old prefix, wrong counts or first tags, and an archive scan.

Claim: https://github.com/ItsNotRexed/Leonida/issues/2300#issuecomment-7082717677
Depends on: #2496

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:22:41 +01:00
8e0f861199 story_index, anim_index, restofworld_index: control bytes are not text
The three tools share `_printable`, whose test was `9 <= b < 127 or b in (10, 13)`. The second half can never
add anything (10 and 13 are already in 9..126), and the first half counts the C0 control bytes 0x0B, 0x0C and
0x0E..0x1F as printable, so a binary stream made of them was classified "text" instead of `raw:<hex>`. The test
is now printable ASCII plus tab, LF and CR, as in `gamedata_index.printable`.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ABh36DZ2sZNq3ChN6EZJs
2026-11-22 19:43:51 +03:00
ec9b6cf22b tools: text reports are written with LF line ends on every platform (BUILD-468)
open(path, "w") without newline= turns each \n into \r\n on Windows, so the same JSON report, manifest or assembly
listing was CRLF there and LF elsewhere. 91 text-mode writes (open, os.fdopen, Path.write_text) under tools/, scripts/,
make_game.py and runtime/test/shaders/ now pass newline="\n". tests/test_text_write_line_endings.py reads the source
with ast and fails for a writer that forgets it; CONTRIBUTING.md names the convention.

AI-assisted (Claude), on behalf of dev-ewann.
2026-11-22 14:20:30 +00:00
47ebcc6799 Merge pull request #1573 from bobcatchris 2026-11-22 08:38:06 -05:00
c334c78477 tools: CSV reports end their lines with LF on Windows too
csv.writer and csv.DictWriter default to lineterminator="\r\n", and every
one of these tools opens its report with newline="", which turns that into
CRLF on Windows and LF elsewhere: the same archive gave different report
bytes depending on the machine that exported it. awc_manifest and awc_shorts
already pin "\n" and bink_index documents why its stdout writer needs it, so
this brings the remaining 16 writers onto the same convention. A structural
test keeps new writers on it, and three tools are run end to end to check the
bytes.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-22 07:13:06 -06:00
f2ef4c318a story_index: document hard links after report replacement
AI-assisted with DeepSeek V4 Pro. Verified with synthetic tests on macOS arm64.
2026-11-22 13:58:40 +01:00
e1da5a1a9b story_index.py: retain the previous report after a failed write 2026-11-22 13:12:19 +01:00
f698bf2206 tools: a path outside the console's code page no longer crashes a tool's output
On Windows a redirected stdout uses the console's ANSI code page, and python -I ignores PYTHONUTF8 and
PYTHONIOENCODING, so a caller path with a character outside that code page ended a run with
UnicodeEncodeError as soon as a tool printed it. #1406 fixed make_game.py; every CLI tool in tools/ now starts
its __main__ block the same way (stdout errors="replace").

tests/test_tool_stdout_encoding.py checks every tool's __main__ block and reruns two measured crashes
(analyze_4240_full.py, meshlet_page.py) with a private-use-named input. Found by @abilliontokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 11:27:00 +00:00
b7d70d0c29 story_index.py: refuse report paths that overwrite source archives or carved inputs 2026-11-22 16:31:40 +05:30
d7ed514a39 story_index: report filesystem errors when writing reports 2026-11-22 04:49:34 -03:00
b32d172fd0 story_index.py: validate output path before scanning and honour --quiet 2026-11-22 06:59:17 +01:00
f2a6304080 story/anim/rbf0_index: skip the carver's index.json and .csv reports in a carve folder
In directory mode the three tools read every regular file, so rpf9_carve.py's index.json (and any
.csv report next to the streams) was counted as a payload, with a made-up offset (its position in
the listing). On a 1.0.2.1 copy's script.rpf carve (1,057 streams) each reported 1,058 files, and
the inflated total was off by exactly the index's size; on the data.rpf carve rbf0_index reported
28,510 streams for 28,509.

page_index.py and navmesh_decode.py already skip .json and .csv; the three tools now do the same.
story_index keeps skipping its own report as well (#712).

Tests: each tool gets a carve folder with three streams plus an index.json and a report.csv, and
must list only the three streams. All three fail on the old code (offsets 3 and 4, or 4 streams
for 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 7372a903afdb4b6f1a9aa7df7e60283848965dc5)
2026-11-22 00:33:52 -05:00
52b78d7141 story_index.py: a limited scan stays inside its window, and a rerun skips its own report
scan_rpf() read Bink headers and inflated zlib streams up to the end of the
archive, not the end of the --start/--limit window, so a stream crossing the
limit was counted whole and readable_fraction could exceed 1.0 (6.0 for a
2-byte window). Both now stop at the window end; a stream that doesn't fit
counts as unread.

scan_dir() indexed every regular file, so a report written into the scanned
folder was indexed as a payload on the next run. It now skips the output
file (by os.path.samestat, so ./report.json and report.json are the same).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 03:15:11 +01:00
ce37e3c9b5 bvh/page/story_index: pass --max-inflate to directory scans, reject --start/--limit for a directory
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 01:08:28 +01:00
76b7b5b329 Index tools: refuse --limit/--start values that scan nothing, survive empty archives
bvh_index, page_index, story_index, navmesh_index and psin_index:

- --limit 0 meant "no limit" (0 is falsy) and a negative --limit scanned
  nothing and still exited 0 with an empty index. --limit must now be at
  least 1 and --start 0 or more; argparse rejects anything else (rc 2).
- An empty .rpf crashed in mmap ("cannot mmap an empty file") and a file
  shorter than the header word crashed in struct.unpack_from. bvh, page and
  story now treat both as an archive with no streams.
- bvh_index directory mode: one bad archive no longer aborts the scan and
  loses the results of the others; it is reported and skipped.
- A --start past the end of the archive is a one-line error (rc 2) instead
  of a traceback (bvh also checked nothing and reported a negative span).
- page_index checks the output extension before the scan, not after it.

Docs: the accepted ranges and the empty-archive behaviour are written next
to each tool's usage in docs/{bvh,navmesh,page_directory,psin,story}.md.

Tests: synthetic cases for each tool; they fail on main and pass here.
2026-11-21 23:21:29 +01:00
cfb5f9a329 story: tighten PSIN header handling and measured field wording
Use the PSIN +4 first-section offset when it points at a known tag, keep the LSCH fallback, and test the header path. Label the RSC8 +8 field as a u32 rather than a proven size, and state that no RSC7 header was found instead of claiming absence.
2026-11-21 19:49:55 +00:00
ec1dbca94f Docs: measure story and minigame archive composition
Index story000, shared_minigame000, and their script/restofworld/bvh4 companions with the fixed carver. Record the measured PSIN section chain, 0DRP page headers, RSC8 v8001 headers, nested RPF9 sub-archives, Bink 2 video and the bounded story000 layout. Add a stdlib story_index.py and synthetic-header tests; no game payload or name is committed.
2026-11-21 19:46:43 +00:00