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_paths.h" |
39 | #include "editor/editor_scale.h" |
40 | #include "editor/editor_settings.h" |
41 | #include "editor/editor_string_names.h" |
42 | #include "editor/progress_dialog.h" |
43 | #include "scene/gui/file_dialog.h" |
44 | #include "scene/gui/menu_button.h" |
45 | #include "scene/gui/separator.h" |
46 | #include "scene/gui/tree.h" |
47 | #include "scene/main/http_request.h" |
48 | |
49 | void ExportTemplateManager::_update_template_status() { |
50 | // Fetch installed templates from the file system. |
51 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
52 | const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir(); |
53 | |
54 | Error err = da->change_dir(templates_dir); |
55 | ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'." ); |
56 | |
57 | RBSet<String> templates; |
58 | da->list_dir_begin(); |
59 | if (err == OK) { |
60 | String c = da->get_next(); |
61 | while (!c.is_empty()) { |
62 | if (da->current_is_dir() && !c.begins_with("." )) { |
63 | templates.insert(c); |
64 | } |
65 | c = da->get_next(); |
66 | } |
67 | } |
68 | da->list_dir_end(); |
69 | |
70 | // Update the state of the current version. |
71 | String current_version = VERSION_FULL_CONFIG; |
72 | current_value->set_text(current_version); |
73 | |
74 | if (templates.has(current_version)) { |
75 | current_missing_label->hide(); |
76 | current_installed_label->show(); |
77 | |
78 | current_installed_hb->show(); |
79 | current_version_exists = true; |
80 | } else { |
81 | current_installed_label->hide(); |
82 | current_missing_label->show(); |
83 | |
84 | current_installed_hb->hide(); |
85 | current_version_exists = false; |
86 | } |
87 | |
88 | if (is_downloading_templates) { |
89 | install_options_vb->hide(); |
90 | download_progress_hb->show(); |
91 | } else { |
92 | download_progress_hb->hide(); |
93 | install_options_vb->show(); |
94 | |
95 | if (templates.has(current_version)) { |
96 | current_installed_path->set_text(templates_dir.path_join(current_version)); |
97 | } |
98 | } |
99 | |
100 | // Update the list of other installed versions. |
101 | installed_table->clear(); |
102 | TreeItem *installed_root = installed_table->create_item(); |
103 | |
104 | for (RBSet<String>::Element *E = templates.back(); E; E = E->prev()) { |
105 | String version_string = E->get(); |
106 | if (version_string == current_version) { |
107 | continue; |
108 | } |
109 | |
110 | TreeItem *ti = installed_table->create_item(installed_root); |
111 | ti->set_text(0, version_string); |
112 | |
113 | ti->add_button(0, get_editor_theme_icon(SNAME("Folder" )), OPEN_TEMPLATE_FOLDER, false, TTR("Open the folder containing these templates." )); |
114 | ti->add_button(0, get_editor_theme_icon(SNAME("Remove" )), UNINSTALL_TEMPLATE, false, TTR("Uninstall these templates." )); |
115 | } |
116 | } |
117 | |
118 | void ExportTemplateManager::_download_current() { |
119 | if (is_downloading_templates) { |
120 | return; |
121 | } |
122 | is_downloading_templates = true; |
123 | |
124 | install_options_vb->hide(); |
125 | download_progress_hb->show(); |
126 | |
127 | if (mirrors_available) { |
128 | String mirror_url = _get_selected_mirror(); |
129 | if (mirror_url.is_empty()) { |
130 | _set_current_progress_status(TTR("There are no mirrors available." ), true); |
131 | return; |
132 | } |
133 | |
134 | _download_template(mirror_url, true); |
135 | } else if (!is_refreshing_mirrors) { |
136 | _set_current_progress_status(TTR("Retrieving the mirror list..." )); |
137 | _refresh_mirrors(); |
138 | } |
139 | } |
140 | |
141 | void ExportTemplateManager::_download_template(const String &p_url, bool p_skip_check) { |
142 | if (!p_skip_check && is_downloading_templates) { |
143 | return; |
144 | } |
145 | is_downloading_templates = true; |
146 | |
147 | install_options_vb->hide(); |
148 | download_progress_hb->show(); |
149 | _set_current_progress_status(TTR("Starting the download..." )); |
150 | |
151 | download_templates->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_templates.tpz" )); |
152 | download_templates->set_use_threads(true); |
153 | |
154 | const String proxy_host = EDITOR_GET("network/http_proxy/host" ); |
155 | const int proxy_port = EDITOR_GET("network/http_proxy/port" ); |
156 | download_templates->set_http_proxy(proxy_host, proxy_port); |
157 | download_templates->set_https_proxy(proxy_host, proxy_port); |
158 | |
159 | Error err = download_templates->request(p_url); |
160 | if (err != OK) { |
161 | _set_current_progress_status(TTR("Error requesting URL:" ) + " " + p_url, true); |
162 | return; |
163 | } |
164 | |
165 | set_process(true); |
166 | _set_current_progress_status(TTR("Connecting to the mirror..." )); |
167 | } |
168 | |
169 | void ExportTemplateManager::_download_template_completed(int p_status, int p_code, const PackedStringArray &, const PackedByteArray &p_data) { |
170 | switch (p_status) { |
171 | case HTTPRequest::RESULT_CANT_RESOLVE: { |
172 | _set_current_progress_status(TTR("Can't resolve the requested address." ), true); |
173 | } break; |
174 | case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED: |
175 | case HTTPRequest::RESULT_CONNECTION_ERROR: |
176 | case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: |
177 | case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR: |
178 | case HTTPRequest::RESULT_CANT_CONNECT: { |
179 | _set_current_progress_status(TTR("Can't connect to the mirror." ), true); |
180 | } break; |
181 | case HTTPRequest::RESULT_NO_RESPONSE: { |
182 | _set_current_progress_status(TTR("No response from the mirror." ), true); |
183 | } break; |
184 | case HTTPRequest::RESULT_REQUEST_FAILED: { |
185 | _set_current_progress_status(TTR("Request failed." ), true); |
186 | } break; |
187 | case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: { |
188 | _set_current_progress_status(TTR("Request ended up in a redirect loop." ), true); |
189 | } break; |
190 | default: { |
191 | if (p_code != 200) { |
192 | _set_current_progress_status(TTR("Request failed:" ) + " " + itos(p_code), true); |
193 | } else { |
194 | _set_current_progress_status(TTR("Download complete; extracting templates..." )); |
195 | String path = download_templates->get_download_file(); |
196 | |
197 | is_downloading_templates = false; |
198 | bool ret = _install_file_selected(path, true); |
199 | if (ret) { |
200 | // Clean up downloaded file. |
201 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
202 | Error err = da->remove(path); |
203 | if (err != OK) { |
204 | EditorNode::get_singleton()->add_io_error(TTR("Cannot remove temporary file:" ) + "\n" + path + "\n" ); |
205 | } |
206 | } else { |
207 | EditorNode::get_singleton()->add_io_error(vformat(TTR("Templates installation failed.\nThe problematic templates archives can be found at '%s'." ), path)); |
208 | } |
209 | } |
210 | } break; |
211 | } |
212 | |
213 | set_process(false); |
214 | } |
215 | |
216 | void ExportTemplateManager::_cancel_template_download() { |
217 | if (!is_downloading_templates) { |
218 | return; |
219 | } |
220 | |
221 | download_templates->cancel_request(); |
222 | download_progress_hb->hide(); |
223 | install_options_vb->show(); |
224 | is_downloading_templates = false; |
225 | } |
226 | |
227 | void ExportTemplateManager::_refresh_mirrors() { |
228 | if (is_refreshing_mirrors) { |
229 | return; |
230 | } |
231 | is_refreshing_mirrors = true; |
232 | |
233 | String current_version = VERSION_FULL_CONFIG; |
234 | const String mirrors_metadata_url = "https://godotengine.org/mirrorlist/" + current_version + ".json" ; |
235 | request_mirrors->request(mirrors_metadata_url); |
236 | } |
237 | |
238 | void ExportTemplateManager::_refresh_mirrors_completed(int p_status, int p_code, const PackedStringArray &, const PackedByteArray &p_data) { |
239 | if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) { |
240 | EditorNode::get_singleton()->show_warning(TTR("Error getting the list of mirrors." )); |
241 | is_refreshing_mirrors = false; |
242 | if (is_downloading_templates) { |
243 | _cancel_template_download(); |
244 | } |
245 | return; |
246 | } |
247 | |
248 | String response_json; |
249 | { |
250 | const uint8_t *r = p_data.ptr(); |
251 | response_json.parse_utf8((const char *)r, p_data.size()); |
252 | } |
253 | |
254 | JSON json; |
255 | Error err = json.parse(response_json); |
256 | if (err != OK) { |
257 | EditorNode::get_singleton()->show_warning(TTR("Error parsing JSON with the list of mirrors. Please report this issue!" )); |
258 | is_refreshing_mirrors = false; |
259 | if (is_downloading_templates) { |
260 | _cancel_template_download(); |
261 | } |
262 | return; |
263 | } |
264 | |
265 | mirrors_list->clear(); |
266 | mirrors_list->add_item(TTR("Best available mirror" ), 0); |
267 | |
268 | mirrors_available = false; |
269 | |
270 | Dictionary mirror_data = json.get_data(); |
271 | if (mirror_data.has("mirrors" )) { |
272 | Array mirrors = mirror_data["mirrors" ]; |
273 | |
274 | for (int i = 0; i < mirrors.size(); i++) { |
275 | Dictionary m = mirrors[i]; |
276 | ERR_CONTINUE(!m.has("url" ) || !m.has("name" )); |
277 | |
278 | mirrors_list->add_item(m["name" ]); |
279 | mirrors_list->set_item_metadata(i + 1, m["url" ]); |
280 | |
281 | mirrors_available = true; |
282 | } |
283 | } |
284 | if (!mirrors_available) { |
285 | EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases." )); |
286 | if (is_downloading_templates) { |
287 | _cancel_template_download(); |
288 | } |
289 | } |
290 | |
291 | is_refreshing_mirrors = false; |
292 | |
293 | if (is_downloading_templates) { |
294 | String mirror_url = _get_selected_mirror(); |
295 | if (mirror_url.is_empty()) { |
296 | _set_current_progress_status(TTR("There are no mirrors available." ), true); |
297 | return; |
298 | } |
299 | |
300 | _download_template(mirror_url, true); |
301 | } |
302 | } |
303 | |
304 | bool ExportTemplateManager::_humanize_http_status(HTTPRequest *p_request, String *r_status, int *r_downloaded_bytes, int *r_total_bytes) { |
305 | *r_status = "" ; |
306 | *r_downloaded_bytes = -1; |
307 | *r_total_bytes = -1; |
308 | bool success = true; |
309 | |
310 | switch (p_request->get_http_client_status()) { |
311 | case HTTPClient::STATUS_DISCONNECTED: |
312 | *r_status = TTR("Disconnected" ); |
313 | success = false; |
314 | break; |
315 | case HTTPClient::STATUS_RESOLVING: |
316 | *r_status = TTR("Resolving" ); |
317 | break; |
318 | case HTTPClient::STATUS_CANT_RESOLVE: |
319 | *r_status = TTR("Can't Resolve" ); |
320 | success = false; |
321 | break; |
322 | case HTTPClient::STATUS_CONNECTING: |
323 | *r_status = TTR("Connecting..." ); |
324 | break; |
325 | case HTTPClient::STATUS_CANT_CONNECT: |
326 | *r_status = TTR("Can't Connect" ); |
327 | success = false; |
328 | break; |
329 | case HTTPClient::STATUS_CONNECTED: |
330 | *r_status = TTR("Connected" ); |
331 | break; |
332 | case HTTPClient::STATUS_REQUESTING: |
333 | *r_status = TTR("Requesting..." ); |
334 | break; |
335 | case HTTPClient::STATUS_BODY: |
336 | *r_status = TTR("Downloading" ); |
337 | *r_downloaded_bytes = p_request->get_downloaded_bytes(); |
338 | *r_total_bytes = p_request->get_body_size(); |
339 | |
340 | if (p_request->get_body_size() > 0) { |
341 | *r_status += " " + String::humanize_size(p_request->get_downloaded_bytes()) + "/" + String::humanize_size(p_request->get_body_size()); |
342 | } else { |
343 | *r_status += " " + String::humanize_size(p_request->get_downloaded_bytes()); |
344 | } |
345 | break; |
346 | case HTTPClient::STATUS_CONNECTION_ERROR: |
347 | *r_status = TTR("Connection Error" ); |
348 | success = false; |
349 | break; |
350 | case HTTPClient::STATUS_TLS_HANDSHAKE_ERROR: |
351 | *r_status = TTR("TLS Handshake Error" ); |
352 | success = false; |
353 | break; |
354 | } |
355 | |
356 | return success; |
357 | } |
358 | |
359 | void ExportTemplateManager::_set_current_progress_status(const String &p_status, bool p_error) { |
360 | download_progress_bar->hide(); |
361 | download_progress_label->set_text(p_status); |
362 | |
363 | if (p_error) { |
364 | download_progress_label->add_theme_color_override("font_color" , get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
365 | } else { |
366 | download_progress_label->add_theme_color_override("font_color" , get_theme_color(SNAME("font_color" ), SNAME("Label" ))); |
367 | } |
368 | } |
369 | |
370 | void ExportTemplateManager::_set_current_progress_value(float p_value, const String &p_status) { |
371 | download_progress_bar->show(); |
372 | download_progress_bar->set_value(p_value); |
373 | download_progress_label->set_text(p_status); |
374 | } |
375 | |
376 | void ExportTemplateManager::_install_file() { |
377 | install_file_dialog->popup_file_dialog(); |
378 | } |
379 | |
380 | bool ExportTemplateManager::_install_file_selected(const String &p_file, bool p_skip_progress) { |
381 | Ref<FileAccess> io_fa; |
382 | zlib_filefunc_def io = zipio_create_io(&io_fa); |
383 | |
384 | unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io); |
385 | if (!pkg) { |
386 | EditorNode::get_singleton()->show_warning(TTR("Can't open the export templates file." )); |
387 | return false; |
388 | } |
389 | int ret = unzGoToFirstFile(pkg); |
390 | |
391 | // Count them and find version. |
392 | int fc = 0; |
393 | String version; |
394 | String contents_dir; |
395 | |
396 | while (ret == UNZ_OK) { |
397 | unz_file_info info; |
398 | char fname[16384]; |
399 | ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0); |
400 | if (ret != UNZ_OK) { |
401 | break; |
402 | } |
403 | |
404 | String file = String::utf8(fname); |
405 | if (file.ends_with("version.txt" )) { |
406 | Vector<uint8_t> uncomp_data; |
407 | uncomp_data.resize(info.uncompressed_size); |
408 | |
409 | // Read. |
410 | unzOpenCurrentFile(pkg); |
411 | ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size()); |
412 | ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used." , file)); |
413 | unzCloseCurrentFile(pkg); |
414 | |
415 | String data_str; |
416 | data_str.parse_utf8((const char *)uncomp_data.ptr(), uncomp_data.size()); |
417 | data_str = data_str.strip_edges(); |
418 | |
419 | // Version number should be of the form major.minor[.patch].status[.module_config] |
420 | // so it can in theory have 3 or more slices. |
421 | if (data_str.get_slice_count("." ) < 3) { |
422 | EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside the export templates file: %s." ), data_str)); |
423 | unzClose(pkg); |
424 | return false; |
425 | } |
426 | |
427 | version = data_str; |
428 | contents_dir = file.get_base_dir().trim_suffix("/" ).trim_suffix("\\" ); |
429 | } |
430 | |
431 | if (file.get_file().size() != 0) { |
432 | fc++; |
433 | } |
434 | |
435 | ret = unzGoToNextFile(pkg); |
436 | } |
437 | |
438 | if (version.is_empty()) { |
439 | EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside the export templates file." )); |
440 | unzClose(pkg); |
441 | return false; |
442 | } |
443 | |
444 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
445 | String template_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(version); |
446 | Error err = d->make_dir_recursive(template_path); |
447 | if (err != OK) { |
448 | EditorNode::get_singleton()->show_warning(TTR("Error creating path for extracting templates:" ) + "\n" + template_path); |
449 | unzClose(pkg); |
450 | return false; |
451 | } |
452 | |
453 | EditorProgress *p = nullptr; |
454 | if (!p_skip_progress) { |
455 | p = memnew(EditorProgress("ltask" , TTR("Extracting Export Templates" ), fc)); |
456 | } |
457 | |
458 | fc = 0; |
459 | ret = unzGoToFirstFile(pkg); |
460 | while (ret == UNZ_OK) { |
461 | // Get filename. |
462 | unz_file_info info; |
463 | char fname[16384]; |
464 | ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0); |
465 | if (ret != UNZ_OK) { |
466 | break; |
467 | } |
468 | |
469 | String file_path(String::utf8(fname).simplify_path()); |
470 | |
471 | String file = file_path.get_file(); |
472 | |
473 | if (file.size() == 0) { |
474 | ret = unzGoToNextFile(pkg); |
475 | continue; |
476 | } |
477 | |
478 | Vector<uint8_t> uncomp_data; |
479 | uncomp_data.resize(info.uncompressed_size); |
480 | |
481 | // Read |
482 | unzOpenCurrentFile(pkg); |
483 | ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size()); |
484 | ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used." , file)); |
485 | unzCloseCurrentFile(pkg); |
486 | |
487 | String base_dir = file_path.get_base_dir().trim_suffix("/" ); |
488 | |
489 | if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) { |
490 | base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/" ); |
491 | file = base_dir.path_join(file); |
492 | |
493 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
494 | ERR_CONTINUE(da.is_null()); |
495 | |
496 | String output_dir = template_path.path_join(base_dir); |
497 | |
498 | if (!DirAccess::exists(output_dir)) { |
499 | Error mkdir_err = da->make_dir_recursive(output_dir); |
500 | ERR_CONTINUE(mkdir_err != OK); |
501 | } |
502 | } |
503 | |
504 | if (p) { |
505 | p->step(TTR("Importing:" ) + " " + file, fc); |
506 | } |
507 | |
508 | String to_write = template_path.path_join(file); |
509 | Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE); |
510 | |
511 | if (f.is_null()) { |
512 | ret = unzGoToNextFile(pkg); |
513 | fc++; |
514 | ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'." ); |
515 | } |
516 | |
517 | f->store_buffer(uncomp_data.ptr(), uncomp_data.size()); |
518 | f.unref(); // close file. |
519 | #ifndef WINDOWS_ENABLED |
520 | FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF); |
521 | #endif |
522 | |
523 | ret = unzGoToNextFile(pkg); |
524 | fc++; |
525 | } |
526 | |
527 | if (p) { |
528 | memdelete(p); |
529 | } |
530 | unzClose(pkg); |
531 | |
532 | _update_template_status(); |
533 | EditorSettings::get_singleton()->set_meta("export_template_download_directory" , p_file.get_base_dir()); |
534 | return true; |
535 | } |
536 | |
537 | void ExportTemplateManager::_uninstall_template(const String &p_version) { |
538 | uninstall_confirm->set_text(vformat(TTR("Remove templates for the version '%s'?" ), p_version)); |
539 | uninstall_confirm->popup_centered(); |
540 | uninstall_version = p_version; |
541 | } |
542 | |
543 | void ExportTemplateManager::_uninstall_template_confirmed() { |
544 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
545 | const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir(); |
546 | |
547 | Error err = da->change_dir(templates_dir); |
548 | ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'." ); |
549 | err = da->change_dir(uninstall_version); |
550 | ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.path_join(uninstall_version) + "'." ); |
551 | |
552 | err = da->erase_contents_recursive(); |
553 | ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.path_join(uninstall_version) + "'." ); |
554 | |
555 | da->change_dir(".." ); |
556 | err = da->remove(uninstall_version); |
557 | ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.path_join(uninstall_version) + "'." ); |
558 | |
559 | _update_template_status(); |
560 | } |
561 | |
562 | String ExportTemplateManager::_get_selected_mirror() const { |
563 | if (mirrors_list->get_item_count() == 1) { |
564 | return "" ; |
565 | } |
566 | |
567 | int selected = mirrors_list->get_selected_id(); |
568 | if (selected == 0) { |
569 | // This is a special "best available" value; so pick the first available mirror from the rest of the list. |
570 | selected = 1; |
571 | } |
572 | |
573 | return mirrors_list->get_item_metadata(selected); |
574 | } |
575 | |
576 | void ExportTemplateManager::_mirror_options_button_cbk(int p_id) { |
577 | switch (p_id) { |
578 | case VISIT_WEB_MIRROR: { |
579 | String mirror_url = _get_selected_mirror(); |
580 | if (mirror_url.is_empty()) { |
581 | EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available." )); |
582 | return; |
583 | } |
584 | |
585 | OS::get_singleton()->shell_open(mirror_url); |
586 | } break; |
587 | |
588 | case COPY_MIRROR_URL: { |
589 | String mirror_url = _get_selected_mirror(); |
590 | if (mirror_url.is_empty()) { |
591 | EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available." )); |
592 | return; |
593 | } |
594 | |
595 | DisplayServer::get_singleton()->clipboard_set(mirror_url); |
596 | } break; |
597 | } |
598 | } |
599 | |
600 | void ExportTemplateManager::_installed_table_button_cbk(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
601 | if (p_button != MouseButton::LEFT) { |
602 | return; |
603 | } |
604 | TreeItem *ti = Object::cast_to<TreeItem>(p_item); |
605 | if (!ti) { |
606 | return; |
607 | } |
608 | |
609 | switch (p_id) { |
610 | case OPEN_TEMPLATE_FOLDER: { |
611 | String version_string = ti->get_text(0); |
612 | _open_template_folder(version_string); |
613 | } break; |
614 | |
615 | case UNINSTALL_TEMPLATE: { |
616 | String version_string = ti->get_text(0); |
617 | _uninstall_template(version_string); |
618 | } break; |
619 | } |
620 | } |
621 | |
622 | void ExportTemplateManager::_open_template_folder(const String &p_version) { |
623 | const String &templates_dir = EditorPaths::get_singleton()->get_export_templates_dir(); |
624 | OS::get_singleton()->shell_show_in_file_manager(templates_dir.path_join(p_version), true); |
625 | } |
626 | |
627 | void ExportTemplateManager::() { |
628 | _update_template_status(); |
629 | _refresh_mirrors(); |
630 | popup_centered(Size2(720, 280) * EDSCALE); |
631 | } |
632 | |
633 | void ExportTemplateManager::ok_pressed() { |
634 | if (!is_downloading_templates) { |
635 | hide(); |
636 | return; |
637 | } |
638 | |
639 | hide_dialog_accept->popup_centered(); |
640 | } |
641 | |
642 | void ExportTemplateManager::_hide_dialog() { |
643 | hide(); |
644 | } |
645 | |
646 | bool ExportTemplateManager::can_install_android_template() { |
647 | const String templates_dir = EditorPaths::get_singleton()->get_export_templates_dir().path_join(VERSION_FULL_CONFIG); |
648 | return FileAccess::exists(templates_dir.path_join("android_source.zip" )); |
649 | } |
650 | |
651 | Error ExportTemplateManager::install_android_template() { |
652 | const String &templates_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(VERSION_FULL_CONFIG); |
653 | const String &source_zip = templates_path.path_join("android_source.zip" ); |
654 | ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN); |
655 | return install_android_template_from_file(source_zip); |
656 | } |
657 | Error ExportTemplateManager::install_android_template_from_file(const String &p_file) { |
658 | // To support custom Android builds, we install the Java source code and buildsystem |
659 | // from android_source.zip to the project's res://android folder. |
660 | |
661 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
662 | ERR_FAIL_COND_V(da.is_null(), ERR_CANT_CREATE); |
663 | |
664 | // Make res://android dir (if it does not exist). |
665 | da->make_dir("android" ); |
666 | { |
667 | // Add version, to ensure building won't work if template and Godot version don't match. |
668 | Ref<FileAccess> f = FileAccess::open("res://android/.build_version" , FileAccess::WRITE); |
669 | ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE); |
670 | f->store_line(VERSION_FULL_CONFIG); |
671 | } |
672 | |
673 | // Create the android build directory. |
674 | Error err = da->make_dir_recursive("android/build" ); |
675 | ERR_FAIL_COND_V(err != OK, err); |
676 | { |
677 | // Add an empty .gdignore file to avoid scan. |
678 | Ref<FileAccess> f = FileAccess::open("res://android/build/.gdignore" , FileAccess::WRITE); |
679 | ERR_FAIL_COND_V(f.is_null(), ERR_CANT_CREATE); |
680 | f->store_line("" ); |
681 | } |
682 | |
683 | // Uncompress source template. |
684 | |
685 | Ref<FileAccess> io_fa; |
686 | zlib_filefunc_def io = zipio_create_io(&io_fa); |
687 | |
688 | unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io); |
689 | ERR_FAIL_COND_V_MSG(!pkg, ERR_CANT_OPEN, "Android sources not in ZIP format." ); |
690 | |
691 | int ret = unzGoToFirstFile(pkg); |
692 | int total_files = 0; |
693 | // Count files to unzip. |
694 | while (ret == UNZ_OK) { |
695 | total_files++; |
696 | ret = unzGoToNextFile(pkg); |
697 | } |
698 | ret = unzGoToFirstFile(pkg); |
699 | |
700 | ProgressDialog::get_singleton()->add_task("uncompress_src" , TTR("Uncompressing Android Build Sources" ), total_files); |
701 | |
702 | HashSet<String> dirs_tested; |
703 | int idx = 0; |
704 | while (ret == UNZ_OK) { |
705 | // Get file path. |
706 | unz_file_info info; |
707 | char fpath[16384]; |
708 | ret = unzGetCurrentFileInfo(pkg, &info, fpath, 16384, nullptr, 0, nullptr, 0); |
709 | if (ret != UNZ_OK) { |
710 | break; |
711 | } |
712 | |
713 | String path = String::utf8(fpath); |
714 | String base_dir = path.get_base_dir(); |
715 | |
716 | if (!path.ends_with("/" )) { |
717 | Vector<uint8_t> uncomp_data; |
718 | uncomp_data.resize(info.uncompressed_size); |
719 | |
720 | // Read. |
721 | unzOpenCurrentFile(pkg); |
722 | unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size()); |
723 | unzCloseCurrentFile(pkg); |
724 | |
725 | if (!dirs_tested.has(base_dir)) { |
726 | da->make_dir_recursive(String("android/build" ).path_join(base_dir)); |
727 | dirs_tested.insert(base_dir); |
728 | } |
729 | |
730 | String to_write = String("res://android/build" ).path_join(path); |
731 | Ref<FileAccess> f = FileAccess::open(to_write, FileAccess::WRITE); |
732 | if (f.is_valid()) { |
733 | f->store_buffer(uncomp_data.ptr(), uncomp_data.size()); |
734 | f.unref(); // close file. |
735 | #ifndef WINDOWS_ENABLED |
736 | FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF); |
737 | #endif |
738 | } else { |
739 | ERR_PRINT("Can't uncompress file: " + to_write); |
740 | } |
741 | } |
742 | |
743 | ProgressDialog::get_singleton()->task_step("uncompress_src" , path, idx); |
744 | |
745 | idx++; |
746 | ret = unzGoToNextFile(pkg); |
747 | } |
748 | |
749 | ProgressDialog::get_singleton()->end_task("uncompress_src" ); |
750 | unzClose(pkg); |
751 | |
752 | return OK; |
753 | } |
754 | |
755 | void ExportTemplateManager::_notification(int p_what) { |
756 | switch (p_what) { |
757 | case NOTIFICATION_ENTER_TREE: |
758 | case NOTIFICATION_THEME_CHANGED: { |
759 | current_value->add_theme_font_override("font" , get_theme_font(SNAME("main" ), EditorStringName(EditorFonts))); |
760 | current_missing_label->add_theme_color_override("font_color" , get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
761 | current_installed_label->add_theme_color_override("font_color" , get_theme_color(SNAME("disabled_font_color" ), EditorStringName(Editor))); |
762 | |
763 | mirror_options_button->set_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl" ))); |
764 | } break; |
765 | |
766 | case NOTIFICATION_VISIBILITY_CHANGED: { |
767 | if (!is_visible()) { |
768 | set_process(false); |
769 | } else if (is_visible() && is_downloading_templates) { |
770 | set_process(true); |
771 | } |
772 | } break; |
773 | |
774 | case NOTIFICATION_PROCESS: { |
775 | update_countdown -= get_process_delta_time(); |
776 | if (update_countdown > 0) { |
777 | return; |
778 | } |
779 | update_countdown = 0.5; |
780 | |
781 | String status; |
782 | int downloaded_bytes; |
783 | int total_bytes; |
784 | bool success = _humanize_http_status(download_templates, &status, &downloaded_bytes, &total_bytes); |
785 | |
786 | if (downloaded_bytes >= 0) { |
787 | if (total_bytes > 0) { |
788 | _set_current_progress_value(float(downloaded_bytes) / total_bytes, status); |
789 | } else { |
790 | _set_current_progress_value(0, status); |
791 | } |
792 | } else { |
793 | _set_current_progress_status(status); |
794 | } |
795 | |
796 | if (!success) { |
797 | set_process(false); |
798 | } |
799 | } break; |
800 | |
801 | case NOTIFICATION_WM_CLOSE_REQUEST: { |
802 | // This won't stop the window from closing, but will show the alert if the download is active. |
803 | ok_pressed(); |
804 | } break; |
805 | } |
806 | } |
807 | |
808 | void ExportTemplateManager::_bind_methods() { |
809 | } |
810 | |
811 | ExportTemplateManager::ExportTemplateManager() { |
812 | set_title(TTR("Export Template Manager" )); |
813 | set_hide_on_ok(false); |
814 | set_ok_button_text(TTR("Close" )); |
815 | |
816 | // Downloadable export templates are only available for stable and official alpha/beta/RC builds |
817 | // (which always have a number following their status, e.g. "alpha1"). |
818 | // Therefore, don't display download-related features when using a development version |
819 | // (whose builds aren't numbered). |
820 | downloads_available = |
821 | String(VERSION_STATUS) != String("dev" ) && |
822 | String(VERSION_STATUS) != String("alpha" ) && |
823 | String(VERSION_STATUS) != String("beta" ) && |
824 | String(VERSION_STATUS) != String("rc" ); |
825 | |
826 | VBoxContainer *main_vb = memnew(VBoxContainer); |
827 | add_child(main_vb); |
828 | |
829 | // Current version controls. |
830 | HBoxContainer *current_hb = memnew(HBoxContainer); |
831 | main_vb->add_child(current_hb); |
832 | |
833 | Label *current_label = memnew(Label); |
834 | current_label->set_theme_type_variation("HeaderSmall" ); |
835 | current_label->set_text(TTR("Current Version:" )); |
836 | current_hb->add_child(current_label); |
837 | |
838 | current_value = memnew(Label); |
839 | current_hb->add_child(current_value); |
840 | |
841 | // Current version statuses. |
842 | // Status: Current version is missing. |
843 | current_missing_label = memnew(Label); |
844 | current_missing_label->set_theme_type_variation("HeaderSmall" ); |
845 | |
846 | current_missing_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
847 | current_missing_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
848 | current_missing_label->set_text(TTR("Export templates are missing. Download them or install from a file." )); |
849 | current_hb->add_child(current_missing_label); |
850 | |
851 | // Status: Current version is installed. |
852 | current_installed_label = memnew(Label); |
853 | current_installed_label->set_theme_type_variation("HeaderSmall" ); |
854 | current_installed_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
855 | current_installed_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
856 | current_installed_label->set_text(TTR("Export templates are installed and ready to be used." )); |
857 | current_hb->add_child(current_installed_label); |
858 | current_installed_label->hide(); |
859 | |
860 | // Currently installed template. |
861 | current_installed_hb = memnew(HBoxContainer); |
862 | main_vb->add_child(current_installed_hb); |
863 | |
864 | current_installed_path = memnew(LineEdit); |
865 | current_installed_path->set_editable(false); |
866 | current_installed_path->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
867 | current_installed_hb->add_child(current_installed_path); |
868 | |
869 | current_open_button = memnew(Button); |
870 | current_open_button->set_text(TTR("Open Folder" )); |
871 | current_open_button->set_tooltip_text(TTR("Open the folder containing installed templates for the current version." )); |
872 | current_installed_hb->add_child(current_open_button); |
873 | current_open_button->connect("pressed" , callable_mp(this, &ExportTemplateManager::_open_template_folder).bind(VERSION_FULL_CONFIG)); |
874 | |
875 | current_uninstall_button = memnew(Button); |
876 | current_uninstall_button->set_text(TTR("Uninstall" )); |
877 | current_uninstall_button->set_tooltip_text(TTR("Uninstall templates for the current version." )); |
878 | current_installed_hb->add_child(current_uninstall_button); |
879 | current_uninstall_button->connect("pressed" , callable_mp(this, &ExportTemplateManager::_uninstall_template).bind(VERSION_FULL_CONFIG)); |
880 | |
881 | main_vb->add_child(memnew(HSeparator)); |
882 | |
883 | // Download and install section. |
884 | HBoxContainer *install_templates_hb = memnew(HBoxContainer); |
885 | main_vb->add_child(install_templates_hb); |
886 | |
887 | // Download and install buttons are available. |
888 | install_options_vb = memnew(VBoxContainer); |
889 | install_options_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
890 | install_templates_hb->add_child(install_options_vb); |
891 | |
892 | HBoxContainer *download_install_hb = memnew(HBoxContainer); |
893 | install_options_vb->add_child(download_install_hb); |
894 | |
895 | Label *mirrors_label = memnew(Label); |
896 | mirrors_label->set_text(TTR("Download from:" )); |
897 | download_install_hb->add_child(mirrors_label); |
898 | |
899 | mirrors_list = memnew(OptionButton); |
900 | mirrors_list->set_custom_minimum_size(Size2(280, 0) * EDSCALE); |
901 | download_install_hb->add_child(mirrors_list); |
902 | mirrors_list->add_item(TTR("Best available mirror" ), 0); |
903 | |
904 | request_mirrors = memnew(HTTPRequest); |
905 | mirrors_list->add_child(request_mirrors); |
906 | request_mirrors->connect("request_completed" , callable_mp(this, &ExportTemplateManager::_refresh_mirrors_completed)); |
907 | |
908 | mirror_options_button = memnew(MenuButton); |
909 | mirror_options_button->get_popup()->add_item(TTR("Open in Web Browser" ), VISIT_WEB_MIRROR); |
910 | mirror_options_button->get_popup()->add_item(TTR("Copy Mirror URL" ), COPY_MIRROR_URL); |
911 | download_install_hb->add_child(mirror_options_button); |
912 | mirror_options_button->get_popup()->connect("id_pressed" , callable_mp(this, &ExportTemplateManager::_mirror_options_button_cbk)); |
913 | |
914 | download_install_hb->add_spacer(); |
915 | |
916 | Button *download_current_button = memnew(Button); |
917 | download_current_button->set_text(TTR("Download and Install" )); |
918 | download_current_button->set_tooltip_text(TTR("Download and install templates for the current version from the best possible mirror." )); |
919 | download_install_hb->add_child(download_current_button); |
920 | download_current_button->connect("pressed" , callable_mp(this, &ExportTemplateManager::_download_current)); |
921 | |
922 | // Update downloads buttons to prevent unsupported downloads. |
923 | if (!downloads_available) { |
924 | download_current_button->set_disabled(true); |
925 | download_current_button->set_tooltip_text(TTR("Official export templates aren't available for development builds." )); |
926 | } |
927 | |
928 | HBoxContainer *install_file_hb = memnew(HBoxContainer); |
929 | install_file_hb->set_alignment(BoxContainer::ALIGNMENT_END); |
930 | install_options_vb->add_child(install_file_hb); |
931 | |
932 | install_file_button = memnew(Button); |
933 | install_file_button->set_text(TTR("Install from File" )); |
934 | install_file_button->set_tooltip_text(TTR("Install templates from a local file." )); |
935 | install_file_hb->add_child(install_file_button); |
936 | install_file_button->connect("pressed" , callable_mp(this, &ExportTemplateManager::_install_file)); |
937 | |
938 | // Templates are being downloaded; buttons unavailable. |
939 | download_progress_hb = memnew(HBoxContainer); |
940 | download_progress_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
941 | install_templates_hb->add_child(download_progress_hb); |
942 | download_progress_hb->hide(); |
943 | |
944 | download_progress_bar = memnew(ProgressBar); |
945 | download_progress_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
946 | download_progress_bar->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
947 | download_progress_bar->set_min(0); |
948 | download_progress_bar->set_max(1); |
949 | download_progress_bar->set_value(0); |
950 | download_progress_bar->set_step(0.01); |
951 | download_progress_hb->add_child(download_progress_bar); |
952 | |
953 | download_progress_label = memnew(Label); |
954 | download_progress_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
955 | download_progress_hb->add_child(download_progress_label); |
956 | |
957 | Button *download_cancel_button = memnew(Button); |
958 | download_cancel_button->set_text(TTR("Cancel" )); |
959 | download_cancel_button->set_tooltip_text(TTR("Cancel the download of the templates." )); |
960 | download_progress_hb->add_child(download_cancel_button); |
961 | download_cancel_button->connect("pressed" , callable_mp(this, &ExportTemplateManager::_cancel_template_download)); |
962 | |
963 | download_templates = memnew(HTTPRequest); |
964 | install_templates_hb->add_child(download_templates); |
965 | download_templates->connect("request_completed" , callable_mp(this, &ExportTemplateManager::_download_template_completed)); |
966 | |
967 | main_vb->add_child(memnew(HSeparator)); |
968 | |
969 | // Other installed templates table. |
970 | HBoxContainer *installed_versions_hb = memnew(HBoxContainer); |
971 | main_vb->add_child(installed_versions_hb); |
972 | Label *installed_label = memnew(Label); |
973 | installed_label->set_theme_type_variation("HeaderSmall" ); |
974 | installed_label->set_text(TTR("Other Installed Versions:" )); |
975 | installed_versions_hb->add_child(installed_label); |
976 | |
977 | installed_table = memnew(Tree); |
978 | installed_table->set_hide_root(true); |
979 | installed_table->set_custom_minimum_size(Size2(0, 100) * EDSCALE); |
980 | installed_table->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
981 | main_vb->add_child(installed_table); |
982 | installed_table->connect("button_clicked" , callable_mp(this, &ExportTemplateManager::_installed_table_button_cbk)); |
983 | |
984 | // Dialogs. |
985 | uninstall_confirm = memnew(ConfirmationDialog); |
986 | uninstall_confirm->set_title(TTR("Uninstall Template" )); |
987 | add_child(uninstall_confirm); |
988 | uninstall_confirm->connect("confirmed" , callable_mp(this, &ExportTemplateManager::_uninstall_template_confirmed)); |
989 | |
990 | install_file_dialog = memnew(FileDialog); |
991 | install_file_dialog->set_title(TTR("Select Template File" )); |
992 | install_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM); |
993 | install_file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE); |
994 | install_file_dialog->set_current_dir(EditorSettings::get_singleton()->get_meta("export_template_download_directory" , "" )); |
995 | install_file_dialog->add_filter("*.tpz" , TTR("Godot Export Templates" )); |
996 | install_file_dialog->connect("file_selected" , callable_mp(this, &ExportTemplateManager::_install_file_selected).bind(false)); |
997 | add_child(install_file_dialog); |
998 | |
999 | hide_dialog_accept = memnew(AcceptDialog); |
1000 | hide_dialog_accept->set_text(TTR("The templates will continue to download.\nYou may experience a short editor freeze when they finish." )); |
1001 | add_child(hide_dialog_accept); |
1002 | hide_dialog_accept->connect("confirmed" , callable_mp(this, &ExportTemplateManager::_hide_dialog)); |
1003 | } |
1004 | |