Skip to content

Commit

Permalink
Merge pull request #13 from joverwey/johan/bevy_0_10
Browse files Browse the repository at this point in the history
Johan/bevy 0 10
  • Loading branch information
johanhelsing committed May 6, 2023
2 parents 8cac303 + b8ad519 commit 4b0fcb3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 160 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ 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.5.0"
version = "0.6.0"

[dependencies]
bevy = {version = "0.9", default-features = false, features = ["bevy_asset"]}
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"
Expand All @@ -26,9 +26,10 @@ wasm-bindgen-futures = "0.4"
web-sys = {version = "0.3.22", default-features = false}

[dev-dependencies]
bevy = {version = "0.9", default-features = false, features = [
bevy = {version = "0.10", default-features = false, features = [
"bevy_asset",
"bevy_core_pipeline",
"bevy_sprite",
"png",
"render",
"x11", # GitHub Actions runners don't have libxkbcommon installed, so can't use Wayland
]}
6 changes: 2 additions & 4 deletions examples/web_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use bevy_web_asset::WebAssetPlugin;
fn main() {
App::new()
// The web asset plugin must be inserted before the `AssetPlugin` so
// that the asset plugin doesn't create another instance of an asset
// server. WebAssetPlugin will handle initialization of AssetPlugin
// so we remove it from the default plugins group.
// that the AssetServer is already created by the time the AssetPlugin is initialized.
.add_plugin(WebAssetPlugin::default())
.add_plugins(DefaultPlugins.build().disable::<AssetPlugin>())
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
Expand Down
72 changes: 0 additions & 72 deletions src/filesystem_watcher.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
mod web_asset_io;
mod web_asset_plugin;

mod filesystem_watcher;

pub use web_asset_io::WebAssetIo;
pub use web_asset_plugin::WebAssetPlugin;
40 changes: 13 additions & 27 deletions src/web_asset_io.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use bevy::asset::{AssetIo, AssetIoError, BoxedFuture};
use std::{
path::{Path, PathBuf},
sync::{Arc, RwLock},
use bevy::{
asset::{AssetIo, AssetIoError},
utils::BoxedFuture,
};

use super::filesystem_watcher::FilesystemWatcher;
use std::path::{Path, PathBuf};

/// Wraps the default bevy AssetIo and adds support for loading http urls
pub struct WebAssetIo {
pub(crate) root_path: PathBuf,
pub(crate) default_io: Box<dyn AssetIo>,
pub(crate) filesystem_watcher: Arc<RwLock<Option<FilesystemWatcher>>>,
}

fn is_http(path: &Path) -> bool {
Expand All @@ -33,7 +29,7 @@ impl AssetIo for WebAssetIo {
.map_err(|e| e.dyn_into::<js_sys::TypeError>().unwrap());

if let Err(err) = &response {
warn!("Failed to fetch asset {uri}: {err:?}");
// warn!("Failed to fetch asset {uri}: {err:?}");
}

let response = response.map_err(|_| AssetIoError::NotFound(path.to_path_buf()))?;
Expand All @@ -55,7 +51,6 @@ impl AssetIo for WebAssetIo {
.body_bytes()
.await
.map_err(|_| AssetIoError::NotFound(path.to_path_buf()))?;

Ok(bytes)
});

Expand All @@ -72,34 +67,25 @@ impl AssetIo for WebAssetIo {
self.default_io.read_directory(path)
}

fn watch_path_for_changes(&self, path: &Path) -> Result<(), AssetIoError> {
if is_http(path) {
fn watch_path_for_changes(
&self,
to_watch: &Path,
to_reload: Option<PathBuf>,
) -> Result<(), AssetIoError> {
if is_http(to_watch) {
// TODO: we could potentially start polling over http here
// but should probably only be done if the server supports caching

// This is where we would write to a self.network_watcher

Ok(()) // Pretend everything is fine
} else {
// We can now simply use our own watcher

let absolute_path = self.root_path.join(path);

if let Ok(mut filesystem_watcher) = self.filesystem_watcher.write() {
if let Some(ref mut watcher) = *filesystem_watcher {
watcher
.watch(&absolute_path)
.map_err(|_error| AssetIoError::PathWatchError(absolute_path))?;
}
}

Ok(())
self.default_io.watch_path_for_changes(to_watch, to_reload)
}
}

fn watch_for_changes(&self) -> Result<(), AssetIoError> {
// self.filesystem_watcher is created in `web_asset_plugin.rs`
Ok(()) // This could create self.network_watcher
self.default_io.watch_for_changes()
}

fn is_dir(&self, path: &Path) -> bool {
Expand Down
56 changes: 5 additions & 51 deletions src/web_asset_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use bevy::asset::FileAssetIo;
use bevy::prelude::*;
use std::sync::{Arc, RwLock};

use super::filesystem_watcher::FilesystemWatcher;
use super::WebAssetIo;

/// Add this plugin to bevy to support loading http and https urls.
///
/// Needs to be added before Bevy's `DefaultPlugins`.
/// Also, make sure `AssetPlugin` is not loaded through `DefaultPlugins`.
///
/// # Example
///
Expand All @@ -17,61 +13,19 @@ use super::WebAssetIo;
/// # use bevy_web_asset::WebAssetPlugin;
///
/// let mut app = App::new();
/// // The web asset plugin should be added instead of the `AssetPlugin`
/// // Internally, WebAssetPlugin will create an AssetPlugin and hook into
/// // it in the right places
/// app.add_plugin(WebAssetPlugin::default());
/// app.add_plugins(DefaultPlugins.build().disable::<AssetPlugin>());
/// app.add_plugin(WebAssetPlugin);
/// app.add_plugins(DefaultPlugins);
/// ```
///});
#[derive(Default)]
pub struct WebAssetPlugin {
/// Settings for the underlying (regular) AssetPlugin
pub asset_plugin: AssetPlugin,
}
pub struct WebAssetPlugin;

impl Plugin for WebAssetPlugin {
fn build(&self, app: &mut App) {
// First, configure the underlying plugin
// We use out own watcher, so `watch_for_changes` is always false
let asset_plugin = AssetPlugin {
asset_folder: self.asset_plugin.asset_folder.clone(),
watch_for_changes: false,
};

// Create the `FileAssetIo` wrapper
let asset_io = {
// This makes calling `WebAssetIo::watch_for_changes` redundant
let filesystem_watcher = match self.asset_plugin.watch_for_changes {
true => Arc::new(RwLock::new(Some(FilesystemWatcher::default()))),
false => Arc::new(RwLock::new(None)),
};

// Create the `FileAssetIo`
let default_io = asset_plugin.create_platform_default_asset_io();

// The method doesn't change, so we just use `FileAssetIo`'s
let root_path = FileAssetIo::get_base_path().join(&self.asset_plugin.asset_folder);

WebAssetIo {
default_io,
root_path,
filesystem_watcher,
}
let asset_io = WebAssetIo {
default_io: AssetPlugin::default().create_platform_default_asset_io(),
};

// Add the asset server with our `WebAssetIo` wrapping `FileAssetIo`
app.insert_resource(AssetServer::new(asset_io));

// Add the asset plugin
app.add_plugin(asset_plugin);

// Optionally add the filesystem watcher system
if self.asset_plugin.watch_for_changes {
app.add_system_to_stage(
bevy::asset::AssetStage::LoadAssets,
super::filesystem_watcher::filesystem_watcher_system,
);
}
}
}

0 comments on commit 4b0fcb3

Please sign in to comment.