Read and patch UE3 packages and soundbanks

This commit is contained in:
2026-10-09 19:38:57 +02:00
parent b3c9b84dc7
commit 61bbf26f3b
10 changed files with 734 additions and 11 deletions

View File

@@ -89,6 +89,72 @@ def pack(media):
return bytes(hdr) + bank, bytes(payload)
def bank(bank_id, media, name=None, extra_sources=()):
"""A v56 soundbank holding `media` {id: wem} in memory (DIDX/DATA, 16-byte aligned) with one HIRC
Sound object per media; extra_sources: [(media id, bank id, off, size)] for sources of other banks."""
data = bytearray()
didx = b""
for m in sorted(media):
data += bytes(_a(len(data), 16) - len(data))
didx += struct.pack("<III", m, len(data), len(media[m]))
data += media[m]
bkhd = struct.pack("<II", 56, bank_id) + bytes(8)
head = b"BKHD" + struct.pack("<I", len(bkhd)) + bkhd + b"DIDX" + struct.pack("<I", len(didx)) + didx
data_pos = len(head) + 8 # DATA payload, from the start of the bank
objs = []
for i, m in enumerate(sorted(media)):
off = struct.unpack_from("<I", didx, 12 * i + 4)[0]
objs.append((m, bank_id, data_pos + off, len(media[m])))
objs += list(extra_sources)
hirc = struct.pack("<I", len(objs))
for i, (m, b, off, size) in enumerate(objs):
body = struct.pack("<7I", 0x100 + i, 0x00040001, 0, m, b, off, size) + bytes(6)
hirc += bytes([2]) + struct.pack("<I", len(body)) + body
out = head + b"DATA" + struct.pack("<I", len(data)) + bytes(data) + b"HIRC" + struct.pack("<I", len(hirc)) + hirc
if name:
nm = name.encode()
stid = struct.pack("<II", 1, 1) + struct.pack("<IB", bank_id, len(nm)) + nm
out += b"STID" + struct.pack("<I", len(stid)) + stid
return out
def package(path, body, block_size=0x1000, chunk_blocks=3):
"""A fully LZO-compressed UE3 package (docs/formats/upk.md) whose uncompressed data area is `body`.
Small blocks so that tests cross block and chunk boundaries."""
from ackit import lzo
folder = b"None\x00"
nblocks = max(1, _a(len(body), block_size) // block_size)
nchunks = _a(nblocks, chunk_blocks) // chunk_blocks
def summary(chunks):
s = struct.pack("<IHHi", 0x9E2A83C1, 805, 101, 0) + struct.pack("<i", len(folder)) + folder
s += struct.pack("<12i", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + b"GUID-synthetic!!"
s += struct.pack("<i3i", 1, 0, 0, 0) + struct.pack("<3I", 7748, 0, 2)
s += struct.pack("<i", len(chunks)) + b"".join(struct.pack("<4i", *c) for c in chunks)
return s + struct.pack("<Ii", 0x12345678, 0)
header_u = len(summary([])) + 16 # uncompressed header (some spare bytes)
chunks, blobs = [], []
coff = len(summary([(0, 0, 0, 0)] * nchunks)) + 16
u = header_u
for c in range(nchunks):
part = body[(u - header_u):(u - header_u) + block_size * chunk_blocks]
blocks = [part[i:i + block_size] for i in range(0, len(part), block_size)] or [b""]
comp = [lzo.compress_stored(b) for b in blocks]
hdr = struct.pack("<4I", 0x9E2A83C1, block_size, sum(map(len, comp)), len(part))
hdr += b"".join(struct.pack("<2I", len(cb), len(b)) for cb, b in zip(comp, blocks))
blob = hdr + b"".join(comp)
chunks.append((u, len(part), coff, len(blob)))
blobs.append(blob)
coff += len(blob)
u += len(part)
s = summary(chunks)
out = s + bytes(16) + b"".join(blobs)
assert len(s) + 16 == chunks[0][2]
os.makedirs(os.path.dirname(path), exist_ok=True)
open(path, "wb").write(out)
return header_u
def make_game(root):
"""Create the folder; returns the dict of original WEMs by target."""
cooked = os.path.join(root, "BmGame", "CookedPCConsole")