107 lines
6.6 KiB
Python
107 lines
6.6 KiB
Python
"""Compose item-only or cumulative Q103 replies from the same frozen, suppressed registry."""
|
|
from bisect import bisect_right
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
import argparse
|
|
import json
|
|
import os
|
|
import re
|
|
import struct
|
|
import sys
|
|
W=Path('/mnt/Games/@project/build/q103-equipment-provider-20261128')
|
|
J=Path(__file__).resolve().parent
|
|
P=W.parents[1]
|
|
CH=W.parent/'q103-charge-provider-20261128'
|
|
ITEM=W.parent/'q103-items-on-q86-20261128'
|
|
from importlib.util import spec_from_file_location,module_from_spec
|
|
spec=spec_from_file_location('charge_provider_builder',CH/'compose.py')
|
|
base=module_from_spec(spec)
|
|
spec.loader.exec_module(base)
|
|
from overlay import initialization
|
|
sha=base.sha
|
|
registry=base.registry
|
|
def read(p):return json.loads(p.read_bytes())
|
|
|
|
def compose(dst,include_charge):
|
|
dst.mkdir(parents=True,exist_ok=True)
|
|
target=dst/'candidate-include/gateway';target.mkdir(parents=True,exist_ok=True)
|
|
source_header=(J/'current/candidate-include/gateway/item_sparse_compat.hpp').read_bytes()
|
|
source_baked=(J/'current/candidate-include/gateway/baked_hotfix_pushes.hpp').read_bytes()
|
|
item=read(ITEM/'provider-companion.json');charge=read(CH/'row-patches.json')
|
|
assert len(charge)==192 and len(item['rows'])==129
|
|
codec_pin=read(CH/'candidate.json')['source_codec']
|
|
codec_pin=sha((J/'current/host/libforever_codec.so').read_bytes())
|
|
os.environ['GW_TEST_CODEC_PATH']=str(J/'current/host/libforever_codec.so');codec=base.load()
|
|
defs=P/'tools/WoWDBDefs/definitions'
|
|
charge_schema=base.checked_schema(defs,'SpellEffect')
|
|
required={(int(r['table_hash'],16),r['id']):(r['before_sha256'],bytes.fromhex(r['after'])) for r in item['rows']}
|
|
if include_charge:
|
|
required.update({(0xf04238a5,int(i)):(r['before_sha256'],bytes.fromhex(r['after_hex'])) for i,r in charge.items()})
|
|
assert len(required)==(321 if include_charge else 129)
|
|
blob,records,old_root,pos,offsets=base.read_header(source_header)
|
|
assert len(records)==230674
|
|
pages=[tuple(map(int,m.groups())) for m in registry.PUSH.finditer(source_header.split(b'inline constexpr Push Pushes[]={',1)[1].split(b'};',1)[0])]
|
|
old=[p[0] for p in pages]
|
|
assert len(old)==len(set(old)) and registry.digest_identity(records,blob)==old_root
|
|
_,advertised=initialization(codec);reserved=set(old)|{n for n,_ in advertised}
|
|
reserved.update(map(int,re.findall(rb'\{(\d+), 0x[0-9a-f]+ull\}',source_baked)))
|
|
out=bytearray(source_header);newblob=bytearray(blob);baked=source_baked
|
|
where={};patches={};byte_count=0
|
|
for idx,(table,rid,at,length,status) in enumerate(records):
|
|
key=(table,rid)
|
|
if key not in required:continue
|
|
pin,after=required[key];prior=bytes(blob[at:at+length])
|
|
assert status==1 and sha(prior)==pin and len(prior)==len(after) and after!=prior and key not in patches
|
|
if table==0xf04238a5:
|
|
r=base.decode(prior,charge_schema,rid)
|
|
assert r['EffectAttributes']==0 and base.decode(after,charge_schema,rid)==dict(r,EffectAttributes=64)
|
|
for j,(a,b) in enumerate(zip(prior,after,strict=True)):
|
|
if a==b:continue
|
|
byte_count+=1;p=at+j;line=bisect_right(offsets,p)-1;txt=pos[line]+4*(p-offsets[line])
|
|
assert out[txt:txt+4]==f'\\{a:03o}'.encode()
|
|
out[txt:txt+4]=f'\\{b:03o}'.encode();newblob[p]=b
|
|
patches[key]=(prior,after);where[idx]=key
|
|
assert patches.keys()==required.keys()
|
|
new_root=registry.digest_identity(records,newblob)
|
|
header=base.replace_once(bytes(out),f'PushID={old_root};'.encode(),f'PushID={new_root};'.encode())
|
|
changes=[]
|
|
for original,first,count in pages:
|
|
touched={key for index,key in where.items() if first<=index<first+count}
|
|
if not touched:continue
|
|
now=registry.digest_identity(records,newblob,first,first+count)
|
|
assert original==registry.digest_identity(records,blob,first,first+count) and now not in reserved
|
|
reserved.add(now);header=base.replace_once(header,f'{{{original},{first},{count}}}'.encode(),f'{{{now},{first},{count}}}'.encode())
|
|
before=base.request(codec,[original]);rows,_=base.parse_hotfix_reply(before)
|
|
packet=bytearray(before);at=8+21*len(rows);matched=set()
|
|
assert len(rows)<=count and len({r[1] for r in rows})==len(rows)
|
|
for i,(owner,unique,table,rid,n,status) in enumerate(rows):
|
|
assert owner==original and 1<=unique<=count and records[first+unique-1][:2]==(table,rid)
|
|
struct.pack_into('<I',packet,4+21*i,now)
|
|
key=(table,rid)
|
|
if key in touched:
|
|
prior,after=patches[key]
|
|
assert n==len(prior) and packet[at:at+n]==prior and key not in matched
|
|
packet[at:at+n]=after;matched.add(key)
|
|
at+=n
|
|
assert at==len(packet) and matched==touched
|
|
old_hash=base.wire_digest(before);new_hash=base.wire_digest(packet)
|
|
assert all(original!=n for n,_ in advertised)
|
|
baked=base.replace_once(baked,f'{{{original}, 0x{old_hash}ull}}'.encode(),f'{{{now}, 0x{new_hash}ull}}'.encode())
|
|
changes.append(dict(before=original,after=now,first=first,count=count,serialized_rows=len(rows),types=dict(Counter(hex(t) for t,i in touched)),
|
|
before_reply_sha256=sha(before),after_reply_sha256=sha(packet),after_fnv64=new_hash))
|
|
checked,rows,pushid,_,_=base.read_header(header)
|
|
assert checked==newblob and rows==records and pushid==new_root
|
|
(target/'item_sparse_compat.hpp').write_bytes(header)
|
|
(target/'baked_hotfix_pushes.hpp').write_bytes(baked)
|
|
(dst/'rows.json').write_text(json.dumps([dict(table=t,id=i,before_sha256=sha(a),after_hex=b.hex()) for (t,i),(a,b) in sorted(patches.items())],indent=2)+'\n')
|
|
receipt=dict(variant='cumulative' if include_charge else 'item-only',source_codec=codec_pin,root_before=old_root,root_after=new_root,
|
|
source_headers=dict(item_sparse_compat=sha(source_header),baked_hotfix_pushes=sha(source_baked)),
|
|
rows=len(patches),changed_wire_bytes=byte_count,tables=dict(Counter(hex(t) for t,i in patches)),registry_count=len(records),
|
|
old_advertised=advertised,changed_pages=changes,headers={f.name:sha(f.read_bytes()) for f in sorted(target.iterdir())})
|
|
(dst/'candidate.json').write_text(json.dumps(receipt,indent=2)+'\n')
|
|
print('PASS',receipt['variant'],len(patches),'rows,',byte_count,'bytes,',len(changes),'suppressed pages, new root',new_root,flush=True)
|
|
|
|
if __name__=='__main__':
|
|
parser=argparse.ArgumentParser();parser.add_argument('variant',choices=('item-only','cumulative'));parser.add_argument('--output',type=Path)
|
|
args=parser.parse_args();compose(args.output or J/args.variant,args.variant=='cumulative')
|