export_template_manager.cpp 34 KB

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