editor_asset_installer.cpp 9.8 KB

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