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-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 "editor_asset_installer.h"
  31. #include "editor_node.h"
  32. #include "io/zip_io.h"
  33. #include "os/dir_access.h"
  34. #include "os/file_access.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. print_line("PPATH IS: " + ppath);
  143. ERR_CONTINUE(!dir_map.has(ppath));
  144. parent = dir_map[ppath];
  145. }
  146. TreeItem *ti = tree->create_item(parent);
  147. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  148. ti->set_checked(0, true);
  149. ti->set_editable(0, true);
  150. if (isdir) {
  151. dir_map[path] = ti;
  152. ti->set_text(0, path.get_file() + "/");
  153. ti->set_icon(0, get_icon("folder", "FileDialog"));
  154. ti->set_metadata(0, String());
  155. } else {
  156. String file = path.get_file();
  157. String extension = file.get_extension().to_lower();
  158. if (extension_guess.has(extension)) {
  159. ti->set_icon(0, extension_guess[extension]);
  160. } else {
  161. ti->set_icon(0, generic_extension);
  162. }
  163. ti->set_text(0, file);
  164. String res_path = "res://" + path;
  165. if (FileAccess::exists(res_path)) {
  166. ti->set_custom_color(0, get_color("error_color", "Editor"));
  167. ti->set_tooltip(0, res_path + " (Already Exists)");
  168. ti->set_checked(0, false);
  169. } else {
  170. ti->set_tooltip(0, res_path);
  171. }
  172. ti->set_metadata(0, res_path);
  173. }
  174. status_map[E->get()] = ti;
  175. }
  176. popup_centered_ratio();
  177. updating = false;
  178. }
  179. void EditorAssetInstaller::ok_pressed() {
  180. FileAccess *src_f = NULL;
  181. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  182. unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
  183. if (!pkg) {
  184. error->set_text(TTR("Error opening package file, not in zip format."));
  185. return;
  186. }
  187. int ret = unzGoToFirstFile(pkg);
  188. Vector<String> failed_files;
  189. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());
  190. int idx = 0;
  191. while (ret == UNZ_OK) {
  192. //get filename
  193. unz_file_info info;
  194. char fname[16384];
  195. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);
  196. String name = fname;
  197. if (status_map.has(name) && status_map[name]->is_checked(0)) {
  198. String path = status_map[name]->get_metadata(0);
  199. if (path == String()) { // a dir
  200. String dirpath;
  201. TreeItem *t = status_map[name];
  202. while (t) {
  203. dirpath = t->get_text(0) + dirpath;
  204. t = t->get_parent();
  205. }
  206. if (dirpath.ends_with("/")) {
  207. dirpath = dirpath.substr(0, dirpath.length() - 1);
  208. }
  209. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  210. da->make_dir(dirpath);
  211. memdelete(da);
  212. } else {
  213. Vector<uint8_t> data;
  214. data.resize(info.uncompressed_size);
  215. //read
  216. unzOpenCurrentFile(pkg);
  217. unzReadCurrentFile(pkg, data.ptr(), data.size());
  218. unzCloseCurrentFile(pkg);
  219. FileAccess *f = FileAccess::open(path, FileAccess::WRITE);
  220. if (f) {
  221. f->store_buffer(data.ptr(), data.size());
  222. memdelete(f);
  223. } else {
  224. failed_files.push_back(path);
  225. }
  226. ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
  227. }
  228. }
  229. idx++;
  230. ret = unzGoToNextFile(pkg);
  231. }
  232. ProgressDialog::get_singleton()->end_task("uncompress");
  233. unzClose(pkg);
  234. if (failed_files.size()) {
  235. String msg = "The following files failed extraction from package:\n\n";
  236. for (int i = 0; i < failed_files.size(); i++) {
  237. if (i > 15) {
  238. msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  239. break;
  240. }
  241. msg += failed_files[i];
  242. }
  243. if (EditorNode::get_singleton() != NULL)
  244. EditorNode::get_singleton()->show_warning(msg);
  245. } else {
  246. if (EditorNode::get_singleton() != NULL)
  247. EditorNode::get_singleton()->show_warning(TTR("Package Installed Successfully!"), TTR("Success!"));
  248. }
  249. EditorFileSystem::get_singleton()->scan_changes();
  250. }
  251. void EditorAssetInstaller::_bind_methods() {
  252. ClassDB::bind_method("_item_edited", &EditorAssetInstaller::_item_edited);
  253. }
  254. EditorAssetInstaller::EditorAssetInstaller() {
  255. VBoxContainer *vb = memnew(VBoxContainer);
  256. add_child(vb);
  257. tree = memnew(Tree);
  258. vb->add_margin_child("Package Contents:", tree, true);
  259. tree->connect("item_edited", this, "_item_edited");
  260. error = memnew(AcceptDialog);
  261. add_child(error);
  262. get_ok()->set_text(TTR("Install"));
  263. set_title(TTR("Package Installer"));
  264. updating = false;
  265. set_hide_on_ok(true);
  266. }