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

Asset Load Retry Plugin #11349

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use std::{
};
use thiserror::Error;

use crate::{retry::AssetLoadRetrySettings, UntypedAssetLoadFailedEvent};

/// Errors that occur while loading assets.
#[derive(Error, Debug, Clone)]
pub enum AssetReaderError {
Expand Down Expand Up @@ -97,6 +99,14 @@ pub trait AssetReader: Send + Sync + 'static {
Ok(meta_bytes)
})
}

/// Returns default retry settings to use for a particular failed asset load attempt using this reader.
fn get_default_retry_settings(
&self,
_load_error: &UntypedAssetLoadFailedEvent,
) -> AssetLoadRetrySettings {
AssetLoadRetrySettings::no_retries()
}
}

pub type Writer = dyn AsyncWrite + Unpin + Send + Sync;
Expand Down
26 changes: 24 additions & 2 deletions crates/bevy_asset/src/io/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::io::{
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
use crate::{
io::{
get_meta_path, AssetLoadRetrySettings, AssetReader, AssetReaderError, EmptyPathStream,
PathStream, Reader, VecReader,
},
retry::{IoErrorRetrySettingsProvider, ProvideAssetLoadRetrySettings},
UntypedAssetLoadFailedEvent,
};
use bevy_log::error;
use bevy_utils::BoxedFuture;
Expand All @@ -12,15 +17,24 @@ use web_sys::Response;
/// Reader implementation for loading assets via HTTP in WASM.
pub struct HttpWasmAssetReader {
root_path: PathBuf,
retry_settings_provider: Box<dyn ProvideAssetLoadRetrySettings>,
}

impl HttpWasmAssetReader {
/// Creates a new `WasmAssetReader`. The path provided will be used to build URLs to query for assets.
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
root_path: path.as_ref().to_owned(),
retry_settings_provider: Box::new(IoErrorRetrySettingsProvider::from(
AssetLoadRetrySettings::network_default(),
)),
}
}
/// Overrides the default retry settings.
pub fn with_retry_defaults(mut self, provider: Box<dyn ProvideAssetLoadRetrySettings>) -> Self {
self.retry_settings_provider = provider;
self
}
}

fn js_value_to_err<'a>(context: &'a str) -> impl FnOnce(JsValue) -> std::io::Error + 'a {
Expand Down Expand Up @@ -95,4 +109,12 @@ impl AssetReader for HttpWasmAssetReader {
error!("Reading directories is not supported with the HttpWasmAssetReader");
Box::pin(async move { Ok(false) })
}

fn get_default_retry_settings(
&self,
load_error: &UntypedAssetLoadFailedEvent,
) -> AssetLoadRetrySettings {
self.retry_settings_provider
.get_retry_settings(AssetLoadRetrySettings::no_retries(), load_error)
}
}
2 changes: 2 additions & 0 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub mod io;
pub mod meta;
pub mod processor;
pub mod retry;
pub mod saver;

pub mod prelude {
#[doc(hidden)]
pub use crate::{
retry::{AssetLoadRetrier, AssetLoadRetryPlugin, AssetLoadRetrySettings},
Asset, AssetApp, AssetEvent, AssetId, AssetMode, AssetPlugin, AssetServer, Assets, Handle,
UntypedHandle,
};
Expand Down
Loading