editor_asset_installer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*************************************************************************/
  2. /* editor_asset_installer.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 "editor_asset_installer.h"
  31. #include "core/io/dir_access.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/zip_io.h"
  34. #include "editor_node.h"
  35. #include "progress_dialog.h"
  36. void EditorAssetInstaller::_update_subitems(TreeItem *p_item, bool p_check, bool p_first) {
  37. if (p_check) {
  38. if (p_item->get_custom_color(0) == Color()) {
  39. p_item->set_checked(0, true);
  40. }
  41. } else {
  42. p_item->set_checked(0, false);
  43. }
  44. if (p_item->get_first_child()) {
  45. _update_subitems(p_item->get_first_child(), p_check);
  46. }
  47. if (!p_first && p_item->get_next()) {
  48. _update_subitems(p_item->get_next(), p_check);
  49. }
  50. }
  51. void EditorAssetInstaller::_uncheck_parent(TreeItem *p_item) {
  52. if (!p_item) {
  53. return;
  54. }
  55. bool any_checked = false;
  56. TreeItem *item = p_item->get_first_child();
  57. while (item) {
  58. if (item->is_checked(0)) {
  59. any_checked = true;
  60. break;
  61. }
  62. item = item->get_next();
  63. }
  64. if (!any_checked) {
  65. p_item->set_checked(0, false);
  66. _uncheck_parent(p_item->get_parent());
  67. }
  68. }
  69. void EditorAssetInstaller::_item_edited() {
  70. if (updating) {
  71. return;
  72. }
  73. TreeItem *item = tree->get_edited();
  74. if (!item) {
  75. return;
  76. }
  77. String path = item->get_metadata(0);
  78. updating = true;
  79. if (path == String() || item == tree->get_root()) { //a dir or root
  80. _update_subitems(item, item->is_checked(0), true);
  81. }
  82. if (item->is_checked(0)) {
  83. while (item) {
  84. item->set_checked(0, true);
  85. item = item->get_parent();
  86. }
  87. } else {
  88. _uncheck_parent(item->get_parent());
  89. }
  90. updating = false;
  91. }
  92. void EditorAssetInstaller::open(const String &p_path, int p_depth) {
  93. package_path = p_path;
  94. Set<String> files_sorted;
  95. FileAccess *src_f = nullptr;
  96. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  97. unzFile pkg = unzOpen2(p_path.utf8().get_data(), &io);
  98. if (!pkg) {
  99. error->set_text(TTR("Error opening package file, not in ZIP format."));
  100. return;
  101. }
  102. int ret = unzGoToFirstFile(pkg);
  103. while (ret == UNZ_OK) {
  104. //get filename
  105. unz_file_info info;
  106. char fname[16384];
  107. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  108. String name = fname;
  109. files_sorted.insert(name);
  110. ret = unzGoToNextFile(pkg);
  111. }
  112. Map<String, Ref<Texture2D>> extension_guess;
  113. {
  114. extension_guess["png"] = tree->get_theme_icon("ImageTexture", "EditorIcons");
  115. extension_guess["jpg"] = tree->get_theme_icon("ImageTexture", "EditorIcons");
  116. extension_guess["atlastex"] = tree->get_theme_icon("AtlasTexture", "EditorIcons");
  117. extension_guess["scn"] = tree->get_theme_icon("PackedScene", "EditorIcons");
  118. extension_guess["tscn"] = tree->get_theme_icon("PackedScene", "EditorIcons");
  119. extension_guess["gdshader"] = tree->get_theme_icon("Shader", "EditorIcons");
  120. extension_guess["gd"] = tree->get_theme_icon("GDScript", "EditorIcons");
  121. extension_guess["vs"] = tree->get_theme_icon("VisualScript", "EditorIcons");
  122. }
  123. Ref<Texture2D> generic_extension = tree->get_theme_icon("Object", "EditorIcons");
  124. unzClose(pkg);
  125. updating = true;
  126. tree->clear();
  127. TreeItem *root = tree->create_item();
  128. root->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  129. root->set_checked(0, true);
  130. root->set_icon(0, tree->get_theme_icon("folder", "FileDialog"));
  131. root->set_text(0, "res://");
  132. root->set_editable(0, true);
  133. Map<String, TreeItem *> dir_map;
  134. for (Set<String>::Element *E = files_sorted.front(); E; E = E->next()) {
  135. String path = E->get();
  136. int depth = p_depth;
  137. bool skip = false;
  138. while (depth > 0) {
  139. int pp = path.find("/");
  140. if (pp == -1) {
  141. skip = true;
  142. break;
  143. }
  144. path = path.substr(pp + 1, path.length());
  145. depth--;
  146. }
  147. if (skip || path == String()) {
  148. continue;
  149. }
  150. bool isdir = false;
  151. if (path.ends_with("/")) {
  152. //a directory
  153. path = path.substr(0, path.length() - 1);
  154. isdir = true;
  155. }
  156. int pp = path.rfind("/");
  157. TreeItem *parent;
  158. if (pp == -1) {
  159. parent = root;
  160. } else {
  161. String ppath = path.substr(0, pp);
  162. ERR_CONTINUE(!dir_map.has(ppath));
  163. parent = dir_map[ppath];
  164. }
  165. TreeItem *ti = tree->create_item(parent);
  166. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  167. ti->set_checked(0, true);
  168. ti->set_editable(0, true);
  169. if (isdir) {
  170. dir_map[path] = ti;
  171. ti->set_text(0, path.get_file() + "/");
  172. ti->set_icon(0, tree->get_theme_icon("folder", "FileDialog"));
  173. ti->set_metadata(0, String());
  174. } else {
  175. String file = path.get_file();
  176. String extension = file.get_extension().to_lower();
  177. if (extension_guess.has(extension)) {
  178. ti->set_icon(0, extension_guess[extension]);
  179. } else {
  180. ti->set_icon(0, generic_extension);
  181. }
  182. ti->set_text(0, file);
  183. String res_path = "res://" + path;
  184. if (FileAccess::exists(res_path)) {
  185. ti->set_custom_color(0, tree->get_theme_color("error_color", "Editor"));
  186. ti->set_tooltip(0, vformat(TTR("%s (Already Exists)"), res_path));
  187. ti->set_checked(0, false);
  188. } else {
  189. ti->set_tooltip(0, res_path);
  190. }
  191. ti->set_metadata(0, res_path);
  192. }
  193. status_map[E->get()] = ti;
  194. }
  195. popup_centered_ratio();
  196. updating = false;
  197. }
  198. void EditorAssetInstaller::ok_pressed() {
  199. FileAccess *src_f = nullptr;
  200. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  201. unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
  202. if (!pkg) {
  203. error->set_text(TTR("Error opening package file, not in ZIP format."));
  204. return;
  205. }
  206. int ret = unzGoToFirstFile(pkg);
  207. Vector<String> failed_files;
  208. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());
  209. int idx = 0;
  210. while (ret == UNZ_OK) {
  211. //get filename
  212. unz_file_info info;
  213. char fname[16384];
  214. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  215. String name = fname;
  216. if (status_map.has(name) && status_map[name]->is_checked(0)) {
  217. String path = status_map[name]->get_metadata(0);
  218. if (path == String()) { // a dir
  219. String dirpath;
  220. TreeItem *t = status_map[name];
  221. while (t) {
  222. dirpath = t->get_text(0) + dirpath;
  223. t = t->get_parent();
  224. }
  225. if (dirpath.ends_with("/")) {
  226. dirpath = dirpath.substr(0, dirpath.length() - 1);
  227. }
  228. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  229. da->make_dir(dirpath);
  230. memdelete(da);
  231. } else {
  232. Vector<uint8_t> data;
  233. data.resize(info.uncompressed_size);
  234. //read
  235. unzOpenCurrentFile(pkg);
  236. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  237. unzCloseCurrentFile(pkg);
  238. FileAccess *f = FileAccess::open(path, FileAccess::WRITE);
  239. if (f) {
  240. f->store_buffer(data.ptr(), data.size());
  241. memdelete(f);
  242. } else {
  243. failed_files.push_back(path);
  244. }
  245. ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
  246. }
  247. }
  248. idx++;
  249. ret = unzGoToNextFile(pkg);
  250. }
  251. ProgressDialog::get_singleton()->end_task("uncompress");
  252. unzClose(pkg);
  253. if (failed_files.size()) {
  254. String msg = TTR("The following files failed extraction from package:") + "\n\n";
  255. for (int i = 0; i < failed_files.size(); i++) {
  256. if (i > 15) {
  257. msg += "\n" + vformat(TTR("And %s more files."), itos(failed_files.size() - i));
  258. break;
  259. }
  260. msg += failed_files[i];
  261. }
  262. if (EditorNode::get_singleton() != nullptr) {
  263. EditorNode::get_singleton()->show_warning(msg);
  264. }
  265. } else {
  266. if (EditorNode::get_singleton() != nullptr) {
  267. EditorNode::get_singleton()->show_warning(TTR("Package installed successfully!"), TTR("Success!"));
  268. }
  269. }
  270. EditorFileSystem::get_singleton()->scan_changes();
  271. }
  272. void EditorAssetInstaller::_bind_methods() {
  273. }
  274. EditorAssetInstaller::EditorAssetInstaller() {
  275. VBoxContainer *vb = memnew(VBoxContainer);
  276. add_child(vb);
  277. tree = memnew(Tree);
  278. vb->add_margin_child(TTR("Package Contents:"), tree, true);
  279. tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_edited));
  280. error = memnew(AcceptDialog);
  281. add_child(error);
  282. get_ok_button()->set_text(TTR("Install"));
  283. set_title(TTR("Package Installer"));
  284. updating = false;
  285. set_hide_on_ok(true);
  286. }