1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-12-01 15:26:01 +00:00
oot/tools/split_out_overlay_funcs.py

79 lines
2.4 KiB
Python
Raw Normal View History

2020-03-17 04:31:30 +00:00
#!/usr/bin/python3
import os
import sys
import re
from disassemble import get_z_name
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = script_dir + "/../"
src_dir = root_dir + "src/overlays/"
asm_dir = root_dir + "asm/non_matchings/overlays/"
def get_c_file_path(file):
c_file = get_z_name(file) + ".c"
for root, dirs, files in os.walk(src_dir):
if c_file in files:
return os.path.join(root, c_file)
return None
def handle_file(asm_root, asm_file, c_file_path):
file_path = os.path.join(asm_root, asm_file)
with open(file_path) as f:
file_lines = f.readlines()
2020-03-17 04:31:30 +00:00
new_files = []
num_rodata = 0
for i, line in enumerate(file_lines):
asm_basename = asm_file.split(".")[0]
if line.startswith("glabel func"):
new_files.append((i, line.split(" ")[1].strip() + ".s", "func"))
elif line.startswith(".section .data"):
new_files.append((i, asm_basename + ".data.s", "data"))
elif line.startswith(".section .rodata"):
type = "rodata"
ext = ".rodata.s"
if num_rodata > 0:
type = "reloc"
ext = ".reloc.s"
num_rodata += 1
new_files.append((i, asm_basename + ext, type))
elif line.startswith(".bss"):
new_files.append((i, asm_basename + ".bss.s", "bss"))
if len(new_files) == 1:
return
os.remove(file_path)
for i, new_file in enumerate(new_files):
if i < len(new_files) - 1:
new_file_lines = file_lines[new_file[0]:new_files[i+1][0]]
else:
new_file_lines = file_lines[new_file[0]:]
2020-03-17 04:31:30 +00:00
with open(os.path.join(asm_root, new_file[1]), mode="w", newline="\n") as out_file:
out_file.writelines(new_file_lines)
2020-03-17 04:31:30 +00:00
os.remove(c_file_path)
pragma_begin = "#pragma GLOBAL_ASM(\"" + asm_root.split("../")[1] + "/"
pragma_end = "\")"
with open(c_file_path, mode="w", newline="\n") as c_file:
for new_file in new_files:
c_file.write(pragma_begin + new_file[1] + pragma_end + "\n\n")
def main():
for root, dirs, files in os.walk(asm_dir):
for file in files:
if file.startswith("z_"):
c_file_path = get_c_file_path(root.split("/")[-1])
if c_file_path is not None:
handle_file(root, file, c_file_path)
main()