-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·154 lines (122 loc) · 4.96 KB
/
generate.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
#!/usr/bin/env python3
"""Generate a debugging report for a test case. See README."""
from __future__ import annotations
import argparse
import hashlib
import os
import sys
import traceback
import warnings
from explorerscript.ssb_converting.decompiler.graph_building.graph_minimizer import (
SsbGraphMinimizer,
)
from explorerscript.ssb_converting.decompiler.label_jump_to_resolver import (
OpsLabelJumpToResolver,
)
from skytemple_files.common.ppmdu_config.xml_reader import Pmd2XmlReader
from skytemple_files.script.ssb.handler import SsbHandler
RENDER = True
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument("output_dir")
parser.add_argument("edition")
parser.add_argument("path_to_problematic_ssb")
parser.add_argument("path_to_original_source_code")
args = parser.parse_args()
os.makedirs(args.output_dir, exist_ok=True)
args.output_dir = os.path.abspath(args.output_dir)
write_readme(args.output_dir)
with open(args.path_to_problematic_ssb, "rb") as f:
b = f.read()
ssb = SsbHandler.deserialize(b, static_data=Pmd2XmlReader.load_default(args.edition))
with open(os.path.join(args.output_dir, "compiled.ssb"), "wb") as f2:
f2.write(b)
try:
routine_ops = ssb.get_filled_routine_ops()
resolver = OpsLabelJumpToResolver(routine_ops)
routine_ops = list(resolver)
grapher = SsbGraphMinimizer(routine_ops)
try:
draw_graphs(grapher, args, "0_before_optimize")
grapher.optimize_paths()
draw_graphs(grapher, args, "1_after_optimize")
grapher.build_branches()
draw_graphs(grapher, args, "2_after_branch_before_group")
grapher.group_branches()
draw_graphs(grapher, args, "3_after_branch1")
grapher.invert_branches()
draw_graphs(grapher, args, "4_after_branch2")
grapher.build_and_group_switch_cases()
draw_graphs(grapher, args, "5_after_switch1")
grapher.group_switch_cases()
draw_graphs(grapher, args, "6_after_switch2")
grapher.build_switch_fallthroughs()
draw_graphs(grapher, args, "7_after_switch3")
grapher.build_loops()
draw_graphs(grapher, args, "8_after_loops")
grapher.remove_label_markers()
draw_graphs(grapher, args, "9_done")
except:
warnings.warn("Failed at least one graph step. Skipping rest. Decompiling will fail.")
except:
warnings.warn("Failed even preparing graph debugging. This script decompilation fails very early.")
with open(os.path.join(args.output_dir, "original.exps"), "w") as fo:
with open(args.path_to_original_source_code, "r") as fs:
fo.write(fs.read())
try:
decompiled, _ = ssb.to_explorerscript()
except:
warnings.warn("Failed decompiling. Writing failed_decompile_err.txt instead.")
with open(os.path.join(args.output_dir, "failed_decompile_err.txt"), "w") as ferr:
ferr.write("".join(traceback.format_exception(*sys.exc_info())))
else:
with open(os.path.join(args.output_dir, "decompiled.exps"), "w") as fd:
fd.write(decompiled)
def draw_graphs(grapher, args, run_name):
local_output_dir = os.path.abspath(os.path.join(args.output_dir, "graphs", run_name))
print(f">> {run_name}")
if not RENDER:
return
os.makedirs(local_output_dir, exist_ok=True)
for i, graph in enumerate(grapher._graphs):
dot_name = os.path.join(local_output_dir, f"{i}.dot")
hash_dotfile_before = None
if os.path.exists(dot_name):
with open(dot_name, "r") as f:
hash_dotfile_before = hashlib.md5(f.read().encode("utf-8")).hexdigest()
with open(dot_name, "w") as f:
graph.write_dot(f)
with open(dot_name, "r") as f:
hash_dotfile_same = (
hashlib.md5(f.read().encode("utf-8")).hexdigest() == hash_dotfile_before
)
unconnected_vertices = []
if not hash_dotfile_same:
print("Writing svg for " + dot_name)
try:
os.remove(os.path.join(local_output_dir, f"{i}.dot.svg"))
except FileNotFoundError:
pass
os.chdir(local_output_dir)
os.system(f"dot -Tsvg -O {i}.dot")
print("done.")
for v in graph.vs:
if len(list(v.all_edges())) < 1 and v["name"] != 0:
unconnected_vertices.append(v["label"])
if len(unconnected_vertices) > 0:
warnings.warn(
f"Routine {i} has unconnected ops: {unconnected_vertices}"
)
def write_readme(output_dir):
with open(os.path.join(output_dir, "README.md"), "w") as f:
f.write("""# <name>
Fixed in ExplorerScript: Not fixed yet.
## Source of ExplorerScript:
...
## Issue description:
...
## How to approach fix:
...
""")
if __name__ == "__main__":
main(sys.argv)