Skip to content

Commit 5e8489c

Browse files
committed
Fix up all the dependents and examples to add the default source in the AssetPlugin.
1 parent 526302e commit 5e8489c

File tree

6 files changed

+67
-59
lines changed

6 files changed

+67
-59
lines changed

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,16 +1901,17 @@ struct MorphTargetNames {
19011901

19021902
#[cfg(test)]
19031903
mod test {
1904-
use std::path::Path;
1904+
use std::{path::Path, sync::Mutex};
19051905

19061906
use crate::{Gltf, GltfAssetLabel, GltfNode, GltfSkin};
19071907
use bevy_app::{App, TaskPoolPlugin};
19081908
use bevy_asset::{
19091909
io::{
19101910
memory::{Dir, MemoryAssetReader},
1911-
AssetSourceBuilder, AssetSourceId,
1911+
AssetSourceBuilder,
19121912
},
1913-
AssetApp, AssetLoader, AssetPlugin, AssetServer, Assets, Handle, LoadState,
1913+
AssetApp, AssetLoader, AssetPlugin, AssetServer, Assets, DefaultAssetSource, Handle,
1914+
LoadState,
19141915
};
19151916
use bevy_ecs::{resource::Resource, world::World};
19161917
use bevy_image::{Image, ImageLoaderSettings};
@@ -1923,14 +1924,15 @@ mod test {
19231924
fn test_app(dir: Dir) -> App {
19241925
let mut app = App::new();
19251926
let reader = MemoryAssetReader { root: dir };
1926-
app.register_asset_source(
1927-
AssetSourceId::Default,
1928-
AssetSourceBuilder::new(move || Box::new(reader.clone())),
1929-
)
1930-
.add_plugins((
1927+
app.add_plugins((
19311928
LogPlugin::default(),
19321929
TaskPoolPlugin::default(),
1933-
AssetPlugin::default(),
1930+
AssetPlugin {
1931+
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
1932+
AssetSourceBuilder::new(move || Box::new(reader.clone())),
1933+
)),
1934+
..Default::default()
1935+
},
19341936
ScenePlugin,
19351937
MeshPlugin,
19361938
crate::GltfPlugin::default(),
@@ -2341,27 +2343,27 @@ mod test {
23412343

23422344
let mut app = App::new();
23432345
let custom_reader = MemoryAssetReader { root: dir.clone() };
2344-
// Create a default asset source so we definitely don't try to read from disk.
2345-
app.register_asset_source(
2346-
AssetSourceId::Default,
2347-
AssetSourceBuilder::new(move || {
2348-
Box::new(MemoryAssetReader {
2349-
root: Dir::default(),
2350-
})
2351-
}),
2352-
)
2353-
.register_asset_source(
2354-
"custom",
2355-
AssetSourceBuilder::new(move || Box::new(custom_reader.clone())),
2356-
)
2357-
.add_plugins((
2346+
app.add_plugins((
23582347
LogPlugin::default(),
23592348
TaskPoolPlugin::default(),
2360-
AssetPlugin::default(),
2349+
AssetPlugin {
2350+
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2351+
AssetSourceBuilder::new(move || {
2352+
Box::new(MemoryAssetReader {
2353+
root: Dir::default(),
2354+
})
2355+
}),
2356+
)),
2357+
..Default::default()
2358+
},
23612359
ScenePlugin,
23622360
MeshPlugin,
23632361
crate::GltfPlugin::default(),
2364-
));
2362+
))
2363+
.register_asset_source(
2364+
"custom",
2365+
AssetSourceBuilder::new(move || Box::new(custom_reader.clone())),
2366+
);
23652367

23662368
app.finish();
23672369
app.cleanup();

examples/asset/asset_settings.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! This example demonstrates the usage of '.meta' files and [`AssetServer::load_with_settings`] to override the default settings for loading an asset
22
33
use bevy::{
4+
asset::DefaultAssetSource,
45
image::{ImageLoaderSettings, ImageSampler},
56
prelude::*,
67
};
@@ -10,7 +11,10 @@ fn main() {
1011
.add_plugins(
1112
// This just tells the asset server to look in the right examples folder
1213
DefaultPlugins.set(AssetPlugin {
13-
file_path: "examples/asset/files".to_string(),
14+
default_source: DefaultAssetSource::FromPaths {
15+
file_path: "examples/asset/files".to_string(),
16+
processed_file_path: None,
17+
},
1418
..Default::default()
1519
}),
1620
)

examples/asset/custom_asset_reader.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
44
55
use bevy::{
6-
asset::io::{
7-
AssetReader, AssetReaderError, AssetSource, AssetSourceBuilder, AssetSourceId,
8-
ErasedAssetReader, PathStream, Reader,
6+
asset::{
7+
io::{
8+
AssetReader, AssetReaderError, AssetSource, AssetSourceBuilder, ErasedAssetReader,
9+
PathStream, Reader,
10+
},
11+
DefaultAssetSource,
912
},
1013
prelude::*,
1114
};
12-
use std::path::Path;
15+
use std::{path::Path, sync::Mutex};
1316

1417
/// A custom asset reader implementation that wraps a given asset reader implementation
1518
struct CustomAssetReader(Box<dyn ErasedAssetReader>);
@@ -35,26 +38,20 @@ impl AssetReader for CustomAssetReader {
3538
}
3639
}
3740

38-
/// A plugins that registers our new asset reader
39-
struct CustomAssetReaderPlugin;
40-
41-
impl Plugin for CustomAssetReaderPlugin {
42-
fn build(&self, app: &mut App) {
43-
app.register_asset_source(
44-
AssetSourceId::Default,
45-
AssetSourceBuilder::new(|| {
46-
Box::new(CustomAssetReader(
47-
// This is the default reader for the current platform
48-
AssetSource::get_default_reader("assets".to_string())(),
49-
))
50-
}),
51-
);
52-
}
53-
}
54-
5541
fn main() {
5642
App::new()
57-
.add_plugins((CustomAssetReaderPlugin, DefaultPlugins))
43+
.add_plugins(DefaultPlugins.set(AssetPlugin {
44+
// The default source must be registered in the `AssetPlugin`.
45+
default_source: DefaultAssetSource::FromBuilder(Mutex::new(AssetSourceBuilder::new(
46+
|| {
47+
Box::new(CustomAssetReader(
48+
// This is the default reader for the current platform
49+
AssetSource::get_default_reader("assets".to_string())(),
50+
))
51+
},
52+
))),
53+
..Default::default()
54+
}))
5855
.add_systems(Startup, setup)
5956
.run();
6057
}

examples/asset/extra_source.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ use std::path::Path;
1212

1313
fn main() {
1414
App::new()
15+
// DefaultPlugins contains AssetPlugin so it must be added to our App
16+
// before inserting our new asset source.
17+
.add_plugins(DefaultPlugins)
1518
// Add an extra asset source with the name "example_files" to
1619
// AssetSourceBuilders.
17-
//
18-
// This must be done before AssetPlugin finalizes building assets.
1920
.register_asset_source(
2021
"example_files",
2122
AssetSourceBuilder::platform_default("examples/asset/files", None),
2223
)
23-
// DefaultPlugins contains AssetPlugin so it must be added to our App
24-
// after inserting our new asset source.
25-
.add_plugins(DefaultPlugins)
2624
.add_systems(Startup, setup)
2725
.run();
2826
}

examples/asset/processing/asset_processing.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy::{
77
processor::LoadTransformAndSave,
88
saver::{AssetSaver, SavedAsset},
99
transformer::{AssetTransformer, TransformedAsset},
10-
AssetLoader, AsyncWriteExt, LoadContext,
10+
AssetLoader, AsyncWriteExt, DefaultAssetSource, LoadContext,
1111
},
1212
prelude::*,
1313
reflect::TypePath,
@@ -31,9 +31,12 @@ fn main() {
3131
mode: AssetMode::Processed,
3232
// This is just overriding the default paths to scope this to the correct example folder
3333
// You can generally skip this in your own projects
34-
file_path: "examples/asset/processing/assets".to_string(),
35-
processed_file_path: "examples/asset/processing/imported_assets/Default"
36-
.to_string(),
34+
default_source: DefaultAssetSource::FromPaths {
35+
file_path: "examples/asset/processing/assets".to_string(),
36+
processed_file_path: Some(
37+
"examples/asset/processing/imported_assets/Default".to_string(),
38+
),
39+
},
3740
..default()
3841
}),
3942
TextPlugin,

examples/tools/scene_viewer/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use argh::FromArgs;
1212
use bevy::{
13-
asset::UnapprovedPathMode,
13+
asset::{DefaultAssetSource, UnapprovedPathMode},
1414
camera::primitives::{Aabb, Sphere},
1515
camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
1616
core_pipeline::prepass::{DeferredPrepass, DepthPrepass},
@@ -86,7 +86,11 @@ fn main() {
8686
..default()
8787
})
8888
.set(AssetPlugin {
89-
file_path: std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string()),
89+
default_source: DefaultAssetSource::FromPaths {
90+
file_path: std::env::var("CARGO_MANIFEST_DIR")
91+
.unwrap_or_else(|_| ".".to_string()),
92+
processed_file_path: None,
93+
},
9094
// Allow scenes to be loaded from anywhere on disk
9195
unapproved_path_mode: UnapprovedPathMode::Allow,
9296
..default()

0 commit comments

Comments
 (0)