45 lines
2.1 KiB
Python
Vendored
45 lines
2.1 KiB
Python
Vendored
"""Recover wire metadata from the last C# revision, without transpiling method bodies.
|
|
|
|
Run from the repository root: python tools/migration/generate_protocol.py
|
|
"""
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
|
|
BASE = "ca2e80f"
|
|
PREFIX = "Ananta.Server/Ananta.Core/Protocol/Client4229938/"
|
|
|
|
|
|
def source(name):
|
|
return subprocess.check_output(["git", "show", f"{BASE}:{PREFIX}{name}"]).decode("utf-8-sig")
|
|
|
|
|
|
ids = {name: int(value) for name, value in re.findall(
|
|
r"const uint (\w+)\s*=\s*(\d+)", source("MethodId.cs"))}
|
|
Path("pkg/protocol/testdata/legacy_method_ids.json").write_text(
|
|
json.dumps(ids, indent=2) + "\n", encoding="utf-8")
|
|
header = f"// Code generated from {BASE} C# protocol metadata; DO NOT EDIT.\npackage protocol\n\n"
|
|
names = header + "var canonicalMethodIDs = map[string]uint32{\n"
|
|
names += "".join(f'{json.dumps(name)}: {value},\n' for name, value in ids.items())
|
|
names += "}\n"
|
|
Path("pkg/protocol/method_names_generated.go").write_text(names, encoding="utf-8")
|
|
|
|
rows = re.findall(
|
|
r'case (\d+)u: body = (Array.Empty<byte>\(\)|\[[^\]]*\]); shape = "([^"]*)"; return true;',
|
|
source("DefaultReturnCatalog4229938.cs"))
|
|
assert len(rows) == 2356, "C# return catalog changed; review before generating"
|
|
out = [header, "var defaultReturnBodies = map[uint32]string{"]
|
|
for mid, body, shape in rows:
|
|
data = b"" if body.startswith("Array") else bytes(
|
|
int(value.strip(), 16) for value in body[1:-1].split(",") if value.strip())
|
|
encoded = "".join(f"\\x{value:02x}" for value in data)
|
|
out.append(f'{mid}: "{encoded}", // {shape}')
|
|
out += ["}", "", "// DefaultReturnBody returns an independent payload for the client return reader.",
|
|
"func DefaultReturnBody(methodID uint32) ([]byte, bool) {",
|
|
"body, ok := defaultReturnBodies[methodID]", "return []byte(body), ok", "}"]
|
|
Path("pkg/protocol/default_returns_generated.go").write_text("\n".join(out) + "\n", encoding="utf-8")
|
|
subprocess.run(["gofmt", "-w", "pkg/protocol/default_returns_generated.go",
|
|
"pkg/protocol/method_names_generated.go"], check=True)
|
|
print(f"Recovered {len(ids)} names and {len(rows)} return contracts from {BASE}")
|