-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_vtable3.py
57 lines (38 loc) · 1.77 KB
/
make_vtable3.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
from pathlib import Path
from orjson import loads
def load_file(path:Path) -> dict[str, list[list[str]]]:
return loads(Path(path).read_bytes())
def write_code(class_name:str, functions:list[str]) -> str:
if class_name.startswith("cocos2d::"):
class_name = class_name.removeprefix("cocos2d::")
code = """
typedef struct {}_vtable {}_vtable, *P{}_vtable;
struct {}_vtable {\n""".replace("{}", class_name)
# Mangle Functions and build the structure...
for func in map(lambda f: f.replace("::", "_").replace("~","delete_"), functions):
code += " void* %s;\n" % func.split("(", 1)[0]
code += "};\n"
return code
def write_vtables():
vtables_name = input("vtables file name:")
if vtables_name.endswith(".json"):
vtables_name = vtables_name.removesuffix(".json")
output_header = input("output header file (example: <myghidra_headers.h>):")
if output_header.endswith(".h"):
output_header = vtables_name.removesuffix(".h")
code = "/* Generated by make_vtable3.py version 3.0 */\n\n"
# TODO: Request someone to merge libcocos2d stuff with these vtables...
# you can get this file by running DumpAllVirtuals.java into ghidra from the geode-bindings
data = load_file(f"{vtables_name}.json")
for name, tables in data.items():
for n, t in enumerate(tables):
if n > 0:
code += "/* %s_%i */\n" % (name, n)
code += (write_code("%s_%i" % (name, n), t) + "\n")
else:
code += "/* %s */\n" % name
code += (write_code(name, t) + "\n")
with open(f"{output_header}.h", "w") as w:
w.write(code)
if __name__ == "__main__":
write_vtables()