Skip to content

Commit c70678a

Browse files
committed
Revert "Make adding a subasset label return a result for if there is a duplicate label. (bevyengine#18013)"
This reverts commit ed1143b.
1 parent 0d90da8 commit c70678a

File tree

3 files changed

+234
-315
lines changed

3 files changed

+234
-315
lines changed

crates/bevy_asset/src/lib.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ mod tests {
670670
},
671671
loader::{AssetLoader, LoadContext},
672672
Asset, AssetApp, AssetEvent, AssetId, AssetLoadError, AssetLoadFailedEvent, AssetPath,
673-
AssetPlugin, AssetServer, Assets, DuplicateLabelAssetError, LoadState, UnapprovedPathMode,
673+
AssetPlugin, AssetServer, Assets, LoadState, UnapprovedPathMode,
674674
};
675675
use alloc::{
676676
boxed::Box,
@@ -726,8 +726,6 @@ mod tests {
726726
CannotLoadDependency { dependency: AssetPath<'static> },
727727
#[error("A RON error occurred during loading")]
728728
RonSpannedError(#[from] ron::error::SpannedError),
729-
#[error(transparent)]
730-
DuplicateLabelAssetError(#[from] DuplicateLabelAssetError),
731729
#[error("An IO error occurred during loading")]
732730
Io(#[from] std::io::Error),
733731
}
@@ -773,7 +771,7 @@ mod tests {
773771
.sub_texts
774772
.drain(..)
775773
.map(|text| load_context.add_labeled_asset(text.clone(), SubText { text }))
776-
.collect::<Result<Vec<_>, _>>()?,
774+
.collect(),
777775
})
778776
}
779777

@@ -1811,49 +1809,6 @@ mod tests {
18111809
app.world_mut().run_schedule(Update);
18121810
}
18131811

1814-
#[test]
1815-
fn fails_to_load_for_duplicate_subasset_labels() {
1816-
let mut app = App::new();
1817-
1818-
let dir = Dir::default();
1819-
dir.insert_asset_text(
1820-
Path::new("a.ron"),
1821-
r#"(
1822-
text: "b",
1823-
dependencies: [],
1824-
embedded_dependencies: [],
1825-
sub_texts: ["A", "A"],
1826-
)"#,
1827-
);
1828-
1829-
app.register_asset_source(
1830-
AssetSourceId::Default,
1831-
AssetSource::build()
1832-
.with_reader(move || Box::new(MemoryAssetReader { root: dir.clone() })),
1833-
)
1834-
.add_plugins((
1835-
TaskPoolPlugin::default(),
1836-
LogPlugin::default(),
1837-
AssetPlugin::default(),
1838-
));
1839-
1840-
app.init_asset::<CoolText>()
1841-
.init_asset::<SubText>()
1842-
.register_asset_loader(CoolTextLoader);
1843-
1844-
let asset_server = app.world().resource::<AssetServer>().clone();
1845-
let handle = asset_server.load::<CoolText>("a.ron");
1846-
1847-
run_app_until(&mut app, |_world| match asset_server.load_state(&handle) {
1848-
LoadState::Loading => None,
1849-
LoadState::Failed(err) => {
1850-
assert!(matches!(*err, AssetLoadError::AssetLoaderError(_)));
1851-
Some(())
1852-
}
1853-
state => panic!("Unexpected asset state: {state:?}"),
1854-
});
1855-
}
1856-
18571812
// This test is not checking a requirement, but documenting a current limitation. We simply are
18581813
// not capable of loading subassets when doing nested immediate loads.
18591814
#[test]

crates/bevy_asset/src/loader.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use alloc::{
1313
};
1414
use atomicow::CowArc;
1515
use bevy_ecs::world::World;
16+
use bevy_log::warn;
1617
use bevy_platform_support::collections::{HashMap, HashSet};
1718
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
1819
use core::any::{Any, TypeId};
@@ -461,7 +462,7 @@ impl<'a> LoadContext<'a> {
461462
&mut self,
462463
label: String,
463464
load: impl FnOnce(&mut LoadContext) -> A,
464-
) -> Result<Handle<A>, DuplicateLabelAssetError> {
465+
) -> Handle<A> {
465466
let mut context = self.begin_labeled_asset();
466467
let asset = load(&mut context);
467468
let complete_asset = context.finish(asset);
@@ -478,11 +479,7 @@ impl<'a> LoadContext<'a> {
478479
/// new [`LoadContext`] to track the dependencies for the labeled asset.
479480
///
480481
/// See [`AssetPath`] for more on labeled assets.
481-
pub fn add_labeled_asset<A: Asset>(
482-
&mut self,
483-
label: String,
484-
asset: A,
485-
) -> Result<Handle<A>, DuplicateLabelAssetError> {
482+
pub fn add_labeled_asset<A: Asset>(&mut self, label: String, asset: A) -> Handle<A> {
486483
self.labeled_asset_scope(label, |_| asset)
487484
}
488485

@@ -495,7 +492,7 @@ impl<'a> LoadContext<'a> {
495492
&mut self,
496493
label: impl Into<CowArc<'static, str>>,
497494
loaded_asset: CompleteLoadedAsset<A>,
498-
) -> Result<Handle<A>, DuplicateLabelAssetError> {
495+
) -> Handle<A> {
499496
let label = label.into();
500497
let CompleteLoadedAsset {
501498
asset,
@@ -506,25 +503,19 @@ impl<'a> LoadContext<'a> {
506503
let handle = self
507504
.asset_server
508505
.get_or_create_path_handle(labeled_path, None);
509-
let has_duplicate = self
510-
.labeled_assets
511-
.insert(
512-
label.clone(),
513-
LabeledAsset {
514-
asset: loaded_asset,
515-
handle: handle.clone().untyped(),
516-
},
517-
)
518-
.is_some();
519-
if has_duplicate {
520-
return Err(DuplicateLabelAssetError(label.to_string()));
521-
}
506+
self.labeled_assets.insert(
507+
label,
508+
LabeledAsset {
509+
asset: loaded_asset,
510+
handle: handle.clone().untyped(),
511+
},
512+
);
522513
for (label, asset) in labeled_assets {
523514
if self.labeled_assets.insert(label.clone(), asset).is_some() {
524-
return Err(DuplicateLabelAssetError(label.to_string()));
515+
warn!("A labeled asset with the label \"{label}\" already exists. Replacing with the new asset.");
525516
}
526517
}
527-
Ok(handle)
518+
handle
528519
}
529520

530521
/// Returns `true` if an asset with the label `label` exists in this context.
@@ -674,8 +665,3 @@ pub enum ReadAssetBytesError {
674665
#[error("The LoadContext for this read_asset_bytes call requires hash metadata, but it was not provided. This is likely an internal implementation error.")]
675666
MissingAssetHash,
676667
}
677-
678-
/// An error when labeled assets have the same label, containing the duplicate label.
679-
#[derive(Error, Debug)]
680-
#[error("Encountered a duplicate label while loading an asset: \"{0}\"")]
681-
pub struct DuplicateLabelAssetError(pub String);

0 commit comments

Comments
 (0)