From b3f65f9d4b3b625309e5b92aae4221e116e9a068 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 1 May 2025 21:26:55 -0930 Subject: drop dependency on pixlyzer and start using pumpkin extractor --- codegen/lib/code/blocks.py | 17 +++-- codegen/lib/code/shapes.py | 97 +++++++++++++------------- codegen/lib/download.py | 45 +++++++++--- codegen/lib/extract.py | 167 ++++++++++----------------------------------- 4 files changed, 135 insertions(+), 191 deletions(-) (limited to 'codegen/lib') diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py index aaa95ffa..212d080c 100644 --- a/codegen/lib/code/blocks.py +++ b/codegen/lib/code/blocks.py @@ -12,7 +12,7 @@ BLOCKS_RS_DIR = get_dir_location('../azalea-block/src/generated.rs') # - Block: Has properties and states. -def generate_blocks(blocks_report: dict, pixlyzer_block_datas: dict, ordered_blocks: list[str], burger_data: dict): +def generate_blocks(blocks_report: dict, pumpkin_block_datas: dict, ordered_blocks: list[str], burger_data: dict): with open(BLOCKS_RS_DIR, 'r') as f: existing_code = f.read().splitlines() @@ -21,6 +21,11 @@ def generate_blocks(blocks_report: dict, pixlyzer_block_datas: dict, ordered_blo burger_block_datas = burger_data[0]['blocks']['block'] + pumpkin_block_map = {} + for block_data in pumpkin_block_datas['blocks']: + block_id = block_data['name'] + pumpkin_block_map[block_id] = block_data + # Find properties properties = {} @@ -77,8 +82,8 @@ def generate_blocks(blocks_report: dict, pixlyzer_block_datas: dict, ordered_blo new_make_block_states_macro_code.append(' Blocks => {') for block_id in ordered_blocks: block_data_report = blocks_report['minecraft:' + block_id] - block_data_pixlyzer = pixlyzer_block_datas.get(f'minecraft:{block_id}', {}) block_data_burger = burger_block_datas.get(block_id, {}) + block_data_pumpkin = pumpkin_block_map[block_id] default_property_variants: dict[str, str] = {} for state in block_data_report['states']: @@ -116,11 +121,11 @@ def generate_blocks(blocks_report: dict, pixlyzer_block_datas: dict, ordered_blo # make the block behavior behavior_constructor = 'BlockBehavior::new()' # requires tool - if block_data_pixlyzer.get('requires_tool'): + if block_data_burger.get('requires_correct_tool_for_drops'): behavior_constructor += '.requires_correct_tool_for_drops()' # strength - destroy_time = block_data_pixlyzer.get('hardness') - explosion_resistance = block_data_pixlyzer.get('explosion_resistance') + destroy_time = block_data_pumpkin.get('hardness') + explosion_resistance = block_data_pumpkin.get('blast_resistance') if destroy_time and explosion_resistance: behavior_constructor += f'.strength({destroy_time}, {explosion_resistance})' elif destroy_time: @@ -128,7 +133,7 @@ def generate_blocks(blocks_report: dict, pixlyzer_block_datas: dict, ordered_blo elif explosion_resistance: behavior_constructor += f'.explosion_resistance({explosion_resistance})' # friction - friction = block_data_pixlyzer.get('friction') + friction = block_data_burger.get('friction') if friction != None: behavior_constructor += f'.friction({friction})' diff --git a/codegen/lib/code/shapes.py b/codegen/lib/code/shapes.py index 0896bd82..3a3e6d83 100644 --- a/codegen/lib/code/shapes.py +++ b/codegen/lib/code/shapes.py @@ -5,64 +5,69 @@ COLLISION_BLOCKS_RS_DIR = get_dir_location( '../azalea-physics/src/collision/blocks.rs') -def generate_block_shapes(blocks_pixlyzer: dict, shapes: dict, aabbs: dict, block_states_report): - blocks, shapes = simplify_shapes(blocks_pixlyzer, shapes, aabbs) +def generate_block_shapes(pumpkin_block_datas: dict, block_states_report): + blocks, shapes = simplify_shapes(pumpkin_block_datas) code = generate_block_shapes_code(blocks, shapes, block_states_report) with open(COLLISION_BLOCKS_RS_DIR, 'w') as f: f.write(code) -def simplify_shapes(blocks: dict, shapes: dict, aabbs: dict): - new_id_increment = 0 - - new_shapes = {} - old_id_to_new_id = {} - - old_id_to_new_id[None] = 0 - new_shapes[0] = () - new_id_increment += 1 - - used_shape_ids = set() - # determine the used shape ids - for _block_id, block_data in blocks.items(): - block_shapes = {state.get('collision_shape') for state in block_data['states'].values()} - block_shapes.update({state.get('outline_shape') for state in block_data['states'].values()}) - for s in block_shapes: - used_shape_ids.add(s) - - for shape_id, shape in enumerate(shapes): - if shape_id not in used_shape_ids: continue - # pixlyzer gives us shapes as an index or list of indexes into the - # aabbs list - # and aabbs look like { "from": number or [x, y, z], "to": (number or vec3) } - # convert them to [x1, y1, z1, x2, y2, z2] - shape = [shape] if isinstance(shape, int) else shape - shape = [aabbs[shape_aabb] for shape_aabb in shape] - shape = tuple([( - (tuple(part['from']) if isinstance( - part['from'], list) else ((part['from'],)*3)) - + (tuple(part['to']) if isinstance(part['to'], list) - else ((part['to'],)*3)) - ) for part in shape]) - - old_id_to_new_id[shape_id] = new_id_increment - new_shapes[new_id_increment] = shape - new_id_increment += 1 - - # now map the blocks to the new shape ids +def simplify_shapes(blocks: dict) -> tuple[dict, dict]: + ''' + Returns new_blocks and new_shapes, + where new_blocks is like { grass_block: { collision: [1, 1], outline: [1, 1] } } + and new_shapes is like { 1: [ [0, 0, 0, 1, 1, 1] ] } + ''' new_blocks = {} - for block_id, block_data in blocks.items(): - block_id = block_id.split(':')[-1] + new_shapes = {} - block_collision_shapes = [state.get('collision_shape') for state in block_data['states'].values()] - block_outline_shapes = [state.get('outline_shape') for state in block_data['states'].values()] + all_shapes_ids = {} + + for block_data in blocks['blocks']: + new_block_collision_shapes = [] + new_block_outline_shapes = [] + + for state in block_data['states']: + collision_shape = [] + for box_id in state['collision_shapes']: + box = blocks['shapes'][box_id] + collision_shape.append( + tuple(box['min'] + box['max']) + ) + outline_shape = [] + for box_id in state['outline_shapes']: + box = blocks['shapes'][box_id] + outline_shape.append( + tuple(box['min'] + box['max']) + ) + + collision_shape = tuple(collision_shape) + outline_shape = tuple(outline_shape) + + if collision_shape in all_shapes_ids: + collision_shape_id = all_shapes_ids[collision_shape] + else: + collision_shape_id = len(all_shapes_ids) + all_shapes_ids[collision_shape] = collision_shape_id + new_shapes[collision_shape_id] = collision_shape + if outline_shape in all_shapes_ids: + outline_shape_id = all_shapes_ids[outline_shape] + else: + outline_shape_id = len(all_shapes_ids) + all_shapes_ids[outline_shape] = outline_shape_id + new_shapes[outline_shape_id] = outline_shape + + block_id = block_data['name'] + new_block_collision_shapes.append(collision_shape_id) + new_block_outline_shapes.append(outline_shape_id) new_blocks[block_id] = { - 'collision': [old_id_to_new_id[shape_id] for shape_id in block_collision_shapes], - 'outline': [old_id_to_new_id[shape_id] for shape_id in block_outline_shapes] + 'collision': new_block_collision_shapes, + 'outline': new_block_outline_shapes } + return new_blocks, new_shapes diff --git a/codegen/lib/download.py b/codegen/lib/download.py index 599e0b72..43ca8d2a 100644 --- a/codegen/lib/download.py +++ b/codegen/lib/download.py @@ -22,12 +22,41 @@ def get_burger(): os.system(f'cd {get_dir_location("__cache__")}/Burger && python -m venv venv && venv/bin/pip install six jawa') -def get_pixlyzer(): - if not os.path.exists(get_dir_location('__cache__/pixlyzer')): - print('\033[92mDownloading bixilon/pixlyzer...\033[m') +def get_pumpkin_extractor(): + if not os.path.exists(get_dir_location('__cache__/pumpkin-extractor')): + print('\033[92mDownloading Pumpkin-MC/Extractor...\033[m') os.system( - 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') + f'cd {get_dir_location("__cache__")} && git clone https://github.com/Pumpkin-MC/Extractor pumpkin-extractor && cd pumpkin-extractor && git pull') + + GIT_PATCH = '''diff --git a/src/main/kotlin/de/snowii/extractor/extractors/Blocks.kt b/src/main/kotlin/de/snowii/extractor/extractors/Blocks.kt +index 936cd7b..9876a4b 100644 +--- a/src/main/kotlin/de/snowii/extractor/extractors/Blocks.kt ++++ b/src/main/kotlin/de/snowii/extractor/extractors/Blocks.kt +@@ -106,12 +106,18 @@ class Blocks : Extractor.Extractor { + } + + val collisionShapeIdxsJson = JsonArray() ++ val outlineShapeIdxsJson = JsonArray() + for (box in state.getCollisionShape(EmptyBlockView.INSTANCE, BlockPos.ORIGIN).boundingBoxes) { + val idx = shapes.putIfAbsent(box, shapes.size) + collisionShapeIdxsJson.add(Objects.requireNonNullElseGet(idx) { shapes.size - 1 }) + } ++ for (box in state.getOutlineShape(EmptyBlockView.INSTANCE, BlockPos.ORIGIN).boundingBoxes) { ++ val idx = shapes.putIfAbsent(box, shapes.size) ++ outlineShapeIdxsJson.add(Objects.requireNonNullElseGet(idx) { shapes.size - 1 }) ++ } + + stateJson.add("collision_shapes", collisionShapeIdxsJson) ++ stateJson.add("outline_shapes", outlineShapeIdxsJson) + + for (blockEntity in Registries.BLOCK_ENTITY_TYPE) { + if (blockEntity.supports(state)) { +''' + os.system( + f'cd {get_dir_location("__cache__")}/pumpkin-extractor && git apply - <' + data, end='', flush=True) + if '[Server thread/INFO] (pumpkin_extractor) Done' in data: + print('Pumpkin extractor done') + break + if data == b'': + break - # map jar + download dependencies - run_python_command_and_download_deps( - f'cd {pixlyzer_dir}/wrapper && {determine_python_command()} PixLyzer.py --only-version={version_id} --dont-compile --only-map' - ) - # update the pom.xml - # list directories in pixlyzer/wrapper/data/data/dependencies/libraries - pom_xml_dependencies = ''' - org.jetbrains.kotlin - kotlin-test-junit - 1.7.21 - test - - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - 1.7.21 - - - - net.minecraft - client - ${minecraft.version} - system - ${project.basedir}/wrapper/data/data/${minecraft.version}_yarn/${minecraft.version}-exhibitionism.jar - - - de.bixilon - mbf-kotlin - 0.2.1 - - - org.objenesis - objenesis - 3.3 - - - org.apache.commons - commons-lang3 - 3.12.0 - - - com.fasterxml.jackson.core - jackson-databind - 2.14.0 - - - de.bixilon - kutil - 1.17.1 - ''' - # walk dir f'{pixlyzer_dir}/wrapper/data/data/dependencies/libraries' - for root, dirs, files in os.walk(f'{pixlyzer_dir}/wrapper/data/data/dependencies/libraries'): - for file in files: - full_path = os.path.join( - root.replace('\\', '/').replace( - f'{pixlyzer_dir}/wrapper/data/data/dependencies/libraries/'.replace('\\', '/'), ''), - file - ).replace('\\', '/') - print(full_path) - if not full_path.endswith('.jar'): - continue - split_path = full_path.split('/') - group = '' - for group_index in range(0, len(split_path) - 3): - group += split_path[group_index] + '.' - if group.endswith('.'): - group = group[:-1] - artifact = split_path[-3] - version = split_path[-2] - path = '${project.basedir}/wrapper/data/data/dependencies/libraries/' + full_path - pom_xml_dependencies += """ - - """ + group + """ - """ + artifact + """ - """ + version + """ - system - """ + path + """ - - """ - print('pom_xml_dependencies', pom_xml_dependencies) - assert pom_xml_dependencies != '' - pom_xml = open(f'{pixlyzer_dir}/pom.xml', 'r').read() - pom_xml = re.sub( - r'.*?', f'{pom_xml_dependencies}', pom_xml, flags=re.DOTALL) - pom_xml = re.sub( - r'.*?', f'{version_id}', pom_xml, flags=re.DOTALL) - open(f'{pixlyzer_dir}/pom.xml', 'w').write(pom_xml) - - # compile - os.system( - f'cd {pixlyzer_dir} && mvn clean -Dmaven.repo.local=. verify') - # run pixlyzer.py again lol - run_python_command_and_download_deps( - f'cd {pixlyzer_dir}/wrapper && {determine_python_command()} PixLyzer.py --only-version={version_id} --no-compile' - ) + p.terminate() - source_dir = get_dir_location( - f'{pixlyzer_dir}/wrapper/data/version/{version_id}') - - if not os.path.exists(source_dir): - print('PixLyzer failed, no output!') - exit() - if os.path.exists(target_dir): - os.unlink(target_dir) - os.rename( - source_dir, - target_dir - ) + # move the run/pumpkin_extractor_output directory to target_parent_dir + # 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) - with open(f'{target_dir}/{category}.min.json', '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: -- cgit v1.2.3