Skip to content

Shader split: open HAL's .cmp containers - #203

Closed
marcolaux wants to merge 1 commit into
seedds:mainfrom
marcolaux:fix/hal-cmp-shader-containers
Closed

marcolaux wants to merge 1 commit into
seedds:mainfrom
marcolaux:fix/hal-cmp-shader-containers

Conversation

@marcolaux

Copy link
Copy Markdown
Contributor

Summary

HAL keeps its assets in .cmp containers: a 4-byte little-endian uncompressed size, then a frame (zstd, or LZ4 in the ubin/ map data). The split decompressed a zstd file by its magic at offset 0, so a frame that starts at offset 4 was read raw, and the shaders inside the BFRES archives of those containers were never seen.

On Kirby and the Forgotten Land this is the difference between 121 shaders found and 27,288, and between a game that plays and one that shows a black screen: the game asked for 900 shaders the map did not have, and the log filled with [RED] shader ... is not in the shader map.

The split now opens a size-prefixed frame (unzs_bytes for zstd, unlz4_bytes for LZ4). The size prefix is checked against the decompressed length, so a file that is not a container is left alone, and a container that does not open is reported instead of being passed over in silence.

Evidence

  • Before: the split found 121 shaders in Kirby and the Forgotten Land; the game asked for 900 it did not have.
    After: the split finds 27,288 — 18,476 in gfx/, 8,313 in ptcl/, 383 in lyt/, 67 in the loose shader files, 54 in main.
  • The 7,201 .cmp files of the game's RomFS: 5,121 zstd frames with 19,364 BNSH inside (11.2 GB decompressed), 2,080 LZ4 frames (0.3 GB, no BNSH — the ubin/ map data). All 7,201 open; the length check holds for every one of them.
  • No other game on hand has such a file: Super Mario Bros. Wonder (v0 and v65536) and Yoshi's Crafted World (v1.0.1) have none, so the new branch does nothing for them.
  • The chain version is unchanged: shader_split.py is not one of the chain's tools (dm/gfx/nvn/dmsh_chain.h needs no rewrite, and no shader library is invalidated by the change).
  • All 15 test files in dm/tools/tests pass. The docs entry is in docs/reference/shaders.md and the symptom index is regenerated with dm/tools/docs_symptoms.py.

Merge Danger

Door: two-way

Revertible tool change. A revert puts the split back to reading .cmp containers raw, which is how it was.

Blast Radius: the shader split

Only files whose bytes 4 to 8 are a zstd or LZ4 frame magic are looked at, and only when their first four bytes are not the zstd magic itself; the decompressed length must match the size prefix, so a file that is not a container is left alone. A container that cannot be opened (a frame that needs a zstd dictionary Pack/ZsDic.pack.zs does not have) no longer stops the split: the file is read as it is and the split says so.

No game binaries, keys, saves or raw game logs are committed.

HAL keeps its assets in .cmp containers: a 4-byte little-endian uncompressed
size, then a frame (zstd, or LZ4 in the ubin/ map data). The split
decompressed a zstd file by its magic at offset 0, so a frame that starts at
offset 4 was read raw, and the shaders inside the BFRES archives of those
containers were never seen.

On Kirby and the Forgotten Land this is the difference between 121 shaders
found and 27,288, and between a game that plays and one that shows a black
screen: the game asked for 900 shaders the map did not have, and the log
filled with "[RED] shader ... is not in the shader map".

The split now opens a size-prefixed frame (unzs_bytes for zstd, unlz4_bytes
for LZ4). The size prefix is checked against the decompressed length, so a
file that is not a container is left alone, and a container that does not
open is reported instead of being passed over in silence.

- Found on: Kirby and the Forgotten Land (01004D300C5AE000, in bring-up):
  7,201 .cmp files, 5,121 zstd frames with 19,364 BNSH inside (11.2 GB
  decompressed) and 2,080 LZ4 frames (0.3 GB, no BNSH). The split now finds
  27,288 shaders: 18,476 in gfx/, 8,313 in ptcl/, 383 in lyt/, 67 in the
  loose shader files, 54 in main.
- No other game on hand has such a file: Super Mario Bros. Wonder (v0 and
  v65536) and Yoshi's Crafted World (v1.0.1) have none.
- The chain version is unchanged: shader_split.py is not one of the chain's
  tools (dm/gfx/nvn/dmsh_chain.h needs no rewrite).
- The docs entry is in docs/reference/shaders.md; the symptom index is
  regenerated. All 15 test files in dm/tools/tests pass.

Co-Authored-By: Claude Code <[email protected]>
@marcolaux

Copy link
Copy Markdown
Contributor Author

fixes #156

