export_template_manager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. while (ret == UNZ_OK) {
  156. unz_file_info info;
  157. char fname[16384];
  158. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  159. String file = fname;
  160. if (file.ends_with("version.txt")) {
  161. Vector<uint8_t> data;
  162. data.resize(info.uncompressed_size);
  163. //read
  164. unzOpenCurrentFile(pkg);
  165. ret = unzReadCurrentFile(pkg, data.ptrw(), data.size());
  166. unzCloseCurrentFile(pkg);
  167. String data_str;
  168. data_str.parse_utf8((const char *)data.ptr(), data.size());
  169. data_str = data_str.strip_edges();
  170. // Version number should be of the form major.minor[.patch].status[.module_config]
  171. // so it can in theory have 3 or more slices.
  172. if (data_str.get_slice_count(".") < 3) {
  173. EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid version.txt format inside templates: %s."), data_str));
  174. unzClose(pkg);
  175. return false;
  176. }
  177. version = data_str;
  178. }
  179. if (file.get_file().size() != 0) {
  180. fc++;
  181. }
  182. ret = unzGoToNextFile(pkg);
  183. }
  184. if (version == String()) {
  185. EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside templates."));
  186. unzClose(pkg);
  187. return false;
  188. }
  189. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(version);
  190. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  191. Error err = d->make_dir_recursive(template_path);
  192. if (err != OK) {
  193. EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:") + "\n" + template_path);
  194. unzClose(pkg);
  195. return false;
  196. }
  197. memdelete(d);
  198. ret = unzGoToFirstFile(pkg);
  199. EditorProgress *p = NULL;
  200. if (p_use_progress) {
  201. p = memnew(EditorProgress("ltask", TTR("Extracting Export Templates"), fc));
  202. }
  203. fc = 0;
  204. while (ret == UNZ_OK) {
  205. //get filename
  206. unz_file_info info;
  207. char fname[16384];
  208. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  209. String file = String(fname).get_file();
  210. if (file.size() == 0) {
  211. ret = unzGoToNextFile(pkg);
  212. continue;
  213. }
  214. Vector<uint8_t> data;
  215. data.resize(info.uncompressed_size);
  216. //read
  217. unzOpenCurrentFile(pkg);
  218. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  219. unzCloseCurrentFile(pkg);
  220. if (p) {
  221. p->step(TTR("Importing:") + " " + file, fc);
  222. }
  223. FileAccess *f = FileAccess::open(template_path.plus_file(file), FileAccess::WRITE);
  224. if (!f) {
  225. ret = unzGoToNextFile(pkg);
  226. fc++;
  227. ERR_CONTINUE(!f);
  228. }
  229. f->store_buffer(data.ptr(), data.size());
  230. memdelete(f);
  231. ret = unzGoToNextFile(pkg);
  232. fc++;
  233. }
  234. if (p) {
  235. memdelete(p);
  236. }
  237. unzClose(pkg);
  238. _update_template_list();
  239. return true;
  240. }
  241. void ExportTemplateManager::popup_manager() {
  242. _update_template_list();
  243. popup_centered_minsize(Size2(400, 400) * EDSCALE);
  244. }
  245. void ExportTemplateManager::ok_pressed() {
  246. template_open->popup_centered_ratio();
  247. }
  248. void ExportTemplateManager::_http_download_mirror_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  249. if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
  250. EditorNode::get_singleton()->show_warning("Error getting the list of mirrors.");
  251. return;
  252. }
  253. String mirror_str;
  254. {
  255. PoolByteArray::Read r = p_data.read();
  256. mirror_str.parse_utf8((const char *)r.ptr(), p_data.size());
  257. }
  258. template_list_state->hide();
  259. template_download_progress->hide();
  260. Variant r;
  261. String errs;
  262. int errline;
  263. Error err = JSON::parse(mirror_str, r, errs, errline);
  264. if (err != OK) {
  265. EditorNode::get_singleton()->show_warning("Error parsing JSON of mirror list. Please report this issue!");
  266. return;
  267. }
  268. bool mirrors_found = false;
  269. Dictionary d = r;
  270. if (d.has("mirrors")) {
  271. Array mirrors = d["mirrors"];
  272. for (int i = 0; i < mirrors.size(); i++) {
  273. Dictionary m = mirrors[i];
  274. ERR_CONTINUE(!m.has("url") || !m.has("name"));
  275. LinkButton *lb = memnew(LinkButton);
  276. lb->set_text(m["name"]);
  277. lb->connect("pressed", this, "_begin_template_download", varray(m["url"]));
  278. template_list->add_child(lb);
  279. mirrors_found = true;
  280. }
  281. }
  282. if (!mirrors_found) {
  283. EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
  284. return;
  285. }
  286. }
  287. void ExportTemplateManager::_http_download_templates_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  288. switch (p_status) {
  289. case HTTPRequest::RESULT_CANT_RESOLVE: {
  290. template_list_state->set_text(TTR("Can't resolve."));
  291. } break;
  292. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  293. case HTTPRequest::RESULT_CONNECTION_ERROR:
  294. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  295. template_list_state->set_text(TTR("Can't connect."));
  296. } break;
  297. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  298. case HTTPRequest::RESULT_CANT_CONNECT: {
  299. template_list_state->set_text(TTR("Can't connect."));
  300. } break;
  301. case HTTPRequest::RESULT_NO_RESPONSE: {
  302. template_list_state->set_text(TTR("No response."));
  303. } break;
  304. case HTTPRequest::RESULT_REQUEST_FAILED: {
  305. template_list_state->set_text(TTR("Request Failed."));
  306. } break;
  307. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  308. template_list_state->set_text(TTR("Redirect Loop."));
  309. } break;
  310. default: {
  311. if (p_code != 200) {
  312. template_list_state->set_text(TTR("Failed:") + " " + itos(p_code));
  313. } else {
  314. String path = download_templates->get_download_file();
  315. template_list_state->set_text(TTR("Download Complete."));
  316. template_downloader->hide();
  317. int ret = _install_from_file(path, false);
  318. if (ret) {
  319. Error err = OS::get_singleton()->move_to_trash(path);
  320. if (err != OK) {
  321. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + path + "\n");
  322. }
  323. } else {
  324. WARN_PRINTS(vformat(TTR("Templates installation failed. The problematic templates archives can be found at '%s'."), path));
  325. }
  326. }
  327. } break;
  328. }
  329. set_process(false);
  330. }
  331. void ExportTemplateManager::_begin_template_download(const String &p_url) {
  332. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  333. OS::get_singleton()->shell_open(p_url);
  334. return;
  335. }
  336. for (int i = 0; i < template_list->get_child_count(); i++) {
  337. BaseButton *b = Object::cast_to<BaseButton>(template_list->get_child(0));
  338. if (b) {
  339. b->set_disabled(true);
  340. }
  341. }
  342. download_data.clear();
  343. download_templates->set_download_file(EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_templates.tpz"));
  344. download_templates->set_use_threads(true);
  345. Error err = download_templates->request(p_url);
  346. if (err != OK) {
  347. EditorNode::get_singleton()->show_warning(TTR("Error requesting url: ") + p_url);
  348. return;
  349. }
  350. set_process(true);
  351. template_list_state->show();
  352. template_download_progress->set_max(100);
  353. template_download_progress->set_value(0);
  354. template_download_progress->show();
  355. template_list_state->set_text(TTR("Connecting to Mirror..."));
  356. }
  357. void ExportTemplateManager::_window_template_downloader_closed() {
  358. download_templates->cancel_request();
  359. }
  360. void ExportTemplateManager::_notification(int p_what) {
  361. if (p_what == NOTIFICATION_PROCESS) {
  362. update_countdown -= get_process_delta_time();
  363. if (update_countdown > 0) {
  364. return;
  365. }
  366. update_countdown = 0.5;
  367. String status;
  368. bool errored = false;
  369. switch (download_templates->get_http_client_status()) {
  370. case HTTPClient::STATUS_DISCONNECTED:
  371. status = TTR("Disconnected");
  372. errored = true;
  373. break;
  374. case HTTPClient::STATUS_RESOLVING: status = TTR("Resolving"); break;
  375. case HTTPClient::STATUS_CANT_RESOLVE:
  376. status = TTR("Can't Resolve");
  377. errored = true;
  378. break;
  379. case HTTPClient::STATUS_CONNECTING: status = TTR("Connecting..."); break;
  380. case HTTPClient::STATUS_CANT_CONNECT:
  381. status = TTR("Can't Connect");
  382. errored = true;
  383. break;
  384. case HTTPClient::STATUS_CONNECTED: status = TTR("Connected"); break;
  385. case HTTPClient::STATUS_REQUESTING: status = TTR("Requesting..."); break;
  386. case HTTPClient::STATUS_BODY:
  387. status = TTR("Downloading");
  388. if (download_templates->get_body_size() > 0) {
  389. status += " " + String::humanize_size(download_templates->get_downloaded_bytes()) + "/" + String::humanize_size(download_templates->get_body_size());
  390. template_download_progress->set_max(download_templates->get_body_size());
  391. template_download_progress->set_value(download_templates->get_downloaded_bytes());
  392. } else {
  393. status += " " + String::humanize_size(download_templates->get_downloaded_bytes());
  394. }
  395. break;
  396. case HTTPClient::STATUS_CONNECTION_ERROR:
  397. status = TTR("Connection Error");
  398. errored = true;
  399. break;
  400. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR:
  401. status = TTR("SSL Handshake Error");
  402. errored = true;
  403. break;
  404. }
  405. template_list_state->set_text(status);
  406. if (errored) {
  407. set_process(false);
  408. ;
  409. }
  410. }
  411. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  412. if (!is_visible_in_tree()) {
  413. set_process(false);
  414. }
  415. }
  416. }
  417. void ExportTemplateManager::_bind_methods() {
  418. ClassDB::bind_method("_download_template", &ExportTemplateManager::_download_template);
  419. ClassDB::bind_method("_uninstall_template", &ExportTemplateManager::_uninstall_template);
  420. ClassDB::bind_method("_uninstall_template_confirm", &ExportTemplateManager::_uninstall_template_confirm);
  421. ClassDB::bind_method("_install_from_file", &ExportTemplateManager::_install_from_file);
  422. ClassDB::bind_method("_http_download_mirror_completed", &ExportTemplateManager::_http_download_mirror_completed);
  423. ClassDB::bind_method("_http_download_templates_completed", &ExportTemplateManager::_http_download_templates_completed);
  424. ClassDB::bind_method("_begin_template_download", &ExportTemplateManager::_begin_template_download);
  425. ClassDB::bind_method("_window_template_downloader_closed", &ExportTemplateManager::_window_template_downloader_closed);
  426. }
  427. ExportTemplateManager::ExportTemplateManager() {
  428. VBoxContainer *main_vb = memnew(VBoxContainer);
  429. add_child(main_vb);
  430. current_hb = memnew(HBoxContainer);
  431. main_vb->add_margin_child(TTR("Current Version:"), current_hb, false);
  432. installed_scroll = memnew(ScrollContainer);
  433. main_vb->add_margin_child(TTR("Installed Versions:"), installed_scroll, true);
  434. installed_vb = memnew(VBoxContainer);
  435. installed_scroll->add_child(installed_vb);
  436. installed_scroll->set_enable_v_scroll(true);
  437. installed_scroll->set_enable_h_scroll(false);
  438. installed_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  439. get_cancel()->set_text(TTR("Close"));
  440. get_ok()->set_text(TTR("Install From File"));
  441. remove_confirm = memnew(ConfirmationDialog);
  442. remove_confirm->set_title(TTR("Remove Template"));
  443. add_child(remove_confirm);
  444. remove_confirm->connect("confirmed", this, "_uninstall_template_confirm");
  445. template_open = memnew(FileDialog);
  446. template_open->set_title(TTR("Select template file"));
  447. template_open->add_filter("*.tpz ; Godot Export Templates");
  448. template_open->set_access(FileDialog::ACCESS_FILESYSTEM);
  449. template_open->set_mode(FileDialog::MODE_OPEN_FILE);
  450. template_open->connect("file_selected", this, "_install_from_file", varray(true));
  451. add_child(template_open);
  452. set_title(TTR("Export Template Manager"));
  453. set_hide_on_ok(false);
  454. request_mirror = memnew(HTTPRequest);
  455. add_child(request_mirror);
  456. request_mirror->connect("request_completed", this, "_http_download_mirror_completed");
  457. download_templates = memnew(HTTPRequest);
  458. add_child(download_templates);
  459. download_templates->connect("request_completed", this, "_http_download_templates_completed");
  460. template_downloader = memnew(AcceptDialog);
  461. template_downloader->set_title(TTR("Download Templates"));
  462. template_downloader->get_ok()->set_text(TTR("Close"));
  463. template_downloader->set_exclusive(true);
  464. add_child(template_downloader);
  465. template_downloader->connect("popup_hide", this, "_window_template_downloader_closed");
  466. VBoxContainer *vbc = memnew(VBoxContainer);
  467. template_downloader->add_child(vbc);
  468. ScrollContainer *sc = memnew(ScrollContainer);
  469. sc->set_custom_minimum_size(Size2(400, 200) * EDSCALE);
  470. vbc->add_margin_child(TTR("Select mirror from list: (Shift+Click: Open in Browser)"), sc);
  471. template_list = memnew(VBoxContainer);
  472. sc->add_child(template_list);
  473. sc->set_enable_v_scroll(true);
  474. sc->set_enable_h_scroll(false);
  475. template_list_state = memnew(Label);
  476. vbc->add_child(template_list_state);
  477. template_download_progress = memnew(ProgressBar);
  478. vbc->add_child(template_download_progress);
  479. update_countdown = 0;
  480. }