| 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 | |
| 31 | #include "editor_asset_installer.h" |
| 32 | |
| 33 | #include "core/io/dir_access.h" |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "core/io/zip_io.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_string_names.h" |
| 38 | #include "editor/file_system/editor_file_system.h" |
| 39 | #include "editor/gui/editor_file_dialog.h" |
| 40 | #include "editor/gui/editor_toaster.h" |
| 41 | #include "editor/gui/progress_dialog.h" |
| 42 | #include "editor/themes/editor_scale.h" |
| 43 | #include "scene/gui/check_box.h" |
| 44 | #include "scene/gui/label.h" |
| 45 | #include "scene/gui/link_button.h" |
| 46 | #include "scene/gui/margin_container.h" |
| 47 | #include "scene/gui/separator.h" |
| 48 | #include "scene/gui/split_container.h" |
| 49 | |
| 50 | void EditorAssetInstaller::_item_checked_cbk() { |
| 51 | if (updating_source || !source_tree->get_edited()) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | updating_source = true; |
| 56 | TreeItem *item = source_tree->get_edited(); |
| 57 | item->propagate_check(0); |
| 58 | _fix_conflicted_indeterminate_state(source_tree->get_root(), 0); |
| 59 | _update_confirm_button(); |
| 60 | _rebuild_destination_tree(); |
| 61 | updating_source = false; |
| 62 | } |
| 63 | |
| 64 | // Determine parent state based on non-conflict children, to avoid indeterminate state, and allow toggle dir with conflicts. |
| 65 | bool EditorAssetInstaller::_fix_conflicted_indeterminate_state(TreeItem *p_item, int p_column) { |
| 66 | if (p_item->get_child_count() == 0) { |
| 67 | return false; |
| 68 | } |
| 69 | bool all_non_conflict_checked = true; |
| 70 | bool all_non_conflict_unchecked = true; |
| 71 | bool has_conflict_child = false; |
| 72 | bool has_indeterminate_child = false; |
| 73 | TreeItem *child_item = p_item->get_first_child(); |
| 74 | while (child_item) { |
| 75 | has_conflict_child |= _fix_conflicted_indeterminate_state(child_item, p_column); |
| 76 | Dictionary child_meta = child_item->get_metadata(p_column); |
| 77 | bool child_conflict = child_meta.get("is_conflict", false); |
| 78 | if (child_conflict) { |
| 79 | child_item->set_checked(p_column, false); |
| 80 | has_conflict_child = true; |
| 81 | } else { |
| 82 | bool child_checked = child_item->is_checked(p_column); |
| 83 | bool child_indeterminate = child_item->is_indeterminate(p_column); |
| 84 | all_non_conflict_checked &= (child_checked || child_indeterminate); |
| 85 | all_non_conflict_unchecked &= !child_checked; |
| 86 | has_indeterminate_child |= child_indeterminate; |
| 87 | } |
| 88 | child_item = child_item->get_next(); |
| 89 | } |
| 90 | if (has_indeterminate_child) { |
| 91 | p_item->set_indeterminate(p_column, true); |
| 92 | } else if (all_non_conflict_checked) { |
| 93 | p_item->set_checked(p_column, true); |
| 94 | } else if (all_non_conflict_unchecked) { |
| 95 | p_item->set_checked(p_column, false); |
| 96 | } |
| 97 | if (has_conflict_child) { |
| 98 | p_item->set_custom_color(p_column, get_theme_color(SNAME("error_color"), EditorStringName(Editor))); |
| 99 | } else { |
| 100 | p_item->clear_custom_color(p_column); |
| 101 | } |
| 102 | return has_conflict_child; |
| 103 | } |
| 104 | |
| 105 | bool EditorAssetInstaller::_is_item_checked(const String &p_source_path) const { |
| 106 | return file_item_map.has(p_source_path) && (file_item_map[p_source_path]->is_checked(0) || file_item_map[p_source_path]->is_indeterminate(0)); |
| 107 | } |
| 108 | |
| 109 | void EditorAssetInstaller::open_asset(const String &p_path, bool p_autoskip_toplevel) { |
| 110 | package_path = p_path; |
| 111 | asset_files.clear(); |
| 112 | |
| 113 | Ref<FileAccess> io_fa; |
| 114 | zlib_filefunc_def io = zipio_create_io(&io_fa); |
| 115 | |
| 116 | unzFile pkg = unzOpen2(p_path.utf8().get_data(), &io); |
| 117 | if (!pkg) { |
| 118 | EditorToaster::get_singleton()->popup_str(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name), EditorToaster::SEVERITY_ERROR); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | int ret = unzGoToFirstFile(pkg); |
| 123 | |
| 124 | while (ret == UNZ_OK) { |
| 125 | //get filename |
| 126 | unz_file_info info; |
| 127 | char fname[16384]; |
| 128 | unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0); |
| 129 | |
| 130 | String source_name = String::utf8(fname); |
| 131 | |
| 132 | // Create intermediate directories if they aren't reported by unzip. |
| 133 | // We are only interested in subfolders, so skip the root slash. |
| 134 | int separator = source_name.find_char('/', 1); |
| 135 | while (separator != -1) { |
| 136 | String dir_name = source_name.substr(0, separator + 1); |
| 137 | if (!dir_name.is_empty() && !asset_files.has(dir_name)) { |
| 138 | asset_files.insert(dir_name); |
| 139 | } |
| 140 | |
| 141 | separator = source_name.find_char('/', separator + 1); |
| 142 | } |
| 143 | |
| 144 | if (!source_name.is_empty() && !asset_files.has(source_name)) { |
| 145 | asset_files.insert(source_name); |
| 146 | } |
| 147 | |
| 148 | ret = unzGoToNextFile(pkg); |
| 149 | } |
| 150 | |
| 151 | unzClose(pkg); |
| 152 | |
| 153 | asset_title_label->set_text(asset_name); |
| 154 | |
| 155 | _check_has_toplevel(); |
| 156 | // Default to false, unless forced. |
| 157 | skip_toplevel = p_autoskip_toplevel; |
| 158 | skip_toplevel_check->set_block_signals(true); |
| 159 | skip_toplevel_check->set_pressed(!skip_toplevel_check->is_disabled() && skip_toplevel); |
| 160 | skip_toplevel_check->set_block_signals(false); |
| 161 | |
| 162 | _update_file_mappings(); |
| 163 | _rebuild_source_tree(); |
| 164 | _rebuild_destination_tree(); |
| 165 | |
| 166 | popup_centered_clamped(Size2(620, 640) * EDSCALE); |
| 167 | } |
| 168 | |
| 169 | void EditorAssetInstaller::_update_file_mappings() { |
| 170 | mapped_files.clear(); |
| 171 | |
| 172 | bool first = true; |
| 173 | for (const String &E : asset_files) { |
| 174 | if (first) { |
| 175 | first = false; |
| 176 | |
| 177 | if (!toplevel_prefix.is_empty() && skip_toplevel) { |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | String path = E; // We're going to mutate it. |
| 183 | if (!toplevel_prefix.is_empty() && skip_toplevel) { |
| 184 | path = path.trim_prefix(toplevel_prefix); |
| 185 | } |
| 186 | |
| 187 | mapped_files[E] = path; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void EditorAssetInstaller::_rebuild_source_tree() { |
| 192 | updating_source = true; |
| 193 | source_tree->clear(); |
| 194 | |
| 195 | TreeItem *root = source_tree->create_item(); |
| 196 | root->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); |
| 197 | root->set_checked(0, true); |
| 198 | root->set_icon(0, get_theme_icon(SNAME("folder"), SNAME("FileDialog"))); |
| 199 | root->set_text(0, "/"); |
| 200 | root->set_editable(0, true); |
| 201 | |
| 202 | file_item_map.clear(); |
| 203 | HashMap<String, TreeItem *> directory_item_map; |
| 204 | int num_file_conflicts = 0; |
| 205 | first_file_conflict = nullptr; |
| 206 | |
| 207 | for (const String &E : asset_files) { |
| 208 | String path = E; // We're going to mutate it. |
| 209 | |
| 210 | bool is_directory = false; |
| 211 | if (path.ends_with("/")) { |
| 212 | path = path.trim_suffix("/"); |
| 213 | is_directory = true; |
| 214 | } |
| 215 | |
| 216 | TreeItem *parent_item; |
| 217 | |
| 218 | int separator = path.rfind_char('/'); |
| 219 | if (separator == -1) { |
| 220 | parent_item = root; |
| 221 | } else { |
| 222 | String parent_path = path.substr(0, separator); |
| 223 | HashMap<String, TreeItem *>::Iterator I = directory_item_map.find(parent_path); |
| 224 | ERR_CONTINUE(!I); |
| 225 | parent_item = I->value; |
| 226 | } |
| 227 | |
| 228 | TreeItem *ti; |
| 229 | if (is_directory) { |
| 230 | ti = _create_dir_item(source_tree, parent_item, path, directory_item_map); |
| 231 | } else { |
| 232 | ti = _create_file_item(source_tree, parent_item, path, &num_file_conflicts); |
| 233 | } |
| 234 | file_item_map[E] = ti; |
| 235 | } |
| 236 | |
| 237 | _update_conflict_status(num_file_conflicts); |
| 238 | _update_confirm_button(); |
| 239 | |
| 240 | updating_source = false; |
| 241 | } |
| 242 | |
| 243 | void EditorAssetInstaller::_update_source_tree() { |
| 244 | int num_file_conflicts = 0; |
| 245 | first_file_conflict = nullptr; |
| 246 | |
| 247 | for (const KeyValue<String, TreeItem *> &E : file_item_map) { |
| 248 | TreeItem *ti = E.value; |
| 249 | Dictionary item_meta = ti->get_metadata(0); |
| 250 | if ((bool)item_meta.get("is_dir", false)) { |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | String asset_path = item_meta.get("asset_path", ""); |
| 255 | ERR_CONTINUE(asset_path.is_empty()); |
| 256 | |
| 257 | bool target_exists = _update_source_item_status(ti, asset_path); |
| 258 | if (target_exists) { |
| 259 | if (first_file_conflict == nullptr) { |
| 260 | first_file_conflict = ti; |
| 261 | } |
| 262 | num_file_conflicts += 1; |
| 263 | } |
| 264 | |
| 265 | item_meta["is_conflict"] = target_exists; |
| 266 | ti->set_metadata(0, item_meta); |
| 267 | } |
| 268 | |
| 269 | _update_conflict_status(num_file_conflicts); |
| 270 | _update_confirm_button(); |
| 271 | } |
| 272 | |
| 273 | bool EditorAssetInstaller::_update_source_item_status(TreeItem *p_item, const String &p_path) { |
| 274 | ERR_FAIL_COND_V(!mapped_files.has(p_path), false); |
| 275 | String target_path = target_dir_path.path_join(mapped_files[p_path]); |
| 276 | |
| 277 | bool target_exists = FileAccess::exists(target_path); |
| 278 | if (target_exists) { |
| 279 | p_item->set_custom_color(0, get_theme_color(SNAME("error_color"), EditorStringName(Editor))); |
| 280 | p_item->set_tooltip_text(0, vformat(TTR("%s (already exists)"), target_path)); |
| 281 | p_item->set_checked(0, false); |
| 282 | } else { |
| 283 | p_item->clear_custom_color(0); |
| 284 | p_item->set_tooltip_text(0, target_path); |
| 285 | p_item->set_checked(0, true); |
| 286 | } |
| 287 | |
| 288 | p_item->propagate_check(0); |
| 289 | _fix_conflicted_indeterminate_state(p_item->get_tree()->get_root(), 0); |
| 290 | return target_exists; |
| 291 | } |
| 292 | |
| 293 | void EditorAssetInstaller::_rebuild_destination_tree() { |
| 294 | destination_tree->clear(); |
| 295 | |
| 296 | TreeItem *root = destination_tree->create_item(); |
| 297 | root->set_icon(0, get_theme_icon(SNAME("folder"), SNAME("FileDialog"))); |
| 298 | root->set_text(0, target_dir_path + (target_dir_path == "res://" ? "" : "/")); |
| 299 | |
| 300 | HashMap<String, TreeItem *> directory_item_map; |
| 301 | |
| 302 | for (const KeyValue<String, String> &E : mapped_files) { |
| 303 | if (!_is_item_checked(E.key)) { |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | String path = E.value; // We're going to mutate it. |
| 308 | |
| 309 | bool is_directory = false; |
| 310 | if (path.ends_with("/")) { |
| 311 | path = path.trim_suffix("/"); |
| 312 | is_directory = true; |
| 313 | } |
| 314 | |
| 315 | TreeItem *parent_item; |
| 316 | |
| 317 | int separator = path.rfind_char('/'); |
| 318 | if (separator == -1) { |
| 319 | parent_item = root; |
| 320 | } else { |
| 321 | String parent_path = path.substr(0, separator); |
| 322 | HashMap<String, TreeItem *>::Iterator I = directory_item_map.find(parent_path); |
| 323 | ERR_CONTINUE(!I); |
| 324 | parent_item = I->value; |
| 325 | } |
| 326 | |
| 327 | if (is_directory) { |
| 328 | _create_dir_item(destination_tree, parent_item, path, directory_item_map); |
| 329 | } else { |
| 330 | int num_file_conflicts = 0; // Don't need it, but need to pass something. |
| 331 | _create_file_item(destination_tree, parent_item, path, &num_file_conflicts); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | TreeItem *EditorAssetInstaller::_create_dir_item(Tree *p_tree, TreeItem *p_parent, const String &p_path, HashMap<String, TreeItem *> &p_item_map) { |
| 337 | TreeItem *ti = p_tree->create_item(p_parent); |
| 338 | |
| 339 | if (p_tree == source_tree) { |
| 340 | ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); |
| 341 | ti->set_editable(0, true); |
| 342 | ti->set_checked(0, true); |
| 343 | ti->propagate_check(0); |
| 344 | _fix_conflicted_indeterminate_state(ti->get_tree()->get_root(), 0); |
| 345 | |
| 346 | Dictionary meta; |
| 347 | meta["asset_path"] = p_path + "/"; |
| 348 | meta["is_dir"] = true; |
| 349 | meta["is_conflict"] = false; |
| 350 | ti->set_metadata(0, meta); |
| 351 | } |
| 352 | |
| 353 | ti->set_text(0, p_path.get_file() + "/"); |
| 354 | ti->set_icon(0, get_theme_icon(SNAME("folder"), SNAME("FileDialog"))); |
| 355 | |
| 356 | p_item_map[p_path] = ti; |
| 357 | return ti; |
| 358 | } |
| 359 | |
| 360 | TreeItem *EditorAssetInstaller::_create_file_item(Tree *p_tree, TreeItem *p_parent, const String &p_path, int *r_conflicts) { |
| 361 | TreeItem *ti = p_tree->create_item(p_parent); |
| 362 | |
| 363 | if (p_tree == source_tree) { |
| 364 | ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); |
| 365 | ti->set_editable(0, true); |
| 366 | |
| 367 | bool target_exists = _update_source_item_status(ti, p_path); |
| 368 | if (target_exists) { |
| 369 | if (first_file_conflict == nullptr) { |
| 370 | first_file_conflict = ti; |
| 371 | } |
| 372 | *r_conflicts += 1; |
| 373 | } |
| 374 | |
| 375 | Dictionary meta; |
| 376 | meta["asset_path"] = p_path; |
| 377 | meta["is_dir"] = false; |
| 378 | meta["is_conflict"] = target_exists; |
| 379 | ti->set_metadata(0, meta); |
| 380 | } |
| 381 | |
| 382 | String file = p_path.get_file(); |
| 383 | String extension = file.get_extension().to_lower(); |
| 384 | if (extension_icon_map.has(extension)) { |
| 385 | ti->set_icon(0, extension_icon_map[extension]); |
| 386 | } else { |
| 387 | ti->set_icon(0, generic_extension_icon); |
| 388 | } |
| 389 | ti->set_text(0, file); |
| 390 | |
| 391 | return ti; |
| 392 | } |
| 393 | |
| 394 | void EditorAssetInstaller::_update_conflict_status(int p_conflicts) { |
| 395 | if (p_conflicts >= 1) { |
| 396 | asset_conflicts_link->set_text(vformat(TTRN("%d file conflicts with your project and won't be installed", "%d files conflict with your project and won't be installed", p_conflicts), p_conflicts)); |
| 397 | asset_conflicts_link->show(); |
| 398 | asset_conflicts_label->hide(); |
| 399 | } else { |
| 400 | asset_conflicts_link->hide(); |
| 401 | asset_conflicts_label->show(); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void EditorAssetInstaller::_update_confirm_button() { |
| 406 | TreeItem *root = source_tree->get_root(); |
| 407 | get_ok_button()->set_disabled(!root || (!root->is_checked(0) && !root->is_indeterminate(0))); |
| 408 | } |
| 409 | |
| 410 | void EditorAssetInstaller::_toggle_source_tree(bool p_visible, bool p_scroll_to_error) { |
| 411 | source_tree_vb->set_visible(p_visible); |
| 412 | show_source_files_button->set_pressed_no_signal(p_visible); // To keep in sync if triggered by something else. |
| 413 | |
| 414 | if (p_visible) { |
| 415 | show_source_files_button->set_button_icon(get_editor_theme_icon(SNAME("Back"))); |
| 416 | destination_tree_mc->set_theme_type_variation(""); |
| 417 | destination_tree->set_theme_type_variation("TreeSecondary"); |
| 418 | destination_tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_DISABLED); |
| 419 | } else { |
| 420 | show_source_files_button->set_button_icon(get_editor_theme_icon(SNAME("Forward"))); |
| 421 | destination_tree_mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 422 | destination_tree->set_theme_type_variation(""); |
| 423 | destination_tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH); |
| 424 | } |
| 425 | |
| 426 | if (p_visible && p_scroll_to_error && first_file_conflict) { |
| 427 | source_tree->scroll_to_item(first_file_conflict, true); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void EditorAssetInstaller::_check_has_toplevel() { |
| 432 | // Check if the file structure has a distinct top-level directory. This is typical |
| 433 | // for archives generated by GitHub, etc, but not for manually created ZIPs. |
| 434 | |
| 435 | toplevel_prefix = ""; |
| 436 | skip_toplevel_check->set_pressed(false); |
| 437 | skip_toplevel_check->set_disabled(true); |
| 438 | skip_toplevel_check->set_tooltip_text(TTRC("This asset doesn't have a root directory, so it can't be ignored.")); |
| 439 | |
| 440 | if (asset_files.is_empty()) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | String first_asset; |
| 445 | for (const String &E : asset_files) { |
| 446 | if (first_asset.is_empty()) { // Checking the first file/directory. |
| 447 | if (!E.ends_with("/")) { |
| 448 | return; // No directories in this asset. |
| 449 | } |
| 450 | |
| 451 | // We will match everything else against this directory. |
| 452 | first_asset = E; |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | if (!E.begins_with(first_asset)) { |
| 457 | return; // Found a file or a directory that doesn't share the same base path. |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | toplevel_prefix = first_asset; |
| 462 | skip_toplevel_check->set_disabled(false); |
| 463 | skip_toplevel_check->set_tooltip_text(TTRC("Ignore the root directory when extracting files.")); |
| 464 | } |
| 465 | |
| 466 | void EditorAssetInstaller::_set_skip_toplevel(bool p_checked) { |
| 467 | if (skip_toplevel == p_checked) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | skip_toplevel = p_checked; |
| 472 | _update_file_mappings(); |
| 473 | _update_source_tree(); |
| 474 | _rebuild_destination_tree(); |
| 475 | } |
| 476 | |
| 477 | void EditorAssetInstaller::_open_target_dir_dialog() { |
| 478 | if (!target_dir_dialog) { |
| 479 | target_dir_dialog = memnew(EditorFileDialog); |
| 480 | target_dir_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR); |
| 481 | target_dir_dialog->set_title(TTRC("Select Install Folder")); |
| 482 | target_dir_dialog->set_current_dir(target_dir_path); |
| 483 | target_dir_dialog->connect("dir_selected", callable_mp(this, &EditorAssetInstaller::_target_dir_selected)); |
| 484 | add_child(target_dir_dialog); |
| 485 | } |
| 486 | |
| 487 | target_dir_dialog->popup_file_dialog(); |
| 488 | } |
| 489 | |
| 490 | void EditorAssetInstaller::_target_dir_selected(const String &p_target_path) { |
| 491 | if (target_dir_path == p_target_path) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | target_dir_path = p_target_path; |
| 496 | _update_file_mappings(); |
| 497 | _update_source_tree(); |
| 498 | _rebuild_destination_tree(); |
| 499 | } |
| 500 | |
| 501 | void EditorAssetInstaller::ok_pressed() { |
| 502 | _install_asset(); |
| 503 | } |
| 504 | |
| 505 | void EditorAssetInstaller::_install_asset() { |
| 506 | Ref<FileAccess> io_fa; |
| 507 | zlib_filefunc_def io = zipio_create_io(&io_fa); |
| 508 | |
| 509 | unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io); |
| 510 | if (!pkg) { |
| 511 | EditorToaster::get_singleton()->popup_str(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name), EditorToaster::SEVERITY_ERROR); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | Vector<String> failed_files; |
| 516 | int ret = unzGoToFirstFile(pkg); |
| 517 | |
| 518 | ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), file_item_map.size()); |
| 519 | |
| 520 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 521 | for (int idx = 0; ret == UNZ_OK; ret = unzGoToNextFile(pkg), idx++) { |
| 522 | unz_file_info info; |
| 523 | char fname[16384]; |
| 524 | ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0); |
| 525 | if (ret != UNZ_OK) { |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | String source_name = String::utf8(fname); |
| 530 | if (!_is_item_checked(source_name)) { |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | HashMap<String, String>::Iterator E = mapped_files.find(source_name); |
| 535 | if (!E) { |
| 536 | continue; // No remapped path means we don't want it; most likely the root. |
| 537 | } |
| 538 | |
| 539 | String target_path = target_dir_path.path_join(E->value); |
| 540 | |
| 541 | Dictionary asset_meta = file_item_map[source_name]->get_metadata(0); |
| 542 | bool is_dir = asset_meta.get("is_dir", false); |
| 543 | if (is_dir) { |
| 544 | if (target_path.ends_with("/")) { |
| 545 | target_path = target_path.substr(0, target_path.length() - 1); |
| 546 | } |
| 547 | |
| 548 | da->make_dir_recursive(target_path); |
| 549 | } else { |
| 550 | Vector<uint8_t> uncomp_data; |
| 551 | uncomp_data.resize(info.uncompressed_size); |
| 552 | |
| 553 | unzOpenCurrentFile(pkg); |
| 554 | unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size()); |
| 555 | unzCloseCurrentFile(pkg); |
| 556 | |
| 557 | // Ensure that the target folder exists. |
| 558 | da->make_dir_recursive(target_path.get_base_dir()); |
| 559 | |
| 560 | Ref<FileAccess> f = FileAccess::open(target_path, FileAccess::WRITE); |
| 561 | if (f.is_valid()) { |
| 562 | f->store_buffer(uncomp_data.ptr(), uncomp_data.size()); |
| 563 | } else { |
| 564 | failed_files.push_back(target_path); |
| 565 | } |
| 566 | |
| 567 | ProgressDialog::get_singleton()->task_step("uncompress", target_path, idx); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | ProgressDialog::get_singleton()->end_task("uncompress"); |
| 572 | unzClose(pkg); |
| 573 | |
| 574 | if (failed_files.size()) { |
| 575 | String msg = vformat(TTR("The following files failed extraction from asset \"%s\":"), asset_name) + "\n\n"; |
| 576 | for (int i = 0; i < failed_files.size(); i++) { |
| 577 | if (i > 10) { |
| 578 | msg += "\n" + vformat(TTR("(and %s more files)"), itos(failed_files.size() - i)); |
| 579 | break; |
| 580 | } |
| 581 | msg += "\n" + failed_files[i]; |
| 582 | } |
| 583 | if (EditorNode::get_singleton() != nullptr) { |
| 584 | EditorNode::get_singleton()->show_warning(msg); |
| 585 | } |
| 586 | } else { |
| 587 | if (EditorNode::get_singleton() != nullptr) { |
| 588 | EditorNode::get_singleton()->show_warning(vformat(TTR("Asset \"%s\" installed successfully!"), asset_name), TTRC("Success!")); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | EditorFileSystem::get_singleton()->scan_changes(); |
| 593 | } |
| 594 | |
| 595 | void EditorAssetInstaller::set_asset_name(const String &p_asset_name) { |
| 596 | asset_name = p_asset_name; |
| 597 | } |
| 598 | |
| 599 | String EditorAssetInstaller::get_asset_name() const { |
| 600 | return asset_name; |
| 601 | } |
| 602 | |
| 603 | void EditorAssetInstaller::_notification(int p_what) { |
| 604 | switch (p_what) { |
| 605 | case NOTIFICATION_THEME_CHANGED: { |
| 606 | if (show_source_files_button->is_pressed()) { |
| 607 | show_source_files_button->set_button_icon(get_editor_theme_icon(SNAME("Back"))); |
| 608 | } else { |
| 609 | show_source_files_button->set_button_icon(get_editor_theme_icon(SNAME("Forward"))); |
| 610 | } |
| 611 | asset_conflicts_link->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor))); |
| 612 | |
| 613 | generic_extension_icon = get_editor_theme_icon(SNAME("Object")); |
| 614 | |
| 615 | extension_icon_map.clear(); |
| 616 | { |
| 617 | extension_icon_map["bmp"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 618 | extension_icon_map["dds"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 619 | extension_icon_map["exr"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 620 | extension_icon_map["hdr"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 621 | extension_icon_map["jpg"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 622 | extension_icon_map["jpeg"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 623 | extension_icon_map["png"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 624 | extension_icon_map["svg"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 625 | extension_icon_map["tga"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 626 | extension_icon_map["webp"] = get_editor_theme_icon(SNAME("ImageTexture")); |
| 627 | |
| 628 | extension_icon_map["wav"] = get_editor_theme_icon(SNAME("AudioStreamWAV")); |
| 629 | extension_icon_map["ogg"] = get_editor_theme_icon(SNAME("AudioStreamOggVorbis")); |
| 630 | extension_icon_map["mp3"] = get_editor_theme_icon(SNAME("AudioStreamMP3")); |
| 631 | |
| 632 | extension_icon_map["scn"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 633 | extension_icon_map["tscn"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 634 | extension_icon_map["escn"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 635 | extension_icon_map["dae"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 636 | extension_icon_map["gltf"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 637 | extension_icon_map["glb"] = get_editor_theme_icon(SNAME("PackedScene")); |
| 638 | |
| 639 | extension_icon_map["gdshader"] = get_editor_theme_icon(SNAME("Shader")); |
| 640 | extension_icon_map["gdshaderinc"] = get_editor_theme_icon(SNAME("TextFile")); |
| 641 | extension_icon_map["gd"] = get_editor_theme_icon(SNAME("GDScript")); |
| 642 | if (ClassDB::class_exists("CSharpScript")) { |
| 643 | extension_icon_map["cs"] = get_editor_theme_icon(SNAME("CSharpScript")); |
| 644 | } else { |
| 645 | // Mark C# support as unavailable. |
| 646 | extension_icon_map["cs"] = get_editor_theme_icon(SNAME("ImportFail")); |
| 647 | } |
| 648 | |
| 649 | extension_icon_map["res"] = get_editor_theme_icon(SNAME("Resource")); |
| 650 | extension_icon_map["tres"] = get_editor_theme_icon(SNAME("Resource")); |
| 651 | extension_icon_map["atlastex"] = get_editor_theme_icon(SNAME("AtlasTexture")); |
| 652 | // By default, OBJ files are imported as Mesh resources rather than PackedScenes. |
| 653 | extension_icon_map["obj"] = get_editor_theme_icon(SNAME("MeshItem")); |
| 654 | |
| 655 | extension_icon_map["txt"] = get_editor_theme_icon(SNAME("TextFile")); |
| 656 | extension_icon_map["md"] = get_editor_theme_icon(SNAME("TextFile")); |
| 657 | extension_icon_map["rst"] = get_editor_theme_icon(SNAME("TextFile")); |
| 658 | extension_icon_map["json"] = get_editor_theme_icon(SNAME("TextFile")); |
| 659 | extension_icon_map["yml"] = get_editor_theme_icon(SNAME("TextFile")); |
| 660 | extension_icon_map["yaml"] = get_editor_theme_icon(SNAME("TextFile")); |
| 661 | extension_icon_map["toml"] = get_editor_theme_icon(SNAME("TextFile")); |
| 662 | extension_icon_map["cfg"] = get_editor_theme_icon(SNAME("TextFile")); |
| 663 | extension_icon_map["ini"] = get_editor_theme_icon(SNAME("TextFile")); |
| 664 | } |
| 665 | } break; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | EditorAssetInstaller::EditorAssetInstaller() { |
| 670 | VBoxContainer *vb = memnew(VBoxContainer); |
| 671 | add_child(vb); |
| 672 | |
| 673 | // Status bar. |
| 674 | |
| 675 | HBoxContainer *asset_status = memnew(HBoxContainer); |
| 676 | vb->add_child(asset_status); |
| 677 | |
| 678 | Label *asset_label = memnew(Label); |
| 679 | asset_label->set_text(TTRC("Asset:")); |
| 680 | asset_label->set_theme_type_variation("HeaderSmall"); |
| 681 | asset_status->add_child(asset_label); |
| 682 | |
| 683 | asset_title_label = memnew(Label); |
| 684 | asset_title_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 685 | asset_status->add_child(asset_title_label); |
| 686 | |
| 687 | // File remapping controls. |
| 688 | |
| 689 | HBoxContainer *remapping_tools = memnew(HBoxContainer); |
| 690 | vb->add_child(remapping_tools); |
| 691 | |
| 692 | show_source_files_button = memnew(Button); |
| 693 | show_source_files_button->set_toggle_mode(true); |
| 694 | show_source_files_button->set_tooltip_text(TTRC("Open the list of the asset contents and select which files to install.")); |
| 695 | remapping_tools->add_child(show_source_files_button); |
| 696 | show_source_files_button->connect(SceneStringName(toggled), callable_mp(this, &EditorAssetInstaller::_toggle_source_tree).bind(false)); |
| 697 | |
| 698 | Button *target_dir_button = memnew(Button); |
| 699 | target_dir_button->set_text(TTRC("Change Install Folder")); |
| 700 | target_dir_button->set_tooltip_text(TTRC("Change the folder where the contents of the asset are going to be installed.")); |
| 701 | remapping_tools->add_child(target_dir_button); |
| 702 | target_dir_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetInstaller::_open_target_dir_dialog)); |
| 703 | |
| 704 | remapping_tools->add_child(memnew(VSeparator)); |
| 705 | |
| 706 | skip_toplevel_check = memnew(CheckBox); |
| 707 | skip_toplevel_check->set_text(TTRC("Ignore asset root")); |
| 708 | skip_toplevel_check->set_tooltip_text(TTRC("Ignore the root directory when extracting files.")); |
| 709 | skip_toplevel_check->connect(SceneStringName(toggled), callable_mp(this, &EditorAssetInstaller::_set_skip_toplevel)); |
| 710 | remapping_tools->add_child(skip_toplevel_check); |
| 711 | |
| 712 | remapping_tools->add_spacer(); |
| 713 | |
| 714 | asset_conflicts_label = memnew(Label); |
| 715 | asset_conflicts_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 716 | asset_conflicts_label->set_theme_type_variation("HeaderSmall"); |
| 717 | asset_conflicts_label->set_text(TTRC("No files conflict with your project")); |
| 718 | remapping_tools->add_child(asset_conflicts_label); |
| 719 | asset_conflicts_link = memnew(LinkButton); |
| 720 | asset_conflicts_link->set_theme_type_variation("HeaderSmallLink"); |
| 721 | asset_conflicts_link->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 722 | asset_conflicts_link->set_tooltip_text(TTRC("Show contents of the asset and conflicting files.")); |
| 723 | asset_conflicts_link->set_visible(false); |
| 724 | remapping_tools->add_child(asset_conflicts_link); |
| 725 | asset_conflicts_link->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetInstaller::_toggle_source_tree).bind(true, true)); |
| 726 | |
| 727 | // File hierarchy trees. |
| 728 | |
| 729 | HSplitContainer *tree_split = memnew(HSplitContainer); |
| 730 | tree_split->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 731 | vb->add_child(tree_split); |
| 732 | |
| 733 | source_tree_vb = memnew(VBoxContainer); |
| 734 | source_tree_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 735 | source_tree_vb->set_visible(show_source_files_button->is_pressed()); |
| 736 | tree_split->add_child(source_tree_vb); |
| 737 | |
| 738 | Label *source_tree_label = memnew(Label); |
| 739 | source_tree_label->set_text(TTRC("Contents of the asset:")); |
| 740 | source_tree_label->set_theme_type_variation("HeaderSmall"); |
| 741 | source_tree_vb->add_child(source_tree_label); |
| 742 | |
| 743 | source_tree = memnew(Tree); |
| 744 | source_tree->set_accessibility_name(TTRC("Source Files")); |
| 745 | source_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 746 | source_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 747 | source_tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_checked_cbk)); |
| 748 | source_tree->set_theme_type_variation("TreeSecondary"); |
| 749 | source_tree_vb->add_child(source_tree); |
| 750 | |
| 751 | VBoxContainer *destination_tree_vb = memnew(VBoxContainer); |
| 752 | destination_tree_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 753 | tree_split->add_child(destination_tree_vb); |
| 754 | |
| 755 | Label *destination_tree_label = memnew(Label); |
| 756 | destination_tree_label->set_text(TTRC("Installation preview:")); |
| 757 | destination_tree_label->set_theme_type_variation("HeaderSmall"); |
| 758 | destination_tree_vb->add_child(destination_tree_label); |
| 759 | |
| 760 | destination_tree_mc = memnew(MarginContainer); |
| 761 | destination_tree_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 762 | destination_tree_mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 763 | destination_tree_vb->add_child(destination_tree_mc); |
| 764 | |
| 765 | destination_tree = memnew(Tree); |
| 766 | destination_tree->set_accessibility_name(TTRC("Destination Files")); |
| 767 | destination_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 768 | destination_tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH); |
| 769 | destination_tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_checked_cbk)); |
| 770 | destination_tree_mc->add_child(destination_tree); |
| 771 | |
| 772 | // Dialog configuration. |
| 773 | |
| 774 | set_title(TTRC("Configure Asset Before Installing")); |
| 775 | set_ok_button_text(TTRC("Install")); |
| 776 | set_hide_on_ok(true); |
| 777 | } |