Skip to content

Commit 0203aca

Browse files
committed
fix: reload of assets
This makes sure asset content is only removed when none of the handles are using them. And replaces the handle to CID entry instead of removing it, which leaves a data gap that can cause occasional errors.
1 parent 59c713c commit 0203aca

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

framework_crates/bones_asset/src/server.rs

Lines changed: 23 additions & 21 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

0 commit comments

Comments
 (0)