export_template_manager.cpp 35 KB

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