Files
WoW-Hybrid-gateway/tests/auth_metadata_fixture.py

136 lines
6.5 KiB
Python

"""Exact source-header oracle for auth DBReply/HotfixConnect fixtures.
Compact literal/record parsing follows item_sparse_compat's frozen-registry
validator. No codec output is used to construct expected rows or page bodies.
"""
import codecs
import re
import struct
from dataclasses import dataclass
from item_sparse_compat import MAX_PACKAGE_ROWS, MAX_PACKAGE_BYTES, MAX_PUSH_BYTES
import spell_aura_restrictions_hotfix_fixture as aura
@dataclass(frozen=True)
class Row:
table: int
ident: int
body: bytes
status: int = 1
@property
def key(self):
return self.table, self.ident
def registry(path):
text = path.read_text(encoding='ascii')
pool = re.search(r'uint8_t (PayloadBlob|Payload)\[\]\s*=\s*(.*?);', text, re.S)
assert pool, ('Missing compact payload pool', path)
# These generators use three-digit octal escapes exclusively. Reject
# unparsed C++ syntax instead of silently dropping a declaration.
literals = re.findall(r'"((?:\\[0-7]{3})*)"', pool[2])
assert re.sub(r'"(?:\\[0-7]{3})*"|\s', '', pool[2]) == ''
blob = codecs.escape_decode(''.join(literals).encode('ascii'))[0]
assert len(blob) <= MAX_PACKAGE_BYTES
entries = re.search(r'Records\[\]\s*=\s*\{(.*?)\};', text, re.S)[1]
pattern = re.compile(r'\{\s*(0x[0-9a-fA-F]+),\s*(\d+),\s*\{\s*(?:'
+ pool[1] + r'\s*\+\s*(\d+),\s*(\d+)\s*)?\}'
r'(?:,\s*([12]))?\s*\}')
rows = []
position = 0
for match in pattern.finditer(entries):
assert entries[position:match.start()].strip() == (',' if rows else ''), ('Record syntax', path)
table, ident = int(match[1], 16), int(match[2])
offset, length = int(match[3] or 0), int(match[4] or 0)
status = int(match[5] or 1)
assert table and 0 <= ident <= 0x7fffffff
assert offset + length <= len(blob) and length <= 65535
assert (status == 2) == (length == 0)
rows.append(Row(table, ident, blob[offset:offset + length], status))
position = match.end()
assert entries[position:].strip() in ('', ',')
assert 0 < len(rows) <= MAX_PACKAGE_ROWS and len({r.key for r in rows}) == len(rows)
# Preserve the compiled frozen page descriptors. Repartitioning an older
# package would change both cache identities and unique row positions.
directory = re.search(r'Pushes\[\]\s*=\s*\{(.*?)\};', text, re.S)
if directory:
triples = re.findall(r'\{(\d+),\s*(\d+),\s*(\d+)\}', directory[1])
assert re.sub(r'\{\d+,\s*\d+,\s*\d+\}|[,\s]', '', directory[1]) == ''
pages = [tuple(map(int, triple)) for triple in triples]
else:
pages = [(int(re.search(r'PushID\s*=\s*(\d+)', text)[1]), 0, len(rows))]
end = 0
for ident, first, count in pages:
assert 0 < ident <= 0x7fffffff and first == end and count > 0
end = first + count
assert end == len(rows) and len({p[0] for p in pages}) == len(pages)
return rows, pages
class Metadata:
def __init__(self, root):
sources = {name: registry(root / (name + '_compat.hpp')) for name in
('item_sparse', 'source_toy', 'source_recipe', 'source_meditation')}
aura_rows = [Row(aura.TABLE, ident, payload) for ident, payload in aura.reviewed_payloads()]
sources['aura_restrictions'] = aura_rows, [(aura.PUSH, 0, len(aura_rows))]
generated_text = (root / 'generated_transmog_items.hpp').read_text()
ids = re.search(r'Ids\[\]\s*=\s*\{(.*?)\};', generated_text, re.S)[1]
assert re.sub(r'[\d,\s]', '', ids) == ''
generated = set(map(int, re.findall(r'\d+', ids)))
assert len(generated) == 131
def effective(row):
remove = row.table == 0x7b03245c
if row.table in (0x50238ec2, 0x919be54e):
remove |= row.ident in generated
if row.table == 0xe491ac55 and len(row.body) >= 8:
remove |= struct.unpack_from('<I', row.body, 4)[0] in generated
return Row(row.table, row.ident, b'', 2) if remove else row
recipes = {r.key: r for r in sources['source_recipe'][0]}
self.db = {}
# The aura overlay only owns new table/record pairs.
assert not ({r.key for r in aura_rows} & {
r.key for name, (rows, _) in sources.items() if name != 'aura_restrictions' for r in rows})
for name in ('source_toy', 'item_sparse', 'source_meditation', 'aura_restrictions', 'source_recipe'):
for row in sources[name][0]:
self.db[row.key] = effective(row)
# Full HotfixConnect pages prove every payload. Bound the separate
# linear DBReply lookups to all override components, all policy-induced
# removals, each table/status boundary, and each frozen page boundary.
self.db_probes = {(0x919be54e, 23043)}
for name, (rows, pages) in sources.items():
if name != 'item_sparse':
self.db_probes.update(row.key for row in rows)
groups = {}
for row in rows:
group = (row.table, row.status)
if group not in groups:
self.db_probes.add(row.key)
groups[group] = row.key
if effective(row) != row or row.key in recipes:
self.db_probes.add(row.key)
self.db_probes.update(groups.values())
for _, first, count in pages:
self.db_probes.update((rows[first].key, rows[first + count - 1].key))
self.pages = {}
self.superseded = 0
for name, (rows, pages) in sources.items():
for ident, first, count in pages:
records = []
for unique, row in enumerate(rows[first:first + count], 1):
if name == 'item_sparse' and row.key in recipes:
self.superseded += 1
continue
records.append((unique, effective(row)))
metadata = b''.join(struct.pack('<5IB', ident, unique, row.table, row.ident,
len(row.body), row.status << 5)
for unique, row in records)
content = b''.join(row.body for _, row in records)
wire = struct.pack('<I', len(records)) + metadata + struct.pack('<I', len(content)) + content
assert len(wire) <= MAX_PUSH_BYTES and ident not in self.pages
self.pages[ident] = wire
self.source_counts = {name: len(rows) for name, (rows, _) in sources.items()}