-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.cpp
421 lines (361 loc) · 12.2 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#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 const HANDLE CONSOLE_OUTPUT = GetStdHandle(STD_OUTPUT_HANDLE);
static const HANDLE CONSOLE_INPUT = GetStdHandle(STD_INPUT_HANDLE);
static bool isCommandLine;
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) {
WIN32_FIND_DATAA pathData;
HANDLE handle = FindFirstFileA((arguments.inputPath + directory.path + '*').c_str(), &pathData);
if (handle == INVALID_HANDLE_VALUE) return;
do {
if (pathData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (!std::strcmp(pathData.cFileName, ".") || !std::strcmp(pathData.cFileName, "..")) continue;
directory.folders.emplace_back(Directory{ .path = directory.path + pathData.cFileName + "\\" });
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(PathFindExtensionA(pathData.cFileName))) directory.files.emplace_back(pathData.cFileName);
} while (FindNextFileA(handle, &pathData));
FindClose(handle);
}
static bool decompile_files_recursively(const Directory& directory) {
CreateDirectoryA((arguments.outputPath + directory.path).c_str(), NULL);
std::string outputFile;
for (uint32_t i = 0; i < directory.files.size(); i++) {
outputFile = directory.files[i];
PathRemoveExtensionA(outputFile.data());
outputFile = outputFile.c_str();
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;
}
switch (MessageBoxA(NULL, ("Error running " + assertion.function + "\nSource: " + assertion.source + ":" + assertion.line + "\n\nFile: " + assertion.filePath + "\n\n" + assertion.message).c_str(),
PROGRAM_NAME, MB_ICONERROR | MB_CANCELTRYCONTINUE | MB_DEFBUTTON3)) {
case IDCANCEL:
return false;
case IDTRYAGAIN:
print("Retrying...");
i--;
continue;
case IDCONTINUE:
print("File skipped.");
filesSkipped++;
}
} catch (...) {
MessageBoxA(NULL, std::string("Unknown exception\n\nFile: " + bytecode.filePath).c_str(), PROGRAM_NAME, MB_ICONERROR | MB_OK);
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];
#ifndef _DEBUG
if (!isCommandLine) return nullptr;
#endif
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;
}
static void wait_for_exit() {
if (isCommandLine) return;
print("Press enter to exit.");
input();
}
int main(int argc, char* argv[]) {
print(std::string(PROGRAM_NAME) + "\nCompiled on " + __DATE__);
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
{
DWORD consoleProcessId;
GetWindowThreadProcessId(GetConsoleWindow(), &consoleProcessId);
#ifdef _DEBUG
isCommandLine = false;
#else
isCommandLine = consoleProcessId != GetCurrentProcessId();
#endif
}
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!");
if (isCommandLine) return EXIT_FAILURE;
arguments.inputPath.resize(MAX_PATH, NULL);
OPENFILENAMEA dialogInfo = {
.lStructSize = sizeof(OPENFILENAMEA),
.hwndOwner = NULL,
.lpstrFilter = NULL,
.lpstrCustomFilter = NULL,
.lpstrFile = arguments.inputPath.data(),
.nMaxFile = (DWORD)arguments.inputPath.size(),
.lpstrFileTitle = NULL,
.lpstrInitialDir = NULL,
.lpstrTitle = PROGRAM_NAME,
.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST,
.lpstrDefExt = NULL,
.FlagsEx = NULL
};
print("Please select a valid LuaJIT bytecode file.");
if (!GetOpenFileNameA(&dialogInfo)) return EXIT_FAILURE;
arguments.inputPath = arguments.inputPath.c_str();
}
DWORD pathAttributes;
if (!arguments.outputPath.size()) {
arguments.outputPath.resize(MAX_PATH);
GetModuleFileNameA(NULL, arguments.outputPath.data(), arguments.outputPath.size());
*PathFindFileNameA(arguments.outputPath.data()) = '\x00';
arguments.outputPath = arguments.outputPath.c_str();
arguments.outputPath += "output\\";
arguments.outputPath.shrink_to_fit();
} else {
pathAttributes = GetFileAttributesA(arguments.outputPath.c_str());
if (pathAttributes == INVALID_FILE_ATTRIBUTES) {
print("Failed to open output path: " + arguments.outputPath);
return EXIT_FAILURE;
}
if (!(pathAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
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);
}
pathAttributes = GetFileAttributesA(arguments.inputPath.c_str());
if (pathAttributes == INVALID_FILE_ATTRIBUTES) {
print("Failed to open input path: " + arguments.inputPath);
wait_for_exit();
return EXIT_FAILURE;
}
Directory root;
if (pathAttributes & FILE_ATTRIBUTE_DIRECTORY) {
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);
wait_for_exit();
return EXIT_FAILURE;
}
} else {
root.files.emplace_back(PathFindFileNameA(arguments.inputPath.c_str()));
*PathFindFileNameA(arguments.inputPath.c_str()) = '\x00';
arguments.inputPath = arguments.inputPath.c_str();
}
try {
if (!decompile_files_recursively(root)) {
print("--------------------\nAborted!");
wait_for_exit();
return EXIT_FAILURE;
}
} catch (...) {
throw;
}
#ifndef _DEBUG
print("--------------------\n" + (filesSkipped ? "Failed to decompile " + std::to_string(filesSkipped) + " file" + (filesSkipped > 1 ? "s" : "") + ".\n" : "") + "Done!");
wait_for_exit();
#endif
return EXIT_SUCCESS;
}
void print(const std::string& message) {
WriteConsoleA(CONSOLE_OUTPUT, (message + '\n').data(), message.size() + 1, NULL, NULL);
}
std::string input() {
static char BUFFER[1024];
FlushConsoleInputBuffer(CONSOLE_INPUT);
DWORD charsRead;
return ReadConsoleA(CONSOLE_INPUT, BUFFER, sizeof(BUFFER), &charsRead, NULL) && charsRead > 2 ? std::string(BUFFER, charsRead - 2) : "";
}
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 ? '=' : ' ';
}
WriteConsoleA(CONSOLE_OUTPUT, PROGRESS_BAR, sizeof(PROGRESS_BAR) - 1, NULL, NULL);
isProgressBarActive = true;
}
void erase_progress_bar() {
static constexpr char PROGRESS_BAR_ERASER[] = "\r \r";
if (!isProgressBarActive) return;
WriteConsoleA(CONSOLE_OUTPUT, PROGRESS_BAR_ERASER, sizeof(PROGRESS_BAR_ERASER) - 1, NULL, NULL);
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;
}