Skip to content

Commit

Permalink
Merge pull request #18 from voidentente/bevy-0.12
Browse files Browse the repository at this point in the history
Port to Bevy 0.12, use isahc instead of surf
  • Loading branch information
johanhelsing authored Nov 20, 2023
2 parents 4b0fcb3 + c03e743 commit 8aa8de8
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 136 deletions.
19 changes: 8 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
[package]
authors = ["Johan Helsing <[email protected]>"]
categories = ["network-programming", "game-development", "wasm", "web-programming"]
description = "Bevy asset loader that transparently supports loading over http(s)"
description = "Implementations for http(s) asset sources for Bevy"
edition = "2021"
keywords = ["gamedev", "networking", "wasm", "bevy"]
license = "MIT OR Apache-2.0"
name = "bevy_web_asset"
repository = "https://github.com/johanhelsing/bevy_web_asset"
version = "0.6.0"
version = "0.7.0"

[dependencies]
bevy = {version = "0.10", default-features = false, features = ["bevy_asset"]}

# Copied from https://github.com/bevyengine/bevy/blob/main/crates/bevy_asset/Cargo.toml
crossbeam-channel = "0.5.0"
notify = "5.0.0"
bevy = {version = "0.12", default-features = false, features = ["bevy_asset"]}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
surf = {version = "2.3", default-features = false, features = ["h1-client-rustls"]}
isahc = "1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = {version = "0.3.22", default-features = false}
js-sys = {version = "0.3", default-features = false}
wasm-bindgen = {version = "0.2", default-features = false}
wasm-bindgen-futures = "0.4"
web-sys = {version = "0.3.22", default-features = false}

[dev-dependencies]
bevy = {version = "0.10", default-features = false, features = [
bevy = {version = "0.12", default-features = false, features = [
"bevy_asset",
"bevy_core_pipeline",
"bevy_sprite",
"png",
"webgl2",
"x11", # GitHub Actions runners don't have libxkbcommon installed, so can't use Wayland
]}
]}
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
[![crates.io](https://img.shields.io/crates/d/bevy_web_asset.svg)](https://crates.io/crates/bevy_web_asset)
[![docs.rs](https://img.shields.io/docsrs/bevy_web_asset)](https://docs.rs/bevy_web_asset)

This is a tiny crate that that wraps the standard bevy asset loader, and adds
the ability to load assets from http and https urls.
This is a tiny crate that that adds the ability to load assets from http and https urls.

Supports both wasm (web-sys) and native.

If asset paths start with http:// or https://, then we try to do a web request
to load the asset, otherwise, we just call the normal asset io.

This is nice if you want to keep your content on a server, even when developing
native games. Use cases can be:

Expand All @@ -25,7 +21,7 @@ native games. Use cases can be:

## Usage

NOTE: You need to add the plugin instead of `AssetPlugin` and before `DefaultPlugins`:
NOTE: You need to add the plugin before `AssetPlugin`:

Bevy 0.7 users, see [the 0.3.0 readme](https://github.com/johanhelsing/bevy_web_asset/tree/v0.3.0) for how to add the plugin.

Expand All @@ -37,9 +33,11 @@ use bevy_web_asset::WebAssetPlugin;

fn main() {
App::new()
// The `WebAssetPlugin` must be inserted instead of `AssetPlugin` and before `DefaultPlugins`
.add_plugin(WebAssetPlugin::default())
.add_plugins(DefaultPlugins.build().disable::<AssetPlugin>())
// The `WebAssetPlugin` must be inserted before the `AssetPlugin`
.add_plugins((
WebAssetPlugin::default(),
DefaultPlugins
))
// ...
.run();
}
Expand Down
6 changes: 3 additions & 3 deletions examples/web_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use bevy_web_asset::WebAssetPlugin;
fn main() {
App::new()
// The web asset plugin must be inserted before the `AssetPlugin` so
// that the AssetServer is already created by the time the AssetPlugin is initialized.
.add_plugin(WebAssetPlugin::default())
// that the AssetPlugin recognizes the new sources.
.add_plugins(WebAssetPlugin::default())
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_systems(Startup, setup)
.run();
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

mod web_asset_io;
mod web_asset_plugin;
mod web_asset_source;

pub use web_asset_io::WebAssetIo;
pub use web_asset_plugin::WebAssetPlugin;
102 changes: 0 additions & 102 deletions src/web_asset_io.rs

This file was deleted.

24 changes: 15 additions & 9 deletions src/web_asset_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy::prelude::*;

use super::WebAssetIo;
use crate::web_asset_source::*;
use bevy::asset::io::AssetSource;

/// Add this plugin to bevy to support loading http and https urls.
///
Expand All @@ -13,19 +14,24 @@ use super::WebAssetIo;
/// # use bevy_web_asset::WebAssetPlugin;
///
/// let mut app = App::new();
/// app.add_plugin(WebAssetPlugin);
/// app.add_plugins(DefaultPlugins);
///
/// app.add_plugins((
/// WebAssetPlugin::default(),
/// DefaultPlugins
/// ));
/// ```
///});
#[derive(Default)]
pub struct WebAssetPlugin;

impl Plugin for WebAssetPlugin {
fn build(&self, app: &mut App) {
let asset_io = WebAssetIo {
default_io: AssetPlugin::default().create_platform_default_asset_io(),
};

app.insert_resource(AssetServer::new(asset_io));
app.register_asset_source(
"http",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Http)),
);
app.register_asset_source(
"https",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Https)),
);
}
}
Loading

0 comments on commit 8aa8de8

Please sign in to comment.