Skip to content

Commit 4a77584

Browse files
committed
Fix duplicate asset loader registration warning
# Objective The original fix (bevyengine#11870) did not actually implement the described logic. It checked if there were independently multiple loaders for a given asset type and multiple loaders for a given extension. However, this did not handle the case where those loaders were not the same. For example, there could be a loader for type `Foo` and extension `.foo`. Anther loader could exist for type `Bar` but extension `.bar`. If a third loader was added for type `Foo` but extension `.bar`, the warning would have been incorrectly logged. ## Solution Instead of independently checking to see if there are preexisting loaders for both the extension and type, look up the indices of the loaders for the type in question. Then check to see if the loaders registered for the extensions has any overlap. Only log if there are loaders that fit this criteria.
1 parent 765166b commit 4a77584

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

crates/bevy_asset/src/server/loaders.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl AssetLoaders {
4646
};
4747

4848
if is_new {
49+
let existing_loaders_for_type_id = self.type_id_to_loaders.get(&loader_asset_type);
4950
let mut duplicate_extensions = Vec::new();
5051
for extension in AssetLoader::extensions(&*loader) {
5152
let list = self
@@ -54,26 +55,26 @@ impl AssetLoaders {
5455
.or_default();
5556

5657
if !list.is_empty() {
57-
duplicate_extensions.push(extension);
58+
if let Some(existing_loaders_for_type_id) = existing_loaders_for_type_id {
59+
if list.iter().any(|index| existing_loaders_for_type_id.contains(index)) {
60+
duplicate_extensions.push(extension);
61+
}
62+
}
5863
}
5964

6065
list.push(loader_index);
6166
}
62-
63-
self.type_name_to_loader.insert(type_name, loader_index);
64-
65-
let list = self
66-
.type_id_to_loaders
67-
.entry(loader_asset_type)
68-
.or_default();
69-
70-
let duplicate_asset_registration = !list.is_empty();
71-
if !duplicate_extensions.is_empty() && duplicate_asset_registration {
67+
if !duplicate_extensions.is_empty() {
7268
warn!("Duplicate AssetLoader registered for Asset type `{loader_asset_type_name}` with extensions `{duplicate_extensions:?}`. \
7369
Loader must be specified in a .meta file in order to load assets of this type with these extensions.");
7470
}
7571

76-
list.push(loader_index);
72+
self.type_name_to_loader.insert(type_name, loader_index);
73+
74+
self.type_id_to_loaders
75+
.entry(loader_asset_type)
76+
.or_default()
77+
.push(loader_index);
7778

7879
self.loaders.push(MaybeAssetLoader::Ready(loader));
7980
} else {
@@ -107,6 +108,8 @@ impl AssetLoaders {
107108

108109
self.preregistered_loaders.insert(type_name, loader_index);
109110
self.type_name_to_loader.insert(type_name, loader_index);
111+
112+
let existing_loaders_for_type_id = self.type_id_to_loaders.get(&loader_asset_type);
110113
let mut duplicate_extensions = Vec::new();
111114
for extension in extensions {
112115
let list = self
@@ -115,24 +118,24 @@ impl AssetLoaders {
115118
.or_default();
116119

117120
if !list.is_empty() {
118-
duplicate_extensions.push(extension);
121+
if let Some(existing_loaders_for_type_id) = existing_loaders_for_type_id {
122+
if list.iter().any(|index| existing_loaders_for_type_id.contains(index)) {
123+
duplicate_extensions.push(extension);
124+
}
125+
}
119126
}
120127

121128
list.push(loader_index);
122129
}
123-
124-
let list = self
125-
.type_id_to_loaders
126-
.entry(loader_asset_type)
127-
.or_default();
128-
129-
let duplicate_asset_registration = !list.is_empty();
130-
if !duplicate_extensions.is_empty() && duplicate_asset_registration {
130+
if !duplicate_extensions.is_empty() {
131131
warn!("Duplicate AssetLoader preregistered for Asset type `{loader_asset_type_name}` with extensions `{duplicate_extensions:?}`. \
132132
Loader must be specified in a .meta file in order to load assets of this type with these extensions.");
133133
}
134134

135-
list.push(loader_index);
135+
self.type_id_to_loaders
136+
.entry(loader_asset_type)
137+
.or_default()
138+
.push(loader_index);
136139

137140
let (mut sender, receiver) = async_broadcast::broadcast(1);
138141
sender.set_overflow(true);

0 commit comments

Comments
 (0)