diff --git a/anim_header.h b/anim_header.h new file mode 100644 index 000000000..5556acc32 --- /dev/null +++ b/anim_header.h @@ -0,0 +1,2 @@ +extern enum MarioAnims; +extern const struct Animation *const mario_anims[]; diff --git a/fast64_internal/sm64/animation/classes.py b/fast64_internal/sm64/animation/classes.py index e62382574..00f85e39a 100644 --- a/fast64_internal/sm64/animation/classes.py +++ b/fast64_internal/sm64/animation/classes.py @@ -294,22 +294,28 @@ def headers_to_c(self, is_dma_structure: bool) -> str: data.write("\n") return data.getvalue() - def data_to_c(self): + def data_to_c(self, is_dma_structure: bool): if self.reference: return values_table, indices_table = self.create_tables() data = StringIO() - data.write(values_table.to_c()) - data.write("\n\n") - data.write(indices_table.to_c()) + + if is_dma_structure: + data.write(indices_table.to_c()) + data.write("\n\n") + data.write(values_table.to_c()) + else: + data.write(values_table.to_c()) + data.write("\n\n") + data.write(indices_table.to_c()) return data.getvalue() def to_c(self, is_dma_structure: bool): data = StringIO() - c_data = self.data_to_c() + c_data = self.data_to_c(is_dma_structure) c_headers = self.headers_to_c(is_dma_structure) if is_dma_structure: data.write(c_headers) diff --git a/fast64_internal/sm64/animation/exporting.py b/fast64_internal/sm64/animation/exporting.py index abcb0257b..f21dc25c3 100644 --- a/fast64_internal/sm64/animation/exporting.py +++ b/fast64_internal/sm64/animation/exporting.py @@ -128,11 +128,11 @@ def updateIncludes(level_name, groupName, dir_name, dir_path, header_type: str): writeIfNotFound(groupPathH, '\n#include "levels/' + level_name + "/" + dir_name + '/anim_header.h"', "\n#endif") -def write_anim_header(geo_dir_path: str, anims_name: str): - headerPath = os.path.join(geo_dir_path, "anim_header.h") - headerFile = open(headerPath, "w", newline="\n") - headerFile.write("extern const struct Animation *const " + anims_name + "[];\n") - headerFile.close() +def write_anim_header(anim_header_path: str, table_name: str, generate_enums: bool, enum_list_name: str): + with open(anim_header_path, "w", newline="\n") as file: + if generate_enums: + file.write(f"extern enum {enum_list_name};\n") + file.write(f"extern const struct Animation *const {table_name}[];\n") def update_table_file(table_path: str, header_names: str, table_name: str, generate_enums: bool, enum_list_name: str): @@ -142,6 +142,7 @@ def update_table_file(table_path: str, header_names: str, table_name: str, gener with open(table_path, "r") as file: text = file.read() + # Enum if generate_enums: enum_list_index = text.find(enum_list_name) if enum_list_index == -1: # If there is no enum list, add one and find again @@ -154,6 +155,7 @@ def update_table_file(table_path: str, header_names: str, table_name: str, gener enum_list_end = text.find("}", enum_list_index) text = text[:enum_list_end] + f"\t{enum_name},\n" + text[enum_list_end:] + # Table table_index = text.find(table_name) if table_index == -1: # If there is no table, add one and find again text += f"const struct Animation *const {table_name}[] = {{\n\tNULL,\n}};\n" @@ -176,7 +178,7 @@ def update_table_file(table_path: str, header_names: str, table_name: str, gener else: text = text[:table_end] + f"\t{header_reference},\n" + text[table_end:] - with open(table_path, "w") as file: + with open(table_path, "w", newline="\n") as file: file.write(text) diff --git a/fast64_internal/sm64/animation/operators.py b/fast64_internal/sm64/animation/operators.py index e70633691..38d6652c5 100644 --- a/fast64_internal/sm64/animation/operators.py +++ b/fast64_internal/sm64/animation/operators.py @@ -292,15 +292,9 @@ def execute_operator(self, context): ) if sm64_props.export_type == "C": - anim_dir_path, dir_path, geo_dir_path, level_name = anim_export_props.get_animation_paths() - - if not os.path.exists(dir_path): - os.mkdir(dir_path) - if not os.path.exists(geo_dir_path): - os.mkdir(geo_dir_path) - if not os.path.exists(anim_dir_path): - os.mkdir(anim_dir_path) - + anim_dir_path, dir_path, geo_dir_path, level_name = anim_export_props.get_animation_paths( + create_directories=True + ) anim_file_name = action_props.get_anim_file_name(action) anim_path = os.path.join(anim_dir_path, anim_file_name) if anim_export_props.header_type != "Custom": @@ -309,19 +303,26 @@ def execute_operator(self, context): with open(anim_path, "w", newline="\n") as file: file.write(animation.to_c(anim_export_props.is_dma_structure(sm64_props))) - table_name = table_props.get_anim_table_name(anim_export_props.actor_name) - write_anim_header(geo_dir_path, table_name) - - table_path = os.path.join(anim_dir_path, table_props.get_anim_table_file_name()) - data_file_path = os.path.join(anim_dir_path, "data.inc.c") - update_table_file( - table_path, - [header.get_anim_name(anim_export_props.actor_name, action) for header in action_props.get_headers()], - table_name, - table_props.generate_enums, - table_props.get_enum_list_name(anim_export_props.actor_name), - ) - updateDataFile(data_file_path, anim_file_name) + if anim_export_props.header_type != "DMA": + table_name = table_props.get_anim_table_name(anim_export_props.actor_name) + enum_list_name = table_props.get_enum_list_name(anim_export_props.actor_name) + + anim_header_path = os.path.join(geo_dir_path, "anim_header.h") + write_anim_header(anim_header_path, table_name, table_props.generate_enums, enum_list_name) + + table_path = os.path.join(anim_dir_path, table_props.get_anim_table_file_name()) + data_file_path = os.path.join(anim_dir_path, "data.inc.c") + update_table_file( + table_path, + [ + header.get_anim_name(anim_export_props.actor_name, action) + for header in action_props.get_headers() + ], + table_name, + table_props.generate_enums, + enum_list_name, + ) + updateDataFile(data_file_path, anim_file_name) if not anim_export_props.header_type in {"Custom", "DMA"}: updateIncludes( diff --git a/fast64_internal/sm64/animation/properties.py b/fast64_internal/sm64/animation/properties.py index 1a59fa967..f0bf52c36 100644 --- a/fast64_internal/sm64/animation/properties.py +++ b/fast64_internal/sm64/animation/properties.py @@ -706,7 +706,7 @@ def is_dma_structure(self, sm64_props): return self.use_dma_structure return False - def get_animation_paths(self): + def get_animation_paths(self, create_directories: bool = False): custom_export = self.header_type == "Custom" exportPath, level_name = getPathAndLevel( @@ -719,13 +719,21 @@ def get_animation_paths(self): dirName = toAlnum(self.actor_name) if self.header_type == "DMA": - anim_dir_path = os.path.join(exportPath, self.DMAFolder) + anim_dir_path = os.path.join(exportPath, self.dma_folder) dir_path = "" geo_dir_path = "" else: dir_path = getExportDir(custom_export, exportPath, self.header_type, level_name, "", dirName)[0] geo_dir_path = os.path.join(dir_path, dirName) anim_dir_path = os.path.join(geo_dir_path, "anims") + if create_directories: + if not os.path.exists(dir_path): + os.mkdir(dir_path) + if not os.path.exists(geo_dir_path): + os.mkdir(geo_dir_path) + + if create_directories and not os.path.exists(anim_dir_path): + os.mkdir(anim_dir_path) return ( abspath(anim_dir_path),