diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 18008a24135a2..7ba0cece7b7ac 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -34,7 +34,7 @@ rand = "0.8.0" [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2" } -web-sys = { version = "0.3", features = ["Request", "Window", "Response"]} +web-sys = { version = "0.3", features = ["Request", "Window", "Response"] } wasm-bindgen-futures = "0.4" js-sys = "0.3" @@ -44,3 +44,4 @@ ndk-glue = { version = "0.5" } [dev-dependencies] futures-lite = "1.4.0" tempfile = "3.2.0" +bevy_core = { path = "../bevy_core", version = "0.6.0" } diff --git a/crates/bevy_asset/src/assets.rs b/crates/bevy_asset/src/assets.rs index ade30b169dcd6..27d013588a067 100644 --- a/crates/bevy_asset/src/assets.rs +++ b/crates/bevy_asset/src/assets.rs @@ -270,10 +270,16 @@ pub trait AddAsset { } impl AddAsset for App { + /// Add an [`Asset`] to the [`App`]. + /// + /// Adding the same [`Asset`] again after it has been added does nothing. fn add_asset(&mut self) -> &mut Self where T: Asset, { + if self.world.contains_resource::>() { + return self; + } let assets = { let asset_server = self.world.get_resource::().unwrap(); asset_server.register_asset_type::() @@ -305,3 +311,26 @@ impl AddAsset for App { self } } + +#[cfg(test)] +mod tests { + use bevy_app::App; + + use crate::{AddAsset, Assets}; + + #[test] + fn asset_overwriting() { + #[derive(bevy_reflect::TypeUuid)] + #[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"] + struct MyAsset; + let mut app = App::new(); + app.add_plugin(bevy_core::CorePlugin) + .add_plugin(crate::AssetPlugin); + app.add_asset::(); + let mut assets_before = app.world.get_resource_mut::>().unwrap(); + let handle = assets_before.add(MyAsset); + app.add_asset::(); // Ensure this doesn't overwrite the Asset + let assets_after = app.world.get_resource_mut::>().unwrap(); + assert!(assets_after.get(handle).is_some()) + } +}