Skip to content

Commit 4c14f2a

Browse files
committed
AssetLoadRetryPlugin
1 parent c9e1fcd commit 4c14f2a

File tree

7 files changed

+862
-3
lines changed

7 files changed

+862
-3
lines changed

crates/bevy_asset/src/io/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use std::{
3232
};
3333
use thiserror::Error;
3434

35+
use crate::{retry::AssetLoadRetrySettings, UntypedAssetLoadFailedEvent};
36+
3537
/// Errors that occur while loading assets.
3638
#[derive(Error, Debug, Clone)]
3739
pub enum AssetReaderError {
@@ -97,6 +99,14 @@ pub trait AssetReader: Send + Sync + 'static {
9799
Ok(meta_bytes)
98100
})
99101
}
102+
103+
/// Returns default retry settings to use for a particular failed asset load attempt using this reader.
104+
fn get_default_retry_settings(
105+
&self,
106+
_load_error: &UntypedAssetLoadFailedEvent,
107+
) -> AssetLoadRetrySettings {
108+
AssetLoadRetrySettings::no_retries()
109+
}
100110
}
101111

102112
pub type Writer = dyn AsyncWrite + Unpin + Send + Sync;

crates/bevy_asset/src/io/wasm.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use crate::io::{
2-
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
1+
use crate::{
2+
io::{
3+
get_meta_path, AssetLoadRetrySettings, AssetReader, AssetReaderError, EmptyPathStream,
4+
PathStream, Reader, VecReader,
5+
},
6+
retry::{IoErrorRetrySettingsProvider, ProvideAssetLoadRetrySettings},
7+
UntypedAssetLoadFailedEvent,
38
};
49
use bevy_log::error;
510
use bevy_utils::BoxedFuture;
@@ -12,15 +17,24 @@ use web_sys::Response;
1217
/// Reader implementation for loading assets via HTTP in WASM.
1318
pub struct HttpWasmAssetReader {
1419
root_path: PathBuf,
20+
retry_settings_provider: Box<dyn ProvideAssetLoadRetrySettings>,
1521
}
1622

1723
impl HttpWasmAssetReader {
1824
/// Creates a new `WasmAssetReader`. The path provided will be used to build URLs to query for assets.
1925
pub fn new<P: AsRef<Path>>(path: P) -> Self {
2026
Self {
2127
root_path: path.as_ref().to_owned(),
28+
retry_settings_provider: Box::new(IoErrorRetrySettingsProvider::from(
29+
AssetLoadRetrySettings::network_default(),
30+
)),
2231
}
2332
}
33+
/// Overrides the default retry settings.
34+
pub fn with_retry_defaults(mut self, provider: Box<dyn ProvideAssetLoadRetrySettings>) -> Self {
35+
self.retry_settings_provider = provider;
36+
self
37+
}
2438
}
2539

2640
fn js_value_to_err<'a>(context: &'a str) -> impl FnOnce(JsValue) -> std::io::Error + 'a {
@@ -95,4 +109,12 @@ impl AssetReader for HttpWasmAssetReader {
95109
error!("Reading directories is not supported with the HttpWasmAssetReader");
96110
Box::pin(async move { Ok(false) })
97111
}
112+
113+
fn get_default_retry_settings(
114+
&self,
115+
load_error: &UntypedAssetLoadFailedEvent,
116+
) -> AssetLoadRetrySettings {
117+
self.retry_settings_provider
118+
.get_retry_settings(AssetLoadRetrySettings::no_retries(), load_error)
119+
}
98120
}

crates/bevy_asset/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
pub mod io;
22
pub mod meta;
33
pub mod processor;
4+
pub mod retry;
45
pub mod saver;
56

67
pub mod prelude {
78
#[doc(hidden)]
89
pub use crate::{
10+
retry::{AssetLoadRetrier, AssetLoadRetryPlugin, AssetLoadRetrySettings},
911
Asset, AssetApp, AssetEvent, AssetId, AssetMode, AssetPlugin, AssetServer, Assets, Handle,
1012
UntypedHandle,
1113
};

0 commit comments

Comments
 (0)