From a046f76a91e6b795d3c5246a81d5d026dbf5f9ca Mon Sep 17 00:00:00 2001 From: Lila Date: Mon, 23 Sep 2024 19:53:39 +0100 Subject: [PATCH] write to existing string io buffer --- fast64_internal/sm64/animation/classes.py | 22 +++++++-------------- fast64_internal/sm64/animation/exporting.py | 3 ++- fast64_internal/sm64/sm64_classes.py | 8 ++++---- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/fast64_internal/sm64/animation/classes.py b/fast64_internal/sm64/animation/classes.py index be0d653b7..246c46ebc 100644 --- a/fast64_internal/sm64/animation/classes.py +++ b/fast64_internal/sm64/animation/classes.py @@ -84,13 +84,11 @@ def to_c(self, dma_structure: bool = False): indice_table, value_table = self.create_tables() if dma_structure: - text_data.write(indice_table.to_c()) - text_data.write("\n") - text_data.write(value_table.to_c()) + indice_table.to_c(text_data, new_lines=2) + value_table.to_c(text_data) else: - text_data.write(value_table.to_c()) - text_data.write("\n") - text_data.write(indice_table.to_c()) + value_table.to_c(text_data, new_lines=2) + indice_table.to_c(text_data) return text_data.getvalue() @@ -287,12 +285,7 @@ def get_indice_reference(self, override: Optional[str | int] = None, expected_ty ), f"Indice reference must be a {expected_type}, but instead is equal to {reference}." return reference - def to_c( - self, - values_override: Optional[str] = None, - indice_override: Optional[str] = None, - dma_structure=False, - ): + def to_c(self, values_override: Optional[str] = None, indice_override: Optional[str] = None, dma_structure=False): assert not dma_structure or isinstance(self.flags, SM64_AnimFlags), "Flags must be int/enum for C DMA" return ( f"static const struct Animation {self.reference}{'[]' if dma_structure else ''} = {{\n" @@ -313,7 +306,7 @@ def to_binary( values_override: Optional[int] = None, indice_override: Optional[int] = None, segment_data: SegmentData | None = None, - length: int = 0, + length=0, ): assert isinstance(self.flags, SM64_AnimFlags), "Flags must be int/enum for binary" values_address = self.get_values_reference(values_override, int) @@ -777,8 +770,7 @@ def data_and_headers_to_c_combined(self): if data_set: indice_tables, value_tables = create_tables(data_set, self.values_reference) for table in value_tables + indice_tables: - text_data.write(table.to_c()) - text_data.write("\n") + table.to_c(text_data, new_lines=2) for anim_header in headers_set: text_data.write(anim_header.to_c()) text_data.write("\n") diff --git a/fast64_internal/sm64/animation/exporting.py b/fast64_internal/sm64/animation/exporting.py index 97ebcfe79..2c49a4bec 100644 --- a/fast64_internal/sm64/animation/exporting.py +++ b/fast64_internal/sm64/animation/exporting.py @@ -773,7 +773,7 @@ def export_animation_table_c( if not anim_props.is_dma: update_data_file( anim_directory / "data.inc.c", - files_data.keys(), + list(files_data.keys()), anim_props.override_files, ) else: @@ -783,6 +783,7 @@ def export_animation_table_c( print("All animation data files exported.") if anim_props.is_dma: # DonĀ“t create an actual table and or update includes for dma exports return + assert geo_directory and header_directory and isinstance(table.reference, str) header_path = geo_directory / "anim_header.h" update_anim_header(header_path, table.reference, anim_props.gen_enums, anim_props.override_files) diff --git a/fast64_internal/sm64/sm64_classes.py b/fast64_internal/sm64/sm64_classes.py index 20228b114..2ec157ab0 100644 --- a/fast64_internal/sm64/sm64_classes.py +++ b/fast64_internal/sm64/sm64_classes.py @@ -268,14 +268,14 @@ class IntArray: def to_binary(self): return self.data.astype(">i2").tobytes() - def to_c(self): # TODO: Use io stream arg + def to_c(self, c_data: StringIO | None = None, new_lines=1): assert self.name, "Array must have a name" data = self.data byte_count = data.itemsize data_type = f"{'s' if data.dtype == np.int16 else 'u'}{byte_count * 8}" print(f'Generating {data_type} array "{self.name}" with {len(self.data)} elements') - c_data = StringIO() + c_data = c_data or StringIO() c_data.write(f"// {len(self.data)}\n") c_data.write(f"static const {data_type} {toAlnum(self.name)}[] = {{\n\t") i = self.wrap_start @@ -286,5 +286,5 @@ def to_c(self): # TODO: Use io stream arg c_data.write("\n\t") i = 0 - c_data.write("\n};\n") - return c_data.getvalue() + c_data.write("\n};" + ("\n" * new_lines)) + return c_data