Skip to content

Commit b8059fa

Browse files
committed
Provide a from_builder and from_paths method for creating DefaultAssetSource.
1 parent efa0bcb commit b8059fa

File tree

9 files changed

+75
-63
lines changed

9 files changed

+75
-63
lines changed

crates/bevy_asset/src/io/file/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl FileAssetReader {
5252
/// Returns the base path of the assets directory, which is normally the executable's parent
5353
/// directory.
5454
///
55-
/// To change this, set [`DefaultAssetSource::FromPaths::file_path`][crate::DefaultAssetSource::FromPaths::file_path].
55+
/// To change this, set [`DefaultAssetSource::Paths::file_path`][crate::DefaultAssetSource::Paths::file_path].
5656
pub fn get_base_path() -> PathBuf {
5757
get_base_path()
5858
}

crates/bevy_asset/src/lib.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub enum AssetMetaCheck {
335335
/// Type to define how to create the default asset source.
336336
pub enum DefaultAssetSource {
337337
/// Create the default asset source given these file paths.
338-
FromPaths {
338+
Paths {
339339
/// The path to the unprocessed assets.
340340
file_path: String,
341341
/// The path to the processed assets.
@@ -347,14 +347,33 @@ pub enum DefaultAssetSource {
347347
///
348348
/// Note: The Mutex is just an implementation detail for applying the
349349
/// plugin.
350-
FromBuilder(Mutex<AssetSourceBuilder>),
350+
Builder(Mutex<AssetSourceBuilder>),
351+
}
352+
353+
impl DefaultAssetSource {
354+
/// Creates an instance that will build the default source for the platform given the file
355+
/// paths.
356+
///
357+
/// If `processed_file_path` is [`None`], the default file path is used when in
358+
/// [`AssetMode::Processed`].
359+
pub fn from_paths(file_path: String, processed_file_path: Option<String>) -> Self {
360+
Self::Paths {
361+
file_path,
362+
processed_file_path,
363+
}
364+
}
365+
366+
/// Creates an instance that will build the source from the provided builder.
367+
pub fn from_builder(builder: AssetSourceBuilder) -> Self {
368+
Self::Builder(Mutex::new(builder))
369+
}
351370
}
352371

353372
impl Default for AssetPlugin {
354373
fn default() -> Self {
355374
Self {
356375
mode: AssetMode::Unprocessed,
357-
default_source: DefaultAssetSource::FromPaths {
376+
default_source: DefaultAssetSource::Paths {
358377
file_path: Self::DEFAULT_UNPROCESSED_FILE_PATH.to_string(),
359378
processed_file_path: None,
360379
},
@@ -379,7 +398,7 @@ impl Plugin for AssetPlugin {
379398
let mut default_source_builder;
380399
let default_source_builder_ref;
381400
match &self.default_source {
382-
DefaultAssetSource::FromPaths {
401+
DefaultAssetSource::Paths {
383402
file_path,
384403
processed_file_path,
385404
} => {
@@ -393,7 +412,7 @@ impl Plugin for AssetPlugin {
393412
AssetSourceBuilder::platform_default(file_path, processed_file_path);
394413
default_source_builder_ref = &mut default_source_builder;
395414
}
396-
DefaultAssetSource::FromBuilder(builder) => {
415+
DefaultAssetSource::Builder(builder) => {
397416
lock = builder.lock().unwrap_or_else(PoisonError::into_inner);
398417
default_source_builder_ref = &mut *lock;
399418
}
@@ -946,8 +965,8 @@ mod tests {
946965
app.add_plugins((
947966
TaskPoolPlugin::default(),
948967
AssetPlugin {
949-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
950-
AssetSourceBuilder::new(move || Box::new(gated_memory_reader.clone())),
968+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
969+
move || Box::new(gated_memory_reader.clone()),
951970
)),
952971
..Default::default()
953972
},
@@ -1956,10 +1975,8 @@ mod tests {
19561975
app.add_plugins((
19571976
TaskPoolPlugin::default(),
19581977
AssetPlugin {
1959-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
1960-
AssetSourceBuilder::new(move || {
1961-
Box::new(MemoryAssetReader { root: dir.clone() })
1962-
}),
1978+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
1979+
move || Box::new(MemoryAssetReader { root: dir.clone() }),
19631980
)),
19641981
..Default::default()
19651982
},
@@ -2085,8 +2102,8 @@ mod tests {
20852102
app.add_plugins((
20862103
TaskPoolPlugin::default(),
20872104
AssetPlugin {
2088-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2089-
AssetSourceBuilder::new(move || Box::new(memory_reader.clone())),
2105+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
2106+
move || Box::new(memory_reader.clone()),
20902107
)),
20912108
unapproved_path_mode: mode,
20922109
..Default::default()
@@ -2222,8 +2239,8 @@ mod tests {
22222239
app.add_plugins((
22232240
TaskPoolPlugin::default(),
22242241
AssetPlugin {
2225-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2226-
AssetSourceBuilder::new(move || Box::new(reader.clone())),
2242+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
2243+
move || Box::new(reader.clone()),
22272244
)),
22282245
..Default::default()
22292246
},
@@ -2282,8 +2299,8 @@ mod tests {
22822299
app.add_plugins((
22832300
TaskPoolPlugin::default(),
22842301
AssetPlugin {
2285-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2286-
AssetSourceBuilder::new(move || Box::new(reader.clone())),
2302+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
2303+
move || Box::new(reader.clone()),
22872304
)),
22882305
..Default::default()
22892306
},
@@ -2352,14 +2369,14 @@ mod tests {
23522369
app.add_plugins((
23532370
TaskPoolPlugin::default(),
23542371
AssetPlugin {
2355-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2372+
default_source: DefaultAssetSource::from_builder(
23562373
AssetSourceBuilder::new(move || Box::new(memory_reader.clone())).with_watcher(
23572374
move |sender| {
23582375
sender_sender.send(sender).unwrap();
23592376
Some(Box::new(FakeWatcher))
23602377
},
23612378
),
2362-
)),
2379+
),
23632380
watch_for_changes_override: Some(true),
23642381
..Default::default()
23652382
},
@@ -2548,7 +2565,7 @@ mod tests {
25482565
app.add_plugins((
25492566
TaskPoolPlugin::default(),
25502567
AssetPlugin {
2551-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(asset_source)),
2568+
default_source: DefaultAssetSource::from_builder(asset_source),
25522569
..Default::default()
25532570
},
25542571
))
@@ -2613,7 +2630,7 @@ mod tests {
26132630
app.add_plugins((
26142631
TaskPoolPlugin::default(),
26152632
AssetPlugin {
2616-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(asset_source)),
2633+
default_source: DefaultAssetSource::from_builder(asset_source),
26172634
..Default::default()
26182635
},
26192636
))
@@ -2692,7 +2709,7 @@ mod tests {
26922709
app.add_plugins((
26932710
TaskPoolPlugin::default(),
26942711
AssetPlugin {
2695-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(asset_source)),
2712+
default_source: DefaultAssetSource::from_builder(asset_source),
26962713
..Default::default()
26972714
},
26982715
))
@@ -2762,7 +2779,7 @@ mod tests {
27622779
app.add_plugins((
27632780
TaskPoolPlugin::default(),
27642781
AssetPlugin {
2765-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(default_source)),
2782+
default_source: DefaultAssetSource::from_builder(default_source),
27662783
..Default::default()
27672784
},
27682785
DiagnosticsPlugin,

crates/bevy_asset/src/processor/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn create_app_with_asset_processor() -> AppWithProcessor {
181181
app.add_plugins((
182182
TaskPoolPlugin::default(),
183183
AssetPlugin {
184-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(default_source_builder)),
184+
default_source: DefaultAssetSource::from_builder(default_source_builder),
185185
mode: AssetMode::Processed,
186186
use_asset_processor_override: Some(true),
187187
watch_for_changes_override: Some(true),
@@ -1438,13 +1438,13 @@ fn only_reprocesses_wrong_hash_on_startup() {
14381438
app.add_plugins((
14391439
TaskPoolPlugin::default(),
14401440
AssetPlugin {
1441-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
1441+
default_source: DefaultAssetSource::from_builder(
14421442
AssetSourceBuilder::new(move || Box::new(source_memory_reader.clone()))
14431443
.with_processed_reader(move || Box::new(processed_memory_reader.clone()))
14441444
.with_processed_writer(move |_| {
14451445
Some(Box::new(processed_memory_writer.clone()))
14461446
}),
1447-
)),
1447+
),
14481448
mode: AssetMode::Processed,
14491449
use_asset_processor_override: Some(true),
14501450
..Default::default()

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,8 +1928,8 @@ mod test {
19281928
LogPlugin::default(),
19291929
TaskPoolPlugin::default(),
19301930
AssetPlugin {
1931-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
1932-
AssetSourceBuilder::new(move || Box::new(reader.clone())),
1931+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
1932+
move || Box::new(reader.clone()),
19331933
)),
19341934
..Default::default()
19351935
},
@@ -2347,12 +2347,12 @@ mod test {
23472347
LogPlugin::default(),
23482348
TaskPoolPlugin::default(),
23492349
AssetPlugin {
2350-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
2351-
AssetSourceBuilder::new(move || {
2350+
default_source: DefaultAssetSource::from_builder(AssetSourceBuilder::new(
2351+
move || {
23522352
Box::new(MemoryAssetReader {
23532353
root: Dir::default(),
23542354
})
2355-
}),
2355+
},
23562356
)),
23572357
..Default::default()
23582358
},

examples/asset/asset_settings.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ fn main() {
1111
.add_plugins(
1212
// This just tells the asset server to look in the right examples folder
1313
DefaultPlugins.set(AssetPlugin {
14-
default_source: DefaultAssetSource::FromPaths {
15-
file_path: "examples/asset/files".to_string(),
16-
processed_file_path: None,
17-
},
14+
default_source: DefaultAssetSource::from_paths(
15+
"examples/asset/files".to_string(),
16+
None,
17+
),
1818
..Default::default()
1919
}),
2020
)

examples/asset/custom_asset_reader.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy::{
1212
},
1313
prelude::*,
1414
};
15-
use std::{path::Path, sync::Mutex};
15+
use std::path::Path;
1616

1717
/// A custom asset reader implementation that wraps a given asset reader implementation
1818
struct CustomAssetReader(Box<dyn ErasedAssetReader>);
@@ -42,14 +42,12 @@ fn main() {
4242
App::new()
4343
.add_plugins(DefaultPlugins.set(AssetPlugin {
4444
// 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-
))),
45+
default_source: DefaultAssetSource::from_builder(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+
})),
5351
..Default::default()
5452
}))
5553
.add_systems(Startup, setup)

examples/asset/processing/asset_processing.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ 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-
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-
},
34+
default_source: DefaultAssetSource::from_paths(
35+
"examples/asset/processing/assets".to_string(),
36+
Some("examples/asset/processing/imported_assets/Default".to_string()),
37+
),
4038
..default()
4139
}),
4240
TextPlugin,

examples/tools/scene_viewer/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ fn main() {
8686
..default()
8787
})
8888
.set(AssetPlugin {
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-
},
89+
default_source: DefaultAssetSource::from_paths(
90+
std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string()),
91+
None,
92+
),
9493
// Allow scenes to be loaded from anywhere on disk
9594
unapproved_path_mode: UnapprovedPathMode::Allow,
9695
..default()

release-content/migration-guides/registering_asset_sources.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ Now, this is written as:
5555
App::new()
5656
.add_plugins(DefaultPlugins.set(
5757
AssetPlugin {
58-
default_source: DefaultAssetSource::FromPaths {
59-
file_path: "some/path".to_string(),
60-
// Note: Setting this to None will just use the default path.
61-
processed_file_path: Some("some/processed_path".to_string()),
62-
},
58+
default_source: DefaultAssetSource::from_paths(
59+
"some/path".to_string(),
60+
// Note: Setting this to None will just use the default processed path.
61+
Some("some/processed_path".to_string()),
62+
),
6363
..Default::default()
6464
}
6565
));
@@ -86,9 +86,9 @@ Now, this is written as:
8686
App::new()
8787
.add_plugins(DefaultPlugins.set(
8888
AssetPlugin {
89-
default_source: DefaultAssetSource::FromBuilder(Mutex::new(
89+
default_source: DefaultAssetSource::from_builder(
9090
AssetSourceBuilder::new(move || Box::new(todo!()))
91-
)),
91+
),
9292
..Default::default()
9393
}
9494
));

0 commit comments

Comments
 (0)