91 lines
3.9 KiB
Python
91 lines
3.9 KiB
Python
"""Build the small JNI binding table from the official 1.21.1 client mapping."""
|
|
import hashlib
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
MAPPING_SHA1 = "2244b6f072256667bcd9a73df124d6c58de77992"
|
|
PRIMITIVES = dict(zip("void boolean byte char short int long float double".split(), "VZBCSIJFD"))
|
|
|
|
|
|
def descriptor(type_name, classes):
|
|
dimensions = 0
|
|
while type_name.endswith("[]"):
|
|
dimensions += 1
|
|
type_name = type_name[:-2]
|
|
if type_name in PRIMITIVES:
|
|
return "[" * dimensions + PRIMITIVES[type_name]
|
|
return "[" * dimensions + "L" + classes.get(type_name, type_name).replace(".", "/") + ";"
|
|
|
|
|
|
def read_mapping(raw):
|
|
classes, members = {}, {}
|
|
owner = None
|
|
for line in raw.decode("utf-8").splitlines():
|
|
if not line or line.startswith("#") or " -> " not in line:
|
|
continue
|
|
left, right = line.strip().rsplit(" -> ", 1)
|
|
if not line[0].isspace():
|
|
owner = left
|
|
classes[left] = right.removesuffix(":")
|
|
members[left] = {}
|
|
continue
|
|
match = re.fullmatch(r"(?:\d+:\d+:)?(\S+) (\S+?)(?::\d+(?::\d+)?)?", left)
|
|
if match:
|
|
result, member = match.groups()
|
|
members[owner].setdefault(member, set()).add((result, right))
|
|
return classes, members
|
|
|
|
|
|
def main(mapping_path, spec_path, output_path):
|
|
raw = Path(mapping_path).read_bytes()
|
|
if hashlib.sha1(raw).hexdigest() != MAPPING_SHA1:
|
|
raise ValueError("expected the unmodified Mojang Java 1.21.1 client mappings")
|
|
classes, members = read_mapping(raw)
|
|
aliases, rows = {}, []
|
|
for line in Path(spec_path).read_text().splitlines():
|
|
if not line.strip() or line.startswith("#"):
|
|
continue
|
|
kind, alias, owner, *entry = line.split()
|
|
if kind == "class":
|
|
aliases[alias] = owner
|
|
rows.append((alias, "Class", owner, classes.get(owner, owner), "", "", "", ""))
|
|
continue
|
|
owner = aliases[owner]
|
|
if kind == "ctor":
|
|
member = "<init>" + entry[0]
|
|
result, obfuscated = "void", "<init>"
|
|
else:
|
|
member = entry[0]
|
|
variants = members[owner].get(member, ())
|
|
if len(entry) == 2:
|
|
variants = [variant for variant in variants if variant[0] == entry[1]]
|
|
if len(variants) != 1:
|
|
raise ValueError(f"{alias}: expected one declaration of {owner}.{member}, found {variants}")
|
|
result, obfuscated = next(iter(variants))
|
|
name, separator, arguments = member.partition("(")
|
|
signatures = []
|
|
for class_map in ({}, classes):
|
|
signature = descriptor(result, class_map)
|
|
if separator:
|
|
arguments_list = arguments.removesuffix(")").split(",") if arguments != ")" else []
|
|
signature = "(" + "".join(descriptor(arg, class_map) for arg in arguments_list) + ")" + signature
|
|
signatures.append(signature)
|
|
rows.append((alias, "Constructor" if kind == "ctor" else kind.title(), owner,
|
|
classes.get(owner, owner), name, obfuscated, *signatures))
|
|
body = ["// Generated from Mojang Java 1.21.1 mappings.", "#pragma once",
|
|
"enum class Kind { Class, Field, Staticfield, Method, Staticmethod, Constructor };",
|
|
"enum Binding {"]
|
|
body.extend(f" {row[0]}," for row in rows)
|
|
body.extend([" BindingCount", "};",
|
|
"struct Declaration { Kind kind; const char* owner[2]; const char* name[2]; const char* signature[2]; };",
|
|
"inline constexpr Declaration declarations[] = {"])
|
|
for _, kind, named_owner, obf_owner, named, obf, named_sig, obf_sig in rows:
|
|
body.append(f' {{Kind::{kind}, {{"{named_owner}", "{obf_owner}"}}, {{"{named}", "{obf}"}}, {{"{named_sig}", "{obf_sig}"}}}},')
|
|
body.extend(["};", ""])
|
|
Path(output_path).write_text("\n".join(body), encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(*sys.argv[1:])
|