Files

84 lines
4.4 KiB
Python

"""#88 (2026-11-28): ERROR #109 freeze 0x141df4f.. (sequence hash probe) <- 0x141d6f8d4 HasSequence <- bow-event check
0x14279dff1 / CM2Model2 0x1425e5a18, five players 11-27/28 (taxi BB->Stonard, BRM, UC). Converted M2 whose 0x24
sequenceIdxHashById has no -1 on some probe path ([0] size 1 etc.) loop forever when asked for an absent animation.
Rebuild every live flagged table as the native hash (ops/m2_sequence_hash.native_sequence_hash: single ID -> [i,-1]).
Input: seqhash_audit.py output (flagged decoded ckeys); only ckeys referenced by the parent ROOT are patched."""
import json, hashlib, struct, sys, collections
from pathlib import Path
H = Path(__file__).resolve().parent; sys.path.insert(0, str(H))
sys.path.insert(0, '/mnt/Games/@project/wt-master-merge/ops')
import frozen_store as fs
from m2_sequence_hash import native_sequence_hash
md5 = lambda b: hashlib.md5(b).hexdigest(); sha = lambda b: hashlib.sha256(b).hexdigest()
WHY = '#88: ERROR #109 sequence-hash probe loop: native sequence hash (terminated) on live converted M2'
def split(data):
"""Return (prefix, md20, suffix) where md20 is the MD20 body inside MD21 chunk or the whole file."""
if data[:4] == b'MD20':
return b'', data, b''
pos = 0
while pos + 8 <= len(data):
tag, size = data[pos:pos + 4], struct.unpack('<I', data[pos + 4:pos + 8])[0]
if tag == b'MD21':
return data[:pos], data[pos + 8:pos + 8 + size], data[pos + 8 + size:]
pos += 8 + size
return None
def rebuild(data):
parts = split(data)
if not parts:
return None, 'no-md20'
pre, body, post = parts
count = struct.unpack_from('<I', body, 0x1C)[0]
if count == 0:
return None, 'no-sequences'
new, changed = native_sequence_hash(body)
if not changed:
return None, 'unchanged'
if not pre and not post and data[:4] == b'MD20':
return new, 'ok'
return pre + b'MD21' + struct.pack('<I', len(new)) + new + post, 'ok'
def main(flagged_json):
flagged = {x['ckey'] for x in json.load(open(flagged_json)) if 'ckey' in x}
store = fs.Store(); root = store.root; lay = store.layout; assert lay['root_md5'] == fs.ROOT
cells = collections.defaultdict(list) # ckey -> [(fid, group, at)]
for g in lay['groups']:
f = 0; ck = memoryview(root)[g['ckeys_offset']:g['ckeys_offset'] + 16 * g['count']]
for i, (d,) in enumerate(struct.iter_unpack('<I', memoryview(root)[g['ids_offset']:g['ckeys_offset']])):
f += d; k = ck[16 * i:16 * i + 16].hex()
if k in flagged: cells[k].append((f, g, g['ckeys_offset'] + i * 16))
f += 1
patched = bytearray(root); pins = []; stats = collections.Counter(); per_fid = collections.Counter()
for k, refs in cells.items():
for f, _, _ in refs: per_fid[f] += 1
multi = sorted(f for f, n in per_fid.items() if n > 1)
for k in sorted(cells):
old = store.read(k)[2]; assert md5(old) == k
new, why = rebuild(old); stats[why] += 1
if not new: continue
for f, g, at in cells[k]:
if per_fid[f] > 1:
stats['skip-multicell'] += 1; continue
patched[at:at + 16] = bytes.fromhex(md5(new))
d = H / 'pins' / str(f); d.mkdir(parents=True, exist_ok=True)
(d / 'before.bin').write_bytes(old); (d / 'after.bin').write_bytes(new)
sel = {x: g[x] for x in ('locale', 'flags1', 'flags2', 'flags3') if x in g}
pin = dict(fdid=f, table=f'model/{f}.m2', parent_root=fs.ROOT, selected_group=sel, encrypted_original=False,
original_decoded=dict(file='before.bin', md5=k, sha256=sha(old)),
after_decoded=dict(file='after.bin', md5=md5(new), sha256=sha(new)), reason=WHY)
(d / 'pin.json').write_text(json.dumps(pin, indent=1) + '\n')
pins.append(dict(file=str(d / 'pin.json'), sha256=sha((d / 'pin.json').read_bytes()), type='root_selector'))
stats['pinned'] += 1
(H / 'components.json').write_text(json.dumps(pins, indent=2) + '\n')
(H / 'selected-root.json').write_text(json.dumps(dict(parent_build=fs.BUILD, parent_root=fs.ROOT, decoded_root_sha256=sha(bytes(patched)), parent_root_sha256=sha(root)), indent=2) + '\n')
(H / 'prep-report.json').write_text(json.dumps(dict(live_ckeys=len(cells), stats=stats, multicell_fdids=multi), indent=1) + '\n')
print('live ckeys', len(cells), dict(stats), 'multicell', len(multi))
if __name__ == '__main__':
main(sys.argv[1])