diff options
Diffstat (limited to 'codegen')
| -rwxr-xr-x | codegen/genregistries.py | 2 | ||||
| -rw-r--r-- | codegen/lib/code/entity.py | 7 | ||||
| -rw-r--r-- | codegen/lib/code/inventory.py | 108 | ||||
| -rwxr-xr-x | codegen/lib/code/registry.py | 6 | ||||
| -rwxr-xr-x | codegen/migrate.py | 2 |
5 files changed, 121 insertions, 4 deletions
diff --git a/codegen/genregistries.py b/codegen/genregistries.py index 43591d59..e24dcc6a 100755 --- a/codegen/genregistries.py +++ b/codegen/genregistries.py @@ -1,3 +1,4 @@ +import lib.code.inventory import lib.code.registry import lib.code.version import lib.code.packet @@ -10,6 +11,7 @@ version_id = lib.code.version.get_version_id() registries = lib.extract.get_registries_report(version_id) lib.code.registry.generate_registries(registries) +lib.code.inventory.update_menus(registries['minecraft:menu']['entries']) lib.code.utils.fmt() diff --git a/codegen/lib/code/entity.py b/codegen/lib/code/entity.py index c787ffd4..67e818b7 100644 --- a/codegen/lib/code/entity.py +++ b/codegen/lib/code/entity.py @@ -12,7 +12,7 @@ DATA_RS_DIR = get_dir_location( '../azalea-world/src/entity/data.rs') def generate_metadata_names(burger_dataserializers: dict, mappings: Mappings): - serializer_names = [None] * len(burger_dataserializers) + serializer_names: list[Optional[str]] = [None] * len(burger_dataserializers) for burger_serializer in burger_dataserializers.values(): print(burger_serializer) @@ -105,7 +105,8 @@ use super::{ SnifferState, VillagerData }; use azalea_chat::FormattedText; -use azalea_core::{BlockPos, Direction, Particle, Slot, Vec3}; +use azalea_core::{BlockPos, Direction, Particle, Vec3}; +use azalea_inventory::ItemSlot; use bevy_ecs::{bundle::Bundle, component::Component}; use derive_more::{Deref, DerefMut}; use thiserror::Error; @@ -425,7 +426,7 @@ impl From<EntityDataValue> for UpdateMetadataError { elif type_name == 'OptionalUnsignedInt': default = f'OptionalUnsignedInt(Some({default}))' if default != 'Empty' else 'OptionalUnsignedInt(None)' elif type_name == 'ItemStack': - default = f'Slot::Present({default})' if default != 'Empty' else 'Slot::Empty' + default = f'ItemSlot::Present({default})' if default != 'Empty' else 'ItemSlot::Empty' elif type_name == 'BlockState': default = f'{default}' if default != 'Empty' else 'azalea_block::BlockState::AIR' elif type_name == 'OptionalBlockState': diff --git a/codegen/lib/code/inventory.py b/codegen/lib/code/inventory.py new file mode 100644 index 00000000..caab57f2 --- /dev/null +++ b/codegen/lib/code/inventory.py @@ -0,0 +1,108 @@ +from lib.utils import padded_hex, 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 Any, Optional +import os +import re + +# The directory where declare_menus! {} is done +inventory_menus_dir = get_dir_location(f'../azalea-inventory/src/lib.rs') + + +def update_menus(initial_menu_entries: dict[str, Any]): + # new_menus is a dict of { menu_id: { "protocol_id": protocol_id } } + # so convert that into an array where the protocol id is the index and the + # values are enum variant names + new_menus: list[str] = [''] * len(initial_menu_entries) + for menu_id, menu in initial_menu_entries.items(): + new_menus[menu['protocol_id']] = menu_name_to_enum_name(menu_id) + + new_menus.insert(0, 'Player') + + with open(inventory_menus_dir, 'r') as f: + menus_rs = f.read().splitlines() + + start_line_index = 0 + + current_menus = [] + in_the_macro = False + for i, line in enumerate(menus_rs): + if line.startswith('declare_menus!'): + in_the_macro = True + start_line_index = i + if in_the_macro: + if line.startswith(' ') and line.endswith('{'): + # get the variant name for this menu + current_menu = line[:-1].strip() + current_menus.append(current_menu) + + print('current_menus', current_menus) + print('new_menus', new_menus) + + # now we have the current menus, so compare that with the expected + # menus and update the file if needed + if current_menus != new_menus: + # ok so insert the new menus with todo!() for the body + current_menus_list_index = 0 + new_menus_list_index = 0 + insert_line_index = start_line_index + 1 + # figure out what menus need to be placed + while True: + # if the values at the indexes are the same, add to both and don't do anything + if ( + current_menus_list_index < len(current_menus) + and new_menus_list_index < len(new_menus) + and current_menus[current_menus_list_index] == new_menus[new_menus_list_index] + ): + current_menus_list_index += 1 + new_menus_list_index += 1 + # increase insert_line_index until we get a line that starts with } + while not menus_rs[insert_line_index].strip().startswith('}'): + insert_line_index += 1 + insert_line_index += 1 + # print('same', current_menus_list_index, + # new_menus_list_index, insert_line_index) + # something was added to new_menus but not current_menus + elif new_menus_list_index < len(new_menus) and new_menus[new_menus_list_index] not in current_menus: + # insert the new menu + menus_rs.insert( + insert_line_index, f' {new_menus[new_menus_list_index]} {{\n todo!()\n }},') + insert_line_index += 1 + new_menus_list_index += 1 + print('added', current_menus_list_index, + new_menus_list_index, insert_line_index) + # something was removed from new_menus but is still in current_menus + elif current_menus_list_index < len(current_menus) and current_menus[current_menus_list_index] not in new_menus: + # remove the current menu + while not menus_rs[insert_line_index].strip().startswith('}'): + menus_rs.pop(insert_line_index) + menus_rs.pop(insert_line_index) + current_menus_list_index += 1 + print('removed', current_menus_list_index, + new_menus_list_index, insert_line_index) + + # if current_menus_list_index overflowed, then add the rest of the new menus + elif current_menus_list_index >= len(current_menus): + for i in range(new_menus_list_index, len(new_menus)): + menus_rs.insert( + insert_line_index, f' {new_menus[i]} {{\n todo!()\n }},') + insert_line_index += 1 + print('current_menus_list_index overflowed', current_menus_list_index, + new_menus_list_index, insert_line_index) + break + # if new_menus_list_index overflowed, then remove the rest of the current menus + elif new_menus_list_index >= len(new_menus): + for _ in range(current_menus_list_index, len(current_menus)): + while not menus_rs[insert_line_index].strip().startswith('}'): + menus_rs.pop(insert_line_index) + menus_rs.pop(insert_line_index) + # current_menus_list_index += 1 + print('new_menus_list_index overflowed', current_menus_list_index, + new_menus_list_index, insert_line_index) + break + with open(inventory_menus_dir, 'w') as f: + f.write('\n'.join(menus_rs)) + + +def menu_name_to_enum_name(menu_name: str) -> str: + return to_camel_case(menu_name.split(':')[-1]) diff --git a/codegen/lib/code/registry.py b/codegen/lib/code/registry.py index a67b5e4d..e203c11a 100755 --- a/codegen/lib/code/registry.py +++ b/codegen/lib/code/registry.py @@ -16,12 +16,16 @@ def generate_registries(registries: dict): # Stone => "minecraft:stone" # }); + 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' - registry_struct_name = to_camel_case(registry_name.split(':')[1]) + registry_struct_name = to_camel_case(registry_name) registry_code = [] registry_code.append(f'enum {registry_struct_name} {{') diff --git a/codegen/migrate.py b/codegen/migrate.py index fa238561..0222ab00 100755 --- a/codegen/migrate.py +++ b/codegen/migrate.py @@ -1,5 +1,6 @@ from lib.code.packet import fix_state from lib.utils import PacketIdentifier, group_packets +import lib.code.inventory import lib.code.language import lib.code.registry import lib.code.version @@ -134,6 +135,7 @@ lib.code.language.write_language(language) print('Generating registries...') registries = lib.extract.get_registries_report(new_version_id) lib.code.registry.generate_registries(registries) +lib.code.inventory.update_menus(registries['minecraft:menu']['entries']) print('Generating entity metadata...') burger_entities_data = new_burger_data[0]['entities'] |
