Skip to content

Commit

Permalink
enums that actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilaa3 committed Apr 9, 2024
1 parent e4a1cc0 commit 8f2fb1c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
54 changes: 41 additions & 13 deletions fast64_internal/sm64/animation/exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,51 @@ def update_includes(level_name: str, group_name: str, dir_name: str, dir_path: s
writeIfNotFound(header_path, f'\n#include "levels/{level_name}/{dir_name}/anim_header.h"', "\n#endif")


def write_anim_header(anim_header_path: str, table_name: str):
def write_anim_header(
anim_header_path: str,
table_name: str,
generate_enums: bool,
):
with open(anim_header_path, "w", newline="\n") as file:
if generate_enums:
file.write('#include "anims/table_enum.h"\n')
file.write(f"extern const struct Animation *const {table_name}[];\n")


def update_enum_file(
enum_path: str,
enum_list_name: str,
header_names: list[str],
override_files: bool,
):
if override_files or not os.path.exists(enum_path):
text = ""
else:
with open(enum_path, "r") as file:
text = file.read()

enum_list_index = text.find(enum_list_name)
if enum_list_index == -1: # If there is no enum list, add one and find again
text = f"enum {enum_list_name} {{\n}};\n\n" + text
enum_list_index = text.find(enum_list_name)

for header_name in header_names:
enum_name = anim_name_to_enum(header_name)
if text.find(enum_name, enum_list_index) == -1:
enum_list_end = text.find("}", enum_list_index)
text = text[:enum_list_end] + f"\t{enum_name},\n" + text[enum_list_end:]

with open(enum_path, "w", newline="\n") as file:
file.write(text)


def update_table_file(
table_path: str,
header_names: list[str],
table_name: str,
generate_enums: bool,
override_files: bool,
enum_path: str,
enum_list_name: str,
):
if override_files or not os.path.exists(table_path):
Expand All @@ -108,19 +142,13 @@ def update_table_file(
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
text = f"enum {enum_list_name} {{\n}};\n\n" + text
enum_list_index = text.find(enum_list_name)

for header_name in header_names:
enum_name = anim_name_to_enum(header_name)
if text.find(enum_name, enum_list_index) == -1:
enum_list_end = text.find("}", enum_list_index)
text = text[:enum_list_end] + f"\t{enum_name},\n" + text[enum_list_end:]

update_enum_file(enum_path, enum_list_name, header_names, override_files)
include_text = '#include "table_enum.h"\n'
include_index = text.find(include_text)
if include_index == -1: # If there is no table, add one and find again
text += include_text
include_index = text.find(include_text)
# Table
table_index = text.find(table_name)
if table_index == -1: # If there is no table, add one and find again
Expand Down
10 changes: 6 additions & 4 deletions fast64_internal/sm64/animation/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,14 @@ def execute_operator(self, context: Context):
table_name = table_props.get_anim_table_name(actor_name)
enum_list_name = table_props.get_enum_list_name(actor_name)

write_anim_header(os.path.join(geo_dir_path, "anim_header.h"), table_name)
write_anim_header(os.path.join(geo_dir_path, "anim_header.h"), table_name, table_props.generate_enums)
update_table_file(
os.path.join(anim_dir_path, table_props.get_anim_table_file_name()),
os.path.join(anim_dir_path, "table.inc.c"),
table_props.get_header_names(actor_name),
table_name,
table_props.generate_enums,
table_props.override_files,
os.path.join(anim_dir_path, "table_enum.h"),
enum_list_name,
)
update_data_file(
Expand Down Expand Up @@ -368,13 +369,14 @@ def execute_operator(self, context: Context):
table_name = table_props.get_anim_table_name(actor_name)
enum_list_name = table_props.get_enum_list_name(actor_name)

write_anim_header(os.path.join(geo_dir_path, "anim_header.h"), table_name)
write_anim_header(os.path.join(geo_dir_path, "anim_header.h"), table_name, table_props.generate_enums)
update_table_file(
os.path.join(anim_dir_path, table_props.get_anim_table_file_name()),
os.path.join(anim_dir_path, "table.inc.c"),
[header.get_anim_name(actor_name, action) for header in action_props.headers],
table_name,
table_props.generate_enums,
table_props.override_files,
os.path.join(anim_dir_path, "table_enum.h"),
enum_list_name,
)
update_data_file(os.path.join(anim_dir_path, "data.inc.c"), [anim_file_name])
Expand Down
3 changes: 0 additions & 3 deletions fast64_internal/sm64/animation/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,6 @@ def get_anim_table_name(self, actor_name):
return self.custom_table_name
return f"{actor_name}_anims"

def get_anim_table_file_name(self):
return "table.inc.c"

def get_enum_list_name(self, actor_name: str):
return f"{actor_name.title()}Anims"

Expand Down

0 comments on commit 8f2fb1c

Please sign in to comment.