-
Notifications
You must be signed in to change notification settings - Fork 137
/
import_exporter.cpp
1687 lines (1594 loc) · 66.5 KB
/
import_exporter.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "import_exporter.h"
#include "bytecode/bytecode_tester.h"
#include "bytecode/bytecode_versions.h"
#include "compat/oggstr_loader_compat.h"
#include "compat/optimized_translation_extractor.h"
#include "compat/resource_loader_compat.h"
#include "compat/sample_loader_compat.h"
#include "compat/texture_loader_compat.h"
#include "gdre_settings.h"
#include "pcfg_loader.h"
#include "util_functions.h"
#include "core/crypto/crypto_core.h"
#include "core/io/config_file.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/math/audio_frame.h"
#include "core/os/os.h"
#include "core/string/optimized_translation.h"
#include "core/variant/variant_parser.h"
#include "core/version_generated.gen.h"
#include "modules/gltf/gltf_document.h"
#include "modules/minimp3/audio_stream_mp3.h"
#include "modules/regex/regex.h"
#include "modules/vorbis/audio_stream_ogg_vorbis.h"
#include "scene/resources/audio_stream_wav.h"
#include "scene/resources/font.h"
#include "thirdparty/minimp3/minimp3_ex.h"
GDRESettings *get_settings() {
return GDRESettings::get_singleton();
}
int get_ver_major() {
return get_settings()->get_ver_major();
}
int get_ver_minor() {
return get_settings()->get_ver_minor();
}
int get_ver_rev() {
return get_settings()->get_ver_rev();
}
// export all the imported resources
Error ImportExporter::export_imports(const String &p_out_dir, const Vector<String> &files_to_export) {
String t;
return _export_imports(p_out_dir, files_to_export, nullptr, t);
}
Ref<ImportExporterReport> ImportExporter::get_report() {
return report;
}
Error ImportExporter::_export_imports(const String &p_out_dir, const Vector<String> &files_to_export, EditorProgressGDDC *pr, String &error_string) {
reset_log();
report = Ref<ImportExporterReport>(memnew(ImportExporterReport(get_settings()->get_version_string())));
report->log_file_location = get_settings()->get_log_file_path();
ERR_FAIL_COND_V_MSG(!get_settings()->is_pack_loaded(), ERR_DOES_NOT_EXIST, "pack/dir not loaded!");
uint64_t last_progress_upd = OS::get_singleton()->get_ticks_usec();
String output_dir = !p_out_dir.is_empty() ? p_out_dir : get_settings()->get_project_path();
Error err = OK;
if (opt_lossy) {
WARN_PRINT_ONCE("Converting lossy imports, you may lose fidelity for indicated assets when re-importing upon loading the project");
}
// TODO: make this use "copy"
Array _files = get_settings()->get_import_files();
Vector<Ref<ImportInfo>> files;
// Put scene files at the end, otherwise it slows down the export tremendously
for (int i = 0; i < _files.size(); i++) {
Ref<ImportInfo> thing = _files[i];
if (thing->get_importer() != "scene") {
files.push_back(_files[i]);
}
}
for (int i = 0; i < _files.size(); i++) {
Ref<ImportInfo> thing = _files[i];
if (thing->get_importer() == "scene") {
files.push_back(_files[i]);
}
}
Ref<DirAccess> dir = DirAccess::open(output_dir);
bool partial_export = files_to_export.size() > 0;
report->session_files_total = partial_export ? files_to_export.size() : files.size();
if (opt_decompile) {
decompile_scripts(output_dir);
// This only works if we decompile the scripts first
recreate_plugin_configs(output_dir);
}
for (int i = 0; i < files.size(); i++) {
Ref<ImportInfo> iinfo = files[i];
String path = iinfo->get_path();
String source = iinfo->get_source_file();
String type = iinfo->get_type();
String importer = iinfo->get_importer();
auto loss_type = iinfo->get_import_loss_type();
// If files_to_export is empty, then we export everything
if (partial_export) {
auto dest_files = iinfo->get_dest_files();
bool has_path = false;
for (auto dest : dest_files) {
if (files_to_export.has(path)) {
has_path = true;
break;
}
}
if (!has_path) {
continue;
}
}
if (pr) {
if (OS::get_singleton()->get_ticks_usec() - last_progress_upd > 10000) {
last_progress_upd = OS::get_singleton()->get_ticks_usec();
bool cancel = pr->step(path, i, true);
if (cancel) {
return ERR_PRINTER_ON_FIRE;
}
}
}
if (loss_type != ImportInfo::LOSSLESS) {
if (!opt_lossy) {
report->lossy_imports.push_back(iinfo);
print_line("Not converting lossy import " + path);
continue;
}
}
// ***** Set export destination *****
iinfo->set_export_dest(iinfo->get_source_file());
bool should_rewrite_metadata = false;
// This is a Godot asset that was imported outside of project directory
if (!iinfo->get_source_file().begins_with("res://")) {
if (get_ver_major() <= 2) {
// import_md_path is the resource path in v2
iinfo->set_export_dest(String("res://.assets").path_join(iinfo->get_import_md_path().get_base_dir().path_join(iinfo->get_source_file().get_file()).replace("res://", "")));
} else {
// import_md_path is the .import/.remap path in v3-v4
iinfo->set_export_dest(iinfo->get_import_md_path().get_basename());
// If the source_file path was not actually in the project structure, save it elsewhere
if (iinfo->get_source_file().find(iinfo->get_export_dest().replace("res://", "")) == -1) {
iinfo->set_export_dest(iinfo->get_export_dest().replace("res://", "res://.assets"));
}
}
should_rewrite_metadata = true;
}
String src_ext = iinfo->get_source_file().get_extension().to_lower();
bool not_exported = false;
// ***** Export resource *****
if (opt_export_textures && importer == "texture") {
// Right now we only convert 2d image textures
auto tex_type = TextureLoaderCompat::recognize(path, &err);
switch (tex_type) {
case TextureLoaderCompat::FORMAT_V2_IMAGE_TEXTURE:
case TextureLoaderCompat::FORMAT_V3_STREAM_TEXTURE2D:
case TextureLoaderCompat::FORMAT_V4_COMPRESSED_TEXTURE2D: {
// Export texture
err = export_texture(output_dir, iinfo);
} break;
case TextureLoaderCompat::FORMAT_NOT_TEXTURE:
if (err == ERR_FILE_UNRECOGNIZED) {
WARN_PRINT("Import of type " + type + " is not a texture(?): " + path);
} else {
WARN_PRINT("Failed to load texture " + type + " " + path);
}
report->not_converted.push_back(iinfo);
break;
default:
report_unsupported_resource(type, src_ext, path);
report->not_converted.push_back(iinfo);
not_exported = true;
}
} else if (opt_export_samples && (importer == "sample" || importer == "wav")) {
err = export_sample(output_dir, iinfo);
} else if (opt_export_ogg && (importer == "ogg_vorbis" || importer == "oggvorbisstr")) {
err = convert_oggstr_to_ogg(output_dir, iinfo->get_path(), iinfo->get_export_dest());
} else if (opt_export_mp3 && importer == "mp3") {
err = convert_mp3str_to_mp3(output_dir, iinfo->get_path(), iinfo->get_export_dest());
} else if (importer == "bitmap") {
err = export_texture(output_dir, iinfo);
} else if ((opt_bin2text && iinfo->is_auto_converted())
//|| (opt_bin2text && iinfo->get_source_file().get_extension() == "tres" && iinfo->get_source_file().get_extension() == "res") ||
// (opt_bin2text && iinfo->get_importer() == "scene" && iinfo->get_source_file().get_extension() == "tscn")
) {
// We don't currently support converting old 2.x xml resources
if (src_ext == "xml") {
WARN_PRINT_ONCE("Conversion of Godot 2.x xml resource files currently unimplemented");
report_unsupported_resource(type, src_ext, path);
report->not_converted.push_back(iinfo);
not_exported = true;
}
err = convert_res_bin_2_txt(output_dir, iinfo->get_path(), iinfo->get_export_dest());
// v2-v3 export left the autoconverted resource in the main path, remove it
if (get_ver_major() <= 3 && !err) {
dir->remove(iinfo->get_path().replace("res://", ""));
}
} else if (importer == "scene" && !iinfo->is_auto_converted()) {
// escn files are scenes exported from a blender plugin in a godot compatible format
// These are the only ones we support at the moment
if (src_ext == "escn") {
err = convert_res_bin_2_txt(output_dir, iinfo->get_path(), iinfo->get_export_dest());
} else {
if (iinfo->get_ver_major() <= 3) {
WARN_PRINT_ONCE("Export of Godot 2/3.x models/imported scenes currently unimplemented");
report_unsupported_resource(itos(iinfo->get_ver_major()) + ".x " + type, src_ext, path);
report->not_converted.push_back(iinfo);
not_exported = true;
} else {
err = export_scene(output_dir, iinfo);
}
}
} else if (importer == "font_data_dynamic") {
err = export_fontfile(output_dir, iinfo);
} else if (importer == "csv_translation") {
err = export_translation(output_dir, iinfo);
} else if (importer == "wavefront_obj") {
WARN_PRINT_ONCE("Export of obj meshes currently unimplemented");
report_unsupported_resource(type, src_ext, path);
report->not_converted.push_back(iinfo);
not_exported = true;
} else {
report_unsupported_resource(type, src_ext, path);
report->not_converted.push_back(iinfo);
not_exported = true;
}
// ****REWRITE METADATA****
if (err == ERR_PRINTER_ON_FIRE || ((err == OK && should_rewrite_metadata) && iinfo->is_import())) {
if (iinfo->get_ver_major() <= 2 && opt_rewrite_imd_v2) {
// TODO: handle v2 imports with more than one source, like atlas textures
err = rewrite_import_source(iinfo->get_export_dest(), output_dir, iinfo);
} else if (iinfo->get_ver_major() >= 3 && opt_rewrite_imd_v3 && (iinfo->get_source_file().find(iinfo->get_export_dest().replace("res://", "")) != -1)) {
// Currently, we only rewrite the import data for v3 if the source file was somehow recorded as an absolute file path,
// But is still in the project structure
err = rewrite_import_source(iinfo->get_export_dest(), output_dir, iinfo);
} else if (iinfo->is_dirty()) {
err = iinfo->save_to(output_dir.path_join(iinfo->get_import_md_path().replace("res://", "")));
if (iinfo->get_export_dest() != iinfo->get_source_file()) {
// we still report this as unwritten so that we can report to the user that it was saved to a non-original path
err = ERR_PRINTER_ON_FIRE;
}
}
// if we didn't rewrite the metadata, the err will still be ERR_PRINTER_ON_FIRE
// if we failed, it won't be OK
if (err != OK) {
if (err == ERR_PRINTER_ON_FIRE) {
print_line("Did not rewrite import metadata for " + iinfo->get_source_file());
} else {
print_line("Failed to rewrite import metadata for " + iinfo->get_source_file());
}
err = ERR_DATABASE_CANT_WRITE;
} else {
// we successfully rewrote the import data
err = ERR_PRINTER_ON_FIRE;
}
// saved to non-original path, but we exporter knows we can't rewrite the metadata
} else if (err == ERR_DATABASE_CANT_WRITE) {
print_line("Did not rewrite import metadata for " + iinfo->get_source_file());
} else if (err == OK && iinfo->is_dirty()) {
err = iinfo->save_to(output_dir.path_join(iinfo->get_import_md_path().replace("res://", "")));
if (err == OK) {
err = ERR_PRINTER_ON_FIRE;
} else {
print_line("Failed to rewrite import metadata for " + iinfo->get_source_file());
err = ERR_DATABASE_CANT_WRITE;
}
}
// We continue down here so that modified import metadata is saved
if (not_exported) {
continue;
}
// write md5 files
if (opt_write_md5_files && iinfo->is_import() && (err == OK || err == ERR_PRINTER_ON_FIRE) && get_ver_major() > 2) {
err = ((Ref<ImportInfoModern>)iinfo)->save_md5_file(output_dir);
if (err && err != ERR_PRINTER_ON_FIRE) {
err = ERR_LINK_FAILED;
} else {
err = OK;
}
}
// ***** Record export result *****
// the following are successful exports, but we failed to rewrite metadata or write md5 files
// we had to rewrite the import metadata
if (err == ERR_PRINTER_ON_FIRE) {
report->rewrote_metadata.push_back(iinfo);
err = OK;
// necessary to rewrite import metadata but failed
} else if (err == ERR_DATABASE_CANT_WRITE) {
report->failed_rewrite_md.push_back(iinfo);
err = OK;
} else if (err == ERR_LINK_FAILED) {
report->failed_rewrite_md5.push_back(iinfo);
err = OK;
}
if (err == ERR_UNAVAILABLE) {
// already reported in exporters below
report->not_converted.push_back(iinfo);
} else if (err != OK) {
report->failed.push_back(iinfo);
print_line("Failed to convert " + type + " resource " + path);
} else {
if (loss_type != ImportInfo::LOSSLESS) {
report->lossy_imports.push_back(iinfo);
}
report->success.push_back(iinfo);
}
// remove remaps
if (!err && get_settings()->has_any_remaps()) {
if (get_settings()->has_remap(iinfo->get_export_dest(), iinfo->get_path())) {
get_settings()->remove_remap(iinfo->get_export_dest(), iinfo->get_path(), output_dir);
}
}
}
if (get_settings()->is_project_config_loaded()) { // some pcks do not have project configs
if (get_settings()->save_project_config(output_dir) != OK) {
print_line("ERROR: Failed to save project config!");
} else {
// Remove binary project config, as editors will load from it instead of the text one
dir->remove(get_settings()->get_project_config_path().get_file());
}
}
report->print_report();
return OK;
}
Error ImportExporter::decompile_scripts(const String &p_out_dir) {
GDScriptDecomp *decomp;
// we have to remove remaps if they exist
bool has_remaps = get_settings()->has_any_remaps();
Vector<String> code_files = get_settings()->get_code_files();
if (code_files.is_empty()) {
return OK;
}
uint64_t revision = 0;
// we don't consider the dev versions here, only the release/beta versions
switch (get_ver_major()) {
case 1:
switch (get_ver_minor()) {
case 0:
revision = 0xe82dc40;
break;
case 1:
revision = 0x65d48d6;
break;
}
break;
case 2:
switch (get_ver_minor()) {
case 0:
revision = 0x23441ec;
break;
case 1: {
switch (get_settings()->get_ver_rev()) {
case 0:
case 1:
revision = 0x7124599;
break;
case 2:
revision = 0x85585c7;
break;
case 3:
case 4:
case 5:
case 6:
revision = 0xed80f45;
break;
}
} break;
}
break;
case 3:
switch (get_ver_minor()) {
case 0:
revision = 0x54a2ac;
break;
case 1: {
// They broke compatibility for 3.1.x in successive patch releases.
if (get_settings()->get_version_string().contains("beta")) {
revision = 0x1ca61a3;
} else {
switch (get_settings()->get_ver_rev()) {
case 0:
revision = 0x1a36141;
break;
case 1:
revision = 0x514a3fb;
break;
}
}
} break;
case 2:
case 3:
case 4:
revision = 0x5565f55;
break;
case 5:
revision = 0xa7aad78;
break;
default:
// We do not anticipate further GSScript changes in 3.x because GDScript 2.0 in 4.x can't be backported
// but just in case...
WARN_PRINT("Unsupported version 3." + itos(get_ver_minor()) + "." + itos(get_ver_rev()) + " of Godot detected");
WARN_PRINT("If your scripts are mangled, please report this on the Github issue tracker");
revision = 0xa7aad78;
break;
}
break;
case 4:
switch (get_ver_minor()) {
// Compiled mode was removed in 4.0; if this is part of a 4.0 project, might be in one of the pre GDScript 2.0 4.0-dev bytecodes
// but we can't really detect which mutually incompatible revision it is (there are several), so we just fail
case 0:
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Support for Godot 4.0 pre-release dev GDScript not implemented, failed to decompile");
break;
case 1: // might be added back in 4.1; if so, this will need to be updated
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Support for Godot 4.1 GDScript not yet implemented, failed to decompile");
break;
default:
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown version, failed to decompile");
break;
}
break;
default:
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown version, failed to decompile");
}
if (revision > 0) {
decomp = create_decomp_for_commit(revision);
} else {
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown version, failed to decompile");
}
print_line("Script version " + get_settings()->get_version_string() + " (rev 0x" + String::num_int64(revision, 16) + ") detected");
Error err;
for (String f : code_files) {
String dest_file = f.replace(".gdc", ".gd").replace(".gde", ".gd");
Ref<DirAccess> da = DirAccess::open(p_out_dir);
print_verbose("decompiling " + f);
bool encrypted = false;
if (f.get_extension().to_lower() == "gde") {
encrypted = true;
err = decomp->decompile_byte_code_encrypted(f, get_settings()->get_encryption_key());
} else {
err = decomp->decompile_byte_code(f);
}
if (err) {
memdelete(decomp);
report->failed_scripts.push_back(f);
// TODO: make it not fail hard on the first script that fails to decompile
if (encrypted) {
report->had_encryption_error = true;
ERR_FAIL_V_MSG(err, "error decompiling encrypted script " + f);
} else {
ERR_FAIL_V_MSG(err, "error decompiling " + f);
}
} else {
String text = decomp->get_script_text();
String out_path = p_out_dir.path_join(dest_file.replace("res://", ""));
Ref<FileAccess> fa = FileAccess::open(out_path, FileAccess::WRITE);
if (fa.is_null()) {
report->failed_scripts.push_back(f);
memdelete(decomp);
ERR_FAIL_V_MSG(ERR_FILE_CANT_WRITE, "error failed to save " + f);
}
fa->store_string(text);
da->remove(f.replace("res://", ""));
if (has_remaps && get_settings()->has_remap(f, dest_file)) {
get_settings()->remove_remap(f, dest_file);
}
// TODO: make "remove_remap" do this instead
if (da->file_exists(f.replace(".gdc", ".gd.remap").replace("res://", ""))) {
da->remove(f.replace(".gdc", ".gd.remap").replace("res://", ""));
}
print_verbose("successfully decompiled " + f);
report->decompiled_scripts.push_back(f);
}
}
memdelete(decomp);
// save changed config file
if (get_settings()->is_project_config_loaded()) { // some game pcks do not have project configs
err = get_settings()->save_project_config(p_out_dir);
}
if (err) {
WARN_PRINT("Failed to save changed project config!");
}
return OK;
}
Error ImportExporter::recreate_plugin_config(const String &output_dir, const String &plugin_dir) {
Error err;
static const Vector<String> wildcards = { "*.gd" };
String rel_plugin_path = String("addons").path_join(plugin_dir);
String abs_plugin_path = output_dir.path_join(rel_plugin_path);
auto gd_scripts = gdreutil::get_recursive_dir_list(abs_plugin_path, wildcards, false);
String main_script;
auto copy_gdext_dll_func = [&](String gdextension_path) -> Error {
bool is_gdnative = gdextension_path.ends_with(".gdnlib");
String lib_section_name = is_gdnative ? "entry" : "libraries";
// check if directory; we can't do anything here if we're working from an unpacked dir
if (get_settings()->get_pack_type() == GDRESettings::PackInfo::PackType::DIR) {
return OK;
}
String parent_dir = get_settings()->get_pack_path().get_base_dir();
// open gdextension_path like a config file
Vector<String> libs_to_find;
List<String> platforms;
Dictionary lib_to_parentdir;
Dictionary lib_to_platform;
// what the decomp is running on
String our_platform = OS::get_singleton()->get_name().to_lower();
Ref<ConfigFile> cf;
cf.instantiate();
cf->load(gdextension_path);
cf->get_section_keys(lib_section_name, &platforms);
auto add_path_func = [&](String path, String platform) mutable {
String plugin_pardir = path.get_base_dir();
if (!plugin_pardir.is_absolute_path()) {
plugin_pardir = rel_plugin_path.path_join(plugin_pardir);
} else {
plugin_pardir = plugin_pardir.replace("res://", "");
}
libs_to_find.push_back(path.get_file());
lib_to_parentdir[path.get_file()] = plugin_pardir;
auto splits = platform.split(".");
if (!splits.is_empty()) {
platform = splits[0];
}
platform = platform.to_lower();
// normalize old platform names
if (platform == "x11") {
platform = "linux";
} else if (platform == "osx") {
platform = "macos";
}
lib_to_platform[path.get_file()] = platform;
};
for (String &platform : platforms) {
add_path_func(cf->get_value(lib_section_name, platform), platform);
}
platforms.clear();
cf->get_section_keys("dependencies", &platforms);
for (String &platform : platforms) {
Array keys;
if (!is_gdnative) {
Dictionary val = cf->get_value("dependencies", platform);
// the keys in the dict are the library names
keys = val.keys();
} else {
// they're just arrays in gdnlib
keys = cf->get_value("dependencies", platform);
}
for (int i = 0; i < keys.size(); i++) {
add_path_func(keys[i], platform);
}
}
// search for the libraries
Vector<String> lib_paths;
// auto paths = gdreutil::get_recursive_dir_list(parent_dir, libs_to_find, true);
// get a non-recursive dir listing
{
Ref<DirAccess> da = DirAccess::open(parent_dir, &err);
ERR_FAIL_COND_V_MSG(err, ERR_FILE_CANT_OPEN, "Failed to open directory " + parent_dir);
da->list_dir_begin();
String f = da->get_next();
while (!f.is_empty()) {
if (f != "." && f != "..") {
if (libs_to_find.has(f)) {
lib_paths.push_back(parent_dir.path_join(f));
}
}
f = da->get_next();
}
}
if (lib_paths.size() == 0) {
// if we're on MacOS, try one path up
if (parent_dir.get_file() == "Resources") {
parent_dir = parent_dir.get_base_dir();
lib_paths = gdreutil::get_recursive_dir_list(parent_dir, libs_to_find, true);
}
if (lib_paths.size() == 0) {
WARN_PRINT("Failed to find gdextension libraries for plugin " + plugin_dir);
return ERR_BUG;
}
}
// copy all of the libraries to the plugin directory
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
bool found_our_platform = false;
for (String path : lib_paths) {
String lib_name = path.get_file();
String lib_dir_path = output_dir.path_join(lib_to_parentdir[lib_name]);
if (!da->dir_exists(lib_dir_path)) {
ERR_FAIL_COND_V_MSG(da->make_dir_recursive(lib_dir_path), ERR_FILE_CANT_WRITE, "Failed to make plugin directory " + lib_dir_path);
}
String dest_path = lib_dir_path.path_join(lib_name);
ERR_FAIL_COND_V_MSG(da->copy(path, dest_path), ERR_FILE_CANT_WRITE, "Failed to copy library " + path + " to " + dest_path);
if (lib_to_platform[lib_name] == our_platform) {
found_our_platform = true;
}
}
if (!found_our_platform) {
WARN_PRINT("Failed to find library for our platform for plugin " + plugin_dir);
return ERR_PRINTER_ON_FIRE;
}
return OK;
};
bool found_our_platform = true;
if (get_ver_major() >= 3) {
// check if this is a gdextension/gdnative plugin
static const Vector<String> gdext_wildcards = { "*.gdextension", "*.gdnlib" };
auto gdexts = gdreutil::get_recursive_dir_list(abs_plugin_path, gdext_wildcards, true);
for (String gdext : gdexts) {
err = copy_gdext_dll_func(gdext);
if (err) {
found_our_platform = false;
}
}
// gdextension/gdnative plugins may not have a main script, and no plugin.cfg
if (gd_scripts.is_empty()) {
return found_our_platform ? OK : ERR_PRINTER_ON_FIRE;
}
}
for (int j = 0; j < gd_scripts.size(); j++) {
String gd_script_abs_path = abs_plugin_path.path_join(gd_scripts[j]);
String gd_text = FileAccess::get_file_as_string(gd_script_abs_path, &err);
ERR_FAIL_COND_V_MSG(err, err, "failed to open gd_script " + gd_script_abs_path + "!");
if (gd_text.find("extends EditorPlugin") != -1) {
main_script = gd_scripts[j];
break;
}
}
ERR_FAIL_COND_V_MSG(main_script == "", ERR_FILE_NOT_FOUND, "Failed to find main script for plugin " + plugin_dir + "!");
String plugin_cfg_text = String("[plugin]\n\n") +
"name=\"" + plugin_dir.replace("_", " ").replace(".", " ") + "\"\n" +
"description=\"" + plugin_dir.replace("_", " ").replace(".", " ") + " plugin\"\n" +
"author=\"Unknown\"\n" +
"version=\"1.0\"\n" +
"script=\"" + main_script + "\"";
Ref<FileAccess> f = FileAccess::open(abs_plugin_path.path_join("plugin.cfg"), FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, "can't open plugin.cfg for writing");
f->store_string(plugin_cfg_text);
print_line("Recreated plugin config for " + plugin_dir);
return found_our_platform ? OK : ERR_PRINTER_ON_FIRE;
}
// Recreates the "plugin.cfg" files for each plugin to avoid loading errors.
Error ImportExporter::recreate_plugin_configs(const String &output_dir) {
Error err;
if (!DirAccess::exists(output_dir.path_join("addons"))) {
return OK;
}
Vector<String> dirs;
Ref<DirAccess> da = DirAccess::open(output_dir.path_join("addons"), &err);
da->list_dir_begin();
String f = da->get_next();
while (!f.is_empty()) {
if (f != "." && f != ".." && da->current_is_dir()) {
dirs.append(f);
}
f = da->get_next();
}
da->list_dir_end();
for (int i = 0; i < dirs.size(); i++) {
if (dirs[i].contains("godotsteam")) {
report->godotsteam_detected = true;
}
err = recreate_plugin_config(output_dir, dirs[i]);
if (err == ERR_PRINTER_ON_FIRE) {
// we successfully copied the dlls, but failed to find one for our platform
WARN_PRINT("Failed to find library for this platform for plugin " + dirs[i]);
report->failed_gdnative_copy.push_back(dirs[i]);
} else if (err) {
WARN_PRINT("Failed to recreate plugin.cfg for " + dirs[i]);
report->failed_plugin_cfg_create.push_back(dirs[i]);
}
}
return OK;
}
Error ImportExporter::export_fontfile(const String &output_dir, Ref<ImportInfo> &iinfo) {
Error err;
Ref<FontFile> fontfile = ResourceLoader::load(iinfo->get_path(), "", ResourceFormatLoader::CACHE_MODE_IGNORE, &err);
ERR_FAIL_COND_V_MSG(err, err, "Failed to load font file " + iinfo->get_path());
String out_path = output_dir.path_join(iinfo->get_export_dest().replace("res://", ""));
err = ensure_dir(out_path.get_base_dir());
ERR_FAIL_COND_V(err, err);
Ref<FileAccess> f = FileAccess::open(out_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(f.is_null(), ERR_FILE_CANT_WRITE);
// just get the raw data and write it; TTF files are stored as raw data in the fontdata file
PackedByteArray data = fontfile->get_data();
f->store_buffer(data.ptr(), data.size());
f->flush();
return OK;
}
#define TEST_TR_KEY(key) \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
} \
key = key.to_upper(); \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
} \
key = key.to_lower(); \
test = default_translation->get_message(key); \
if (test == s) { \
return key; \
}
String guess_key_from_tr(String s, Ref<Translation> default_translation) {
static const Vector<String> prefixes = { "$$", "##", "TR_", "KEY_TEXT_" };
String key = s;
String test;
TEST_TR_KEY(key);
String str = s;
//remove punctuation
str = str.replace("\n", "").replace(".", "").replace("…", "").replace("!", "").replace("?", "");
str = str.strip_escapes().strip_edges().replace(" ", "_");
key = str;
TEST_TR_KEY(key);
// Try adding prefixes
for (String prefix : prefixes) {
key = prefix + str;
TEST_TR_KEY(key);
}
// failed
return "";
}
Error ImportExporter::export_translation(const String &output_dir, Ref<ImportInfo> &iinfo) {
Error err;
ResourceFormatLoaderCompat rlc;
// translation files are usually imported from one CSV and converted to multiple "<LOCALE>.translation" files
String default_locale = get_settings()->pack_has_project_config() && get_settings()->has_project_setting("locale/fallback")
? get_settings()->get_project_setting("locale/fallback")
: "en";
Vector<Ref<Translation>> translations;
Vector<Vector<StringName>> translation_messages;
Ref<Translation> default_translation;
Vector<StringName> default_messages;
String header = "key";
Vector<StringName> keys;
for (String path : iinfo->get_dest_files()) {
Ref<Translation> tr = rlc.load(path, "", &err);
ERR_FAIL_COND_V_MSG(err != OK, err, "Could not load translation file " + iinfo->get_path());
ERR_FAIL_COND_V_MSG(!tr.is_valid(), err, "Translation file " + iinfo->get_path() + " was not valid");
String locale = tr->get_locale();
header += "," + locale;
List<StringName> message_list;
Vector<StringName> messages;
if (tr->get_class_name() == "OptimizedTranslation") {
Ref<OptimizedTranslation> otr = tr;
Ref<OptimizedTranslationExtractor> ote;
ote.instantiate();
ote->set("locale", locale);
ote->set("hash_table", otr->get("hash_table"));
ote->set("bucket_table", otr->get("bucket_table"));
ote->set("strings", otr->get("strings"));
ote->get_message_value_list(&message_list);
for (auto message : message_list) {
messages.push_back(message);
}
} else {
// We have a real translation class, get the keys
if (locale == default_locale) {
List<StringName> key_list;
tr->get_message_list(&key_list);
for (auto key : key_list) {
keys.push_back(key);
}
}
Dictionary msgdict = tr->get("messages");
Array values = msgdict.values();
for (int i = 0; i < values.size(); i++) {
messages.push_back(values[i]);
}
}
if (locale == default_locale) {
default_messages = messages;
default_translation = tr;
}
translation_messages.push_back(messages);
translations.push_back(tr);
}
// We can't recover the keys from Optimized translations, we have to guess
int missing_keys = 0;
if (default_translation.is_null()) {
if (translations.size() == 1) {
// this is one of the rare dynamic translation schemes, which we don't support currently
report_unsupported_resource("Translation", "Dynamic multi-csv", iinfo->get_path());
return ERR_UNAVAILABLE;
}
ERR_FAIL_V_MSG(ERR_FILE_MISSING_DEPENDENCIES, "No default translation found for " + iinfo->get_path());
}
if (keys.size() == 0) {
for (const StringName &s : default_messages) {
String key = guess_key_from_tr(s, default_translation);
if (key.is_empty()) {
missing_keys++;
keys.push_back("<MISSING KEY " + s + ">");
} else {
keys.push_back(key);
}
}
}
header += "\n";
if (missing_keys) {
iinfo->set_export_dest("res://.assets/" + iinfo->get_source_file().replace("res://", ""));
}
String output_path = output_dir.simplify_path().path_join(iinfo->get_export_dest().replace("res://", ""));
err = ensure_dir(output_path.get_base_dir());
ERR_FAIL_COND_V(err, err);
Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(f.is_null(), ERR_FILE_CANT_WRITE);
// Set UTF-8 BOM (required for opening with Excel in UTF-8 format, works with all Godot versions)
f->store_8(0xef);
f->store_8(0xbb);
f->store_8(0xbf);
f->store_string(header);
for (int i = 0; i < keys.size(); i++) {
Vector<String> line_values;
line_values.push_back(keys[i]);
for (auto messages : translation_messages) {
line_values.push_back(messages[i]);
}
f->store_csv_line(line_values, ",");
}
f->flush();
f = Ref<FileAccess>();
if (missing_keys) {
report->translation_export_message += "WARNING: Could not recover " + itos(missing_keys) + " keys for translation.csv" + "\n";
report->translation_export_message += "Saved " + iinfo->get_source_file().get_file() + " to " + iinfo->get_export_dest() + "\n";
WARN_PRINT("Could not guess all keys in translation.csv");
}
print_line("Recreated translation.csv");
return missing_keys ? ERR_DATABASE_CANT_WRITE : OK;
}
Error _export_scene(const String &output_dir, Ref<ImportInfo> &iinfo) {
Error err;
// All 3.x scenes that were imported from scenes/models SHOULD be compatible with 4.x
// The "escn" format basically force Godot to have compatibility with 3.x scenes
// This will also pull in any dependencies that were created by the importer (like textures and materials, which should also be similarly compatible)
Ref<PackedScene> scene = ResourceLoader::load(iinfo->get_path(), "", ResourceFormatLoader::CACHE_MODE_IGNORE, &err);
ERR_FAIL_COND_V_MSG(err, err, "Failed to load scene " + iinfo->get_path());
// GLTF export can result in inaccurate models
// save it under .assets, which won't be picked up for import by the godot editor
String new_path = iinfo->get_export_dest().replace("res://", "res://.assets/");
// we only export glbs
if (new_path.get_extension().to_lower() != "glb") {
new_path = new_path.get_basename() + ".glb";
}
iinfo->set_export_dest(new_path);
String out_path = output_dir.path_join(iinfo->get_export_dest().replace("res://", ""));
err = ImportExporter::ensure_dir(out_path.get_base_dir());
List<String> deps;
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> state;
state.instantiate();
int32_t flags = 0;
flags |= 16; // EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
Node *root = scene->instantiate();
err = doc->append_from_scene(root, state, flags);
ERR_FAIL_COND_V_MSG(err, err, "Failed to append scene " + iinfo->get_path() + " to glTF document");
err = doc->write_to_filesystem(state, out_path);
root->queue_free();
ERR_FAIL_COND_V_MSG(err, err, "Failed to write glTF document to " + out_path);
return OK;
}
Error ImportExporter::export_scene(const String &output_dir, Ref<ImportInfo> &iinfo) {
Error err;
report->exported_scenes = true;
Vector<uint64_t> texture_uids;
{
// real load here
List<String> get_deps;
// We need to preload any Texture resources that are used by the scene with our own loader
ResourceLoader::get_dependencies(iinfo->get_path(), &get_deps, true);
TextureLoaderCompat tlc;
tlc.glft_export = true; // overides `get_image()` for glTF export
Vector<Ref<Texture>> textures;
for (String dep : get_deps) {
auto splits = dep.split("::");
if (splits[1].contains("Texture")) {
Ref<ImportInfo> tex_iinfo = get_settings()->get_import_info_by_source(splits[2]);
if (tex_iinfo.is_null()) {
WARN_PRINT("Failed to find import info for texture " + splits[2] + " for scene " + iinfo->get_path());
continue;
}
Ref<Texture> texture = tlc.load_texture(tex_iinfo->get_path(), &err);
if (err || texture.is_null()) {
WARN_PRINT("Failed to load texture " + tex_iinfo->get_path() + " for scene " + iinfo->get_path());
continue;
}
#ifdef TOOLS_ENABLED
texture->set_import_path(tex_iinfo->get_path());
#endif
texture->set_path(splits[2]);
if (!splits[0].is_empty()) {
auto id = ResourceUID::get_singleton()->text_to_id(splits[0]);
if (id != ResourceUID::INVALID_ID) {
if (!ResourceUID::get_singleton()->has_id(id)) {
ResourceUID::get_singleton()->add_id(id, splits[2]);
} else {
ResourceUID::get_singleton()->set_id(id, splits[2]);
}
texture_uids.push_back(id);
}
}
textures.push_back(texture);
}
}
err = _export_scene(output_dir, iinfo);
}
// remove the UIDs
for (uint64_t id : texture_uids) {
ResourceUID::get_singleton()->remove_id(id);
}
return err ? err : ERR_PRINTER_ON_FIRE; // We always save to an unoriginal path
}
Error ImportExporter::export_texture(const String &output_dir, Ref<ImportInfo> &iinfo) {
String path = iinfo->get_path();
String source = iinfo->get_source_file();
bool should_rewrite_metadata = false;
bool lossy = false;
// for Godot 2.x resources, we can easily rewrite the metadata to point to a renamed file with a different extension,
// but this isn't the case for 3.x and greater, so we have to save in the original (lossy) format.
String source_ext = source.get_extension().to_lower();
if (source_ext != "png" || iinfo->get_ver_major() == 2) {
if (iinfo->get_ver_major() > 2) {
if ((source_ext == "jpg" || source_ext == "jpeg") && opt_export_jpg) {
lossy = true;
} else if (source_ext == "webp" && opt_export_webp) {
// if the engine is <3.4, it can't handle lossless encoded WEBPs
if (get_ver_major() < 4 && !(get_ver_major() == 3 && get_ver_minor() >= 4)) {
lossy = true;
}
} else {
iinfo->set_export_dest(iinfo->get_export_dest().get_basename() + ".png");
// If this is version 3-4, we need to rewrite the import metadata to point to the new resource name
// disable this for now
if (false && opt_rewrite_imd_v3 && iinfo->is_import()) {
should_rewrite_metadata = true;
} else {
// save it under .assets, which won't be picked up for import by the godot editor
if (!iinfo->get_export_dest().replace("res://", "").begins_with(".assets")) {
String prefix = ".assets";
if (iinfo->get_export_dest().begins_with("res://")) {
prefix = "res://.assets";
}
iinfo->set_export_dest(prefix.path_join(iinfo->get_export_dest().replace("res://", "")));
}
}
}
} else { //version 2
iinfo->set_export_dest(iinfo->get_export_dest().get_basename() + ".png");
if (opt_rewrite_imd_v2 && iinfo->is_import()) {
should_rewrite_metadata = true;
}
}
}
Error err;
if (iinfo->get_importer() == "bitmap") {
err = _convert_bitmap(output_dir, path, iinfo->get_export_dest(), lossy);
} else {
err = _convert_tex(output_dir, path, iinfo->get_export_dest(), lossy);
}
if (err == ERR_UNAVAILABLE) {
// Already reported in export functions above
return ERR_UNAVAILABLE;
}
ERR_FAIL_COND_V(err, err);
// If lossy, also convert it as a png
if (lossy) {
String dest = iinfo->get_export_dest().get_basename() + ".png";
if (!dest.replace("res://", "").begins_with(".assets")) {
String prefix = ".assets";
if (dest.begins_with("res://")) {
prefix = "res://.assets";