forked from GDRETools/gdsdecomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytecode_85585c7.cpp
698 lines (663 loc) · 15.3 KB
/
bytecode_85585c7.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*************************************************************************/
/* bytecode_85585c7.cpp */
/*************************************************************************/
#include "core/io/marshalls.h"
#include "core/string/print_string.h"
#include "core/templates/rb_map.h"
#include "bytecode_85585c7.h"
static const char *func_names[] = {
"sin",
"cos",
"tan",
"sinh",
"cosh",
"tanh",
"asin",
"acos",
"atan",
"atan2",
"sqrt",
"fmod",
"fposmod",
"floor",
"ceil",
"round",
"abs",
"sign",
"pow",
"log",
"exp",
"is_nan",
"is_inf",
"ease",
"decimals",
"stepify",
"lerp",
"dectime",
"randomize",
"randi",
"randf",
"rand_range",
"seed",
"rand_seed",
"deg2rad",
"rad2deg",
"linear2db",
"db2linear",
"max",
"min",
"clamp",
"nearest_po2",
"weakref",
"funcref",
"convert",
"typeof",
"type_exists",
"str",
"print",
"printt",
"prints",
"printerr",
"printraw",
"var2str",
"str2var",
"var2bytes",
"bytes2var",
"range",
"load",
"inst2dict",
"dict2inst",
"hash",
"Color8",
"ColorN",
"print_stack",
"instance_from_id",
};
static constexpr uint64_t FUNC_MAX = sizeof(func_names) / sizeof(func_names[0]);
enum Token {
TK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S_OPEN,
TK_PARENTHESIS_CLOSE,
TK_COMMA,
TK_SEMICOLON,
TK_PERIOD,
TK_QUESTION_MARK,
TK_COLON,
TK_NEWLINE,
TK_CONST_PI,
TK_ERROR,
TK_EOF,
TK_CURSOR, //used for code completion
TK_MAX
};
Error GDScriptDecomp_85585c7::decompile_buffer(Vector<uint8_t> p_buffer) {
//Cleanup
script_text = String();
//Load bytecode
Vector<StringName> identifiers;
Vector<Variant> constants;
VMap<uint32_t, uint32_t> lines;
Vector<uint32_t> tokens;
const uint8_t *buf = p_buffer.ptr();
int total_len = p_buffer.size();
ERR_FAIL_COND_V(p_buffer.size() < 24 || p_buffer[0] != 'G' || p_buffer[1] != 'D' || p_buffer[2] != 'S' || p_buffer[3] != 'C', ERR_INVALID_DATA);
int version = decode_uint32(&buf[4]);
if (version != bytecode_version) {
ERR_FAIL_COND_V(version > bytecode_version, ERR_INVALID_DATA);
}
int identifier_count = decode_uint32(&buf[8]);
int constant_count = decode_uint32(&buf[12]);
int line_count = decode_uint32(&buf[16]);
int token_count = decode_uint32(&buf[20]);
const uint8_t *b = &buf[24];
total_len -= 24;
identifiers.resize(identifier_count);
for (int i = 0; i < identifier_count; i++) {
int len = decode_uint32(b);
ERR_FAIL_COND_V(len > total_len, ERR_INVALID_DATA);
b += 4;
Vector<uint8_t> cs;
cs.resize(len);
for (int j = 0; j < len; j++) {
cs.write[j] = b[j] ^ 0xb6;
}
cs.write[cs.size() - 1] = 0;
String s;
s.parse_utf8((const char *)cs.ptr());
b += len;
total_len -= len + 4;
identifiers.write[i] = s;
}
constants.resize(constant_count);
for (int i = 0; i < constant_count; i++) {
Variant v;
int len;
Error err = VariantDecoderCompat::decode_variant_compat(variant_ver_major, v, b, total_len, &len);
if (err) {
error_message = RTR("Invalid constant");
return err;
}
b += len;
total_len -= len;
constants.write[i] = v;
}
ERR_FAIL_COND_V(line_count * 8 > total_len, ERR_INVALID_DATA);
for (int i = 0; i < line_count; i++) {
uint32_t token = decode_uint32(b);
b += 4;
uint32_t linecol = decode_uint32(b);
b += 4;
lines.insert(token, linecol);
total_len -= 8;
}
tokens.resize(token_count);
for (int i = 0; i < token_count; i++) {
ERR_FAIL_COND_V(total_len < 1, ERR_INVALID_DATA);
if ((*b) & TOKEN_BYTE_MASK) { //little endian always
ERR_FAIL_COND_V(total_len < 4, ERR_INVALID_DATA);
tokens.write[i] = decode_uint32(b) & ~TOKEN_BYTE_MASK;
b += 4;
} else {
tokens.write[i] = *b;
b += 1;
total_len--;
}
}
//Decompile script
String line;
int indent = 0;
Token prev_token = TK_NEWLINE;
for (int i = 0; i < tokens.size(); i++) {
switch (Token(tokens[i] & TOKEN_MASK)) {
case TK_EMPTY: {
//skip
} break;
case TK_IDENTIFIER: {
uint32_t identifier = tokens[i] >> TOKEN_BITS;
ERR_FAIL_COND_V(identifier >= (uint32_t)identifiers.size(), ERR_INVALID_DATA);
line += String(identifiers[identifier]);
} break;
case TK_CONSTANT: {
uint32_t constant = tokens[i] >> TOKEN_BITS;
ERR_FAIL_COND_V(constant >= (uint32_t)constants.size(), ERR_INVALID_DATA);
line += get_constant_string(constants, constant);
} break;
case TK_SELF: {
line += "self";
} break;
case TK_BUILT_IN_TYPE: {
line += VariantDecoderCompat::get_variant_type_name(tokens[i] >> TOKEN_BITS, variant_ver_major);
} break;
case TK_BUILT_IN_FUNC: {
ERR_FAIL_COND_V(tokens[i] >> TOKEN_BITS >= FUNC_MAX, ERR_INVALID_DATA);
line += func_names[tokens[i] >> TOKEN_BITS];
} break;
case TK_OP_IN: {
_ensure_space(line);
line += "in ";
} break;
case TK_OP_EQUAL: {
_ensure_space(line);
line += "== ";
} break;
case TK_OP_NOT_EQUAL: {
_ensure_space(line);
line += "!= ";
} break;
case TK_OP_LESS: {
_ensure_space(line);
line += "< ";
} break;
case TK_OP_LESS_EQUAL: {
_ensure_space(line);
line += "<= ";
} break;
case TK_OP_GREATER: {
_ensure_space(line);
line += "> ";
} break;
case TK_OP_GREATER_EQUAL: {
_ensure_space(line);
line += ">= ";
} break;
case TK_OP_AND: {
_ensure_space(line);
line += "and ";
} break;
case TK_OP_OR: {
_ensure_space(line);
line += "or ";
} break;
case TK_OP_NOT: {
_ensure_space(line);
line += "not ";
} break;
case TK_OP_ADD: {
_ensure_space(line);
line += "+ ";
} break;
case TK_OP_SUB: {
if (prev_token != TK_NEWLINE)
_ensure_space(line);
line += "- ";
//TODO: do not add space after unary "-"
} break;
case TK_OP_MUL: {
_ensure_space(line);
line += "* ";
} break;
case TK_OP_DIV: {
_ensure_space(line);
line += "/ ";
} break;
case TK_OP_MOD: {
_ensure_space(line);
line += "% ";
} break;
case TK_OP_SHIFT_LEFT: {
_ensure_space(line);
line += "<< ";
} break;
case TK_OP_SHIFT_RIGHT: {
_ensure_space(line);
line += ">> ";
} break;
case TK_OP_ASSIGN: {
_ensure_space(line);
line += "= ";
} break;
case TK_OP_ASSIGN_ADD: {
_ensure_space(line);
line += "+= ";
} break;
case TK_OP_ASSIGN_SUB: {
_ensure_space(line);
line += "-= ";
} break;
case TK_OP_ASSIGN_MUL: {
_ensure_space(line);
line += "*= ";
} break;
case TK_OP_ASSIGN_DIV: {
_ensure_space(line);
line += "/= ";
} break;
case TK_OP_ASSIGN_MOD: {
_ensure_space(line);
line += "%= ";
} break;
case TK_OP_ASSIGN_SHIFT_LEFT: {
_ensure_space(line);
line += "<<= ";
} break;
case TK_OP_ASSIGN_SHIFT_RIGHT: {
_ensure_space(line);
line += ">>= ";
} break;
case TK_OP_ASSIGN_BIT_AND: {
_ensure_space(line);
line += "&= ";
} break;
case TK_OP_ASSIGN_BIT_OR: {
_ensure_space(line);
line += "|= ";
} break;
case TK_OP_ASSIGN_BIT_XOR: {
_ensure_space(line);
line += "^= ";
} break;
case TK_OP_BIT_AND: {
_ensure_space(line);
line += "& ";
} break;
case TK_OP_BIT_OR: {
_ensure_space(line);
line += "| ";
} break;
case TK_OP_BIT_XOR: {
_ensure_space(line);
line += "^ ";
} break;
case TK_OP_BIT_INVERT: {
_ensure_space(line);
line += "~ ";
} break;
//case TK_OP_PLUS_PLUS: {
// line += "++";
//} break;
//case TK_OP_MINUS_MINUS: {
// line += "--";
//} break;
case TK_CF_IF: {
if (prev_token != TK_NEWLINE)
_ensure_space(line);
line += "if ";
} break;
case TK_CF_ELIF: {
line += "elif ";
} break;
case TK_CF_ELSE: {
if (prev_token != TK_NEWLINE)
_ensure_space(line);
line += "else ";
} break;
case TK_CF_FOR: {
line += "for ";
} break;
case TK_CF_DO: {
line += "do ";
} break;
case TK_CF_WHILE: {
line += "while ";
} break;
case TK_CF_SWITCH: {
line += "swith ";
} break;
case TK_CF_CASE: {
line += "case ";
} break;
case TK_CF_BREAK: {
line += "break";
} break;
case TK_CF_CONTINUE: {
line += "continue";
} break;
case TK_CF_PASS: {
line += "pass";
} break;
case TK_CF_RETURN: {
line += "return ";
} break;
case TK_PR_FUNCTION: {
line += "func ";
} break;
case TK_PR_CLASS: {
line += "class ";
} break;
case TK_PR_EXTENDS: {
if (prev_token != TK_NEWLINE)
_ensure_space(line);
line += "extends ";
} break;
case TK_PR_ONREADY: {
line += "onready ";
} break;
case TK_PR_TOOL: {
line += "tool ";
} break;
case TK_PR_STATIC: {
line += "static ";
} break;
case TK_PR_EXPORT: {
line += "export ";
} break;
case TK_PR_SETGET: {
line += " setget ";
} break;
case TK_PR_CONST: {
line += "const ";
} break;
case TK_PR_VAR: {
if (line != String() && prev_token != TK_PR_ONREADY)
line += " ";
line += "var ";
} break;
case TK_PR_PRELOAD: {
line += "preload";
} break;
case TK_PR_ASSERT: {
line += "assert ";
} break;
case TK_PR_YIELD: {
line += "yield ";
} break;
case TK_PR_SIGNAL: {
line += "signal ";
} break;
case TK_PR_BREAKPOINT: {
line += "breakpoint ";
} break;
case TK_BRACKET_OPEN: {
line += "[";
} break;
case TK_BRACKET_CLOSE: {
line += "]";
} break;
case TK_CURLY_BRACKET_OPEN: {
line += "{";
} break;
case TK_CURLY_BRACKET_CLOSE: {
line += "}";
} break;
case TK_PARENTHESIS_OPEN: {
line += "(";
} break;
case TK_PARENTHESIS_CLOSE: {
line += ")";
} break;
case TK_COMMA: {
line += ", ";
} break;
case TK_SEMICOLON: {
line += ";";
} break;
case TK_PERIOD: {
line += ".";
} break;
case TK_QUESTION_MARK: {
line += "?";
} break;
case TK_COLON: {
line += ":";
} break;
case TK_NEWLINE: {
for (int j = 0; j < indent; j++) {
script_text += "\t";
}
script_text += line + "\n";
line = String();
indent = tokens[i] >> TOKEN_BITS;
} break;
case TK_CONST_PI: {
line += "PI";
} break;
case TK_ERROR: {
//skip - invalid
} break;
case TK_EOF: {
//skip - invalid
} break;
case TK_CURSOR: {
//skip - invalid
} break;
case TK_MAX: {
//skip - invalid
} break;
}
prev_token = Token(tokens[i] & TOKEN_MASK);
}
if (!line.is_empty()) {
for (int j = 0; j < indent; j++) {
script_text += "\t";
}
script_text += line + "\n";
}
if (script_text == String()) {
error_message = RTR("Invalid token");
return ERR_INVALID_DATA;
}
return OK;
}
namespace {
// TODO: Refactor bytecode classes so that this can be moved to bytecode base
// we should be after the open parenthesis, which has already been checked
bool test_built_in_func_arg_count(const Vector<uint32_t> &tokens, Pair<int, int> arg_count, int &curr_pos, int &num_args) {
int pos = curr_pos;
int comma_count = 0;
int min_args = arg_count.first;
int max_args = arg_count.second;
uint32_t t = tokens[pos] & 255; // TOKEN_MASK for all revisions
int bracket_open = 0;
if (min_args == 0 && max_args == 0) {
for (; pos < tokens.size(); pos++, t = tokens[pos] & 255) {
if (t == TK_PARENTHESIS_CLOSE) {
break;
} else if (t != TK_NEWLINE) {
return false;
}
}
// if we didn't find a close parenthesis, then we have an error
if (pos == tokens.size()) {
return false;
}
curr_pos = pos;
return true;
}
// count the commas
// at least in 3.x and below, the only time commas are allowed in function args are other expressions
// this is not the case for GDScript 2.0 (4.x), due to lambdas, but that doesn't have a compiled version yet
for (; pos < tokens.size(); pos++, t = tokens[pos] & 255) {
switch (t) {
case TK_BRACKET_OPEN:
case TK_CURLY_BRACKET_OPEN:
case TK_PARENTHESIS_OPEN:
bracket_open++;
break;
case TK_BRACKET_CLOSE:
case TK_CURLY_BRACKET_CLOSE:
case TK_PARENTHESIS_CLOSE:
bracket_open--;
break;
case TK_COMMA:
if (bracket_open == 0) {
comma_count++;
}
break;
default:
break;
}
if (bracket_open == -1) {
break;
}
}
// trailing commas are not allowed after the last argument
if (pos == tokens.size() || t != TK_PARENTHESIS_CLOSE || comma_count < min_args - 1 || comma_count > max_args - 1) {
return false;
}
num_args = comma_count + 1;
curr_pos = pos;
return true;
}
} //anonymous namespace
// 85585c7 (Godot v2.1.2) added ColorN func
GDScriptDecomp::BYTECODE_TEST_RESULT GDScriptDecomp_85585c7::test_bytecode(Vector<uint8_t> buffer) {
Vector<StringName> identifiers;
Vector<Variant> constants;
Vector<uint32_t> tokens;
ERR_FAIL_COND_V_MSG(get_ids_consts_tokens(buffer, bytecode_version, identifiers, constants, tokens) != OK, BYTECODE_TEST_RESULT::BYTECODE_TEST_CORRUPT, "Failed to get identifiers, constants, and tokens from bytecode.");
bool tested_colorN_shift = false;
int token_count = tokens.size();
for (int i = 0; i < token_count; i++) {
if ((tokens[i] & TOKEN_MASK) == TK_BUILT_IN_FUNC) { // ignore all tokens until we find TK_BUILT_IN_FUNC
int num_args = 0;
int func_id = tokens[i] >> TOKEN_BITS;
// if the func_id is >= size of func_names, this is likely an earlier version 10 revision
if (func_id >= FUNC_MAX) {
return BYTECODE_TEST_RESULT::BYTECODE_TEST_FAIL;
}
if (i + 2 >= token_count) {
// if we're at the end of the token list, then we don't have a valid call in any bytecode revision
return BYTECODE_TEST_RESULT::BYTECODE_TEST_CORRUPT;
}
// all TK_BUILT_IN_FUNC tokens must be followed by TK_PARENTHESIS_OPEN
if ((tokens[i + 1] & TOKEN_MASK) != TK_PARENTHESIS_OPEN) {
// ed80f45 added the ENUM token, which shifted PARENTHESIS_OPEN by 1. this is another bytecode 10 revision
return BYTECODE_TEST_RESULT::BYTECODE_TEST_FAIL;
}
i += 2; // skip TK_BUILT_IN_FUNC and TK_PARENTHESIS_OPEN
auto arg_count = get_arg_count_for_builtin(func_names[func_id]);
if (!tested_colorN_shift && func_id >= 63 && func_id + 1 < FUNC_MAX) {
// ColorN and print_stack would be pass cases here.
tested_colorN_shift = true;
}
if (!test_built_in_func_arg_count(tokens, arg_count, i, num_args)) {
return BYTECODE_TEST_RESULT::BYTECODE_TEST_FAIL;
}
}
}
if (tested_colorN_shift) {
return BYTECODE_TEST_RESULT::BYTECODE_TEST_PASS;
}
// we didn't find a ColorN call
return BYTECODE_TEST_RESULT::BYTECODE_TEST_UNKNOWN;
}