papermario/tools/substitute2.py
Maide 0ec2010b9b
dro and flo (#285)
* dro_01

* UnkFunc25

* UnkFunc26

* UnkFunc27

* Fixup old UnkFuncs

* a

* UnkFunc28

* UnkFunc29

* UnkFunc30

* dro_02

* UnkFunc30

* UnkFunc31

* UnkFunc33

* UnkFunc34

* UnkFunc35

* UnkFunc36

* Fix splat.yaml and remove asm

* Fixed finally

* a

* flo_00

* update func

* flo_03

* flo_07

* flo_08

* UnkFunc37 & 38

* UnkFunc39

* UnkFunc40

* a

* Some copies

* Func

* flo_09

* Fix linker alignment warnings

* flo_10 (partial)

* flo_11

* flo_12

* flo_13

* UnkFunc41

* Copy funcs

* UnkFunc42

* flo_14

* UnkFunc43

* UnkFunc44

* flo_16

* UnkFunc44

* UnkFunc45

* flo_15

* flo_17

* flo_18

* flo_19

* flo_21

* flo_22

* flo_23

* flo_24

* flo_25

* cya asm

* a

* a

* Finally OK!

* Alter scripts

* Fix food func

* PR1

* PR2

* Add tree structs to disassembly and change names

* tabs to spaces and callback &

* Fix the script too

* PR3

* Fucking newlines
2021-04-30 02:09:30 +09:00

137 lines
4.4 KiB
Python

from pathlib import Path
RUN_ME = False
if not RUN_ME:
print(f"Are you sure yo uwant to run this? Edit RUN_ME in the script if so")
exit()
FUNC=""" Enemy* enemy = script->owner1.enemy;
Npc* npc = get_npc_unsafe(enemy->npcID);
npc->duration--;
if (npc->duration <= 0) {
enemy->varTable[2] = 0;
npc->duration = 0;
script->functionTemp[0].s = 0;
}
}""".splitlines()
NEW_FUNC_NAME = f"UnkFunc45"
NEW_INCLUDE = f"#include \"world/common/{NEW_FUNC_NAME}.inc.c\""
RENAMED = []
def parse_folder(path):
for entry in path.iterdir():
if entry.is_dir():
parse_folder(entry)
continue
if not "area" in str(entry):
continue
area_name = entry.parts[3]
fd = entry.read_text().splitlines()
i = 0
while i < len(fd):
if (#i+2 < len(fd) and
fd[i].startswith("INCLUDE_ASM") or
fd[i].startswith("ApiStatus N(") or
fd[i].startswith("void N(")):
#and fd[i+1] == "/*"):
if fd[i].startswith("INCLUDE_ASM"):
base_fd = i+3
base_name = i+2
elif fd[i].startswith("ApiStatus N(") or fd[i].startswith("void N("):
base_fd = i+1
base_name = i
if len(fd[base_fd:]) < len(FUNC):
i += 1
continue
#print(f"Starting from {fd[base_fd]}")
for func, test in zip(fd[base_fd:], FUNC):
if func != test:
break
else:
print(f"Found a match in {entry}: {fd[i]}")
#print("\n".join(fd[i:i+3+len(FUNC)+1]))
#print("\n".join(fd))
include_path = Path("src/world/common/") / (NEW_FUNC_NAME + ".inc.c")
if not include_path.is_file():
name_start = fd[base_name].find("N(")
name_end = fd[base_name].find("(",name_start+2)
new_file_name = fd[base_name][:name_start] + "N(" + NEW_FUNC_NAME + ")" + fd[base_name][name_end:]
new_fd = ["#include \"common.h\"",
"#include \"map.h\"",
"",
new_file_name,
]
new_fd.extend(FUNC)
include_path.write_text("\n".join(new_fd))
if fd[i].startswith("INCLUDE_ASM"):
old_func_name = fd[i].split(",")[2].strip().replace(");", "")
else:
old_func_name = fd[i].split("N(",1)[1].split(")",1)[0]
RENAMED.append({"name":old_func_name, "area":area_name})
fd = fd[:i] + [NEW_INCLUDE] + fd[base_fd+len(FUNC)+1:]
if i+1 < len(fd) and fd[i+1] != "":
fd = fd[:i+1] + [""] + fd[i+1:]
if fd[-1] != "":
fd.append("")
#print("\n".join(fd))
entry.write_text("\n".join(fd))
break
i += 1
parse_folder(Path("src/world"))
def do_renames_asm(path):
for entry in path.iterdir():
if entry.is_dir():
do_renames_asm(entry)
continue
fd = entry.read_text()
for rename in RENAMED:
if rename["name"] in fd:
fd = fd.replace(rename["name"], f"{rename['area']}_{NEW_FUNC_NAME}")
entry.write_text(fd)
do_renames_asm(Path("ver/us/asm"))
def do_renames_src(path):
for entry in path.iterdir():
if entry.is_dir():
do_renames_src(entry)
continue
fd = entry.read_text().splitlines()
i = 0
renamed = False
while i < len(fd):
for rename in RENAMED:
start = fd[i].find(rename["name"])
if start > -1:
renamed = True
if fd[i][start-1] == "(":
fd[i] = fd[i].replace(rename["name"], f"{NEW_FUNC_NAME}")
else:
fd[i] = fd[i].replace(rename["name"], f"N({NEW_FUNC_NAME})")
i += 1
if renamed:
entry.write_text("\n".join(fd))
do_renames_src(Path("src/world"))