Skip to content

Commit

Permalink
Add AppBuilder::asset_loader_from_instance (#580)
Browse files Browse the repository at this point in the history
* Implement add_asset_loader_from_instance

* Add example of different data loaders
  • Loading branch information
will-hart authored Oct 1, 2020
1 parent 056f84a commit 1beee4f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ bevy_gilrs = { path = "crates/bevy_gilrs", optional = true, version = "0.2.1" }
rand = "0.7.3"
serde = { version = "1", features = ["derive"] }
log = "0.4"
ron = "0.6"

#wasm
console_error_panic_hook = "0.1.6"
Expand Down Expand Up @@ -172,6 +173,10 @@ path = "examples/asset/hot_asset_reloading.rs"
name = "asset_loading"
path = "examples/asset/asset_loading.rs"

[[example]]
name = "custom_loader"
path = "examples/asset/custom_asset_loading.rs"

[[example]]
name = "audio"
path = "examples/audio/audio.rs"
Expand Down
3 changes: 3 additions & 0 deletions assets/data/test_data.data1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MyCustomData (
num: 42
)
3 changes: 3 additions & 0 deletions assets/data/test_data.data2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MySecondCustomData (
is_set: true
)
18 changes: 16 additions & 2 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub trait AddAsset {
where
TLoader: AssetLoader<TAsset> + FromResources,
TAsset: Send + Sync + 'static;
fn add_asset_loader_from_instance<TAsset, TLoader>(&mut self, instance: TLoader) -> &mut Self
where
TLoader: AssetLoader<TAsset> + FromResources,
TAsset: Send + Sync + 'static;
}

impl AddAsset for AppBuilder {
Expand All @@ -135,7 +139,7 @@ impl AddAsset for AppBuilder {
.add_event::<AssetEvent<T>>()
}

fn add_asset_loader<TAsset, TLoader>(&mut self) -> &mut Self
fn add_asset_loader_from_instance<TAsset, TLoader>(&mut self, instance: TLoader) -> &mut Self
where
TLoader: AssetLoader<TAsset> + FromResources,
TAsset: Send + Sync + 'static,
Expand All @@ -156,7 +160,7 @@ impl AddAsset for AppBuilder {
.resources()
.get_mut::<AssetServer>()
.expect("AssetServer does not exist. Consider adding it as a resource.");
asset_server.add_loader(TLoader::from_resources(self.resources()));
asset_server.add_loader(instance);
let handler = ChannelAssetHandler::new(
TLoader::from_resources(self.resources()),
asset_channel.sender.clone(),
Expand All @@ -165,4 +169,14 @@ impl AddAsset for AppBuilder {
}
self
}

fn add_asset_loader<TAsset, TLoader>(&mut self) -> &mut Self
where
TLoader: AssetLoader<TAsset> + FromResources,
TAsset: Send + Sync + 'static,
{
self.add_asset_loader_from_instance::<TAsset, TLoader>(TLoader::from_resources(
self.resources(),
))
}
}
76 changes: 76 additions & 0 deletions examples/asset/custom_asset_loading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use bevy::{asset::AssetLoader, prelude::*};
use ron::de::from_bytes;
use serde::Deserialize;
use std::path::Path;

#[derive(Deserialize)]
pub struct MyCustomData {
pub num: i32,
}

#[derive(Deserialize)]
pub struct MySecondCustomData {
pub is_set: bool,
}

// create a custom loader for data files
#[derive(Default)]
pub struct DataFileLoader {
matching_extensions: Vec<&'static str>,
}

impl DataFileLoader {
pub fn from_extensions(matching_extensions: Vec<&'static str>) -> Self {
DataFileLoader {
matching_extensions,
}
}
}

impl<TAsset> AssetLoader<TAsset> for DataFileLoader
where
for<'de> TAsset: Deserialize<'de>,
{
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<TAsset, anyhow::Error> {
Ok(from_bytes::<TAsset>(bytes.as_slice())?)
}

fn extensions(&self) -> &[&str] {
self.matching_extensions.as_slice()
}
}

/// This example illustrates various ways to load assets
fn main() {
App::build()
.add_default_plugins()
.add_asset::<MyCustomData>()
.add_asset_loader_from_instance::<MyCustomData, DataFileLoader>(
DataFileLoader::from_extensions(vec!["data1"]),
)
.add_asset::<MySecondCustomData>()
.add_asset_loader_from_instance::<MySecondCustomData, DataFileLoader>(
DataFileLoader::from_extensions(vec!["data2"]),
)
.add_startup_system(setup.system())
.run();
}

fn setup(
asset_server: Res<AssetServer>,
mut data1s: ResMut<Assets<MyCustomData>>,
mut data2s: ResMut<Assets<MySecondCustomData>>,
) {
let data1_handle = asset_server
.load_sync(&mut data1s, "assets/data/test_data.data1")
.unwrap();
let data2_handle = asset_server
.load_sync(&mut data2s, "assets/data/test_data.data2")
.unwrap();

let data1 = data1s.get(&data1_handle).unwrap();
println!("Data 1 loaded with value {}", data1.num);

let data2 = data2s.get(&data2_handle).unwrap();
println!("Data 2 loaded with value {}", data2.is_set);
}

0 comments on commit 1beee4f

Please sign in to comment.