diff options
Diffstat (limited to 'editor/editor_file_system.cpp')
-rw-r--r-- | editor/editor_file_system.cpp | 571 |
1 files changed, 302 insertions, 269 deletions
diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 3d6b523733..099dfe69d5 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -38,9 +38,9 @@ #include "core/io/resource_saver.h" #include "core/os/os.h" #include "core/variant/variant_parser.h" -#include "editor_node.h" -#include "editor_resource_preview.h" -#include "editor_settings.h" +#include "editor/editor_node.h" +#include "editor/editor_resource_preview.h" +#include "editor/editor_settings.h" EditorFileSystem *EditorFileSystem::singleton = nullptr; //the name is the version, to keep compatibility with different versions of Godot @@ -194,7 +194,6 @@ void EditorFileSystemDirectory::_bind_methods() { EditorFileSystemDirectory::EditorFileSystemDirectory() { modified_time = 0; parent = nullptr; - verified = false; } EditorFileSystemDirectory::~EditorFileSystemDirectory() { @@ -219,89 +218,88 @@ void EditorFileSystem::_scan_filesystem() { String project = ProjectSettings::get_singleton()->get_resource_path(); String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(CACHE_FILE_NAME); - FileAccess *f = FileAccess::open(fscache, FileAccess::READ); - - bool first = true; - if (f) { - //read the disk cache - while (!f->eof_reached()) { - String l = f->get_line().strip_edges(); - if (first) { - if (first_scan) { - // only use this on first scan, afterwards it gets ignored - // this is so on first reimport we synchronize versions, then - // we don't care until editor restart. This is for usability mainly so - // your workflow is not killed after changing a setting by forceful reimporting - // everything there is. - filesystem_settings_version_for_import = l.strip_edges(); - if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) { - revalidate_import_files = true; + { + Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ); + + bool first = true; + if (f.is_valid()) { + //read the disk cache + while (!f->eof_reached()) { + String l = f->get_line().strip_edges(); + if (first) { + if (first_scan) { + // only use this on first scan, afterwards it gets ignored + // this is so on first reimport we synchronize versions, then + // we don't care until editor restart. This is for usability mainly so + // your workflow is not killed after changing a setting by forceful reimporting + // everything there is. + filesystem_settings_version_for_import = l.strip_edges(); + if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) { + revalidate_import_files = true; + } } + first = false; + continue; + } + if (l.is_empty()) { + continue; } - first = false; - continue; - } - if (l == String()) { - continue; - } - if (l.begins_with("::")) { - Vector<String> split = l.split("::"); - ERR_CONTINUE(split.size() != 3); - String name = split[1]; + if (l.begins_with("::")) { + Vector<String> split = l.split("::"); + ERR_CONTINUE(split.size() != 3); + String name = split[1]; - cpath = name; + cpath = name; - } else { - Vector<String> split = l.split("::"); - ERR_CONTINUE(split.size() != 9); - String name = split[0]; - String file; - - file = name; - name = cpath.plus_file(name); - - FileCache fc; - fc.type = split[1]; - fc.uid = split[2].to_int(); - fc.modification_time = split[3].to_int(); - fc.import_modification_time = split[4].to_int(); - fc.import_valid = split[5].to_int() != 0; - fc.import_group_file = split[6].strip_edges(); - fc.script_class_name = split[7].get_slice("<>", 0); - fc.script_class_extends = split[7].get_slice("<>", 1); - fc.script_class_icon_path = split[7].get_slice("<>", 2); - - String deps = split[8].strip_edges(); - if (deps.length()) { - Vector<String> dp = deps.split("<>"); - for (int i = 0; i < dp.size(); i++) { - String path = dp[i]; - fc.deps.push_back(path); + } else { + Vector<String> split = l.split("::"); + ERR_CONTINUE(split.size() != 9); + String name = split[0]; + String file; + + file = name; + name = cpath.plus_file(name); + + FileCache fc; + fc.type = split[1]; + fc.uid = split[2].to_int(); + fc.modification_time = split[3].to_int(); + fc.import_modification_time = split[4].to_int(); + fc.import_valid = split[5].to_int() != 0; + fc.import_group_file = split[6].strip_edges(); + fc.script_class_name = split[7].get_slice("<>", 0); + fc.script_class_extends = split[7].get_slice("<>", 1); + fc.script_class_icon_path = split[7].get_slice("<>", 2); + + String deps = split[8].strip_edges(); + if (deps.length()) { + Vector<String> dp = deps.split("<>"); + for (int i = 0; i < dp.size(); i++) { + String path = dp[i]; + fc.deps.push_back(path); + } } - } - file_cache[name] = fc; + file_cache[name] = fc; + } } } - - f->close(); - memdelete(f); } String update_cache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4"); if (FileAccess::exists(update_cache)) { { - FileAccessRef f2 = FileAccess::open(update_cache, FileAccess::READ); + Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ); String l = f2->get_line().strip_edges(); - while (l != String()) { + while (!l.is_empty()) { file_cache.erase(l); //erase cache for this, so it gets updated l = f2->get_line().strip_edges(); } } - DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); d->remove(update_cache); //bye bye update cache } @@ -315,14 +313,12 @@ void EditorFileSystem::_scan_filesystem() { new_filesystem = memnew(EditorFileSystemDirectory); new_filesystem->parent = nullptr; - DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES); d->change_dir("res://"); _scan_new_dir(new_filesystem, d, sp); file_cache.clear(); //clear caches, no longer needed - memdelete(d); - if (!first_scan) { //on the first scan this is done from the main thread after re-importing _save_filesystem_cache(); @@ -336,13 +332,11 @@ void EditorFileSystem::_save_filesystem_cache() { String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(CACHE_FILE_NAME); - FileAccess *f = FileAccess::open(fscache, FileAccess::WRITE); - ERR_FAIL_COND_MSG(!f, "Cannot create file '" + fscache + "'. Check user write permissions."); + Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE); + ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions."); f->store_line(filesystem_settings_version_for_import); _save_filesystem_cache(filesystem, f); - f->close(); - memdelete(f); } void EditorFileSystem::_thread_func(void *_userdata) { @@ -365,9 +359,9 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo } Error err; - FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err); + Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err); - if (!f) { //no import file, do reimport + if (f.is_null()) { //no import file, do reimport return true; } @@ -401,11 +395,10 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo break; } else if (err != OK) { ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'."); - memdelete(f); return false; //parse error, try reimport manually (Avoid reimport loop on broken file) } - if (assign != String()) { + if (!assign.is_empty()) { if (assign.begins_with("path")) { to_check.push_back(value); } else if (assign == "files") { @@ -432,8 +425,6 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo } } - memdelete(f); - if (importer_name == "keep") { return false; //keep mode, do not reimport } @@ -454,8 +445,8 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo // Read the md5's from a separate file (so the import parameters aren't dependent on the file version String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path); - FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err); - if (!md5s) { // No md5's stored for this resource + Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err); + if (md5s.is_null()) { // No md5's stored for this resource return true; } @@ -473,10 +464,9 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo break; } else if (err != OK) { ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'."); - memdelete(md5s); return false; // parse error } - if (assign != String()) { + if (!assign.is_empty()) { if (!p_only_imported_files) { if (assign == "source_md5") { source_md5 = value; @@ -486,7 +476,6 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo } } } - memdelete(md5s); //imported files are gone, reimport for (const String &E : to_check) { @@ -497,11 +486,11 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo //check source md5 matching if (!p_only_imported_files) { - if (source_file != String() && source_file != p_path) { + if (!source_file.is_empty() && source_file != p_path) { return true; //file was moved, reimport } - if (source_md5 == String()) { + if (source_md5.is_empty()) { return true; //lacks md5, so just reimport } @@ -510,7 +499,7 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo return true; } - if (dest_files.size() && dest_md5 != String()) { + if (dest_files.size() && !dest_md5.is_empty()) { md5 = FileAccess::get_multiple_md5(dest_files); if (md5 != dest_md5) { return true; @@ -521,6 +510,45 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo return false; //nothing changed } +bool EditorFileSystem::_scan_import_support(Vector<String> reimports) { + if (import_support_queries.size() == 0) { + return false; + } + Map<String, int> import_support_test; + Vector<bool> import_support_tested; + import_support_tested.resize(import_support_queries.size()); + for (int i = 0; i < import_support_queries.size(); i++) { + import_support_tested.write[i] = false; + if (import_support_queries[i]->is_active()) { + Vector<String> extensions = import_support_queries[i]->get_file_extensions(); + for (int j = 0; j < extensions.size(); j++) { + import_support_test.insert(extensions[j], i); + } + } + } + + if (import_support_test.size() == 0) { + return false; //well nothing to do + } + + for (int i = 0; i < reimports.size(); i++) { + Map<String, int>::Element *E = import_support_test.find(reimports[i].get_extension()); + if (E) { + import_support_tested.write[E->get()] = true; + } + } + + for (int i = 0; i < import_support_tested.size(); i++) { + if (import_support_tested[i]) { + if (import_support_queries.write[i]->query()) { + return true; + } + } + } + + return false; +} + bool EditorFileSystem::_update_scan_actions() { sources_changed.clear(); @@ -577,7 +605,7 @@ bool EditorFileSystem::_update_scan_actions() { ERR_CONTINUE(idx == -1); _delete_internal_files(ia.dir->files[idx]->file); memdelete(ia.dir->files[idx]); - ia.dir->files.remove(idx); + ia.dir->files.remove_at(idx); fs_changed = true; @@ -613,7 +641,7 @@ bool EditorFileSystem::_update_scan_actions() { if (_scan_extensions()) { //needs editor restart //extensions also may provide filetypes to be imported, so they must run before importing - if (EditorNode::immediate_confirmation_dialog(TTR("Some extensions need the editor to restart to take effect."), first_scan ? TTR("Restart") : TTR("Save&Restart"), TTR("Continue"))) { + if (EditorNode::immediate_confirmation_dialog(TTR("Some extensions need the editor to restart to take effect."), first_scan ? TTR("Restart") : TTR("Save & Restart"), TTR("Continue"))) { if (!first_scan) { EditorNode::get_singleton()->save_all_scenes(); } @@ -622,7 +650,12 @@ bool EditorFileSystem::_update_scan_actions() { return true; } } + if (reimports.size()) { + if (_scan_import_support(reimports)) { + return true; + } + reimport_files(reimports); } else { //reimport files will update the uid cache file so if nothing was reimported, update it manually @@ -699,7 +732,7 @@ EditorFileSystem::ScanProgress EditorFileSystem::ScanProgress::get_sub(int p_cur return sp; } -void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress) { +void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAccess> &da, const ScanProgress &p_progress) { List<String> dirs; List<String> files; @@ -710,7 +743,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess da->list_dir_begin(); while (true) { String f = da->get_next(); - if (f == "") { + if (f.is_empty()) { break; } @@ -820,7 +853,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess scan_actions.push_back(ia); } - if (fc->type == String()) { + if (fc->type.is_empty()) { fi->type = ResourceLoader::get_resource_type(path); fi->import_group_file = ResourceLoader::get_import_group_file(path); //there is also the chance that file type changed due to reimport, must probably check this somehow here (or kind of note it for next time in another file?) @@ -889,7 +922,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess if (script == nullptr) { continue; } - const Vector<DocData::ClassDoc> &docs = script->get_documentation(); + Vector<DocData::ClassDoc> docs = script->get_documentation(); for (int j = 0; j < docs.size(); j++) { EditorHelp::get_doc_data()->add_doc(docs[j]); } @@ -924,7 +957,7 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const //then scan files and directories and check what's different - DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); Error ret = da->change_dir(cd); ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder."); @@ -932,7 +965,7 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const da->list_dir_begin(); while (true) { String f = da->get_next(); - if (f == "") { + if (f.is_empty()) { break; } @@ -955,10 +988,9 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const efd->parent = p_dir; efd->name = f; - DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES); d->change_dir(cd.plus_file(f)); _scan_new_dir(efd, d, p_progress.get_sub(1, 1)); - memdelete(d); ItemAction ia; ia.action = ItemAction::ACTION_DIR_ADD; @@ -1094,12 +1126,11 @@ void EditorFileSystem::_delete_internal_files(String p_file) { if (FileAccess::exists(p_file + ".import")) { List<String> paths; ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths); - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); for (const String &E : paths) { da->remove(E); } da->remove(p_file + ".import"); - memdelete(da); } } @@ -1163,6 +1194,7 @@ void EditorFileSystem::_notification(int p_what) { call_deferred(SNAME("scan")); //this should happen after every editor node entered the tree } break; + case NOTIFICATION_EXIT_TREE: { Thread &active_thread = thread.is_started() ? thread : thread_sources; if (use_threads && active_thread.is_started()) { @@ -1184,8 +1216,8 @@ void EditorFileSystem::_notification(int p_what) { } filesystem = nullptr; new_filesystem = nullptr; - } break; + case NOTIFICATION_PROCESS: { if (use_threads) { if (scanning_changes) { @@ -1239,14 +1271,14 @@ EditorFileSystemDirectory *EditorFileSystem::get_filesystem() { return filesystem; } -void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file) { +void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) { if (!p_dir) { return; //none } p_file->store_line("::" + p_dir->get_path() + "::" + String::num(p_dir->modified_time)); for (int i = 0; i < p_dir->files.size(); i++) { - if (p_dir->files[i]->import_group_file != String()) { + if (!p_dir->files[i]->import_group_file.is_empty()) { group_file_cache.insert(p_dir->files[i]->import_group_file); } String s = p_dir->files[i]->file + "::" + p_dir->files[i]->type + "::" + itos(p_dir->files[i]->uid) + "::" + itos(p_dir->files[i]->modified_time) + "::" + itos(p_dir->files[i]->import_modified_time) + "::" + itos(p_dir->files[i]->import_valid) + "::" + p_dir->files[i]->import_group_file + "::" + p_dir->files[i]->script_class_name + "<>" + p_dir->files[i]->script_class_extends + "<>" + p_dir->files[i]->script_class_icon_path; @@ -1386,7 +1418,7 @@ EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p f = f.substr(6, f.length()); f = f.replace("\\", "/"); - if (f == String()) { + if (f.is_empty()) { return filesystem; } @@ -1424,8 +1456,8 @@ EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p void EditorFileSystem::_save_late_updated_files() { //files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file String fscache = EditorSettings::get_singleton()->get_project_settings_dir().plus_file("filesystem_update4"); - FileAccessRef f = FileAccess::open(fscache, FileAccess::WRITE); - ERR_FAIL_COND_MSG(!f, "Cannot create file '" + fscache + "'. Check user write permissions."); + Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE); + ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions."); for (Set<String>::Element *E = late_update_files.front(); E; E = E->next()) { f->store_line(E->get()); } @@ -1465,7 +1497,7 @@ void EditorFileSystem::_scan_script_classes(EditorFileSystemDirectory *p_dir) { int filecount = p_dir->files.size(); const EditorFileSystemDirectory::FileInfo *const *files = p_dir->files.ptr(); for (int i = 0; i < filecount; i++) { - if (files[i]->script_class_name == String()) { + if (files[i]->script_class_name.is_empty()) { continue; } @@ -1536,7 +1568,7 @@ void EditorFileSystem::update_file(const String &p_file) { } } memdelete(fs->files[cpos]); - fs->files.remove(cpos); + fs->files.remove_at(cpos); } call_deferred(SNAME("emit_signal"), "filesystem_changed"); //update later @@ -1545,7 +1577,7 @@ void EditorFileSystem::update_file(const String &p_file) { } String type = ResourceLoader::get_resource_type(p_file); - if (type == "" && textfile_extensions.has(p_file.get_extension())) { + if (type.is_empty() && textfile_extensions.has(p_file.get_extension())) { type = "TextFile"; } ResourceUID::ID uid = ResourceLoader::get_resource_uid(p_file); @@ -1604,6 +1636,10 @@ void EditorFileSystem::update_file(const String &p_file) { _queue_update_script_classes(); } +Set<String> EditorFileSystem::get_valid_extensions() const { + return valid_extensions; +} + Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) { String importer_name; @@ -1616,9 +1652,9 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector ERR_CONTINUE(err != OK); ERR_CONTINUE(!config->has_section_key("remap", "importer")); String file_importer_name = config->get_value("remap", "importer"); - ERR_CONTINUE(file_importer_name == String()); + ERR_CONTINUE(file_importer_name.is_empty()); - if (importer_name != String() && importer_name != file_importer_name) { + if (!importer_name.is_empty() && importer_name != file_importer_name) { EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file)); ERR_FAIL_V(ERR_FILE_CORRUPT); } @@ -1633,7 +1669,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT); List<ResourceImporter::ImportOption> options; - importer->get_import_options(&options); + importer->get_import_options(p_files[i], &options); //set default values for (const ResourceImporter::ImportOption &E : options) { source_file_options[p_files[i]][E.option.name] = E.default_value; @@ -1644,7 +1680,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector config->get_section_keys("params", &sk); for (const String ¶m : sk) { Variant value = config->get_value("params", param); - //override with whathever is in file + //override with whatever is in file source_file_options[p_files[i]][param] = value; } } @@ -1656,7 +1692,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector return OK; // (do nothing) } - ERR_FAIL_COND_V(importer_name == String(), ERR_UNCONFIGURED); + ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED); Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); @@ -1666,78 +1702,78 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector for (const KeyValue<String, Map<StringName, Variant>> &E : source_file_options) { const String &file = E.key; String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file); - FileAccessRef f = FileAccess::open(file + ".import", FileAccess::WRITE); - ERR_FAIL_COND_V_MSG(!f, ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'."); - - //write manually, as order matters ([remap] has to go first for performance). - f->store_line("[remap]"); - f->store_line(""); - f->store_line("importer=\"" + importer->get_importer_name() + "\""); - int version = importer->get_format_version(); - if (version > 0) { - f->store_line("importer_version=" + itos(version)); - } - if (importer->get_resource_type() != "") { - f->store_line("type=\"" + importer->get_resource_type() + "\""); - } - Vector<String> dest_paths; + { + Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE); + ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'."); + + //write manually, as order matters ([remap] has to go first for performance). + f->store_line("[remap]"); + f->store_line(""); + f->store_line("importer=\"" + importer->get_importer_name() + "\""); + int version = importer->get_format_version(); + if (version > 0) { + f->store_line("importer_version=" + itos(version)); + } + if (!importer->get_resource_type().is_empty()) { + f->store_line("type=\"" + importer->get_resource_type() + "\""); + } - if (err == OK) { - String path = base_path + "." + importer->get_save_extension(); - f->store_line("path=\"" + path + "\""); - dest_paths.push_back(path); - } - - f->store_line("group_file=" + Variant(p_group_file).get_construct_string()); - - if (err == OK) { - f->store_line("valid=true"); - } else { - f->store_line("valid=false"); - } - f->store_line("[deps]\n"); + if (err == OK) { + String path = base_path + "." + importer->get_save_extension(); + f->store_line("path=\"" + path + "\""); + dest_paths.push_back(path); + } - f->store_line(""); + f->store_line("group_file=" + Variant(p_group_file).get_construct_string()); - f->store_line("source_file=" + Variant(file).get_construct_string()); - if (dest_paths.size()) { - Array dp; - for (int i = 0; i < dest_paths.size(); i++) { - dp.push_back(dest_paths[i]); + if (err == OK) { + f->store_line("valid=true"); + } else { + f->store_line("valid=false"); } - f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n"); - } - f->store_line("[params]"); - f->store_line(""); + f->store_line("[deps]\n"); - //store options in provided order, to avoid file changing. Order is also important because first match is accepted first. + f->store_line(""); - List<ResourceImporter::ImportOption> options; - importer->get_import_options(&options); - //set default values - for (const ResourceImporter::ImportOption &F : options) { - String base = F.option.name; - Variant v = F.default_value; - if (source_file_options[file].has(base)) { - v = source_file_options[file][base]; + f->store_line("source_file=" + Variant(file).get_construct_string()); + if (dest_paths.size()) { + Array dp; + for (int i = 0; i < dest_paths.size(); i++) { + dp.push_back(dest_paths[i]); + } + f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n"); + } + f->store_line("[params]"); + f->store_line(""); + + //store options in provided order, to avoid file changing. Order is also important because first match is accepted first. + + List<ResourceImporter::ImportOption> options; + importer->get_import_options(file, &options); + //set default values + for (const ResourceImporter::ImportOption &F : options) { + String base = F.option.name; + Variant v = F.default_value; + if (source_file_options[file].has(base)) { + v = source_file_options[file][base]; + } + String value; + VariantWriter::write_to_string(v, value); + f->store_line(base + "=" + value); } - String value; - VariantWriter::write_to_string(v, value); - f->store_line(base + "=" + value); } - f->close(); - // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled. - FileAccessRef md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE); - ERR_FAIL_COND_V_MSG(!md5s, ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'."); + { + Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE); + ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'."); - md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\""); - if (dest_paths.size()) { - md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n"); + md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\""); + if (dest_paths.size()) { + md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n"); + } } - md5s->close(); EditorFileSystemDirectory *fs = nullptr; int cpos = -1; @@ -1759,7 +1795,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector if (ResourceCache::has(file)) { Resource *r = ResourceCache::get(file); - if (r->get_import_path() != String()) { + if (!r->get_import_path().is_empty()) { String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file); r->set_import_path(dst_path); r->set_import_last_modified_time(0); @@ -1783,7 +1819,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName Map<StringName, Variant> params; String importer_name; //empty by default though - if (p_custom_importer != String()) { + if (!p_custom_importer.is_empty()) { importer_name = p_custom_importer; } if (p_custom_options != nullptr) { @@ -1808,7 +1844,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName } if (cf->has_section("remap")) { - if (p_custom_importer == String()) { + if (p_custom_importer.is_empty()) { importer_name = cf->get_value("remap", "importer"); } @@ -1834,7 +1870,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName Ref<ResourceImporter> importer; bool load_default = false; //find the importer - if (importer_name != "") { + if (!importer_name.is_empty()) { importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); } @@ -1851,7 +1887,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName //mix with default params, in case a parameter is missing List<ResourceImporter::ImportOption> opts; - importer->get_import_options(&opts); + importer->get_import_options(p_file, &opts); for (const ResourceImporter::ImportOption &E : opts) { if (!params.has(E.option.name)) { //this one is not present params[E.option.name] = E.default_value; @@ -1883,106 +1919,104 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName //as import is complete, save the .import file - FileAccess *f = FileAccess::open(p_file + ".import", FileAccess::WRITE); - ERR_FAIL_COND_MSG(!f, "Cannot open file from path '" + p_file + ".import'."); - - //write manually, as order matters ([remap] has to go first for performance). - f->store_line("[remap]"); - f->store_line(""); - f->store_line("importer=\"" + importer->get_importer_name() + "\""); - int version = importer->get_format_version(); - if (version > 0) { - f->store_line("importer_version=" + itos(version)); - } - if (importer->get_resource_type() != "") { - f->store_line("type=\"" + importer->get_resource_type() + "\""); - } - - if (uid == ResourceUID::INVALID_ID) { - uid = ResourceUID::get_singleton()->create_id(); - } + Vector<String> dest_paths; + { + Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE); + ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file from path '" + p_file + ".import'."); - f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); //store in readable format + //write manually, as order matters ([remap] has to go first for performance). + f->store_line("[remap]"); + f->store_line(""); + f->store_line("importer=\"" + importer->get_importer_name() + "\""); + int version = importer->get_format_version(); + if (version > 0) { + f->store_line("importer_version=" + itos(version)); + } + if (!importer->get_resource_type().is_empty()) { + f->store_line("type=\"" + importer->get_resource_type() + "\""); + } - Vector<String> dest_paths; + if (uid == ResourceUID::INVALID_ID) { + uid = ResourceUID::get_singleton()->create_id(); + } - if (err == OK) { - if (importer->get_save_extension() == "") { - //no path - } else if (import_variants.size()) { - //import with variants - for (const String &E : import_variants) { - String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension(); + f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); //store in readable format - f->store_line("path." + E + "=\"" + path + "\""); + if (err == OK) { + if (importer->get_save_extension().is_empty()) { + //no path + } else if (import_variants.size()) { + //import with variants + for (const String &E : import_variants) { + String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension(); + + f->store_line("path." + E + "=\"" + path + "\""); + dest_paths.push_back(path); + } + } else { + String path = base_path + "." + importer->get_save_extension(); + f->store_line("path=\"" + path + "\""); dest_paths.push_back(path); } + } else { - String path = base_path + "." + importer->get_save_extension(); - f->store_line("path=\"" + path + "\""); - dest_paths.push_back(path); + f->store_line("valid=false"); } - } else { - f->store_line("valid=false"); - } + if (metadata != Variant()) { + f->store_line("metadata=" + metadata.get_construct_string()); + } - if (metadata != Variant()) { - f->store_line("metadata=" + metadata.get_construct_string()); - } + f->store_line(""); - f->store_line(""); + f->store_line("[deps]\n"); - f->store_line("[deps]\n"); + if (gen_files.size()) { + Array genf; + for (const String &E : gen_files) { + genf.push_back(E); + dest_paths.push_back(E); + } - if (gen_files.size()) { - Array genf; - for (const String &E : gen_files) { - genf.push_back(E); - dest_paths.push_back(E); + String value; + VariantWriter::write_to_string(genf, value); + f->store_line("files=" + value); + f->store_line(""); } - String value; - VariantWriter::write_to_string(genf, value); - f->store_line("files=" + value); - f->store_line(""); - } - - f->store_line("source_file=" + Variant(p_file).get_construct_string()); + f->store_line("source_file=" + Variant(p_file).get_construct_string()); - if (dest_paths.size()) { - Array dp; - for (int i = 0; i < dest_paths.size(); i++) { - dp.push_back(dest_paths[i]); + if (dest_paths.size()) { + Array dp; + for (int i = 0; i < dest_paths.size(); i++) { + dp.push_back(dest_paths[i]); + } + f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n"); } - f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n"); - } - f->store_line("[params]"); - f->store_line(""); + f->store_line("[params]"); + f->store_line(""); - //store options in provided order, to avoid file changing. Order is also important because first match is accepted first. + //store options in provided order, to avoid file changing. Order is also important because first match is accepted first. - for (const ResourceImporter::ImportOption &E : opts) { - String base = E.option.name; - String value; - VariantWriter::write_to_string(params[base], value); - f->store_line(base + "=" + value); + for (const ResourceImporter::ImportOption &E : opts) { + String base = E.option.name; + String value; + VariantWriter::write_to_string(params[base], value); + f->store_line(base + "=" + value); + } } - f->close(); - memdelete(f); - // Store the md5's of the various files. These are stored separately so that the .import files can be version controlled. - FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE); - ERR_FAIL_COND_MSG(!md5s, "Cannot open MD5 file '" + base_path + ".md5'."); + { + Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE); + ERR_FAIL_COND_MSG(md5s.is_null(), "Cannot open MD5 file '" + base_path + ".md5'."); - md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\""); - if (dest_paths.size()) { - md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n"); + md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\""); + if (dest_paths.size()) { + md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n"); + } } - md5s->close(); - memdelete(md5s); //update modified times, to avoid reimport fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file); @@ -2003,7 +2037,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName if (ResourceCache::has(p_file)) { Resource *r = ResourceCache::get(p_file); - if (r->get_import_path() != String()) { + if (!r->get_import_path().is_empty()) { String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file); r->set_import_path(dst_path); r->set_import_last_modified_time(0); @@ -2062,7 +2096,7 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) { groups_to_reimport.insert(file); //groups do not belong to grups group_file = String(); - } else if (group_file != String()) { + } else if (!group_file.is_empty()) { //it's a group file, add group to import and skip this file groups_to_reimport.insert(group_file); } else { @@ -2271,6 +2305,7 @@ static void _scan_extensions_dir(EditorFileSystemDirectory *d, Set<String> &exte bool EditorFileSystem::_scan_extensions() { EditorFileSystemDirectory *d = get_filesystem(); Set<String> extensions; + _scan_extensions_dir(d, extensions); //verify against loaded extensions @@ -2294,14 +2329,14 @@ bool EditorFileSystem::_scan_extensions() { String extension_list_config_file = NativeExtension::get_extension_list_config_file(); if (extensions.size()) { if (extensions_added.size() || extensions_removed.size()) { //extensions were added or removed - FileAccessRef f = FileAccess::open(extension_list_config_file, FileAccess::WRITE); + Ref<FileAccess> f = FileAccess::open(extension_list_config_file, FileAccess::WRITE); for (const String &E : extensions) { f->store_line(E); } } } else { if (loaded_extensions.size() || FileAccess::exists(extension_list_config_file)) { //extensions were removed - DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); da->remove(extension_list_config_file); } } @@ -2371,6 +2406,14 @@ void EditorFileSystem::_update_extensions() { } } +void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) { + ERR_FAIL_COND(import_support_queries.find(p_query) != -1); + import_support_queries.push_back(p_query); +} +void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) { + import_support_queries.erase(p_query); +} + EditorFileSystem::EditorFileSystem() { ResourceLoader::import = _resource_import; reimport_on_missing_imported_files = GLOBAL_DEF("editor/import/reimport_missing_imported_files", true); @@ -2379,24 +2422,14 @@ EditorFileSystem::EditorFileSystem() { filesystem = memnew(EditorFileSystemDirectory); //like, empty filesystem->parent = nullptr; - scanning = false; - importing = false; - use_threads = true; new_filesystem = nullptr; - abort_scan = false; - scanning_changes = false; - scanning_changes_done = false; - // This should probably also work on Unix and use the string it returns for FAT32 or exFAT - DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "exFAT"); scan_total = 0; update_script_classes_queued.clear(); - first_scan = true; - scan_changes_pending = false; - revalidate_import_files = false; import_threads.init(); ResourceUID::get_singleton()->clear(); //will be updated on scan ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path); |