Files
UnheardCity/tests/test_formats.py
2026-10-09 02:48:24 +02:00

65 lines
2.3 KiB
Python

"""Container formats: WAD round trip, stream packs, grouped entries, target reads."""
import os
from ackit.inject import read_target, inject
from ackit.pack import Pack
from ackit.wad import Wad, write_wad
from ackit.wem import split_concatenated
import synth
def _wad(root, name):
c = os.path.join(root, "BmGame", "CookedPCConsole")
return {"INT": os.path.join(c, "English(US)", "28CCF006_LOC_INT.wad"),
"SFX": os.path.join(c, "SFX", "17705D3E.wad")}[name]
def test_wad_roundtrip_identical(synth_game, tmp_path):
root, _ = synth_game
for name in ("INT", "SFX"):
src = _wad(root, name)
w = Wad(src)
assert w.check() == []
out = str(tmp_path / ("rt_%s.wad" % name))
write_wad(w, out, {})
assert open(out, "rb").read() == open(src, "rb").read()
def test_grouped_entry_scan(synth_game):
root, originals = synth_game
w = Wad(_wad(root, "INT"))
wems, _ = split_concatenated(w.read(synth.SPEECH_ENTRY))
assert len(wems) == 3
assert all(off % 0x800 == 0 for off, _ in wems)
for i in range(3):
assert read_target(_wad(root, "INT"), "%08X:%d" % (synth.SPEECH_ENTRY, i)) == originals["%08X:%d" % (synth.SPEECH_ENTRY, i)]
def test_pack_noop_rebuild_identical(synth_game):
root, originals = synth_game
wad = _wad(root, "SFX")
bnk_path = os.path.join(os.path.dirname(wad), "%08X.bnk" % synth.PACK_ID)
bnk = open(bnk_path, "rb").read()
payload = Wad(wad).read(synth.PACK_ID)
pk = Pack(bnk)
nb, npay = pk.rebuild(payload, {})
assert nb == bnk and npay == payload
assert pk.gaps(payload) # the synthetic pack has non-zero padding, like the real 'Music' pack
for m in synth.MEDIA:
t = "%08X/%08X" % (synth.PACK_ID, m)
assert read_target(wad, t) == originals[t]
def test_pack_resize_keeps_other_media(synth_game, tmp_path):
root, originals = synth_game
wad = _wad(root, "SFX")
big = synth.wem(6.0, 330, 48000, 2)
t = "%08X/%08X" % (synth.PACK_ID, synth.MEDIA[1])
out = str(tmp_path / "17705D3E.wad")
inject(wad, {t: big}, {}, out_path=out)
assert Wad(out).check() == []
assert read_target(out, t) == big
for m in (synth.MEDIA[0], synth.MEDIA[2]):
o = "%08X/%08X" % (synth.PACK_ID, m)
assert read_target(out, o) == originals[o]