Files
praxisandClaude Fable 5.2 8a24c445b0 Content pipeline and operator tools
367 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

141 lines
6.5 KiB
Python

"""Append records to a single-section WDC5 DB2 in its own native encoding.
Used for client-only tables (for example WMOMinimapTexture) that have no
hotfix layout in the server sources: the file's field storage info, pallets,
ID list and relationship map fully define the encoding, so new rows reuse it
instead of a guessed hotfix layout. Supported storage: bitpacked (1),
bitpacked-indexed/pallet (3) and bitpacked-signed (5); no common data, no
inline strings, no copy table, no sparse offset map, one section.
"""
import struct
HEADER = struct.Struct('<4I2I2I I 2H 7I') # after magic, version, schema[128]
HEADER_AT = 136
SECTION = struct.Struct('<Q 8I')
FIELD_INFO = struct.Struct('<HHI I 3I')
NAMES = ('record_count', 'field_count', 'record_size', 'string_table_size', 'table_hash',
'layout_hash', 'min_id', 'max_id', 'locale', 'flags', 'id_index', 'total_field_count',
'bitpacked_offset', 'lookup_cols', 'fsi_size', 'common_size', 'pallet_size', 'section_count')
SUPPORTED = {1, 3, 5}
def parse(raw):
if raw[:4] != b'WDC5':
raise ValueError('WDC5 required')
h = dict(zip(NAMES, HEADER.unpack_from(raw, HEADER_AT)))
if h['section_count'] != 1 or h['common_size']:
raise ValueError('single section without common data required')
at = HEADER_AT + HEADER.size
s = SECTION.unpack_from(raw, at)
sec = dict(zip(('tact', 'file_offset', 'record_count', 'string_table_size', 'records_end',
'id_list_size', 'relationship_size', 'offset_map_count', 'copy_count'), s))
if sec['tact'] or sec['records_end'] or sec['offset_map_count'] or sec['copy_count']:
raise ValueError('encrypted, sparse or copy-table sections unsupported')
at += SECTION.size
structure = raw[at:at + 4 * h['field_count']]
at += 4 * h['field_count']
info = [FIELD_INFO.unpack_from(raw, at + FIELD_INFO.size * i) for i in range(h['field_count'])]
at += FIELD_INFO.size * h['field_count']
if any(f[3] not in SUPPORTED for f in info):
raise ValueError('unsupported field storage %r' % [f[3] for f in info])
pallets, p = [], at
for f in info:
n = f[2] // 4 if f[3] == 3 else 0
pallets.append(list(struct.unpack_from('<%dI' % n, raw, p)))
p += 4 * n
if p - at != h['pallet_size'] or p != sec['file_offset']:
raise ValueError('pallet extent mismatch')
n, size = h['record_count'], h['record_size']
rec = sec['file_offset']
strings = raw[rec + n * size:rec + n * size + sec['string_table_size']]
ids_at = rec + n * size + sec['string_table_size']
ids = list(struct.unpack_from('<%dI' % n, raw, ids_at))
rel_at = ids_at + sec['id_list_size']
relation = None
if sec['relationship_size']:
count, lo, hi = struct.unpack_from('<3I', raw, rel_at)
relation = [None] * n
for i in range(count):
foreign, index = struct.unpack_from('<2I', raw, rel_at + 12 + 8 * i)
relation[index] = foreign
if rel_at + sec['relationship_size'] != len(raw):
raise ValueError('trailing section data')
rows = []
for i in range(n):
v = int.from_bytes(raw[rec + i * size:rec + (i + 1) * size], 'little')
values = []
for f, pal in zip(info, pallets):
bits = (v >> f[0]) & ((1 << f[1]) - 1)
if f[3] == 3:
values.append(pal[bits])
elif f[3] == 5 and bits >> (f[1] - 1):
values.append(bits - (1 << f[1]))
else:
values.append(bits)
rows.append(values)
return dict(raw=raw, header=h, section=sec, structure=structure, info=info, pallets=pallets,
strings=strings, ids=ids, relation=relation, rows=rows)
def _encode(values, info, pallets, size):
v = 0
for value, f, pal in zip(values, info, pallets):
if f[3] == 3:
if value not in pal:
pal.append(value)
bits = pal.index(value)
elif f[3] == 5:
bits = value & ((1 << f[1]) - 1)
else:
bits = value
if bits >> f[1]:
raise ValueError('value %r exceeds %d-bit field' % (value, f[1]))
v |= bits << f[0]
return v.to_bytes(size, 'little')
def append(raw, new_rows):
"""new_rows: [(record_id, [field values...], relation_or_None)]; returns new file bytes."""
t = parse(raw)
h, sec, info = dict(t['header']), dict(t['section']), t['info']
pallets = [list(p) for p in t['pallets']]
taken = set(t['ids'])
size, base = h['record_size'], sec['file_offset']
records = bytearray(raw[base:base + h['record_count'] * size])
ids, relation = list(t['ids']), t['relation']
for record_id, values, foreign in new_rows:
if record_id in taken:
raise ValueError('record ID %d already present' % record_id)
if len(values) != h['field_count'] or (relation is None) != (foreign is None):
raise ValueError('row shape does not match the table')
taken.add(record_id)
records += _encode(values, info, pallets, size)
ids.append(record_id)
if relation is not None:
relation.append(foreign)
n = len(ids)
info = [list(f) for f in info]
for f, pal in zip(info, pallets):
if f[3] == 3:
if len(pal) > (1 << f[1]):
raise ValueError('pallet outgrew its index width')
f[2] = 4 * len(pal)
pallet_bytes = b''.join(struct.pack('<%dI' % len(p), *p) for p in pallets)
rel_bytes = b''
if relation is not None:
foreign = [x for x in relation]
rel_bytes = struct.pack('<3I', n, min(foreign), max(foreign)) + b''.join(
struct.pack('<2I', f, i) for i, f in enumerate(foreign))
h.update(record_count=n, min_id=min(ids), max_id=max(ids), pallet_size=len(pallet_bytes))
head = HEADER_AT + HEADER.size + SECTION.size + len(t['structure']) + FIELD_INFO.size * len(info)
sec.update(file_offset=head + len(pallet_bytes), record_count=n, id_list_size=4 * n,
relationship_size=len(rel_bytes))
out = bytearray(raw[:HEADER_AT])
out += HEADER.pack(*(h[k] for k in NAMES))
out += SECTION.pack(*(sec[k] for k in ('tact', 'file_offset', 'record_count', 'string_table_size',
'records_end', 'id_list_size', 'relationship_size',
'offset_map_count', 'copy_count')))
out += t['structure'] + b''.join(FIELD_INFO.pack(*f) for f in info) + pallet_bytes
out += records + t['strings'] + struct.pack('<%dI' % n, *ids) + rel_bytes
return bytes(out)