Skip to content

Commit 096c3b4

Browse files
zicklagTekhnaeRaav
andauthored
fix: fix crashes during asset hot reload. (#498)
Co-authored-by: Tekhnae Raav <[email protected]>
1 parent d5d333f commit 096c3b4

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

framework_crates/bones_asset/src/server.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -511,27 +511,30 @@ impl AssetServer {
511511
dependencies: partial.dependencies,
512512
data: partial.data,
513513
};
514-
515514
// If there is already loaded asset data for this path
516-
if let Some((_, cid)) = server.store.asset_ids.remove(&handle) {
517-
// Remove the old asset data
518-
let (_, previous_asset) = server.store.assets.remove(&cid).unwrap();
519-
520-
// Remove the previous asset's reverse dependencies.
521-
//
522-
// aka. now that we are removing the old asset, none of the assets that the
523-
// old asset dependended on should have a reverse dependency record saying that
524-
// this asset depends on it.
525-
//
526-
// In other words, this asset is removed and doesn't depend on anything else
527-
// anymore.
528-
for dep in previous_asset.dependencies.iter() {
529-
server
530-
.store
531-
.reverse_dependencies
532-
.get_mut(dep)
533-
.unwrap()
534-
.remove(&handle);
515+
if let Some(cid) = server.store.asset_ids.insert(handle, partial.cid) {
516+
// If no other handles use this content
517+
if server.store.asset_ids.iter().all(|map| *map.value() != cid) {
518+
// Remove the old asset data
519+
tracing::debug!(?cid, "Removing asset content");
520+
let (_, previous_asset) = server.store.assets.remove(&cid).unwrap();
521+
522+
// Remove the previous asset's reverse dependencies.
523+
//
524+
// aka. now that we are removing the old asset, none of the assets that the
525+
// old asset dependended on should have a reverse dependency record saying that
526+
// this asset depends on it.
527+
//
528+
// In other words, this asset is removed and doesn't depend on anything else
529+
// anymore.
530+
for dep in previous_asset.dependencies.iter() {
531+
server
532+
.store
533+
.reverse_dependencies
534+
.get_mut(dep)
535+
.unwrap()
536+
.remove(&handle);
537+
}
535538
}
536539

537540
// If there are any assets that depended on this asset, they now need to be re-loaded.
@@ -555,7 +558,6 @@ impl AssetServer {
555558
.insert(handle);
556559
}
557560

558-
server.store.asset_ids.insert(handle, partial.cid);
559561
server.store.assets.insert(partial.cid, loaded_asset);
560562
server.load_progress.inc_loaded();
561563

@@ -830,11 +832,13 @@ impl AssetServer {
830832
///
831833
/// # Panics
832834
///
833-
/// Panics if the asset is not loaded or if the asset asset with the given handle doesn't have a
835+
/// Panics if the asset is not loaded or if the asset with the given handle doesn't have a
834836
/// schema matching `T`.
835837
#[track_caller]
836838
pub fn get<T: HasSchema>(&self, handle: Handle<T>) -> MappedMapRef<Cid, LoadedAsset, T> {
837-
self.try_get(handle).unwrap().unwrap()
839+
self.try_get(handle)
840+
.expect("asset not found (handle has no cid)")
841+
.expect("asset does not have matching schema for given type")
838842
}
839843

840844
/// Borrow a loaded asset.

0 commit comments

Comments
 (0)