Skip to content

Commit 259fb68

Browse files
mockersfnvdaz
andauthored
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 <[email protected]>
1 parent 0387331 commit 259fb68

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

crates/bevy_asset/src/io/file/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod file_asset;
66
#[cfg(not(feature = "multi-threaded"))]
77
mod sync_file_asset;
88

9+
use bevy_log::error;
910
#[cfg(feature = "file_watcher")]
1011
pub use file_watcher::*;
1112

@@ -73,7 +74,15 @@ impl FileAssetWriter {
7374
/// watching for changes.
7475
///
7576
/// See `get_base_path` below.
76-
pub fn new<P: AsRef<Path>>(path: P) -> Self {
77+
pub fn new<P: AsRef<Path> + std::fmt::Debug>(path: P, create_root: bool) -> Self {
78+
if create_root {
79+
if let Err(e) = std::fs::create_dir_all(&path) {
80+
error!(
81+
"Failed to create root directory {:?} for file asset writer: {:?}",
82+
path, e
83+
);
84+
}
85+
}
7786
Self {
7887
root_path: get_base_path().join(path.as_ref()),
7988
}

crates/bevy_asset/src/io/source.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> PartialEq for AssetSourceId<'a> {
111111
#[derive(Default)]
112112
pub struct AssetSourceBuilder {
113113
pub reader: Option<Box<dyn FnMut() -> Box<dyn AssetReader> + Send + Sync>>,
114-
pub writer: Option<Box<dyn FnMut() -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
114+
pub writer: Option<Box<dyn FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
115115
pub watcher: Option<
116116
Box<
117117
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
@@ -120,7 +120,8 @@ pub struct AssetSourceBuilder {
120120
>,
121121
>,
122122
pub processed_reader: Option<Box<dyn FnMut() -> Box<dyn AssetReader> + Send + Sync>>,
123-
pub processed_writer: Option<Box<dyn FnMut() -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
123+
pub processed_writer:
124+
Option<Box<dyn FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
124125
pub processed_watcher: Option<
125126
Box<
126127
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
@@ -142,8 +143,8 @@ impl AssetSourceBuilder {
142143
watch_processed: bool,
143144
) -> Option<AssetSource> {
144145
let reader = self.reader.as_mut()?();
145-
let writer = self.writer.as_mut().and_then(|w| w());
146-
let processed_writer = self.processed_writer.as_mut().and_then(|w| w());
146+
let writer = self.writer.as_mut().and_then(|w| w(false));
147+
let processed_writer = self.processed_writer.as_mut().and_then(|w| w(true));
147148
let mut source = AssetSource {
148149
id: id.clone(),
149150
reader,
@@ -200,7 +201,7 @@ impl AssetSourceBuilder {
200201
/// Will use the given `writer` function to construct unprocessed [`AssetWriter`] instances.
201202
pub fn with_writer(
202203
mut self,
203-
writer: impl FnMut() -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
204+
writer: impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
204205
) -> Self {
205206
self.writer = Some(Box::new(writer));
206207
self
@@ -230,7 +231,7 @@ impl AssetSourceBuilder {
230231
/// Will use the given `writer` function to construct processed [`AssetWriter`] instances.
231232
pub fn with_processed_writer(
232233
mut self,
233-
writer: impl FnMut() -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
234+
writer: impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
234235
) -> Self {
235236
self.processed_writer = Some(Box::new(writer));
236237
self
@@ -443,10 +444,13 @@ impl AssetSource {
443444
/// the asset root. This will return [`None`] if this platform does not support writing assets by default.
444445
pub fn get_default_writer(
445446
_path: String,
446-
) -> impl FnMut() -> Option<Box<dyn AssetWriter>> + Send + Sync {
447-
move || {
447+
) -> impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync {
448+
move |_create_root: bool| {
448449
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
449-
return Some(Box::new(super::file::FileAssetWriter::new(&_path)));
450+
return Some(Box::new(super::file::FileAssetWriter::new(
451+
&_path,
452+
_create_root,
453+
)));
450454
#[cfg(any(target_arch = "wasm32", target_os = "android"))]
451455
return None;
452456
}

0 commit comments

Comments
 (0)