Skip to content

Commit

Permalink
write to existing string io buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilaa3 committed Sep 23, 2024
1 parent a5766a4 commit a046f76
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
22 changes: 7 additions & 15 deletions fast64_internal/sm64/animation/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion fast64_internal/sm64/animation/exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions fast64_internal/sm64/sm64_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit a046f76

Please sign in to comment.