editor_asset_installer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**************************************************************************/
  2. /* editor_asset_installer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. }
  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(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name));
  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 = String::utf8(fname);
  109. files_sorted.insert(name);
  110. ret = unzGoToNextFile(pkg);
  111. }
  112. Map<String, Ref<Texture>> extension_guess;
  113. {
  114. extension_guess["bmp"] = tree->get_icon("ImageTexture", "EditorIcons");
  115. extension_guess["dds"] = tree->get_icon("ImageTexture", "EditorIcons");
  116. extension_guess["exr"] = tree->get_icon("ImageTexture", "EditorIcons");
  117. extension_guess["hdr"] = tree->get_icon("ImageTexture", "EditorIcons");
  118. extension_guess["jpg"] = tree->get_icon("ImageTexture", "EditorIcons");
  119. extension_guess["jpeg"] = tree->get_icon("ImageTexture", "EditorIcons");
  120. extension_guess["png"] = tree->get_icon("ImageTexture", "EditorIcons");
  121. extension_guess["svg"] = tree->get_icon("ImageTexture", "EditorIcons");
  122. extension_guess["tga"] = tree->get_icon("ImageTexture", "EditorIcons");
  123. extension_guess["webp"] = tree->get_icon("ImageTexture", "EditorIcons");
  124. extension_guess["wav"] = tree->get_icon("AudioStreamSample", "EditorIcons");
  125. extension_guess["ogg"] = tree->get_icon("AudioStreamOGGVorbis", "EditorIcons");
  126. extension_guess["mp3"] = tree->get_icon("AudioStreamMP3", "EditorIcons");
  127. extension_guess["scn"] = tree->get_icon("PackedScene", "EditorIcons");
  128. extension_guess["tscn"] = tree->get_icon("PackedScene", "EditorIcons");
  129. extension_guess["escn"] = tree->get_icon("PackedScene", "EditorIcons");
  130. extension_guess["dae"] = tree->get_icon("PackedScene", "EditorIcons");
  131. extension_guess["gltf"] = tree->get_icon("PackedScene", "EditorIcons");
  132. extension_guess["glb"] = tree->get_icon("PackedScene", "EditorIcons");
  133. extension_guess["gdshader"] = tree->get_icon("Shader", "EditorIcons");
  134. extension_guess["shader"] = tree->get_icon("Shader", "EditorIcons");
  135. extension_guess["gd"] = tree->get_icon("GDScript", "EditorIcons");
  136. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  137. extension_guess["cs"] = tree->get_icon("CSharpScript", "EditorIcons");
  138. } else {
  139. // Mark C# support as unavailable.
  140. extension_guess["cs"] = tree->get_icon("ImportFail", "EditorIcons");
  141. }
  142. extension_guess["vs"] = tree->get_icon("VisualScript", "EditorIcons");
  143. extension_guess["res"] = tree->get_icon("Resource", "EditorIcons");
  144. extension_guess["tres"] = tree->get_icon("Resource", "EditorIcons");
  145. extension_guess["atlastex"] = tree->get_icon("AtlasTexture", "EditorIcons");
  146. // By default, OBJ files are imported as Mesh resources rather than PackedScenes.
  147. extension_guess["obj"] = tree->get_icon("Mesh", "EditorIcons");
  148. extension_guess["txt"] = tree->get_icon("TextFile", "EditorIcons");
  149. extension_guess["md"] = tree->get_icon("TextFile", "EditorIcons");
  150. extension_guess["rst"] = tree->get_icon("TextFile", "EditorIcons");
  151. extension_guess["json"] = tree->get_icon("TextFile", "EditorIcons");
  152. extension_guess["yml"] = tree->get_icon("TextFile", "EditorIcons");
  153. extension_guess["yaml"] = tree->get_icon("TextFile", "EditorIcons");
  154. extension_guess["toml"] = tree->get_icon("TextFile", "EditorIcons");
  155. extension_guess["cfg"] = tree->get_icon("TextFile", "EditorIcons");
  156. extension_guess["ini"] = tree->get_icon("TextFile", "EditorIcons");
  157. }
  158. Ref<Texture> generic_extension = get_icon("Object", "EditorIcons");
  159. unzClose(pkg);
  160. updating = true;
  161. tree->clear();
  162. TreeItem *root = tree->create_item();
  163. root->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  164. root->set_checked(0, true);
  165. root->set_icon(0, get_icon("folder", "FileDialog"));
  166. root->set_text(0, "res://");
  167. root->set_editable(0, true);
  168. Map<String, TreeItem *> dir_map;
  169. int num_file_conflicts = 0;
  170. for (Set<String>::Element *E = files_sorted.front(); E; E = E->next()) {
  171. String path = E->get();
  172. int depth = p_depth;
  173. bool skip = false;
  174. while (depth > 0) {
  175. int pp = path.find("/");
  176. if (pp == -1) {
  177. skip = true;
  178. break;
  179. }
  180. path = path.substr(pp + 1, path.length());
  181. depth--;
  182. }
  183. if (skip || path == String()) {
  184. continue;
  185. }
  186. bool isdir = false;
  187. if (path.ends_with("/")) {
  188. //a directory
  189. path = path.substr(0, path.length() - 1);
  190. isdir = true;
  191. }
  192. int pp = path.rfind("/");
  193. TreeItem *parent;
  194. if (pp == -1) {
  195. parent = root;
  196. } else {
  197. String ppath = path.substr(0, pp);
  198. ERR_CONTINUE(!dir_map.has(ppath));
  199. parent = dir_map[ppath];
  200. }
  201. TreeItem *ti = tree->create_item(parent);
  202. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  203. ti->set_checked(0, true);
  204. ti->set_editable(0, true);
  205. if (isdir) {
  206. dir_map[path] = ti;
  207. ti->set_text(0, path.get_file() + "/");
  208. ti->set_icon(0, get_icon("folder", "FileDialog"));
  209. ti->set_metadata(0, String());
  210. } else {
  211. String file = path.get_file();
  212. String extension = file.get_extension().to_lower();
  213. if (extension_guess.has(extension)) {
  214. ti->set_icon(0, extension_guess[extension]);
  215. } else {
  216. ti->set_icon(0, generic_extension);
  217. }
  218. ti->set_text(0, file);
  219. String res_path = "res://" + path;
  220. if (FileAccess::exists(res_path)) {
  221. num_file_conflicts += 1;
  222. ti->set_custom_color(0, get_color("error_color", "Editor"));
  223. ti->set_tooltip(0, vformat(TTR("%s (already exists)"), res_path));
  224. ti->set_checked(0, false);
  225. } else {
  226. ti->set_tooltip(0, res_path);
  227. }
  228. ti->set_metadata(0, res_path);
  229. }
  230. status_map[E->get()] = ti;
  231. }
  232. if (num_file_conflicts >= 1) {
  233. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - %d file(s) conflict with your project:"), asset_name, num_file_conflicts));
  234. } else {
  235. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - No files conflict with your project:"), asset_name));
  236. }
  237. popup_centered_ratio();
  238. updating = false;
  239. }
  240. void EditorAssetInstaller::ok_pressed() {
  241. FileAccess *src_f = nullptr;
  242. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  243. unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
  244. if (!pkg) {
  245. error->set_text(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name));
  246. return;
  247. }
  248. int ret = unzGoToFirstFile(pkg);
  249. Vector<String> failed_files;
  250. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());
  251. int idx = 0;
  252. while (ret == UNZ_OK) {
  253. //get filename
  254. unz_file_info info;
  255. char fname[16384];
  256. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  257. String name = String::utf8(fname);
  258. if (status_map.has(name) && status_map[name]->is_checked(0)) {
  259. String path = status_map[name]->get_metadata(0);
  260. if (path == String()) { // a dir
  261. String dirpath;
  262. TreeItem *t = status_map[name];
  263. while (t) {
  264. dirpath = t->get_text(0) + dirpath;
  265. t = t->get_parent();
  266. }
  267. if (dirpath.ends_with("/")) {
  268. dirpath = dirpath.substr(0, dirpath.length() - 1);
  269. }
  270. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  271. da->make_dir(dirpath);
  272. memdelete(da);
  273. } else {
  274. Vector<uint8_t> data;
  275. data.resize(info.uncompressed_size);
  276. //read
  277. unzOpenCurrentFile(pkg);
  278. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  279. unzCloseCurrentFile(pkg);
  280. FileAccess *f = FileAccess::open(path, FileAccess::WRITE);
  281. if (f) {
  282. f->store_buffer(data.ptr(), data.size());
  283. memdelete(f);
  284. } else {
  285. failed_files.push_back(path);
  286. }
  287. ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
  288. }
  289. }
  290. idx++;
  291. ret = unzGoToNextFile(pkg);
  292. }
  293. ProgressDialog::get_singleton()->end_task("uncompress");
  294. unzClose(pkg);
  295. if (failed_files.size()) {
  296. String msg = vformat(TTR("The following files failed extraction from asset \"%s\":"), asset_name) + "\n\n";
  297. for (int i = 0; i < failed_files.size(); i++) {
  298. if (i > 15) {
  299. msg += "\n" + vformat(TTR("(and %s more files)"), itos(failed_files.size() - i));
  300. break;
  301. }
  302. msg += failed_files[i];
  303. }
  304. if (EditorNode::get_singleton() != nullptr) {
  305. EditorNode::get_singleton()->show_warning(msg);
  306. }
  307. } else {
  308. if (EditorNode::get_singleton() != nullptr) {
  309. EditorNode::get_singleton()->show_warning(vformat(TTR("Asset \"%s\" installed successfully!"), asset_name), TTR("Success!"));
  310. }
  311. }
  312. EditorFileSystem::get_singleton()->scan_changes();
  313. }
  314. void EditorAssetInstaller::set_asset_name(const String &p_asset_name) {
  315. asset_name = p_asset_name;
  316. }
  317. String EditorAssetInstaller::get_asset_name() const {
  318. return asset_name;
  319. }
  320. void EditorAssetInstaller::_bind_methods() {
  321. ClassDB::bind_method("_item_edited", &EditorAssetInstaller::_item_edited);
  322. }
  323. EditorAssetInstaller::EditorAssetInstaller() {
  324. VBoxContainer *vb = memnew(VBoxContainer);
  325. add_child(vb);
  326. asset_contents = memnew(Label);
  327. vb->add_child(asset_contents);
  328. tree = memnew(Tree);
  329. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  330. tree->connect("item_edited", this, "_item_edited");
  331. vb->add_child(tree);
  332. error = memnew(AcceptDialog);
  333. add_child(error);
  334. get_ok()->set_text(TTR("Install"));
  335. set_title(TTR("Asset Installer"));
  336. updating = false;
  337. set_hide_on_ok(true);
  338. }