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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/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["vs"] = tree->get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  107. extension_guess["res"] = tree->get_theme_icon(SNAME("Resource"), SNAME("EditorIcons"));
  108. extension_guess["tres"] = tree->get_theme_icon(SNAME("Resource"), SNAME("EditorIcons"));
  109. extension_guess["atlastex"] = tree->get_theme_icon(SNAME("AtlasTexture"), SNAME("EditorIcons"));
  110. // By default, OBJ files are imported as Mesh resources rather than PackedScenes.
  111. extension_guess["obj"] = tree->get_theme_icon(SNAME("Mesh"), SNAME("EditorIcons"));
  112. extension_guess["txt"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  113. extension_guess["md"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  114. extension_guess["rst"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  115. extension_guess["json"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  116. extension_guess["yml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  117. extension_guess["yaml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  118. extension_guess["toml"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  119. extension_guess["cfg"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  120. extension_guess["ini"] = tree->get_theme_icon(SNAME("TextFile"), SNAME("EditorIcons"));
  121. }
  122. Ref<Texture2D> generic_extension = tree->get_theme_icon(SNAME("Object"), SNAME("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, tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
  130. root->set_text(0, "res://");
  131. root->set_editable(0, true);
  132. HashMap<String, TreeItem *> dir_map;
  133. int num_file_conflicts = 0;
  134. for (const String &E : files_sorted) {
  135. String path = E;
  136. int depth = p_depth;
  137. bool skip = false;
  138. while (depth > 0) {
  139. int pp = path.find("/");
  140. if (pp == -1) {
  141. skip = true;
  142. break;
  143. }
  144. path = path.substr(pp + 1, path.length());
  145. depth--;
  146. }
  147. if (skip || path.is_empty()) {
  148. continue;
  149. }
  150. bool isdir = false;
  151. if (path.ends_with("/")) {
  152. //a directory
  153. path = path.substr(0, path.length() - 1);
  154. isdir = true;
  155. }
  156. int pp = path.rfind("/");
  157. TreeItem *parent;
  158. if (pp == -1) {
  159. parent = root;
  160. } else {
  161. String ppath = path.substr(0, pp);
  162. ERR_CONTINUE(!dir_map.has(ppath));
  163. parent = dir_map[ppath];
  164. }
  165. TreeItem *ti = tree->create_item(parent);
  166. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  167. ti->set_checked(0, true);
  168. ti->set_editable(0, true);
  169. if (isdir) {
  170. dir_map[path] = ti;
  171. ti->set_text(0, path.get_file() + "/");
  172. ti->set_icon(0, tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
  173. ti->set_metadata(0, String());
  174. } else {
  175. String file = path.get_file();
  176. String extension = file.get_extension().to_lower();
  177. if (extension_guess.has(extension)) {
  178. ti->set_icon(0, extension_guess[extension]);
  179. } else {
  180. ti->set_icon(0, generic_extension);
  181. }
  182. ti->set_text(0, file);
  183. String res_path = "res://" + path;
  184. if (FileAccess::exists(res_path)) {
  185. num_file_conflicts += 1;
  186. ti->set_custom_color(0, tree->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  187. ti->set_tooltip(0, vformat(TTR("%s (already exists)"), res_path));
  188. ti->set_checked(0, false);
  189. ti->propagate_check(0);
  190. } else {
  191. ti->set_tooltip(0, res_path);
  192. }
  193. ti->set_metadata(0, res_path);
  194. }
  195. status_map[E] = ti;
  196. }
  197. if (num_file_conflicts >= 1) {
  198. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - %d file(s) conflict with your project:"), asset_name, num_file_conflicts));
  199. } else {
  200. asset_contents->set_text(vformat(TTR("Contents of asset \"%s\" - No files conflict with your project:"), asset_name));
  201. }
  202. popup_centered_ratio(0.5);
  203. updating = false;
  204. }
  205. void EditorAssetInstaller::ok_pressed() {
  206. Ref<FileAccess> io_fa;
  207. zlib_filefunc_def io = zipio_create_io(&io_fa);
  208. unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
  209. if (!pkg) {
  210. error->set_text(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name));
  211. return;
  212. }
  213. int ret = unzGoToFirstFile(pkg);
  214. Vector<String> failed_files;
  215. ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());
  216. int idx = 0;
  217. while (ret == UNZ_OK) {
  218. //get filename
  219. unz_file_info info;
  220. char fname[16384];
  221. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  222. if (ret != UNZ_OK) {
  223. break;
  224. }
  225. String name = String::utf8(fname);
  226. if (status_map.has(name) && (status_map[name]->is_checked(0) || status_map[name]->is_indeterminate(0))) {
  227. String path = status_map[name]->get_metadata(0);
  228. if (path.is_empty()) { // a dir
  229. String dirpath;
  230. TreeItem *t = status_map[name];
  231. while (t) {
  232. dirpath = t->get_text(0) + dirpath;
  233. t = t->get_parent();
  234. }
  235. if (dirpath.ends_with("/")) {
  236. dirpath = dirpath.substr(0, dirpath.length() - 1);
  237. }
  238. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  239. da->make_dir(dirpath);
  240. } else {
  241. Vector<uint8_t> data;
  242. data.resize(info.uncompressed_size);
  243. //read
  244. unzOpenCurrentFile(pkg);
  245. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  246. unzCloseCurrentFile(pkg);
  247. Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE);
  248. if (f.is_valid()) {
  249. f->store_buffer(data.ptr(), data.size());
  250. } else {
  251. failed_files.push_back(path);
  252. }
  253. ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
  254. }
  255. }
  256. idx++;
  257. ret = unzGoToNextFile(pkg);
  258. }
  259. ProgressDialog::get_singleton()->end_task("uncompress");
  260. unzClose(pkg);
  261. if (failed_files.size()) {
  262. String msg = vformat(TTR("The following files failed extraction from asset \"%s\":"), asset_name) + "\n\n";
  263. for (int i = 0; i < failed_files.size(); i++) {
  264. if (i > 15) {
  265. msg += "\n" + vformat(TTR("(and %s more files)"), itos(failed_files.size() - i));
  266. break;
  267. }
  268. msg += failed_files[i];
  269. }
  270. if (EditorNode::get_singleton() != nullptr) {
  271. EditorNode::get_singleton()->show_warning(msg);
  272. }
  273. } else {
  274. if (EditorNode::get_singleton() != nullptr) {
  275. EditorNode::get_singleton()->show_warning(vformat(TTR("Asset \"%s\" installed successfully!"), asset_name), TTR("Success!"));
  276. }
  277. }
  278. EditorFileSystem::get_singleton()->scan_changes();
  279. }
  280. void EditorAssetInstaller::set_asset_name(const String &p_asset_name) {
  281. asset_name = p_asset_name;
  282. }
  283. String EditorAssetInstaller::get_asset_name() const {
  284. return asset_name;
  285. }
  286. void EditorAssetInstaller::_bind_methods() {
  287. }
  288. EditorAssetInstaller::EditorAssetInstaller() {
  289. VBoxContainer *vb = memnew(VBoxContainer);
  290. add_child(vb);
  291. asset_contents = memnew(Label);
  292. vb->add_child(asset_contents);
  293. tree = memnew(Tree);
  294. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  295. tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_edited));
  296. tree->connect("check_propagated_to_item", callable_mp(this, &EditorAssetInstaller::_check_propagated_to_item));
  297. vb->add_child(tree);
  298. error = memnew(AcceptDialog);
  299. add_child(error);
  300. set_ok_button_text(TTR("Install"));
  301. set_title(TTR("Asset Installer"));
  302. set_hide_on_ok(true);
  303. }