export_template_manager.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*************************************************************************/
  2. /* export_template_manager.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export_template_manager.h"
  31. #include "core/input/input.h"
  32. #include "core/io/json.h"
  33. #include "core/io/zip_io.h"
  34. #include "core/os/dir_access.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_list() {
  42. while (current_hb->get_child_count()) {
  43. memdelete(current_hb->get_child(0));
  44. }
  45. while (installed_vb->get_child_count()) {
  46. memdelete(installed_vb->get_child(0));
  47. }
  48. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  49. Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
  50. Set<String> templates;
  51. d->list_dir_begin();
  52. if (err == OK) {
  53. String c = d->get_next();
  54. while (c != String()) {
  55. if (d->current_is_dir() && !c.begins_with(".")) {
  56. templates.insert(c);
  57. }
  58. c = d->get_next();
  59. }
  60. }
  61. d->list_dir_end();
  62. memdelete(d);
  63. String current_version = VERSION_FULL_CONFIG;
  64. // Downloadable export templates are only available for stable and official alpha/beta/RC builds
  65. // (which always have a number following their status, e.g. "alpha1").
  66. // Therefore, don't display download-related features when using a development version
  67. // (whose builds aren't numbered).
  68. const bool downloads_available =
  69. String(VERSION_STATUS) != String("dev") &&
  70. String(VERSION_STATUS) != String("alpha") &&
  71. String(VERSION_STATUS) != String("beta") &&
  72. String(VERSION_STATUS) != String("rc");
  73. Label *current = memnew(Label);
  74. current->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  75. current_hb->add_child(current);
  76. if (templates.has(current_version)) {
  77. current->add_theme_color_override("font_color", current->get_theme_color("success_color", "Editor"));
  78. // Only display a redownload button if it can be downloaded in the first place
  79. if (downloads_available) {
  80. Button *redownload = memnew(Button);
  81. redownload->set_text(TTR("Redownload"));
  82. current_hb->add_child(redownload);
  83. redownload->connect("pressed", callable_mp(this, &ExportTemplateManager::_download_template), varray(current_version));
  84. }
  85. Button *uninstall = memnew(Button);
  86. uninstall->set_text(TTR("Uninstall"));
  87. current_hb->add_child(uninstall);
  88. current->set_text(current_version + " " + TTR("(Installed)"));
  89. uninstall->connect("pressed", callable_mp(this, &ExportTemplateManager::_uninstall_template), varray(current_version));
  90. } else {
  91. current->add_theme_color_override("font_color", current->get_theme_color("error_color", "Editor"));
  92. Button *redownload = memnew(Button);
  93. redownload->set_text(TTR("Download"));
  94. if (!downloads_available) {
  95. redownload->set_disabled(true);
  96. redownload->set_tooltip(TTR("Official export templates aren't available for development builds."));
  97. }
  98. redownload->connect("pressed", callable_mp(this, &ExportTemplateManager::_download_template), varray(current_version));
  99. current_hb->add_child(redownload);
  100. current->set_text(current_version + " " + TTR("(Missing)"));
  101. }
  102. for (Set<String>::Element *E = templates.back(); E; E = E->prev()) {
  103. HBoxContainer *hbc = memnew(HBoxContainer);
  104. Label *version = memnew(Label);
  105. version->set_modulate(current->get_theme_color("disabled_font_color", "Editor"));
  106. String text = E->get();
  107. if (text == current_version) {
  108. text += " " + TTR("(Current)");
  109. }
  110. version->set_text(text);
  111. version->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  112. hbc->add_child(version);
  113. Button *uninstall = memnew(Button);
  114. uninstall->set_text(TTR("Uninstall"));
  115. hbc->add_child(uninstall);
  116. uninstall->connect("pressed", callable_mp(this, &ExportTemplateManager::_uninstall_template), varray(E->get()));
  117. installed_vb->add_child(hbc);
  118. }
  119. }
  120. void ExportTemplateManager::_download_template(const String &p_version) {
  121. while (template_list->get_child_count()) {
  122. memdelete(template_list->get_child(0));
  123. }
  124. template_downloader->popup_centered();
  125. template_list_state->set_text(TTR("Retrieving mirrors, please wait..."));
  126. template_download_progress->set_max(100);
  127. template_download_progress->set_value(0);
  128. request_mirror->request("https://godotengine.org/mirrorlist/" + p_version + ".json");
  129. template_list_state->show();
  130. template_download_progress->show();
  131. }
  132. void ExportTemplateManager::_uninstall_template(const String &p_version) {
  133. remove_confirm->set_text(vformat(TTR("Remove template version '%s'?"), p_version));
  134. remove_confirm->popup_centered();
  135. to_remove = p_version;
  136. }
  137. void ExportTemplateManager::_uninstall_template_confirm() {
  138. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  139. const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
  140. Error err = da->change_dir(templates_dir);
  141. ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
  142. err = da->change_dir(to_remove);
  143. ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
  144. err = da->erase_contents_recursive();
  145. ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.plus_file(to_remove) + "'.");
  146. da->change_dir("..");
  147. err = da->remove(to_remove);
  148. ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
  149. _update_template_list();
  150. }
  151. bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_progress) {
  152. // unzClose() will take care of closing the file stored in the unzFile,
  153. // so we don't need to `memdelete(fa)` in this method.
  154. FileAccess *fa = nullptr;
  155. zlib_filefunc_def io = zipio_create_io_from_file(&fa);
  156. unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
  157. if (!pkg) {
  158. EditorNode::get_singleton()->show_warning(TTR("Can't open export templates zip."));
  159. return false;
  160. }
  161. int ret = unzGoToFirstFile(pkg);
  162. int fc = 0; //count them and find version
  163. String version;
  164. String contents_dir;
  165. while (ret == UNZ_OK) {
  166. unz_file_info info;
  167. char fname[16384];
  168. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  169. String file = fname;
  170. if (file.ends_with("version.txt")) {
  171. Vector<uint8_t> data;
  172. data.resize(info.uncompressed_size);
  173. //read
  174. unzOpenCurrentFile(pkg);
  175. ret = unzReadCurrentFile(pkg, data.ptrw(), data.size());
  176. unzCloseCurrentFile(pkg);
  177. String data_str;
  178. data_str.parse_utf8((const char *)data.ptr(), data.size());
  179. data_str = data_str.strip_edges();
  180. // Version number should be of the form major.minor[.patch].status[.module_config]
  181. // so it can in theory have 3 or more slices.
  182. if (data_str.get_slice_count(".") < 3) {
  183. EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside templates: %s."), data_str));
  184. unzClose(pkg);
  185. return false;
  186. }
  187. version = data_str;
  188. contents_dir = file.get_base_dir().trim_suffix("/").trim_suffix("\\");
  189. }
  190. if (file.get_file().size() != 0) {
  191. fc++;
  192. }
  193. ret = unzGoToNextFile(pkg);
  194. }
  195. if (version == String()) {
  196. EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside templates."));
  197. unzClose(pkg);
  198. return false;
  199. }
  200. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(version);
  201. DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  202. Error err = d->make_dir_recursive(template_path);
  203. if (err != OK) {
  204. EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:") + "\n" + template_path);
  205. unzClose(pkg);
  206. return false;
  207. }
  208. ret = unzGoToFirstFile(pkg);
  209. EditorProgress *p = nullptr;
  210. if (p_use_progress) {
  211. p = memnew(EditorProgress("ltask", TTR("Extracting Export Templates"), fc));
  212. }
  213. fc = 0;
  214. while (ret == UNZ_OK) {
  215. //get filename
  216. unz_file_info info;
  217. char fname[16384];
  218. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  219. String file_path(String(fname).simplify_path());
  220. String file = file_path.get_file();
  221. if (file.size() == 0) {
  222. ret = unzGoToNextFile(pkg);
  223. continue;
  224. }
  225. Vector<uint8_t> data;
  226. data.resize(info.uncompressed_size);
  227. //read
  228. unzOpenCurrentFile(pkg);
  229. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  230. unzCloseCurrentFile(pkg);
  231. String base_dir = file_path.get_base_dir().trim_suffix("/");
  232. if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) {
  233. base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/");
  234. file = base_dir.plus_file(file);
  235. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  236. ERR_CONTINUE(!da);
  237. String output_dir = template_path.plus_file(base_dir);
  238. if (!DirAccess::exists(output_dir)) {
  239. Error mkdir_err = da->make_dir_recursive(output_dir);
  240. ERR_CONTINUE(mkdir_err != OK);
  241. }
  242. }
  243. if (p) {
  244. p->step(TTR("Importing:") + " " + file, fc);
  245. }
  246. String to_write = template_path.plus_file(file);
  247. FileAccessRef f = FileAccess::open(to_write, FileAccess::WRITE);
  248. if (!f) {
  249. ret = unzGoToNextFile(pkg);
  250. fc++;
  251. ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
  252. }
  253. f->store_buffer(data.ptr(), data.size());
  254. #ifndef WINDOWS_ENABLED
  255. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  256. #endif
  257. ret = unzGoToNextFile(pkg);
  258. fc++;
  259. }
  260. if (p) {
  261. memdelete(p);
  262. }
  263. unzClose(pkg);
  264. _update_template_list();
  265. return true;
  266. }
  267. void ExportTemplateManager::popup_manager() {
  268. _update_template_list();
  269. popup_centered(Size2(400, 400) * EDSCALE);
  270. }
  271. void ExportTemplateManager::ok_pressed() {
  272. template_open->popup_centered_ratio();
  273. }
  274. void ExportTemplateManager::_http_download_mirror_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  275. if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
  276. EditorNode::get_singleton()->show_warning(TTR("Error getting the list of mirrors."));
  277. return;
  278. }
  279. String mirror_str;
  280. {
  281. const uint8_t *r = p_data.ptr();
  282. mirror_str.parse_utf8((const char *)r, p_data.size());
  283. }
  284. template_list_state->hide();
  285. template_download_progress->hide();
  286. Variant r;
  287. String errs;
  288. int errline;
  289. Error err = JSON::parse(mirror_str, r, errs, errline);
  290. if (err != OK) {
  291. EditorNode::get_singleton()->show_warning(TTR("Error parsing JSON of mirror list. Please report this issue!"));
  292. return;
  293. }
  294. bool mirrors_found = false;
  295. Dictionary d = r;
  296. if (d.has("mirrors")) {
  297. Array mirrors = d["mirrors"];
  298. for (int i = 0; i < mirrors.size(); i++) {
  299. Dictionary m = mirrors[i];
  300. ERR_CONTINUE(!m.has("url") || !m.has("name"));
  301. LinkButton *lb = memnew(LinkButton);
  302. lb->set_text(m["name"]);
  303. lb->connect("pressed", callable_mp(this, &ExportTemplateManager::_begin_template_download), varray(m["url"]));
  304. template_list->add_child(lb);
  305. mirrors_found = true;
  306. }
  307. }
  308. if (!mirrors_found) {
  309. EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
  310. return;
  311. }
  312. }
  313. void ExportTemplateManager::_http_download_templates_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  314. switch (p_status) {
  315. case HTTPRequest::RESULT_CANT_RESOLVE: {
  316. template_list_state->set_text(TTR("Can't resolve."));
  317. } break;
  318. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  319. case HTTPRequest::RESULT_CONNECTION_ERROR:
  320. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
  321. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  322. case HTTPRequest::RESULT_CANT_CONNECT: {
  323. template_list_state->set_text(TTR("Can't connect."));
  324. } break;
  325. case HTTPRequest::RESULT_NO_RESPONSE: {
  326. template_list_state->set_text(TTR("No response."));
  327. } break;
  328. case HTTPRequest::RESULT_REQUEST_FAILED: {
  329. template_list_state->set_text(TTR("Request Failed."));
  330. } break;
  331. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  332. template_list_state->set_text(TTR("Redirect Loop."));
  333. } break;
  334. default: {
  335. if (p_code != 200) {
  336. template_list_state->set_text(TTR("Failed:") + " " + itos(p_code));
  337. } else {
  338. String path = download_templates->get_download_file();
  339. template_list_state->set_text(TTR("Download Complete."));
  340. template_downloader->hide();
  341. bool ret = _install_from_file(path, false);
  342. if (ret) {
  343. // Clean up downloaded file.
  344. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  345. Error err = da->remove(path);
  346. if (err != OK) {
  347. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove temporary file:") + "\n" + path + "\n");
  348. }
  349. } else {
  350. EditorNode::get_singleton()->add_io_error(vformat(TTR("Templates installation failed.\nThe problematic templates archives can be found at '%s'."), path));
  351. }
  352. }
  353. } break;
  354. }
  355. set_process(false);
  356. }
  357. void ExportTemplateManager::_begin_template_download(const String &p_url) {
  358. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  359. OS::get_singleton()->shell_open(p_url);
  360. return;
  361. }
  362. for (int i = 0; i < template_list->get_child_count(); i++) {
  363. BaseButton *b = Object::cast_to<BaseButton>(template_list->get_child(0));
  364. if (b) {
  365. b->set_disabled(true);
  366. }
  367. }
  368. download_data.clear();
  369. download_templates->set_download_file(EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_templates.tpz"));
  370. download_templates->set_use_threads(true);
  371. Error err = download_templates->request(p_url);
  372. if (err != OK) {
  373. EditorNode::get_singleton()->show_warning(TTR("Error requesting URL:") + " " + p_url);
  374. return;
  375. }
  376. set_process(true);
  377. template_list_state->show();
  378. template_download_progress->set_max(100);
  379. template_download_progress->set_value(0);
  380. template_download_progress->show();
  381. template_list_state->set_text(TTR("Connecting to Mirror..."));
  382. }
  383. void ExportTemplateManager::_window_template_downloader_closed() {
  384. download_templates->cancel_request();
  385. }
  386. void ExportTemplateManager::_notification(int p_what) {
  387. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  388. if (!is_visible()) {
  389. set_process(false);
  390. }
  391. }
  392. if (p_what == NOTIFICATION_PROCESS) {
  393. update_countdown -= get_process_delta_time();
  394. if (update_countdown > 0) {
  395. return;
  396. }
  397. update_countdown = 0.5;
  398. String status;
  399. bool errored = false;
  400. switch (download_templates->get_http_client_status()) {
  401. case HTTPClient::STATUS_DISCONNECTED:
  402. status = TTR("Disconnected");
  403. errored = true;
  404. break;
  405. case HTTPClient::STATUS_RESOLVING:
  406. status = TTR("Resolving");
  407. break;
  408. case HTTPClient::STATUS_CANT_RESOLVE:
  409. status = TTR("Can't Resolve");
  410. errored = true;
  411. break;
  412. case HTTPClient::STATUS_CONNECTING:
  413. status = TTR("Connecting...");
  414. break;
  415. case HTTPClient::STATUS_CANT_CONNECT:
  416. status = TTR("Can't Connect");
  417. errored = true;
  418. break;
  419. case HTTPClient::STATUS_CONNECTED:
  420. status = TTR("Connected");
  421. break;
  422. case HTTPClient::STATUS_REQUESTING:
  423. status = TTR("Requesting...");
  424. break;
  425. case HTTPClient::STATUS_BODY:
  426. status = TTR("Downloading");
  427. if (download_templates->get_body_size() > 0) {
  428. status += " " + String::humanize_size(download_templates->get_downloaded_bytes()) + "/" + String::humanize_size(download_templates->get_body_size());
  429. template_download_progress->set_max(download_templates->get_body_size());
  430. template_download_progress->set_value(download_templates->get_downloaded_bytes());
  431. } else {
  432. status += " " + String::humanize_size(download_templates->get_downloaded_bytes());
  433. }
  434. break;
  435. case HTTPClient::STATUS_CONNECTION_ERROR:
  436. status = TTR("Connection Error");
  437. errored = true;
  438. break;
  439. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR:
  440. status = TTR("SSL Handshake Error");
  441. errored = true;
  442. break;
  443. }
  444. template_list_state->set_text(status);
  445. if (errored) {
  446. set_process(false);
  447. }
  448. }
  449. }
  450. bool ExportTemplateManager::can_install_android_template() {
  451. const String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
  452. return FileAccess::exists(templates_dir.plus_file("android_source.zip"));
  453. }
  454. Error ExportTemplateManager::install_android_template() {
  455. // To support custom Android builds, we install the Java source code and buildsystem
  456. // from android_source.zip to the project's res://android folder.
  457. DirAccessRef da = DirAccess::open("res://");
  458. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  459. // Make res://android dir (if it does not exist).
  460. da->make_dir("android");
  461. {
  462. // Add version, to ensure building won't work if template and Godot version don't match.
  463. FileAccessRef f = FileAccess::open("res://android/.build_version", FileAccess::WRITE);
  464. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  465. f->store_line(VERSION_FULL_CONFIG);
  466. f->close();
  467. }
  468. // Create the android plugins directory.
  469. Error err = da->make_dir_recursive("android/plugins");
  470. ERR_FAIL_COND_V(err != OK, err);
  471. err = da->make_dir_recursive("android/build");
  472. ERR_FAIL_COND_V(err != OK, err);
  473. {
  474. // Add an empty .gdignore file to avoid scan.
  475. FileAccessRef f = FileAccess::open("res://android/build/.gdignore", FileAccess::WRITE);
  476. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  477. f->store_line("");
  478. f->close();
  479. }
  480. // Uncompress source template.
  481. const String &templates_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
  482. const String &source_zip = templates_path.plus_file("android_source.zip");
  483. ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
  484. FileAccess *src_f = nullptr;
  485. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  486. unzFile pkg = unzOpen2(source_zip.utf8().get_data(), &io);
  487. ERR_FAIL_COND_V_MSG(!pkg, ERR_CANT_OPEN, "Android sources not in ZIP format.");
  488. int ret = unzGoToFirstFile(pkg);
  489. int total_files = 0;
  490. // Count files to unzip.
  491. while (ret == UNZ_OK) {
  492. total_files++;
  493. ret = unzGoToNextFile(pkg);
  494. }
  495. ret = unzGoToFirstFile(pkg);
  496. ProgressDialog::get_singleton()->add_task("uncompress_src", TTR("Uncompressing Android Build Sources"), total_files);
  497. Set<String> dirs_tested;
  498. int idx = 0;
  499. while (ret == UNZ_OK) {
  500. // Get file path.
  501. unz_file_info info;
  502. char fpath[16384];
  503. ret = unzGetCurrentFileInfo(pkg, &info, fpath, 16384, nullptr, 0, nullptr, 0);
  504. String path = fpath;
  505. String base_dir = path.get_base_dir();
  506. if (!path.ends_with("/")) {
  507. Vector<uint8_t> data;
  508. data.resize(info.uncompressed_size);
  509. // Read.
  510. unzOpenCurrentFile(pkg);
  511. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  512. unzCloseCurrentFile(pkg);
  513. if (!dirs_tested.has(base_dir)) {
  514. da->make_dir_recursive(String("android/build").plus_file(base_dir));
  515. dirs_tested.insert(base_dir);
  516. }
  517. String to_write = String("res://android/build").plus_file(path);
  518. FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
  519. if (f) {
  520. f->store_buffer(data.ptr(), data.size());
  521. memdelete(f);
  522. #ifndef WINDOWS_ENABLED
  523. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  524. #endif
  525. } else {
  526. ERR_PRINT("Can't uncompress file: " + to_write);
  527. }
  528. }
  529. ProgressDialog::get_singleton()->task_step("uncompress_src", path, idx);
  530. idx++;
  531. ret = unzGoToNextFile(pkg);
  532. }
  533. ProgressDialog::get_singleton()->end_task("uncompress_src");
  534. unzClose(pkg);
  535. return OK;
  536. }
  537. void ExportTemplateManager::_bind_methods() {
  538. }
  539. ExportTemplateManager::ExportTemplateManager() {
  540. VBoxContainer *main_vb = memnew(VBoxContainer);
  541. add_child(main_vb);
  542. current_hb = memnew(HBoxContainer);
  543. main_vb->add_margin_child(TTR("Current Version:"), current_hb, false);
  544. installed_scroll = memnew(ScrollContainer);
  545. main_vb->add_margin_child(TTR("Installed Versions:"), installed_scroll, true);
  546. installed_vb = memnew(VBoxContainer);
  547. installed_scroll->add_child(installed_vb);
  548. installed_scroll->set_enable_v_scroll(true);
  549. installed_scroll->set_enable_h_scroll(false);
  550. installed_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  551. get_cancel()->set_text(TTR("Close"));
  552. get_ok()->set_text(TTR("Install From File"));
  553. remove_confirm = memnew(ConfirmationDialog);
  554. remove_confirm->set_title(TTR("Remove Template"));
  555. add_child(remove_confirm);
  556. remove_confirm->connect("confirmed", callable_mp(this, &ExportTemplateManager::_uninstall_template_confirm));
  557. template_open = memnew(FileDialog);
  558. template_open->set_title(TTR("Select Template File"));
  559. template_open->add_filter("*.tpz ; " + TTR("Godot Export Templates"));
  560. template_open->set_access(FileDialog::ACCESS_FILESYSTEM);
  561. template_open->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
  562. template_open->connect("file_selected", callable_mp(this, &ExportTemplateManager::_install_from_file), varray(true));
  563. add_child(template_open);
  564. set_title(TTR("Export Template Manager"));
  565. set_hide_on_ok(false);
  566. request_mirror = memnew(HTTPRequest);
  567. add_child(request_mirror);
  568. request_mirror->connect("request_completed", callable_mp(this, &ExportTemplateManager::_http_download_mirror_completed));
  569. download_templates = memnew(HTTPRequest);
  570. add_child(download_templates);
  571. download_templates->connect("request_completed", callable_mp(this, &ExportTemplateManager::_http_download_templates_completed));
  572. template_downloader = memnew(AcceptDialog);
  573. template_downloader->set_title(TTR("Download Templates"));
  574. template_downloader->get_ok()->set_text(TTR("Close"));
  575. template_downloader->set_exclusive(true);
  576. add_child(template_downloader);
  577. template_downloader->connect("cancelled", callable_mp(this, &ExportTemplateManager::_window_template_downloader_closed));
  578. VBoxContainer *vbc = memnew(VBoxContainer);
  579. template_downloader->add_child(vbc);
  580. ScrollContainer *sc = memnew(ScrollContainer);
  581. sc->set_custom_minimum_size(Size2(400, 200) * EDSCALE);
  582. vbc->add_margin_child(TTR("Select mirror from list: (Shift+Click: Open in Browser)"), sc);
  583. template_list = memnew(VBoxContainer);
  584. sc->add_child(template_list);
  585. sc->set_enable_v_scroll(true);
  586. sc->set_enable_h_scroll(false);
  587. template_list_state = memnew(Label);
  588. vbc->add_child(template_list_state);
  589. template_download_progress = memnew(ProgressBar);
  590. vbc->add_child(template_download_progress);
  591. update_countdown = 0;
  592. }