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

Switch to ureq, and store headers/query in a resource #36

Open
wants to merge 6 commits 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
18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ repository = "https://github.com/johanhelsing/bevy_web_asset"
version = "0.9.0"

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
"bevy_asset",
] }
bevy = { version = "0.14", default-features = false, features = ["bevy_asset"] }
pin-project = "1.1.5"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
surf = { version = "2.3", default-features = false, features = [
"h1-client-rustls",
] }
ureq = "2.10.1"
bevy_tasks = { version = "*", features = ["multi_threaded"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.67", default-features = false }
web-sys = { version = "0.3.67", default-features = false, features = [
"Window",
"Request",
"Headers",
"RequestInit",
"RequestMode",
] }
js-sys = { version = "0.3", default-features = false }
wasm-bindgen = { version = "0.2", default-features = false }
wasm-bindgen-futures = "0.4"
Expand All @@ -37,6 +40,7 @@ bevy = { version = "0.14", default-features = false, features = [
"bevy_core_pipeline",
"bevy_sprite",
"png",
"jpeg",
"webgl2",
"x11", # GitHub Actions runners don't have libxkbcommon installed, so can't use Wayland
] }
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,30 @@ commands.spawn(SpriteBundle {
});
```

Supports adding headers and query parameters, specified initially when adding the plugin:

```rust ignore
WebAssetPlugin::default()
.enable_fake_extensions() // for URLs which don't have a file extension, add "..png" which won't be sent
.push_header("x-api-key", "somekey") // set a api key for all requests
.push_header("Accept", "application/octet-stream") // we want a binary file
.push_query("quality", "high"), // this appends ?quality=high to the actual requests
```

## Bevy version support

I intend to support the latest bevy release in the `main` branch.

|bevy|bevy_web_asset|
|----|--------------|
|0.14|0.9, main |
|0.13|0.8 |
|0.12|0.7 |
|0.9 |0.5 |
|0.8 |0.4 |
|0.7 |0.3 |
|0.6 |0.2 |
|0.5 |0.1 |
| bevy | bevy_web_asset |
| ---- | -------------- |
| 0.14 | 0.9, main |
| 0.13 | 0.8 |
| 0.12 | 0.7 |
| 0.9 | 0.5 |
| 0.8 | 0.4 |
| 0.7 | 0.3 |
| 0.6 | 0.2 |
| 0.5 | 0.1 |

## License

Expand Down
5 changes: 3 additions & 2 deletions examples/web_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
.add_plugins((
// The web asset plugin must be inserted before the `AssetPlugin` so
// that the AssetPlugin recognizes the new sources.
WebAssetPlugin,
WebAssetPlugin::default(),
DefaultPlugins,
))
.add_systems(Startup, setup)
Expand All @@ -18,7 +18,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

commands.spawn(SpriteBundle {
// Simply use a url where you would normally use an asset folder relative path
texture: asset_server.load("https://s3.johanhelsing.studio/dump/favicon.png"),
texture: asset_server
.load("https://pixnio.com/free-images/2024/09/30/2024-09-30-09-05-06-960x640.jpg"), // no-attribution pixnio license
..default()
});
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ mod web_asset_source;

pub use web_asset_plugin::WebAssetPlugin;
pub use web_asset_source::WebAssetReader;
pub use web_asset_source::WebAssetReaderData;
pub use web_asset_source::WebAssetReaderDataInner;
60 changes: 57 additions & 3 deletions src/web_asset_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::{Arc, RwLock};

use bevy::prelude::*;

use crate::web_asset_source::*;
Expand All @@ -21,17 +23,69 @@ use bevy::asset::io::AssetSource;
/// ));
/// ```
#[derive(Default)]
pub struct WebAssetPlugin;
pub struct WebAssetPlugin {
data: WebAssetReaderDataInner,
}

impl WebAssetPlugin {
/// Headers will be passed along with each request
pub fn new(data: WebAssetReaderDataInner) -> Self {
Self { data }
}

/// Enable "fake extension". This turns "test/example..png" into "test/example", but leaves single dots alone.
pub fn enable_fake_extensions(mut self) -> Self {
self.data.fake_extensions = true;
self
}

/// Push a new header to be sent along every asset load. The same key can be pushed multiple times.
pub fn push_header(mut self, key: impl ToString, value: impl ToString) -> Self {
self.data
.headers
.entry(key.to_string())
.or_insert_with(Vec::new)
.push(value.to_string());
self
}

/// Push a query parameter, which will be appended to the reqeust before its sent
pub fn push_query(mut self, key: impl ToString, value: impl ToString) -> Self {
self.data.query.insert(key.to_string(), value.to_string());
self
}
}

impl Plugin for WebAssetPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(WebAssetReaderData {
data: Arc::new(RwLock::new(self.data.clone())),
});

// seems to be the best way to resolve borrow/move issues here with the closures
let (weak_a, weak_b) = {
let res = app.world().resource::<WebAssetReaderData>();
(Arc::downgrade(&res.data), Arc::downgrade(&res.data))
};

app.register_asset_source(
"http",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Http)),
AssetSource::build().with_reader(move || {
Box::new(WebAssetReader {
protocol: Protocol::Http,
shared: weak_a.upgrade().unwrap(),
})
}),
);

app.register_asset_source(
"https",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Https)),
AssetSource::build().with_reader(move || {
Box::new(WebAssetReader {
protocol: Protocol::Https,
shared: weak_b.upgrade().unwrap(),
})
}),
);
}
}
Loading