Skip to content

Commit c67b9a4

Browse files
committed
Merge pull request #100086 from erodozer/multipart-ext-import
Support multi dot extensions in import plugins
2 parents 665bdf4 + e48fea7 commit c67b9a4

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

core/io/resource_importer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Error ResourceFormatImporter::get_import_order_threads_and_importer(const String
262262
importer = get_importer_by_name(pat.importer);
263263
}
264264
} else {
265-
importer = get_importer_by_extension(p_path.get_extension().to_lower());
265+
importer = get_importer_by_file(p_path);
266266
}
267267

268268
if (importer.is_valid()) {
@@ -286,7 +286,7 @@ int ResourceFormatImporter::get_import_order(const String &p_path) const {
286286
importer = get_importer_by_name(pat.importer);
287287
}
288288
} else {
289-
importer = get_importer_by_extension(p_path.get_extension().to_lower());
289+
importer = get_importer_by_file(p_path);
290290
}
291291

292292
if (importer.is_valid()) {
@@ -471,12 +471,12 @@ void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importe
471471
}
472472
}
473473

474-
void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
474+
void ResourceFormatImporter::get_importers_for_file(const String &p_file, List<Ref<ResourceImporter>> *r_importers) {
475475
for (int i = 0; i < importers.size(); i++) {
476476
List<String> local_exts;
477477
importers[i]->get_recognized_extensions(&local_exts);
478478
for (const String &F : local_exts) {
479-
if (p_extension.to_lower() == F) {
479+
if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
480480
r_importers->push_back(importers[i]);
481481
break;
482482
}
@@ -490,17 +490,18 @@ void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_import
490490
}
491491
}
492492

493-
Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
493+
Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_file(const String &p_file) const {
494494
Ref<ResourceImporter> importer;
495495
float priority = 0;
496496

497497
for (int i = 0; i < importers.size(); i++) {
498498
List<String> local_exts;
499499
importers[i]->get_recognized_extensions(&local_exts);
500500
for (const String &F : local_exts) {
501-
if (p_extension.to_lower() == F && importers[i]->get_priority() > priority) {
501+
if (p_file.right(F.length()).nocasecmp_to(F) == 0 && importers[i]->get_priority() > priority) {
502502
importer = importers[i];
503503
priority = importers[i]->get_priority();
504+
break;
504505
}
505506
}
506507
}

core/io/resource_importer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class ResourceFormatImporter : public ResourceFormatLoader {
8989

9090
void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); }
9191
Ref<ResourceImporter> get_importer_by_name(const String &p_name) const;
92-
Ref<ResourceImporter> get_importer_by_extension(const String &p_extension) const;
93-
void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers);
92+
Ref<ResourceImporter> get_importer_by_file(const String &p_file) const;
93+
void get_importers_for_file(const String &p_file, List<Ref<ResourceImporter>> *r_importers);
9494
void get_importers(List<Ref<ResourceImporter>> *r_importers);
9595

9696
bool are_import_settings_valid(const String &p_path) const;

core/io/resource_loader.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_
6161
return ret;
6262
}
6363

64-
String extension = p_path.get_extension();
65-
6664
List<String> extensions;
6765
if (p_for_type.is_empty()) {
6866
get_recognized_extensions(&extensions);
@@ -71,7 +69,8 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_
7169
}
7270

