From 7740c0f879207cf9e970160139cb96bd101d8b50 Mon Sep 17 00:00:00 2001 From: Rich Churcher Date: Tue, 5 Nov 2024 11:06:00 +1300 Subject: [PATCH] Support creating asset directories (#16220) # Objective Exposes a means to create an asset directory (and its parent directories). Wasn't sure whether we also wanted the variant to create directories without the parent (i.e. `mkdir` instead of `mkdir -p`)? Fixes https://github.com/bevyengine/bevy_editor_prototypes/issues/144 --- crates/bevy_asset/src/io/file/file_asset.rs | 6 ++++++ .../bevy_asset/src/io/file/sync_file_asset.rs | 6 ++++++ crates/bevy_asset/src/io/mod.rs | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/crates/bevy_asset/src/io/file/file_asset.rs b/crates/bevy_asset/src/io/file/file_asset.rs index 3c20702167..a7af0197e2 100644 --- a/crates/bevy_asset/src/io/file/file_asset.rs +++ b/crates/bevy_asset/src/io/file/file_asset.rs @@ -164,6 +164,12 @@ impl AssetWriter for FileAssetWriter { Ok(()) } + async fn create_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> { + let full_path = self.root_path.join(path); + async_fs::create_dir_all(full_path).await?; + Ok(()) + } + async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> { let full_path = self.root_path.join(path); async_fs::remove_dir_all(full_path).await?; diff --git a/crates/bevy_asset/src/io/file/sync_file_asset.rs b/crates/bevy_asset/src/io/file/sync_file_asset.rs index 5887724736..cadea49492 100644 --- a/crates/bevy_asset/src/io/file/sync_file_asset.rs +++ b/crates/bevy_asset/src/io/file/sync_file_asset.rs @@ -205,6 +205,12 @@ impl AssetWriter for FileAssetWriter { Ok(()) } + async fn create_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> { + let full_path = self.root_path.join(path); + std::fs::create_dir_all(full_path)?; + Ok(()) + } + async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> { let full_path = self.root_path.join(path); std::fs::remove_dir_all(full_path)?; diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index eceefb2e7a..207e51659b 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -384,6 +384,12 @@ pub trait AssetWriter: Send + Sync + 'static { old_path: &'a Path, new_path: &'a Path, ) -> impl ConditionalSendFuture>; + /// Creates a directory at the given path, including all parent directories if they do not + /// already exist. + fn create_directory<'a>( + &'a self, + path: &'a Path, + ) -> impl ConditionalSendFuture>; /// Removes the directory at the given path, including all assets _and_ directories in that directory. fn remove_directory<'a>( &'a self, @@ -460,6 +466,12 @@ pub trait ErasedAssetWriter: Send + Sync + 'static { old_path: &'a Path, new_path: &'a Path, ) -> BoxedFuture<'a, Result<(), AssetWriterError>>; + /// Creates a directory at the given path, including all parent directories if they do not + /// already exist. + fn create_directory<'a>( + &'a self, + path: &'a Path, + ) -> BoxedFuture<'a, Result<(), AssetWriterError>>; /// Removes the directory at the given path, including all assets _and_ directories in that directory. fn remove_directory<'a>( &'a self, @@ -523,6 +535,12 @@ impl ErasedAssetWriter for T { ) -> BoxedFuture<'a, Result<(), AssetWriterError>> { Box::pin(Self::rename_meta(self, old_path, new_path)) } + fn create_directory<'a>( + &'a self, + path: &'a Path, + ) -> BoxedFuture<'a, Result<(), AssetWriterError>> { + Box::pin(Self::create_directory(self, path)) + } fn remove_directory<'a>( &'a self, path: &'a Path,