export_template_manager.cpp 24 KB

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