51 lines
2.3 KiB
Python
51 lines
2.3 KiB
Python
"""Build the signature-to-ID index for original enUS NPC text in build 70334."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import struct
|
|
from pathlib import Path
|
|
|
|
|
|
def generate(source, digest):
|
|
raw = source.read_bytes()
|
|
if hashlib.sha256(raw).hexdigest() != digest:
|
|
raise ValueError("BroadcastText input SHA256 differs from the reviewed export")
|
|
rows, ids = {}, set()
|
|
for line in raw.splitlines():
|
|
row = json.loads(line)
|
|
strings = [row[field].encode("utf-8") for field in ("Text", "Text1")]
|
|
if any(b"\0" in text for text in strings):
|
|
raise ValueError("embedded NUL in BroadcastText")
|
|
body = b"\0".join(strings) + b"\0" + struct.pack("<I", row["LanguageID"])
|
|
if len(row["EmoteDelay"]) != 3 or len(row["EmoteID"]) != 3:
|
|
raise ValueError("expected three emote/delay pairs")
|
|
body += b"".join(struct.pack("<II", delay, emote)
|
|
for delay, emote in zip(row["EmoteDelay"], row["EmoteID"]))
|
|
key = hashlib.sha256(body).hexdigest()
|
|
ident = row["ID"]
|
|
if key != row["key"] or key in rows or ident in ids or not 0 < ident < (1 << 24):
|
|
raise ValueError("invalid or duplicate BroadcastText identity")
|
|
ids.add(ident)
|
|
rows[key] = ident
|
|
if not rows:
|
|
raise ValueError("empty BroadcastText index")
|
|
result = ["// Generated by ops/npc_broadcast_text_index.py from SHA256 " + digest,
|
|
"#pragma once", "#include <array>", "#include <cstdint>",
|
|
"namespace gateway::npc_text::broadcast_index {",
|
|
"using Words = std::array<uint64_t, 4>;",
|
|
"struct Entry { Words key; uint32_t id; };",
|
|
"inline constexpr std::array<Entry, " + str(len(rows)) + "> rows{{"]
|
|
for key, ident in sorted(rows.items()):
|
|
words = ", ".join("0x" + key[i:i+16] + "ULL" for i in range(0, len(key), 16))
|
|
result.append(" {{" + words + "}, " + str(ident) + "},")
|
|
return "\n".join([*result, "}};", "}", ""])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
ap = argparse.ArgumentParser(description=__doc__)
|
|
ap.add_argument("index", type=Path)
|
|
ap.add_argument("header", type=Path)
|
|
ap.add_argument("--sha256", required=True)
|
|
args = ap.parse_args()
|
|
args.header.write_text(generate(args.index, args.sha256), encoding="ascii")
|