Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/export_template_manager.cpp
9903 views
1
/**************************************************************************/
2
/* export_template_manager.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "export_template_manager.h"
32
33
#include "core/io/dir_access.h"
34
#include "core/io/json.h"
35
#include "core/io/zip_io.h"
36
#include "core/version.h"
37
#include "editor/editor_node.h"
38
#include "editor/editor_string_names.h"
39
#include "editor/export/editor_export_preset.h"
40
#include "editor/file_system/editor_file_system.h"
41
#include "editor/file_system/editor_paths.h"
42
#include "editor/gui/progress_dialog.h"
43
#include "editor/settings/editor_settings.h"
44
#include "editor/themes/editor_scale.h"
45
#include "scene/gui/file_dialog.h"
46
#include "scene/gui/line_edit.h"
47
#include "scene/gui/link_button.h"
48
#include "scene/gui/menu_button.h"
49
#include "scene/gui/option_button.h"
50
#include "scene/gui/separator.h"
51
#include "scene/gui/tree.h"
52
#include "scene/main/http_request.h"
53
54
enum DownloadsAvailability {
55
DOWNLOADS_AVAILABLE,
56
DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE,
57
DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS,
58
DOWNLOADS_NOT_AVAILABLE_FOR_DOUBLE_BUILDS,
59
};
60
61
static DownloadsAvailability _get_downloads_availability() {
62
const int network_mode = EDITOR_GET("network/connection/network_mode");
63
64
// Downloadable export templates are only available for stable and official alpha/beta/RC builds
65
// (which always have a number following their status, e.g. "alpha1").
66
// Therefore, don't display download-related features when using a development version
67
// (whose builds aren't numbered).
68
if (String(GODOT_VERSION_STATUS) == String("dev") ||
69
String(GODOT_VERSION_STATUS) == String("alpha") ||
70
String(GODOT_VERSION_STATUS) == String("beta") ||
71
String(GODOT_VERSION_STATUS) == String("rc")) {
72
return DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS;
73
}
74
75
#ifdef REAL_T_IS_DOUBLE
76
return DOWNLOADS_NOT_AVAILABLE_FOR_DOUBLE_BUILDS;
77
#endif
78
79
if (network_mode == EditorSettings::NETWORK_OFFLINE) {
80
return DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE;
81
}
82
83
return DOWNLOADS_AVAILABLE;
84
}
85
86
void ExportTemplateManager::_update_template_status() {
87
// Fetch installed templates from the file system.
88
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
89
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
90
91
Error err = da->change_dir(templates_dir);
92
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
93
94
RBSet<String> templates;
95
da->list_dir_begin();
96
if (err == OK) {
97
String c = da->get_next();
98
while (!c.is_empty()) {
99
if (da->current_is_dir() && !c.begins_with(".")) {
100
templates.insert(c);
101
}
102
c = da->get_next();
103
}
104
}
105
da->list_dir_end();
106
107
// Update the state of the current version.
108
String current_version = GODOT_VERSION_FULL_CONFIG;
109
current_value->set_text(current_version);
110
111
if (templates.has(current_version)) {
112
current_missing_label->hide();
113
current_installed_label->show();
114
115
current_installed_hb->show();
116
current_version_exists = true;
117
} else {
118
current_installed_label->hide();
119
current_missing_label->show();
120
121
current_installed_hb->hide();
122
current_version_exists = false;
123
}
124
125
if (is_downloading_templates) {
126
install_options_vb->hide();
127
download_progress_hb->show();
128
} else {
129
download_progress_hb->hide();
130
install_options_vb->show();
131
132
if (templates.has(current_version)) {
133
current_installed_path->set_text(templates_dir.path_join(current_version));
134
}
135
}
136
137
// Update the list of other installed versions.
138
installed_table->clear();
139
TreeItem *installed_root = installed_table->create_item();
140
141
for (RBSet<String>::Element *E = templates.back(); E; E = E->prev()) {
142
String version_string = E->get();
143
if (version_string == current_version) {
144
continue;
145
}
146
147
TreeItem *ti = installed_table->create_item(installed_root);
148
ti->set_text(0, version_string);
149
150
#ifndef ANDROID_ENABLED
151
ti->add_button(0, get_editor_theme_icon(SNAME("Folder")), OPEN_TEMPLATE_FOLDER, false, TTR("Open the folder containing these templates."));
152
#endif
153
ti->add_button(0, get_editor_theme_icon(SNAME("Remove")), UNINSTALL_TEMPLATE, false, TTR("Uninstall these templates."));
154
}
155
}
156
157
void ExportTemplateManager::_download_current() {
158
if (is_downloading_templates) {
159
return;
160
}
161
is_downloading_templates = true;
162
163
install_options_vb->hide();
164
download_progress_hb->show();
165
166
if (mirrors_available) {
167
String mirror_url = _get_selected_mirror();
168
if (mirror_url.is_empty()) {
169
_set_current_progress_status(TTR("There are no mirrors available."), true);
170
return;
171
}
172
173
_download_template(mirror_url, true);
174
} else if (!is_refreshing_mirrors) {
175
_set_current_progress_status(TTR("Retrieving the mirror list..."));
176
_refresh_mirrors();
177
}
178
}
179
180
void ExportTemplateManager::_download_template(const String &p_url, bool p_skip_check) {
181
if (!p_skip_check && is_downloading_templates) {
182
return;
183
}
184
is_downloading_templates = true;
185
186
install_options_vb->hide();
187
download_progress_hb->show();
188
download_progress_bar->show();
189
download_progress_bar->set_indeterminate(true);
190
191
_set_current_progress_status(TTR("Starting the download..."));
192
193
download_templates->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_templates.tpz"));
194
download_templates->set_use_threads(true);
195
196
const String proxy_host = EDITOR_GET("network/http_proxy/host");
197
const int proxy_port = EDITOR_GET("network/http_proxy/port");
198
download_templates->set_http_proxy(proxy_host, proxy_port);
199
download_templates->set_https_proxy(proxy_host, proxy_port);
200
201
Error err = download_templates->request(p_url);
202
if (err != OK) {
203
_set_current_progress_status(TTR("Error requesting URL:") + " " + p_url, true);
204
download_progress_hb->hide();
205
return;
206
}
207
208
set_process(true);
209
_set_current_progress_status(TTR("Connecting to the mirror..."));
210
}
211
212
void ExportTemplateManager::_download_template_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
213
switch (p_status) {
214
case HTTPRequest::RESULT_CANT_RESOLVE: {
215
_set_current_progress_status(TTR("Can't resolve the requested address."), true);
216
} break;
217
case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
218
case HTTPRequest::RESULT_CONNECTION_ERROR:
219
case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
220
case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR:
221
case HTTPRequest::RESULT_CANT_CONNECT: {
222
_set_current_progress_status(TTR("Can't connect to the mirror."), true);
223
} break;
224
case HTTPRequest::RESULT_NO_RESPONSE: {
225
_set_current_progress_status(TTR("No response from the mirror."), true);
226
} break;
227
case HTTPRequest::RESULT_REQUEST_FAILED: {
228
_set_current_progress_status(TTR("Request failed."), true);
229
} break;
230
case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
231
_set_current_progress_status(TTR("Request ended up in a redirect loop."), true);
232
} break;
233
default: {
234
if (p_code != 200) {
235
_set_current_progress_status(TTR("Request failed:") + " " + itos(p_code), true);
236
} else {
237
_set_current_progress_status(TTR("Download complete; extracting templates..."));
238
String path = download_templates->get_download_file();
239
240
is_downloading_templates = false;
241
bool ret = _install_file_selected(path, true);
242
if (ret) {
243
// Clean up downloaded file.
244
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
245
Error err = da->remove(path);
246
if (err != OK) {
247
EditorNode::get_singleton()->add_io_error(TTR("Cannot remove temporary file:") + "\n" + path + "\n");
248
}
249
} else {
250
EditorNode::get_singleton()->add_io_error(vformat(TTR("Templates installation failed.\nThe problematic templates archives can be found at '%s'."), path));
251
}
252
}
253
} break;
254
}
255
256
set_process(false);
257
}
258
259
void ExportTemplateManager::_cancel_template_download() {
260
if (!is_downloading_templates) {
261
return;
262
}
263
264
download_templates->cancel_request();
265
download_progress_hb->hide();
266
install_options_vb->show();
267
is_downloading_templates = false;
268
}
269
270
void ExportTemplateManager::_refresh_mirrors() {
271
if (is_refreshing_mirrors) {
272
return;
273
}
274
is_refreshing_mirrors = true;
275
276
String current_version = GODOT_VERSION_FULL_CONFIG;
277
const String mirrors_metadata_url = "https://godotengine.org/mirrorlist/" + current_version + ".json";
278
request_mirrors->request(mirrors_metadata_url);
279
}
280
281
void ExportTemplateManager::_refresh_mirrors_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
282
if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
283
EditorNode::get_singleton()->show_warning(TTR("Error getting the list of mirrors."));
284
is_refreshing_mirrors = false;
285
if (is_downloading_templates) {
286
_cancel_template_download();
287
}
288
return;
289
}
290
291
String response_json = String::utf8((const char *)p_data.ptr(), p_data.size());
292
293
JSON json;
294
Error err = json.parse(response_json);
295
if (err != OK) {
296
EditorNode::get_singleton()->show_warning(TTR("Error parsing JSON with the list of mirrors. Please report this issue!"));
297
is_refreshing_mirrors = false;
298
if (is_downloading_templates) {
299
_cancel_template_download();
300
}
301
return;
302
}
303
304
mirrors_list->clear();
305
mirrors_list->add_item(TTR("Best available mirror"), 0);
306
307
mirrors_available = false;
308
309
Dictionary mirror_data = json.get_data();
310
if (mirror_data.has("mirrors")) {
311
Array mirrors = mirror_data["mirrors"];
312
313
for (int i = 0; i < mirrors.size(); i++) {
314
Dictionary m = mirrors[i];
315
ERR_CONTINUE(!m.has("url") || !m.has("name"));
316
317
mirrors_list->add_item(m["name"]);
318
mirrors_list->set_item_metadata(i + 1, m["url"]);
319
320
mirrors_available = true;
321
}
322
}
323
if (!mirrors_available) {
324
EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
325
if (is_downloading_templates) {
326
_cancel_template_download();
327
}
328
}
329
330
is_refreshing_mirrors = false;
331
332
if (is_downloading_templates) {
333
String mirror_url = _get_selected_mirror();
334
if (mirror_url.is_empty()) {
335
_set_current_progress_status(TTR("There are no mirrors available."), true);
336
return;
337
}
338
339
_download_template(mirror_url, true);
340
}
341
}
342
343
void ExportTemplateManager::_force_online_mode() {
344
EditorSettings::get_singleton()->set_setting("network/connection/network_mode", EditorSettings::NETWORK_ONLINE);
345
EditorSettings::get_singleton()->notify_changes();
346
EditorSettings::get_singleton()->save();
347
348
popup_manager();
349
}
350
351
bool ExportTemplateManager::_humanize_http_status(HTTPRequest *p_request, String *r_status, int *r_downloaded_bytes, int *r_total_bytes) {
352
*r_status = "";
353
*r_downloaded_bytes = -1;
354
*r_total_bytes = -1;
355
bool success = true;
356
357
switch (p_request->get_http_client_status()) {
358
case HTTPClient::STATUS_DISCONNECTED:
359
*r_status = TTR("Disconnected");
360
success = false;
361
break;
362
case HTTPClient::STATUS_RESOLVING:
363
*r_status = TTR("Resolving");
364
break;
365
case HTTPClient::STATUS_CANT_RESOLVE:
366
*r_status = TTR("Can't Resolve");
367
success = false;
368
break;
369
case HTTPClient::STATUS_CONNECTING:
370
*r_status = TTR("Connecting...");
371
break;
372
case HTTPClient::STATUS_CANT_CONNECT:
373
*r_status = TTR("Can't Connect");
374
success = false;
375
break;
376
case HTTPClient::STATUS_CONNECTED:
377
*r_status = TTR("Connected");
378
break;
379
case HTTPClient::STATUS_REQUESTING:
380
*r_status = TTR("Requesting...");
381
break;
382
case HTTPClient::STATUS_BODY:
383
*r_status = TTR("Downloading");
384
*r_downloaded_bytes = p_request->get_downloaded_bytes();
385
*r_total_bytes = p_request->get_body_size();
386
387
if (p_request->get_body_size() > 0) {
388
*r_status += " " + String::humanize_size(p_request->get_downloaded_bytes()) + "/" + String::humanize_size(p_request->get_body_size());
389
} else {
390
*r_status += " " + String::humanize_size(p_request->get_downloaded_bytes());
391
}
392
break;
393
case HTTPClient::STATUS_CONNECTION_ERROR:
394
*r_status = TTR("Connection Error");
395
success = false;
396
break;
397
case HTTPClient::STATUS_TLS_HANDSHAKE_ERROR:
398
*r_status = TTR("TLS Handshake Error");
399
success = false;
400
break;
401
}
402
403
return success;
404
}
405
406
void ExportTemplateManager::_set_current_progress_status(const String &p_status, bool p_error) {
407
download_progress_label->set_text(p_status);
408
409
if (p_error) {
410
download_progress_bar->hide();
411
download_progress_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
412
} else {
413
download_progress_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), SNAME("Label")));
414
}
415
}
416
417
void ExportTemplateManager::_set_current_progress_value(float p_value, const String &p_status) {
418
download_progress_bar->show();
419
download_progress_bar->set_indeterminate(false);
420
download_progress_bar->set_value(p_value);
421
download_progress_label->set_text(p_status);
422
}
423
424
void ExportTemplateManager::_install_file() {
425
install_file_dialog->popup_file_dialog();
426
}
427
428
bool ExportTemplateManager::_install_file_selected(const String &p_file, bool p_skip_progress) {
429
Ref<FileAccess> io_fa;
430
zlib_filefunc_def io = zipio_create_io(&io_fa);
431
432
unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
433
if (!pkg) {
434
EditorNode::get_singleton()->show_warning(TTR("Can't open the export templates file."));
435
return false;
436
}
437
int ret = unzGoToFirstFile(pkg);
438
439
// Count them and find version.
440
int fc = 0;
441
String version;
442
String contents_dir;
443
444
while (ret == UNZ_OK) {
445
unz_file_info info;
446
char fname[16384];
447
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
448
if (ret != UNZ_OK) {
449
break;
450
}
451
452
String file = String::utf8(fname);
453
454
// Skip the __MACOSX directory created by macOS's built-in file zipper.
455
if (file.begins_with("__MACOSX")) {
456
ret = unzGoToNextFile(pkg);
457
continue;
458
}
459
460
if (file.ends_with("version.txt")) {
461
Vector<uint8_t> uncomp_data;
462
uncomp_data.resize(info.uncompressed_size);
463
464
// Read.
465
unzOpenCurrentFile(pkg);
466
ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
467
ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", file));
468
unzCloseCurrentFile(pkg);
469
470
String data_str = String::utf8((const char *)uncomp_data.ptr(), uncomp_data.size());
471
data_str = data_str.strip_edges();
472
473
// Version number should be of the form major.minor[.patch].status[.module_config]
474
// so it can in theory have 3 or more slices.
475
if (data_str.get_slice_count(".") < 3) {
476
EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside the export templates file: %s."), data_str));
477
unzClose(pkg);
478
return false;
479
}
480
481
version = data_str;
482
contents_dir = file.get_base_dir().trim_suffix("/").trim_suffix("\\");
483
}
484
485
if (file.get_file().size() != 0) {
486
fc++;
487
}
488
489
ret = unzGoToNextFile(pkg);
490
}
491
492
if (version.is_empty()) {
493
EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside the export templates file."));
494
unzClose(pkg);
495
return false;
496
}
497
498
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
499
String template_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(version);
500
Error err = d->make_dir_recursive(template_path);
501
if (err != OK) {
502
EditorNode::get_singleton()->show_warning(TTR("Error creating path for extracting templates:") + "\n" + template_path);
503
unzClose(pkg);
504
return false;
505
}
506
507
EditorProgress *p = nullptr;
508
if (!p_skip_progress) {
509
p = memnew(EditorProgress("ltask", TTR("Extracting Export Templates"), fc));
510
}
511
512
fc = 0;
513
ret = unzGoToFirstFile(pkg);
514
while (ret == UNZ_OK) {
515
// Get filename.
516
unz_file_info info;
517
char fname[16384];
518
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
519
if (ret != UNZ_OK) {
520
break;
521
}
522
523
if (String::utf8(fname).ends_with("/")) {
524
// File is a directory, ignore it.
525
// Directories will be created when extracting each file.
526
ret = unzGoToNextFile(pkg);
527
continue;
528
}
529
530
String file_path(String::utf8(fname).simplify_path());
531
532
String file = file_path.get_file();
533
534
// Skip the __MACOSX directory created by macOS's built-in file zipper.
535
if (file.is_empty() || file.begins_with("__MACOSX")) {
536
ret = unzGoToNextFile(pkg);
537
continue;
538
}
539
540
Vector<uint8_t> uncomp_data;
541
uncomp_data.resize(info.uncompressed_size);
542
543
// Read
544
unzOpenCurrentFile(pkg);
545
ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
546
ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", file));
547
unzCloseCurrentFile(pkg);
548
549
String base_dir = file_path.get_base_dir().trim_suffix("/");
550
551
if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) {
552
base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/");
553
file = base_dir.path_join(file);
554
555
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
556
ERR_CONTINUE(da.is_null());
557
558
String output_dir = template_path.path_join(base_dir);
559
560
if (!DirAccess::exists(output_dir)) {
561
Error mkdir_err = da->make_dir_recursive(output_dir);
562
ERR_CONTINUE(mkdir_err != OK);
563
}
564
}
565
566
if (p) {
567
p->step(TTR("Importing:") + " " + file, fc);
568
}
569
570
String to_write = template_path.path_join(file);
571
Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE);
572
573
if (f.is_null()) {
574
ret = unzGoToNextFile(pkg);
575
fc++;
576
ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
577
}
578
579
f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
580
f.unref(); // close file.
581
#ifndef WINDOWS_ENABLED
582
FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
583
#endif
584
585
ret = unzGoToNextFile(pkg);
586
fc++;
587
}
588
589
if (p) {
590
memdelete(p);
591
}
592
unzClose(pkg);
593
594
_update_template_status();
595
EditorSettings::get_singleton()->set("_export_template_download_directory", p_file.get_base_dir());
596
return true;
597
}
598
599
void ExportTemplateManager::_uninstall_template(const String &p_version) {
600
uninstall_confirm->set_text(vformat(TTR("Remove templates for the version '%s'?"), p_version));
601
uninstall_confirm->popup_centered();
602
uninstall_version = p_version;
603
}
604
605
void ExportTemplateManager::_uninstall_template_confirmed() {
606
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
607
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
608
609
Error err = da->change_dir(templates_dir);
610
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
611
err = da->change_dir(uninstall_version);
612
ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.path_join(uninstall_version) + "'.");
613
614
err = da->erase_contents_recursive();
615
ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.path_join(uninstall_version) + "'.");
616
617
da->change_dir("..");
618
err = da->remove(uninstall_version);
619
ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.path_join(uninstall_version) + "'.");
620
621
_update_template_status();
622
}
623
624
String ExportTemplateManager::_get_selected_mirror() const {
625
if (mirrors_list->get_item_count() == 1) {
626
return "";
627
}
628
629
int selected = mirrors_list->get_selected_id();
630
if (selected == 0) {
631
// This is a special "best available" value; so pick the first available mirror from the rest of the list.
632
selected = 1;
633
}
634
635
return mirrors_list->get_item_metadata(selected);
636
}
637
638
void ExportTemplateManager::_mirror_options_button_cbk(int p_id) {
639
switch (p_id) {
640
case VISIT_WEB_MIRROR: {
641
String mirror_url = _get_selected_mirror();
642
if (mirror_url.is_empty()) {
643
EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
644
return;
645
}
646
647
OS::get_singleton()->shell_open(mirror_url);
648
} break;
649
650
case COPY_MIRROR_URL: {
651
String mirror_url = _get_selected_mirror();
652
if (mirror_url.is_empty()) {
653
EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
654
return;
655
}
656
657
DisplayServer::get_singleton()->clipboard_set(mirror_url);
658
} break;
659
}
660
}
661
662
void ExportTemplateManager::_installed_table_button_cbk(Object *p_item, int p_column, int p_id, MouseButton p_button) {
663
if (p_button != MouseButton::LEFT) {
664
return;
665
}
666
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
667
if (!ti) {
668
return;
669
}
670
671
switch (p_id) {
672
case OPEN_TEMPLATE_FOLDER: {
673
String version_string = ti->get_text(0);
674
_open_template_folder(version_string);
675
} break;
676
677
case UNINSTALL_TEMPLATE: {
678
String version_string = ti->get_text(0);
679
_uninstall_template(version_string);
680
} break;
681
}
682
}
683
684
void ExportTemplateManager::_open_template_folder(const String &p_version) {
685
const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir();
686
OS::get_singleton()->shell_show_in_file_manager(templates_dir.path_join(p_version), true);
687
}
688
689
void ExportTemplateManager::popup_manager() {
690
_update_template_status();
691
692
switch (_get_downloads_availability()) {
693
case DOWNLOADS_AVAILABLE: {
694
current_missing_label->set_text(TTR("Export templates are missing. Download them or install from a file."));
695
696
mirrors_list->clear();
697
mirrors_list->add_item(TTR("Best available mirror"), 0);
698
mirrors_list->set_disabled(false);
699
mirrors_list->set_tooltip_text("");
700
701
mirror_options_button->set_disabled(false);
702
703
download_current_button->set_disabled(false);
704
download_current_button->set_tooltip_text("");
705
706
if (!is_downloading_templates) {
707
_refresh_mirrors();
708
}
709
710
enable_online_hb->hide();
711
} break;
712
713
case DOWNLOADS_NOT_AVAILABLE_IN_OFFLINE_MODE: {
714
current_missing_label->set_text(TTR("Export templates are missing. Install them from a file."));
715
716
mirrors_list->clear();
717
mirrors_list->add_item(TTR("Not available in offline mode"), 0);
718
mirrors_list->set_disabled(true);
719
mirrors_list->set_tooltip_text(TTR("Template downloading is disabled in offline mode."));
720
721
mirror_options_button->set_disabled(true);
722
723
download_current_button->set_disabled(true);
724
download_current_button->set_tooltip_text(TTR("Template downloading is disabled in offline mode."));
725
726
enable_online_hb->show();
727
} break;
728
729
case DOWNLOADS_NOT_AVAILABLE_FOR_DEV_BUILDS: {
730
current_missing_label->set_text(TTR("Export templates are missing. Install them from a file."));
731
732
mirrors_list->clear();
733
mirrors_list->add_item(TTR("No templates for development builds"), 0);
734
mirrors_list->set_disabled(true);
735
mirrors_list->set_tooltip_text(TTR("Official export templates aren't available for development builds."));
736
737
mirror_options_button->set_disabled(true);
738
739
download_current_button->set_disabled(true);
740
download_current_button->set_tooltip_text(TTR("Official export templates aren't available for development builds."));
741
742
enable_online_hb->hide();
743
} break;
744
745
case DOWNLOADS_NOT_AVAILABLE_FOR_DOUBLE_BUILDS: {
746
current_missing_label->set_text(TTR("Export templates are missing. Install them from a file."));
747
748
mirrors_list->clear();
749
mirrors_list->add_item(TTR("No templates for double-precision builds"), 0);
750
mirrors_list->set_disabled(true);
751
mirrors_list->set_tooltip_text(TTR("Official export templates aren't available for double-precision builds."));
752
753
mirror_options_button->set_disabled(true);
754
755
download_current_button->set_disabled(true);
756
download_current_button->set_tooltip_text(TTR("Official export templates aren't available for double-precision builds."));
757
}
758
}
759
760
popup_centered(Size2(720, 280) * EDSCALE);
761
}
762
763
void ExportTemplateManager::ok_pressed() {
764
if (!is_downloading_templates) {
765
hide();
766
return;
767
}
768
769
hide_dialog_accept->popup_centered();
770
}
771
772
void ExportTemplateManager::_hide_dialog() {
773
hide();
774
}
775
776
String ExportTemplateManager::get_android_build_directory(const Ref<EditorExportPreset> &p_preset) {
777
if (p_preset.is_valid()) {
778
String gradle_build_dir = p_preset->get("gradle_build/gradle_build_directory");
779
if (!gradle_build_dir.is_empty()) {
780
return gradle_build_dir.path_join("build");
781
}
782
}
783
return "res://android/build";
784
}
785
786
String ExportTemplateManager::get_android_source_zip(const Ref<EditorExportPreset> &p_preset) {
787
if (p_preset.is_valid()) {
788
String android_source_zip = p_preset->get("gradle_build/android_source_template");
789
if (!android_source_zip.is_empty()) {
790
return android_source_zip;
791
}
792
}
793
794
const String templates_dir = EditorPaths::get_singleton()->get_export_templates_dir().path_join(GODOT_VERSION_FULL_CONFIG);
795
return templates_dir.path_join("android_source.zip");
796
}
797
798
String ExportTemplateManager::get_android_template_identifier(const Ref<EditorExportPreset> &p_preset) {
799
// The template identifier is the Godot version for the default template, and the full path plus md5 hash for custom templates.
800
if (p_preset.is_valid()) {
801
String android_source_zip = p_preset->get("gradle_build/android_source_template");
802
if (!android_source_zip.is_empty()) {
803
return android_source_zip + String(" [") + FileAccess::get_md5(android_source_zip) + String("]");
804
}
805
}
806
return GODOT_VERSION_FULL_CONFIG;
807
}
808
809
bool ExportTemplateManager::is_android_template_installed(const Ref<EditorExportPreset> &p_preset) {
810
return DirAccess::exists(get_android_build_directory(p_preset));
811
}
812
813
bool ExportTemplateManager::can_install_android_template(const Ref<EditorExportPreset> &p_preset) {
814
return FileAccess::exists(get_android_source_zip(p_preset));
815
}
816
817
Error ExportTemplateManager::install_android_template(const Ref<EditorExportPreset> &p_preset) {
818
const String source_zip = get_android_source_zip(p_preset);
819
ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
820
return install_android_template_from_file(source_zip, p_preset);
821
}
822
823
Error ExportTemplateManager::install_android_template_from_file(const String &p_file, const Ref<EditorExportPreset> &p_preset) {
824
// To support custom Android builds, we install the Java source code and buildsystem
825
// from android_source.zip to the project's res://android folder.
826
827
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
828
ERR_FAIL_COND_V(da.is_null(), ERR_CANT_CREATE);
829
830
String build_dir = get_android_build_directory(p_preset);
831
String parent_dir = build_dir.get_base_dir();
832
833
// Make parent of the build dir (if it does not exist).
834
da->make_dir_recursive(parent_dir);
835
{
836
// Add identifier, to ensure building won't work if the current template doesn't match.
837
Ref<FileAccess> f = FileAccess::open(parent_dir.path_join(".build_version"), FileAccess::WRITE);
838
ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE);
839
f->store_line(get_android_template_identifier(p_preset));
840
}
841
842
// Create the android build directory.
843
Error err = da->make_dir_recursive(build_dir);
844
ERR_FAIL_COND_V(err != OK, err);
845
{
846
// Add an empty .gdignore file to avoid scan.
847
Ref<FileAccess> f = FileAccess::open(build_dir.path_join(".gdignore"), FileAccess::WRITE);
848
ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE);
849
f->store_line("");
850
}
851
852
// Uncompress source template.
853
854
Ref<FileAccess> io_fa;
855
zlib_filefunc_def io = zipio_create_io(&io_fa);
856
857
unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
858
ERR_FAIL_NULL_V_MSG(pkg, ERR_CANT_OPEN, "Android sources not in ZIP format.");
859
860
int ret = unzGoToFirstFile(pkg);
861
int total_files = 0;
862
// Count files to unzip.
863
while (ret == UNZ_OK) {
864
total_files++;
865
ret = unzGoToNextFile(pkg);
866
}
867
ret = unzGoToFirstFile(pkg);
868
869
ProgressDialog::get_singleton()->add_task("uncompress_src", TTR("Uncompressing Android Build Sources"), total_files);
870
871
HashSet<String> dirs_tested;
872
int idx = 0;
873
while (ret == UNZ_OK) {
874
// Get file path.
875
unz_file_info info;
876
char fpath[16384];
877
ret = unzGetCurrentFileInfo(pkg, &info, fpath, 16384, nullptr, 0, nullptr, 0);
878
if (ret != UNZ_OK) {
879
break;
880
}
881
882
String path = String::utf8(fpath);
883
String base_dir = path.get_base_dir();
884
885
if (!path.ends_with("/")) {
886
Vector<uint8_t> uncomp_data;
887
uncomp_data.resize(info.uncompressed_size);
888
889
// Read.
890
unzOpenCurrentFile(pkg);
891
unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
892
unzCloseCurrentFile(pkg);
893
894
if (!dirs_tested.has(base_dir)) {
895
da->make_dir_recursive(build_dir.path_join(base_dir));
896
dirs_tested.insert(base_dir);
897
}
898
899
String to_write = build_dir.path_join(path);
900
Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE);
901
if (f.is_valid()) {
902
f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
903
f.unref(); // close file.
904
#ifndef WINDOWS_ENABLED
905
FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
906
#endif
907
} else {
908
ERR_PRINT("Can't uncompress file: " + to_write);
909
}
910
}
911
912
ProgressDialog::get_singleton()->task_step("uncompress_src", path, idx);
913
914
idx++;
915
ret = unzGoToNextFile(pkg);
916
}
917
918
ProgressDialog::get_singleton()->end_task("uncompress_src");
919
unzClose(pkg);
920
EditorFileSystem::get_singleton()->scan_changes();
921
return OK;
922
}
923
924
void ExportTemplateManager::_notification(int p_what) {
925
switch (p_what) {
926
case NOTIFICATION_THEME_CHANGED: {
927
current_value->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("main"), EditorStringName(EditorFonts)));
928
current_missing_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
929
current_installed_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
930
931
mirror_options_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
932
} break;
933
934
case NOTIFICATION_VISIBILITY_CHANGED: {
935
if (!is_visible()) {
936
set_process(false);
937
} else if (is_visible() && is_downloading_templates) {
938
set_process(true);
939
}
940
} break;
941
942
case NOTIFICATION_PROCESS: {
943
update_countdown -= get_process_delta_time();
944
if (update_countdown > 0) {
945
return;
946
}
947
update_countdown = 0.5;
948
949
String status;
950
int downloaded_bytes;
951
int total_bytes;
952
bool success = _humanize_http_status(download_templates, &status, &downloaded_bytes, &total_bytes);
953
954
if (downloaded_bytes >= 0) {
955
if (total_bytes > 0) {
956
_set_current_progress_value(float(downloaded_bytes) / total_bytes, status);
957
} else {
958
_set_current_progress_value(0, status);
959
}
960
} else {
961
_set_current_progress_status(status);
962
}
963
964
if (!success) {
965
set_process(false);
966
}
967
} break;
968
969
case NOTIFICATION_WM_CLOSE_REQUEST: {
970
// This won't stop the window from closing, but will show the alert if the download is active.
971
ok_pressed();
972
} break;
973
}
974
}
975
976
ExportTemplateManager::ExportTemplateManager() {
977
set_title(TTR("Export Template Manager"));
978
set_hide_on_ok(false);
979
set_ok_button_text(TTR("Close"));
980
981
VBoxContainer *main_vb = memnew(VBoxContainer);
982
add_child(main_vb);
983
984
// Current version controls.
985
HBoxContainer *current_hb = memnew(HBoxContainer);
986
main_vb->add_child(current_hb);
987
988
Label *current_label = memnew(Label);
989
current_label->set_theme_type_variation("HeaderSmall");
990
current_label->set_text(TTR("Current Version:"));
991
current_hb->add_child(current_label);
992
993
current_value = memnew(Label);
994
current_value->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
995
current_hb->add_child(current_value);
996
997
// Current version statuses.
998
// Status: Current version is missing.
999
current_missing_label = memnew(Label);
1000
current_missing_label->set_theme_type_variation("HeaderSmall");
1001
1002
current_missing_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1003
current_missing_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
1004
current_hb->add_child(current_missing_label);
1005
1006
// Status: Current version is installed.
1007
current_installed_label = memnew(Label);
1008
current_installed_label->set_theme_type_variation("HeaderSmall");
1009
current_installed_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1010
current_installed_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
1011
current_installed_label->set_text(TTR("Export templates are installed and ready to be used."));
1012
current_hb->add_child(current_installed_label);
1013
current_installed_label->hide();
1014
1015
// Currently installed template.
1016
current_installed_hb = memnew(HBoxContainer);
1017
main_vb->add_child(current_installed_hb);
1018
1019
current_installed_path = memnew(LineEdit);
1020
current_installed_path->set_editable(false);
1021
current_installed_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1022
current_installed_path->set_accessibility_name(TTRC("Installed Path"));
1023
current_installed_hb->add_child(current_installed_path);
1024
1025
#ifndef ANDROID_ENABLED
1026
Button *current_open_button = memnew(Button);
1027
current_open_button->set_text(TTR("Open Folder"));
1028
current_open_button->set_tooltip_text(TTR("Open the folder containing installed templates for the current version."));
1029
current_installed_hb->add_child(current_open_button);
1030
current_open_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_open_template_folder).bind(GODOT_VERSION_FULL_CONFIG));
1031
#endif
1032
1033
current_uninstall_button = memnew(Button);
1034
current_uninstall_button->set_text(TTR("Uninstall"));
1035
current_uninstall_button->set_tooltip_text(TTR("Uninstall templates for the current version."));
1036
current_installed_hb->add_child(current_uninstall_button);
1037
current_uninstall_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_uninstall_template).bind(GODOT_VERSION_FULL_CONFIG));
1038
1039
main_vb->add_child(memnew(HSeparator));
1040
1041
// Download and install section.
1042
HBoxContainer *install_templates_hb = memnew(HBoxContainer);
1043
main_vb->add_child(install_templates_hb);
1044
1045
// Download and install buttons are available.
1046
install_options_vb = memnew(VBoxContainer);
1047
install_options_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1048
install_templates_hb->add_child(install_options_vb);
1049
1050
HBoxContainer *download_install_hb = memnew(HBoxContainer);
1051
install_options_vb->add_child(download_install_hb);
1052
1053
Label *mirrors_label = memnew(Label);
1054
mirrors_label->set_text(TTR("Download from:"));
1055
download_install_hb->add_child(mirrors_label);
1056
1057
mirrors_list = memnew(OptionButton);
1058
mirrors_list->set_accessibility_name(TTRC("Mirror"));
1059
mirrors_list->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
1060
download_install_hb->add_child(mirrors_list);
1061
1062
request_mirrors = memnew(HTTPRequest);
1063
mirrors_list->add_child(request_mirrors);
1064
request_mirrors->connect("request_completed", callable_mp(this, &ExportTemplateManager::_refresh_mirrors_completed));
1065
1066
mirror_options_button = memnew(MenuButton);
1067
mirror_options_button->set_accessibility_name(TTRC("Mirror Options"));
1068
mirror_options_button->get_popup()->add_item(TTR("Open in Web Browser"), VISIT_WEB_MIRROR);
1069
mirror_options_button->get_popup()->add_item(TTR("Copy Mirror URL"), COPY_MIRROR_URL);
1070
download_install_hb->add_child(mirror_options_button);
1071
mirror_options_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ExportTemplateManager::_mirror_options_button_cbk));
1072
1073
download_install_hb->add_spacer();
1074
1075
download_current_button = memnew(Button);
1076
download_current_button->set_text(TTR("Download and Install"));
1077
download_current_button->set_tooltip_text(TTR("Download and install templates for the current version from the best possible mirror."));
1078
download_install_hb->add_child(download_current_button);
1079
download_current_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_download_current));
1080
1081
HBoxContainer *install_file_hb = memnew(HBoxContainer);
1082
install_file_hb->set_alignment(BoxContainer::ALIGNMENT_END);
1083
install_options_vb->add_child(install_file_hb);
1084
1085
install_file_button = memnew(Button);
1086
install_file_button->set_text(TTR("Install from File"));
1087
install_file_button->set_tooltip_text(TTR("Install templates from a local file."));
1088
install_file_hb->add_child(install_file_button);
1089
install_file_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_install_file));
1090
1091
enable_online_hb = memnew(HBoxContainer);
1092
install_options_vb->add_child(enable_online_hb);
1093
1094
Label *enable_online_label = memnew(Label);
1095
enable_online_label->set_text(TTR("Online mode is needed to download the templates."));
1096
enable_online_hb->add_child(enable_online_label);
1097
1098
LinkButton *enable_online_button = memnew(LinkButton);
1099
enable_online_button->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
1100
enable_online_button->set_text(TTR("Go Online"));
1101
enable_online_hb->add_child(enable_online_button);
1102
enable_online_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_force_online_mode));
1103
1104
// Templates are being downloaded; buttons unavailable.
1105
download_progress_hb = memnew(HBoxContainer);
1106
download_progress_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1107
install_templates_hb->add_child(download_progress_hb);
1108
download_progress_hb->hide();
1109
1110
download_progress_bar = memnew(ProgressBar);
1111
download_progress_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1112
download_progress_bar->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
1113
download_progress_bar->set_min(0);
1114
download_progress_bar->set_max(1);
1115
download_progress_bar->set_value(0);
1116
download_progress_bar->set_step(0.01);
1117
download_progress_bar->set_editor_preview_indeterminate(true);
1118
download_progress_hb->add_child(download_progress_bar);
1119
1120
download_progress_label = memnew(Label);
1121
download_progress_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1122
download_progress_hb->add_child(download_progress_label);
1123
1124
Button *download_cancel_button = memnew(Button);
1125
download_cancel_button->set_text(TTR("Cancel"));
1126
download_cancel_button->set_tooltip_text(TTR("Cancel the download of the templates."));
1127
download_progress_hb->add_child(download_cancel_button);
1128
download_cancel_button->connect(SceneStringName(pressed), callable_mp(this, &ExportTemplateManager::_cancel_template_download));
1129
1130
download_templates = memnew(HTTPRequest);
1131
install_templates_hb->add_child(download_templates);
1132
download_templates->connect("request_completed", callable_mp(this, &ExportTemplateManager::_download_template_completed));
1133
1134
main_vb->add_child(memnew(HSeparator));
1135
1136
// Other installed templates table.
1137
HBoxContainer *installed_versions_hb = memnew(HBoxContainer);
1138
main_vb->add_child(installed_versions_hb);
1139
Label *installed_label = memnew(Label);
1140
installed_label->set_theme_type_variation("HeaderSmall");
1141
installed_label->set_text(TTR("Other Installed Versions:"));
1142
installed_versions_hb->add_child(installed_label);
1143
1144
installed_table = memnew(Tree);
1145
installed_table->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1146
installed_table->set_hide_root(true);
1147
installed_table->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
1148
installed_table->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1149
main_vb->add_child(installed_table);
1150
installed_table->connect("button_clicked", callable_mp(this, &ExportTemplateManager::_installed_table_button_cbk));
1151
1152
// Dialogs.
1153
uninstall_confirm = memnew(ConfirmationDialog);
1154
uninstall_confirm->set_title(TTR("Uninstall Template"));
1155
add_child(uninstall_confirm);
1156
uninstall_confirm->connect(SceneStringName(confirmed), callable_mp(this, &ExportTemplateManager::_uninstall_template_confirmed));
1157
1158
install_file_dialog = memnew(FileDialog);
1159
install_file_dialog->set_title(TTR("Select Template File"));
1160
install_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
1161
install_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
1162
install_file_dialog->set_current_dir(EDITOR_DEF("_export_template_download_directory", ""));
1163
install_file_dialog->add_filter("*.tpz", TTR("Godot Export Templates"));
1164
install_file_dialog->connect("file_selected", callable_mp(this, &ExportTemplateManager::_install_file_selected).bind(false));
1165
add_child(install_file_dialog);
1166
1167
hide_dialog_accept = memnew(AcceptDialog);
1168
hide_dialog_accept->set_text(TTR("The templates will continue to download.\nYou may experience a short editor freeze when they finish."));
1169
add_child(hide_dialog_accept);
1170
hide_dialog_accept->connect(SceneStringName(confirmed), callable_mp(this, &ExportTemplateManager::_hide_dialog));
1171
}
1172
1173