Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print precise and correct watch warnings (and only when necessary) #10787

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ impl EmbeddedAssetRegistry {
Box::new(MemoryAssetReader {
root: processed_dir.clone(),
})
});
})
// Note that we only add a processed watch warning because we don't want to warn
// noisily about embedded watching (which is niche) when users enable file watching.
.with_processed_watch_warning(
"Consider enabling the `embedded_watcher` cargo feature.",
);

#[cfg(feature = "embedded_watcher")]
{
Expand Down
48 changes: 41 additions & 7 deletions crates/bevy_asset/src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct AssetSourceBuilder {
+ Sync,
>,
>,
pub watch_warning: Option<&'static str>,
pub processed_watch_warning: Option<&'static str>,
}

impl AssetSourceBuilder {
Expand Down Expand Up @@ -156,23 +158,31 @@ impl AssetSourceBuilder {

if watch {
let (sender, receiver) = crossbeam_channel::unbounded();
match self.watcher.as_mut().and_then(|w|(w)(sender)) {
match self.watcher.as_mut().and_then(|w| (w)(sender)) {
Some(w) => {
source.watcher = Some(w);
source.event_receiver = Some(receiver);
},
None => warn!("{id} does not have an AssetWatcher configured. Consider enabling the `file_watcher` feature. Note that Web and Android do not currently support watching assets."),
}
None => {
if let Some(warning) = self.watch_warning {
warn!("{id} does not have an AssetWatcher configured. {warning}");
}
}
}
}

if watch_processed {
let (sender, receiver) = crossbeam_channel::unbounded();
match self.processed_watcher.as_mut().and_then(|w|(w)(sender)) {
match self.processed_watcher.as_mut().and_then(|w| (w)(sender)) {
Some(w) => {
source.processed_watcher = Some(w);
source.processed_event_receiver = Some(receiver);
},
None => warn!("{id} does not have a processed AssetWatcher configured. Consider enabling the `file_watcher` feature. Note that Web and Android do not currently support watching assets."),
}
None => {
if let Some(warning) = self.processed_watch_warning {
warn!("{id} does not have a processed AssetWatcher configured. {warning}");
}
}
}
}
Some(source)
Expand Down Expand Up @@ -238,6 +248,18 @@ impl AssetSourceBuilder {
self
}

/// Enables a warning for the unprocessed source watcher, which will print when watching is enabled and the unprocessed source doesn't have a watcher.
pub fn with_watch_warning(mut self, warning: &'static str) -> Self {
self.watch_warning = Some(warning);
self
}

/// Enables a warning for the processed source watcher, which will print when watching is enabled and the processed source doesn't have a watcher.
pub fn with_processed_watch_warning(mut self, warning: &'static str) -> Self {
self.processed_watch_warning = Some(warning);
self
}

/// Returns a builder containing the "platform default source" for the given `path` and `processed_path`.
/// For most platforms, this will use [`FileAssetReader`](crate::io::file::FileAssetReader) / [`FileAssetWriter`](crate::io::file::FileAssetWriter),
/// but some platforms (such as Android) have their own default readers / writers / watchers.
Expand All @@ -248,7 +270,8 @@ impl AssetSourceBuilder {
.with_watcher(AssetSource::get_default_watcher(
path.to_string(),
Duration::from_millis(300),
));
))
.with_watch_warning(AssetSource::get_default_watch_warning());
if let Some(processed_path) = processed_path {
default
.with_processed_reader(AssetSource::get_default_reader(processed_path.to_string()))
Expand All @@ -257,6 +280,7 @@ impl AssetSourceBuilder {
processed_path.to_string(),
Duration::from_millis(300),
))
.with_processed_watch_warning(AssetSource::get_default_watch_warning())
} else {
default
}
Expand Down Expand Up @@ -428,6 +452,16 @@ impl AssetSource {
}
}

/// Returns the default non-existent [`AssetWatcher`] warning for the current platform.
pub fn get_default_watch_warning() -> &'static str {
#[cfg(target_arch = "wasm32")]
return "Web does not currently support watching assets.";
#[cfg(target_os = "android")]
return "Android does not currently support watching assets.";
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
return "Consider enabling the `file_watcher` feature.";
}

/// Returns a builder function for this platform's default [`AssetWatcher`]. `path` is the relative path to
/// the asset root. This will return [`None`] if this platform does not support watching assets by default.
/// `file_debounce_time` is the amount of time to wait (and debounce duplicate events) before returning an event.
Expand Down