export_template_manager.cpp 37 KB

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