export_template_manager.cpp 41 KB

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