Skip to content

Commit 17e33b9

Browse files
committed
Arc AssetSources and share them between the processor, processor's asset server, and the regular asset server.
1 parent 3dc6482 commit 17e33b9

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

crates/bevy_asset/src/io/source.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ impl AssetSource {
431431
.ok_or_else(|| MissingProcessedAssetReaderError(self.id.clone_owned()))
432432
}
433433

434+
/// Return's this source's ungated processed [`AssetReader`](crate::io::AssetReader), if it
435+
/// exists.
436+
#[inline]
437+
pub(crate) fn ungated_processed_reader(&self) -> Option<&dyn ErasedAssetReader> {
438+
self.ungated_processed_reader.as_deref()
439+
}
440+
434441
/// Return's this source's processed [`AssetWriter`](crate::io::AssetWriter), if it exists.
435442
#[inline]
436443
pub fn processed_writer(

crates/bevy_asset/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl Plugin for AssetPlugin {
375375
let sources = builders.build_sources(watch, false);
376376

377377
app.insert_resource(AssetServer::new_with_meta_check(
378-
sources,
378+
Arc::new(sources),
379379
AssetServerMode::Unprocessed,
380380
self.meta_check.clone(),
381381
watch,
@@ -388,9 +388,7 @@ impl Plugin for AssetPlugin {
388388
.unwrap_or(cfg!(feature = "asset_processor"));
389389
if use_asset_processor {
390390
let mut builders = app.world_mut().resource_mut::<AssetSourceBuilders>();
391-
let processor = AssetProcessor::new(&mut builders);
392-
let mut sources = builders.build_sources(false, watch);
393-
sources.gate_on_processor(processor.data.processing_state.clone());
391+
let (processor, sources) = AssetProcessor::new(&mut builders, watch);
394392
// the main asset server shares loaders with the processor asset server
395393
app.insert_resource(AssetServer::new_with_loaders(
396394
sources,
@@ -406,7 +404,7 @@ impl Plugin for AssetPlugin {
406404
let mut builders = app.world_mut().resource_mut::<AssetSourceBuilders>();
407405
let sources = builders.build_sources(false, watch);
408406
app.insert_resource(AssetServer::new_with_meta_check(
409-
sources,
407+
Arc::new(sources),
410408
AssetServerMode::Processed,
411409
AssetMetaCheck::Always,
412410
watch,

crates/bevy_asset/src/processor/mod.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub struct AssetProcessorData {
116116
processors: RwLock<HashMap<&'static str, Arc<dyn ErasedProcessor>>>,
117117
/// Default processors for file extensions
118118
default_processors: RwLock<HashMap<Box<str>, &'static str>>,
119-
sources: AssetSources,
119+
sources: Arc<AssetSources>,
120120
}
121121

122122
/// The current state of processing, including the overall state and the state of all assets.
@@ -135,19 +135,25 @@ pub(crate) struct ProcessingState {
135135

136136
impl AssetProcessor {
137137
/// Creates a new [`AssetProcessor`] instance.
138-
pub fn new(source: &mut AssetSourceBuilders) -> Self {
139-
let data = Arc::new(AssetProcessorData::new(source.build_sources(true, false)));
138+
pub fn new(
139+
sources: &mut AssetSourceBuilders,
140+
watch_processed: bool,
141+
) -> (Self, Arc<AssetSources>) {
142+
let state = Arc::new(ProcessingState::new());
143+
let mut sources = sources.build_sources(true, watch_processed);
144+
sources.gate_on_processor(state.clone());
145+
let sources = Arc::new(sources);
146+
147+
let data = Arc::new(AssetProcessorData::new(sources.clone(), state));
140148
// The asset processor uses its own asset server with its own id space
141-
let mut sources = source.build_sources(false, false);
142-
sources.gate_on_processor(data.processing_state.clone());
143149
let server = AssetServer::new_with_meta_check(
144-
sources,
150+
sources.clone(),
145151
AssetServerMode::Processed,
146152
AssetMetaCheck::Always,
147153
false,
148154
UnapprovedPathMode::default(),
149155
);
150-
Self { server, data }
156+
(Self { server, data }, sources)
151157
}
152158

153159
/// Gets a reference to the [`Arc`] containing the [`AssetProcessorData`].
@@ -513,7 +519,7 @@ impl AssetProcessor {
513519
}
514520
}
515521
AssetSourceEvent::RemovedUnknown { path, is_meta } => {
516-
let processed_reader = source.processed_reader().unwrap();
522+
let processed_reader = source.ungated_processed_reader().unwrap();
517523
match processed_reader.is_directory(&path).await {
518524
Ok(is_directory) => {
519525
if is_directory {
@@ -590,7 +596,7 @@ impl AssetProcessor {
590596
"Removing folder {} because source was removed",
591597
path.display()
592598
);
593-
let processed_reader = source.processed_reader().unwrap();
599+
let processed_reader = source.ungated_processed_reader().unwrap();
594600
match processed_reader.read_directory(path).await {
595601
Ok(mut path_stream) => {
596602
while let Some(child_path) = path_stream.next().await {
@@ -787,7 +793,7 @@ impl AssetProcessor {
787793
}
788794

789795
for source in self.sources().iter_processed() {
790-
let Ok(processed_reader) = source.processed_reader() else {
796+
let Some(processed_reader) = source.ungated_processed_reader() else {
791797
continue;
792798
};
793799
let Ok(processed_writer) = source.processed_writer() else {
@@ -1210,10 +1216,10 @@ impl AssetProcessor {
12101216

12111217
impl AssetProcessorData {
12121218
/// Initializes a new [`AssetProcessorData`] using the given [`AssetSources`].
1213-
pub fn new(source: AssetSources) -> Self {
1219+
pub(crate) fn new(sources: Arc<AssetSources>, processing_state: Arc<ProcessingState>) -> Self {
12141220
AssetProcessorData {
1215-
processing_state: Arc::new(ProcessingState::new()),
1216-
sources: source,
1221+
processing_state,
1222+
sources,
12171223
log_factory: Mutex::new(Some(Box::new(FileTransactionLogFactory::default()))),
12181224
log: Default::default(),
12191225
processors: Default::default(),

crates/bevy_asset/src/server/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) struct AssetServerData {
6969
pub(crate) loaders: Arc<RwLock<AssetLoaders>>,
7070
asset_event_sender: Sender<InternalAssetEvent>,
7171
asset_event_receiver: Receiver<InternalAssetEvent>,
72-
sources: AssetSources,
72+
sources: Arc<AssetSources>,
7373
mode: AssetServerMode,
7474
meta_check: AssetMetaCheck,
7575
unapproved_path_mode: UnapprovedPathMode,
@@ -91,7 +91,7 @@ impl AssetServer {
9191
/// Create a new instance of [`AssetServer`]. If `watch_for_changes` is true, the [`AssetReader`](crate::io::AssetReader) storage will watch for changes to
9292
/// asset sources and hot-reload them.
9393
pub fn new(
94-
sources: AssetSources,
94+
sources: Arc<AssetSources>,
9595
mode: AssetServerMode,
9696
watching_for_changes: bool,
9797
unapproved_path_mode: UnapprovedPathMode,
@@ -109,7 +109,7 @@ impl AssetServer {
109109
/// Create a new instance of [`AssetServer`]. If `watch_for_changes` is true, the [`AssetReader`](crate::io::AssetReader) storage will watch for changes to
110110
/// asset sources and hot-reload them.
111111
pub fn new_with_meta_check(
112-
sources: AssetSources,
112+
sources: Arc<AssetSources>,
113113
mode: AssetServerMode,
114114
meta_check: AssetMetaCheck,
115115
watching_for_changes: bool,
@@ -126,7 +126,7 @@ impl AssetServer {
126126
}
127127

128128
pub(crate) fn new_with_loaders(
129-
sources: AssetSources,
129+
sources: Arc<AssetSources>,
130130
loaders: Arc<RwLock<AssetLoaders>>,
131131
mode: AssetServerMode,
132132
meta_check: AssetMetaCheck,

0 commit comments

Comments
 (0)