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

117 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Generate build 70334 profession hotfix records from the reviewed Octo export."""
import hashlib
import json
import re
import struct
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
COMPONENT = ROOT / "docs/evidence/content-data/octo-profession-recipes-20261122/source-component.json"
ITEMS = ROOT / "docs/evidence/content-data/octo-recipes-20261122/item-icons.json"
ITEMS_SHA256 = "adda0ed0f512455b27b24d71249350311a23eef73fe5bbf5fffa95b8fc6144b3"
# Claude 2026-11-22: ItemSparse for the 773 Turtle items without client rows (gateway ops/claude_itemsparse_773).
SPARSE = ROOT / "docs/evidence/content-data/octo-recipes-20261122/item-sparse-773.json"
SPARSE_SHA256 = "0d357eef3be3f3cbd97715dd07f72a00b87ad04eee719bb63b7c0cff31d71bdd"
OUTPUT = ROOT / "include/gateway/source_recipe_compat.hpp"
COMPONENT_SHA256 = "6deb81eadd8f8f0f75d41ae3df546a067336bd774bdaf81f84563d8e87520d8e"
def identities_in_header(path):
"""Read existing generated row directories, including the combined source table."""
pattern = re.compile(r'\{(0x[0-9a-fA-F]+),\s*(\d+),')
out = set()
with path.open(encoding="utf-8") as source:
for line in source:
if "Records[]" in line or line.lstrip().startswith("{0x"):
out.update((int(table, 16), int(ident)) for table, ident in pattern.findall(line))
if not out:
raise ValueError(f"No generated record directory in {path}")
return out
def push_ids(path):
text = path.read_text(encoding="utf-8")
ids = {int(value) for value in re.findall(r'\{(-?\d+),\s*\d+,\s*\d+\}', text)}
ids.update(int(value) for value in re.findall(r'PushID\s*=\s*(-?\d+)', text))
if not ids:
raise ValueError(f"No generated push directory in {path}")
return ids
def page_id(rows):
"""Stable push ID for small source components such as Meditation."""
removed = any(row['status'] == 2 for row in rows)
if removed:
source = b'GW-HOTFIX-STATUS-1\0' + b''.join(
struct.pack('<IIB', row['table'], row['id'], row['status']) + row['wire'] for row in rows)
else:
source = b''.join(struct.pack('<II', row['table'], row['id']) + row['wire'] for row in rows)
ident = int.from_bytes(hashlib.sha256(source).digest()[:4], 'little') & 0x7fffffff
if not ident:
raise ValueError('Zero source push identity')
return ident
def generate():
raw = COMPONENT.read_bytes()
if hashlib.sha256(raw).hexdigest() != COMPONENT_SHA256:
raise ValueError("Recipe component differs from the reviewed export")
component = json.loads(raw)
rows = component["rows"]
if component["build"] != 70334 or len(rows) != 4740:
raise ValueError("Unexpected recipe component build or row count")
item_bytes = ITEMS.read_bytes()
if hashlib.sha256(item_bytes).hexdigest() != ITEMS_SHA256:
raise ValueError("Item icon catalogue differs from the verified native bindings")
sparse_bytes = SPARSE.read_bytes()
if hashlib.sha256(sparse_bytes).hexdigest() != SPARSE_SHA256:
raise ValueError("ItemSparse component differs from the converted 773-item catalogue")
sparse = json.loads(sparse_bytes)["rows"]
if len(sparse) != 773:
raise ValueError("Unexpected ItemSparse component row count")
rows = rows + json.loads(item_bytes)["rows"] + sparse
digest = hashlib.sha256(raw + item_bytes + sparse_bytes).hexdigest()
# Put the new push after existing item/Toy pushes on clients with a warm cache.
push = 0x7FC00000 | (int(digest[:8], 16) & 0x003FFFFF)
records = []
payload = bytearray()
previous = None
for row in sorted(rows, key=lambda r: (int(r["table_hash"], 16), r["id"])):
table, ident = int(row["table_hash"], 16), row["id"]
data = bytes.fromhex(row["wire_hex"])
if (table, ident) == previous or (row["action"] == "delete") != (not data):
raise ValueError(f"Duplicate or inconsistent recipe record {table:x}/{ident}")
previous = table, ident
# HotfixRecord::Status values in the build 70334 packet serializer.
status = 1 if data else 2
records.append(f" {{0x{table:08x}, {ident}, {{Payload + {len(payload)}, {len(data)}}}, {status}}},")
payload.extend(data)
if 8 + 21 * len(records) + len(payload) > 1048576:
raise ValueError("Recipe push exceeds the Windows codec response bound")
literals = ['"' + "".join(f"\\{b:03o}" for b in payload[i:i + 48]) + '"'
for i in range(0, len(payload), 48)]
return "\n".join([
"// Generated by ops/build_source_recipe_compat.py.", "#pragma once",
"#include <algorithm>", "#include <cstdint>", "#include <iterator>",
"#include <span>", "#include <utility>", "",
"namespace gateway::source_recipe_compat {",
f"inline constexpr int32_t PushID = {push};",
"inline constexpr uint8_t Payload[] =", *literals, ";",
"struct Record { uint32_t table, id; std::span<const uint8_t> data; uint8_t status; };",
"inline constexpr Record Records[] = {", *records, "};", "",
"inline const Record* lookup(uint32_t table, uint32_t id) {",
" const auto key = std::pair{table, id};",
" const auto row = std::lower_bound(std::begin(Records), std::end(Records), key,",
" [](const Record& record, auto wanted) {",
" return std::pair{record.table, record.id} < wanted;",
" });",
" return row != std::end(Records) && row->table == table && row->id == id ? row : nullptr;",
"}", "} // namespace gateway::source_recipe_compat", "",
])
if __name__ == "__main__":
OUTPUT.write_text(generate())