101 lines
5.9 KiB
Python
101 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline dense repack of a local native CASC store into a fresh output store (owner 2026-11-24).
|
|
|
|
casc_compact.py keeps every archive number; this one renumbers. Every frame reachable from the active
|
|
build (its ENCODING EKey + every EKey ENCODING lists, as casc_compact.reachable) is copied byte-for-byte into
|
|
new archives data.000..data.K packed up to the 1 GiB segment limit, in source order (archive, offset), so
|
|
the old native data stays first and each frame's 30-byte local header is only regenerated when its offset
|
|
changes (relocate_archive_header). Key prefixes with two index rows (~310k: a
|
|
framed row, then a bare-BLTE row 30 bytes later) keep only the first row, which native lookup uses;
|
|
that row's frame has an all-zero local header (copied verbatim, like every unchanged byte). Native archives 0..63 start with a 16-record segment-header block
|
|
(special EKey e356c4a9..., one record per IDX bucket, keyed by segment number). New archive k < 64 gets the
|
|
source archive k's block verbatim (the records name segment k, so they stay valid) and keeps those index rows;
|
|
segment-header rows for k > K are dropped with their archives. Archives >= 64 never had one.
|
|
IDX buckets: one fresh generation each (highest source generation + 1), rows sorted, native capacity.
|
|
shmem / *.lru are not copied (native CASC regenerates them). The source store is never modified.
|
|
"""
|
|
import argparse, collections, json, os, struct, sys, time
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import casc_local_archive as A
|
|
from casc_compact import read_indexes, span, reachable, reflink, SEGMENT, SEGMENT_HEADER_KEY, SEGMENT_INDEX_TAIL
|
|
|
|
HEADER_BLOCK = 16 * 30
|
|
|
|
|
|
def repack(client, output):
|
|
api, _ = A.lookup3_api(); data = client / 'Data/data'
|
|
buckets = read_indexes(data, api)
|
|
# Native lookup takes the first row of a key prefix; 310k prefixes also carry a second row 30 bytes later
|
|
# (bare BLTE, no local header). Keeping the later one breaks every such file (dark client, AnchorUtil.lua).
|
|
rows = {}
|
|
for _, _, layout in buckets.values():
|
|
for r in layout['rows']: rows.setdefault(r[:9], r)
|
|
ref, identity = reachable(client, data, rows)
|
|
seghdr = {p for p in rows if p[3:9] == SEGMENT_INDEX_TAIL}
|
|
live = sorted(((span(rows[p])[0], span(rows[p])[1], span(rows[p])[2], p) for p in ref & rows.keys() if p not in seghdr))
|
|
missing = ref - rows.keys() - seghdr
|
|
out = output / 'Data/data'; out.mkdir(parents=True, exist_ok=False)
|
|
for name in ('config', 'indices', 'wow_classic_beta'):
|
|
if (client / 'Data' / name).exists(): reflink(client / 'Data' / name, output / 'Data' / name)
|
|
for name in os.listdir(client):
|
|
if name != 'Data': reflink(client / name, output / name)
|
|
blocks = {} # source archive k -> its native segment-header block (k < 64)
|
|
for p in seghdr:
|
|
seg, off, size = span(rows[p])
|
|
if off < HEADER_BLOCK and size == 30: blocks.setdefault(seg, None)
|
|
raw_alias = 0; new_rows = {}; k = -1; fh = None; pos = 0; handles = {}; t0 = time.time(); copied = 0
|
|
def open_segment(n):
|
|
f = open(out / f'data.{n:03d}', 'xb'); p = 0
|
|
if n in blocks:
|
|
with open(data / f'data.{n:03d}', 'rb') as s: blk = s.read(HEADER_BLOCK)
|
|
assert all(blk[i * 30:i * 30 + 13] == SEGMENT_HEADER_KEY for i in range(16)), n
|
|
f.write(blk); p = HEADER_BLOCK
|
|
return f, p
|
|
try:
|
|
for seg, off, size, prefix in live:
|
|
if fh is None or pos + size > SEGMENT:
|
|
if fh: fh.close()
|
|
k += 1; fh, pos = open_segment(k)
|
|
src = handles.get(seg) or handles.setdefault(seg, open(data / f'data.{seg:03d}', 'rb'))
|
|
src.seek(off); blob = src.read(size)
|
|
if len(blob) != size: raise ValueError(f'short span {seg}:{off}')
|
|
if off == pos or (blob[:30] == bytes(30) and blob[30:34] == b'BLTE'):
|
|
header = blob[:30]; raw_alias += blob[:30] == bytes(30) # zeroed local header: native reads it as-is
|
|
else:
|
|
header = A.relocate_archive_header(blob[:30], prefix, size, pos, api)
|
|
fh.write(header); fh.write(memoryview(blob)[30:])
|
|
new_rows[prefix] = prefix + ((k << 30) | pos).to_bytes(5, 'big') + struct.pack('<I', size)
|
|
pos += size; copied += size
|
|
if copied >> 33 != (copied - size) >> 33: print(f' {copied >> 30} GiB, archive {k}, {time.time() - t0:.0f} s', flush=True)
|
|
finally:
|
|
if fh: fh.close()
|
|
for h in handles.values(): h.close()
|
|
K = k
|
|
for p in seghdr: # keep header rows of archives that still exist and got their block back
|
|
seg, off, size = span(rows[p])
|
|
if seg <= K and seg in blocks: new_rows[p] = rows[p]
|
|
home = {r[:9]: b for b, (_, _, layout) in buckets.items() for r in layout['rows']}
|
|
report = []
|
|
for bucket, (name, raw, layout) in sorted(buckets.items()):
|
|
mine = sorted((r for p, r in new_rows.items() if home[p] == bucket), key=lambda r: r[:9])
|
|
result, meta = A.rebase_index_rows(raw, bucket, mine, api)
|
|
gen = int(name[2:10], 16) + 1
|
|
(out / f'{bucket:02x}{gen:08x}.idx').write_bytes(result); report.append(dict(bucket=bucket, rows=len(mine)))
|
|
return dict(**identity, archives=K + 1, live_frames=len(live), zero_header_spans=raw_alias, segment_header_blocks=sum(1 for s in blocks if s <= K),
|
|
unindexed_reachable=len(missing), copied_bytes=copied, indexes=report)
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument('client', type=Path); p.add_argument('output', type=Path); p.add_argument('--report', type=Path)
|
|
a = p.parse_args()
|
|
r = repack(a.client.resolve(), a.output.resolve())
|
|
from casc_compact import verify
|
|
r['verify'] = verify(a.output.resolve())
|
|
text = json.dumps(r, indent=1); print(json.dumps({k: v for k, v in r.items() if k != 'indexes'}))
|
|
if a.report: a.report.write_text(text + '\n')
|
|
|
|
|
|
if __name__ == '__main__': main()
|