export_template_manager.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/json.h"
  32. #include "core/io/zip_io.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/input.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_icon("Folder", "EditorIcons"), OPEN_TEMPLATE_FOLDER, false, TTR("Open the folder containing these templates."));
  95. ti->add_button(0, get_icon("Remove", "EditorIcons"), UNINSTALL_TEMPLATE, false, TTR("Uninstall these templates."));
  96. }
  97. minimum_size_changed();
  98. update();
  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.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 (!mirrors_available && !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(EditorSettings::get_singleton()->get_cache_dir().plus_file("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 PoolStringArray &headers, const PoolByteArray &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. DirAccessRef 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 PoolStringArray &headers, const PoolByteArray &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. PoolByteArray::Read r = p_data.read();
  215. response_json.parse_utf8((const char *)r.ptr(), p_data.size());
  216. }
  217. Variant json;
  218. String errs;
  219. int errline;
  220. Error err = JSON::parse(response_json, json, errs, errline);
  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 data = json;
  233. if (data.has("mirrors")) {
  234. Array mirrors = 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.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_SSL_HANDSHAKE_ERROR:
  304. *r_status = TTR("SSL 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_color_override("font_color", get_color("error_color", "Editor"));
  315. } else {
  316. download_progress_label->add_color_override("font_color", get_color("font_color", "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. // Progress cannot be happening with an error, so make sure that the color is correct.
  324. download_progress_label->add_color_override("font_color", get_color("font_color", "Label"));
  325. }
  326. void ExportTemplateManager::_install_file() {
  327. install_file_dialog->popup_centered_ratio();
  328. }
  329. bool ExportTemplateManager::_install_file_selected(const String &p_file, bool p_skip_progress) {
  330. // unzClose() will take care of closing the file stored in the unzFile,
  331. // so we don't need to `memdelete(fa)` in this method.
  332. FileAccess *fa = nullptr;
  333. zlib_filefunc_def io = zipio_create_io_from_file(&fa);
  334. unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
  335. if (!pkg) {
  336. EditorNode::get_singleton()->show_warning(TTR("Can't open the export templates file."));
  337. return false;
  338. }
  339. int ret = unzGoToFirstFile(pkg);
  340. // Count them and find version.
  341. int fc = 0;
  342. String version;
  343. String contents_dir;
  344. while (ret == UNZ_OK) {
  345. unz_file_info info;
  346. char fname[16384];
  347. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  348. String file = String::utf8(fname);
  349. if (file.ends_with("version.txt")) {
  350. Vector<uint8_t> data;
  351. data.resize(info.uncompressed_size);
  352. // Read.
  353. unzOpenCurrentFile(pkg);
  354. ret = unzReadCurrentFile(pkg, data.ptrw(), data.size());
  355. unzCloseCurrentFile(pkg);
  356. String data_str;
  357. data_str.parse_utf8((const char *)data.ptr(), 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 == String()) {
  375. EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside the export templates file."));
  376. unzClose(pkg);
  377. return false;
  378. }
  379. DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  380. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(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. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  398. String file_path(String::utf8(fname).simplify_path());
  399. String file = file_path.get_file();
  400. if (file.size() == 0) {
  401. ret = unzGoToNextFile(pkg);
  402. continue;
  403. }
  404. Vector<uint8_t> data;
  405. data.resize(info.uncompressed_size);
  406. // Read
  407. unzOpenCurrentFile(pkg);
  408. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  409. unzCloseCurrentFile(pkg);
  410. String base_dir = file_path.get_base_dir().trim_suffix("/");
  411. if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) {
  412. base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/");
  413. file = base_dir.plus_file(file);
  414. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  415. ERR_CONTINUE(!da);
  416. String output_dir = template_path.plus_file(base_dir);
  417. if (!DirAccess::exists(output_dir)) {
  418. Error mkdir_err = da->make_dir_recursive(output_dir);
  419. ERR_CONTINUE(mkdir_err != OK);
  420. }
  421. }
  422. if (p) {
  423. p->step(TTR("Importing:") + " " + file, fc);
  424. }
  425. String to_write = template_path.plus_file(file);
  426. FileAccessRef f = FileAccess::open(to_write, FileAccess::WRITE);
  427. if (!f) {
  428. ret = unzGoToNextFile(pkg);
  429. fc++;
  430. ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
  431. }
  432. f->store_buffer(data.ptr(), data.size());
  433. #ifndef WINDOWS_ENABLED
  434. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  435. #endif
  436. ret = unzGoToNextFile(pkg);
  437. fc++;
  438. }
  439. if (p) {
  440. memdelete(p);
  441. }
  442. unzClose(pkg);
  443. _update_template_status();
  444. return true;
  445. }
  446. void ExportTemplateManager::_uninstall_template(const String &p_version) {
  447. uninstall_confirm->set_text(vformat(TTR("Remove templates for the version '%s'?"), p_version));
  448. uninstall_confirm->popup_centered();
  449. uninstall_version = p_version;
  450. }
  451. void ExportTemplateManager::_uninstall_template_confirmed() {
  452. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  453. const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
  454. Error err = da->change_dir(templates_dir);
  455. ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
  456. err = da->change_dir(uninstall_version);
  457. ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.plus_file(uninstall_version) + "'.");
  458. err = da->erase_contents_recursive();
  459. ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.plus_file(uninstall_version) + "'.");
  460. da->change_dir("..");
  461. err = da->remove(uninstall_version);
  462. ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.plus_file(uninstall_version) + "'.");
  463. _update_template_status();
  464. }
  465. String ExportTemplateManager::_get_selected_mirror() const {
  466. if (mirrors_list->get_item_count() == 1) {
  467. return "";
  468. }
  469. int selected = mirrors_list->get_selected_id();
  470. if (selected == 0) {
  471. // This is a special "best available" value; so pick the first available mirror from the rest of the list.
  472. selected = 1;
  473. }
  474. return mirrors_list->get_item_metadata(selected);
  475. }
  476. void ExportTemplateManager::_mirror_options_button_cbk(int p_id) {
  477. switch (p_id) {
  478. case VISIT_WEB_MIRROR: {
  479. String mirror_url = _get_selected_mirror();
  480. if (mirror_url.empty()) {
  481. EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
  482. return;
  483. }
  484. OS::get_singleton()->shell_open(mirror_url);
  485. } break;
  486. case COPY_MIRROR_URL: {
  487. String mirror_url = _get_selected_mirror();
  488. if (mirror_url.empty()) {
  489. EditorNode::get_singleton()->show_warning(TTR("There are no mirrors available."));
  490. return;
  491. }
  492. OS::get_singleton()->set_clipboard(mirror_url);
  493. } break;
  494. }
  495. }
  496. void ExportTemplateManager::_installed_table_button_cbk(Object *p_item, int p_column, int p_id) {
  497. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  498. if (!ti) {
  499. return;
  500. }
  501. switch (p_id) {
  502. case OPEN_TEMPLATE_FOLDER: {
  503. String version_string = ti->get_text(0);
  504. _open_template_folder(version_string);
  505. } break;
  506. case UNINSTALL_TEMPLATE: {
  507. String version_string = ti->get_text(0);
  508. _uninstall_template(version_string);
  509. } break;
  510. }
  511. }
  512. void ExportTemplateManager::_open_template_folder(const String &p_version) {
  513. const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
  514. OS::get_singleton()->shell_open("file://" + templates_dir.plus_file(p_version));
  515. }
  516. void ExportTemplateManager::popup_manager() {
  517. _update_template_status();
  518. _refresh_mirrors();
  519. popup_centered(Size2(720, 280) * EDSCALE);
  520. }
  521. void ExportTemplateManager::ok_pressed() {
  522. if (!is_downloading_templates) {
  523. hide();
  524. return;
  525. }
  526. hide_dialog_accept->popup_centered();
  527. }
  528. void ExportTemplateManager::cancel_pressed() {
  529. // This won't stop the window from closing, but will show the alert if the download is active.
  530. ok_pressed();
  531. }
  532. void ExportTemplateManager::_hide_dialog() {
  533. hide();
  534. }
  535. bool ExportTemplateManager::can_install_android_template() {
  536. const String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
  537. return FileAccess::exists(templates_dir.plus_file("android_source.zip"));
  538. }
  539. Error ExportTemplateManager::install_android_template() {
  540. const String &templates_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
  541. const String &source_zip = templates_path.plus_file("android_source.zip");
  542. ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
  543. return install_android_template_from_file(source_zip);
  544. }
  545. Error ExportTemplateManager::install_android_template_from_file(const String &p_file) {
  546. // To support custom Android builds, we install the Java source code and buildsystem
  547. // from android_source.zip to the project's res://android folder.
  548. DirAccessRef da = DirAccess::open("res://");
  549. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  550. // Make res://android dir (if it does not exist).
  551. da->make_dir("android");
  552. {
  553. // Add version, to ensure building won't work if template and Godot version don't match.
  554. FileAccessRef f = FileAccess::open("res://android/.build_version", FileAccess::WRITE);
  555. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  556. f->store_line(VERSION_FULL_CONFIG);
  557. f->close();
  558. }
  559. // Create the android plugins directory.
  560. Error err = da->make_dir_recursive("android/plugins");
  561. ERR_FAIL_COND_V(err != OK, err);
  562. err = da->make_dir_recursive("android/build");
  563. ERR_FAIL_COND_V(err != OK, err);
  564. {
  565. // Add an empty .gdignore file to avoid scan.
  566. FileAccessRef f = FileAccess::open("res://android/build/.gdignore", FileAccess::WRITE);
  567. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  568. f->store_line("");
  569. f->close();
  570. }
  571. // Uncompress source template.
  572. FileAccess *src_f = nullptr;
  573. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  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. Set<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. String path = String::utf8(fpath);
  593. String base_dir = path.get_base_dir();
  594. if (!path.ends_with("/")) {
  595. Vector<uint8_t> data;
  596. data.resize(info.uncompressed_size);
  597. // Read.
  598. unzOpenCurrentFile(pkg);
  599. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  600. unzCloseCurrentFile(pkg);
  601. if (!dirs_tested.has(base_dir)) {
  602. da->make_dir_recursive(String("android/build").plus_file(base_dir));
  603. dirs_tested.insert(base_dir);
  604. }
  605. String to_write = String("res://android/build").plus_file(path);
  606. FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
  607. if (f) {
  608. f->store_buffer(data.ptr(), data.size());
  609. memdelete(f);
  610. #ifndef WINDOWS_ENABLED
  611. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  612. #endif
  613. } else {
  614. ERR_PRINT("Can't uncompress file: " + to_write);
  615. }
  616. }
  617. ProgressDialog::get_singleton()->task_step("uncompress_src", path, idx);
  618. idx++;
  619. ret = unzGoToNextFile(pkg);
  620. }
  621. ProgressDialog::get_singleton()->end_task("uncompress_src");
  622. unzClose(pkg);
  623. return OK;
  624. }
  625. void ExportTemplateManager::_notification(int p_what) {
  626. switch (p_what) {
  627. case NOTIFICATION_ENTER_TREE:
  628. case NOTIFICATION_THEME_CHANGED: {
  629. current_value->add_font_override("font", get_font("bold", "EditorFonts"));
  630. current_missing_label->add_color_override("font_color", get_color("error_color", "Editor"));
  631. current_installed_label->add_color_override("font_color", get_color("disabled_font_color", "Editor"));
  632. mirror_options_button->set_icon(get_icon("GuiTabMenuHl", "EditorIcons"));
  633. } break;
  634. case NOTIFICATION_VISIBILITY_CHANGED: {
  635. if (!is_visible()) {
  636. set_process(false);
  637. } else if (is_visible() && is_downloading_templates) {
  638. set_process(true);
  639. }
  640. } break;
  641. case NOTIFICATION_PROCESS: {
  642. update_countdown -= get_process_delta_time();
  643. if (update_countdown > 0) {
  644. return;
  645. }
  646. update_countdown = 0.5;
  647. String status;
  648. int downloaded_bytes;
  649. int total_bytes;
  650. bool success = _humanize_http_status(download_templates, &status, &downloaded_bytes, &total_bytes);
  651. if (downloaded_bytes >= 0) {
  652. if (total_bytes > 0) {
  653. _set_current_progress_value(float(downloaded_bytes) / total_bytes, status);
  654. } else {
  655. _set_current_progress_value(0, status);
  656. }
  657. } else {
  658. _set_current_progress_status(status);
  659. }
  660. if (!success) {
  661. set_process(false);
  662. }
  663. } break;
  664. }
  665. }
  666. void ExportTemplateManager::_bind_methods() {
  667. ClassDB::bind_method("_hide_dialog", &ExportTemplateManager::_hide_dialog);
  668. ClassDB::bind_method("_open_template_folder", &ExportTemplateManager::_open_template_folder);
  669. ClassDB::bind_method("_refresh_mirrors_completed", &ExportTemplateManager::_refresh_mirrors_completed);
  670. ClassDB::bind_method("_mirror_options_button_cbk", &ExportTemplateManager::_mirror_options_button_cbk);
  671. ClassDB::bind_method("_download_template", &ExportTemplateManager::_download_template);
  672. ClassDB::bind_method("_download_current", &ExportTemplateManager::_download_current);
  673. ClassDB::bind_method("_cancel_template_download", &ExportTemplateManager::_cancel_template_download);
  674. ClassDB::bind_method("_download_template_completed", &ExportTemplateManager::_download_template_completed);
  675. ClassDB::bind_method("_uninstall_template", &ExportTemplateManager::_uninstall_template);
  676. ClassDB::bind_method("_uninstall_template_confirmed", &ExportTemplateManager::_uninstall_template_confirmed);
  677. ClassDB::bind_method("_install_file", &ExportTemplateManager::_install_file);
  678. ClassDB::bind_method("_installed_table_button_cbk", &ExportTemplateManager::_installed_table_button_cbk);
  679. ClassDB::bind_method("_install_file_selected", &ExportTemplateManager::_install_file_selected);
  680. }
  681. ExportTemplateManager::ExportTemplateManager() {
  682. set_title(TTR("Export Template Manager"));
  683. set_hide_on_ok(false);
  684. get_ok()->set_text(TTR("Close"));
  685. // Downloadable export templates are only available for stable and official alpha/beta/RC builds
  686. // (which always have a number following their status, e.g. "alpha1").
  687. // Therefore, don't display download-related features when using a development version
  688. // (whose builds aren't numbered).
  689. downloads_available =
  690. String(VERSION_STATUS) != String("dev") &&
  691. String(VERSION_STATUS) != String("alpha") &&
  692. String(VERSION_STATUS) != String("beta") &&
  693. String(VERSION_STATUS) != String("rc");
  694. VBoxContainer *main_vb = memnew(VBoxContainer);
  695. add_child(main_vb);
  696. // Current version controls.
  697. HBoxContainer *current_hb = memnew(HBoxContainer);
  698. main_vb->add_child(current_hb);
  699. Label *current_label = memnew(Label);
  700. current_label->set_text(TTR("Current Version:"));
  701. current_hb->add_child(current_label);
  702. current_value = memnew(Label);
  703. current_hb->add_child(current_value);
  704. // Current version statuses.
  705. // Status: Current version is missing.
  706. current_missing_label = memnew(Label);
  707. current_missing_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  708. current_missing_label->set_align(Label::ALIGN_RIGHT);
  709. current_missing_label->set_text(TTR("Export templates are missing. Download them or install from a file."));
  710. current_hb->add_child(current_missing_label);
  711. // Status: Current version is installed.
  712. current_installed_label = memnew(Label);
  713. current_installed_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  714. current_installed_label->set_align(Label::ALIGN_RIGHT);
  715. current_installed_label->set_text(TTR("Export templates are installed and ready to be used."));
  716. current_hb->add_child(current_installed_label);
  717. current_installed_label->hide();
  718. // Currently installed template.
  719. current_installed_hb = memnew(HBoxContainer);
  720. main_vb->add_child(current_installed_hb);
  721. current_installed_path = memnew(LineEdit);
  722. current_installed_path->set_editable(false);
  723. current_installed_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  724. current_installed_hb->add_child(current_installed_path);
  725. current_open_button = memnew(Button);
  726. current_open_button->set_text(TTR("Open Folder"));
  727. current_open_button->set_tooltip(TTR("Open the folder containing installed templates for the current version."));
  728. current_installed_hb->add_child(current_open_button);
  729. current_open_button->connect("pressed", this, "_open_template_folder", varray(VERSION_FULL_CONFIG));
  730. current_uninstall_button = memnew(Button);
  731. current_uninstall_button->set_text(TTR("Uninstall"));
  732. current_uninstall_button->set_tooltip(TTR("Uninstall templates for the current version."));
  733. current_installed_hb->add_child(current_uninstall_button);
  734. current_uninstall_button->connect("pressed", this, "_uninstall_template", varray(VERSION_FULL_CONFIG));
  735. main_vb->add_child(memnew(HSeparator));
  736. // Download and install section.
  737. HBoxContainer *install_templates_hb = memnew(HBoxContainer);
  738. main_vb->add_child(install_templates_hb);
  739. // Download and install buttons are available.
  740. install_options_vb = memnew(VBoxContainer);
  741. install_options_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  742. install_templates_hb->add_child(install_options_vb);
  743. HBoxContainer *download_install_hb = memnew(HBoxContainer);
  744. install_options_vb->add_child(download_install_hb);
  745. Label *mirrors_label = memnew(Label);
  746. mirrors_label->set_text(TTR("Download from:"));
  747. download_install_hb->add_child(mirrors_label);
  748. mirrors_list = memnew(OptionButton);
  749. mirrors_list->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
  750. download_install_hb->add_child(mirrors_list);
  751. mirrors_list->add_item(TTR("Best available mirror"), 0);
  752. request_mirrors = memnew(HTTPRequest);
  753. mirrors_list->add_child(request_mirrors);
  754. request_mirrors->connect("request_completed", this, "_refresh_mirrors_completed");
  755. mirror_options_button = memnew(MenuButton);
  756. mirror_options_button->get_popup()->add_item(TTR("Open in Web Browser"), VISIT_WEB_MIRROR);
  757. mirror_options_button->get_popup()->add_item(TTR("Copy Mirror URL"), COPY_MIRROR_URL);
  758. download_install_hb->add_child(mirror_options_button);
  759. mirror_options_button->get_popup()->connect("id_pressed", this, "_mirror_options_button_cbk");
  760. download_install_hb->add_spacer();
  761. Button *download_current_button = memnew(Button);
  762. download_current_button->set_text(TTR("Download and Install"));
  763. download_current_button->set_tooltip(TTR("Download and install templates for the current version from the best possible mirror."));
  764. download_install_hb->add_child(download_current_button);
  765. download_current_button->connect("pressed", this, "_download_current");
  766. // Update downloads buttons to prevent unsupported downloads.
  767. if (!downloads_available) {
  768. download_current_button->set_disabled(true);
  769. download_current_button->set_tooltip(TTR("Official export templates aren't available for development builds."));
  770. }
  771. HBoxContainer *install_file_hb = memnew(HBoxContainer);
  772. install_file_hb->set_alignment(BoxContainer::ALIGN_END);
  773. install_options_vb->add_child(install_file_hb);
  774. install_file_button = memnew(Button);
  775. install_file_button->set_text(TTR("Install from File"));
  776. install_file_button->set_tooltip(TTR("Install templates from a local file."));
  777. install_file_hb->add_child(install_file_button);
  778. install_file_button->connect("pressed", this, "_install_file");
  779. // Templates are being downloaded; buttons unavailable.
  780. download_progress_hb = memnew(HBoxContainer);
  781. download_progress_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  782. install_templates_hb->add_child(download_progress_hb);
  783. download_progress_hb->hide();
  784. download_progress_bar = memnew(ProgressBar);
  785. download_progress_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  786. download_progress_bar->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  787. download_progress_bar->set_min(0);
  788. download_progress_bar->set_max(1);
  789. download_progress_bar->set_value(0);
  790. download_progress_bar->set_step(0.01);
  791. download_progress_hb->add_child(download_progress_bar);
  792. download_progress_label = memnew(Label);
  793. download_progress_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  794. download_progress_label->set_clip_text(true);
  795. download_progress_hb->add_child(download_progress_label);
  796. Button *download_cancel_button = memnew(Button);
  797. download_cancel_button->set_text(TTR("Cancel"));
  798. download_cancel_button->set_tooltip(TTR("Cancel the download of the templates."));
  799. download_progress_hb->add_child(download_cancel_button);
  800. download_cancel_button->connect("pressed", this, "_cancel_template_download");
  801. download_templates = memnew(HTTPRequest);
  802. install_templates_hb->add_child(download_templates);
  803. download_templates->connect("request_completed", this, "_download_template_completed");
  804. main_vb->add_child(memnew(HSeparator));
  805. // Other installed templates table.
  806. HBoxContainer *installed_versions_hb = memnew(HBoxContainer);
  807. main_vb->add_child(installed_versions_hb);
  808. Label *installed_label = memnew(Label);
  809. installed_label->set_text(TTR("Other Installed Versions:"));
  810. installed_versions_hb->add_child(installed_label);
  811. installed_table = memnew(Tree);
  812. installed_table->set_hide_root(true);
  813. installed_table->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  814. installed_table->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  815. main_vb->add_child(installed_table);
  816. installed_table->connect("button_pressed", this, "_installed_table_button_cbk");
  817. // Dialogs.
  818. uninstall_confirm = memnew(ConfirmationDialog);
  819. uninstall_confirm->set_title(TTR("Uninstall Template"));
  820. add_child(uninstall_confirm);
  821. uninstall_confirm->connect("confirmed", this, "_uninstall_template_confirmed");
  822. install_file_dialog = memnew(FileDialog);
  823. install_file_dialog->set_title(TTR("Select Template File"));
  824. install_file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  825. install_file_dialog->set_mode(FileDialog::MODE_OPEN_FILE);
  826. install_file_dialog->add_filter("*.tpz ; " + TTR("Godot Export Templates"));
  827. install_file_dialog->connect("file_selected", this, "_install_file_selected", varray(false));
  828. add_child(install_file_dialog);
  829. hide_dialog_accept = memnew(AcceptDialog);
  830. hide_dialog_accept->set_text(TTR("The templates will continue to download.\nYou may experience a short editor freeze when they finish."));
  831. add_child(hide_dialog_accept);
  832. hide_dialog_accept->connect("confirmed", this, "_hide_dialog");
  833. }