From bc910ff3e8f1ecf7cabefa3fcd7ea8f813113b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 10 Jan 2024 07:20:31 +0100 Subject: [PATCH 1/3] auto create imported asset folder if needed --- 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..411c2619d092d 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::warn; #[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, should_create: bool) -> Self { + if should_create { + if let Err(e) = std::fs::create_dir_all(&path) { + warn!( + "Failed to create root directory {:?} for file asset reader: {:?}", + 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 b16dafb5bfc6c..88fa404319c72 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 |_should_create: 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, + _should_create, + ))); #[cfg(any(target_arch = "wasm32", target_os = "android"))] return None; } From eccc51eb6765cca22a4e4240f8f80fb35dcebd8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 10 Jan 2024 21:52:29 +0100 Subject: [PATCH 2/3] switched to error --- crates/bevy_asset/src/io/file/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_asset/src/io/file/mod.rs b/crates/bevy_asset/src/io/file/mod.rs index 411c2619d092d..864c158532715 100644 --- a/crates/bevy_asset/src/io/file/mod.rs +++ b/crates/bevy_asset/src/io/file/mod.rs @@ -6,7 +6,7 @@ mod file_asset; #[cfg(not(feature = "multi-threaded"))] mod sync_file_asset; -use bevy_log::warn; +use bevy_log::error; #[cfg(feature = "file_watcher")] pub use file_watcher::*; @@ -77,8 +77,8 @@ impl FileAssetWriter { pub fn new + std::fmt::Debug>(path: P, should_create: bool) -> Self { if should_create { if let Err(e) = std::fs::create_dir_all(&path) { - warn!( - "Failed to create root directory {:?} for file asset reader: {:?}", + error!( + "Failed to create root directory {:?} for file asset writer: {:?}", path, e ); } From 72d3c3671d5fb3770c4693adc17c58b8065c98d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 11 Jan 2024 06:30:03 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Kyle <37520732+nvdaz@users.noreply.github.com> --- crates/bevy_asset/src/io/file/mod.rs | 4 ++-- crates/bevy_asset/src/io/source.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_asset/src/io/file/mod.rs b/crates/bevy_asset/src/io/file/mod.rs index 864c158532715..6ee59fab37d24 100644 --- a/crates/bevy_asset/src/io/file/mod.rs +++ b/crates/bevy_asset/src/io/file/mod.rs @@ -74,8 +74,8 @@ impl FileAssetWriter { /// watching for changes. /// /// See `get_base_path` below. - pub fn new + std::fmt::Debug>(path: P, should_create: bool) -> Self { - if should_create { + 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: {:?}", diff --git a/crates/bevy_asset/src/io/source.rs b/crates/bevy_asset/src/io/source.rs index 88fa404319c72..d16c9c44f6f4b 100644 --- a/crates/bevy_asset/src/io/source.rs +++ b/crates/bevy_asset/src/io/source.rs @@ -445,11 +445,11 @@ impl AssetSource { pub fn get_default_writer( _path: String, ) -> impl FnMut(bool) -> Option> + Send + Sync { - move |_should_create: bool| { + move |_create_root: bool| { #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))] return Some(Box::new(super::file::FileAssetWriter::new( &_path, - _should_create, + _create_root, ))); #[cfg(any(target_arch = "wasm32", target_os = "android"))] return None;