diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-06-20 06:22:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-20 06:22:16 +0000 |
| commit | a1484f66290517b6c36f2e82c92613f23d2c4935 (patch) | |
| tree | f0a590ef6deac0c23c932773354fc4f75903953a /codegen/lib/code | |
| parent | e2553bbaf2a550f4941b924e703a922345a1389f (diff) | |
| parent | 405a00c0d1908a4b3fbd8e6684c77dfb178ac55d (diff) | |
| download | azalea-drasl-a1484f66290517b6c36f2e82c92613f23d2c4935.tar.xz | |
Merge branch 'main' into 1.19.1
Diffstat (limited to 'codegen/lib/code')
| -rw-r--r-- | codegen/lib/code/blocks.py | 185 | ||||
| -rw-r--r-- | codegen/lib/code/packet.py | 9 | ||||
| -rw-r--r-- | codegen/lib/code/utils.py | 5 | ||||
| -rw-r--r-- | codegen/lib/code/version.py | 3 |
4 files changed, 195 insertions, 7 deletions
diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py new file mode 100644 index 00000000..4ab4917f --- /dev/null +++ b/codegen/lib/code/blocks.py @@ -0,0 +1,185 @@ +from typing import Optional +from lib.utils import to_snake_case, upper_first_letter, get_dir_location, to_camel_case +from ..mappings import Mappings +import re + +BLOCKS_RS_DIR = get_dir_location('../azalea-block/src/blocks.rs') + +# Terminology: +# - Property: A property of a block, like "direction" +# - Variant: A potential state of a property, like "up" +# - State: A possible state of a block, a combination of variants +# - Block: Has properties and states. + + +def generate_blocks(blocks_burger: dict, blocks_report: dict, ordered_blocks: list[str], mappings: Mappings): + with open(BLOCKS_RS_DIR, 'r') as f: + existing_code = f.read().splitlines() + + new_make_block_states_macro_code = [] + new_make_block_states_macro_code.append('make_block_states! {') + + def get_property_struct_name(property: Optional[dict], block_data_burger: dict, property_variants: list[str]) -> str: + # these are hardcoded because otherwise they cause conflicts + # some names inspired by https://github.com/feather-rs/feather/blob/main/feather/blocks/src/generated/table.rs + if property_variants == ['north', 'east', 'south', 'west', 'up', 'down']: + return 'FacingCubic' + if property_variants == ['north', 'south', 'west', 'east']: + return 'FacingCardinal' + if property_variants == ['top', 'bottom']: + return 'TopBottom' + if property_variants == ['north_south', 'east_west', 'ascending_east', 'ascending_west', 'ascending_north', 'ascending_south']: + return 'RailShape' + if property_variants == ['straight', 'inner_left', 'inner_right', 'outer_left', 'outer_right']: + return 'StairShape' + if property_variants == ['normal', 'sticky']: + return 'PistonType' + if property_variants == ['x', 'z']: + return 'AxisXZ' + if property_variants == ['single', 'left', 'right']: + return 'ChestType' + if property_variants == ['compare', 'subtract']: + return 'ComparatorType' + + if property is None: + return ''.join(map(to_camel_case, property_variants)) + + property_name = None + for class_name in [block_data_burger['class']] + block_data_burger['super']: + property_name = mappings.get_field( + class_name, property['field_name']) + if property_name: + break + assert property_name + property_name = to_camel_case(property_name.lower()) + if property['type'] == 'int': + property_name = to_camel_case( + block_data_burger['text_id']) + property_name + + # if property_variants == ['none', 'low', 'tall']: + + if property_variants == ['up', 'side', 'none']: + property_name = 'Wire' + to_camel_case(property_name) + + return property_name + + # Find properties + properties = {} + + # This dict looks like { 'FloweringAzaleaLeavesDistance': 'distance' } + property_struct_names_to_names = {} + for block_id in ordered_blocks: + block_data_burger = blocks_burger[block_id] + block_data_report = blocks_report[f'minecraft:{block_id}'] + + block_properties = {} + for property_name in list(block_data_report.get('properties', {}).keys()): + property_burger = None + for property in block_data_burger.get('states', []): + if property['name'] == property_name: + property_burger = property + break + + property_variants = block_data_report['properties'][property_name] + + if property_burger is None: + print( + 'Warning: The reports have states for a block, but Burger doesn\'t!', block_data_burger) + + property_struct_name = get_property_struct_name( + property_burger, block_data_burger, property_variants) + + if property_struct_name in properties: + if not properties[property_struct_name] == property_variants: + raise Exception( + 'There are multiple enums with the same name! ' + f'Name: {property_struct_name}, variants: {property_variants}/{properties[property_struct_name]}. ' + 'This can be fixed by hardcoding a name in the get_property_struct_name function.' + ) + + block_properties[property_struct_name] = property_variants + + # if the name ends with _<number>, remove that part + ending = property_name.split('_')[-1] + if ending.isdigit(): + property_name = property_name[:-(len(ending) + 1)] + + # `type` is a reserved keyword, so we use kind instead ¯\_(ツ)_/¯ + if property_name == 'type': + property_name = 'kind' + property_struct_names_to_names[property_struct_name] = property_name + + properties.update(block_properties) + + # Property codegen + new_make_block_states_macro_code.append(' Properties => {') + for property_struct_name, property_variants in properties.items(): + # "face" => Face { + # Floor, + # Wall, + # Ceiling, + # }, + property_name = property_struct_names_to_names[property_struct_name] + new_make_block_states_macro_code.append( + f' "{property_name}" => {property_struct_name} {{') + + for variant in property_variants: + new_make_block_states_macro_code.append( + f' {to_camel_case(variant)},') + + new_make_block_states_macro_code.append( + f' }},') + new_make_block_states_macro_code.append(' },') + + # Block codegen + new_make_block_states_macro_code.append(' Blocks => {') + for block_id in ordered_blocks: + block_data_burger = blocks_burger[block_id] + block_data_report = blocks_report['minecraft:' + block_id] + + block_properties = block_data_burger.get('states', []) + block_properties_burger = block_data_burger.get('states', []) + + default_property_variants: dict[str, str] = {} + for state in block_data_report['states']: + if state.get('default'): + default_property_variants = state.get('properties', {}) + + # TODO: use burger to generate the blockbehavior + new_make_block_states_macro_code.append( + f' {block_id} => BlockBehavior::default(), {{') + for property_name in list(block_data_report.get('properties', {}).keys()): + property_burger = None + for property in block_data_burger.get('states', []): + if property['name'] == property_name: + property_burger = property + break + + property_default = default_property_variants.get(property_name) + property_variants = block_data_report['properties'][property_name] + + property_struct_name = get_property_struct_name( + property_burger, block_data_burger, property_variants) + assert property_default is not None + new_make_block_states_macro_code.append( + f' {property_struct_name}={to_camel_case(property_default)},') + new_make_block_states_macro_code.append(' },') + new_make_block_states_macro_code.append(' }') + new_make_block_states_macro_code.append('}') + + new_code = [] + in_macro = False + for line in existing_code: + if line == 'make_block_states! {': + in_macro = True + elif line == '}': + if in_macro: + in_macro = False + new_code.extend(new_make_block_states_macro_code) + continue + if in_macro: + continue + new_code.append(line) + + with open(BLOCKS_RS_DIR, 'w') as f: + f.write('\n'.join(new_code)) diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py index 36e0ba0c..2aabf39a 100644 --- a/codegen/lib/code/packet.py +++ b/codegen/lib/code/packet.py @@ -1,6 +1,6 @@ -from .utils import burger_type_to_rust_type, write_packet_file -from ..utils import padded_hex, to_snake_case, to_camel_case -from ..mappings import Mappings +from lib.code.utils import burger_type_to_rust_type, write_packet_file +from lib.utils import padded_hex, to_snake_case, to_camel_case, get_dir_location +from lib.mappings import Mappings import os @@ -74,7 +74,8 @@ def generate_packet(burger_packets, mappings: Mappings, target_packet_id, target '\n'.join(generated_packet_code)) print() - mod_rs_dir = f'../azalea-protocol/src/packets/{state}/mod.rs' + mod_rs_dir = get_dir_location( + f'../azalea-protocol/src/packets/{state}/mod.rs') with open(mod_rs_dir, 'r') as f: mod_rs = f.read().splitlines() diff --git a/codegen/lib/code/utils.py b/codegen/lib/code/utils.py index 28a5ef3c..ecfff4fb 100644 --- a/codegen/lib/code/utils.py +++ b/codegen/lib/code/utils.py @@ -1,4 +1,5 @@ +from lib.utils import get_dir_location import os # utilities specifically for codegen @@ -67,9 +68,9 @@ def burger_type_to_rust_type(burger_type): def write_packet_file(state, packet_name_snake_case, code): - with open(f'../azalea-protocol/src/packets/{state}/{packet_name_snake_case}.rs', 'w') as f: + with open(get_dir_location(f'../azalea-protocol/src/packets/{state}/{packet_name_snake_case}.rs'), 'w') as f: f.write(code) def fmt(): - os.system('cd .. && cargo fmt') + os.system(f'cd {get_dir_location("..")} && cargo fmt') diff --git a/codegen/lib/code/version.py b/codegen/lib/code/version.py index e131a598..511d30d1 100644 --- a/codegen/lib/code/version.py +++ b/codegen/lib/code/version.py @@ -1,7 +1,8 @@ +from lib.utils import get_dir_location import re import os -README_DIR = os.path.join(os.path.dirname(__file__), '../../../README.md') +README_DIR = get_dir_location('../README.md') VERSION_REGEX = r'\*Currently supported Minecraft version: `(.*)`.\*' |
