diff options
| author | mat <git@matdoes.dev> | 2026-03-02 18:53:36 -0530 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-03-02 19:23:44 -0500 |
| commit | 782db2e05cb9ea1b8dbd1ab7f5ff2eac29737fef (patch) | |
| tree | 2f420e585267098189c94ecae2dc886c4136216f /codegen/lib | |
| parent | 8fd11c174d921a8085bb7e67ae62958e23d174cb (diff) | |
| download | azalea-drasl-782db2e05cb9ea1b8dbd1ab7f5ff2eac29737fef.tar.xz | |
disambiguate non-minecraft identifier namespaces in codegen
Diffstat (limited to 'codegen/lib')
| -rw-r--r-- | codegen/lib/code/data_components.py | 24 | ||||
| -rw-r--r-- | codegen/lib/code/inventory.py | 4 | ||||
| -rw-r--r-- | codegen/lib/code/packet.py | 4 | ||||
| -rw-r--r-- | codegen/lib/code/registry.py | 11 | ||||
| -rw-r--r-- | codegen/lib/code/tags.py | 7 | ||||
| -rw-r--r-- | codegen/lib/utils.py | 12 |
6 files changed, 37 insertions, 25 deletions
diff --git a/codegen/lib/code/data_components.py b/codegen/lib/code/data_components.py index abbe2eb6..e6de6201 100644 --- a/codegen/lib/code/data_components.py +++ b/codegen/lib/code/data_components.py @@ -1,7 +1,7 @@ from typing import Any, Optional import lib.code.utils import lib.extract -import lib.utils +from lib.utils import identifier_to_path, to_camel_case, to_snake_case DATA_COMPONENTS_DIR = "azalea-inventory/src/components/mod.rs" @@ -54,7 +54,7 @@ def get_expected_variants(version_id: str): registry["entries"].items(), key=lambda x: x[1]["protocol_id"] ) for variant_name, _variant in registry_entries: - variant_struct_name = lib.utils.to_camel_case(variant_name.split(":")[-1]) + variant_struct_name = to_camel_case(identifier_to_path(variant_name)) expected_variants.append(variant_struct_name) return expected_variants @@ -198,10 +198,10 @@ use crate::{ components_to_item_defaults = {} for item_resource_id, data in items.items(): - item_resource_id = item_resource_id.split(":")[1] + item_resource_id = identifier_to_path(item_resource_id) components = data["components"] for component_resource_id, component_value in components.items(): - component_resource_id = component_resource_id.split(":")[1] + component_resource_id = identifier_to_path(component_resource_id) if component_resource_id not in components_to_item_defaults: components_to_item_defaults[component_resource_id] = {} components_to_item_defaults[component_resource_id][item_resource_id] = ( @@ -212,7 +212,7 @@ use crate::{ item_resource_id_to_protocol_id = {} item_resource_ids = [None] * len(registries["minecraft:item"]["entries"]) for item_resource_id, item_data in registries["minecraft:item"]["entries"].items(): - item_resource_id = item_resource_id.split(":")[-1] + item_resource_id = identifier_to_path(item_resource_id) item_protocol_id = item_data["protocol_id"] item_resource_id_to_protocol_id[item_resource_id] = item_protocol_id item_resource_ids[item_protocol_id] = item_resource_id @@ -249,7 +249,7 @@ use crate::{ # manual implementations if isinstance(python_value, dict) and len(python_value) > 0: if target_rust_type == "ConsumeEffect": - variant = lib.utils.to_camel_case(python_value["type"].split(":")[-1]) + variant = to_camel_case(python_value["type"].split(":")[-1]) type_with_variant = f"ConsumeEffect::{variant}" details_without_type = python_value.copy() del details_without_type["type"] @@ -329,7 +329,7 @@ use crate::{ if entity_id and entity_id.startswith("minecraft:"): entity_name = entity_id[10:] # Remove "minecraft:" prefix - entity_name_camel = lib.utils.to_camel_case(entity_name) + entity_name_camel = to_camel_case(entity_name) return f"EntityKind::{entity_name_camel}" raise ValueError(f"Unknown or missing EntityKind: {python_value}") elif target_rust_type == "NbtCompound": @@ -435,7 +435,7 @@ use crate::{ return "DamageType::Registry(azalea_registry::data::DamageKind::new_raw(0))" else: # enum variant - return f"{target_rust_type}::{lib.utils.to_camel_case(python_value.split(':')[-1])}" + return f"{target_rust_type}::{to_camel_case(identifier_to_path(python_value))}" if isinstance(python_value, list): # convert Vec<Thing> into Thing main_vec = "vec![" @@ -463,7 +463,7 @@ use crate::{ for v in python_value: # handle tags correctly if isinstance(v, str) and v.startswith("#minecraft:"): - tag_name = lib.utils.to_snake_case(v.split(":")[-1]).upper() + tag_name = to_snake_case(identifier_to_path(v)).upper() if inner_type == "EntityKind": tag_module = "entities" elif inner_type == "ItemKind": @@ -494,7 +494,7 @@ use crate::{ return str(python_value) for component_resource_id, item_defaults in components_to_item_defaults.items(): - component_struct_name = lib.utils.to_camel_case(component_resource_id) + component_struct_name = to_camel_case(component_resource_id) component_struct_fields = enum_and_struct_fields[component_struct_name] if len(component_struct_fields) == 1 and isinstance( @@ -608,7 +608,7 @@ use crate::{ for item_resource_id, value in item_defaults.items(): if value == most_common_default_value: continue - item_variant_name = lib.utils.to_camel_case(item_resource_id) + item_variant_name = to_camel_case(item_resource_id) code.append( f" ItemKind::{item_variant_name} => {value}," ) @@ -629,7 +629,7 @@ use crate::{ code.append(" fn default_for_item(item: ItemKind) -> Option<Self> {") code.append(" let value = match item {") for item_resource_id, value in item_defaults.items(): - item_variant_name = lib.utils.to_camel_case(item_resource_id) + item_variant_name = to_camel_case(item_resource_id) code.append(f" ItemKind::{item_variant_name} => {value},") code.append(" _ => return None,") code.append(" };") diff --git a/codegen/lib/code/inventory.py b/codegen/lib/code/inventory.py index 066319e0..7acb269d 100644 --- a/codegen/lib/code/inventory.py +++ b/codegen/lib/code/inventory.py @@ -1,5 +1,5 @@ from typing import Any -from lib.utils import to_camel_case, get_dir_location +from lib.utils import identifier_to_path, to_camel_case, get_dir_location # The directory where declare_menus! {} is done inventory_menus_dir = get_dir_location("../azalea-inventory/src/lib.rs") @@ -128,4 +128,4 @@ def update_menus(initial_menu_entries: dict[str, Any]): def menu_name_to_enum_name(menu_name: str) -> str: - return to_camel_case(menu_name.split(":")[-1]) + return to_camel_case(identifier_to_path(menu_name)) diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index 50d01ce2..9ce5c137 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -1,4 +1,4 @@ -from lib.utils import to_snake_case, to_camel_case, get_dir_location +from lib.utils import identifier_to_path, to_snake_case, to_camel_case, get_dir_location from lib.code.utils import burger_type_to_rust_type, write_packet_file from lib.mappings import Mappings from typing import Optional @@ -120,7 +120,7 @@ def packet_direction_report_to_packet_names(report): name_to_id = {} for identifier, packet in report.items(): packet_id = packet["protocol_id"] - name_to_id[identifier.split(":")[-1]] = packet_id + name_to_id[identifier_to_path(identifier)] = packet_id names_sorted = [name for name in sorted(name_to_id, key=lambda x: name_to_id[x])] return names_sorted diff --git a/codegen/lib/code/registry.py b/codegen/lib/code/registry.py index 8957a8fe..01462e1f 100644 --- a/codegen/lib/code/registry.py +++ b/codegen/lib/code/registry.py @@ -1,4 +1,4 @@ -from lib.utils import get_dir_location, to_camel_case +from lib.utils import get_dir_location, identifier_to_path, to_camel_case BUILTIN_REGISTRIES_DIR = get_dir_location("../azalea-registry/src/builtin.rs") DATA_REGISTRIES_DIR = get_dir_location("../azalea-registry/src/data.rs") @@ -16,7 +16,7 @@ def generate_builtin_registries(registries: dict): # Stone => "minecraft:stone" # }); - registry_name = registry_name.split(":")[1] + registry_name = identifier_to_path(registry_name) registry_enum_name = registry_name_to_enum_name(registry_name) existing_registry_enum_names.add(registry_enum_name) @@ -27,8 +27,7 @@ def generate_builtin_registries(registries: dict): registry["entries"].items(), key=lambda x: x[1]["protocol_id"] ) for variant_name, _variant in registry_entries: - # strip out the "minecraft:" prefix - variant_name = variant_name.split(":")[-1] + variant_name = identifier_to_path(variant_name) variant_struct_name = to_camel_case(variant_name) registry_code.append(f'\t{variant_struct_name} => "{variant_name}",') registry_code.append("}") @@ -117,7 +116,7 @@ def generate_data_registries(data_registries: dict): registry_code.append(f"enum {registry_enum_name}Key {{") registry_entries.sort() for variant_name in registry_entries: - variant_struct_name = to_camel_case(variant_name.split(":")[-1]) + variant_struct_name = to_camel_case(identifier_to_path(variant_name)) registry_code.append(f' {variant_struct_name} => "{variant_name}",') registry_code.append("}") @@ -146,7 +145,7 @@ def generate_data_registries(data_registries: dict): def registry_name_to_enum_name(registry_name: str) -> str: - registry_name = registry_name.split(":")[-1] + registry_name = identifier_to_path(registry_name) if registry_name == "block_type": # avoid conflicting with BlockKind diff --git a/codegen/lib/code/tags.py b/codegen/lib/code/tags.py index 651b6892..b6e225ac 100644 --- a/codegen/lib/code/tags.py +++ b/codegen/lib/code/tags.py @@ -1,5 +1,5 @@ from lib.code.registry import registry_name_to_enum_name -from lib.utils import to_snake_case, upper_first_letter, get_dir_location, to_camel_case +from lib.utils import identifier_to_namespace, identifier_to_path, to_snake_case, upper_first_letter, get_dir_location, to_camel_case TAGS_DIR = get_dir_location("../azalea-registry/src/tags") @@ -19,14 +19,15 @@ use crate::{{builtin::{struct_name}, tags::RegistryTag}}; protocol_ids = {} for k, v in registries["minecraft:" + registry_name]["entries"].items(): - protocol_ids[k.split(":")[1]] = v["protocol_id"] + protocol_ids[identifier_to_path(k)] = v["protocol_id"] for tag_name, tag in sorted(tags.items(), key=lambda x: x[0]): entries = [] queue = tag["values"].copy() while queue != []: ident = queue.pop(0) - namespace, entry_name = ident.split(":") + namespace = identifier_to_namespace(ident) + entry_name = identifier_to_path(ident) if namespace[0] == "#": queue += tags[entry_name]["values"] continue diff --git a/codegen/lib/utils.py b/codegen/lib/utils.py index 8d1e8756..52c7206c 100644 --- a/codegen/lib/utils.py +++ b/codegen/lib/utils.py @@ -66,3 +66,15 @@ def group_packets(packets: list[PacketIdentifier]): def get_dir_location(name: str): return os.path.join(os.path.dirname(os.path.dirname(__file__)), name) + +def identifier_to_namespace(ident: str): + return ident.split(":")[0] +def identifier_to_path(ident: str): + if ":" not in ident: + return ident + + namespace, path = ident.lstrip("#").split(":") + if namespace in {"minecraft", "brigadier"}: + return path + # support for mods + return f"{namespace}_{path}" |
