2020-10-21 02:59:09 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import re
|
2021-01-02 08:51:14 +01:00
|
|
|
from pathlib import Path
|
2022-04-08 20:25:49 +02:00
|
|
|
from unicodedata import name
|
2020-10-21 02:59:09 +02:00
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
root_dir = script_dir + "/../"
|
|
|
|
src_dir = root_dir + "src/"
|
2021-02-22 10:21:23 +01:00
|
|
|
asm_dir = root_dir + "ver/current/asm/"
|
2020-10-21 02:59:09 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Replace many functions with one")
|
2020-11-20 05:08:05 +01:00
|
|
|
parser.add_argument("from_list", help="path to line-separated file of functions to be replaced. first line is the string to replace them with")
|
2020-10-21 02:59:09 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
from_funcs = []
|
|
|
|
|
|
|
|
with open(args.from_list) as f:
|
|
|
|
from_text = f.readlines()
|
|
|
|
|
2022-11-10 03:00:56 +01:00
|
|
|
to_line = """
|
|
|
|
extern s32 N(ItemChoice_HasSelectedItem);
|
|
|
|
extern s32 N(ItemChoice_SelectedItemID);
|
2020-10-21 02:59:09 +02:00
|
|
|
|
2022-11-10 03:00:56 +01:00
|
|
|
ApiStatus N(ItemChoice_SaveSelected)(Evt* script, s32 isInitialCall) {
|
|
|
|
Bytecode* args = script->ptrReadPos;
|
|
|
|
|
|
|
|
N(ItemChoice_SelectedItemID) = evt_get_variable(script, *args++);
|
|
|
|
N(ItemChoice_HasSelectedItem) = TRUE;
|
|
|
|
return ApiStatus_DONE2;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
func_name = "ItemChoice_SaveSelected"
|
|
|
|
|
|
|
|
for from_line in from_text:
|
2020-10-21 02:59:09 +02:00
|
|
|
if len(from_line.strip()) > 0:
|
|
|
|
from_funcs.append(from_line.rstrip().split(" ")[-1])
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(src_dir):
|
|
|
|
for f_name in files:
|
|
|
|
if f_name.endswith(".c"):
|
|
|
|
f_path = os.path.join(root, f_name)
|
|
|
|
with open(f_path) as f:
|
|
|
|
f_text_orig = f.read()
|
2020-11-20 05:08:05 +01:00
|
|
|
|
2020-10-21 02:59:09 +02:00
|
|
|
f_text = f_text_orig
|
|
|
|
for func in from_funcs:
|
|
|
|
search_pattern = re.compile("\n.*" + func + "\).*\n")
|
2022-11-10 03:00:56 +01:00
|
|
|
f_text = re.sub(search_pattern, to_line, f_text)
|
2020-10-21 02:59:09 +02:00
|
|
|
if f_text != f_text_orig:
|
|
|
|
with open(f_path, "w", newline="\n") as f:
|
|
|
|
f.write(f_text)
|
2020-11-20 05:08:05 +01:00
|
|
|
|
2022-04-08 20:25:49 +02:00
|
|
|
# # Rename symbols in from_funcs to namespace equivalents
|
|
|
|
# for root, dirs, files in os.walk
|
|
|
|
|
2022-11-10 03:00:56 +01:00
|
|
|
to_replace = []
|
|
|
|
|
2020-11-22 05:32:54 +01:00
|
|
|
for root, dirs, files in os.walk(asm_dir):
|
|
|
|
for f_name in files:
|
|
|
|
if f_name.endswith(".s"):
|
|
|
|
f_path = os.path.join(root, f_name)
|
|
|
|
with open(f_path) as f:
|
|
|
|
f_text_orig = f.read()
|
|
|
|
|
|
|
|
f_text = f_text_orig
|
2022-04-08 20:25:49 +02:00
|
|
|
|
2022-11-10 03:00:56 +01:00
|
|
|
namespace = Path(f_path).parent.name
|
|
|
|
non_matching = "nonmatchings" in str(Path(f_path))
|
|
|
|
|
|
|
|
if non_matching:
|
2022-04-08 20:25:49 +02:00
|
|
|
namespace = Path(f_path).parent.name
|
2022-11-10 03:00:56 +01:00
|
|
|
if "world" not in str(Path(f_path)):
|
|
|
|
namespace = Path(f_path).parent.name
|
2022-04-08 20:25:49 +02:00
|
|
|
|
2022-11-10 03:00:56 +01:00
|
|
|
namespace = "dead_" + namespace
|
2022-04-08 20:25:49 +02:00
|
|
|
|
2020-11-22 05:32:54 +01:00
|
|
|
for func in from_funcs:
|
2022-04-08 20:25:49 +02:00
|
|
|
f_text = f_text.replace(func, namespace + "_" + func_name)
|
2020-11-22 05:32:54 +01:00
|
|
|
if f_text != f_text_orig:
|
2022-11-10 03:00:56 +01:00
|
|
|
if non_matching:
|
|
|
|
syms = sorted(set(re.findall(r"D_[0-9A-F]{8}_[0-9A-F]{6}", f_text)))
|
|
|
|
to_replace.append(syms[0] + " " + namespace + "_ItemChoice_HasSelectedItem\n")
|
|
|
|
to_replace.append(syms[1] + " " + namespace + "_ItemChoice_SelectedItemID\n")
|
|
|
|
|
2020-11-22 05:32:54 +01:00
|
|
|
with open(f_path, "w", newline="\n") as f:
|
|
|
|
f.write(f_text)
|
2022-11-10 03:00:56 +01:00
|
|
|
|
|
|
|
with open("to_rename.txt", "w") as f:
|
|
|
|
f.writelines(to_replace)
|