From 9c1c2862361a4863cfd0af36c80705fb6213c3a4 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 28 May 2022 20:59:22 -0500 Subject: default block properties --- codegen/lib/code/blocks.py | 46 +++++++++++++++++++++++++++++++++++++++++----- codegen/lib/utils.py | 7 ++++++- 2 files changed, 47 insertions(+), 6 deletions(-) (limited to 'codegen') diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py index fd268b98..a8f9afd1 100644 --- a/codegen/lib/code/blocks.py +++ b/codegen/lib/code/blocks.py @@ -1,5 +1,6 @@ +from lib.utils import to_camel_case from lib.utils import get_dir_location - +import json BLOCKS_RS_DIR = get_dir_location('../azalea-block/src/blocks.rs') @@ -11,10 +12,45 @@ def generate_blocks(blocks: dict): new_make_block_states_macro_code = [] new_make_block_states_macro_code.append('make_block_states! {') + # Find properties properties = {} - for block_name, block_data in blocks.items(): - block_properties = block_data['properties'] - + for block_data in blocks.values(): + block_properties = block_data.get('properties', {}) properties.update(block_properties) - print(properties) + # Property codegen + new_make_block_states_macro_code.append(' Properties => {') + for property_name, property_variants in properties.items(): + new_make_block_states_macro_code.append( + f' {to_camel_case(property_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, block_data in blocks.items(): + block_id = block_id.split(':')[1] + block_states = block_data['states'] + + default_property_variants = {} + for state in block_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 in block_data.get('properties', {}): + property_default = default_property_variants.get(property) + new_make_block_states_macro_code.append( + f' {to_camel_case(property)}={to_camel_case(property_default)},') + new_make_block_states_macro_code.append(' },') + new_make_block_states_macro_code.append(' },') + + print('\n'.join(new_make_block_states_macro_code)) diff --git a/codegen/lib/utils.py b/codegen/lib/utils.py index 77a7b2d4..fb43af21 100644 --- a/codegen/lib/utils.py +++ b/codegen/lib/utils.py @@ -11,7 +11,12 @@ def to_snake_case(name: str): def to_camel_case(name: str): s = re.sub('_([a-z])', lambda m: m.group(1).upper(), name) - return s[0].upper() + s[1:] + s = s[0].upper() + s[1:] + # if the first character is a number, we need to add an underscore + # maybe we could convert it to the number name (like 2 would become "two")? + if s[0].isdigit(): + s = f'_{s}' + return s def padded_hex(n: int): -- cgit v1.2.3