-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_decompile.cpp
224 lines (186 loc) · 8.46 KB
/
main_decompile.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdinc.h>
#include "program.hpp"
#include "disassembler.hpp"
#include "decompiler_ir2.hpp"
int decompile(fs::path input, fs::path output, ProgramContext& program)
{
if(output.empty())
{
output = input;
output.replace_extension(program.opt.emit_ir2? ".ir2" : ".sc");
}
try
{
const Commands& commands = program.commands;
FILE* outstream;
auto lang = (program.opt.emit_ir2? Options::Lang::IR2 : Options::Lang::GTA3Script);
auto guard = make_scope_guard([&] {
if(outstream != stdout) fclose(outstream);
});
if(lang == Options::Lang::GTA3Script)
program.fatal_error(nocontext, "GTA3script output is disabled, please use -emit-ir2 for IR2 output");
outstream = (output != "-"? u8fopen(output, "wb") : stdout);
if(!outstream)
program.fatal_error(nocontext, "could not open file '{}' for writing", output.generic_u8string());
auto opt_bytecode = read_file_binary(input);
if(!opt_bytecode)
program.fatal_error(nocontext, "file '{}' does not exist", input.generic_u8string());
std::vector<uint8_t> script_img;
if(program.opt.streamed_scripts)
{
auto img_path = fs::path(input).replace_filename("script.img");
if(auto opt = read_file_binary(img_path))
script_img = std::move(*opt);
else
program.fatal_error(nocontext, "file '{}' does not exist", img_path.generic_u8string());
}
auto println = [&](const std::string& line) { fprintf(outstream, "%s\n", line.c_str()); };
if(!decompile(opt_bytecode->data(), opt_bytecode->size(), script_img.data(), script_img.size(), program, lang, println))
throw ProgramFailure();
return 0;
}
catch(const ProgramFailure&)
{
fprintf(stderr, "gta3sc: decompilation failed\n");
return EXIT_FAILURE;
}
}
bool decompile(const void* bytecode, size_t bytecode_size,
const void* script_img, size_t script_img_size,
ProgramContext& program, Options::Lang lang,
std::function<void(const std::string&)> callback)
{
Expects(!program.opt.streamed_scripts || program.opt.headerless || script_img != nullptr);
try
{
optional<DecompiledScmHeader> opt_header;
size_t ignore_stream_id = -1;
auto scan_type = program.opt.linear_sweep? Disassembler::Type::LinearSweep :
Disassembler::Type::RecursiveTraversal;
if(!program.opt.headerless)
{
opt_header = DecompiledScmHeader::from_bytecode(bytecode, bytecode_size,
program.opt.get_header<DecompiledScmHeader::Version>());
if(!opt_header)
program.fatal_error(nocontext, "corrupted scm header");
DecompiledScmHeader& header = *opt_header;
auto it = std::find_if(header.streamed_scripts.begin(), header.streamed_scripts.end(), [](const auto& pair) {
return iequal_to()(pair.name, "AAA");
});
if(it != header.streamed_scripts.end())
ignore_stream_id = (it - header.streamed_scripts.begin());
if(program.has_error())
throw ProgramFailure();
}
BinaryFetcher main_segment{ bytecode, std::min<uint32_t>(bytecode_size, opt_header? opt_header->main_size : bytecode_size) };
std::vector<BinaryFetcher> mission_segments;
std::vector<BinaryFetcher> stream_segments;
if(!program.opt.headerless)
{
DecompiledScmHeader& header = *opt_header;
mission_segments = mission_scripts_fetcher(bytecode, bytecode_size, header, program);
if(program.opt.streamed_scripts)
{
stream_segments = streamed_scripts_fetcher(script_img, script_img_size, header, program);
}
}
if(program.has_error())
throw ProgramFailure();
Disassembler main_segment_asm(program, main_segment, scan_type);
std::vector<Disassembler> mission_segments_asm;
std::vector<Disassembler> stream_segments_asm;
if(!program.opt.headerless)
{
DecompiledScmHeader& header = *opt_header;
mission_segments_asm.reserve(header.mission_offsets.size());
stream_segments_asm.reserve(header.streamed_scripts.size());
// this loop cannot be thread safely unfolded because of main_segment_asm being
// mutated on all the units.
for(auto& mission_bytecode : mission_segments)
{
mission_segments_asm.emplace_back(program, mission_bytecode, main_segment_asm, scan_type);
mission_segments_asm.back().run_analyzer();
}
// this loop cannot be thread safely unfolded because of stream_segment_asm being
// mutated on all the units.
for(size_t i = 0; i < stream_segments.size(); ++i)
{
if(i != ignore_stream_id)
{
auto& stream_bytecode = stream_segments[i];
stream_segments_asm.emplace_back(program, stream_bytecode, main_segment_asm, scan_type);
stream_segments_asm.back().run_analyzer();
}
}
}
if(true)
{
// run main segment analyzer after the missions and streams analyzer
main_segment_asm.run_analyzer(opt_header? opt_header->code_offset : 0);
main_segment_asm.disassembly(opt_header? opt_header->code_offset : 0);
}
for(auto& mission_asm : mission_segments_asm)
mission_asm.disassembly();
for(size_t i = 0; i < stream_segments.size(); ++i)
{
if(i != ignore_stream_id)
{
auto& stream_asm = stream_segments_asm[i];
stream_asm.disassembly();
}
}
if(program.has_error())
throw ProgramFailure();
if(lang == Options::Lang::IR2)
{
if(!program.opt.headerless)
{
std::string temp_string;
for(size_t i = 0; i < opt_header->models.size(); ++i)
{
temp_string = opt_header->models[i];
std::transform(temp_string.begin(), temp_string.end(), temp_string.begin(), toupper_ascii);
callback(fmt::format("#DEFINE_MODEL {} -{}", temp_string, i+1));
}
}
if(!program.opt.headerless)
{
std::string temp_string;
for(size_t i = 0; i < opt_header->streamed_scripts.size(); ++i)
{
temp_string = opt_header->streamed_scripts[i].name;
std::transform(temp_string.begin(), temp_string.end(), temp_string.begin(), toupper_ascii);
callback(fmt::format("#DEFINE_STREAM {} {}", temp_string, i));
}
}
auto main_ir2 = DecompilerIR2(program.commands, main_segment_asm.get_data(), 0, main_segment.size, "MAIN", true);
main_ir2.decompile(callback);
for(size_t i = 0; i < mission_segments_asm.size(); ++i)
{
auto& mission_asm = mission_segments_asm[i];
auto script_name = fmt::format("MISSION_{}", i);
callback(fmt::format("#MISSION_BLOCK_START {}", (int)(i)));
DecompilerIR2(program.commands, mission_asm.get_data(), 0, mission_segments[i].size, std::move(script_name), false, main_ir2).decompile(callback);
callback("#MISSION_BLOCK_END");
}
for(size_t i = 0; i < stream_segments_asm.size(); ++i)
{
if(i != ignore_stream_id)
{
auto& stream_asm = stream_segments_asm[i];
auto script_name = fmt::format("STREAM_{}", i);
callback(fmt::format("#STREAMED_BLOCK_START {}", (int)(i)));
DecompilerIR2(program.commands, stream_asm.get_data(), 0, stream_segments[i].size, std::move(script_name), false, main_ir2).decompile(callback);
callback("#STREAMED_BLOCK_END");
}
}
}
if(program.has_error())
throw ProgramFailure();
return true;
}
catch(const ProgramFailure&)
{
return false;
}
}