forked from marsinator358/luajit-decompiler-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
336 lines (286 loc) · 9.38 KB
/
main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "main.h"
struct Error {
const std::string message;
const std::string filePath;
const std::string function;
const std::string source;
const std::string line;
};
static bool isProgressBarActive = false;
static uint32_t filesSkipped = 0;
static struct {
bool showHelp = false;
bool silentAssertions = false;
bool forceOverwrite = false;
bool ignoreDebugInfo = false;
bool minimizeDiffs = false;
bool unrestrictedAscii = false;
std::string inputPath;
std::string outputPath;
std::string extensionFilter;
} arguments;
struct Directory {
const std::string path;
std::vector<Directory> folders;
std::vector<std::string> files;
};
static std::string string_to_lowercase(const std::string& string) {
std::string lowercaseString = string;
for (uint32_t i = lowercaseString.size(); i--;) {
if (lowercaseString[i] < 'A' || lowercaseString[i] > 'Z') continue;
lowercaseString[i] += 'a' - 'A';
}
return lowercaseString;
}
static void find_files_recursively(Directory& directory) {
for (const auto & entry : std::filesystem::directory_iterator(arguments.inputPath + directory.path)) {
if (entry.is_directory()) {
directory.folders.emplace_back(Directory{ .path = directory.path + entry.path().filename().string() + "/" });
find_files_recursively(directory.folders.back());
if (!directory.folders.back().files.size() && !directory.folders.back().folders.size()) directory.folders.pop_back();
continue;
}
if (!arguments.extensionFilter.size() || arguments.extensionFilter == string_to_lowercase(entry.path().extension().string())) directory.files.emplace_back(entry.path().filename().string());
}
}
static bool decompile_files_recursively(const Directory& directory) {
std::filesystem::create_directory(arguments.outputPath + directory.path);
std::string outputFile;
for (uint32_t i = 0; i < directory.files.size(); i++) {
outputFile = directory.files[i];
outputFile = outputFile.substr(0, outputFile.find_last_of("."));
outputFile += ".lua";
Bytecode bytecode(arguments.inputPath + directory.path + directory.files[i]);
Ast ast(bytecode, arguments.ignoreDebugInfo, arguments.minimizeDiffs);
Lua lua(bytecode, ast, arguments.outputPath + directory.path + outputFile, arguments.forceOverwrite, arguments.minimizeDiffs, arguments.unrestrictedAscii);
try {
print("--------------------\nInput file: " + bytecode.filePath + "\nReading bytecode...");
bytecode();
print("Building ast...");
ast();
print("Writing lua source...");
lua();
print("Output file: " + lua.filePath);
} catch (const Error& assertion) {
erase_progress_bar();
if (arguments.silentAssertions) {
print("\nError running " + assertion.function + "\nSource: " + assertion.source + ":" + assertion.line + "\n\n" + assertion.message);
filesSkipped++;
continue;
}
print("Error running " + assertion.function + "\nSource: " + assertion.source + ":" + assertion.line + "\n\nFile: " + assertion.filePath + "\n\n" + assertion.message);
print("File skipped.");
filesSkipped++;
} catch (...) {
print("Unknown exception\n\nFile: " + bytecode.filePath);
throw;
}
}
for (uint32_t i = 0; i < directory.folders.size(); i++) {
if (!decompile_files_recursively(directory.folders[i])) return false;
}
return true;
}
static char* parse_arguments(const int& argc, char** const& argv) {
if (argc < 2) return nullptr;
arguments.inputPath = argv[1];
bool isInputPathSet = true;
if (arguments.inputPath.size() && arguments.inputPath.front() == '-') {
arguments.inputPath.clear();
isInputPathSet = false;
}
std::string argument;
for (uint32_t i = isInputPathSet ? 2 : 1; i < argc; i++) {
argument = argv[i];
if (argument.size() >= 2 || argument.front() == '-') {
if (argument[1] == '-') {
argument = argument.c_str() + 2;
if (argument == "extension") {
if (i <= argc - 2) {
i++;
arguments.extensionFilter = argv[i];
continue;
}
} else if (argument == "force_overwrite") {
arguments.forceOverwrite = true;
continue;
} else if (argument == "help") {
arguments.showHelp = true;
continue;
} else if (argument == "ignore_debug_info") {
arguments.ignoreDebugInfo = true;
continue;
} else if (argument == "minimize_diffs") {
arguments.minimizeDiffs = true;
} else if (argument == "output") {
if (i <= argc - 2) {
i++;
arguments.outputPath = argv[i];
continue;
}
} else if (argument == "silent_assertions") {
arguments.silentAssertions = true;
continue;
} else if (argument == "unrestricted_ascii") {
arguments.unrestrictedAscii = true;
continue;
}
} else if (argument.size() == 2) {
switch (argument[1]) {
case 'e':
if (i > argc - 2) break;
i++;
arguments.extensionFilter = argv[i];
continue;
case 'f':
arguments.forceOverwrite = true;
continue;
case '?':
case 'h':
arguments.showHelp = true;
continue;
case 'i':
arguments.ignoreDebugInfo = true;
continue;
case 'm':
arguments.minimizeDiffs = true;
continue;
case 'o':
if (i > argc - 2) break;
i++;
arguments.outputPath = argv[i];
continue;
case 's':
arguments.silentAssertions = true;
continue;
case 'u':
arguments.unrestrictedAscii = true;
continue;
}
}
}
return argv[i];
}
return nullptr;
}
int main(int argc, char* argv[]) {
print(std::string(PROGRAM_NAME) + "\nCompiled on " + __DATE__);
if (parse_arguments(argc, argv)) {
print("Invalid argument: " + std::string(parse_arguments(argc, argv)) + "\nUse -? to show usage and options.");
return EXIT_FAILURE;
}
if (arguments.showHelp) {
print(
"Usage: luajit-decompiler-v2.exe INPUT_PATH [options]\n"
"\n"
"Available options:\n"
" -h, -?, --help\t\tShow this message\n"
" -o, --output OUTPUT_PATH\tOverride default output directory\n"
" -e, --extension EXTENSION\tOnly decompile files with the specified extension\n"
" -s, --silent_assertions\tDisable assertion error pop-up window\n"
"\t\t\t\t and auto skip files that fail to decompile\n"
" -f, --force_overwrite\t\tAlways overwrite existing files\n"
" -i, --ignore_debug_info\tIgnore bytecode debug info\n"
" -m, --minimize_diffs\t\tOptimize output formatting to help minimize diffs\n"
" -u, --unrestricted_ascii\tDisable default UTF-8 encoding and string restrictions"
);
return EXIT_SUCCESS;
}
if (!arguments.inputPath.size()) {
print("No input path specified!");
return EXIT_FAILURE;
}
if (!arguments.outputPath.size()) {
arguments.outputPath += "./output/";
arguments.outputPath.shrink_to_fit();
} else {
if (!std::filesystem::is_directory(arguments.outputPath)) {
print("Output path is not a folder!");
return EXIT_FAILURE;
}
switch (arguments.outputPath.back()) {
case '/':
case '\\':
break;
default:
arguments.outputPath += '/';
break;
}
}
if (arguments.extensionFilter.size()) {
if (arguments.extensionFilter.front() != '.') arguments.extensionFilter.insert(arguments.extensionFilter.begin(), '.');
arguments.extensionFilter = string_to_lowercase(arguments.extensionFilter);
}
Directory root;
if (std::filesystem::is_directory(arguments.inputPath)) {
switch (arguments.inputPath.back()) {
case '/':
case '\\':
break;
default:
arguments.inputPath += '/';
break;
}
find_files_recursively(root);
if (!root.files.size() && !root.folders.size()) {
print("No files " + (arguments.extensionFilter.size() ? "with extension " + arguments.extensionFilter + " " : "") + "found in path: " + arguments.inputPath);
return EXIT_FAILURE;
}
} else {
root.files.emplace_back(std::filesystem::path(arguments.inputPath).filename().string());
arguments.inputPath = std::filesystem::path(arguments.inputPath).parent_path().string();
}
try {
if (!decompile_files_recursively(root)) {
print("--------------------\nAborted!");
return EXIT_FAILURE;
}
} catch (...) {
throw;
}
#ifndef _DEBUG
print("--------------------\n" + (filesSkipped ? "Failed to decompile " + std::to_string(filesSkipped) + " file" + (filesSkipped > 1 ? "s" : "") + ".\n" : "") + "Done!");
#endif
return EXIT_SUCCESS;
}
void print(const std::string& message) {
std::cout << message << std::endl;
}
std::string input() {
std::string input;
std::getline(std::cin, input);
return input;
}
void print_progress_bar(const double& progress, const double& total) {
static char PROGRESS_BAR[] = "\r[====================]";
const uint8_t threshold = std::round(20 / total * progress);
for (uint8_t i = 20; i--;) {
PROGRESS_BAR[i + 2] = i < threshold ? '=' : ' ';
}
std::cout << PROGRESS_BAR;
isProgressBarActive = true;
}
void erase_progress_bar() {
static constexpr char PROGRESS_BAR_ERASER[] = "\r \r";
if (!isProgressBarActive) return;
std::cout << PROGRESS_BAR_ERASER;
isProgressBarActive = false;
}
void assert(const bool& assertion, const std::string& message, const std::string& filePath, const std::string& function, const std::string& source, const uint32_t& line) {
if (!assertion) throw Error{
.message = message,
.filePath = filePath,
.function = function,
.source = source,
.line = std::to_string(line)
};
}
std::string byte_to_string(const uint8_t& byte) {
char string[] = "0x00";
uint8_t digit;
for (uint8_t i = 2; i--;) {
digit = (byte >> i * 4) & 0xF;
string[3 - i] = digit >= 0xA ? 'A' + digit - 0xA : '0' + digit;
}
return string;
}