editor_asset_installer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/dir_access.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/zip_io.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/progress_dialog.h"
  37. void EditorAssetInstaller::_item_edited() {
  38. if (updating) {
  39. return;
  40. }
  41. TreeItem *item = tree->get_edited();
  42. if (!item) {
  43. return;
  44. }
  45. updating = true;
  46. item->propagate_check(0);
  47. updating = false;
  48. }
  49. void EditorAssetInstaller::_check_propagated_to_item(Object *p_obj, int column) {
  50. TreeItem *affected_item = Object::cast_to<TreeItem>(p_obj);
  51. if (affected_item && affected_item->get_custom_color(0) != Color()) {
  52. affected_item->set_checked(0, false);
  53. affected_item->propagate_check(0, false);
  54. }
  55. }
  56. void EditorAssetInstaller::open(const String &p_path, int p_depth) {
  57. package_path = p_path;
  58. HashSet<String> files_sorted;
  59. Ref<FileAccess> io_fa;
  60. zlib_filefunc_def io = zipio_create_io(&io_fa);
  61. unzFile pkg = unzOpen2(p_path.utf8().get_data(), &io);
  62. if (!pkg) {
  63. error->set_text(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name));
  64. return;
  65. }
  66. int ret = unzGoToFirstFile(pkg);
  67. while (ret == UNZ_OK) {
  68. //get filename
  69. unz_file_info info;
  70. char fname[16384];
  71. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  72. String name = String::utf8(fname);
  73. files_sorted.insert(name);
  74. ret = unzGoToNextFile(pkg);
  75. }
  76. HashMap<String, Ref<Texture2D>> extension_guess;
  77. {
  78. extension_guess["bmp"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  79. extension_guess["dds"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  80. extension_guess["exr"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  81. extension_guess["hdr"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  82. extension_guess["jpg"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  83. extension_guess["jpeg"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  84. extension_guess["png"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  85. extension_guess["svg"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  86. extension_guess["tga"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  87. extension_guess["webp"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons"));
  88. extension_guess["wav"] = tree->get_theme_icon(SNAME("AudioStreamWAV"), SNAME("EditorIcons"));
  89. extension_guess["ogg"] = tree->get_theme_icon(SNAME("AudioStreamOggVorbis"), SNAME("EditorIcons"));
  90. extension_guess["mp3"] = tree->get_theme_icon(SNAME("AudioStreamMP3"), SNAME("EditorIcons"));
  91. extension_guess["scn"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  92. extension_guess["tscn"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  93. extension_guess["escn"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  94. extension_guess["dae"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  95. extension_guess["gltf"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  96. extension_guess["glb"] = tree->get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  97. extension_guess["gdshader"] = tree->get_theme_icon(SNAME("Shader"), SNAME("EditorIcons"));
  98. extension_guess["gdshaderinc"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  99. extension_guess["gd"] = tree->get_theme_icon(SNAME("GDScript"), SNAME("EditorIcons"));
  100. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  101. extension_guess["cs"] = tree->get_theme_icon(SNAME("CSharpScript"), SNAME("EditorIcons"));
  102. } else {
  103. // Mark C# support as unavailable.
  104. extension_guess["cs"] = tree->get_theme_icon(SNAME("ImportFail"), SNAME("EditorIcons"));
  105. }
  106. extension_guess["res"] = tree->get_theme_icon(SNAME("Resource"), SNAME("EditorIcons"));
  107. extension_guess["tres"] = tree->get_theme_icon(SNAME("Resource"), SNAME("EditorIcons"));
  108. extension_guess["atlastex"] = tree->get_theme_icon(SNAME("AtlasTexture"), SNAME("EditorIcons"));
  109. // By default, OBJ files are imported as Mesh resources rather than PackedScenes.
  110. extension_guess["obj"] = tree->get_theme_icon(SNAME("Mesh"), SNAME("EditorIcons"));
  111. extension_guess["txt"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  112. extension_guess["md"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  113. extension_guess["rst"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  114. extension_guess["json"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  115. extension_guess["yml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  116. extension_guess["yaml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  117. extension_guess["toml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  118. extension_guess["cfg"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  119. extension_guess["ini"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  120. }
  121. Ref<Texture2D> generic_extension = tree->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  122. unzClose(pkg);
  123. updating = true;
  124. tree->clear();
  125. TreeItem *root = tree->create_item();
  126. root->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  127. root->set_checked(0, true);
  128. root->set_icon(0, tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
  129. root->set_text(0, "res://");
  130. root->set_editable(0, true);
  131. HashMap<String, TreeItem *> dir_map;
  132. int num_file_conflicts = 0;
  133. for (const String &E : files_sorted) {
  134. String path = E;
  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.is_empty()) {
  147. continue;
  148. }
  149. bool isdir = false;
  150. if (path.ends_with("/")) {
  151. //a directory
  152. path = path.substr(0, path.length() - 1);
  153. isdir = true;
  154. }
  155. int pp = path.rfind("/");
  156. TreeItem *parent_item;
  157. if (pp == -1) {
  158. parent_item = root;
  159. } else {
  160. String ppath = path.substr(0, pp);
  161. ERR_CONTINUE(!dir_map.has(ppath));
  162. parent_item = dir_map[ppath];
  163. }
  164. TreeItem *ti = tree->create_item(parent_item);
  165. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  166. ti->set_checked(0, true);
  167. ti->set_editable(0, true);
  168. if (isdir) {
  169. dir_map[path] = ti;
  170. ti->set_text(0, path.get_file() + "/");
  171. ti->set_icon(0, tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
  172. ti->set_metadata(0, String());
  173. } else {
  174. String file = path.get_file();
  175. String extension = file.get_extension().to_lower();
  176. if (extension_guess.has(extension)) {
  177. ti->set_icon(0, extension_guess[extension]);
  178. } else {
  179. ti->set_icon(0, generic_extension);
  180. }
  181. ti->set_text(0, file);
  182. String res_path = "res://" + path;
  183. if (FileAccess::exists(res_path)) {
  184. num_file_conflicts += 1;
  185. ti->set_custom_color(0, tree->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  186. ti->set_tooltip_text(0, vformat(TTR("%s (already exists)"), res_path));
  187. ti->set_checked(0, false);
  188. ti->propagate_check(0);
  189. } else {
  190. ti->set_tooltip_text(0, res_path);
  191. }
  192. ti->set_metadata(0, res_path);
  193. }
  194. status_map[E] = ti;
  195. }
  196. if (num_file_conflicts >= 1) {
  197. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - %d file(s) conflict with your project:"), asset_name, num_file_conflicts));
  198. } else {
  199. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - No files conflict with your project:"), asset_name));
  200. }
  201. popup_centered_ratio(0.5);
  202. updating = false;
  203. }
  204. void EditorAssetInstaller::ok_pressed() {
  205. Ref<FileAccess> io_fa;
  206. zlib_filefunc_def io = zipio_create_io(&io_fa);
  207. unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
  208. if (!pkg) {
  209. error->set_text(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name));
  210. return;
  211. }
  212. int ret = unzGoToFirstFile(pkg);
  213. Vector<String> failed_files;
  214. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());
  215. int idx = 0;
  216. while (ret == UNZ_OK) {
  217. //get filename
  218. unz_file_info info;
  219. char fname[16384];
  220. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  221. if (ret != UNZ_OK) {
  222. break;
  223. }
  224. String name = String::utf8(fname);
  225. if (status_map.has(name) && (status_map[name]->is_checked(0) || status_map[name]->is_indeterminate(0))) {
  226. String path = status_map[name]->get_metadata(0);
  227. if (path.is_empty()) { // a dir
  228. String dirpath;
  229. TreeItem *t = status_map[name];
  230. while (t) {
  231. dirpath = t->get_text(0) + dirpath;
  232. t = t->get_parent();
  233. }
  234. if (dirpath.ends_with("/")) {
  235. dirpath = dirpath.substr(0, dirpath.length() - 1);
  236. }
  237. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  238. da->make_dir(dirpath);
  239. } else {
  240. Vector<uint8_t> uncomp_data;
  241. uncomp_data.resize(info.uncompressed_size);
  242. //read
  243. unzOpenCurrentFile(pkg);
  244. unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
  245. unzCloseCurrentFile(pkg);
  246. Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE);
  247. if (f.is_valid()) {
  248. f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
  249. } else {
  250. failed_files.push_back(path);
  251. }
  252. ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
  253. }
  254. }
  255. idx++;
  256. ret = unzGoToNextFile(pkg);
  257. }
  258. ProgressDialog::get_singleton()->end_task("uncompress");
  259. unzClose(pkg);
  260. if (failed_files.size()) {
  261. String msg = vformat(TTR("The following files failed extraction from asset \"%s\":"), asset_name) + "\n\n";
  262. for (int i = 0; i < failed_files.size(); i++) {
  263. if (i > 15) {
  264. msg += "\n" + vformat(TTR("(and %s more files)"), itos(failed_files.size() - i));
  265. break;
  266. }
  267. msg += failed_files[i];
  268. }
  269. if (EditorNode::get_singleton() != nullptr) {
  270. EditorNode::get_singleton()->show_warning(msg);
  271. }
  272. } else {
  273. if (EditorNode::get_singleton() != nullptr) {
  274. EditorNode::get_singleton()->show_warning(vformat(TTR("Asset \"%s\" installed successfully!"), asset_name), TTR("Success!"));
  275. }
  276. }
  277. EditorFileSystem::get_singleton()->scan_changes();
  278. }
  279. void EditorAssetInstaller::set_asset_name(const String &p_asset_name) {
  280. asset_name = p_asset_name;
  281. }
  282. String EditorAssetInstaller::get_asset_name() const {
  283. return asset_name;
  284. }
  285. void EditorAssetInstaller::_bind_methods() {
  286. }
  287. EditorAssetInstaller::EditorAssetInstaller() {
  288. VBoxContainer *vb = memnew(VBoxContainer);
  289. add_child(vb);
  290. asset_contents = memnew(Label);
  291. vb->add_child(asset_contents);
  292. tree = memnew(Tree);
  293. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  294. tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_edited));
  295. tree->connect("check_propagated_to_item", callable_mp(this, &EditorAssetInstaller::_check_propagated_to_item));
  296. vb->add_child(tree);
  297. error = memnew(AcceptDialog);
  298. add_child(error);
  299. set_ok_button_text(TTR("Install"));
  300. set_title(TTR("Asset Installer"));
  301. set_hide_on_ok(true);
  302. }