seedds added a commit that referenced this pull request Oct 26, 2026
…tAsset reads (#203, #238)

#203 as submitted stopped the shader split of Tears of the Kingdom with a
TypeError: its 1,202 Cave/.../C.crbin.<hash>/*.quad files are a 4-byte hash
(the one in their folder name), then a zstd frame, so they pass the gate,
and on the length mismatch the error path took len() of None. The PR checked
Wonder and Yoshi only; of the RomFS of 39 games on hand, only those files
have a frame at +4.

- unsized: a frame whose header records a size other than the prefix is not
  such a container and is read as it is, without decompressing it
  (frame_size). A frame that records none (Kirby's LZ4 frames) must
  decompress to exactly the prefix's size: the whole output is compared,
  where the PR's max_length cut a longer frame at the prefix and let it pass.
- The dictionary handling is shared with unzs (zs_dict_id, unzs_dict). A
  dictionary ZDICTS does not have is still reported, not fatal, as in the PR:
  the ID may be any bytes after a magic at +4.
- unityfs.text_assets: an object shorter than the two length fields, or one
  whose name padding leaves no room for the script's length, is left out
  instead of reading past its end (struct.error, which stopped the split).

Checked:
- Kirby and the Forgotten Land (base and update 1.1.0): 121 shaders before,
  27,288 after (27,302 with the update), no [RED] line. 5,121 zstd frames
  record their size, equal to the prefix; 2,080 + 354 LZ4 frames record none
  and decompress to the prefix's size. The PR's "383 in lyt/" is 378.
- Tears of the Kingdom: the full split exits 1 with #203 alone; with this
  commit its package and tables are byte-identical to main's, no [RED] line.
- main's split and this branch's give byte-identical packages and tables on
  Monster Train 2, Ace Attorney Investigations Collection, AI: THE SOMNIUM
  FILES, nirvanA Initiative, BALL x PIT, Advance Wars 1+2, Brain Memory
  Training Academy (Unity 2017.4 to 6000) and Super Mario Bros. Wonder.
- New test_size_prefixed_frames.py; test_unity_text_glslc.py covers short
  objects (fails on #238's unityfs.py). All of dm/tools/tests pass.
- Not checked here: FINAL FANTASY CRYSTAL CHRONICLES (#238's game) is not on
  hand; Kirby has not been run.
@seedds

seedds commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thank you, this is on main as 7c5b0b2, with a follow-up in eb858ec.

Checked here on Kirby and the Forgotten Land (base and 1.1.0): the split finds 121 shaders before and 27,288 after (27,302 with the update), with no [RED] line; every .cmp container opens to exactly its prefix's size. One number in the description was off: lyt/ has 378, which is what makes the total of 27,288.

The follow-up fixes a crash this branch caused on another game. Tears of the Kingdom has 1,202 files (Cave/…/C.crbin.<hash>/*.quad) that are a 4-byte hash followed by a zstd frame, so they pass the gate. On the length mismatch the error path ran dec = None; why = f'… {len(dec)}', and the TypeError stopped the whole split (exit 1, no tables). The split now first compares the prefix with the size the frame header records, and reads a file whose numbers disagree as it is, without decompressing it. A frame that records no size (Kirby's LZ4 frames) is decompressed and its whole output compared; max_length used to cut a longer frame at the prefix and let it pass. The zstd dictionary handling is shared with unzs now. With the follow-up, Tears of the Kingdom's split is byte-identical to main's, and so are those of eight other games on hand.

Kirby has not been run here yet, so #156 stays open.

@seedds seedds closed this Oct 26, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…tAsset reads (seedds#203, seedds#238)

seedds#203 as submitted stopped the shader split of Tears of the Kingdom with a
TypeError: its 1,202 Cave/.../C.crbin.<hash>/*.quad files are a 4-byte hash
(the one in their folder name), then a zstd frame, so they pass the gate,
and on the length mismatch the error path took len() of None. The PR checked
Wonder and Yoshi only; of the RomFS of 39 games on hand, only those files
have a frame at +4.

- unsized: a frame whose header records a size other than the prefix is not
  such a container and is read as it is, without decompressing it
  (frame_size). A frame that records none (Kirby's LZ4 frames) must
  decompress to exactly the prefix's size: the whole output is compared,
  where the PR's max_length cut a longer frame at the prefix and let it pass.
- The dictionary handling is shared with unzs (zs_dict_id, unzs_dict). A
  dictionary ZDICTS does not have is still reported, not fatal, as in the PR:
  the ID may be any bytes after a magic at +4.
- unityfs.text_assets: an object shorter than the two length fields, or one
  whose name padding leaves no room for the script's length, is left out
  instead of reading past its end (struct.error, which stopped the split).

Checked:
- Kirby and the Forgotten Land (base and update 1.1.0): 121 shaders before,
  27,288 after (27,302 with the update), no [RED] line. 5,121 zstd frames
  record their size, equal to the prefix; 2,080 + 354 LZ4 frames record none
  and decompress to the prefix's size. The PR's "383 in lyt/" is 378.
- Tears of the Kingdom: the full split exits 1 with seedds#203 alone; with this
  commit its package and tables are byte-identical to main's, no [RED] line.
- main's split and this branch's give byte-identical packages and tables on
  Monster Train 2, Ace Attorney Investigations Collection, AI: THE SOMNIUM
  FILES, nirvanA Initiative, BALL x PIT, Advance Wars 1+2, Brain Memory
  Training Academy (Unity 2017.4 to 6000) and Super Mario Bros. Wonder.
- New test_size_prefixed_frames.py; test_unity_text_glslc.py covers short
  objects (fails on seedds#238's unityfs.py). All of dm/tools/tests pass.
- Not checked here: FINAL FANTASY CRYSTAL CHRONICLES (seedds#238's game) is not on
  hand; Kirby has not been run.
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 14, 2026
… Kong Bananza speed fix, bundled font, Ocarina of Time cave walls option

Fixes seedds#214, seedds#207, seedds#223, seedds#203. The Legend of Zelda: Ocarina of Time (0400486024DEC000): Keep cave walls (Qualcomm) is an option, off by default.
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.

2 participants