diff --git a/README.md b/README.md index 50dc0f3..0ce398b 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ the RNC unpacker work for any console. The interface may change before 1.0. ## Use ```bash -uv add "decompassets @ https://github.com/RetroReversing/decomp-assets/archive/refs/tags/v0.9.0.tar.gz" +uv add "decompassets @ https://github.com/RetroReversing/decomp-assets/archive/refs/tags/v0.10.0.tar.gz" ``` ```python diff --git a/pyproject.toml b/pyproject.toml index 1257728..846f541 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "decompassets" -version = "0.9.0" +version = "0.10.0" description = "Find and preview game assets (textures, audio banks, models, fonts) inside cartridge and disc images by their structure, with no per-game offsets." readme = "README.md" license = "AGPL-3.0-only" diff --git a/src/decompassets/__init__.py b/src/decompassets/__init__.py index 68079ef..eaba270 100644 --- a/src/decompassets/__init__.py +++ b/src/decompassets/__init__.py @@ -10,4 +10,4 @@ Everything here reads bytes and returns plain dicts. It never writes a game file, runs a tool or downloads. """ -__version__ = "0.9.0" +__version__ = "0.10.0" diff --git a/src/decompassets/n64/__init__.py b/src/decompassets/n64/__init__.py index 66dc94d..300bc9f 100644 --- a/src/decompassets/n64/__init__.py +++ b/src/decompassets/n64/__init__.py @@ -8,6 +8,7 @@ - rare_models.py: model textures in a deflated asset table. - blast_textures.py: textures packed with a demolition game's own codecs, listed in its texture table. - mainfs.py: image packs and model bitmaps in a party-game series' main filesystem (three packings). +- sno.py: sno unpacking and its texture sheets, read where a caller knows a stream's start and size. - reloc_files.py: textures and sprites in a table of relocatable files, some VPK0-packed (vpk0.py). - uv_textures.py: textures in a UV filesystem of FORM files, drawn by each file's own display list. - audio.py: libultra B1 sound banks, their sample tables and mono VADPCM previews. diff --git a/src/decompassets/n64/sno.py b/src/decompassets/n64/sno.py new file mode 100644 index 0000000..7071286 --- /dev/null +++ b/src/decompassets/n64/sno.py @@ -0,0 +1,64 @@ +"""Sno, the packing a snowboarding sequel uses, and the texture sheets it packs. + +Sno has no header: a stream of byte pairs, (0, byte) a literal, else a run (count = high nibble of the first, +distance = the low nibble × 256 + the second). Where a stream starts and how long it unpacks come from the game's +code, or from a project that has written them down (a decompilation's splat config, say), so it is read only where +the caller knows both: `unpack(data, size)`. Nothing scans a cartridge for it. + +Its texture sheets (sprite sheets and course texture tables) unpack to a header (a texture base and a count), +16-byte entries (texture offset, palette number, width, height, a palette table number, a format: 0 CI4, 1 CI8; +course tables are all CI4), then palette slots of 32 bytes (16 RGBA16 colours; a CI8 palette takes 16 slots). +""" +from __future__ import annotations + +import struct + +from .. import formats as asset_formats +from ..formats import AssetError + +MAX_OUTPUT = 4 * 1024 * 1024 +SLOT = 0x20 + + +def unpack(data, size): + if not 0 < size <= MAX_OUTPUT:raise AssetError('Choose an unpacked size up to 4 MiB.') + out, at = bytearray(), 0 + while len(out) < size: + if at + 1 >= len(data):raise AssetError('The sno stream ends before its size. Check the declared size.') + first, second = data[at], data[at+1];at += 2 + if not first: + out.append(second);continue + count, distance = first >> 4, ((first & 0x0F) << 8) | second + if not count or not distance or distance > len(out):raise AssetError('The sno stream is not valid here. Check the declared start.') + for _ in range(count): + out.append(out[-distance]) + if len(out) >= size:break + return bytes(out) + + +def sheet(data, *, indexed_only=False): + """A sprite sheet's or course texture table's textures as preview items relative to `data`, or None. + + `indexed_only` reads every entry as CI4 (course tables carry no format).""" + if len(data) < 8:return None + count = int.from_bytes(data[4:8], 'big') + palettes = 8 + 16*count + if not 0 < count <= 4096 or palettes > len(data):return None + items = [] + for index in range(count): + offset, palette, width, height, table, kind, _pad = struct.unpack_from('>I6H', data, 8 + 16*index) + if indexed_only:kind = 0 + if table or kind not in (0, 1) or not 0 < width <= 1024 or not 0 < height <= 1024:return None + fmt, colours = ('ci4', 16) if kind == 0 else ('ci8', 256) + size = (width*height + 1)//2 if kind == 0 else width*height + start = palettes + SLOT*palette + if offset + size > len(data) or start + 2*colours > len(data):return None + settings = dict(format=fmt, width=width, height=height, offset=offset, palette_offset=start, palette_format='rgba5551-be') + items.append(dict(preview_settings=settings, frames=1, palettes=1, frame_offsets=[offset], palette_offsets=[start], + palette_entries=[colours], levels=[dict(width=width, height=height, offset=0)])) + return items + + +def preview(data, item): + """One sheet texture from the unpacked sheet.""" + return asset_formats.preview(data, item['preview_settings']) diff --git a/tests/test_sno.py b/tests/test_sno.py new file mode 100644 index 0000000..8587303 --- /dev/null +++ b/tests/test_sno.py @@ -0,0 +1,43 @@ +"""Sno packing and its texture sheets: homemade data only.""" +import base64 +import struct + +import pytest + +from decompassets.formats import AssetError +from decompassets.n64 import sno + +from .test_rom_assets import pixels + +RED, GREEN = bytes.fromhex('f801'), bytes.fromhex('07c1') + + +def pack(raw): + return b''.join(bytes([0, value]) for value in raw) + + +def sheet(entries, palettes, texels): + """entries: (texture offset, palette, width, height, format); offsets count from the sheet's start.""" + head = struct.pack('>II', 0, len(entries)) + head += b''.join(struct.pack('>I6H', o, p, w, h, 0, f, 0) for o, p, w, h, f in entries) + return head + palettes + texels + + +def test_sno_unpacks_literals_and_runs(): + assert sno.unpack(b'\0\x01\0\x02' + bytes([0x30, 0x02]), 5) == bytes([1, 2, 1, 2, 1]) + with pytest.raises(AssetError):sno.unpack(b'\0\x01', 3) + with pytest.raises(AssetError):sno.unpack(bytes([0x31, 0x05]), 3) # refers back before the start + + +def test_sheet_textures_read_with_their_palettes(): + palettes = (RED + GREEN + RED*14) + (GREEN + RED*15) + base = 8 + 16*2 + len(palettes) + data = sheet([(base, 0, 4, 2, 0), (base + 4, 1, 2, 2, 0)], palettes, bytes([0x01, 0x10]*2) + bytes([0x00, 0x00])) + items = sno.sheet(sno.unpack(pack(data), len(data))) + assert [(i['preview_settings']['format'], i['preview_settings']['palette_offset']) for i in items] == [('ci4', 40), ('ci4', 72)] + shown = pixels(base64.b64decode(sno.preview(data, items[0])['data_url'].split(',')[1])) + assert shown[:8] == bytes([255, 0, 0, 255, 0, 255, 0, 255]) + one = 8 + 16 + len(palettes) + assert sno.sheet(sheet([(one, 0, 64, 64, 0)], palettes, bytes(8))) is None # texels past the end + assert sno.sheet(sheet([(one, 0, 4, 2, 7)], palettes, bytes(8))) is None # an unknown format + assert sno.sheet(sheet([(one, 0, 4, 2, 7)], palettes, bytes(8)), indexed_only=True) # course tables are CI4 diff --git a/uv.lock b/uv.lock index a58d006..accb743 100644 --- a/uv.lock +++ b/uv.lock @@ -51,7 +51,7 @@ wheels = [ [[package]] name = "decompassets" -version = "0.9.0" +version = "0.10.0" source = { editable = "." } [package.dev-dependencies]