diff options
Diffstat (limited to 'codegen')
| -rw-r--r-- | codegen/genblocks.py | 28 | ||||
| -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 | ||||
| -rw-r--r-- | codegen/lib/download.py | 48 | ||||
| -rw-r--r-- | codegen/lib/extract.py | 44 | ||||
| -rw-r--r-- | codegen/lib/utils.py | 14 | ||||
| -rw-r--r-- | codegen/migrate.py | 5 | ||||
| -rw-r--r-- | codegen/newpacket.py | 19 |
10 files changed, 318 insertions, 42 deletions
diff --git a/codegen/genblocks.py b/codegen/genblocks.py new file mode 100644 index 00000000..9e35f7f3 --- /dev/null +++ b/codegen/genblocks.py @@ -0,0 +1,28 @@ +import lib.code.version +import lib.code.packet +import lib.code.blocks +import lib.code.utils +import lib.download +import lib.extract +import lib.utils +import sys +import os + +version_id = lib.code.version.get_version_id() + +lib.download.get_burger() +lib.download.get_client_jar(version_id) + +print('Generating data with burger') +os.system( + f'cd {lib.utils.get_dir_location("downloads/Burger")} && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json --toppings blockstates' +) +print('Ok') + +mappings = lib.download.get_mappings_for_version(version_id) +block_states_burger = lib.extract.get_block_states_burger(version_id) +ordered_blocks = lib.extract.get_ordered_blocks_burger(version_id) +block_states_report = lib.extract.get_block_states_report(version_id) + +lib.code.blocks.generate_blocks( + block_states_burger, block_states_report, ordered_blocks, mappings) 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: `(.*)`.\*' diff --git a/codegen/lib/download.py b/codegen/lib/download.py index 58074634..23246c29 100644 --- a/codegen/lib/download.py +++ b/codegen/lib/download.py @@ -1,39 +1,40 @@ +from lib.utils import get_dir_location from .mappings import Mappings import requests import json import os # make sure the downloads directory exists -if not os.path.exists('downloads'): - os.mkdir('downloads') +if not os.path.exists(get_dir_location('downloads')): + os.mkdir(get_dir_location('downloads')) def get_burger(): - if not os.path.exists('downloads/Burger'): + if not os.path.exists(get_dir_location('downloads/Burger')): print('\033[92mDownloading Burger...\033[m') os.system( - 'cd downloads && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') + f'cd {get_dir_location("downloads")} && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') print('\033[92mInstalling dependencies...\033[m') os.system('cd downloads/Burger && pip install six jawa') def get_version_manifest(): - if not os.path.exists(f'downloads/version_manifest.json'): + if not os.path.exists(get_dir_location(f'downloads/version_manifest.json')): print( f'\033[92mDownloading version manifest...\033[m') version_manifest_data = requests.get( 'https://piston-meta.mojang.com/mc/game/version_manifest.json').json() - with open(f'downloads/version_manifest.json', 'w') as f: + with open(get_dir_location(f'downloads/version_manifest.json'), 'w') as f: json.dump(version_manifest_data, f) else: - with open(f'downloads/version_manifest.json', 'r') as f: + with open(get_dir_location(f'downloads/version_manifest.json'), 'r') as f: version_manifest_data = json.load(f) return version_manifest_data def get_version_data(version_id: str): - if not os.path.exists(f'downloads/{version_id}.json'): + if not os.path.exists(get_dir_location(f'downloads/{version_id}.json')): version_manifest_data = get_version_manifest() print( @@ -45,46 +46,43 @@ def get_version_data(version_id: str): raise ValueError( f'No version with id {version_id} found. Maybe delete downloads/version_manifest.json and try again?') package_data = requests.get(package_url).json() - with open(f'downloads/{version_id}.json', 'w') as f: + with open(get_dir_location(f'downloads/{version_id}.json'), 'w') as f: json.dump(package_data, f) else: - with open(f'downloads/{version_id}.json', 'r') as f: + with open(get_dir_location(f'downloads/{version_id}.json'), 'r') as f: package_data = json.load(f) return package_data def get_client_jar(version_id: str): - if not os.path.exists(f'downloads/client-{version_id}.jar'): + if not os.path.exists(get_dir_location(f'downloads/client-{version_id}.jar')): package_data = get_version_data(version_id) print('\033[92mDownloading client jar...\033[m') client_jar_url = package_data['downloads']['client']['url'] - with open(f'downloads/client-{version_id}.jar', 'wb') as f: + with open(get_dir_location(f'downloads/client-{version_id}.jar'), 'wb') as f: f.write(requests.get(client_jar_url).content) -def get_burger_data_for_version(version_id: str): - if not os.path.exists(f'downloads/burger-{version_id}.json'): - get_burger() - get_client_jar(version_id) - - os.system( - f'cd downloads/Burger && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json' - ) - with open(f'downloads/burger-{version_id}.json', 'r') as f: - return json.load(f) +def get_server_jar(version_id: str): + if not os.path.exists(get_dir_location(f'downloads/server-{version_id}.jar')): + package_data = get_version_data(version_id) + print('\033[92mDownloading server jar...\033[m') + server_jar_url = package_data['downloads']['server']['url'] + with open(get_dir_location(f'downloads/server-{version_id}.jar'), 'wb') as f: + f.write(requests.get(server_jar_url).content) def get_mappings_for_version(version_id: str): - if not os.path.exists(f'downloads/mappings-{version_id}.txt'): + if not os.path.exists(get_dir_location(f'downloads/mappings-{version_id}.txt')): package_data = get_version_data(version_id) client_mappings_url = package_data['downloads']['client_mappings']['url'] mappings_text = requests.get(client_mappings_url).text - with open(f'downloads/mappings-{version_id}.txt', 'w') as f: + with open(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'w') as f: f.write(mappings_text) else: - with open(f'downloads/mappings-{version_id}.txt', 'r') as f: + with open(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'r') as f: mappings_text = f.read() return Mappings.parse(mappings_text) diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py new file mode 100644 index 00000000..40263779 --- /dev/null +++ b/codegen/lib/extract.py @@ -0,0 +1,44 @@ +# Extracting data from the Minecraft jars + +from lib.download import get_server_jar, get_burger, get_client_jar +from lib.utils import get_dir_location +import json +import os + + +def generate_data_from_server_jar(version_id: str): + if os.path.exists(get_dir_location(f'downloads/generated-{version_id}')): + return + + get_server_jar(version_id) + os.system( + f'cd {get_dir_location(f"downloads")} && java -DbundlerMainClass=net.minecraft.data.Main -jar {get_dir_location(f"downloads/server-{version_id}.jar")} --all --output \"{get_dir_location(f"downloads/generated-{version_id}")}\"' + ) + + +def get_block_states_report(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_burger(version_id: str): + burger_data = get_burger_data_for_version(version_id) + return burger_data[0]['blocks']['block'] + + +def get_ordered_blocks_burger(version_id: str): + burger_data = get_burger_data_for_version(version_id) + return burger_data[0]['blocks']['ordered_blocks'] + + +def get_burger_data_for_version(version_id: str): + if not os.path.exists(get_dir_location(f'downloads/burger-{version_id}.json')): + get_burger() + get_client_jar(version_id) + + os.system( + f'cd {get_dir_location("downloads/Burger")} && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json' + ) + with open(get_dir_location(f'downloads/burger-{version_id}.json'), 'r') as f: + return json.load(f) diff --git a/codegen/lib/utils.py b/codegen/lib/utils.py index c185c0e5..3887bb35 100644 --- a/codegen/lib/utils.py +++ b/codegen/lib/utils.py @@ -1,4 +1,5 @@ import re +import os # utilities that could be used for things other than codegen @@ -10,8 +11,15 @@ 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 = upper_first_letter(s) + # 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 upper_first_letter(name: str): + return name[0].upper() + name[1:] def padded_hex(n: int): return f'0x{n:02x}' @@ -44,3 +52,7 @@ def group_packets(packets: list[PacketIdentifier]): packet_groups[key] = [] packet_groups[key].append(packet.packet_id) return packet_groups + + +def get_dir_location(name: str): + return os.path.join(os.path.dirname(__file__), '..', name) diff --git a/codegen/migrate.py b/codegen/migrate.py index 95a6ac4d..811a3f76 100644 --- a/codegen/migrate.py +++ b/codegen/migrate.py @@ -4,17 +4,18 @@ import lib.code.utils import lib.code.version import lib.code.packet import lib.download +import lib.extract import sys import os old_version_id = lib.code.version.get_version_id() old_mappings = lib.download.get_mappings_for_version(old_version_id) -old_burger_data = lib.download.get_burger_data_for_version(old_version_id) +old_burger_data = lib.extract.get_burger_data_for_version(old_version_id) old_packet_list = list(old_burger_data[0]['packets']['packet'].values()) new_version_id = sys.argv[1] new_mappings = lib.download.get_mappings_for_version(new_version_id) -new_burger_data = lib.download.get_burger_data_for_version(new_version_id) +new_burger_data = lib.extract.get_burger_data_for_version(new_version_id) new_packet_list = list(new_burger_data[0]['packets']['packet'].values()) diff --git a/codegen/newpacket.py b/codegen/newpacket.py index 2e4c77d7..48d97640 100644 --- a/codegen/newpacket.py +++ b/codegen/newpacket.py @@ -1,17 +1,22 @@ -from lib import download, code # type: ignore +import lib.code.version +import lib.code.packet +import lib.code.utils +import lib.download +import lib.extract import sys -import os -mappings = download.get_mappings_for_version('1.18.2') -burger_data = download.get_burger_data_for_version('1.18.2') +version_id = lib.code.version.get_version_id() + +mappings = lib.download.get_mappings_for_version(version_id) +burger_data = lib.extract.get_burger_data_for_version(version_id) burger_packets_data = burger_data[0]['packets']['packet'] packet_id, direction, state = int(sys.argv[1]), sys.argv[2], sys.argv[3] print( f'Generating code for packet id: {packet_id} with direction {direction} and state {state}') -code.packetcodegen.generate_packet(burger_packets_data, mappings, - packet_id, direction, state) +lib.code.packet.generate_packet(burger_packets_data, mappings, + packet_id, direction, state) -code.fmt() +lib.code.utils.fmt() print('Done!') |
