diff --git a/crates/bevy_asset/src/io/embedded/mod.rs b/crates/bevy_asset/src/io/embedded/mod.rs index f6c26a19e1..f97cab0469 100644 --- a/crates/bevy_asset/src/io/embedded/mod.rs +++ b/crates/bevy_asset/src/io/embedded/mod.rs @@ -52,6 +52,13 @@ impl EmbeddedAssetRegistry { self.dir.insert_meta(asset_path, value); } + /// Removes an asset stored using `full_path` (the full path as [`file`] would return for that file, if it was capable of + /// running in a non-rust file). If no asset is stored with at `full_path` its a no-op. + /// It returning `Option` contains the originally stored `Data` or `None`. + pub fn remove_asset(&self, full_path: &Path) -> Option { + self.dir.remove_asset(full_path) + } + /// Registers a `embedded` [`AssetSource`] that uses this [`EmbeddedAssetRegistry`]. // NOTE: unused_mut because embedded_watcher feature is the only mutable consumer of `let mut source` #[allow(unused_mut)] @@ -300,7 +307,7 @@ macro_rules! load_internal_binary_asset { #[cfg(test)] mod tests { - use super::_embedded_asset_path; + use super::{EmbeddedAssetRegistry, _embedded_asset_path}; use std::path::Path; // Relative paths show up if this macro is being invoked by a local crate. @@ -404,4 +411,15 @@ mod tests { // Really, should be "my_crate/src/the/asset.png" assert_eq!(asset_path, Path::new("my_crate/the/asset.png")); } + + #[test] + fn remove_embedded_asset() { + let reg = EmbeddedAssetRegistry::default(); + let path = std::path::PathBuf::from("a/b/asset.png"); + reg.insert_asset(path.clone(), &path, &[]); + assert!(reg.dir.get_asset(&path).is_some()); + assert!(reg.remove_asset(&path).is_some()); + assert!(reg.dir.get_asset(&path).is_none()); + assert!(reg.remove_asset(&path).is_none()); + } } diff --git a/crates/bevy_asset/src/io/memory.rs b/crates/bevy_asset/src/io/memory.rs index 03edf58d1f..a3707e9b13 100644 --- a/crates/bevy_asset/src/io/memory.rs +++ b/crates/bevy_asset/src/io/memory.rs @@ -55,6 +55,17 @@ impl Dir { ); } + /// Removes the stored asset at `path` and returns the `Data` stored if found and otherwise `None`. + pub fn remove_asset(&self, path: &Path) -> Option { + let mut dir = self.clone(); + if let Some(parent) = path.parent() { + dir = self.get_or_insert_dir(parent); + } + let key: Box = path.file_name().unwrap().to_string_lossy().into(); + let data = dir.0.write().assets.remove(&key); + data + } + pub fn insert_meta(&self, path: &Path, value: impl Into) { let mut dir = self.clone(); if let Some(parent) = path.parent() {