Skip to content
2 changes: 2 additions & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ uuid = { version = "1.13.1", default-features = false, features = [
"v4",
"serde",
] }
glob = "0.3.2"

tracing = { version = "0.1", default-features = false }

[target.'cfg(target_os = "android")'.dependencies]
Expand Down
31 changes: 31 additions & 0 deletions crates/bevy_asset/src/batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use alloc::vec::Vec;

use crate::{Asset, AssetPath, UntypedHandle};
use bevy_reflect::TypePath;

pub struct LoadBatchRequest {
pub requests: Vec<AssetPath<'static>>,
}

impl LoadBatchRequest {
pub fn new<T>(requests: Vec<T>) -> Self
where
T: Into<AssetPath<'static>>,
{
Self {
requests: requests.into_iter().map(Into::into).collect(),
}
}
Comment on lines +11 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but nicer (allows more than just vec![foo, bar]):

Suggested change
pub fn new<T>(requests: Vec<T>) -> Self
where
T: Into<AssetPath<'static>>,
{
Self {
requests: requests.into_iter().map(Into::into).collect(),
}
}
pub fn new<T>(requests: impl IntoIterator<Item = T>) -> Self
where
T: Into<AssetPath<'static>>,
{
Self {
requests: requests.into_iter().map(Into::into).collect(),
}
}

}

/// A "loaded batch" containing handles for all assets stored in a given [`AssetPath`].
///
/// This is produced by [`AssetServer::load_batch`](crate::prelude::AssetServer::load_batch).
///
/// [`AssetPath`]: crate::AssetPath
#[derive(Asset, TypePath)]
pub struct LoadedBatch {
/// The handles of all assets stored in the batch.
#[dependency]
pub handles: Vec<UntypedHandle>,
}
16 changes: 0 additions & 16 deletions crates/bevy_asset/src/folder.rs

This file was deleted.

25 changes: 13 additions & 12 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ pub mod prelude {

mod asset_changed;
mod assets;
mod batch;
mod direct_access_ext;
mod event;
mod folder;
mod handle;
mod id;
mod loader;
Expand All @@ -187,10 +187,10 @@ mod render_asset;
mod server;

pub use assets::*;
pub use batch::*;
pub use bevy_asset_macros::Asset;
pub use direct_access_ext::DirectAssetAccessExt;
pub use event::*;
pub use folder::*;
pub use futures_lite::{AsyncReadExt, AsyncWriteExt};
pub use handle::*;
pub use id::*;
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Plugin for AssetPlugin {
}
}
app.insert_resource(embedded)
.init_asset::<LoadedFolder>()
.init_asset::<LoadedBatch>()
.init_asset::<LoadedUntypedAsset>()
.init_asset::<()>()
.add_event::<UntypedAssetLoadFailedEvent>()
Expand Down Expand Up @@ -668,7 +668,7 @@ pub type AssetEvents = AssetEventSystems;
#[cfg(test)]
mod tests {
use crate::{
folder::LoadedFolder,
batch::LoadedBatch,
handle::Handle,
io::{
gated::{GateOpener, GatedReader},
Expand All @@ -677,7 +677,7 @@ mod tests {
},
loader::{AssetLoader, LoadContext},
Asset, AssetApp, AssetEvent, AssetId, AssetLoadError, AssetLoadFailedEvent, AssetPath,
AssetPlugin, AssetServer, Assets, LoadState, UnapprovedPathMode,
AssetPlugin, AssetServer, Assets, LoadBatchRequest, LoadState, UnapprovedPathMode,
};
use alloc::{
boxed::Box,
Expand Down Expand Up @@ -1574,7 +1574,7 @@ mod tests {
}

#[test]
fn load_folder() {
fn load_batch() {
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
#[cfg(not(feature = "multi_threaded"))]
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
Expand Down Expand Up @@ -1618,29 +1618,30 @@ mod tests {
.init_asset::<SubText>()
.register_asset_loader(CoolTextLoader);
let asset_server = app.world().resource::<AssetServer>().clone();
let handle: Handle<LoadedFolder> = asset_server.load_folder("text");
let handle: Handle<LoadedBatch> =
asset_server.load_batch(LoadBatchRequest::new(vec!["text/*", "b.cool.ron"]));
gate_opener.open(a_path);
gate_opener.open(b_path);
gate_opener.open(c_path);

let mut reader = EventCursor::default();
run_app_until(&mut app, |world| {
let events = world.resource::<Events<AssetEvent<LoadedFolder>>>();
let events = world.resource::<Events<AssetEvent<LoadedBatch>>>();
let asset_server = world.resource::<AssetServer>();
let loaded_folders = world.resource::<Assets<LoadedFolder>>();
let loaded_batchs = world.resource::<Assets<LoadedBatch>>();
let cool_texts = world.resource::<Assets<CoolText>>();
for event in reader.read(events) {
if let AssetEvent::LoadedWithDependencies { id } = event {
if *id == handle.id() {
let loaded_folder = loaded_folders.get(&handle).unwrap();
let loaded_batch = loaded_batchs.get(&handle).unwrap();
let a_handle: Handle<CoolText> =
asset_server.get_handle("text/a.cool.ron").unwrap();
let c_handle: Handle<CoolText> =
asset_server.get_handle("text/c.cool.ron").unwrap();

let mut found_a = false;
let mut found_c = false;
for asset_handle in &loaded_folder.handles {
for asset_handle in &loaded_batch.handles {
if asset_handle.id() == a_handle.id().untyped() {
found_a = true;
} else if asset_handle.id() == c_handle.id().untyped() {
Expand All @@ -1649,7 +1650,7 @@ mod tests {
}
assert!(found_a);
assert!(found_c);
assert_eq!(loaded_folder.handles.len(), 2);
assert_eq!(loaded_batch.handles.len(), 2);

let a_text = cool_texts.get(&a_handle).unwrap();
let b_text = cool_texts.get(&a_text.dependencies[0]).unwrap();
Expand Down
Loading
Loading