export_template_manager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. void ExportTemplateManager::_update_template_list() {
  40. while (current_hb->get_child_count()) {
  41. memdelete(current_hb->get_child(0));
  42. }
  43. while (installed_vb->get_child_count()) {
  44. memdelete(installed_vb->get_child(0));
  45. }
  46. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  47. Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
  48. d->list_dir_begin();
  49. Set<String> templates;
  50. if (err == OK) {
  51. bool isdir;
  52. String c = d->get_next(&isdir);
  53. while (c != String()) {
  54. if (isdir && !c.begins_with(".")) {
  55. templates.insert(c);
  56. }
  57. c = d->get_next(&isdir);
  58. }
  59. }
  60. d->list_dir_end();
  61. memdelete(d);
  62. String current_version = VERSION_FULL_CONFIG;
  63. Label *current = memnew(Label);
  64. current->set_h_size_flags(SIZE_EXPAND_FILL);
  65. current_hb->add_child(current);
  66. if (templates.has(current_version)) {
  67. current->add_color_override("font_color", get_color("success_color", "Editor"));
  68. Button *redownload = memnew(Button);
  69. redownload->set_text(TTR("Re-Download"));
  70. current_hb->add_child(redownload);
  71. redownload->connect("pressed", this, "_download_template", varray(current_version));
  72. Button *uninstall = memnew(Button);
  73. uninstall->set_text(TTR("Uninstall"));
  74. current_hb->add_child(uninstall);
  75. current->set_text(current_version + " " + TTR("(Installed)"));
  76. uninstall->connect("pressed", this, "_uninstall_template", varray(current_version));
  77. } else {
  78. current->add_color_override("font_color", get_color("error_color", "Editor"));
  79. Button *redownload = memnew(Button);
  80. redownload->set_text(TTR("Download"));
  81. redownload->connect("pressed", this, "_download_template", varray(current_version));
  82. current_hb->add_child(redownload);
  83. current->set_text(current_version + " " + TTR("(Missing)"));
  84. }
  85. for (Set<String>::Element *E = templates.back(); E; E = E->prev()) {
  86. HBoxContainer *hbc = memnew(HBoxContainer);
  87. Label *version = memnew(Label);
  88. version->set_modulate(get_color("disabled_font_color", "Editor"));
  89. String text = E->get();
  90. if (text == current_version) {
  91. text += " " + TTR("(Current)");
  92. }
  93. version->set_text(text);
  94. version->set_h_size_flags(SIZE_EXPAND_FILL);
  95. hbc->add_child(version);
  96. Button *uninstall = memnew(Button);
  97. uninstall->set_text(TTR("Uninstall"));
  98. hbc->add_child(uninstall);
  99. uninstall->connect("pressed", this, "_uninstall_template", varray(E->get()));
  100. installed_vb->add_child(hbc);
  101. }
  102. }
  103. void ExportTemplateManager::_download_template(const String &p_version) {
  104. while (template_list->get_child_count()) {
  105. memdelete(template_list->get_child(0));
  106. }
  107. template_downloader->popup_centered_minsize();
  108. template_list_state->set_text(TTR("Retrieving mirrors, please wait..."));
  109. template_download_progress->set_max(100);
  110. template_download_progress->set_value(0);
  111. request_mirror->request("https://godotengine.org/mirrorlist/" + p_version + ".json");
  112. template_list_state->show();
  113. template_download_progress->show();
  114. }
  115. void ExportTemplateManager::_uninstall_template(const String &p_version) {
  116. remove_confirm->set_text(vformat(TTR("Remove template version '%s'?"), p_version));
  117. remove_confirm->popup_centered_minsize();
  118. to_remove = p_version;
  119. }
  120. void ExportTemplateManager::_uninstall_template_confirm() {
  121. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  122. Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
  123. ERR_FAIL_COND(err != OK);
  124. err = d->change_dir(to_remove);
  125. ERR_FAIL_COND(err != OK);
  126. Vector<String> files;
  127. d->list_dir_begin();
  128. bool isdir;
  129. String c = d->get_next(&isdir);
  130. while (c != String()) {
  131. if (!isdir) {
  132. files.push_back(c);
  133. }
  134. c = d->get_next(&isdir);
  135. }
  136. d->list_dir_end();
  137. for (int i = 0; i < files.size(); i++) {
  138. d->remove(files[i]);
  139. }
  140. d->change_dir("..");
  141. d->remove(to_remove);
  142. _update_template_list();
  143. }
  144. bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_progress) {
  145. FileAccess *fa = NULL;
  146. zlib_filefunc_def io = zipio_create_io_from_file(&fa);
  147. unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
  148. if (!pkg) {
  149. EditorNode::get_singleton()->show_warning(TTR("Can't open export templates zip."));
  150. return false;
  151. }
  152. int ret = unzGoToFirstFile(pkg);
  153. int fc = 0; //count them and find version
  154. String version;
  155. String contents_dir;
  156. while (ret == UNZ_OK) {
  157. unz_file_info info;
  158. char fname[16384];
  159. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  160. String file = fname;
  161. if (file.ends_with("version.txt")) {
  162. Vector<uint8_t> data;
  163. data.resize(info.uncompressed_size);
  164. //read
  165. unzOpenCurrentFile(pkg);
  166. ret = unzReadCurrentFile(pkg, data.ptrw(), data.size());
  167. unzCloseCurrentFile(pkg);
  168. String data_str;
  169. data_str.parse_utf8((const char *)data.ptr(), data.size());
  170. data_str = data_str.strip_edges();
  171. // Version number should be of the form major.minor[.patch].status[.module_config]
  172. // so it can in theory have 3 or more slices.
  173. if (data_str.get_slice_count(".") < 3) {
  174. EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside templates: %s."), data_str));
  175. unzClose(pkg);
  176. return false;
  177. }
  178. version = data_str;
  179. contents_dir = file.get_base_dir().trim_suffix("/").trim_suffix("\\");
  180. }
  181. if (file.get_file().size() != 0) {
  182. fc++;
  183. }
  184. ret = unzGoToNextFile(pkg);
  185. }
  186. if (version == String()) {
  187. EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside templates."));
  188. unzClose(pkg);
  189. return false;
  190. }
  191. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(version);
  192. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  193. Error err = d->make_dir_recursive(template_path);
  194. if (err != OK) {
  195. EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:") + "\n" + template_path);
  196. unzClose(pkg);
  197. return false;
  198. }
  199. memdelete(d);
  200. ret = unzGoToFirstFile(pkg);
  201. EditorProgress *p = NULL;
  202. if (p_use_progress) {
  203. p = memnew(EditorProgress("ltask", TTR("Extracting Export Templates"), fc));
  204. }
  205. fc = 0;
  206. while (ret == UNZ_OK) {
  207. //get filename
  208. unz_file_info info;
  209. char fname[16384];
  210. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  211. String file_path(String(fname).simplify_path());
  212. String file = file_path.get_file();
  213. if (file.size() == 0) {
  214. ret = unzGoToNextFile(pkg);
  215. continue;
  216. }
  217. Vector<uint8_t> data;
  218. data.resize(info.uncompressed_size);
  219. //read
  220. unzOpenCurrentFile(pkg);
  221. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  222. unzCloseCurrentFile(pkg);
  223. String base_dir = file_path.get_base_dir().trim_suffix("/");
  224. if (base_dir != contents_dir && base_dir.begins_with(contents_dir)) {
  225. base_dir = base_dir.substr(contents_dir.length(), file_path.length()).trim_prefix("/");
  226. file = base_dir.plus_file(file);
  227. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  228. ERR_CONTINUE(!da);
  229. String output_dir = template_path.plus_file(base_dir);
  230. if (!DirAccess::exists(output_dir)) {
  231. Error mkdir_err = da->make_dir_recursive(output_dir);
  232. ERR_CONTINUE(mkdir_err != OK);
  233. }
  234. }
  235. if (p) {
  236. p->step(TTR("Importing:") + " " + file, fc);
  237. }
  238. String to_write = template_path.plus_file(file);
  239. FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
  240. if (!f) {
  241. ret = unzGoToNextFile(pkg);
  242. fc++;
  243. ERR_CONTINUE(!f);
  244. }
  245. f->store_buffer(data.ptr(), data.size());
  246. memdelete(f);
  247. #ifndef WINDOWS_ENABLED
  248. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  249. #endif
  250. ret = unzGoToNextFile(pkg);
  251. fc++;
  252. }
  253. if (p) {
  254. memdelete(p);
  255. }
  256. unzClose(pkg);
  257. _update_template_list();
  258. return true;
  259. }
  260. void ExportTemplateManager::popup_manager() {
  261. _update_template_list();
  262. popup_centered_minsize(Size2(400, 400) * EDSCALE);
  263. }
  264. void ExportTemplateManager::ok_pressed() {
  265. template_open->popup_centered_ratio();
  266. }
  267. void ExportTemplateManager::_http_download_mirror_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  268. if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
  269. EditorNode::get_singleton()->show_warning("Error getting the list of mirrors.");
  270. return;
  271. }
  272. String mirror_str;
  273. {
  274. PoolByteArray::Read r = p_data.read();
  275. mirror_str.parse_utf8((const char *)r.ptr(), p_data.size());
  276. }
  277. template_list_state->hide();
  278. template_download_progress->hide();
  279. Variant r;
  280. String errs;
  281. int errline;
  282. Error err = JSON::parse(mirror_str, r, errs, errline);
  283. if (err != OK) {
  284. EditorNode::get_singleton()->show_warning("Error parsing JSON of mirror list. Please report this issue!");
  285. return;
  286. }
  287. bool mirrors_found = false;
  288. Dictionary d = r;
  289. if (d.has("mirrors")) {
  290. Array mirrors = d["mirrors"];
  291. for (int i = 0; i < mirrors.size(); i++) {
  292. Dictionary m = mirrors[i];
  293. ERR_CONTINUE(!m.has("url") || !m.has("name"));
  294. LinkButton *lb = memnew(LinkButton);
  295. lb->set_text(m["name"]);
  296. lb->connect("pressed", this, "_begin_template_download", varray(m["url"]));
  297. template_list->add_child(lb);
  298. mirrors_found = true;
  299. }
  300. }
  301. if (!mirrors_found) {
  302. EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
  303. return;
  304. }
  305. }
  306. void ExportTemplateManager::_http_download_templates_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  307. switch (p_status) {
  308. case HTTPRequest::RESULT_CANT_RESOLVE: {
  309. template_list_state->set_text(TTR("Can't resolve."));
  310. } break;
  311. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  312. case HTTPRequest::RESULT_CONNECTION_ERROR:
  313. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  314. template_list_state->set_text(TTR("Can't connect."));
  315. } break;
  316. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  317. case HTTPRequest::RESULT_CANT_CONNECT: {
  318. template_list_state->set_text(TTR("Can't connect."));
  319. } break;
  320. case HTTPRequest::RESULT_NO_RESPONSE: {
  321. template_list_state->set_text(TTR("No response."));
  322. } break;
  323. case HTTPRequest::RESULT_REQUEST_FAILED: {
  324. template_list_state->set_text(TTR("Request Failed."));
  325. } break;
  326. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  327. template_list_state->set_text(TTR("Redirect Loop."));
  328. } break;
  329. default: {
  330. if (p_code != 200) {
  331. template_list_state->set_text(TTR("Failed:") + " " + itos(p_code));
  332. } else {
  333. String path = download_templates->get_download_file();
  334. template_list_state->set_text(TTR("Download Complete."));
  335. template_downloader->hide();
  336. int ret = _install_from_file(path, false);
  337. if (ret) {
  338. Error err = OS::get_singleton()->move_to_trash(path);
  339. if (err != OK) {
  340. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + path + "\n");
  341. }
  342. } else {
  343. WARN_PRINTS(vformat(TTR("Templates installation failed. The problematic templates archives can be found at '%s'."), path));
  344. }
  345. }
  346. } break;
  347. }
  348. set_process(false);
  349. }
  350. void ExportTemplateManager::_begin_template_download(const String &p_url) {
  351. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  352. OS::get_singleton()->shell_open(p_url);
  353. return;
  354. }
  355. for (int i = 0; i < template_list->get_child_count(); i++) {
  356. BaseButton *b = Object::cast_to<BaseButton>(template_list->get_child(0));
  357. if (b) {
  358. b->set_disabled(true);
  359. }
  360. }
  361. download_data.clear();
  362. download_templates->set_download_file(EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_templates.tpz"));
  363. download_templates->set_use_threads(true);
  364. Error err = download_templates->request(p_url);
  365. if (err != OK) {
  366. EditorNode::get_singleton()->show_warning(TTR("Error requesting url: ") + p_url);
  367. return;
  368. }
  369. set_process(true);
  370. template_list_state->show();
  371. template_download_progress->set_max(100);
  372. template_download_progress->set_value(0);
  373. template_download_progress->show();
  374. template_list_state->set_text(TTR("Connecting to Mirror..."));
  375. }
  376. void ExportTemplateManager::_window_template_downloader_closed() {
  377. download_templates->cancel_request();
  378. }
  379. void ExportTemplateManager::_notification(int p_what) {
  380. if (p_what == NOTIFICATION_PROCESS) {
  381. update_countdown -= get_process_delta_time();
  382. if (update_countdown > 0) {
  383. return;
  384. }
  385. update_countdown = 0.5;
  386. String status;
  387. bool errored = false;
  388. switch (download_templates->get_http_client_status()) {
  389. case HTTPClient::STATUS_DISCONNECTED:
  390. status = TTR("Disconnected");
  391. errored = true;
  392. break;
  393. case HTTPClient::STATUS_RESOLVING: status = TTR("Resolving"); break;
  394. case HTTPClient::STATUS_CANT_RESOLVE:
  395. status = TTR("Can't Resolve");
  396. errored = true;
  397. break;
  398. case HTTPClient::STATUS_CONNECTING: status = TTR("Connecting..."); break;
  399. case HTTPClient::STATUS_CANT_CONNECT:
  400. status = TTR("Can't Connect");
  401. errored = true;
  402. break;
  403. case HTTPClient::STATUS_CONNECTED: status = TTR("Connected"); break;
  404. case HTTPClient::STATUS_REQUESTING: status = TTR("Requesting..."); break;
  405. case HTTPClient::STATUS_BODY:
  406. status = TTR("Downloading");
  407. if (download_templates->get_body_size() > 0) {
  408. status += " " + String::humanize_size(download_templates->get_downloaded_bytes()) + "/" + String::humanize_size(download_templates->get_body_size());
  409. template_download_progress->set_max(download_templates->get_body_size());
  410. template_download_progress->set_value(download_templates->get_downloaded_bytes());
  411. } else {
  412. status += " " + String::humanize_size(download_templates->get_downloaded_bytes());
  413. }
  414. break;
  415. case HTTPClient::STATUS_CONNECTION_ERROR:
  416. status = TTR("Connection Error");
  417. errored = true;
  418. break;
  419. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR:
  420. status = TTR("SSL Handshake Error");
  421. errored = true;
  422. break;
  423. }
  424. template_list_state->set_text(status);
  425. if (errored) {
  426. set_process(false);
  427. ;
  428. }
  429. }
  430. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  431. if (!is_visible_in_tree()) {
  432. set_process(false);
  433. }
  434. }
  435. }
  436. bool ExportTemplateManager::can_install_android_template() {
  437. return FileAccess::exists(EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG).plus_file("android_source.zip"));
  438. }
  439. Error ExportTemplateManager::install_android_template() {
  440. DirAccessRef da = DirAccess::open("res://");
  441. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  442. //make android dir (if it does not exist)
  443. da->make_dir("android");
  444. {
  445. //add an empty .gdignore file to avoid scan
  446. FileAccessRef f = FileAccess::open("res://android/.gdignore", FileAccess::WRITE);
  447. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  448. f->store_line("");
  449. f->close();
  450. }
  451. {
  452. //add version, to ensure building won't work if template and Godot version don't match
  453. FileAccessRef f = FileAccess::open("res://android/.build_version", FileAccess::WRITE);
  454. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  455. f->store_line(VERSION_FULL_CONFIG);
  456. f->close();
  457. }
  458. Error err = da->make_dir_recursive("android/build");
  459. ERR_FAIL_COND_V(err != OK, err);
  460. String source_zip = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG).plus_file("android_source.zip");
  461. ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
  462. FileAccess *src_f = NULL;
  463. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  464. unzFile pkg = unzOpen2(source_zip.utf8().get_data(), &io);
  465. ERR_EXPLAIN("Android sources not in zip format");
  466. ERR_FAIL_COND_V(!pkg, ERR_CANT_OPEN);
  467. int ret = unzGoToFirstFile(pkg);
  468. int total_files = 0;
  469. //count files
  470. while (ret == UNZ_OK) {
  471. total_files++;
  472. ret = unzGoToNextFile(pkg);
  473. }
  474. ret = unzGoToFirstFile(pkg);
  475. //decompress files
  476. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Android Build Sources"), total_files);
  477. Set<String> dirs_tested;
  478. int idx = 0;
  479. while (ret == UNZ_OK) {
  480. //get filename
  481. unz_file_info info;
  482. char fname[16384];
  483. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  484. String name = fname;
  485. String base_dir = name.get_base_dir();
  486. if (!name.ends_with("/")) {
  487. Vector<uint8_t> data;
  488. data.resize(info.uncompressed_size);
  489. //read
  490. unzOpenCurrentFile(pkg);
  491. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  492. unzCloseCurrentFile(pkg);
  493. if (!dirs_tested.has(base_dir)) {
  494. da->make_dir_recursive(String("android/build").plus_file(base_dir));
  495. dirs_tested.insert(base_dir);
  496. }
  497. String to_write = String("res://android/build").plus_file(name);
  498. FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
  499. if (f) {
  500. f->store_buffer(data.ptr(), data.size());
  501. memdelete(f);
  502. #ifndef WINDOWS_ENABLED
  503. FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
  504. #endif
  505. } else {
  506. ERR_PRINTS("Can't uncompress file: " + to_write);
  507. }
  508. }
  509. ProgressDialog::get_singleton()->task_step("uncompress", name, idx);
  510. idx++;
  511. ret = unzGoToNextFile(pkg);
  512. }
  513. ProgressDialog::get_singleton()->end_task("uncompress");
  514. unzClose(pkg);
  515. return OK;
  516. }
  517. void ExportTemplateManager::_bind_methods() {
  518. ClassDB::bind_method("_download_template", &ExportTemplateManager::_download_template);
  519. ClassDB::bind_method("_uninstall_template", &ExportTemplateManager::_uninstall_template);
  520. ClassDB::bind_method("_uninstall_template_confirm", &ExportTemplateManager::_uninstall_template_confirm);
  521. ClassDB::bind_method("_install_from_file", &ExportTemplateManager::_install_from_file);
  522. ClassDB::bind_method("_http_download_mirror_completed", &ExportTemplateManager::_http_download_mirror_completed);
  523. ClassDB::bind_method("_http_download_templates_completed", &ExportTemplateManager::_http_download_templates_completed);
  524. ClassDB::bind_method("_begin_template_download", &ExportTemplateManager::_begin_template_download);
  525. ClassDB::bind_method("_window_template_downloader_closed", &ExportTemplateManager::_window_template_downloader_closed);
  526. }
  527. ExportTemplateManager::ExportTemplateManager() {
  528. VBoxContainer *main_vb = memnew(VBoxContainer);
  529. add_child(main_vb);
  530. current_hb = memnew(HBoxContainer);
  531. main_vb->add_margin_child(TTR("Current Version:"), current_hb, false);
  532. installed_scroll = memnew(ScrollContainer);
  533. main_vb->add_margin_child(TTR("Installed Versions:"), installed_scroll, true);
  534. installed_vb = memnew(VBoxContainer);
  535. installed_scroll->add_child(installed_vb);
  536. installed_scroll->set_enable_v_scroll(true);
  537. installed_scroll->set_enable_h_scroll(false);
  538. installed_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  539. get_cancel()->set_text(TTR("Close"));
  540. get_ok()->set_text(TTR("Install From File"));
  541. remove_confirm = memnew(ConfirmationDialog);
  542. remove_confirm->set_title(TTR("Remove Template"));
  543. add_child(remove_confirm);
  544. remove_confirm->connect("confirmed", this, "_uninstall_template_confirm");
  545. template_open = memnew(FileDialog);
  546. template_open->set_title(TTR("Select Template File"));
  547. template_open->add_filter("*.tpz ; Godot Export Templates");
  548. template_open->set_access(FileDialog::ACCESS_FILESYSTEM);
  549. template_open->set_mode(FileDialog::MODE_OPEN_FILE);
  550. template_open->connect("file_selected", this, "_install_from_file", varray(true));
  551. add_child(template_open);
  552. set_title(TTR("Export Template Manager"));
  553. set_hide_on_ok(false);
  554. request_mirror = memnew(HTTPRequest);
  555. add_child(request_mirror);
  556. request_mirror->connect("request_completed", this, "_http_download_mirror_completed");
  557. download_templates = memnew(HTTPRequest);
  558. add_child(download_templates);
  559. download_templates->connect("request_completed", this, "_http_download_templates_completed");
  560. template_downloader = memnew(AcceptDialog);
  561. template_downloader->set_title(TTR("Download Templates"));
  562. template_downloader->get_ok()->set_text(TTR("Close"));
  563. template_downloader->set_exclusive(true);
  564. add_child(template_downloader);
  565. template_downloader->connect("popup_hide", this, "_window_template_downloader_closed");
  566. VBoxContainer *vbc = memnew(VBoxContainer);
  567. template_downloader->add_child(vbc);
  568. ScrollContainer *sc = memnew(ScrollContainer);
  569. sc->set_custom_minimum_size(Size2(400, 200) * EDSCALE);
  570. vbc->add_margin_child(TTR("Select mirror from list: (Shift+Click: Open in Browser)"), sc);
  571. template_list = memnew(VBoxContainer);
  572. sc->add_child(template_list);
  573. sc->set_enable_v_scroll(true);
  574. sc->set_enable_h_scroll(false);
  575. template_list_state = memnew(Label);
  576. vbc->add_child(template_list_state);
  577. template_download_progress = memnew(ProgressBar);
  578. vbc->add_child(template_download_progress);
  579. update_countdown = 0;
  580. }