forked from ChefKissInc/NootedRed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNRedWrapGen.py
executable file
·178 lines (138 loc) · 4.88 KB
/
NRedWrapGen.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python3
def fix_type(type: str) -> str:
assert "const" not in type
if type in ["uchar", "byte", "undefined1"]:
ret = "uint8_t"
elif type in ["short", "ushort", "undefined2"]:
ret = "uint16_t"
elif type in ["int", "uint", "undefined4"]:
ret = "uint32_t"
elif type in ["long", "ulong", "ulonglong", "undefined8"]:
ret = "uint64_t"
elif type != "char *" and "*" in type:
ret = "void *"
else:
ret = type
return ret
def parse_param(param: str) -> tuple[str, str]:
if "*" in param:
type, name = [v.strip() for v in param.split("*")]
type += " *"
elif " " in param:
type, name = [v.strip() for v in param.split(" ")]
else:
raise AssertionError()
return (
fix_type(type),
"that" if name == "this" else name.replace("param_", "param"),
)
def get_fmt_name(name: str) -> str:
return "that" if name == "this" else name
def get_fmt_type(type: str) -> str:
table = {
"uint64_t": "0x%llX",
"uint32_t": "0x%X",
"CAILResult": "0x%X",
"uint16_t": "0x%hX",
"uint8_t": "0x%hhX",
"bool": "%d",
"char *": "%s",
"char": "%c",
"IOReturn": "0x%X",
}
if type not in table and type.endswith(" *"):
return "%p"
assert type in table
return table[type]
def to_pascal_case(inp: str) -> str:
ret = ""
i = 0
while i < len(inp):
if inp[i] == "_":
if i + 1 == len(inp):
break
ret += inp[i + 1].upper()
i += 1
else:
ret += inp[i]
i += 1
return ret[0].upper() + ret[1:]
def locate_line(lines: list[str], needle: str) -> int:
for i, line in enumerate(lines):
if needle in line:
return i
assert False
moduleToClass = {
"hwlibs": "X5000HWLibs",
"x5000": "X5000",
"x6000": "X6000",
"x6000fb": "X6000FB",
"agfxhda": "AppleGFXHDA"
}
module: str = input("Filename without extension: ./NootedRed/kern_")
assert module in moduleToClass
className = moduleToClass[module]
cpp_path: str = f"./NootedRed/kern_{module}.cpp"
hpp_path: str = f"./NootedRed/kern_{module}.hpp"
with open(cpp_path) as cpp_file:
cpp_lines: list[str] = cpp_file.readlines()
with open(hpp_path) as hpp_file:
hpp_lines: list[str] = hpp_file.readlines()
signature: str = input("Signature from \"Edit Function\": ").strip().replace(
" *", "*")
signature_parts: list[str] = signature.split(" ")
return_type: str = fix_type(signature_parts[0].replace("*", " *"))
func_ident: str = signature_parts[1]
func_ident_pascal: str = to_pascal_case(func_ident)
parameters: list[tuple[str, str]] = [parse_param(x) for x in [x.strip()
for x in signature.split("(")[1].split(")")[0].split(",")]]
params_stringified: str = ", ".join([" ".join(x) for x in parameters])
target_line: int = len(cpp_lines)
function: list[str] = [
"\n",
f"{return_type} {className}::wrap{func_ident_pascal}({params_stringified}) {{\n",
]
fmt_types: str = " ".join(
f"{get_fmt_name(x[1])}: {get_fmt_type(x[0])}" for x in parameters)
arguments: str = ", ".join(x[1] for x in parameters)
function += [f" DBGLOG(\"{module}\", \"{func_ident} << ({fmt_types})\", {arguments});\n"]
if return_type == "void":
function += [
f" FunctionCast(wrap{func_ident_pascal}, callback->org{func_ident_pascal})({arguments});\n",
f" DBGLOG(\"{module}\", \"{func_ident} >> void\");\n",
]
else:
function += [
f" auto ret = FunctionCast(wrap{func_ident_pascal}, callback->org{func_ident_pascal})({arguments});\n",
f" DBGLOG(\"{module}\", \"{func_ident} >> {get_fmt_type(return_type)}\", ret);\n",
" return ret;\n",
]
function += ["}\n"] # -- End of function --
cpp_lines[target_line:target_line] = function # Extend at index
symbol: str = input("Symbol: ").strip()
target_line: int = locate_line(
cpp_lines, "Failed to route symbols")
while "};" not in cpp_lines[target_line]:
target_line -= 1
indent = cpp_lines[target_line].split("};")[0]
cpp_lines.insert(
target_line, f"{indent} {{\"{symbol}\", wrap{func_ident_pascal}, this->org{func_ident_pascal}}},\n")
target_line = len(hpp_lines) - 1
while "wrap" not in hpp_lines[target_line]:
target_line -= 1
while ");" not in hpp_lines[target_line]:
target_line += 1
target_line += 1
hpp_lines[target_line:target_line] = [
f" static {return_type} wrap{func_ident_pascal}({params_stringified});\n",
]
while not hpp_lines[target_line].startswith(" mach_vm_address_t org"):
target_line -= 1
target_line += 1
hpp_lines[target_line:target_line] = [
f" mach_vm_address_t org{func_ident_pascal} {{0}};\n",
]
with open(cpp_path, "w") as f:
f.write("".join(cpp_lines))
with open(hpp_path, "w") as f:
f.write("".join(hpp_lines))