From 259fb6896e07bed89df2637aace547f295fc7e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 21 Jan 2024 10:30:43 +0100 Subject: [PATCH] auto create imported asset folder if needed (#11284) # Objective - Since #11218, example `asset_processing` fails: ``` thread 'main' panicked at crates/bevy_asset/src/io/source.rs:489:18: Failed to create file watcher: Error { kind: PathNotFound, paths: ["examples/asset/processing/imported_assets/Default"] } ``` start from a fresh git clone or delete the folder before running to reproduce, it is in gitignore and should not be present on a fresh run https://github.com/bevyengine/bevy/blob/a6574786757c0a0a7ddffb99fdc40ce90980fc82/.gitignore#L18 ## Solution - Auto create the `imported_assets` folder if it is configured --------- Co-authored-by: Kyle <37520732+nvdaz@users.noreply.github.com> --- crates/bevy_asset/src/io/file/mod.rs | 11 ++++++++++- crates/bevy_asset/src/io/source.rs | 22 +++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/bevy_asset/src/io/file/mod.rs b/crates/bevy_asset/src/io/file/mod.rs index 8aeff687d6c07..6ee59fab37d24 100644 --- a/crates/bevy_asset/src/io/file/mod.rs +++ b/crates/bevy_asset/src/io/file/mod.rs @@ -6,6 +6,7 @@ mod file_asset; #[cfg(not(feature = "multi-threaded"))] mod sync_file_asset; +use bevy_log::error; #[cfg(feature = "file_watcher")] pub use file_watcher::*; @@ -73,7 +74,15 @@ impl FileAssetWriter { /// watching for changes. /// /// See `get_base_path` below. - pub fn new>(path: P) -> Self { + pub fn new + std::fmt::Debug>(path: P, create_root: bool) -> Self { + if create_root { + if let Err(e) = std::fs::create_dir_all(&path) { + error!( + "Failed to create root directory {:?} for file asset writer: {:?}", + path, e + ); + } + } Self { root_path: get_base_path().join(path.as_ref()), } diff --git a/crates/bevy_asset/src/io/source.rs b/crates/bevy_asset/src/io/source.rs index a9f8adb8c17f3..57f6170d17b3e 100644 --- a/crates/bevy_asset/src/io/source.rs +++ b/crates/bevy_asset/src/io/source.rs @@ -111,7 +111,7 @@ impl<'a> PartialEq for AssetSourceId<'a> { #[derive(Default)] pub struct AssetSourceBuilder { pub reader: Option Box + Send + Sync>>, - pub writer: Option Option> + Send + Sync>>, + pub writer: Option Option> + Send + Sync>>, pub watcher: Option< Box< dyn FnMut(crossbeam_channel::Sender) -> Option> @@ -120,7 +120,8 @@ pub struct AssetSourceBuilder { >, >, pub processed_reader: Option Box + Send + Sync>>, - pub processed_writer: Option Option> + Send + Sync>>, + pub processed_writer: + Option Option> + Send + Sync>>, pub processed_watcher: Option< Box< dyn FnMut(crossbeam_channel::Sender) -> Option> @@ -142,8 +143,8 @@ impl AssetSourceBuilder { watch_processed: bool, ) -> Option { let reader = self.reader.as_mut()?(); - let writer = self.writer.as_mut().and_then(|w| w()); - let processed_writer = self.processed_writer.as_mut().and_then(|w| w()); + let writer = self.writer.as_mut().and_then(|w| w(false)); + let processed_writer = self.processed_writer.as_mut().and_then(|w| w(true)); let mut source = AssetSource { id: id.clone(), reader, @@ -200,7 +201,7 @@ impl AssetSourceBuilder { /// Will use the given `writer` function to construct unprocessed [`AssetWriter`] instances. pub fn with_writer( mut self, - writer: impl FnMut() -> Option> + Send + Sync + 'static, + writer: impl FnMut(bool) -> Option> + Send + Sync + 'static, ) -> Self { self.writer = Some(Box::new(writer)); self @@ -230,7 +231,7 @@ impl AssetSourceBuilder { /// Will use the given `writer` function to construct processed [`AssetWriter`] instances. pub fn with_processed_writer( mut self, - writer: impl FnMut() -> Option> + Send + Sync + 'static, + writer: impl FnMut(bool) -> Option> + Send + Sync + 'static, ) -> Self { self.processed_writer = Some(Box::new(writer)); self @@ -443,10 +444,13 @@ impl AssetSource { /// the asset root. This will return [`None`] if this platform does not support writing assets by default. pub fn get_default_writer( _path: String, - ) -> impl FnMut() -> Option> + Send + Sync { - move || { + ) -> impl FnMut(bool) -> Option> + Send + Sync { + move |_create_root: bool| { #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] - return Some(Box::new(super::file::FileAssetWriter::new(&_path))); + return Some(Box::new(super::file::FileAssetWriter::new( + &_path, + _create_root, + ))); #[cfg(any(target_arch = "wasm32", target_os = "android"))] return None; }