7371
for (const String &E : extensions) {
74-
if (E.nocasecmp_to(extension) == 0) {
72+
const String ext = !E.begins_with(".") ? "." + E : E;
73+
if (p_path.right(ext.length()).nocasecmp_to(ext) == 0) {
7574
return true;
7675
}
7776
}
@@ -330,7 +329,7 @@ Ref<Resource> ResourceLoader::_load(const String &p_path, const String &p_origin
330329

331330
#ifdef TOOLS_ENABLED
332331
if (Engine::get_singleton()->is_editor_hint()) {
333-
if (ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_path.get_extension()).is_valid()) {
332+
if (ResourceFormatImporter::get_singleton()->get_importer_by_file(p_path).is_valid()) {
334333
// The format is known to the editor, but the file hasn't been imported
335334
// (otherwise, ResourceFormatImporter would have been found as a suitable loader).
336335
found = true;

editor/editor_file_system.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ bool EditorFileSystem::_update_scan_actions() {
970970
Vector<String> dependencies = _get_dependencies(full_path);
971971
for (const String &dep : dependencies) {
972972
const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
973-
if (import_extensions.has(dep.get_extension())) {
973+
if (_can_import_file(dep)) {
974974
reimports.push_back(dependency_path);
975975
}
976976
}
@@ -1224,7 +1224,7 @@ void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir,
12241224
FileCache *fc = file_cache.getptr(path);
12251225
uint64_t mt = FileAccess::get_modified_time(path);
12261226

1227-
if (import_extensions.has(ext)) {
1227+
if (_can_import_file(scan_file)) {
12281228
//is imported
12291229
uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
12301230

@@ -1514,7 +1514,7 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanPr
15141514
scan_actions.push_back(ia);
15151515
}
15161516

1517-
if (import_extensions.has(ext)) {
1517+
if (_can_import_file(f)) {
15181518
//if it can be imported, and it was added, it needs to be reimported
15191519
ItemAction ia;
15201520
ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
@@ -1546,7 +1546,7 @@ void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanPr
15461546

15471547
String path = cd.path_join(p_dir->files[i]->file);
15481548

1549-
if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) {
1549+
if (_can_import_file(p_dir->files[i]->file)) {
15501550
// Check here if file must be imported or not.
15511551
// Same logic as in _process_file_system, the last modifications dates
15521552
// needs to be trusted to prevent reading all the .import files and the md5
@@ -2820,7 +2820,7 @@ Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<Strin
28202820

28212821
if (importer.is_null()) {
28222822
//not found by name, find by extension
2823-
importer = ResourceFormatImporter::get_singleton()->get_importer_by_extension(p_file.get_extension());
2823+
importer = ResourceFormatImporter::get_singleton()->get_importer_by_file(p_file);
28242824
load_default = true;
28252825
if (importer.is_null()) {
28262826
ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, "BUG: File queued for import, but can't be imported, importer for type '" + importer_name + "' not found.");
@@ -3626,8 +3626,18 @@ void EditorFileSystem::_update_extensions() {
36263626
extensionsl.clear();
36273627
ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
36283628
for (const String &E : extensionsl) {
3629-
import_extensions.insert(E);
3629+
import_extensions.insert(!E.begins_with(".") ? "." + E : E);
3630+
}
3631+
}
3632+
3633+
bool EditorFileSystem::_can_import_file(const String &p_file) {
3634+
for (const String &F : import_extensions) {
3635+
if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
3636+
return true;
3637+
}
36303638
}
3639+
3640+
return false;
36313641
}
36323642

36333643
void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {

editor/editor_file_system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ class EditorFileSystem : public Node {
279279

280280
bool _test_for_reimport(const String &p_path, const String &p_expected_import_md5);
281281
bool _is_test_for_reimport_needed(const String &p_path, uint64_t p_last_modification_time, uint64_t p_modification_time, uint64_t p_last_import_modification_time, uint64_t p_import_modification_time, const Vector<String> &p_import_dest_paths);
282+
bool _can_import_file(const String &p_path);
282283
Vector<String> _get_import_dest_paths(const String &p_path);
283284

284285
bool reimport_on_missing_imported_files;

editor/import_dock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void ImportDock::set_edit_path(const String &p_path) {
124124
_update_options(p_path, config);
125125

126126
List<Ref<ResourceImporter>> importers;
127-
ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_path.get_extension(), &importers);
127+
ResourceFormatImporter::get_singleton()->get_importers_for_file(p_path, &importers);
128128
List<Pair<String, String>> importer_names;
129129

130130
for (const Ref<ResourceImporter> &E : importers) {
@@ -314,7 +314,7 @@ void ImportDock::set_edit_multiple_paths(const Vector<String> &p_paths) {
314314
params->update();
315315

316316
List<Ref<ResourceImporter>> importers;
317-
ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_paths[0].get_extension(), &importers);
317+
ResourceFormatImporter::get_singleton()->get_importers_for_file(p_paths[0], &importers);
318318
List<Pair<String, String>> importer_names;
319319

320320
for (const Ref<ResourceImporter> &E : importers) {

0 commit comments

Comments
 (0)