From 4a3a2d2a3da1dab19f492a39f50ac0cd22ae6512 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 9 Jun 2022 19:37:03 -0500 Subject: work on genblocks --- codegen/lib/code/blocks.py | 32 ++++++++++++++++++++++++++++---- codegen/lib/extract.py | 10 +++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) (limited to 'codegen/lib') diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py index ca178ff3..bc5083c7 100644 --- a/codegen/lib/code/blocks.py +++ b/codegen/lib/code/blocks.py @@ -1,11 +1,29 @@ -from lib.utils import to_camel_case from lib.utils import get_dir_location +from lib.utils import to_camel_case +from ..mappings import Mappings import json 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 get_property_variants(data) -> list[str]: + if 'values' in data: + return list(map(str.lower, data['values'])) + if data['type'] == 'bool': + return ['true', 'false'] + if data['type'] == 'int': + # range between data['min'] and data['max'] + return [str(i) for i in range(data['min'], data['max'] + 1)] + raise Exception('Unknown property type: ' + data['type']) + -def generate_blocks(blocks: dict): +def generate_blocks(blocks: dict, mappings: Mappings): with open(BLOCKS_RS_DIR, 'r') as f: existing_code = f.read().splitlines() @@ -15,7 +33,14 @@ def generate_blocks(blocks: dict): # Find properties properties = {} for block_data in blocks.values(): - block_properties = block_data.get('properties', {}) + block_properties = {} + for property in block_data.get('states', []): + property_name = mappings.get_field( + property.get('declared_in', block_data['class']), property['field_name']).lower() + property_variants = get_property_variants(property) + block_properties[property_name] = property_variants + # if property_name == 'eggs': + # print(property, property_name, property_variants) properties.update(block_properties) # Property codegen @@ -35,7 +60,6 @@ def generate_blocks(blocks: dict): # 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 = {} diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py index 2e46736e..bf116437 100644 --- a/codegen/lib/extract.py +++ b/codegen/lib/extract.py @@ -16,10 +16,14 @@ def generate_data_from_server_jar(version_id: str): ) +# the minecraft server jar doesn't give enough useful info so we use burger instead +# def get_block_states(version_id: str): +# generate_data_from_server_jar(version_id) +# with open(get_dir_location(f'downloads/generated-{version_id}/reports/blocks.json'), 'r') as f: +# return json.load(f) def get_block_states(version_id: str): - generate_data_from_server_jar(version_id) - with open(get_dir_location(f'downloads/generated-{version_id}/reports/blocks.json'), 'r') as f: - return json.load(f) + burger_data = get_burger_data_for_version(version_id) + return burger_data[0]['blocks']['block'] def get_burger_data_for_version(version_id: str): -- cgit v1.2.3