diff options
| author | mat <git@matdoes.dev> | 2025-05-31 08:56:17 +0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-05-30 19:22:09 -0845 |
| commit | f27c87b291a920376e006a470efb353dee46ed17 (patch) | |
| tree | f1720081b856c5b7b96ab8111e6de4233f884e75 /codegen/lib/extract.py | |
| parent | e37524899eef8a0034faee35cef4bbf1ba779a7d (diff) | |
| download | azalea-drasl-f27c87b291a920376e006a470efb353dee46ed17.tar.xz | |
more formatting fixes
Diffstat (limited to 'codegen/lib/extract.py')
| -rw-r--r-- | codegen/lib/extract.py | 153 |
1 files changed, 86 insertions, 67 deletions
diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py index e8fc5b4d..97f37ccf 100644 --- a/codegen/lib/extract.py +++ b/codegen/lib/extract.py @@ -1,81 +1,92 @@ # Extracting data from the Minecraft jars -from typing import TYPE_CHECKING -from lib.download import get_mappings_for_version, get_pumpkin_extractor, get_server_jar, get_burger, get_client_jar +from lib.download import ( + get_mappings_for_version, + get_pumpkin_extractor, + get_server_jar, + get_burger, + get_client_jar, +) from lib.utils import get_dir_location, to_camel_case, upper_first_letter from zipfile import ZipFile import subprocess -import requests import json -import sys import re import os def generate_data_from_server_jar(version_id: str): - if os.path.exists(get_dir_location(f'__cache__/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"__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}")}\"' + f'cd {get_dir_location("__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): - return get_report(version_id, 'blocks') + return get_report(version_id, "blocks") + + def get_registries_report(version_id: str): - return get_report(version_id, 'registries') + return get_report(version_id, "registries") + + def get_packets_report(version_id: str): - return get_report(version_id, 'packets') + return get_report(version_id, "packets") + + def get_report(version_id: str, name: str): generate_data_from_server_jar(version_id) - with open(get_dir_location(f'__cache__/generated-{version_id}/reports/{name}.json'), 'r') as f: + with open( + get_dir_location(f"__cache__/generated-{version_id}/reports/{name}.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}') + 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'): + relative_path = file.replace(tags_directory, "")[1:] + if not file.endswith(".json"): continue - with open(file, 'r') as f: + with open(file, "r") as f: tags[relative_path[:-5]] = json.load(f) return tags + python_command = None def determine_python_command(): - return 'venv/bin/python' - + return "venv/bin/python" def run_python_command_and_download_deps(command): - print('>', command) + print(">", command) for _ in range(10): - p = subprocess.Popen( - command, - stderr=subprocess.PIPE, - shell=True - ) + p = subprocess.Popen(command, stderr=subprocess.PIPE, shell=True) - stderr = b'' + stderr = b"" while True: data = p.stderr.read() - if data == b'': + if data == b"": break - print(data.decode(), end='', flush=True) + print(data.decode(), end="", flush=True) stderr += data regex_match = re.search( - r'ModuleNotFoundError: No module named \'(\w+?)\'', stderr.decode()) + r"ModuleNotFoundError: No module named \'(\w+?)\'", stderr.decode() + ) if not regex_match: out, err = p.communicate() if out: @@ -84,58 +95,61 @@ def run_python_command_and_download_deps(command): print(err) break missing_lib = regex_match.group(1) - print('Missing required lib:', missing_lib) - subprocess.run(f'venv/bin/pip install {missing_lib}', cwd=os.path.dirname(os.path.dirname(__file__))) - print('ok') + print("Missing required lib:", missing_lib) + subprocess.run( + f"venv/bin/pip install {missing_lib}", + cwd=os.path.dirname(os.path.dirname(__file__)), + ) + print("ok") def get_burger_data_for_version(version_id: str): - if not os.path.exists(get_dir_location(f'__cache__/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) get_mappings_for_version(version_id) - print('\033[92mRunning Burger...\033[m') + print("\033[92mRunning Burger...\033[m") run_python_command_and_download_deps( - f'cd {get_dir_location("__cache__/Burger")} && '\ - f'venv/bin/python munch.py {get_dir_location("__cache__")}/client-{version_id}.jar '\ - f'--output {get_dir_location("__cache__")}/burger-{version_id}.json '\ - f'--mappings {get_dir_location("__cache__")}/mappings-{version_id}.txt' + f"cd {get_dir_location('__cache__/Burger')} && " + f"venv/bin/python munch.py {get_dir_location('__cache__')}/client-{version_id}.jar " + f"--output {get_dir_location('__cache__')}/burger-{version_id}.json " + f"--mappings {get_dir_location('__cache__')}/mappings-{version_id}.txt" ) - with open(get_dir_location(f'__cache__/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) def get_pumpkin_data(version_id: str, category: str): - assert '/' not in version_id - assert '\\' not in version_id - target_parent_dir = get_dir_location(f'__cache__/pumpkin-{version_id}') - category_dir = f'{target_parent_dir}/{category}.json' + assert "/" not in version_id + assert "\\" not in version_id + target_parent_dir = get_dir_location(f"__cache__/pumpkin-{version_id}") + category_dir = f"{target_parent_dir}/{category}.json" if os.path.exists(category_dir): - with open(category_dir, 'r') as f: + with open(category_dir, "r") as f: return json.load(f) pumpkin_dir = get_pumpkin_extractor() - os.makedirs(f'{pumpkin_dir}/run', exist_ok=True) - with open(f'{pumpkin_dir}/run/eula.txt', 'w') as f: - f.write('eula=true') + os.makedirs(f"{pumpkin_dir}/run", exist_ok=True) + with open(f"{pumpkin_dir}/run/eula.txt", "w") as f: + f.write("eula=true") # run ./gradlew runServer until it logs "(pumpkin_extractor) Done" p = subprocess.Popen( - f'cd {pumpkin_dir} && ./gradlew runServer', + f"cd {pumpkin_dir} && ./gradlew runServer", stderr=subprocess.PIPE, stdout=subprocess.PIPE, - shell=True + shell=True, ) while True: data = p.stdout.readline().decode() - print('>' + data, end='', flush=True) - if '[Server thread/INFO] (pumpkin_extractor) Done' in data: - print('Pumpkin extractor done') + print(">" + data, end="", flush=True) + if "[Server thread/INFO] (pumpkin_extractor) Done" in data: + print("Pumpkin extractor done") break - if data == b'': + if data == b"": break p.terminate() @@ -144,44 +158,49 @@ def get_pumpkin_data(version_id: str, category: str): # delete target_parent_dir if it's empty if os.path.exists(target_parent_dir): os.rmdir(target_parent_dir) - os.rename(f'{pumpkin_dir}/run/pumpkin_extractor_output', target_parent_dir) + os.rename(f"{pumpkin_dir}/run/pumpkin_extractor_output", target_parent_dir) - with open(category_dir, 'r') as f: + with open(category_dir, "r") as f: return json.load(f) def get_file_from_jar(version_id: str, file_dir: str): get_client_jar(version_id) - with ZipFile(get_dir_location(f'__cache__/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() def get_en_us_lang(version_id: str): - return json.loads( - get_file_from_jar(version_id, 'assets/minecraft/lang/en_us.json') - ) + return json.loads(get_file_from_jar(version_id, "assets/minecraft/lang/en_us.json")) + # burger packet id extraction is broken since 1.20.5 (always returns -1, so we have to determine packet id ourselves from the mappings). # this is very much not ideal. -if TYPE_CHECKING: from codegen.lib.mappings import Mappings + def get_packet_list(version_id: str): - if version_id != '1.21': + if version_id != "1.21": return [] generate_data_from_server_jar(version_id) - with open(get_dir_location(f'__cache__/generated-{version_id}/reports/packets.json'), 'r') as f: + with open( + get_dir_location(f"__cache__/generated-{version_id}/reports/packets.json"), "r" + ) as f: packets_report = json.load(f) packet_list = [] for state, state_value in packets_report.items(): for direction, direction_value in state_value.items(): for packet_resourcelocation, packet_value in direction_value.items(): - assert packet_resourcelocation.startswith('minecraft:') - packet_resourcelocation = upper_first_letter(to_camel_case(packet_resourcelocation[len('minecraft:'):])) - packet_list.append({ - 'state': state, - 'direction': direction, - 'name': packet_resourcelocation, - 'id': packet_value['protocol_id'] - }) + assert packet_resourcelocation.startswith("minecraft:") + packet_resourcelocation = upper_first_letter( + to_camel_case(packet_resourcelocation[len("minecraft:") :]) + ) + packet_list.append( + { + "state": state, + "direction": direction, + "name": packet_resourcelocation, + "id": packet_value["protocol_id"], + } + ) |
