Files
WoW-Hybrid-gateway/ops/build_source_meditation_compat.py

154 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""Build the source-backed Race-10 Meditation DB2 hotfix registry."""
import hashlib
import json
import re
from collections import Counter
from pathlib import Path
from build_source_recipe_compat import identities_in_header, page_id, push_ids
ROOT = Path(__file__).resolve().parents[1]
INPUT = ROOT / 'docs/evidence/content-data/quel-dorei-meditation-20261122/source-component.json'
OUTPUT = ROOT / 'include/gateway/source_meditation_compat.hpp'
SOURCE_SHA256 = '2b048e2f890cae79bc66e8be36a9fed6a2e8faaceb98e3ca8a36651a5432b12b'
ROOTS = {46021, 52524, 52525}
TABLE_HASHES = {
'SpellName': 0x46c66698,
'Spell': 0xe111669e,
'SpellMisc': 0xc603ee28,
'SpellEffect': 0xf04238a5,
'SpellCooldowns': 0xf9f37c57,
'SpellCategories': 0xdbe7f829,
'SpellXSpellVisual': 0x27b7a01a,
}
EXPECTED_COUNTS = Counter({
'SpellName': 3, 'Spell': 3, 'SpellMisc': 3, 'SpellEffect': 4,
'SpellCooldowns': 3, 'SpellCategories': 3, 'SpellXSpellVisual': 3,
})
MAX_PUSH_BYTES = 1_048_576
# The four source SpellEffect rows used the target DB2 IDs 1369860..1369863.
# Those IDs already belong to native effects (and one is also required by the
# profession overlay). Preserve the source row payloads but assign fresh DB2
# record IDs above the accepted build base's occupied range (max 1500769).
# SpellEffect.ID is the external primary key and is not part of the row bytes.
SPELL_EFFECT_ID_REMAP = {
1369860: 1500770,
1369861: 1500771,
1369862: 1500772,
1369863: 1500773,
}
def read_component(path=INPUT):
raw = path.read_bytes()
if hashlib.sha256(raw).hexdigest() != SOURCE_SHA256:
raise ValueError('Meditation component differs from the reviewed source digest')
component = json.loads(raw)
if (component.get('kind') != 'race10-quel-dorei-meditation-spell-hotfix-component' or
component.get('schema_version') != 1 or component.get('build') != 70334 or
component.get('spell_ids') != sorted(ROOTS) or
component.get('source_skill_line_ability_links') != 0):
raise ValueError('Unsupported Meditation component identity or source relationship')
source = component.get('source_dbc_authority', {})
expected_source = {
'Spell.dbc': ('patch-5.mpq', '180488ad8162a51a1d846b125dcc5bc83d34ea08bb76c908c7eb22a699b80771'),
'SkillLineAbility.dbc': ('patch-5.mpq', 'c13a8e060892214789063c1fe7ab54092a5fad77933cc7aae8734d09e0db88d4'),
}
for name, (archive, digest) in expected_source.items():
if source.get(name) != {'archive': archive, 'sha256': digest}:
raise ValueError(f'Unrecognized original source pin for {name}')
grants = {(r['race'], r['class'], r['spell']) for r in component.get('race_grants', [])}
if grants != {
(10, 1, 52524), (10, 2, 46021), (10, 3, 46021),
(10, 4, 52525), (10, 5, 46021), (10, 8, 46021),
}:
raise ValueError('Race-10 class grant mapping differs from the pinned source')
rows = component.get('rows')
if not isinstance(rows, list) or len(rows) != 22:
raise ValueError('Expected the full 22-row Meditation component')
normalized, keys, counts = [], set(), Counter()
for row in rows:
table_name = row.get('table_name')
table_hash = row.get('table_hash')
ident = row.get('id')
wire_hex = row.get('wire_hex')
spell_id = row.get('source_spell_id')
if (table_name not in TABLE_HASHES or table_hash != f'0x{TABLE_HASHES[table_name]:08x}' or
type(ident) is not int or not 0 < ident <= 0x7fffffff or spell_id not in ROOTS or
not isinstance(wire_hex, str) or not re.fullmatch(r'(?:[0-9a-f]{2})+', wire_hex)):
raise ValueError('Malformed Meditation record')
wire = bytes.fromhex(wire_hex)
if hashlib.sha256(wire).hexdigest() != row.get('sha256'):
raise ValueError(f'Payload hash mismatch for {table_name}/{ident}')
key = (TABLE_HASHES[table_name], ident)
if key in keys:
raise ValueError(f'Duplicate Meditation record {table_name}/{ident}')
keys.add(key)
counts[table_name] += 1
if table_name == 'SpellEffect':
ident = SPELL_EFFECT_ID_REMAP.get(ident, ident)
normalized.append({'table': key[0], 'id': ident, 'wire': wire, 'status': 1})
if counts != EXPECTED_COUNTS:
raise ValueError(f'Unexpected Meditation table distribution: {counts}')
normalized.sort(key=lambda r: (r['table'], r['id']))
return normalized
def escaped_blob(wire):
lines = []
for start in range(0, len(wire), 48):
lines.append('"' + ''.join(f'\\{byte:03o}' for byte in wire[start:start + 48]) + '"')
return '\n'.join(lines)
def build():
rows = read_component()
if 8 + sum(21 + len(row['wire']) for row in rows) > MAX_PUSH_BYTES:
raise ValueError('Meditation push exceeds the bounded hotfix response')
base = ROOT / 'include/gateway/item_sparse_compat.hpp'
toy = ROOT / 'include/gateway/source_toy_compat.hpp'
recipe = ROOT / 'include/gateway/source_recipe_compat.hpp'
identities = set()
pushes = set()
for path in (base, toy, recipe):
identities |= identities_in_header(path)
pushes |= push_ids(path)
collision = identities & {(row['table'], row['id']) for row in rows}
if collision:
raise ValueError(f'Meditation DB2 key collides with an existing compatibility row: {sorted(collision)[0]}')
push = page_id(rows)
if push in pushes:
raise ValueError(f'Meditation push ID collides with an existing source push: {push}')
payload = b''.join(row['wire'] for row in rows)
output = [
'// Generated by ops/build_source_meditation_compat.py from pinned Race-10 source rows.',
f'// Source SHA256: {SOURCE_SHA256}',
'#pragma once', '#include <cstdint>', '#include <span>',
'namespace gateway::source_meditation_compat {',
f'inline constexpr char SourceSha256[]="{SOURCE_SHA256}";',
'inline constexpr uint8_t PayloadBlob[]=\n' + escaped_blob(payload) + ';',
'struct Record {uint32_t table,id;std::span<const uint8_t> data;};',
]
offset = 0
output.append('inline constexpr Record Records[]={')
for row in rows:
size = len(row['wire'])
output.append(f'{{0x{row["table"]:08x},{row["id"]},{{PayloadBlob+{offset},{size}}}}},')
offset += size
output += [
'};', 'struct Push {int32_t id;uint32_t first,count;};',
'inline constexpr Push Pushes[]={{' + f'{push},0,{len(rows)}' + '}};',
'inline std::span<const uint8_t> find(uint32_t table,uint32_t id){for(const auto& r:Records)if(r.table==table&&r.id==id)return r.data;return {};}',
'}', '',
]
header = '\n'.join(output)
OUTPUT.write_text(header, encoding='utf-8')
return push, header, rows
if __name__ == '__main__':
push, header, rows = build()
print(f'PASS: {len(rows)} source Meditation rows, push {push}, header {hashlib.sha256(header.encode()).hexdigest()}')