Skip to content

tools: stream game-data XML events by root and type (ASSET-139) - #2278

Closed
kacper-serewis wants to merge 1 commit into
ItsNotRexed:mainfrom
kacper-serewis:asset139-streaming-reader
Closed

kacper-serewis wants to merge 1 commit into
ItsNotRexed:mainfrom
kacper-serewis:asset139-streaming-reader

Conversation

@kacper-serewis

Copy link
Copy Markdown
Contributor

Claimed as ASSET-139: #2078 (comment)

Publication requested by the coordinator after reconciling the overlap with #2238: #2078 (comment). This PR is offered for that comparison; it does not depend on #2238, #1995 or #2268.

What and why

Add a stdlib streaming event API for game-data XML. read_xml accepts byte chunks and dispatches start/text/end events by document root and each element's own type, with root/default fallbacks. It preserves values without schema conversion guesses and retains open-element metadata rather than a tree. Attributes are read-only snapshots. Tail text belongs to the parent; text chunks can split arbitrarily.

DTD declarations are refused. Malformed/unsupported XML raises a sanitized error; consumer exceptions propagate unchanged. Callbacks are explicitly provisional until parsing completes, so consumers must discard partial state after an error. The CLI reads local XML files and emits aggregate counts only. Documentation describes the API, memory/depth limits and archive-grouping integration; runtime consumers and RTTI mapping remain future work.

Tested

Windows 11 Pro 10.0.26200, Python 3.13.15. CPU-only; no GPU required or exercised (host RTX 5070, driver 32.0.16.1656).

  • python -I tests/test_gamedata_reader.py: 11 passed, including dispatch precedence, mixed content, single-byte UTF-8/UTF-16 chunking, namespaces, malformed input, unsupported encodings, DTD refusal, callback exceptions, partial delivery and memory across 20,000 siblings.
  • test_gamedata_index.py (19), test_gamedata_xml.py (6), test_docs_links.py (5), test_text_write_line_endings.py (2): all passed.
  • python -m compileall -q make_game.py tools tests and git diff --check: passed.
  • Complete local common.rpf XML corpus: 267 recovered payloads, 259 accepted and 8 rejected, zero acceptance or metadata disagreements with gamedata_xml.scan_xml. Accepted documents contain 428,611 elements, 7,181 typed elements, 111 distinct roots, maximum depth 17. Counts include the complete recovered XML corpus, not a sample. Game data and per-document reports remain local.

Checklist

  • No game data, keys or decryption, including in quoted reports. Fixtures are self-authored and validation shares aggregate counts only.
  • AI-assisted implementation and validation (Codex).
  • Translator contract update: not applicable; no translator changes.

Add a stdlib event reader, synthetic dispatch and memory tests, and aggregate common.rpf validation documentation. AI-assisted. No game data included.
@leoleahy

Copy link
Copy Markdown
Contributor

Independent review & local CPU verification (AI-assisted) on Windows 11 x64 / Python 3.14.8 against commit a3e17412:

Tests run locally:

  • python -I tests/test_gamedata_reader.py -v -> 11/11 OK (0.277s)
  • python -I tests/test_docs_links.py -> 5/5 OK
  • python -I tests/test_text_write_line_endings.py -> 2/2 OK

Code & diff review (tools/gamedata_reader.py, tests/test_gamedata_reader.py, docs/gamedata.md):

  • read_xml dispatches start/text/end events with exact (root, type) > (root, None) > default precedence, keeps missing vs empty type distinct without inheritance, exposes attributes via MappingProxyType, and translates Expat namespace_separator="}" names into {uri}local notation.
  • guarded cleanly separates consumer exceptions (including consumer-raised ValueError, LookupError, or expat.ExpatError) from parser failures so consumer exceptions propagate unchanged while malformed input raises XmlReadError with line/column only (no input text).
  • Refuses DTDs, enforces max_depth, does not close caller streams, and keeps traced memory bounded across 20,000 sibling elements.
  • CLI outputs aggregate counts only and exits non-zero on invalid or unreadable inputs; tests are purely synthetic.

No issues found; looks ready to merge from a CPU/code-read perspective.

@mrp1no

mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent check of a3e1741244d89718fcd10c5bf89f729e1fac8d82 on my own Xbox Series 1.0.2.1 copy (Windows 11 Pro 26300, Python 3.15.2, CPU only): pass, the corpus figures reproduce exactly.

rpf9_carve.py common.rpf, then every XML payload gamedata_xml.load_documents rebuilds from the carve, fed to read_xml in 4 KiB chunks with only a default handler, and compared with gamedata_xml.scan_xml on the same bytes (a throwaway script, not in the PR):

  • 267 payloads: 259 accepted, 8 rejected (all XmlReadError);
  • 0 acceptance disagreements with scan_xml; for the accepted ones, 0 root and 0 element-count disagreements;
  • 428,611 elements, 7,181 typed elements, 111 distinct roots, maximum depth 17.

gamedata_xml.py --check on the same carve agrees: 267 documents, 259 well formed. python -I tests/test_gamedata_reader.py: 11 OK. Nothing from the package is attached. AI-assisted (Claude Code).

@jbot1324

Copy link
Copy Markdown

AI-assisted code read (Claude), not run. Looks correct. One small diagnostic issue.

The dispatch precedence, provisional-event contract, DTD refusal and callback-error passthrough (callback_error is exc) all do what the docs say, and the tests cover them well.

Nit: errors from the chunk iterator are reported as malformed XML. The for chunk in chunks loop is inside the same try that maps ValueError and LookupError to XmlReadError("invalid XML at line N, column M"). If the caller's chunk source raises a ValueError (for example, reading a closed file: ValueError: I/O operation on closed file, or a decompression error that subclasses ValueError in an ArchiveSource), it is reported as invalid XML with a meaningless line and column, and the cause is dropped (from None). Suggest pulling the next(chunk) out of the try so only parser.Parse errors are translated, or re-raising non-parser exceptions unchanged. OSError already passes through correctly, which is why the CLI's unreadable count works.

@ItsNotRexed

Copy link
Copy Markdown
Owner

Closing as a duplicate: the streaming game-data XML reader (ASSET-139) landed on main via #2238. Thanks!

@JRamos1993

Copy link
Copy Markdown
Contributor

Ran this on main 4947579 (Windows 11, Python 3.14). It merges cleanly.

  • Tests: test_gamedata_reader.py 11 OK. Also OK: test_gamedata_index.py 21, test_gamedata_xml.py 7, test_docs_links.py 5, test_text_write_line_endings.py 2.
  • Mutation check: each of six mutations of read_xml fails the tests: route precedence, DTD refusal, callback-error passthrough, depth limit, {uri}local names, and text routing.

One failure: python -I tests/test_tool_stdout_encoding.py fails with ['gamedata_reader.py']. The __main__ block (tools/gamedata_reader.py:176) needs sys.stdout.reconfigure(errors="replace"). Same on the head as-is.

jbot1324's nit reproduces: chunks from a closed stream give XmlReadError: invalid XML at line 1, column 0, with the ValueError dropped (the chunk loop sits inside the mapped try, :130/:139).

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.

6 participants