Skip to content

Commit 55ed39a

Browse files
committed
Send the nested assets.
1 parent 1d0d29c commit 55ed39a

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

crates/bevy_asset/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,20 @@ mod tests {
10071007
// Re-open a and b gates to allow c to load embedded deps (gates are closed after each load)
10081008
gate_opener.open(a_path);
10091009
gate_opener.open(b_path);
1010+
1011+
// Wait for the C-load task to finish. If we don't do this, it's possible for nested asset A
1012+
// to be sent in one frame and for the nested asset B to be sent in the next frame. This
1013+
// causes an alternative order of events (but one that is just as valid), which would make
1014+
// this test flaky.
1015+
while !asset_server
1016+
.data
1017+
.infos
1018+
.read()
1019+
.pending_tasks
1020+
.iter()
1021+
.any(|(_, task)| task.is_finished())
1022+
{}
1023+
10101024
run_app_until(&mut app, |world| {
10111025
let a_text = get::<CoolText>(world, a_id)?;
10121026
let (a_load, a_deps, a_rec_deps) = asset_server.get_load_states(a_id).unwrap();
@@ -1131,9 +1145,19 @@ mod tests {
11311145
AssetEvent::Added {
11321146
id: id_results.b_id,
11331147
},
1148+
// This extra LoadedWithDependencies happens because asset B got replaced when C was
1149+
// loaded (and we only see the modify after since events on `Assets` only merge in a
1150+
// system, so they look like they happen at the wrong time).
1151+
AssetEvent::LoadedWithDependencies {
1152+
id: id_results.b_id,
1153+
},
11341154
AssetEvent::Added {
11351155
id: id_results.c_id,
11361156
},
1157+
AssetEvent::Modified { id: a_id },
1158+
AssetEvent::Modified {
1159+
id: id_results.b_id,
1160+
},
11371161
AssetEvent::LoadedWithDependencies {
11381162
id: id_results.d_id,
11391163
},

crates/bevy_asset/src/server/mod.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl AssetServer {
651651
};
652652

653653
let nested_direct_loaded_assets = RwLock::new(NestedAssets::default());
654-
match self
654+
let result = match self
655655
.load_with_meta_loader_and_reader(
656656
&base_path,
657657
meta.as_ref(),
@@ -686,7 +686,7 @@ impl AssetServer {
686686
handle.unwrap()
687687
};
688688

689-
self.send_loaded_asset(base_handle.id(), loaded_asset);
689+
self.send_loaded_asset(Some(base_handle.id()), loaded_asset);
690690
Ok(final_handle)
691691
}
692692
Err(err) => {
@@ -697,23 +697,44 @@ impl AssetServer {
697697
});
698698
Err(err)
699699
}
700+
};
701+
702+
// Even if the asset failed to load, the nested assets still loaded correctly, so we might
703+
// as well send those assets to "refresh" them.
704+
for (path, asset) in nested_direct_loaded_assets.into_inner().0 {
705+
// Even if the handle is None, one of its subassets may be loaded, so we should send the
706+
// whole complete asset.
707+
let handle = self
708+
.data
709+
.infos
710+
.read()
711+
.get_path_and_type_id_handle(&path, asset.asset.asset_type_id());
712+
self.send_loaded_asset(handle.map(|handle| handle.id()), asset);
700713
}
714+
715+
result
701716
}
702717

703718
/// Sends a load event for the given `loaded_asset` and does the same recursively for all
704719
/// labeled assets.
705-
fn send_loaded_asset(&self, id: UntypedAssetId, mut complete_asset: CompleteErasedLoadedAsset) {
720+
fn send_loaded_asset(
721+
&self,
722+
id: Option<UntypedAssetId>,
723+
mut complete_asset: CompleteErasedLoadedAsset,
724+
) {
706725
for (_, labeled_asset) in complete_asset.labeled_assets.drain() {
707726
self.send_asset_event(InternalAssetEvent::Loaded {
708727
id: labeled_asset.handle.id(),
709728
loaded_asset: labeled_asset.asset,
710729
});
711730
}
712731

713-
self.send_asset_event(InternalAssetEvent::Loaded {
714-
id,
715-
loaded_asset: complete_asset.asset,
716-
});
732+
if let Some(id) = id {
733+
self.send_asset_event(InternalAssetEvent::Loaded {
734+
id,
735+
loaded_asset: complete_asset.asset,
736+
});
737+
}
717738
}
718739

719740
/// Kicks off a reload of the asset stored at the given path. This will only reload the asset if it currently loaded.

0 commit comments

Comments
 (0)