export_template_manager.cpp 34 KB

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