| 611 | } |
| 612 | |
| 613 | Error GDScriptDecomp::decompile_buffer(Vector<uint8_t> p_buffer) { |
| 614 | #if 0 |
| 615 | debug_print(p_buffer); |
| 616 | #endif |
| 617 | //Cleanup |
| 618 | script_text = String(); |
| 619 | |
| 620 | ScriptState state; |
| 621 | //Load bytecode |
| 622 | Error err = get_script_state(p_buffer, state); |
| 623 | ERR_FAIL_COND_V(err != OK, err); |
| 624 | Vector<StringName> &identifiers = state.identifiers; |
| 625 | Vector<Variant> &constants = state.constants; |
| 626 | Vector<uint32_t> &tokens = state.tokens; |
| 627 | HashMap<uint32_t, uint32_t> &lines = state.lines; |
| 628 | HashMap<uint32_t, uint32_t> &columns = state.columns; |
| 629 | int version = state.bytecode_version; |
| 630 | |
| 631 | int bytecode_version = get_bytecode_version(); |
| 632 | int variant_ver_major = get_variant_ver_major(); |
| 633 | uint32_t FUNC_MAX = get_function_count(); |
| 634 | GDSDECOMP_FAIL_COND_V(version != get_bytecode_version(), ERR_INVALID_DATA); |
| 635 | |
| 636 | //Decompile script |
| 637 | String line; |
| 638 | int indent = 0; |
| 639 | |
| 640 | GlobalToken prev_token = G_TK_NEWLINE; |
| 641 | GlobalToken prev_prev_token = G_TK_MAX; |
| 642 | uint32_t prev_line = 1; |
| 643 | uint32_t prev_line_start_column = 1; |
| 644 | |
| 645 | int tab_size = 1; |
| 646 | bool use_spaces = false; |
| 647 | if constexpr (FORCE_SPACES_FOR_2_0) { |
| 648 | if (columns.size() > 0) { |
| 649 | use_spaces = true; |
| 650 | } |
| 651 | } |
| 652 | bool first_line = true; |
| 653 | |
| 654 | auto handle_newline = [&](int i, GlobalToken curr_token) { |
| 655 | auto curr_line = state.get_token_line(i); |
| 656 | auto curr_column = state.get_token_column(i); |
| 657 | for (int j = 0; j < indent; j++) { |
| 658 | script_text += use_spaces ? " " : "\t"; |
| 659 | } |
| 660 | script_text += line; |
| 661 | if (curr_line <= prev_line) { |
| 662 | curr_line = prev_line + 1; // force new line |
| 663 | } |
| 664 | while (curr_line > prev_line) { |
| 665 | if (curr_token != G_TK_NEWLINE && bytecode_version < GDSCRIPT_2_0_VERSION) { |
| 666 | script_text += "\\"; // line continuation |
| 667 | } else if (bytecode_version >= GDSCRIPT_2_0_VERSION && !lines.has(i)) { |
| 668 | if (!first_line || (!gdre::remove_whitespace(line).is_empty())) { |
| 669 | script_text += "\\"; |
| 670 | } |