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
|
/*************************************************************************/
/* export_template_manager.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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/input/input.h"
#include "core/io/dir_access.h"
#include "core/io/json.h"
#include "core/io/zip_io.h"
#include "core/os/keyboard.h"
#include "core/version.h"
#include "editor_node.h"
#include "editor_scale.h"
#include "progress_dialog.h"
#include "scene/gui/link_button.h"
void ExportTemplateManager::_update_template_status() {
// Fetch installed templates from the file system.
DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
Error err = da->change_dir(templates_dir);
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
Set<String> templates;
da->list_dir_begin();
if (err == OK) {
String c = da->get_next();
while (c != String()) {
if (da->current_is_dir() && !c.begins_with(".")) {
templates.insert(c);
}
c = da->get_next();
}
}
da->list_dir_end();
memdelete(da);
// 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.plus_file(current_version));
}
}
// Update the list of other installed versions.
installed_table->clear();
TreeItem *installed_root = installed_table->create_item();
for (Set<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);
ti->add_button(0, get_theme_icon("Folder", "EditorIcons"), OPEN_TEMPLATE_FOLDER, false, TTR("Open the folder containing these templates."));
ti->add_button(0, get_theme_icon("Remove", "EditorIcons"), 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 (!mirrors_available && !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();
_set_current_progress_status(TTR("Starting the download..."));
download_templates->set_download_file(EditorPaths::get_singleton()->get_cache_dir().plus_file("tmp_templates.tpz"));
download_templates->set_use_threads(true);
Error err = download_templates->request(p_url);
if (err != OK) {
_set_current_progress_status(TTR("Error requesting URL:") + " " + p_url, true);
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_SSL_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.
DirAccessRef 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 data = json.get_data();
if (data.has("mirrors")) {
Array mirrors = 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_SSL_HANDSHAKE_ERROR:
*r_status = TTR("SSL Handshake Error");
success = false;
break;
}
return success;
}
void ExportTemplateManager::_set_current_progress_status(const String &p_status, bool p_error) {
download_progress_bar->hide();
download_progress_label->set_text(p_status);
if (p_error) {
download_progress_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
} else {
download_progress_label->add_theme_color_override("font_color", get_theme_color("font_color", "Label"));
}
}
void ExportTemplateManager::_set_current_progress_value(float p_value, const String &p_status) {
download_progress_bar->show();
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) {
// unzClose() will take care of closing the file stored in the unzFile,
// so we don't need to `memdelete(fa)` in this method.
FileAccess *fa = nullptr;
zlib_filefunc_def io = zipio_create_io_from_file(&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);
String file = fname;
if (file.ends_with("version.txt")) {
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
// Read.
unzOpenCurrentFile(pkg);
ret = unzReadCurrentFile(pkg, data.ptrw(), data.size());
unzCloseCurrentFile(pkg);
String data_str;
data_str.parse_utf8((const char *)data.ptr(), 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 == String()) {
EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside the export templates file."));
unzClose(pkg);
return false;
}
DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(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];
unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
String file_path(String(fname).simplify_path());
String file = file_path.get_file();
if (file.size() == 0) {
ret = unzGoToNextFile(pkg);
continue;
}
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
// Read
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg, data.ptrw(), data.size());
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.plus_file(file);
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_CONTINUE(!da);
String output_dir = template_path.plus_file(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.plus_file(file);
FileAccessRef f = FileAccess::open(to_write, FileAccess::WRITE);
if (!f) {
ret = unzGoToNextFile(pkg);
fc++;
ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
}
f->store_buffer(data.ptr(), data.size());
#ifndef WINDOWS_ENABLED
FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
#endif
ret = unzGoToNextFile(pkg);
fc++;
}
if (p) {
memdelete(p);
}
unzClose(pkg);
_update_template_status();
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() {
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
const String &templates_dir = EditorSettings::get_singleton()->get_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.plus_file(uninstall_version) + "'.");
err = da->erase_contents_recursive();
ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.plus_file(uninstall_version) + "'.");
da->change_dir("..");
err = da->remove(uninstall_version);
ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.plus_file(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) {
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 = EditorSettings::get_singleton()->get_templates_dir();
OS::get_singleton()->shell_open("file://" + templates_dir.plus_file(p_version));
}
void ExportTemplateManager::popup_manager() {
_update_template_status();
_refresh_mirrors();
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();
}
bool ExportTemplateManager::can_install_android_template() {
const String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
return FileAccess::exists(templates_dir.plus_file("android_source.zip"));
}
Error ExportTemplateManager::install_android_template() {
const String &templates_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
const String &source_zip = templates_path.plus_file("android_source.zip");
ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
return install_android_template_from_file(source_zip);
}
Error ExportTemplateManager::install_android_template_from_file(const String &p_file) {
// To support custom Android builds, we install the Java source code and buildsystem
// from android_source.zip to the project's res://android folder.
DirAccessRef da = DirAccess::open("res://");
ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
// Make res://android dir (if it does not exist).
da->make_dir("android");
{
// Add version, to ensure building won't work if template and Godot version don't match.
FileAccessRef f = FileAccess::open("res://android/.build_version", FileAccess::WRITE);
ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
f->store_line(VERSION_FULL_CONFIG);
f->close();
}
// Create the android plugins directory.
Error err = da->make_dir_recursive("android/plugins");
ERR_FAIL_COND_V(err != OK, err);
err = da->make_dir_recursive("android/build");
ERR_FAIL_COND_V(err != OK, err);
{
// Add an empty .gdignore file to avoid scan.
FileAccessRef f = FileAccess::open("res://android/build/.gdignore", FileAccess::WRITE);
ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
f->store_line("");
f->close();
}
// Uncompress source template.
FileAccess *src_f = nullptr;
zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
ERR_FAIL_COND_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);
Set<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);
String path = fpath;
String base_dir = path.get_base_dir();
if (!path.ends_with("/")) {
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
// Read.
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg, data.ptrw(), data.size());
unzCloseCurrentFile(pkg);
if (!dirs_tested.has(base_dir)) {
da->make_dir_recursive(String("android/build").plus_file(base_dir));
dirs_tested.insert(base_dir);
}
String to_write = String("res://android/build").plus_file(path);
FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
if (f) {
f->store_buffer(data.ptr(), data.size());
memdelete(f);
#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);
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("font", get_theme_font("main", "EditorFonts"));
current_missing_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
current_installed_label->add_theme_color_override("font_color", get_theme_color("disabled_font_color", "Editor"));
mirror_options_button->set_icon(get_theme_icon("GuiTabMenu", "EditorIcons"));
} 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;
}
}
void ExportTemplateManager::_bind_methods() {
}
ExportTemplateManager::ExportTemplateManager() {
set_title(TTR("Export Template Manager"));
set_hide_on_ok(false);
get_ok_button()->set_text(TTR("Close"));
// 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).
downloads_available =
String(VERSION_STATUS) != String("dev") &&
String(VERSION_STATUS) != String("alpha") &&
String(VERSION_STATUS) != String("beta") &&
String(VERSION_STATUS) != String("rc");
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_align(Label::ALIGN_RIGHT);
current_missing_label->set_text(TTR("Export templates are missing. Download them or install from a file."));
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_align(Label::ALIGN_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);
current_open_button = memnew(Button);
current_open_button->set_text(TTR("Open Folder"));
current_open_button->set_tooltip(TTR("Open the folder containing installed templates for the current version."));
current_installed_hb->add_child(current_open_button);
current_open_button->connect("pressed", callable_mp(this, &ExportTemplateManager::_open_template_folder), varray(VERSION_FULL_CONFIG));
current_uninstall_button = memnew(Button);
current_uninstall_button->set_text(TTR("Uninstall"));
current_uninstall_button->set_tooltip(TTR("Uninstall templates for the current version."));
current_installed_hb->add_child(current_uninstall_button);
current_uninstall_button->connect("pressed", callable_mp(this, &ExportTemplateManager::_uninstall_template), varray(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);
mirrors_list->add_item(TTR("Best available mirror"), 0);
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("Open in Web Browser", VISIT_WEB_MIRROR);
mirror_options_button->get_popup()->add_item("Copy Mirror URL", COPY_MIRROR_URL);
download_install_hb->add_child(mirror_options_button);
mirror_options_button->get_popup()->connect("id_pressed", callable_mp(this, &ExportTemplateManager::_mirror_options_button_cbk));
download_install_hb->add_spacer();
Button *download_current_button = memnew(Button);
download_current_button->set_text(TTR("Download and Install"));
download_current_button->set_tooltip(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("pressed", callable_mp(this, &ExportTemplateManager::_download_current));
// Update downloads buttons to prevent unsupported downloads.
if (!downloads_available) {
download_current_button->set_disabled(true);
download_current_button->set_tooltip(TTR("Official export templates aren't available for development builds."));
}
HBoxContainer *install_file_hb = memnew(HBoxContainer);
install_file_hb->set_alignment(BoxContainer::ALIGN_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(TTR("Install templates from a local file."));
install_file_hb->add_child(install_file_button);
install_file_button->connect("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_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(TTR("Cancel the download of the templates."));
download_progress_hb->add_child(download_cancel_button);
download_cancel_button->connect("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_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_pressed", 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("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->add_filter("*.tpz ; " + TTR("Godot Export Templates"));
install_file_dialog->connect("file_selected", callable_mp(this, &ExportTemplateManager::_install_file_selected), varray(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("confirmed", callable_mp(this, &ExportTemplateManager::_hide_dialog));
}
|