diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2023-07-14 22:20:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-14 22:20:40 -0500 |
| commit | 7405427199e5a994d4a6a706f84434a69cb7a7d9 (patch) | |
| tree | ca537e5d761bc053187d952fced0915c850b92aa /codegen | |
| parent | d1afd02aa84e7b4450c1607277f078eb2a0f1bf3 (diff) | |
| download | azalea-drasl-7405427199e5a994d4a6a706f84434a69cb7a7d9.tar.xz | |
Mining (#95)
* more mining stuff
* initialize azalea-tags crate
* more mining stuff 2
* mining in ecs
* well technically mining works but
no codegen for how long it takes to mine each block yet
* rename downloads to __cache__
it was bothering me since it's not *just* downloads
* codegen block behavior
* fix not sending packet to finish breaking block
* mining animation 🎉
* clippy
* cleanup, move Client::mine into a client extension
* add azalea/src/mining.rs
---------
Co-authored-by: mat <git@matdoes.dev>
Diffstat (limited to 'codegen')
| -rwxr-xr-x | codegen/.gitignore | 4 | ||||
| -rwxr-xr-x | codegen/genblocks.py | 2 | ||||
| -rwxr-xr-x | codegen/genregistries.py | 26 | ||||
| -rwxr-xr-x | codegen/lib/code/blocks.py | 24 | ||||
| -rwxr-xr-x | codegen/lib/code/shapes.py | 2 | ||||
| -rw-r--r-- | codegen/lib/code/tags.py | 35 | ||||
| -rwxr-xr-x | codegen/lib/download.py | 76 | ||||
| -rwxr-xr-x | codegen/lib/extract.py | 35 | ||||
| -rwxr-xr-x | codegen/migrate.py | 5 |
9 files changed, 148 insertions, 61 deletions
diff --git a/codegen/.gitignore b/codegen/.gitignore index 2ef6e1be..ee2504d7 100755 --- a/codegen/.gitignore +++ b/codegen/.gitignore @@ -1,3 +1,5 @@ -downloads __pycache__ *.tmp + +downloads +__cache__ diff --git a/codegen/genblocks.py b/codegen/genblocks.py index 45e7683e..5e28b60a 100755 --- a/codegen/genblocks.py +++ b/codegen/genblocks.py @@ -20,7 +20,7 @@ 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) + block_states_burger, block_states_report, pixlyzer_block_datas, ordered_blocks, mappings) lib.code.shapes.generate_block_shapes( pixlyzer_block_datas, shape_datas['shapes'], shape_datas['aabbs'], block_states_report, block_states_burger, mappings) diff --git a/codegen/genregistries.py b/codegen/genregistries.py index e24dcc6a..01e84cb3 100755 --- a/codegen/genregistries.py +++ b/codegen/genregistries.py @@ -3,16 +3,30 @@ import lib.code.registry import lib.code.version import lib.code.packet import lib.code.utils +import lib.code.tags import lib.download import lib.extract import lib.utils -version_id = lib.code.version.get_version_id() -registries = lib.extract.get_registries_report(version_id) +def generate(version_id: str): + 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.registry.generate_registries(registries) + lib.code.inventory.update_menus(registries['minecraft:menu']['entries']) -lib.code.utils.fmt() -print('Done!') + block_tags = lib.extract.get_registry_tags(version_id, 'blocks') + item_tags = lib.extract.get_registry_tags(version_id, 'items') + fluid_tags = lib.extract.get_registry_tags(version_id, 'fluids') + + lib.code.tags.generate_tags(block_tags, 'blocks', 'Block') + lib.code.tags.generate_tags(item_tags, 'items', 'Item') + lib.code.tags.generate_tags(fluid_tags, 'fluids', 'Fluid') + + lib.code.utils.fmt() + + print('Done!') + +if __name__ == '__main__': + version_id = lib.code.version.get_version_id() + generate(version_id) diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py index a01df648..3af19fa8 100755 --- a/codegen/lib/code/blocks.py +++ b/codegen/lib/code/blocks.py @@ -13,7 +13,7 @@ BLOCKS_RS_DIR = get_dir_location('../azalea-block/src/generated.rs') # - Block: Has properties and states. -def generate_blocks(blocks_burger: dict, blocks_report: dict, ordered_blocks: list[str], mappings: Mappings): +def generate_blocks(blocks_burger: dict, blocks_report: dict, pixlyzer_block_datas: dict, ordered_blocks: list[str], mappings: Mappings): with open(BLOCKS_RS_DIR, 'r') as f: existing_code = f.read().splitlines() @@ -90,6 +90,7 @@ def generate_blocks(blocks_burger: dict, blocks_report: dict, ordered_blocks: li for block_id in ordered_blocks: block_data_burger = blocks_burger[block_id] block_data_report = blocks_report['minecraft:' + block_id] + block_data_pixlyzer = pixlyzer_block_datas[f'minecraft:{block_id}'] block_properties = block_data_burger.get('states', []) block_properties_burger = block_data_burger.get('states', []) @@ -134,9 +135,28 @@ def generate_blocks(blocks_burger: dict, blocks_report: dict, ordered_blocks: li else: properties_code += '\n }' + # make the block behavior + behavior_constructor = 'BlockBehavior::new()' + # requires tool + if block_data_pixlyzer.get('requires_tool'): + behavior_constructor += '.requires_correct_tool_for_drops()' + # strength + destroy_time = block_data_pixlyzer.get('hardness') + explosion_resistance = block_data_pixlyzer.get('explosion_resistance') + if destroy_time and explosion_resistance: + behavior_constructor += f'.strength({destroy_time}, {explosion_resistance})' + elif destroy_time: + behavior_constructor += f'.destroy_time({destroy_time})' + elif explosion_resistance: + behavior_constructor += f'.explosion_resistance({explosion_resistance})' + # friction + friction = block_data_pixlyzer.get('friction') + if friction != None: + behavior_constructor += f'.friction({friction})' + # TODO: use burger to generate the blockbehavior new_make_block_states_macro_code.append( - f' {block_id} => BlockBehavior::default(), {properties_code},') + f' {block_id} => {behavior_constructor}, {properties_code},') new_make_block_states_macro_code.append(' }') new_make_block_states_macro_code.append('}') diff --git a/codegen/lib/code/shapes.py b/codegen/lib/code/shapes.py index 7682fe53..18f2ccbd 100755 --- a/codegen/lib/code/shapes.py +++ b/codegen/lib/code/shapes.py @@ -66,7 +66,7 @@ def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict): def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, block_datas_burger, mappings: Mappings): - # look at downloads/generator-mod-*/blockCollisionShapes.json for format of blocks and shapes + # look at __cache__/generator-mod-*/blockCollisionShapes.json for format of blocks and shapes generated_shape_code = '' for (shape_id, shape) in sorted(shapes.items(), key=lambda shape: int(shape[0])): diff --git a/codegen/lib/code/tags.py b/codegen/lib/code/tags.py new file mode 100644 index 00000000..40b60ae3 --- /dev/null +++ b/codegen/lib/code/tags.py @@ -0,0 +1,35 @@ +from lib.utils import to_snake_case, upper_first_letter, get_dir_location, to_camel_case + +REGISTRIES_DIR = get_dir_location('../azalea-registry/src/tags') + + +def generate_tags(registries: dict, file_name: str, struct_name: str): + tags_dir = f'{REGISTRIES_DIR}/{file_name}.rs' + + generated = f'''// This file was generated by codegen/lib/code/tags.py, don't edit it manually! + +use std::collections::HashSet; + +use once_cell::sync::Lazy; + +use crate::{struct_name}; + +''' + + for tag_name, tag in registries.items(): + tag_name = tag_name.replace('/', '_') + static_set_name = to_snake_case(tag_name).upper() + generated += f'pub static {static_set_name}: Lazy<HashSet<{struct_name}>> = Lazy::new(|| HashSet::from_iter(vec![' + + queue = tag['values'].copy() + while queue != []: + item = queue.pop(0) + namespace, item_name = item.split(':') + if namespace[0] == '#': + queue += registries[item_name]['values'] + continue + generated += f'{struct_name}::{upper_first_letter(to_camel_case(item_name))},\n' + generated += ']));\n' + + with open(tags_dir, 'w') as f: + f.write(generated)
\ No newline at end of file diff --git a/codegen/lib/download.py b/codegen/lib/download.py index 1b22fbcc..319c6080 100755 --- a/codegen/lib/download.py +++ b/codegen/lib/download.py @@ -5,47 +5,47 @@ import requests import json import os -# make sure the downloads directory exists -print('Making downloads') -if not os.path.exists(get_dir_location('downloads')): - print('Made downloads directory.', get_dir_location('downloads')) - os.mkdir(get_dir_location('downloads')) +# make sure the cache directory exists +print('Making __cache__') +if not os.path.exists(get_dir_location('__cache__')): + print('Made __cache__ directory.', get_dir_location('__cache__')) + os.mkdir(get_dir_location('__cache__')) def get_burger(): - if not os.path.exists(get_dir_location('downloads/Burger')): + if not os.path.exists(get_dir_location('__cache__/Burger')): print('\033[92mDownloading Burger...\033[m') os.system( - f'cd {get_dir_location("downloads")} && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') + f'cd {get_dir_location("__cache__")} && git clone https://github.com/pokechu22/Burger && cd Burger && git pull') print('\033[92mInstalling dependencies...\033[m') - os.system(f'cd {get_dir_location("downloads")}/Burger && pip install six jawa') + os.system(f'cd {get_dir_location("__cache__")}/Burger && pip install six jawa') def get_pixlyzer(): - if not os.path.exists(get_dir_location('downloads/pixlyzer')): + if not os.path.exists(get_dir_location('__cache__/pixlyzer')): print('\033[92mDownloading bixilon/pixlyzer...\033[m') os.system( - f'cd {get_dir_location("downloads")} && git clone https://gitlab.bixilon.de/bixilon/pixlyzer.git && cd pixlyzer && git pull') - return get_dir_location('downloads/pixlyzer') + f'cd {get_dir_location("__cache__")} && git clone https://gitlab.bixilon.de/bixilon/pixlyzer.git && cd pixlyzer && git pull') + return get_dir_location('__cache__/pixlyzer') def get_version_manifest(): - if not os.path.exists(get_dir_location(f'downloads/version_manifest.json')): + if not os.path.exists(get_dir_location(f'__cache__/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_v2.json').json() - with open(get_dir_location(f'downloads/version_manifest.json'), 'w') as f: + with open(get_dir_location(f'__cache__/version_manifest.json'), 'w') as f: json.dump(version_manifest_data, f) else: - with open(get_dir_location(f'downloads/version_manifest.json'), 'r') as f: + with open(get_dir_location(f'__cache__/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(get_dir_location(f'downloads/{version_id}.json')): + if not os.path.exists(get_dir_location(f'__cache__/{version_id}.json')): version_manifest_data = get_version_manifest() print( @@ -55,60 +55,60 @@ def get_version_data(version_id: str): filter(lambda v: v['id'] == version_id, version_manifest_data['versions']))['url'] except StopIteration: raise ValueError( - f'No version with id {version_id} found. Maybe delete downloads/version_manifest.json and try again?') + f'No version with id {version_id} found. Maybe delete __cache__/version_manifest.json and try again?') package_data = requests.get(package_url).json() - with open(get_dir_location(f'downloads/{version_id}.json'), 'w') as f: + with open(get_dir_location(f'__cache__/{version_id}.json'), 'w') as f: json.dump(package_data, f) else: - with open(get_dir_location(f'downloads/{version_id}.json'), 'r') as f: + with open(get_dir_location(f'__cache__/{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(get_dir_location(f'downloads/client-{version_id}.jar')): + if not os.path.exists(get_dir_location(f'__cache__/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(get_dir_location(f'downloads/client-{version_id}.jar'), 'wb') as f: + with open(get_dir_location(f'__cache__/client-{version_id}.jar'), 'wb') as f: f.write(requests.get(client_jar_url).content) def get_server_jar(version_id: str): - if not os.path.exists(get_dir_location(f'downloads/server-{version_id}.jar')): + if not os.path.exists(get_dir_location(f'__cache__/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: + with open(get_dir_location(f'__cache__/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(get_dir_location(f'downloads/mappings-{version_id}.txt')): + if not os.path.exists(get_dir_location(f'__cache__/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(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'w') as f: + with open(get_dir_location(f'__cache__/mappings-{version_id}.txt'), 'w') as f: f.write(mappings_text) else: - with open(get_dir_location(f'downloads/mappings-{version_id}.txt'), 'r') as f: + with open(get_dir_location(f'__cache__/mappings-{version_id}.txt'), 'r') as f: mappings_text = f.read() return Mappings.parse(mappings_text) def get_yarn_versions(): # https://meta.fabricmc.net/v2/versions/yarn - if not os.path.exists(get_dir_location('downloads/yarn_versions.json')): + if not os.path.exists(get_dir_location('__cache__/yarn_versions.json')): print('\033[92mDownloading yarn versions...\033[m') yarn_versions_data = requests.get( 'https://meta.fabricmc.net/v2/versions/yarn').json() - with open(get_dir_location('downloads/yarn_versions.json'), 'w') as f: + with open(get_dir_location('__cache__/yarn_versions.json'), 'w') as f: json.dump(yarn_versions_data, f) else: - with open(get_dir_location('downloads/yarn_versions.json'), 'r') as f: + with open(get_dir_location('__cache__/yarn_versions.json'), 'r') as f: yarn_versions_data = json.load(f) return yarn_versions_data @@ -121,7 +121,7 @@ def get_yarn_data(version_id: str): def get_fabric_api_versions(): # https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml - if not os.path.exists(get_dir_location('downloads/fabric_api_versions.json')): + if not os.path.exists(get_dir_location('__cache__/fabric_api_versions.json')): print('\033[92mDownloading Fabric API versions...\033[m') fabric_api_versions_xml_text = requests.get( 'https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml').text @@ -138,17 +138,17 @@ def get_fabric_api_versions(): for version_el in versions_el.findall('version'): fabric_api_versions.append(version_el.text) - with open(get_dir_location('downloads/fabric_api_versions.json'), 'w') as f: + with open(get_dir_location('__cache__/fabric_api_versions.json'), 'w') as f: f.write(json.dumps(fabric_api_versions)) else: - with open(get_dir_location('downloads/fabric_api_versions.json'), 'r') as f: + with open(get_dir_location('__cache__/fabric_api_versions.json'), 'r') as f: fabric_api_versions = json.loads(f.read()) return fabric_api_versions def get_fabric_loader_versions(): # https://meta.fabricmc.net/v2/versions/loader - if not os.path.exists(get_dir_location('downloads/fabric_loader_versions.json')): + if not os.path.exists(get_dir_location('__cache__/fabric_loader_versions.json')): print('\033[92mDownloading Fabric loader versions...\033[m') fabric_api_versions_json = requests.get( 'https://meta.fabricmc.net/v2/versions/loader').json() @@ -157,10 +157,10 @@ def get_fabric_loader_versions(): for version in fabric_api_versions_json: fabric_api_versions.append(version['version']) - with open(get_dir_location('downloads/fabric_loader_versions.json'), 'w') as f: + with open(get_dir_location('__cache__/fabric_loader_versions.json'), 'w') as f: f.write(json.dumps(fabric_api_versions)) else: - with open(get_dir_location('downloads/fabric_loader_versions.json'), 'r') as f: + with open(get_dir_location('__cache__/fabric_loader_versions.json'), 'r') as f: fabric_api_versions = json.loads(f.read()) return fabric_api_versions @@ -174,14 +174,14 @@ def clear_version_cache(): 'fabric_loader_versions.json' ] for file in files: - if os.path.exists(get_dir_location(f'downloads/{file}')): - os.remove(get_dir_location(f'downloads/{file}')) + if os.path.exists(get_dir_location(f'__cache__/{file}')): + os.remove(get_dir_location(f'__cache__/{file}')) - burger_path = get_dir_location("downloads/Burger") + burger_path = get_dir_location("__cache__/Burger") if os.path.exists(burger_path): os.system( f'cd {burger_path} && git pull') - pixlyzer_path = get_dir_location('downloads/pixlyzer') + pixlyzer_path = get_dir_location('__cache__/pixlyzer') if os.path.exists(pixlyzer_path): os.system( f'cd {pixlyzer_path} && git pull')
\ No newline at end of file diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py index f7c78d29..608673fa 100755 --- a/codegen/lib/extract.py +++ b/codegen/lib/extract.py @@ -12,27 +12,44 @@ import os def generate_data_from_server_jar(version_id: str): - if os.path.exists(get_dir_location(f'downloads/generated-{version_id}')): + if os.path.exists(get_dir_location(f'__cache__/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}")}\"' + f'cd {get_dir_location(f"__cache__")} && java -DbundlerMainClass=net.minecraft.data.Main -jar {get_dir_location(f"__cache__/server-{version_id}.jar")} --all --output \"{get_dir_location(f"__cache__/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: + with open(get_dir_location(f'__cache__/generated-{version_id}/reports/blocks.json'), 'r') as f: return json.load(f) def get_registries_report(version_id: str): generate_data_from_server_jar(version_id) - with open(get_dir_location(f'downloads/generated-{version_id}/reports/registries.json'), 'r') as f: + with open(get_dir_location(f'__cache__/generated-{version_id}/reports/registries.json'), 'r') as f: return json.load(f) +def get_registry_tags(version_id: str, name: str): + generate_data_from_server_jar(version_id) + tags_directory = get_dir_location(f'__cache__/generated-{version_id}/data/minecraft/tags/{name}') + if not os.path.exists(tags_directory): + return {} + tags = {} + for root, dirs, files in os.walk(tags_directory, topdown=False): + for name in files: + file = os.path.join(root, name) + relative_path = file.replace(tags_directory, '')[1:] + if not file.endswith('.json'): + continue + with open(file, 'r') as f: + tags[relative_path[:-5]] = json.load(f) + return tags + + def get_block_states_burger(version_id: str): burger_data = get_burger_data_for_version(version_id) return burger_data[0]['blocks']['block'] @@ -96,15 +113,15 @@ def run_python_command_and_download_deps(command): def get_burger_data_for_version(version_id: str): - if not os.path.exists(get_dir_location(f'downloads/burger-{version_id}.json')): + if not os.path.exists(get_dir_location(f'__cache__/burger-{version_id}.json')): get_burger() get_client_jar(version_id) print('\033[92mRunning Burger...\033[m') run_python_command_and_download_deps( - f'cd {get_dir_location("downloads/Burger")} && {determine_python_command()} munch.py {get_dir_location("downloads")}/client-{version_id}.jar --output {get_dir_location("downloads")}/burger-{version_id}.json' + f'cd {get_dir_location("__cache__/Burger")} && {determine_python_command()} munch.py {get_dir_location("__cache__")}/client-{version_id}.jar --output {get_dir_location("__cache__")}/burger-{version_id}.json' ) - with open(get_dir_location(f'downloads/burger-{version_id}.json'), 'r') as f: + with open(get_dir_location(f'__cache__/burger-{version_id}.json'), 'r') as f: return json.load(f) @@ -113,7 +130,7 @@ def get_pixlyzer_data(version_id: str, category: str): Gets data from Pixlyzer. Note that this requires Yarn to release updates first. ''' - target_dir = get_dir_location(f'downloads/pixlyzer-{version_id}') + target_dir = get_dir_location(f'__cache__/pixlyzer-{version_id}') # TODO: right now this False is hard-coded, it should retry with this # enabled if # initially getting the data fails @@ -249,7 +266,7 @@ def get_pixlyzer_data(version_id: str, category: str): def get_file_from_jar(version_id: str, file_dir: str): get_client_jar(version_id) - with ZipFile(get_dir_location(f'downloads/client-{version_id}.jar')) as z: + with ZipFile(get_dir_location(f'__cache__/client-{version_id}.jar')) as z: with z.open(file_dir) as f: return f.read() diff --git a/codegen/migrate.py b/codegen/migrate.py index 0222ab00..6e51c2df 100755 --- a/codegen/migrate.py +++ b/codegen/migrate.py @@ -133,9 +133,8 @@ language = lib.extract.get_en_us_lang(new_version_id) 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']) +import genregistries +genregistries.generate(new_version_id) print('Generating entity metadata...') burger_entities_data = new_burger_data[0]['entities'] |
