export_template_manager.cpp 35 KB

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