editor_asset_installer.cpp 9.5 KB

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