make getting shapes faster (#149)

* supposed to make getting shapes faster

* why was this reversed

* forgot to run codegen

* don't panic when getting the shape for invalid block ids

---------

Co-authored-by: mat <git@matdoes.dev>
This commit is contained in:
urisinger 2024-05-23 05:08:15 +03:00 committed by GitHub
parent 73bcc6639b
commit f9c28ca5fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 3021 additions and 7177 deletions

File diff suppressed because it is too large Load diff

View file

@ -73,9 +73,11 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report,
generated_shape_code += generate_code_for_shape(shape_id, shape)
# 1..100 | 200..300 => &SHAPE1,
generated_match_inner_code = ''
shape_ids_to_block_state_ids = {}
# static SHAPES_MAP: [&Lazy<VoxelShape>; 26644] = [&SHAPE0, &SHAPE1, &SHAPE1, ...]
empty_shapes = []
full_shapes = []
block_state_ids_to_shape_ids = []
for block_id, shape_ids in blocks.items():
if isinstance(shape_ids, int):
shape_ids = [shape_ids]
@ -84,19 +86,24 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report,
for possible_state, shape_id in zip(block_report_data['states'], shape_ids):
block_state_id = possible_state['id']
if shape_id not in shape_ids_to_block_state_ids:
shape_ids_to_block_state_ids[shape_id] = []
shape_ids_to_block_state_ids[shape_id].append(block_state_id)
if shape_id == 0 :
empty_shapes.append(block_state_id)
elif shape_id == 1 :
full_shapes.append(block_state_id)
empty_shape_match_code = convert_ints_to_rust_ranges(shape_ids_to_block_state_ids[0])
block_shape_match_code = convert_ints_to_rust_ranges(shape_ids_to_block_state_ids[1])
block_state_ids_to_shape_ids.append((block_state_id, shape_id))
# shape 1 is the most common so we have a _ => &SHAPE1 at the end
del shape_ids_to_block_state_ids[1]
for shape_id, block_state_ids in shape_ids_to_block_state_ids.items():
generated_match_inner_code += f'{convert_ints_to_rust_ranges(block_state_ids)} => &SHAPE{shape_id},\n'
generated_match_inner_code += '_ => &SHAPE1'
generated_map_code = f'static SHAPES_MAP: [&Lazy<VoxelShape>; {len(block_state_ids_to_shape_ids)}] = ['
block_state_ids_to_shape_ids = sorted(block_state_ids_to_shape_ids, key=lambda x: x[0])
empty_shape_match_code = convert_ints_to_rust_ranges(empty_shapes)
block_shape_match_code = convert_ints_to_rust_ranges(full_shapes)
for block_state_id, shape_id in block_state_ids_to_shape_ids:
generated_map_code += f'&SHAPE{shape_id},\n'
generated_map_code += '];'
if empty_shape_match_code == '':
print('Error: shape 0 was not found')
@ -126,11 +133,10 @@ pub trait BlockWithShape {{
{generated_shape_code}
impl BlockWithShape for BlockState {{
fn shape(&self) -> &'static VoxelShape {{
match self.id {{
{generated_match_inner_code}
}}
SHAPES_MAP.get(self.id as usize).unwrap_or(&&SHAPE1)
}}
fn is_shape_empty(&self) -> bool {{
@ -141,8 +147,12 @@ impl BlockWithShape for BlockState {{
matches!(self.id, {block_shape_match_code})
}}
}}
{generated_map_code}
'''
def generate_code_for_shape(shape_id: str, parts: list[list[float]]):
def make_arguments(part: list[float]):

View file

@ -236,9 +236,9 @@ def get_pixlyzer_data(version_id: str, category: str):
assert pom_xml_dependencies != ''
pom_xml = open(f'{pixlyzer_dir}/pom.xml', 'r').read()
pom_xml = re.sub(
'<dependencies>.*?</dependencies>', f'<dependencies>{pom_xml_dependencies}</dependencies>', pom_xml, flags=re.DOTALL)
r'<dependencies>.*?</dependencies>', f'<dependencies>{pom_xml_dependencies}</dependencies>', pom_xml, flags=re.DOTALL)
pom_xml = re.sub(
'<minecraft\.version>.*?</minecraft\.version>', f'<minecraft.version>{version_id}</minecraft.version>', pom_xml, flags=re.DOTALL)
r'<minecraft\.version>.*?</minecraft\.version>', f'<minecraft.version>{version_id}</minecraft.version>', pom_xml, flags=re.DOTALL)
open(f'{pixlyzer_dir}/pom.xml', 'w').write(pom_xml)
# compile