export_template_manager.cpp 41 KB

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