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_file_system.h"
37#include "editor/editor_node.h"
38#include "editor/editor_string_names.h"
39#include "editor/gui/editor_toaster.h"
40#include "editor/progress_dialog.h"
41#include "scene/gui/check_box.h"
42#include "scene/gui/label.h"
43
44void EditorAssetInstaller::_item_checked() {
45 if (updating || !tree->get_edited()) {
46 return;
47 }
48
49 updating = true;
50 TreeItem *item = tree->get_edited();
51 item->propagate_check(0);
52 updating = false;
53}
54
55void EditorAssetInstaller::_check_propagated_to_item(Object *p_obj, int column) {
56 TreeItem *affected_item = Object::cast_to<TreeItem>(p_obj);
57 if (affected_item && affected_item->get_custom_color(0) != Color()) {
58 affected_item->set_checked(0, false);
59 affected_item->propagate_check(0, false);
60 }
61}
62
63void EditorAssetInstaller::open_asset(const String &p_path, bool p_autoskip_toplevel) {
64 package_path = p_path;
65 asset_files.clear();
66
67 Ref<FileAccess> io_fa;
68 zlib_filefunc_def io = zipio_create_io(&io_fa);
69
70 unzFile pkg = unzOpen2(p_path.utf8().get_data(), &io);
71 if (!pkg) {
72 EditorToaster::get_singleton()->popup_str(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name), EditorToaster::SEVERITY_ERROR);
73 return;
74 }
75
76 int ret = unzGoToFirstFile(pkg);
77
78 while (ret == UNZ_OK) {
79 //get filename
80 unz_file_info info;
81 char fname[16384];
82 unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
83
84 String source_name = String::utf8(fname);
85
86 // Create intermediate directories if they aren't reported by unzip.
87 // We are only interested in subfolders, so skip the root slash.
88 int separator = source_name.find("/", 1);
89 while (separator != -1) {
90 String dir_name = source_name.substr(0, separator + 1);
91 if (!dir_name.is_empty() && !asset_files.has(dir_name)) {
92 asset_files.insert(dir_name);
93 }
94
95 separator = source_name.find("/", separator + 1);
96 }
97
98 if (!source_name.is_empty() && !asset_files.has(source_name)) {
99 asset_files.insert(source_name);
100 }
101
102 ret = unzGoToNextFile(pkg);
103 }
104
105 unzClose(pkg);
106
107 _check_has_toplevel();
108 // Default to false, unless forced.
109 skip_toplevel = p_autoskip_toplevel;
110 skip_toplevel_check->set_pressed(!skip_toplevel_check->is_disabled() && skip_toplevel);
111
112 _rebuild_tree();
113}
114
115void EditorAssetInstaller::_rebuild_tree() {
116 updating = true;
117 tree->clear();
118
119 TreeItem *root = tree->create_item();
120 root->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
121 root->set_checked(0, true);
122 root->set_icon(0, get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
123 root->set_text(0, "res://");
124 root->set_editable(0, true);
125
126 HashMap<String, TreeItem *> directory_item_map;
127 int num_file_conflicts = 0;
128
129 bool first = true;
130 for (const String &E : asset_files) {
131 if (first) {
132 first = false;
133
134 if (!toplevel_prefix.is_empty() && skip_toplevel) {
135 continue;
136 }
137 }
138
139 String path = E; // We're going to mutate it.
140 if (!toplevel_prefix.is_empty() && skip_toplevel) {
141 path = path.trim_prefix(toplevel_prefix);
142 }
143
144 bool is_directory = false;
145 if (path.ends_with("/")) {
146 // Directory.
147 path = path.trim_suffix("/");
148 is_directory = true;
149 }
150
151 TreeItem *parent_item;
152
153 int separator = path.rfind("/");
154 if (separator == -1) {
155 parent_item = root;
156 } else {
157 String parent_path = path.substr(0, separator);
158 HashMap<String, TreeItem *>::Iterator I = directory_item_map.find(parent_path);
159 ERR_CONTINUE(!I);
160 parent_item = I->value;
161 }
162
163 TreeItem *ti;
164 if (is_directory) {
165 ti = _create_dir_item(parent_item, path, directory_item_map);
166 } else {
167 ti = _create_file_item(parent_item, path, &num_file_conflicts);
168 }
169 file_item_map[E] = ti;
170 }
171
172 asset_title_label->set_text(asset_name);
173 if (num_file_conflicts >= 1) {
174 asset_conflicts_label->set_text(vformat(TTRN("%d file conflicts with your project", "%d files conflict with your project", num_file_conflicts), num_file_conflicts));
175 asset_conflicts_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
176 } else {
177 asset_conflicts_label->set_text(TTR("No files conflict with your project"));
178 asset_conflicts_label->remove_theme_color_override("font_color");
179 }
180
181 popup_centered_ratio(0.5);
182 updating = false;
183}
184
185TreeItem *EditorAssetInstaller::_create_dir_item(TreeItem *p_parent, const String &p_path, HashMap<String, TreeItem *> &p_item_map) {
186 TreeItem *ti = tree->create_item(p_parent);
187 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
188 ti->set_checked(0, true);
189 ti->set_editable(0, true);
190
191 ti->set_text(0, p_path.get_file() + "/");
192 ti->set_icon(0, get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
193 ti->set_metadata(0, String());
194
195 p_item_map[p_path] = ti;
196 return ti;
197}
198
199TreeItem *EditorAssetInstaller::_create_file_item(TreeItem *p_parent, const String &p_path, int *r_conflicts) {
200 TreeItem *ti = tree->create_item(p_parent);
201 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
202 ti->set_checked(0, true);
203 ti->set_editable(0, true);
204
205 String file = p_path.get_file();
206 String extension = file.get_extension().to_lower();
207 if (extension_icon_map.has(extension)) {
208 ti->set_icon(0, extension_icon_map[extension]);
209 } else {
210 ti->set_icon(0, generic_extension_icon);
211 }
212 ti->set_text(0, file);
213
214 String res_path = "res://" + p_path;
215 if (FileAccess::exists(res_path)) {
216 *r_conflicts += 1;
217 ti->set_custom_color(0, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
218 ti->set_tooltip_text(0, vformat(TTR("%s (already exists)"), res_path));
219 ti->set_checked(0, false);
220 ti->propagate_check(0);
221 } else {
222 ti->set_tooltip_text(0, res_path);
223 }
224
225 ti->set_metadata(0, res_path);
226
227 return ti;
228}
229
230void EditorAssetInstaller::_check_has_toplevel() {
231 // Check if the file structure has a distinct top-level directory. This is typical
232 // for archives generated by GitHub, etc, but not for manually created ZIPs.
233
234 toplevel_prefix = "";
235 skip_toplevel_check->set_pressed(false);
236 skip_toplevel_check->set_disabled(true);
237 skip_toplevel_check->set_tooltip_text(TTR("This asset doesn't have a root directory, so it can't be ignored."));
238
239 if (asset_files.is_empty()) {
240 return;
241 }
242
243 String first_asset;
244 for (const String &E : asset_files) {
245 if (first_asset.is_empty()) { // Checking the first file/directory.
246 if (!E.ends_with("/")) {
247 return; // No directories in this asset.
248 }
249
250 // We will match everything else against this directory.
251 first_asset = E;
252 continue;
253 }
254
255 if (!E.begins_with(first_asset)) {
256 return; // Found a file or a directory that doesn't share the same base path.
257 }
258 }
259
260 toplevel_prefix = first_asset;
261 skip_toplevel_check->set_disabled(false);
262 skip_toplevel_check->set_tooltip_text("");
263}
264
265void EditorAssetInstaller::_set_skip_toplevel(bool p_checked) {
266 skip_toplevel = p_checked;
267 _rebuild_tree();
268}
269
270void EditorAssetInstaller::ok_pressed() {
271 _install_asset();
272}
273
274void EditorAssetInstaller::_install_asset() {
275 Ref<FileAccess> io_fa;
276 zlib_filefunc_def io = zipio_create_io(&io_fa);
277
278 unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
279 if (!pkg) {
280 EditorToaster::get_singleton()->popup_str(vformat(TTR("Error opening asset file for \"%s\" (not in ZIP format)."), asset_name), EditorToaster::SEVERITY_ERROR);
281 return;
282 }
283
284 Vector<String> failed_files;
285 int ret = unzGoToFirstFile(pkg);
286
287 ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), file_item_map.size());
288
289 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
290 for (int idx = 0; ret == UNZ_OK; ret = unzGoToNextFile(pkg), idx++) {
291 unz_file_info info;
292 char fname[16384];
293 ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
294 if (ret != UNZ_OK) {
295 break;
296 }
297
298 String source_name = String::utf8(fname);
299 if (!file_item_map.has(source_name) || (!file_item_map[source_name]->is_checked(0) && !file_item_map[source_name]->is_indeterminate(0))) {
300 continue;
301 }
302
303 String path = file_item_map[source_name]->get_metadata(0);
304 if (path.is_empty()) { // Directory.
305 // TODO: Metadata can be used to store the entire path of directories too,
306 // so this tree iteration can be avoided.
307 String dir_path;
308 TreeItem *t = file_item_map[source_name];
309 while (t) {
310 dir_path = t->get_text(0) + dir_path;
311 t = t->get_parent();
312 }
313
314 if (dir_path.ends_with("/")) {
315 dir_path = dir_path.substr(0, dir_path.length() - 1);
316 }
317
318 da->make_dir_recursive(dir_path);
319 } else {
320 Vector<uint8_t> uncomp_data;
321 uncomp_data.resize(info.uncompressed_size);
322
323 unzOpenCurrentFile(pkg);
324 unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
325 unzCloseCurrentFile(pkg);
326
327 // Ensure that the target folder exists.
328 da->make_dir_recursive(path.get_base_dir());
329
330 Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE);
331 if (f.is_valid()) {
332 f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
333 } else {
334 failed_files.push_back(path);
335 }
336
337 ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
338 }
339 }
340
341 ProgressDialog::get_singleton()->end_task("uncompress");
342 unzClose(pkg);
343
344 if (failed_files.size()) {
345 String msg = vformat(TTR("The following files failed extraction from asset \"%s\":"), asset_name) + "\n\n";
346 for (int i = 0; i < failed_files.size(); i++) {
347 if (i > 10) {
348 msg += "\n" + vformat(TTR("(and %s more files)"), itos(failed_files.size() - i));
349 break;
350 }
351 msg += "\n" + failed_files[i];
352 }
353 if (EditorNode::get_singleton() != nullptr) {
354 EditorNode::get_singleton()->show_warning(msg);
355 }
356 } else {
357 if (EditorNode::get_singleton() != nullptr) {
358 EditorNode::get_singleton()->show_warning(vformat(TTR("Asset \"%s\" installed successfully!"), asset_name), TTR("Success!"));
359 }
360 }
361 EditorFileSystem::get_singleton()->scan_changes();
362}
363
364void EditorAssetInstaller::set_asset_name(const String &p_asset_name) {
365 asset_name = p_asset_name;
366}
367
368String EditorAssetInstaller::get_asset_name() const {
369 return asset_name;
370}
371
372void EditorAssetInstaller::_notification(int p_what) {
373 switch (p_what) {
374 case NOTIFICATION_THEME_CHANGED: {
375 generic_extension_icon = get_editor_theme_icon(SNAME("Object"));
376
377 extension_icon_map.clear();
378 {
379 extension_icon_map["bmp"] = get_editor_theme_icon(SNAME("ImageTexture"));
380 extension_icon_map["dds"] = get_editor_theme_icon(SNAME("ImageTexture"));
381 extension_icon_map["exr"] = get_editor_theme_icon(SNAME("ImageTexture"));
382 extension_icon_map["hdr"] = get_editor_theme_icon(SNAME("ImageTexture"));
383 extension_icon_map["jpg"] = get_editor_theme_icon(SNAME("ImageTexture"));
384 extension_icon_map["jpeg"] = get_editor_theme_icon(SNAME("ImageTexture"));
385 extension_icon_map["png"] = get_editor_theme_icon(SNAME("ImageTexture"));
386 extension_icon_map["svg"] = get_editor_theme_icon(SNAME("ImageTexture"));
387 extension_icon_map["tga"] = get_editor_theme_icon(SNAME("ImageTexture"));
388 extension_icon_map["webp"] = get_editor_theme_icon(SNAME("ImageTexture"));
389
390 extension_icon_map["wav"] = get_editor_theme_icon(SNAME("AudioStreamWAV"));
391 extension_icon_map["ogg"] = get_editor_theme_icon(SNAME("AudioStreamOggVorbis"));
392 extension_icon_map["mp3"] = get_editor_theme_icon(SNAME("AudioStreamMP3"));
393
394 extension_icon_map["scn"] = get_editor_theme_icon(SNAME("PackedScene"));
395 extension_icon_map["tscn"] = get_editor_theme_icon(SNAME("PackedScene"));
396 extension_icon_map["escn"] = get_editor_theme_icon(SNAME("PackedScene"));
397 extension_icon_map["dae"] = get_editor_theme_icon(SNAME("PackedScene"));
398 extension_icon_map["gltf"] = get_editor_theme_icon(SNAME("PackedScene"));
399 extension_icon_map["glb"] = get_editor_theme_icon(SNAME("PackedScene"));
400
401 extension_icon_map["gdshader"] = get_editor_theme_icon(SNAME("Shader"));
402 extension_icon_map["gdshaderinc"] = get_editor_theme_icon(SNAME("TextFile"));
403 extension_icon_map["gd"] = get_editor_theme_icon(SNAME("GDScript"));
404 if (Engine::get_singleton()->has_singleton("GodotSharp")) {
405 extension_icon_map["cs"] = get_editor_theme_icon(SNAME("CSharpScript"));
406 } else {
407 // Mark C# support as unavailable.
408 extension_icon_map["cs"] = get_editor_theme_icon(SNAME("ImportFail"));
409 }
410
411 extension_icon_map["res"] = get_editor_theme_icon(SNAME("Resource"));
412 extension_icon_map["tres"] = get_editor_theme_icon(SNAME("Resource"));
413 extension_icon_map["atlastex"] = get_editor_theme_icon(SNAME("AtlasTexture"));
414 // By default, OBJ files are imported as Mesh resources rather than PackedScenes.
415 extension_icon_map["obj"] = get_editor_theme_icon(SNAME("Mesh"));
416
417 extension_icon_map["txt"] = get_editor_theme_icon(SNAME("TextFile"));
418 extension_icon_map["md"] = get_editor_theme_icon(SNAME("TextFile"));
419 extension_icon_map["rst"] = get_editor_theme_icon(SNAME("TextFile"));
420 extension_icon_map["json"] = get_editor_theme_icon(SNAME("TextFile"));
421 extension_icon_map["yml"] = get_editor_theme_icon(SNAME("TextFile"));
422 extension_icon_map["yaml"] = get_editor_theme_icon(SNAME("TextFile"));
423 extension_icon_map["toml"] = get_editor_theme_icon(SNAME("TextFile"));
424 extension_icon_map["cfg"] = get_editor_theme_icon(SNAME("TextFile"));
425 extension_icon_map["ini"] = get_editor_theme_icon(SNAME("TextFile"));
426 }
427 } break;
428 }
429}
430
431void EditorAssetInstaller::_bind_methods() {
432}
433
434EditorAssetInstaller::EditorAssetInstaller() {
435 VBoxContainer *vb = memnew(VBoxContainer);
436 add_child(vb);
437
438 HBoxContainer *asset_status = memnew(HBoxContainer);
439 vb->add_child(asset_status);
440
441 Label *asset_label = memnew(Label);
442 asset_label->set_text(TTR("Contents of asset:"));
443 asset_status->add_child(asset_label);
444
445 asset_title_label = memnew(Label);
446 asset_title_label->set_theme_type_variation("HeaderSmall");
447 asset_status->add_child(asset_title_label);
448
449 asset_status->add_spacer();
450
451 asset_conflicts_label = memnew(Label);
452 asset_conflicts_label->set_theme_type_variation("HeaderSmall");
453 asset_status->add_child(asset_conflicts_label);
454
455 skip_toplevel_check = memnew(CheckBox);
456 skip_toplevel_check->set_text(TTR("Ignore the root directory when extracting files"));
457 skip_toplevel_check->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
458 skip_toplevel_check->connect("toggled", callable_mp(this, &EditorAssetInstaller::_set_skip_toplevel));
459 vb->add_child(skip_toplevel_check);
460
461 tree = memnew(Tree);
462 tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
463 tree->connect("item_edited", callable_mp(this, &EditorAssetInstaller::_item_checked));
464 tree->connect("check_propagated_to_item", callable_mp(this, &EditorAssetInstaller::_check_propagated_to_item));
465 vb->add_child(tree);
466
467 set_title(TTR("Select Asset Files to Install"));
468 set_ok_button_text(TTR("Install"));
469 set_hide_on_ok(true);
470}
471