wow-addons/ShipRoutes/tools/casc.py
rope 63eebfc8bd Add Ship Routes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:19:19 +00:00

139 lines
4.2 KiB
Python

"""Minimal local CASC reader: extract files by FileDataID.
usage: python3 casc.py <WoW dir> <product> <outdir> <fdid> [fdid...]"""
import os, sys, struct, zlib
wow, product, outdir = sys.argv[1], sys.argv[2], sys.argv[3]
fdids = [int(a) for a in sys.argv[4:]]
data = os.path.join(wow, "Data")
# .build.info -> build key
lines = open(os.path.join(wow, ".build.info")).read().splitlines()
cols = [c.split("!")[0] for c in lines[0].split("|")]
build_key = None
for l in lines[1:]:
row = dict(zip(cols, l.split("|")))
if row.get("Product") == product:
build_key = row["Build Key"]
print("version", row["Version"], file=sys.stderr)
assert build_key, "product not found"
def cfg(key):
p = os.path.join(data, "config", key[:2], key[2:4], key)
out = {}
for l in open(p):
if " = " in l:
k, v = l.strip().split(" = ", 1)
out[k] = v.split()
return out
build = cfg(build_key)
# local indices: highest version per bucket
idx = {}
best = {}
for f in os.listdir(os.path.join(data, "data")):
if f.endswith(".idx"):
b, v = int(f[:2], 16), int(f[2:10], 16)
if b not in best or v > best[b][0]:
best[b] = (v, f)
for _, f in best.values():
raw = open(os.path.join(data, "data", f), "rb").read()
hsize = struct.unpack_from("<I", raw, 0)[0]
pos = (8 + hsize + 0x0F) & ~0x0F
esize = struct.unpack_from("<I", raw, pos)[0]
pos += 8
for i in range(esize // 18):
e = raw[pos + i * 18: pos + i * 18 + 18]
key = e[:9]
off = int.from_bytes(e[9:14], "big")
size = struct.unpack_from("<I", e, 14)[0]
idx.setdefault(key, (off >> 30, off & 0x3FFFFFFF, size))
def blte(b):
assert b[:4] == b"BLTE", b[:4]
hsize = struct.unpack_from(">I", b, 4)[0]
if hsize == 0:
chunks = [len(b) - 8]
pos = 8
else:
n = int.from_bytes(b[9:12], "big")
chunks = [struct.unpack_from(">I", b, 12 + i * 24)[0] for i in range(n)]
pos = hsize
out = bytearray()
for c in chunks:
ch = b[pos:pos + c]
pos += c
m = ch[:1]
if m == b"N":
out += ch[1:]
elif m == b"Z":
out += zlib.decompress(ch[1:])
elif m == b"E":
raise ValueError("encrypted chunk")
else:
raise ValueError("chunk mode %r" % m)
return bytes(out)
def read_ekey(ekey):
a, off, size = idx[ekey[:9]]
with open(os.path.join(data, "data", "data.%03d" % a), "rb") as fh:
fh.seek(off)
b = fh.read(size)
return blte(b[30:])
# encoding: ckey -> ekey
enc = read_ekey(bytes.fromhex(build["encoding"][1]))
assert enc[:2] == b"EN"
ck, ek = enc[3], enc[4]
cepage = struct.unpack_from(">H", enc, 5)[0] * 1024
cecount = struct.unpack_from(">I", enc, 9)[0]
espec = struct.unpack_from(">I", enc, 18)[0]
pos = 22 + espec + cecount * 32
ckey2ekey = {}
for p in range(cecount):
page = enc[pos + p * cepage: pos + (p + 1) * cepage]
q = 0
while q + 6 + ck <= len(page):
n = page[q]
if n == 0:
break
c = page[q + 6: q + 6 + ck]
ckey2ekey[c] = page[q + 6 + ck: q + 6 + ck + ek]
q += 6 + ck + ek * n
# root (MFST): fdid -> ckey (prefer enUS / all-locale)
root = read_ekey(ckey2ekey[bytes.fromhex(build["root"][0])])
assert root[:4] == b"TSFM", root[:4]
hs, ver = struct.unpack_from("<II", root, 4)
if hs == 0x18:
pos = hs
else:
ver, pos = 0, 12
want = set(fdids)
found = {}
while pos < len(root):
if ver >= 2:
n, locale, u1, u2 = struct.unpack_from("<IIII", root, pos)
u3 = root[pos + 16]
content = u1 | u2 | (u3 << 17)
pos += 17
else:
n, content, locale = struct.unpack_from("<III", root, pos)
pos += 12
deltas = struct.unpack_from("<%di" % n, root, pos)
pos += 4 * n
keys = root[pos: pos + 16 * n]
pos += 16 * n
if not (content & 0x10000000):
pos += 8 * n
fd = -1
for i, d in enumerate(deltas):
fd = fd + 1 + d
if fd in want and (locale & 0x2 or fd not in found):
found[fd] = keys[i * 16:(i + 1) * 16]
os.makedirs(outdir, exist_ok=True)
for fd in fdids:
b = read_ekey(ckey2ekey[found[fd]])
open(os.path.join(outdir, "%d.db2" % fd), "wb").write(b)
print(fd, len(b), b[:4], file=sys.stderr)