96 lines
3.8 KiB
Python
96 lines
3.8 KiB
Python
"""Generic WDC5 reader: rows as {"id", "fields": [values...], "rel"}; floats left as raw u32."""
|
|
import struct
|
|
|
|
def read(path):
|
|
b = open(path, "rb").read()
|
|
assert b[:4] == b"WDC5", b[:4]
|
|
(rc, fc, rs, sts, th, lh, minid, maxid, loc) = struct.unpack_from("<9I", b, 136)
|
|
flags, id_index = struct.unpack_from("<HH", b, 172)
|
|
(tfc, bpo, lcc, fsis, cds, pds, sc) = struct.unpack_from("<7I", b, 176)
|
|
pos = 204
|
|
sections = []
|
|
for i in range(sc):
|
|
sections.append(struct.unpack_from("<Q8I", b, pos))
|
|
pos += 40
|
|
fstruct = [struct.unpack_from("<hH", b, pos + i * 4) for i in range(fc)]
|
|
pos += fc * 4
|
|
finfo = [struct.unpack_from("<HHIIIII", b, pos + i * 24) for i in range(fsis // 24)]
|
|
pos += fsis
|
|
pallet_start = pos
|
|
pos += pds
|
|
common_start = pos
|
|
pos += cds
|
|
# per-field pallet/common offsets
|
|
pal_off, com = [], []
|
|
po, co = pallet_start, common_start
|
|
for (fo, fs, ads, comp, v1, v2, v3) in finfo:
|
|
pal_off.append(po if comp in (3, 4) else None)
|
|
if comp in (3, 4):
|
|
po += ads
|
|
if comp == 2:
|
|
d = {}
|
|
for k in range(ads // 8):
|
|
i_, v_ = struct.unpack_from("<II", b, co + k * 8)
|
|
d[i_] = v_
|
|
com.append(d)
|
|
co += ads
|
|
else:
|
|
com.append(None)
|
|
# encrypted id lists
|
|
for s in sections:
|
|
if s[0] != 0:
|
|
n = struct.unpack_from("<I", b, pos)[0]
|
|
pos += 4 + 4 * n
|
|
rows = []
|
|
for (tact, off, src, ssz, ore, idls, rels, omc, ctc) in sections:
|
|
if tact != 0 and all(x == 0 for x in b[off:off + 16]):
|
|
continue
|
|
p = off
|
|
recs = b[p:p + src * rs]
|
|
p += src * rs + ssz
|
|
ids = list(struct.unpack_from("<%dI" % (idls // 4), b, p)) if idls else None
|
|
p += idls
|
|
copies = [struct.unpack_from("<II", b, p + k * 8) for k in range(ctc)]
|
|
p += ctc * 8 + omc * 6
|
|
rel = {}
|
|
if rels:
|
|
n = struct.unpack_from("<I", b, p)[0]
|
|
for k in range(n):
|
|
fid, ri = struct.unpack_from("<II", b, p + 12 + k * 8)
|
|
rel[ri] = fid
|
|
for r in range(src):
|
|
rec = int.from_bytes(recs[r * rs:(r + 1) * rs], "little")
|
|
vals = []
|
|
for f, (fo, fs, ads, comp, v1, v2, v3) in enumerate(finfo):
|
|
raw = (rec >> fo) & ((1 << fs) - 1)
|
|
if comp == 0:
|
|
n = max(1, fs // 32)
|
|
v = [(raw >> (32 * k)) & 0xFFFFFFFF for k in range(n)] if fs >= 32 else [raw]
|
|
vals.append(v if len(v) > 1 else v[0])
|
|
elif comp == 1:
|
|
vals.append(raw)
|
|
elif comp == 5:
|
|
vals.append(raw - (1 << fs) if raw >> (fs - 1) else raw)
|
|
elif comp == 2:
|
|
vals.append(None) # filled after id known
|
|
elif comp == 3:
|
|
vals.append(struct.unpack_from("<I", b, pal_off[f] + raw * 4)[0])
|
|
elif comp == 4:
|
|
vals.append(list(struct.unpack_from("<%dI" % v3, b, pal_off[f] + raw * 4 * v3)))
|
|
rid = ids[r] if ids else vals[id_index]
|
|
for f, (fo, fs, ads, comp, v1, v2, v3) in enumerate(finfo):
|
|
if comp == 2:
|
|
vals[f] = com[f].get(rid, v1)
|
|
rows.append({"id": rid, "fields": vals, "rel": rel.get(r), "base": off + r * rs, "b": b})
|
|
for new, old in copies:
|
|
src_row = next(x for x in rows if x["id"] == old)
|
|
rows.append(dict(src_row, id=new))
|
|
return rows, finfo
|
|
|
|
def f32(u):
|
|
return struct.unpack("<f", struct.pack("<I", u & 0xFFFFFFFF))[0]
|
|
|
|
def string(row, finfo, f):
|
|
b = row["b"]
|
|
p = row["base"] + finfo[f][0] // 8 + row["fields"][f]
|
|
return b[p:b.index(b"\0", p)].decode("utf8", "replace")
|