73 lines
4.0 KiB
Python
73 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Bring the LOCAL Turtle server's DBCs in line with the current Octo client (owner request 2026-11-24).
|
|
|
|
For every server DBC that differs from the Octo client's copy (highest of dbc..patch-5; patch-O is an optional
|
|
visuals mod with old-base tables and is skipped), the output is the Octo table plus any rows only the local server
|
|
has (local unreleased content, e.g. AreaTrigger 1710, SoundEntries 90000/90001, SpellIcon 3000), whose strings are
|
|
appended to the string block. Layout (field count, record size) must match or the table is left untouched.
|
|
|
|
sync_server_dbc.py <libmpq-readonly.so> <Octo Data dir> <server dbc dir> <output dir>
|
|
"""
|
|
import ctypes as C, os, struct, sys, json, hashlib
|
|
|
|
def reader(lib, data):
|
|
api = C.CDLL(lib)
|
|
sig = {'archive_open': [C.POINTER(C.c_void_p), C.c_char_p, C.c_int64], 'file_number': [C.c_void_p, C.c_char_p, C.POINTER(C.c_uint32)],
|
|
'file_size_unpacked': [C.c_void_p, C.c_uint32, C.POINTER(C.c_int64)], 'file_read': [C.c_void_p, C.c_uint32, C.c_void_p, C.c_int64, C.POINTER(C.c_int64)]}
|
|
for n, a in sig.items(): f = getattr(api, 'libmpq__' + n); f.argtypes = a; f.restype = C.c_int32
|
|
hs = []
|
|
for a in ['dbc.MPQ', 'patch.MPQ', 'patch-1.mpq', 'patch-2.mpq', 'patch-3.mpq', 'patch-4.mpq', 'patch-5.mpq']:
|
|
h = C.c_void_p()
|
|
if api.libmpq__archive_open(C.byref(h), os.path.join(data, a).encode(), -1): raise SystemExit(f'cannot open {a}')
|
|
hs.append(h)
|
|
def read(name):
|
|
best = None
|
|
for h in hs:
|
|
i = C.c_uint32()
|
|
if api.libmpq__file_number(h, ('DBFilesClient\\' + name).encode(), C.byref(i)) == 0:
|
|
s = C.c_int64(); api.libmpq__file_size_unpacked(h, i, C.byref(s)); buf = C.create_string_buffer(s.value); g = C.c_int64()
|
|
if api.libmpq__file_read(h, i, buf, s, C.byref(g)) or g.value != s.value: raise SystemExit(f'read {name}')
|
|
best = buf.raw
|
|
return best
|
|
return read
|
|
|
|
def parse(b):
|
|
magic, n, f, rs, ss = struct.unpack_from('<4s4I', b)
|
|
assert magic == b'WDBC' and len(b) == 20 + n * rs + ss
|
|
return f, rs, [b[20 + i * rs:20 + (i + 1) * rs] for i in range(n)], b[20 + n * rs:]
|
|
|
|
def string_columns(rows, strs, f):
|
|
starts = {0} | {i + 1 for i, c in enumerate(strs) if c == 0}
|
|
cols = []
|
|
for c in range(f):
|
|
vals = [struct.unpack_from('<I', r, 4 * c)[0] for r in rows]
|
|
if any(vals) and all(v in starts and v < len(strs) for v in vals): cols.append(c)
|
|
return cols
|
|
|
|
def main(lib, data, sdir, out):
|
|
read = reader(lib, data); os.makedirs(out, exist_ok=True); report = {}
|
|
for name in sorted(os.listdir(sdir)):
|
|
sv = open(os.path.join(sdir, name), 'rb').read(); oc = read(name)
|
|
if oc is None or oc == sv: continue
|
|
fo, ro, Ro, so = parse(oc); fs, rs, Rs, ss = parse(sv)
|
|
if (fo, ro) != (fs, rs): report[name] = 'layout differs - kept server table'; continue
|
|
ids_o = {struct.unpack_from('<I', r)[0] for r in Ro}
|
|
extra = [r for r in Rs if struct.unpack_from('<I', r)[0] not in ids_o]
|
|
strs = bytearray(so); rows = list(Ro)
|
|
scols = string_columns(Rs, ss, fs)
|
|
for r in extra:
|
|
r = bytearray(r)
|
|
for c in scols:
|
|
v = struct.unpack_from('<I', r, 4 * c)[0]
|
|
if v:
|
|
s = ss[v:ss.index(b'\0', v) + 1]; struct.pack_into('<I', r, 4 * c, len(strs)); strs += s
|
|
rows.append(bytes(r))
|
|
blob = struct.pack('<4s4I', b'WDBC', len(rows), fo, ro, len(strs)) + b''.join(rows) + bytes(strs)
|
|
open(os.path.join(out, name), 'wb').write(blob)
|
|
report[name] = dict(octo_rows=len(Ro), server_rows=len(Rs), kept_server_only=[struct.unpack_from('<I', r)[0] for r in extra],
|
|
sha256=hashlib.sha256(blob).hexdigest())
|
|
json.dump(report, open(os.path.join(out, 'report.json'), 'w'), indent=1)
|
|
for k, v in report.items(): print(k, v if isinstance(v, str) else f"octo {v['octo_rows']} server {v['server_rows']} kept {v['kept_server_only']}")
|
|
|
|
if __name__ == '__main__': main(*sys.argv[1:5])
|