113 lines
5.8 KiB
Python
113 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Build the source-backed Toy DB2 hotfix registry for build 70334."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from item_sparse_compat import package_pages, package_payloads
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
INPUT = ROOT / 'docs/evidence/content-data/turtle-toy-spellbook-20261122/source-rows.json'
|
|
BASE_REGISTRY = ROOT / 'include/gateway/item_sparse_compat.hpp'
|
|
OUTPUT = ROOT / 'include/gateway/source_toy_compat.hpp'
|
|
TABLE_HASH = '0x7b03245c'
|
|
LAYOUT_HASH = '0x821b5d20'
|
|
FIELD_HASH = '65cf1afbb02abf0f3ecfb25ed0f7cb466b61aab7332b1489d3e0bc6ee42107d4'
|
|
SOURCE_COMPAT_HASH = '3472d1e2e25ac4010c9b3cdab67fc55b8d9142cc51aebdaba56954814ca58af2'
|
|
|
|
|
|
def read_rows(path=INPUT):
|
|
raw = path.read_bytes()
|
|
if len(raw) > 1_048_576:
|
|
raise ValueError('Toy source component exceeds bounded input size')
|
|
component = json.loads(raw)
|
|
if (component.get('kind') != 'source-toy-hotfix-component' or
|
|
component.get('schema_version') != 1 or component.get('build') != 70334):
|
|
raise ValueError('Unsupported Toy component identity')
|
|
table = component.get('native_table', {})
|
|
if (table.get('table') != 'Toy' or table.get('table_hash') != TABLE_HASH or
|
|
table.get('layout_hash') != LAYOUT_HASH or
|
|
table.get('serialized_field_sha256') != FIELD_HASH):
|
|
raise ValueError('Toy DB2 native schema differs')
|
|
if component.get('source_catalogue_compat_sha256') != SOURCE_COMPAT_HASH:
|
|
raise ValueError('Toy rows were produced by an unreviewed source compatibility mapper')
|
|
if component.get('native_toy_row_count') != 20 or component.get('native_toy_max_id') != 1779:
|
|
raise ValueError('Pinned native Toy baseline differs')
|
|
entries = component.get('records')
|
|
if not isinstance(entries, list) or len(entries) != 72:
|
|
raise ValueError('Expected the 72 source Toy rows missing from the native table')
|
|
normalized = []
|
|
seen_items, seen_spells = set(), set()
|
|
for row in entries:
|
|
ident, item_id, spell_id = row.get('id'), row.get('item_id'), row.get('spell_id')
|
|
if any(type(v) is not int or v <= 0 for v in (ident, item_id, spell_id)):
|
|
raise ValueError('Toy row identity must use positive integers')
|
|
wire_hex, digest = row.get('wire_hex'), row.get('sha256')
|
|
if (not isinstance(wire_hex, str) or len(wire_hex) != 28 or
|
|
not re.fullmatch(r'[0-9a-f]{64}', digest or '')):
|
|
raise ValueError('Malformed Toy row wire or digest')
|
|
wire = bytes.fromhex(wire_hex)
|
|
if hashlib.sha256(wire).hexdigest() != digest:
|
|
raise ValueError(f'Toy row {ident} wire digest mismatch')
|
|
if (int.from_bytes(wire[1:5], 'little') != ident or
|
|
int.from_bytes(wire[5:9], 'little') != item_id or
|
|
int.from_bytes(wire[9:13], 'little') != 2 or wire[13] != 0xff):
|
|
raise ValueError(f'Toy row {ident} no longer matches reviewed field encoding')
|
|
if item_id in seen_items or spell_id in seen_spells:
|
|
raise ValueError('Toy source item or spell mapping is duplicated')
|
|
seen_items.add(item_id)
|
|
seen_spells.add(spell_id)
|
|
normalized.append(dict(table_name='Toy', table_hash=TABLE_HASH, id=ident,
|
|
wire_hex=wire_hex, sha256=digest))
|
|
if [row['id'] for row in normalized] != list(range(1780, 1852)):
|
|
raise ValueError('Toy source IDs must fill the reviewed native gap')
|
|
return component, normalized
|
|
|
|
|
|
def existing_push_ids(path=BASE_REGISTRY):
|
|
with path.open(encoding='utf-8') as source:
|
|
line = next((line for line in source if line.startswith('inline constexpr Push Pushes[]=')), None)
|
|
if not line:
|
|
raise ValueError('Existing hotfix registry has no push directory')
|
|
return {int(value) for value in re.findall(r'\{(-?\d+),\d+,\d+\}', line)}
|
|
|
|
|
|
def existing_toy_ids(path=BASE_REGISTRY):
|
|
with path.open(encoding='utf-8') as source:
|
|
line = next((line for line in source if line.startswith('inline constexpr Record Records[]=')), None)
|
|
if not line:
|
|
raise ValueError('Existing hotfix registry has no record directory')
|
|
return {int(value) for value in re.findall(r'\{0x7b03245c,(\d+),', line)}
|
|
|
|
|
|
def build(component_path=INPUT, base_path=BASE_REGISTRY):
|
|
_, rows = read_rows(component_path)
|
|
if existing_toy_ids(base_path) & {row['id'] for row in rows}:
|
|
raise ValueError('Source Toy record identity collides with existing hotfix content')
|
|
push_id, header = package_payloads(rows, compact_payloads=True)
|
|
pages = package_pages(rows)
|
|
if len(pages) != 1 or pages[0]['push_id'] != push_id or pages[0]['count'] != 72:
|
|
raise ValueError('Expected one complete Toy hotfix push')
|
|
if push_id in existing_push_ids(base_path):
|
|
raise ValueError('Toy hotfix push collides with existing item content')
|
|
header = header.replace('Generated by ops/item_sparse_compat.py from authorized local snapshots.',
|
|
'Generated by ops/build_source_toy_compat.py from pinned source Toy rows.')
|
|
header = header.replace('namespace gateway::item_sparse_compat {',
|
|
'namespace gateway::source_toy_compat {')
|
|
if 'namespace gateway::source_toy_compat {' not in header:
|
|
raise ValueError('Could not isolate the Toy hotfix registry namespace')
|
|
return push_id, header, pages
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--input', type=Path, default=INPUT)
|
|
parser.add_argument('--base-registry', type=Path, default=BASE_REGISTRY)
|
|
parser.add_argument('--output', type=Path, default=OUTPUT)
|
|
args = parser.parse_args()
|
|
push_id, header, pages = build(args.input, args.base_registry)
|
|
args.output.write_text(header, encoding='utf-8')
|
|
print(f'Built {pages[0]["count"]} Toy rows on push {push_id}.')
|