export_template_manager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "editor_scale.h"
  33. #include "io/json.h"
  34. #include "io/zip_io.h"
  35. #include "os/dir_access.h"
  36. #include "version.h"
  37. void ExportTemplateManager::_update_template_list() {
  38. while (current_hb->get_child_count()) {
  39. memdelete(current_hb->get_child(0));
  40. }
  41. while (installed_vb->get_child_count()) {
  42. memdelete(installed_vb->get_child(0));
  43. }
  44. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  45. Error err = d->change_dir(EditorSettings::get_singleton()->get_settings_path().plus_file("templates"));
  46. d->list_dir_begin();
  47. Set<String> templates;
  48. if (err == OK) {
  49. bool isdir;
  50. String c = d->get_next(&isdir);
  51. while (c != String()) {
  52. if (isdir && !c.begins_with(".")) {
  53. templates.insert(c);
  54. }
  55. c = d->get_next(&isdir);
  56. }
  57. }
  58. d->list_dir_end();
  59. memdelete(d);
  60. String current_version = itos(VERSION_MAJOR) + "." + itos(VERSION_MINOR) + "-" + _MKSTR(VERSION_STATUS) + VERSION_MODULE_CONFIG;
  61. Label *current = memnew(Label);
  62. current->set_h_size_flags(SIZE_EXPAND_FILL);
  63. current_hb->add_child(current);
  64. if (templates.has(current_version)) {
  65. current->add_color_override("font_color", get_color("success_color", "Editor"));
  66. Button *redownload = memnew(Button);
  67. redownload->set_text(TTR("Re-Download"));
  68. current_hb->add_child(redownload);
  69. redownload->connect("pressed", this, "_download_template", varray(current_version));
  70. Button *uninstall = memnew(Button);
  71. uninstall->set_text(TTR("Uninstall"));
  72. current_hb->add_child(uninstall);
  73. current->set_text(current_version + " " + TTR("(Installed)"));
  74. uninstall->connect("pressed", this, "_uninstall_template", varray(current_version));
  75. } else {
  76. current->add_color_override("font_color", get_color("error_color", "Editor"));
  77. Button *redownload = memnew(Button);
  78. redownload->set_text(TTR("Download"));
  79. redownload->connect("pressed", this, "_download_template", varray(current_version));
  80. current_hb->add_child(redownload);
  81. current->set_text(current_version + " " + TTR("(Missing)"));
  82. }
  83. for (Set<String>::Element *E = templates.back(); E; E = E->prev()) {
  84. HBoxContainer *hbc = memnew(HBoxContainer);
  85. Label *version = memnew(Label);
  86. version->set_modulate(get_color("disabled_font_color", "Editor"));
  87. String text = E->get();
  88. if (text == current_version) {
  89. text += " " + TTR("(Current)");
  90. }
  91. version->set_text(text);
  92. version->set_h_size_flags(SIZE_EXPAND_FILL);
  93. hbc->add_child(version);
  94. Button *uninstall = memnew(Button);
  95. uninstall->set_text(TTR("Uninstall"));
  96. hbc->add_child(uninstall);
  97. uninstall->connect("pressed", this, "_uninstall_template", varray(E->get()));
  98. installed_vb->add_child(hbc);
  99. }
  100. }
  101. void ExportTemplateManager::_download_template(const String &p_version) {
  102. print_line("download " + p_version);
  103. while (template_list->get_child_count()) {
  104. memdelete(template_list->get_child(0));
  105. }
  106. template_downloader->popup_centered_minsize();
  107. template_list_state->set_text(TTR("Retrieving mirrors, please wait.."));
  108. template_download_progress->set_max(100);
  109. template_download_progress->set_value(0);
  110. request_mirror->request("https://godotengine.org/mirrorlist/" + p_version + ".json");
  111. template_list_state->show();
  112. template_download_progress->show();
  113. }
  114. void ExportTemplateManager::_uninstall_template(const String &p_version) {
  115. remove_confirm->set_text(vformat(TTR("Remove template version '%s'?"), p_version));
  116. remove_confirm->popup_centered_minsize();
  117. to_remove = p_version;
  118. }
  119. void ExportTemplateManager::_uninstall_template_confirm() {
  120. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  121. Error err = d->change_dir(EditorSettings::get_singleton()->get_settings_path().plus_file("templates"));
  122. ERR_FAIL_COND(err != OK);
  123. err = d->change_dir(to_remove);
  124. ERR_FAIL_COND(err != OK);
  125. Vector<String> files;
  126. d->list_dir_begin();
  127. bool isdir;
  128. String c = d->get_next(&isdir);
  129. while (c != String()) {
  130. if (!isdir) {
  131. files.push_back(c);
  132. }
  133. c = d->get_next(&isdir);
  134. }
  135. d->list_dir_end();
  136. for (int i = 0; i < files.size(); i++) {
  137. d->remove(files[i]);
  138. }
  139. d->change_dir("..");
  140. d->remove(to_remove);
  141. _update_template_list();
  142. }
  143. void ExportTemplateManager::_install_from_file(const String &p_file) {
  144. FileAccess *fa = NULL;
  145. zlib_filefunc_def io = zipio_create_io_from_file(&fa);
  146. unzFile pkg = unzOpen2(p_file.utf8().get_data(), &io);
  147. if (!pkg) {
  148. EditorNode::get_singleton()->show_warning(TTR("Can't open export templates zip."));
  149. return;
  150. }
  151. int ret = unzGoToFirstFile(pkg);
  152. int fc = 0; //count them and find version
  153. String version;
  154. while (ret == UNZ_OK) {
  155. unz_file_info info;
  156. char fname[16384];
  157. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  158. String file = fname;
  159. if (file.ends_with("version.txt")) {
  160. Vector<uint8_t> data;
  161. data.resize(info.uncompressed_size);
  162. //read
  163. unzOpenCurrentFile(pkg);
  164. ret = unzReadCurrentFile(pkg, data.ptr(), data.size());
  165. unzCloseCurrentFile(pkg);
  166. String data_str;
  167. data_str.parse_utf8((const char *)data.ptr(), data.size());
  168. data_str = data_str.strip_edges();
  169. if (data_str.get_slice_count("-") != 2 || data_str.get_slice_count(".") != 2) {
  170. EditorNode::get_singleton()->show_warning(TTR("Invalid version.txt format inside templates."));
  171. unzClose(pkg);
  172. return;
  173. }
  174. String ver = data_str.get_slice("-", 0);
  175. int major = ver.get_slice(".", 0).to_int();
  176. int minor = ver.get_slice(".", 1).to_int();
  177. String rev = data_str.get_slice("-", 1);
  178. if (!rev.is_valid_identifier()) {
  179. EditorNode::get_singleton()->show_warning(TTR("Invalid version.txt format inside templates. Revision is not a valid identifier."));
  180. unzClose(pkg);
  181. return;
  182. }
  183. version = itos(major) + "." + itos(minor) + "-" + rev;
  184. }
  185. fc++;
  186. ret = unzGoToNextFile(pkg);
  187. }
  188. if (version == String()) {
  189. EditorNode::get_singleton()->show_warning(TTR("No version.txt found inside templates."));
  190. unzClose(pkg);
  191. return;
  192. }
  193. String template_path = EditorSettings::get_singleton()->get_settings_path().plus_file("templates").plus_file(version);
  194. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  195. Error err = d->make_dir_recursive(template_path);
  196. if (err != OK) {
  197. EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:\n") + template_path);
  198. unzClose(pkg);
  199. return;
  200. }
  201. memdelete(d);
  202. ret = unzGoToFirstFile(pkg);
  203. EditorProgress p("ltask", TTR("Extracting Export Templates"), fc);
  204. fc = 0;
  205. while (ret == UNZ_OK) {
  206. //get filename
  207. unz_file_info info;
  208. char fname[16384];
  209. unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  210. String file = fname;
  211. Vector<uint8_t> data;
  212. data.resize(info.uncompressed_size);
  213. //read
  214. unzOpenCurrentFile(pkg);
  215. unzReadCurrentFile(pkg, data.ptr(), data.size());
  216. unzCloseCurrentFile(pkg);
  217. print_line(fname);
  218. /*
  219. for(int i=0;i<512;i++) {
  220. print_line(itos(data[i]));
  221. }
  222. */
  223. file = file.get_file();
  224. p.step(TTR("Importing:") + " " + file, fc);
  225. FileAccess *f = FileAccess::open(template_path.plus_file(file), FileAccess::WRITE);
  226. ERR_CONTINUE(!f);
  227. f->store_buffer(data.ptr(), data.size());
  228. memdelete(f);
  229. ret = unzGoToNextFile(pkg);
  230. fc++;
  231. }
  232. unzClose(pkg);
  233. _update_template_list();
  234. }
  235. void ExportTemplateManager::popup_manager() {
  236. _update_template_list();
  237. popup_centered_minsize(Size2(400, 400) * EDSCALE);
  238. }
  239. void ExportTemplateManager::ok_pressed() {
  240. template_open->popup_centered_ratio();
  241. }
  242. void ExportTemplateManager::_http_download_mirror_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  243. if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
  244. EditorNode::get_singleton()->show_warning("Error getting the list of mirrors.");
  245. return;
  246. }
  247. String mirror_str;
  248. {
  249. PoolByteArray::Read r = p_data.read();
  250. mirror_str.parse_utf8((const char *)r.ptr(), p_data.size());
  251. }
  252. template_list_state->hide();
  253. template_download_progress->hide();
  254. Variant r;
  255. String errs;
  256. int errline;
  257. Error err = JSON::parse(mirror_str, r, errs, errline);
  258. if (err != OK) {
  259. EditorNode::get_singleton()->show_warning("Error parsing JSON of mirror list. Please report this issue!");
  260. return;
  261. }
  262. bool mirrors_found = false;
  263. Dictionary d = r;
  264. print_line(r);
  265. if (d.has("mirrors")) {
  266. Array mirrors = d["mirrors"];
  267. for (int i = 0; i < mirrors.size(); i++) {
  268. Dictionary m = mirrors[i];
  269. ERR_CONTINUE(!m.has("url") || !m.has("name"));
  270. LinkButton *lb = memnew(LinkButton);
  271. lb->set_text(m["name"]);
  272. lb->connect("pressed", this, "_begin_template_download", varray(m["url"]));
  273. template_list->add_child(lb);
  274. mirrors_found = true;
  275. }
  276. }
  277. if (!mirrors_found) {
  278. EditorNode::get_singleton()->show_warning(TTR("No download links found for this version. Direct download is only available for official releases."));
  279. return;
  280. }
  281. }
  282. void ExportTemplateManager::_http_download_templates_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  283. switch (p_status) {
  284. case HTTPRequest::RESULT_CANT_RESOLVE: {
  285. template_list_state->set_text(TTR("Can't resolve."));
  286. } break;
  287. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  288. case HTTPRequest::RESULT_CONNECTION_ERROR:
  289. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  290. template_list_state->set_text(TTR("Can't connect."));
  291. } break;
  292. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  293. case HTTPRequest::RESULT_CANT_CONNECT: {
  294. template_list_state->set_text(TTR("Can't connect."));
  295. } break;
  296. case HTTPRequest::RESULT_NO_RESPONSE: {
  297. template_list_state->set_text(TTR("No response."));
  298. } break;
  299. case HTTPRequest::RESULT_REQUEST_FAILED: {
  300. template_list_state->set_text(TTR("Req. Failed."));
  301. } break;
  302. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  303. template_list_state->set_text(TTR("Redirect Loop."));
  304. } break;
  305. default: {
  306. if (p_code != 200) {
  307. template_list_state->set_text(TTR("Failed:") + " " + itos(p_code));
  308. } else {
  309. String path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp").plus_file("tmp_templates.tpz");
  310. FileAccess *f = FileAccess::open(path, FileAccess::WRITE);
  311. if (!f) {
  312. template_list_state->set_text(TTR("Can't write file."));
  313. } else {
  314. int size = p_data.size();
  315. PoolVector<uint8_t>::Read r = p_data.read();
  316. f->store_buffer(r.ptr(), size);
  317. memdelete(f);
  318. template_list_state->set_text(TTR("Download Complete."));
  319. template_downloader->hide();
  320. _install_from_file(path);
  321. }
  322. }
  323. } break;
  324. }
  325. set_process(false);
  326. }
  327. void ExportTemplateManager::_begin_template_download(const String &p_url) {
  328. for (int i = 0; i < template_list->get_child_count(); i++) {
  329. BaseButton *b = Object::cast_to<BaseButton>(template_list->get_child(0));
  330. if (b) {
  331. b->set_disabled(true);
  332. }
  333. }
  334. download_data.clear();
  335. Error err = download_templates->request(p_url);
  336. if (err != OK) {
  337. EditorNode::get_singleton()->show_warning(TTR("Error requesting url: ") + p_url);
  338. return;
  339. }
  340. set_process(true);
  341. template_list_state->show();
  342. template_download_progress->set_max(100);
  343. template_download_progress->set_value(0);
  344. template_download_progress->show();
  345. template_list_state->set_text(TTR("Connecting to Mirror.."));
  346. }
  347. void ExportTemplateManager::_notification(int p_what) {
  348. if (p_what == NOTIFICATION_PROCESS) {
  349. update_countdown -= get_process_delta_time();
  350. if (update_countdown > 0) {
  351. return;
  352. }
  353. update_countdown = 0.5;
  354. String status;
  355. bool errored = false;
  356. switch (download_templates->get_http_client_status()) {
  357. case HTTPClient::STATUS_DISCONNECTED:
  358. status = TTR("Disconnected");
  359. errored = true;
  360. break;
  361. case HTTPClient::STATUS_RESOLVING: status = TTR("Resolving"); break;
  362. case HTTPClient::STATUS_CANT_RESOLVE:
  363. status = TTR("Can't Resolve");
  364. errored = true;
  365. break;
  366. case HTTPClient::STATUS_CONNECTING: status = TTR("Connecting.."); break;
  367. case HTTPClient::STATUS_CANT_CONNECT:
  368. status = TTR("Can't Conect");
  369. errored = true;
  370. break;
  371. case HTTPClient::STATUS_CONNECTED: status = TTR("Connected"); break;
  372. case HTTPClient::STATUS_REQUESTING: status = TTR("Requesting.."); break;
  373. case HTTPClient::STATUS_BODY:
  374. status = TTR("Downloading");
  375. if (download_templates->get_body_size() > 0) {
  376. status += " " + String::humanize_size(download_templates->get_downloaded_bytes()) + "/" + String::humanize_size(download_templates->get_body_size());
  377. template_download_progress->set_max(download_templates->get_body_size());
  378. template_download_progress->set_value(download_templates->get_downloaded_bytes());
  379. } else {
  380. status += " " + String::humanize_size(download_templates->get_downloaded_bytes());
  381. }
  382. break;
  383. case HTTPClient::STATUS_CONNECTION_ERROR:
  384. status = TTR("Connection Error");
  385. errored = true;
  386. break;
  387. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR:
  388. status = TTR("SSL Handshake Error");
  389. errored = true;
  390. break;
  391. }
  392. template_list_state->set_text(status);
  393. if (errored) {
  394. set_process(false);
  395. ;
  396. }
  397. }
  398. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  399. if (!is_visible_in_tree()) {
  400. print_line("closed");
  401. download_templates->cancel_request();
  402. set_process(false);
  403. }
  404. }
  405. }
  406. void ExportTemplateManager::_bind_methods() {
  407. ClassDB::bind_method("_download_template", &ExportTemplateManager::_download_template);
  408. ClassDB::bind_method("_uninstall_template", &ExportTemplateManager::_uninstall_template);
  409. ClassDB::bind_method("_uninstall_template_confirm", &ExportTemplateManager::_uninstall_template_confirm);
  410. ClassDB::bind_method("_install_from_file", &ExportTemplateManager::_install_from_file);
  411. ClassDB::bind_method("_http_download_mirror_completed", &ExportTemplateManager::_http_download_mirror_completed);
  412. ClassDB::bind_method("_http_download_templates_completed", &ExportTemplateManager::_http_download_templates_completed);
  413. ClassDB::bind_method("_begin_template_download", &ExportTemplateManager::_begin_template_download);
  414. }
  415. ExportTemplateManager::ExportTemplateManager() {
  416. VBoxContainer *main_vb = memnew(VBoxContainer);
  417. add_child(main_vb);
  418. current_hb = memnew(HBoxContainer);
  419. main_vb->add_margin_child(TTR("Current Version:"), current_hb, false);
  420. installed_scroll = memnew(ScrollContainer);
  421. main_vb->add_margin_child(TTR("Installed Versions:"), installed_scroll, true);
  422. installed_vb = memnew(VBoxContainer);
  423. installed_scroll->add_child(installed_vb);
  424. installed_scroll->set_enable_v_scroll(true);
  425. installed_scroll->set_enable_h_scroll(false);
  426. installed_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  427. get_cancel()->set_text(TTR("Close"));
  428. get_ok()->set_text(TTR("Install From File"));
  429. remove_confirm = memnew(ConfirmationDialog);
  430. remove_confirm->set_title(TTR("Remove Template"));
  431. add_child(remove_confirm);
  432. remove_confirm->connect("confirmed", this, "_uninstall_template_confirm");
  433. template_open = memnew(FileDialog);
  434. template_open->set_title(TTR("Select template file"));
  435. template_open->add_filter("*.tpz ; Godot Export Templates");
  436. template_open->set_access(FileDialog::ACCESS_FILESYSTEM);
  437. template_open->set_mode(FileDialog::MODE_OPEN_FILE);
  438. template_open->connect("file_selected", this, "_install_from_file");
  439. add_child(template_open);
  440. set_title(TTR("Export Template Manager"));
  441. set_hide_on_ok(false);
  442. request_mirror = memnew(HTTPRequest);
  443. add_child(request_mirror);
  444. request_mirror->connect("request_completed", this, "_http_download_mirror_completed");
  445. download_templates = memnew(HTTPRequest);
  446. add_child(download_templates);
  447. download_templates->connect("request_completed", this, "_http_download_templates_completed");
  448. template_downloader = memnew(AcceptDialog);
  449. template_downloader->set_title(TTR("Download Templates"));
  450. template_downloader->get_ok()->set_text(TTR("Close"));
  451. add_child(template_downloader);
  452. VBoxContainer *vbc = memnew(VBoxContainer);
  453. template_downloader->add_child(vbc);
  454. ScrollContainer *sc = memnew(ScrollContainer);
  455. sc->set_custom_minimum_size(Size2(400, 200) * EDSCALE);
  456. vbc->add_margin_child(TTR("Select mirror from list: "), sc);
  457. template_list = memnew(VBoxContainer);
  458. sc->add_child(template_list);
  459. sc->set_enable_v_scroll(true);
  460. sc->set_enable_h_scroll(false);
  461. template_list_state = memnew(Label);
  462. vbc->add_child(template_list_state);
  463. template_download_progress = memnew(ProgressBar);
  464. vbc->add_child(template_download_progress);
  465. update_countdown = 0;
  466. }