aboutsummaryrefslogtreecommitdiff
path: root/codegen/lib/code/registry.py
blob: 84d613bafec046761e4534e2d41af28b39d788a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from lib.utils import get_dir_location, to_camel_case

REGISTRIES_DIR = get_dir_location("../azalea-registry/src/lib.rs")


def generate_registries(registries: dict):
    with open(REGISTRIES_DIR, "r") as f:
        code = f.read().split("\n")

    existing_registry_enum_names = set()

    for registry_name, registry in registries.items():
        # registry!(Block, {
        #     Air => "minecraft:air",
        #     Stone => "minecraft:stone"
        # });

        registry_name = registry_name.split(":")[1]
        registry_enum_name = registry_name_to_enum_name(registry_name)

        existing_registry_enum_names.add(registry_enum_name)

        registry_code = []
        registry_code.append(f"enum {registry_enum_name} {{")
        registry_entries = sorted(
            registry["entries"].items(), key=lambda x: x[1]["protocol_id"]
        )
        for variant_name, _variant in registry_entries:
            variant_struct_name = to_camel_case(variant_name.split(":")[-1])
            registry_code.append(f'\t{variant_struct_name} => "{variant_name}",')
        registry_code.append("}")

        # when we find a "registry! {" line, find the next line that starts
        # with "enum <name>" and replace that until we find a line that's "}"
        found = False
        in_registry_macro = False
        for i, line in enumerate(list(code)):
            if not in_registry_macro and line == "registry! {":
                in_registry_macro = True
            elif in_registry_macro and line == registry_code[0]:
                # found it, now delete until we get to "}"
                while code[i] != "}":
                    code.pop(i)
                code[i] = "\n".join(registry_code)
                found = True
                break
        if not found:
            code.append("registry! {")
            code.append("\n".join(registry_code))
            code.append("}")
            code.append("")

    # delete the unused registries
    i = 0
    while i < len(code):
        if code[i] == "registry! {":
            # skip until we get to the enum line
            while not code[i].startswith("enum "):
                i += 1
            enum_name = code[i].split(" ")[1]
            if enum_name not in existing_registry_enum_names:
                i -= 1
                while code[i] != "}":
                    code.pop(i)
                code.pop(i)
                # close the registry! block
                code.pop(i)
        else:
            i += 1

    with open(REGISTRIES_DIR, "w") as f:
        f.write("\n".join(code))


def registry_name_to_enum_name(registry_name: str) -> str:
    registry_name = registry_name.split(":")[-1]

    if registry_name.endswith("_type"):
        # change _type to _kind because that's Rustier (and because _type
        # is a reserved keyword)
        registry_name = registry_name[:-5] + "_kind"
    elif registry_name in {"menu"}:
        registry_name += "_kind"

    return to_camel_case(registry_name)