140 lines
5.9 KiB
Python
140 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Prove recipe and missing-item source rows are served through 70334 hotfix paths."""
|
|
import json
|
|
import os
|
|
import re
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path[:0] = [str(ROOT / 'ops'), str(ROOT / 'tests')]
|
|
from build_source_recipe_compat import COMPONENT, ITEMS, SPARSE, OUTPUT, generate
|
|
from native_codec_driver import MODES, load, serialize
|
|
|
|
|
|
def parse_hotfix_reply(packet):
|
|
count = struct.unpack_from('<I', packet)[0]
|
|
offset = 4
|
|
rows = []
|
|
for _ in range(count):
|
|
push, unique, table, ident, size, status = struct.unpack_from('<IIIIIB', packet, offset)
|
|
rows.append((push, unique, table, ident, size, status))
|
|
offset += 21
|
|
body_size = struct.unpack_from('<I', packet, offset)[0]
|
|
offset += 4
|
|
body = packet[offset:]
|
|
assert len(body) == body_size
|
|
return rows, body
|
|
|
|
|
|
def parse_available(packet):
|
|
message_count = struct.unpack_from('<I', packet)[0]
|
|
offset = 4
|
|
hotfixes = None
|
|
for _ in range(message_count):
|
|
opcode, size = struct.unpack_from('<II', packet, offset)
|
|
offset += 8
|
|
body = packet[offset:offset + size]
|
|
offset += size
|
|
if opcode == 0x4B0001:
|
|
realm, count = struct.unpack_from('<II', body)
|
|
assert realm == 0x01000031 and len(body) == 8 + 8 * count
|
|
hotfixes = {struct.unpack_from('<I', body, 8 + 8 * i)[0] for i in range(count)}
|
|
assert offset == len(packet) and hotfixes is not None
|
|
return hotfixes
|
|
|
|
|
|
def base_registry(path=None):
|
|
record_pattern = re.compile(r'\{(0x[0-9a-fA-F]+),(\d+),')
|
|
push_pattern = re.compile(r'\{(-?\d+),(\d+),(\d+)\}')
|
|
identities, pushes = [], []
|
|
path = path or ROOT / 'include/gateway/item_sparse_compat.hpp'
|
|
with path.open(encoding='utf-8') as source:
|
|
for line in source:
|
|
if line.startswith('inline constexpr Record Records[]='):
|
|
identities = [(int(table, 16), int(ident)) for table, ident in record_pattern.findall(line)]
|
|
elif line.startswith('inline constexpr Push Pushes[]='):
|
|
pushes = [(int(push), int(first), int(count)) for push, first, count in push_pattern.findall(line)]
|
|
break
|
|
assert identities and pushes
|
|
return identities, pushes
|
|
|
|
|
|
def normalized_rows():
|
|
rows = []
|
|
for path in (COMPONENT, ITEMS, SPARSE):
|
|
component = json.loads(path.read_text(encoding='utf-8'))
|
|
for row in component['rows']:
|
|
wire = bytes.fromhex(row['wire_hex'])
|
|
status = 2 if row['action'] == 'delete' else 1
|
|
assert (status == 2) == (not wire)
|
|
rows.append((int(row['table_hash'], 16), row['id'], wire, status))
|
|
rows.sort(key=lambda row: (row[0], row[1]))
|
|
assert len(rows) == 6220
|
|
assert len({(table, ident) for table, ident, _, _ in rows}) == len(rows)
|
|
return rows
|
|
|
|
|
|
def run(codec):
|
|
os.environ['GW_TEST_CODEC_PATH'] = str(codec)
|
|
header = generate()
|
|
assert OUTPUT.read_text() == header
|
|
expected = normalized_rows()
|
|
push_match = re.search(r'inline constexpr int32_t PushID = (\d+);', header)
|
|
assert push_match
|
|
push_id = int(push_match.group(1))
|
|
overrides = {(table, ident) for table, ident, _, _ in expected}
|
|
|
|
library = load()
|
|
available = parse_available(serialize(library, MODES.index('--native-client-initialization'),
|
|
struct.pack('<IIQ', 0x01000031, 0, 0)))
|
|
assert push_id in available
|
|
|
|
# The combined recipe, Item and ItemSparse source push must be byte-exact.
|
|
packet = serialize(library, MODES.index('--native-hotfix-reply'),
|
|
struct.pack('<4I', 70334, 0, 1, push_id))
|
|
metadata, body = parse_hotfix_reply(packet)
|
|
assert len(metadata) == len(expected)
|
|
at = 0
|
|
for unique, ((table, ident, raw, state), wire_meta) in enumerate(zip(expected, metadata), 1):
|
|
status = 0x40 if state == 2 else 0x20
|
|
assert wire_meta == (push_id, unique, table, ident, len(raw), status)
|
|
assert body[at:at + len(raw)] == raw
|
|
at += len(raw)
|
|
assert at == len(body)
|
|
|
|
# DBReply must prefer each exact source row, including static-package collisions.
|
|
for table, ident, raw, state in expected:
|
|
reply = serialize(library, MODES.index('--native-db-reply-missing'),
|
|
struct.pack('<III', table, ident, 12345))
|
|
assert struct.unpack_from('<III', reply) == (table, ident, 12345)
|
|
assert reply[12] == (0x40 if state == 2 else 0x20)
|
|
size = struct.unpack_from('<I', reply, 13)[0]
|
|
assert size == len(raw) and reply[17:] == raw
|
|
|
|
# Existing ItemSparse pages advertise and return only rows not superseded by source data.
|
|
effective_registry = codec.parent / 'compatibility-include/gateway/item_sparse_compat.hpp'
|
|
base_rows, base_pushes = base_registry(effective_registry if effective_registry.is_file() else None)
|
|
replaced_count = sum(key in overrides for key in base_rows)
|
|
for push, first, count in base_pushes:
|
|
old = base_rows[first:first + count]
|
|
surviving = [(offset + 1, table, ident) for offset, (table, ident) in enumerate(old)
|
|
if (table, ident) not in overrides]
|
|
assert (push in available) == bool(surviving)
|
|
if not surviving:
|
|
continue
|
|
packet = serialize(library, MODES.index('--native-hotfix-reply'),
|
|
struct.pack('<4I', 70334, 0, 1, push))
|
|
metadata, _ = parse_hotfix_reply(packet)
|
|
assert [(row[1], row[2], row[3]) for row in metadata] == surviving
|
|
|
|
print(f'PASS: {len(expected)} exact source rows (4,740 profession, 707 Item, 773 ItemSparse); '
|
|
f'{replaced_count} static overlaps; one bounded source push; DBReply and deduplicated base pages exact.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 2:
|
|
raise SystemExit('usage: source_recipe_hotfix_fixture.py CODEC_LIBRARY')
|
|
run(Path(sys.argv[1]))
|