summaryrefslogtreecommitdiff
path: root/tools/m2ctx.py
blob: 55dd79dfd8670b05e666100992261b7f2d0aaed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3

import argparse
import os
import sys
import subprocess
import tempfile
from pathlib import Path

script_dir = Path(os.path.dirname(os.path.realpath(__file__)))
root_dir = script_dir / ".."
src_dir = root_dir / "src"

# Project-specific
CPP_FLAGS = [
    "-Iinclude",
    "-Iinclude/libc",
    "-Isrc",
    "-Ibuild/n64-us",
    "-I.",
    "-Iextracted/n64-us",

    "-DF3DEX_GBI_2",
    "-DF3DEX_GBI_PL",
    "-DGBI_DOWHILE",
    "-D__sgi",
    "-D_LANGUAGE_C",
    "-DNON_MATCHING",
    "-D_Static_assert(x, y)=",
    "-D__attribute__(x)=",
    "-D_MIPS_SZLONG=32",
    "-ffreestanding",
    "-DM2CTX",

    "-std=gnu89",
]

# Read through the processes context and replace whatever
def custom_replacements(output):
    actorList = []
    output = output.splitlines()

    i = 0
    while i < len(output):
        line = output[i]

        ############### actorLists[2].first -> Player* ###############
        if "typedef struct ActorListEntry " in line:
            actorListText = ""
            i += 1
            while not output[i].startswith("}"):
                actorListText += output[i]
                i += 1
            actorCats = [
                "actorSwitch",
                "bg",
                "player",
                "explosive",
                "npc",
                "enemy",
                "prop",
                "itemAction",
                "misc",
                "boss",
                "door",
                "chest",
            ]
            for x in range(12):
                actorList.append(actorListText.replace("first;", f"{actorCats[x]};") + "\n")
                if x == 2:
                    actorList[x] = actorList[x].replace("Actor*", "struct Player*")
        elif "ActorListEntry actorLists[ACTORCAT_MAX];" in line:
            output[i] = "struct {\n" + "".join(actorList) + "};"
        ########################################################

        i += 1
    return "\n".join(output)

def import_c_file(in_file) -> str:
    in_file = os.path.relpath(in_file, root_dir)

    cpp_command = ["gcc", "-E", "-P", "-undef", "-dM", *CPP_FLAGS, in_file]
    cpp_command2 = ["gcc", "-E", "-P", "-undef", *CPP_FLAGS, in_file]

    with tempfile.NamedTemporaryFile(suffix=".c") as tmp:
        stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-undef", "-dM", tmp.name], cwd=root_dir, encoding="utf-8")

    out_text = ""
    try:
        out_text += subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8")
        out_text += subprocess.check_output(cpp_command2, cwd=root_dir, encoding="utf-8")
    except subprocess.CalledProcessError:
        print(
            "Failed to preprocess input file, when running command:\n"
            + " ".join(cpp_command),
            file=sys.stderr,
            )
        sys.exit(1)

    if not out_text:
        print("Output is empty - aborting")
        sys.exit(1)

    for line in stock_macros.strip().splitlines():
        out_text = out_text.replace(line + "\n", "")
    return out_text


def main():
    parser = argparse.ArgumentParser(usage="./m2ctx.py path/to/file.c or ./m2ctx.py (from an actor or gamestate's asm dir)",
                                     description="Creates a ctx.c file for m2c or decomp.me. "
                                     "Output will be saved as ctx.c")
    parser.add_argument('filepath', help="path of c file to be processed")
    parser.add_argument("--custom", "-c", dest="custom", action="store_true", default=False,
                        help="Apply custom replacements to the output to help aid m2c output")
    args = parser.parse_args()

    c_file_path = args.filepath
    print("Using file: {}".format(c_file_path))

    output = import_c_file(c_file_path)

    if args.custom:
        output = custom_replacements(output)

    ctxPath = root_dir / "ctx.c"
    with ctxPath.open("w", encoding="UTF-8") as f:
        f.write(output)


if __name__ == "__main__":
    main()