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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 | /**************************************************************************/
/* export_template_manager.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "export_template_manager.h"
#include "core/io/dir_access.h"
#include "core/io/json.h"
#include "core/io/zip_io.h"
#include "core/version.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
#include "editor/progress_dialog.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/file_dialog.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/separator.h"
#include "scene/gui/tree.h"
#include "scene/main/http_request.h"
enum DownloadsAvailability {
DOWNLOADS_AVAILABLE,
DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE,
DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS,
};
static DownloadsAvailability _get_downloads_availability() {
const int network_mode = EDITOR_GET("network/connection/network_mode");
if (network_mode == EditorSettings::NETWORK_OFFLINE) {
return DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE;
}
// Downloadable export templates are only available for stable and official alpha/beta/RC builds
// (which always have a number following their status, e.g. "alpha1").
// Therefore, don't display download-related features when using a development version
// (whose builds aren't numbered).
if (String(VERSION_STATUS) == String("dev") ||
String(VERSION_STATUS) == String("alpha") ||
String(VERSION_STATUS) == String("beta") ||
String(VERSION_STATUS) == String("rc")) {
return DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS;
}
return DOWNLOADS_AVAILABLE;
}
void ExportTemplateManager::_update_template_status() {
// Fetch installed templates from the file system.
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
Error err = da->change_dir(templates_dir);
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
RBSet<String> templates;
da->list_dir_begin();
if (err == OK) {
String c = da->get_next();
while (!c.is_empty()) {
if (da->current_is_dir() && !c.begins_with(".")) {
templates.insert(c);
}
c = da->get_next();
}
}
da->list_dir_end();
// Update the state of the current version.
String current_version = VERSION_FULL_CONFIG;
current_value->set_text(current_version);
if (templates.has(current_version)) {
current_missing_label->hide();
current_installed_label->show();
current_installed_hb->show();
current_version_exists = true;
} else {
current_installed_label->hide();
current_missing_label->show();
current_installed_hb->hide();
current_version_exists = false;
}
if (is_downloading_templates) {
install_options_vb->hide();
download_progress_hb->show();
} else {
download_progress_hb->hide();
install_options_vb->show();
if (templates.has(current_version)) {
current_installed_path->set_text(templates_dir.path_join(current_version));
}
}
// Update the list of other installed versions.
installed_table->clear();
TreeItem *installed_root = installed_table->create_item();
for (RBSet<String>::Element *E = templates.back(); E; E = E->prev()) {
String version_string = E->get();
if (version_string == current_version) {
continue;
}
TreeItem *ti = installed_table->create_item(installed_root);
ti->set_text(0, version_string);
#ifndef ANDROID_ENABLED
ti->add_button(0, get_editor_theme_icon(SNAME("Folder")), OPEN_TEMPLATE_FOLDER, false, TTR("Open the folder containing these templates."));
#endif
ti->add_button(0, get_editor_theme_icon(SNAME("Remove")), UNINSTALL_TEMPLATE, false, TTR("Uninstall these templates."));
}
}
void ExportTemplateManager::_download_current() {
if (is_downloading_templates) {
return;
}
is_downloading_templates = true;
install_options_vb->hide();
download_progress_hb->show();
if (mirrors_available) {
String mirror_url = _get_selected_mirror();
if (mirror_url.is_empty()) {
_set_current_progress_status(TTR("There are no mirrors available."), true);
return;
}
_download_template(mirror_url, true);
} else if (!is_refreshing_mirrors) {
_set_current_progress_status(TTR("Retrieving the mirror list..."));
_refresh_mirrors();
}
}
void ExportTemplateManager::_download_template(const String &p_url, bool p_skip_check) {
if (!p_skip_check && is_downloading_templates) {
return;
}
is_downloading_templates = true;
install_options_vb->hide();
download_progress_hb->show();
download_progress_bar->show();
download_progress_bar->set_indeterminate(true);
_set_current_progress_status(TTR("Starting the download..."));
download_templates->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_templates.tpz"));
download_templates->set_use_threads(true);
const String proxy_host = EDITOR_GET("network/http_proxy/host");
const int proxy_port = EDITOR_GET("network/http_proxy/port");
download_templates->set_http_proxy(proxy_host, proxy_port);
download_templates->set_https_proxy(proxy_host, proxy_port);
Error err = download_templates->request(p_url);
if (err != OK) {
_set_current_progress_status(TTR("Error requesting URL:") + " " + p_url, true);
download_progress_hb->hide();
return;
}
set_process(true);
_set_current_progress_status(TTR("Connecting to the mirror..."));
}
void ExportTemplateManager::_download_template_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
switch (p_status) {
case HTTPRequest::RESULT_CANT_RESOLVE: {
_set_current_progress_status(TTR("Can't resolve the requested address."), true);
} break;
case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
case HTTPRequest::RESULT_CONNECTION_ERROR:
case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR:
case HTTPRequest::RESULT_CANT_CONNECT: {
_set_current_progress_status(TTR("Can't connect to the mirror."), true);
} break;
case HTTPRequest::RESULT_NO_RESPONSE: {
_set_current_progress_status(TTR("No response from the mirror."), true);
} break;
case HTTPRequest::RESULT_REQUEST_FAILED: {
_set_current_progress_status(TTR("Request failed."), true);
} break;
case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
_set_current_progress_status(TTR("Request ended up in a redirect loop."), true);
} break;
default: {
if (p_code != 200) {
_set_current_progress_status(TTR("Request failed:") + " " + itos(p_code), true);
} else {
_set_current_progress_status(TTR("Download complete; extracting templates..."));
String path = download_templates->get_download_file();
is_downloading_templates = false;
bool ret = _install_file_selected(path, true);
if (ret) {
// Clean up downloaded file.
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = da->remove(path);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Cannot remove temporary file:") + "\n" + path + "\n");
}
} else {
EditorNode::get_singleton()->add_io_error(vformat(TTR("Templates installation failed.\nThe problematic templates archives can be found at '%s'."), path));
}
}
} break;
}
set_process(false);
}
void ExportTemplateManager::_cancel_template_download() {
if (!is_downloading_templates) {
return;
}
download_templates->cancel_request();
download_progress_hb->hide();
install_options_vb->show();
is_downloading_templates = false;
}
void ExportTemplateManager::_refresh_mirrors() {
if (is_refreshing_mirrors) {
return;
}
is_refreshing_mirrors = true;
String current_version = VERSION_FULL_CONFIG;
const String mirrors_metadata_url = "https://godotengine.org/mirrorlist/" + current_version + ".json";
request_mirrors->request(mirrors_metadata_url);
}
void ExportTemplateManager::_refresh_mirrors_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
EditorNode::get_singleton()->show_warning(TTR("Error getting the list of mirrors."));
is_refreshing_mirrors = false;
if (is_downloading_templates) {
_cancel_template_download();
}
return;
}
String response_json;
{
const uint8_t *r = p_data.ptr();
response_json.parse_utf8((const char *)r, p_data.size());
}
JSON json;
Error err = json.parse(response_json);
if (err != OK) {
EditorNode::get_singleton()->show_warning(TTR("Error parsing JSON with the list of mirrors. Please report this issue!"));
is_refreshing_mirrors = false;
if (is_downloading_templates) {
_cancel_template_download();
}
return;
}
mirrors_list->clear();
mirrors_list->add_item(TTR("Best available mirror"), 0);
mirrors_available = false;
Dictionary mirror_data = json.get_data();
if (mirror_data.has("mirrors")) {
Array mirrors = mirror_data["mirrors"];
for (int i = 0; i < mirrors.size(); i++) {
Dictionary m = mirrors[i];
ERR_CONTINUE(!m.has("url") || !m.has("name"));
mirrors_list->add_item(m["name"]);
mirrors_list->set_item_metadata(i + 1, m["url"]);
mirrors_available = true;
}
}
if (!mirrors_available) {
EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
if (is_downloading_templates) {
_cancel_template_download();
}
}
is_refreshing_mirrors = false;
if (is_downloading_templates) {
String mirror_url = _get_selected_mirror();
if (mirror_url.is_empty()) {
_set_current_progress_status(TTR("There are no mirrors available."), true);
return;
}
_download_template(mirror_url, true);
}
}
bool ExportTemplateManager::_humanize_http_status(HTTPRequest *p_request, String *r_status, int *r_downloaded_bytes, int *r_total_bytes) {
*r_status = "";
*r_downloaded_bytes = -1;
*r_total_bytes = -1;
bool success = true;
switch (p_request->get_http_client_status()) {
case HTTPClient::STATUS_DISCONNECTED:
*r_status = TTR("Disconnected");
success = false;
break;
case HTTPClient::STATUS_RESOLVING:
*r_status = TTR("Resolving");
break;
case HTTPClient::STATUS_CANT_RESOLVE:
*r_status = TTR("Can't Resolve");
success = false;
break;
case HTTPClient::STATUS_CONNECTING:
*r_status = TTR("Connecting...");
break;
case HTTPClient::STATUS_CANT_CONNECT:
*r_status = TTR("Can't Connect");
success = false;
break;
case HTTPClient::STATUS_CONNECTED:
*r_status = TTR("Connected");
break;
case HTTPClient::STATUS_REQUESTING:
*r_status = TTR("Requesting...");
break;
case HTTPClient::STATUS_BODY:
*r_status = TTR("Downloading");
*r_downloaded_bytes = p_request->get_downloaded_bytes();
*r_total_bytes = p_request->get_body_size();
if (p_request->get_body_size() > 0) {
*r_status += " " + String::humanize_size(p_request->get_downloaded_bytes()) + "/" + String::humanize_size(p_request->get_body_size());
} else {
*r_status += " " + String::humanize_size(p_request->get_downloaded_bytes());
}
break;
case HTTPClient::STATUS_CONNECTION_ERROR:
*r_status = TTR("Connection Error");
success = false;
break;
case HTTPClient::STATUS_TLS_HANDSHAKE_ERROR:
*r_status = TTR("TLS Handshake Error");
success = false;
break;
}
return success;
}
void ExportTemplateManager::_set_current_progress_status(const String &p_status, bool p_error) {
download_progress_label->set_text(p_status);
if (p_error) {
download_progress_bar->hide();
download_progress_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
} else {
download_progress_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Label")));
}
}
void ExportTemplateManager::_set_current_progress_value(float p_value, const String &p_status) {
download_progress_bar->show();
download_progress_bar->set_indeterminate(false);
download_progress_bar->set_value(p_value);
download_progress_label->set_text(p_status);
}
void ExportTemplateManager::_install_file() {
install_file_dialog->popup_file_dialog();
}
bool ExportTemplateManager::_install_file_selected(const String &p_file, bool p_skip_progress) {
Ref<FileAccess> io_fa;
zlib_filefunc_def io = zipio_create_io(&io_fa);
unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
if (!pkg) {
EditorNode::get_singleton()->show_warning(TTR("Can't open the export templates file."));
return false;
}
int ret = unzGoToFirstFile(pkg);
// Count them and find version.
int fc = 0;
String version;
String contents_dir;
while (ret == UNZ_OK) {
unz_file_info info;
char fname[16384];
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
if (ret != UNZ_OK) {
break;
}
String file = String::utf8(fname);
// Skip the __MACOSX directory created by macOS's built-in file zipper.
if (file.begins_with("__MACOSX")) {
ret = unzGoToNextFile(pkg);
continue;
}
if (file.ends_with("version.txt")) {
Vector<uint8_t> uncomp_data;
uncomp_data.resize(info.uncompressed_size);
// Read.
unzOpenCurrentFile(pkg);
ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", file));
unzCloseCurrentFile(pkg);
String data_str;
data_str.parse_utf8((const char *)uncomp_data.ptr(), uncomp_data.size());
data_str = data_str.strip_edges();
// Version number should be of the form major.minor[.patch].status[.module_config]
// so it can in theory have 3 or more slices.
if (data_str.get_slice_count(".") < 3) {
EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside the export templates file: %s."), data_str));
unzClose(pkg);
return false;
}
version = data_str;
contents_dir = file.get_base_dir().trim_suffix("/").trim_suffix("\\");
}
if (file.get_file().size() != 0) {
fc++;
}
ret = unzGoToNextFile(pkg);
}
if (version.is_empty()) {
EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside the export templates file."));
unzClose(pkg);
return false;
}
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
String template_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(version);
Error err = d->make_dir_recursive(template_path);
if (err != OK) {
EditorNode::get_singleton()->show_warning(TTR("Error creating path for extracting templates:") + "\n" + template_path);
unzClose(pkg);
return false;
}
EditorProgress *p = nullptr;
if (!p_skip_progress) {
p = memnew(EditorProgress("ltask", TTR("Extracting Export Templates"), fc));
}
fc = 0;
ret = unzGoToFirstFile(pkg);
while (ret == UNZ_OK) {
// Get filename.
unz_file_info info;
char fname[16384];
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
if (ret != UNZ_OK) {
break;
}
if (String::utf8(fname).ends_with("/")) {
// File is a directory, ignore it.
// Directories will be created when extracting each file.
ret = unzGoToNextFile(pkg);
continue;
}
String file_path(String::utf8(fname).simplify_path());
String file = file_path.get_file();
// Skip the __MACOSX directory created by macOS's built-in file zipper.
if (file.is_empty() || file.begins_with("__MACOSX")) {
ret = unzGoToNextFile(pkg);
continue;
}
Vector<uint8_t> uncomp_data;
uncomp_data.resize(info.uncompressed_size);
// Read
unzOpenCurrentFile(pkg);
ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", file));
unzCloseCurrentFile(pkg);
String base_dir = file_path.get_base_dir().trim_suffix("/");
if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) {
base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/");
file = base_dir.path_join(file);
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_CONTINUE(da.is_null());
String output_dir = template_path.path_join(base_dir);
if (!DirAccess::exists(output_dir)) {
Error mkdir_err = da->make_dir_recursive(output_dir);
ERR_CONTINUE(mkdir_err != OK);
}
}
if (p) {
p->step(TTR("Importing:") + " " + file, fc);
}
String to_write = template_path.path_join(file);
Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE);
if (f.is_null()) {
ret = unzGoToNextFile(pkg);<--- ret is assigned
fc++;
ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
}
f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
f.unref(); // close file.
#ifndef WINDOWS_ENABLED
FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
#endif
ret = unzGoToNextFile(pkg);<--- ret is overwritten
fc++;
}
if (p) {
memdelete(p);
}
unzClose(pkg);
_update_template_status();
EditorSettings::get_singleton()->set("_export_template_download_directory", p_file.get_base_dir());
return true;
}
void ExportTemplateManager::_uninstall_template(const String &p_version) {
uninstall_confirm->set_text(vformat(TTR("Remove templates for the version '%s'?"), p_version));
uninstall_confirm->popup_centered();
uninstall_version = p_version;
}
void ExportTemplateManager::_uninstall_template_confirmed() {
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
Error err = da->change_dir(templates_dir);
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
err = da->change_dir(uninstall_version);
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.path_join(uninstall_version) + "'.");
err = da->erase_contents_recursive();
ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.path_join(uninstall_version) + "'.");
da->change_dir("..");
err = da->remove(uninstall_version);
ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.path_join(uninstall_version) + "'.");
_update_template_status();
}
String ExportTemplateManager::_get_selected_mirror() const {
if (mirrors_list->get_item_count() == 1) {
return "";
}
int selected = mirrors_list->get_selected_id();
if (selected == 0) {
// This is a special "best available" value; so pick the first available mirror from the rest of the list.
selected = 1;
}
return mirrors_list->get_item_metadata(selected);
}
void ExportTemplateManager::_mirror_options_button_cbk(int p_id) {
switch (p_id) {
case VISIT_WEB_MIRROR: {
String mirror_url = _get_selected_mirror();
if (mirror_url.is_empty()) {
EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
return;
}
OS::get_singleton()->shell_open(mirror_url);
} break;
case COPY_MIRROR_URL: {
String mirror_url = _get_selected_mirror();
if (mirror_url.is_empty()) {
EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
return;
}
DisplayServer::get_singleton()->clipboard_set(mirror_url);
} break;
}
}
void ExportTemplateManager::_installed_table_button_cbk(Object *p_item, int p_column, int p_id, MouseButton p_button) {
if (p_button != MouseButton::LEFT) {
return;
}
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
if (!ti) {
return;
}
switch (p_id) {
case OPEN_TEMPLATE_FOLDER: {
String version_string = ti->get_text(0);
_open_template_folder(version_string);
} break;
case UNINSTALL_TEMPLATE: {
String version_string = ti->get_text(0);
_uninstall_template(version_string);
} break;
}
}
void ExportTemplateManager::_open_template_folder(const String &p_version) {
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
OS::get_singleton()->shell_show_in_file_manager(templates_dir.path_join(p_version), true);
}
void ExportTemplateManager::popup_manager() {
_update_template_status();
switch (_get_downloads_availability()) {
case DOWNLOADS_AVAILABLE: {
current_missing_label->set_text(TTR("Export templates are missing. Download them or install from a file."));
mirrors_list->clear();
mirrors_list->add_item(TTR("Best available mirror"), 0);
mirrors_list->set_disabled(false);
mirrors_list->set_tooltip_text("");
mirror_options_button->set_disabled(false);
download_current_button->set_disabled(false);
download_current_button->set_tooltip_text("");
if (!is_downloading_templates) {
_refresh_mirrors();
}
} break;
case DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE: {
current_missing_label->set_text(TTR("Export templates are missing. Install them from a file."));
mirrors_list->clear();
mirrors_list->add_item(TTR("Not available in offline mode"), 0);
mirrors_list->set_disabled(true);
mirrors_list->set_tooltip_text(TTR("Template downloading is disabled in offline mode."));
mirror_options_button->set_disabled(true);
download_current_button->set_disabled(true);
download_current_button->set_tooltip_text(TTR("Template downloading is disabled in offline mode."));
} break;
case DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS: {
current_missing_label->set_text(TTR("Export templates are missing. Install them from a file."));
mirrors_list->clear();
mirrors_list->add_item(TTR("No templates for development builds"), 0);
mirrors_list->set_disabled(true);
mirrors_list->set_tooltip_text(TTR("Official export templates aren't available for development builds."));
mirror_options_button->set_disabled(true);
download_current_button->set_disabled(true);
download_current_button->set_tooltip_text(TTR("Official export templates aren't available for development builds."));
} break;
}
popup_centered(Size2(720, 280) * EDSCALE);
}
void ExportTemplateManager::ok_pressed() {
if (!is_downloading_templates) {
hide();
return;
}
hide_dialog_accept->popup_centered();
}
void ExportTemplateManager::_hide_dialog() {
hide();
}
String ExportTemplateManager::get_android_build_directory(const Ref<EditorExportPreset> &p_preset) {
if (p_preset.is_valid()) {
String gradle_build_dir = p_preset->get("gradle_build/gradle_build_directory");
if (!gradle_build_dir.is_empty()) {
return gradle_build_dir.path_join("build");
}
}
return "res://android/build";
}
String ExportTemplateManager::get_android_source_zip(const Ref<EditorExportPreset> &p_preset) {
if (p_preset.is_valid()) {
String android_source_zip = p_preset->get("gradle_build/android_source_template");
if (!android_source_zip.is_empty()) {
return android_source_zip;
}
}
const String templates_dir = EditorPaths::get_singleton()->get_export_templates_dir().path_join(VERSION_FULL_CONFIG);
return templates_dir.path_join("android_source.zip");
}
String ExportTemplateManager::get_android_template_identifier(const Ref<EditorExportPreset> &p_preset) {
// The template identifier is the Godot version for the default template, and the full path plus md5 hash for custom templates.
if (p_preset.is_valid()) {
String android_source_zip = p_preset->get("gradle_build/android_source_template");
if (!android_source_zip.is_empty()) {
return android_source_zip + String(" [") + FileAccess::get_md5(android_source_zip) + String("]");
}
}
return VERSION_FULL_CONFIG;
}
bool ExportTemplateManager::is_android_template_installed(const Ref<EditorExportPreset> &p_preset) {
return DirAccess::exists(get_android_build_directory(p_preset));
}
bool ExportTemplateManager::can_install_android_template(const Ref<EditorExportPreset> &p_preset) {
return FileAccess::exists(get_android_source_zip(p_preset));
}
Error ExportTemplateManager::install_android_template(const Ref<EditorExportPreset> &p_preset) {
const String source_zip = get_android_source_zip(p_preset);
ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
return install_android_template_from_file(source_zip, p_preset);
}
Error ExportTemplateManager::install_android_template_from_file(const String &p_file, const Ref<EditorExportPreset> &p_preset) {
// To support custom Android builds, we install the Java source code and buildsystem
// from android_source.zip to the project's res://android folder.
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
ERR_FAIL_COND_V(da.is_null(), ERR_CANT_CREATE);
String build_dir = get_android_build_directory(p_preset);
String parent_dir = build_dir.get_base_dir();
// Make parent of the build dir (if it does not exist).
da->make_dir_recursive(parent_dir);
{
// Add identifier, to ensure building won't work if the current template doesn't match.
Ref<FileAccess> f = FileAccess::open(parent_dir.path_join(".build_version"), FileAccess::WRITE);
ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE);
f->store_line(get_android_template_identifier(p_preset));
}
// Create the android build directory.
Error err = da->make_dir_recursive(build_dir);
ERR_FAIL_COND_V(err != OK, err);
{
// Add an empty .gdignore file to avoid scan.
Ref<FileAccess> f = FileAccess::open(build_dir.path_join(".gdignore"), FileAccess::WRITE);
ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE);
f->store_line("");
}
// Uncompress source template.
Ref<FileAccess> io_fa;
zlib_filefunc_def io = zipio_create_io(&io_fa);
unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
ERR_FAIL_NULL_V_MSG(pkg, ERR_CANT_OPEN, "Android sources not in ZIP format.");
int ret = unzGoToFirstFile(pkg);
int total_files = 0;
// Count files to unzip.
while (ret == UNZ_OK) {
total_files++;
ret = unzGoToNextFile(pkg);
}
ret = unzGoToFirstFile(pkg);
ProgressDialog::get_singleton()->add_task("uncompress_src", TTR("Uncompressing Android Build Sources"), total_files);
HashSet<String> dirs_tested;
int idx = 0;
while (ret == UNZ_OK) {
// Get file path.
unz_file_info info;
char fpath[16384];
ret = unzGetCurrentFileInfo(pkg, &info, fpath, 16384, nullptr, 0, nullptr, 0);
if (ret != UNZ_OK) {
break;
}
String path = String::utf8(fpath);
String base_dir = path.get_base_dir();
if (!path.ends_with("/")) {
Vector<uint8_t> uncomp_data;
uncomp_data.resize(info.uncompressed_size);
// Read.
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
unzCloseCurrentFile(pkg);
if (!dirs_tested.has(base_dir)) {
da->make_dir_recursive(build_dir.path_join(base_dir));
dirs_tested.insert(base_dir);
}
String to_write = build_dir.path_join(path);
Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE);
if (f.is_valid()) {
f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
f.unref(); // close file.
#ifndef WINDOWS_ENABLED
FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
#endif
} else {
ERR_PRINT("Can't uncompress file: " + to_write);
}
}
ProgressDialog::get_singleton()->task_step("uncompress_src", path, idx);
idx++;
ret = unzGoToNextFile(pkg);
}
ProgressDialog::get_singleton()->end_task("uncompress_src");
unzClose(pkg);
EditorFileSystem::get_singleton()->scan_changes();
return OK;
}
void ExportTemplateManager::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
current_value->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("main"), EditorStringName(EditorFonts)));
current_missing_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
current_installed_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
mirror_options_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
if (!is_visible()) {
set_process(false);
} else if (is_visible() && is_downloading_templates) {
set_process(true);
}
} break;
case NOTIFICATION_PROCESS: {
update_countdown -= get_process_delta_time();
if (update_countdown > 0) {
return;
}
update_countdown = 0.5;
String status;
int downloaded_bytes;
int total_bytes;
bool success = _humanize_http_status(download_templates, &status, &downloaded_bytes, &total_bytes);
if (downloaded_bytes >= 0) {
if (total_bytes > 0) {
_set_current_progress_value(float(downloaded_bytes) / total_bytes, status);
} else {
_set_current_progress_value(0, status);
}
} else {
_set_current_progress_status(status);
}
if (!success) {
set_process(false);
}
} break;
case NOTIFICATION_WM_CLOSE_REQUEST: {
// This won't stop the window from closing, but will show the alert if the download is active.
ok_pressed();
} break;
}
}
ExportTemplateManager::ExportTemplateManager() {
set_title(TTR("Export Template Manager"));
set_hide_on_ok(false);
set_ok_button_text(TTR("Close"));
VBoxContainer *main_vb = memnew(VBoxContainer);
add_child(main_vb);
// Current version controls.
HBoxContainer *current_hb = memnew(HBoxContainer);
main_vb->add_child(current_hb);
Label *current_label = memnew(Label);
current_label->set_theme_type_variation("HeaderSmall");
current_label->set_text(TTR("Current Version:"));
current_hb->add_child(current_label);
current_value = memnew(Label);
current_hb->add_child(current_value);
// Current version statuses.
// Status: Current version is missing.
current_missing_label = memnew(Label);
current_missing_label->set_theme_type_variation("HeaderSmall");
current_missing_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
current_missing_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
current_hb->add_child(current_missing_label);
// Status: Current version is installed.
current_installed_label = memnew(Label);
current_installed_label->set_theme_type_variation("HeaderSmall");
current_installed_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
current_installed_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
current_installed_label->set_text(TTR("Export templates are installed and ready to be used."));
current_hb->add_child(current_installed_label);
current_installed_label->hide();
// Currently installed template.
current_installed_hb = memnew(HBoxContainer);
main_vb->add_child(current_installed_hb);
current_installed_path = memnew(LineEdit);
current_installed_path->set_editable(false);
current_installed_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
current_installed_hb->add_child(current_installed_path);
#ifndef ANDROID_ENABLED
Button *current_open_button = memnew(Button);
current_open_button->set_text(TTR("Open Folder"));
current_open_button->set_tooltip_text(TTR("Open the folder containing installed templates for the current version."));
current_installed_hb->add_child(current_open_button);
current_open_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_open_template_folder).bind(VERSION_FULL_CONFIG));
#endif
current_uninstall_button = memnew(Button);
current_uninstall_button->set_text(TTR("Uninstall"));
current_uninstall_button->set_tooltip_text(TTR("Uninstall templates for the current version."));
current_installed_hb->add_child(current_uninstall_button);
current_uninstall_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_uninstall_template).bind(VERSION_FULL_CONFIG));
main_vb->add_child(memnew(HSeparator));
// Download and install section.
HBoxContainer *install_templates_hb = memnew(HBoxContainer);
main_vb->add_child(install_templates_hb);
// Download and install buttons are available.
install_options_vb = memnew(VBoxContainer);
install_options_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
install_templates_hb->add_child(install_options_vb);
HBoxContainer *download_install_hb = memnew(HBoxContainer);
install_options_vb->add_child(download_install_hb);
Label *mirrors_label = memnew(Label);
mirrors_label->set_text(TTR("Download from:"));
download_install_hb->add_child(mirrors_label);
mirrors_list = memnew(OptionButton);
mirrors_list->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
download_install_hb->add_child(mirrors_list);
request_mirrors = memnew(HTTPRequest);
mirrors_list->add_child(request_mirrors);
request_mirrors->connect("request_completed", callable_mp(this, &ExportTemplateManager::_refresh_mirrors_completed));
mirror_options_button = memnew(MenuButton);
mirror_options_button->get_popup()->add_item(TTR("Open in Web Browser"), VISIT_WEB_MIRROR);
mirror_options_button->get_popup()->add_item(TTR("Copy Mirror URL"), COPY_MIRROR_URL);
download_install_hb->add_child(mirror_options_button);
mirror_options_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ExportTemplateManager::_mirror_options_button_cbk));
download_install_hb->add_spacer();
download_current_button = memnew(Button);
download_current_button->set_text(TTR("Download and Install"));
download_current_button->set_tooltip_text(TTR("Download and install templates for the current version from the best possible mirror."));
download_install_hb->add_child(download_current_button);
download_current_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_download_current));
HBoxContainer *install_file_hb = memnew(HBoxContainer);
install_file_hb->set_alignment(BoxContainer::ALIGNMENT_END);
install_options_vb->add_child(install_file_hb);
install_file_button = memnew(Button);
install_file_button->set_text(TTR("Install from File"));
install_file_button->set_tooltip_text(TTR("Install templates from a local file."));
install_file_hb->add_child(install_file_button);
install_file_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_install_file));
// Templates are being downloaded; buttons unavailable.
download_progress_hb = memnew(HBoxContainer);
download_progress_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
install_templates_hb->add_child(download_progress_hb);
download_progress_hb->hide();
download_progress_bar = memnew(ProgressBar);
download_progress_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
download_progress_bar->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
download_progress_bar->set_min(0);
download_progress_bar->set_max(1);
download_progress_bar->set_value(0);
download_progress_bar->set_step(0.01);
download_progress_bar->set_editor_preview_indeterminate(true);
download_progress_hb->add_child(download_progress_bar);
download_progress_label = memnew(Label);
download_progress_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
download_progress_hb->add_child(download_progress_label);
Button *download_cancel_button = memnew(Button);
download_cancel_button->set_text(TTR("Cancel"));
download_cancel_button->set_tooltip_text(TTR("Cancel the download of the templates."));
download_progress_hb->add_child(download_cancel_button);
download_cancel_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_cancel_template_download));
download_templates = memnew(HTTPRequest);
install_templates_hb->add_child(download_templates);
download_templates->connect("request_completed", callable_mp(this, &ExportTemplateManager::_download_template_completed));
main_vb->add_child(memnew(HSeparator));
// Other installed templates table.
HBoxContainer *installed_versions_hb = memnew(HBoxContainer);
main_vb->add_child(installed_versions_hb);
Label *installed_label = memnew(Label);
installed_label->set_theme_type_variation("HeaderSmall");
installed_label->set_text(TTR("Other Installed Versions:"));
installed_versions_hb->add_child(installed_label);
installed_table = memnew(Tree);
installed_table->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
installed_table->set_hide_root(true);
installed_table->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
installed_table->set_v_size_flags(Control::SIZE_EXPAND_FILL);
main_vb->add_child(installed_table);
installed_table->connect("button_clicked", callable_mp(this, &ExportTemplateManager::_installed_table_button_cbk));
// Dialogs.
uninstall_confirm = memnew(ConfirmationDialog);
uninstall_confirm->set_title(TTR("Uninstall Template"));
add_child(uninstall_confirm);
uninstall_confirm->connect(SceneStringName(confirmed), callable_mp(this, &ExportTemplateManager::_uninstall_template_confirmed));
install_file_dialog = memnew(FileDialog);
install_file_dialog->set_title(TTR("Select Template File"));
install_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
install_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
install_file_dialog->set_current_dir(EDITOR_DEF("_export_template_download_directory", ""));
install_file_dialog->add_filter("*.tpz", TTR("Godot Export Templates"));
install_file_dialog->connect("file_selected", callable_mp(this, &ExportTemplateManager::_install_file_selected).bind(false));
add_child(install_file_dialog);
hide_dialog_accept = memnew(AcceptDialog);
hide_dialog_accept->set_text(TTR("The templates will continue to download.\nYou may experience a short editor freeze when they finish."));
add_child(hide_dialog_accept);
hide_dialog_accept->connect(SceneStringName(confirmed), callable_mp(this, &ExportTemplateManager::_hide_dialog));
}
|