103 lines
5.0 KiB
Python
103 lines
5.0 KiB
Python
"""Rebase the two Stave rows onto a copy of the selected build-70334 codec headers."""
|
|
import argparse
|
|
from bisect import bisect_right
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import tempfile
|
|
import sys
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
ROOT = HERE.parents[1]
|
|
sys.path[:0] = [str(ROOT / "ops"), str(ROOT / "tests")]
|
|
from live_hotfix_rows import decode
|
|
from native_codec_driver import load
|
|
from octo_parity.compose_charge_provider import (
|
|
read_registry, replace_once, require, digest_identity, page_packet, wire_digest,
|
|
)
|
|
from spell_aura_restrictions_hotfix_fixture import db_query, expected_query, initialization, request
|
|
|
|
# The 70334 Item and ItemSparse schemas place SheatheType at these offsets
|
|
# in the two pinned item 33883 DBReply bodies. ItemSparse has variable strings.
|
|
ROWS = {
|
|
0x50238EC2: (7, "c515616301a25b717dc6ba5bb31ccbb51c1ae1312a2334a80174d03c4f364578"),
|
|
0x919BE54E: (313, "7bad387fd2ce4a292a852edb7010fdc96da4fd834fe1409ff7e6e38d716dfc5e"),
|
|
}
|
|
|
|
|
|
def compose(registry, baked, codec):
|
|
selector, = json.loads((HERE / "setrows.json").read_text())
|
|
item, sheath = selector["ID"], selector["SheatheType"]
|
|
(starts, offsets), original, records, root, pages = read_registry(registry)
|
|
child = bytearray(original)
|
|
header = bytearray(registry)
|
|
changed = {}
|
|
delta = []
|
|
for idx, (table, rid, offset, size, status) in enumerate(records):
|
|
if rid != item or table not in ROWS:
|
|
continue
|
|
field, pin = ROWS[table]
|
|
body = bytes(original[offset:offset + size])
|
|
require(status == 1 and hashlib.sha256(body).hexdigest() == pin and body[field] == 1,
|
|
f"Unexpected parent Stave row {table:#x}")
|
|
require(db_query(codec, table, rid) == expected_query(table, rid, 0x20, body),
|
|
f"Parent codec disagrees with registry for {table:#x}")
|
|
pos = offset + field
|
|
line = bisect_right(offsets, pos) - 1
|
|
literal = starts[line] + 4 * (pos - offsets[line])
|
|
require(header[literal:literal + 4] == br"\001", "Octal parent byte differs")
|
|
child[pos] = sheath
|
|
header[literal:literal + 4] = f"\\{sheath:03o}".encode()
|
|
corrected = child[offset:offset + size]
|
|
delta.append(dict(table_hash=f"0x{table:08x}", id=rid, record_status="valid",
|
|
wire_hex=corrected.hex(), sha256=hashlib.sha256(corrected).hexdigest()))
|
|
changed[idx] = table
|
|
require(len(changed) == 2 and set(changed.values()) == ROWS.keys(), "Missing or duplicate Stave rows")
|
|
new_root = digest_identity(records, child)
|
|
header = replace_once(bytes(header), f"PushID={root};".encode(), f"PushID={new_root};".encode())
|
|
_, advertised = initialization(codec)
|
|
reserved = {p[0] for p in pages} | {p[0] for p in advertised}
|
|
reserved.update(map(int, re.findall(rb"\{(\d+), 0x[0-9a-f]+ull\}", baked)))
|
|
for old, first, count in pages:
|
|
if changed.keys().isdisjoint(range(first, first + count)):
|
|
continue
|
|
page = records[first:first + count]
|
|
old_packet = request(codec, [old])
|
|
rows = decode(old_packet)
|
|
seen = {r["unique_id"] for r in rows}
|
|
removed = {r["unique_id"] for r in rows if r["status"] == "removed"}
|
|
require(all(i - first + 1 in seen - removed for i in changed if first <= i < first + count),
|
|
"Stave row missing from parent hotfix reply")
|
|
visibility = dict(omitted=[(i, i) for i in range(1, count + 1) if i not in seen],
|
|
removed=[(i, i) for i in removed])
|
|
require(page_packet(old, page, original, visibility) == old_packet, "Parent hotfix page differs")
|
|
new = digest_identity(page, child)
|
|
require(new not in reserved and old not in {p[0] for p in advertised},
|
|
"Page collision or unexpected advertised parent")
|
|
reserved.add(new)
|
|
packet = page_packet(new, page, child, visibility)
|
|
header = replace_once(header, f"{{{old},{first},{count}}}".encode(),
|
|
f"{{{new},{first},{count}}}".encode())
|
|
baked = replace_once(baked, f"{{{old}, 0x{wire_digest(old_packet)}ull}}".encode(),
|
|
f"{{{new}, 0x{wire_digest(packet)}ull}}".encode())
|
|
return header, baked, {"rows": delta}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for arg in ("registry", "baked", "output"):
|
|
parser.add_argument(arg, type=Path)
|
|
args = parser.parse_args()
|
|
result = compose(args.registry.read_bytes(), args.baked.read_bytes(), load())
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
if args.output.exists():
|
|
parser.error("Output directory must be new")
|
|
with tempfile.TemporaryDirectory(dir=args.output.parent) as temp:
|
|
dest = Path(temp) / "candidate"
|
|
dest.mkdir()
|
|
(dest / "item_sparse_compat.hpp").write_bytes(result[0])
|
|
(dest / "baked_hotfix_pushes.hpp").write_bytes(result[1])
|
|
(dest / "delta.json").write_text(json.dumps(result[2], indent=2) + "\n")
|
|
dest.rename(args.output)
|