bevy_asset/
assets.rs

1use crate::{
2    self as bevy_asset, Asset, AssetEvent, AssetHandleProvider, AssetId, AssetServer, Handle,
3    UntypedHandle,
4};
5use alloc::sync::Arc;
6use bevy_ecs::{
7    prelude::EventWriter,
8    system::{Res, ResMut, Resource},
9};
10use bevy_reflect::{Reflect, TypePath};
11use bevy_utils::HashMap;
12use core::{any::TypeId, iter::Enumerate, marker::PhantomData, sync::atomic::AtomicU32};
13use crossbeam_channel::{Receiver, Sender};
14use derive_more::derive::{Display, Error};
15use serde::{Deserialize, Serialize};
16use uuid::Uuid;
17
18/// A generational runtime-only identifier for a specific [`Asset`] stored in [`Assets`]. This is optimized for efficient runtime
19/// usage and is not suitable for identifying assets across app runs.
20#[derive(
21    Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Reflect, Serialize, Deserialize,
22)]
23pub struct AssetIndex {
24    pub(crate) generation: u32,
25    pub(crate) index: u32,
26}
27
28impl AssetIndex {
29    /// Convert the [`AssetIndex`] into an opaque blob of bits to transport it in circumstances where carrying a strongly typed index isn't possible.
30    ///
31    /// The result of this function should not be relied upon for anything except putting it back into [`AssetIndex::from_bits`] to recover the index.
32    pub fn to_bits(self) -> u64 {
33        let Self { generation, index } = self;
34        ((generation as u64) << 32) | index as u64
35    }
36    /// Convert an opaque `u64` acquired from [`AssetIndex::to_bits`] back into an [`AssetIndex`]. This should not be used with any inputs other than those
37    /// derived from [`AssetIndex::to_bits`], as there are no guarantees for what will happen with such inputs.
38    pub fn from_bits(bits: u64) -> Self {
39        let index = ((bits << 32) >> 32) as u32;
40        let generation = (bits >> 32) as u32;
41        Self { generation, index }
42    }
43}
44
45/// Allocates generational [`AssetIndex`] values and facilitates their reuse.
46pub(crate) struct AssetIndexAllocator {
47    /// A monotonically increasing index.
48    next_index: AtomicU32,
49    recycled_queue_sender: Sender<AssetIndex>,
50    /// This receives every recycled [`AssetIndex`]. It serves as a buffer/queue to store indices ready for reuse.
51    recycled_queue_receiver: Receiver<AssetIndex>,
52    recycled_sender: Sender<AssetIndex>,
53    recycled_receiver: Receiver<AssetIndex>,
54}
55
56impl Default for AssetIndexAllocator {
57    fn default() -> Self {
58        let (recycled_queue_sender, recycled_queue_receiver) = crossbeam_channel::unbounded();
59        let (recycled_sender, recycled_receiver) = crossbeam_channel::unbounded();
60        Self {
61            recycled_queue_sender,
62            recycled_queue_receiver,
63            recycled_sender,
64            recycled_receiver,
65            next_index: Default::default(),
66        }
67    }
68}
69
70impl AssetIndexAllocator {
71    /// Reserves a new [`AssetIndex`], either by reusing a recycled index (with an incremented generation), or by creating a new index
72    /// by incrementing the index counter for a given asset type `A`.
73    pub fn reserve(&self) -> AssetIndex {
74        if let Ok(mut recycled) = self.recycled_queue_receiver.try_recv() {
75            recycled.generation += 1;
76            self.recycled_sender.send(recycled).unwrap();
77            recycled
78        } else {
79            AssetIndex {
80                index: self
81                    .next_index
82                    .fetch_add(1, core::sync::atomic::Ordering::Relaxed),
83                generation: 0,
84            }
85        }
86    }
87
88    /// Queues the given `index` for reuse. This should only be done if the `index` is no longer being used.
89    pub fn recycle(&self, index: AssetIndex) {
90        self.recycled_queue_sender.send(index).unwrap();
91    }
92}
93
94/// A "loaded asset" containing the untyped handle for an asset stored in a given [`AssetPath`].
95///
96/// [`AssetPath`]: crate::AssetPath
97#[derive(Asset, TypePath)]
98pub struct LoadedUntypedAsset {
99    #[dependency]
100    pub handle: UntypedHandle,
101}
102
103// PERF: do we actually need this to be an enum? Can we just use an "invalid" generation instead
104#[derive(Default)]
105enum Entry<A: Asset> {
106    /// None is an indicator that this entry does not have live handles.
107    #[default]
108    None,
109    /// Some is an indicator that there is a live handle active for the entry at this [`AssetIndex`]
110    Some { value: Option<A>, generation: u32 },
111}
112
113/// Stores [`Asset`] values in a Vec-like storage identified by [`AssetIndex`].
114struct DenseAssetStorage<A: Asset> {
115    storage: Vec<Entry<A>>,
116    len: u32,
117    allocator: Arc<AssetIndexAllocator>,
118}
119
120impl<A: Asset> Default for DenseAssetStorage<A> {
121    fn default() -> Self {
122        Self {
123            len: 0,
124            storage: Default::default(),
125            allocator: Default::default(),
126        }
127    }
128}
129
130impl<A: Asset> DenseAssetStorage<A> {
131    // Returns the number of assets stored.
132    pub(crate) fn len(&self) -> usize {
133        self.len as usize
134    }
135
136    // Returns `true` if there are no assets stored.
137    pub(crate) fn is_empty(&self) -> bool {
138        self.len == 0
139    }
140
141    /// Insert the value at the given index. Returns true if a value already exists (and was replaced)
142    pub(crate) fn insert(
143        &mut self,
144        index: AssetIndex,
145        asset: A,
146    ) -> Result<bool, InvalidGenerationError> {
147        self.flush();
148        let entry = &mut self.storage[index.index as usize];
149        if let Entry::Some { value, generation } = entry {
150            if *generation == index.generation {
151                let exists = value.is_some();
152                if !exists {
153                    self.len += 1;
154                }
155                *value = Some(asset);
156                Ok(exists)
157            } else {
158                Err(InvalidGenerationError {
159                    index,
160                    current_generation: *generation,
161                })
162            }
163        } else {
164            unreachable!("entries should always be valid after a flush");
165        }
166    }
167
168    /// Removes the asset stored at the given `index` and returns it as [`Some`] (if the asset exists).
169    /// This will recycle the id and allow new entries to be inserted.
170    pub(crate) fn remove_dropped(&mut self, index: AssetIndex) -> Option<A> {
171        self.remove_internal(index, |dense_storage| {
172            dense_storage.storage[index.index as usize] = Entry::None;
173            dense_storage.allocator.recycle(index);
174        })
175    }
176
177    /// Removes the asset stored at the given `index` and returns it as [`Some`] (if the asset exists).
178    /// This will _not_ recycle the id. New values with the current ID can still be inserted. The ID will
179    /// not be reused until [`DenseAssetStorage::remove_dropped`] is called.
180    pub(crate) fn remove_still_alive(&mut self, index: AssetIndex) -> Option<A> {
181        self.remove_internal(index, |_| {})
182    }
183
184    fn remove_internal(
185        &mut self,
186        index: AssetIndex,
187        removed_action: impl FnOnce(&mut Self),
188    ) -> Option<A> {
189        self.flush();
190        let value = match &mut self.storage[index.index as usize] {
191            Entry::None => return None,
192            Entry::Some { value, generation } => {
193                if *generation == index.generation {
194                    value.take().inspect(|_| self.len -= 1)
195                } else {
196                    return None;
197                }
198            }
199        };
200        removed_action(self);
201        value
202    }
203
204    pub(crate) fn get(&self, index: AssetIndex) -> Option<&A> {
205        let entry = self.storage.get(index.index as usize)?;
206        match entry {
207            Entry::None => None,
208            Entry::Some { value, generation } => {
209                if *generation == index.generation {
210                    value.as_ref()
211                } else {
212                    None
213                }
214            }
215        }
216    }
217
218    pub(crate) fn get_mut(&mut self, index: AssetIndex) -> Option<&mut A> {
219        let entry = self.storage.get_mut(index.index as usize)?;
220        match entry {
221            Entry::None => None,
222            Entry::Some { value, generation } => {
223                if *generation == index.generation {
224                    value.as_mut()
225                } else {
226                    None
227                }
228            }
229        }
230    }
231
232    pub(crate) fn flush(&mut self) {
233        // NOTE: this assumes the allocator index is monotonically increasing.
234        let new_len = self
235            .allocator
236            .next_index
237            .load(core::sync::atomic::Ordering::Relaxed);
238        self.storage.resize_with(new_len as usize, || Entry::Some {
239            value: None,
240            generation: 0,
241        });
242        while let Ok(recycled) = self.allocator.recycled_receiver.try_recv() {
243            let entry = &mut self.storage[recycled.index as usize];
244            *entry = Entry::Some {
245                value: None,
246                generation: recycled.generation,
247            };
248        }
249    }
250
251    pub(crate) fn get_index_allocator(&self) -> Arc<AssetIndexAllocator> {
252        self.allocator.clone()
253    }
254
255    pub(crate) fn ids(&self) -> impl Iterator<Item = AssetId<A>> + '_ {
256        self.storage
257            .iter()
258            .enumerate()
259            .filter_map(|(i, v)| match v {
260                Entry::None => None,
261                Entry::Some { value, generation } => {
262                    if value.is_some() {
263                        Some(AssetId::from(AssetIndex {
264                            index: i as u32,
265                            generation: *generation,
266                        }))
267                    } else {
268                        None
269                    }
270                }
271            })
272    }
273}
274
275/// Stores [`Asset`] values identified by their [`AssetId`].
276///
277/// Assets identified by [`AssetId::Index`] will be stored in a "dense" vec-like storage. This is more efficient, but it means that
278/// the assets can only be identified at runtime. This is the default behavior.
279///
280/// Assets identified by [`AssetId::Uuid`] will be stored in a hashmap. This is less efficient, but it means that the assets can be referenced
281/// at compile time.
282///
283/// This tracks (and queues) [`AssetEvent`] events whenever changes to the collection occur.
284#[derive(Resource)]
285pub struct Assets<A: Asset> {
286    dense_storage: DenseAssetStorage<A>,
287    hash_map: HashMap<Uuid, A>,
288    handle_provider: AssetHandleProvider,
289    queued_events: Vec<AssetEvent<A>>,
290    /// Assets managed by the `Assets` struct with live strong `Handle`s
291    /// originating from `get_strong_handle`.
292    duplicate_handles: HashMap<AssetId<A>, u16>,
293}
294
295impl<A: Asset> Default for Assets<A> {
296    fn default() -> Self {
297        let dense_storage = DenseAssetStorage::default();
298        let handle_provider =
299            AssetHandleProvider::new(TypeId::of::<A>(), dense_storage.get_index_allocator());
300        Self {
301            dense_storage,
302            handle_provider,
303            hash_map: Default::default(),
304            queued_events: Default::default(),
305            duplicate_handles: Default::default(),
306        }
307    }
308}
309
310impl<A: Asset> Assets<A> {
311    /// Retrieves an [`AssetHandleProvider`] capable of reserving new [`Handle`] values for assets that will be stored in this
312    /// collection.
313    pub fn get_handle_provider(&self) -> AssetHandleProvider {
314        self.handle_provider.clone()
315    }
316
317    /// Reserves a new [`Handle`] for an asset that will be stored in this collection.
318    pub fn reserve_handle(&self) -> Handle<A> {
319        self.handle_provider.reserve_handle().typed::<A>()
320    }
321
322    /// Inserts the given `asset`, identified by the given `id`. If an asset already exists for `id`, it will be replaced.
323    pub fn insert(&mut self, id: impl Into<AssetId<A>>, asset: A) {
324        match id.into() {
325            AssetId::Index { index, .. } => {
326                self.insert_with_index(index, asset).unwrap();
327            }
328            AssetId::Uuid { uuid } => {
329                self.insert_with_uuid(uuid, asset);
330            }
331        }
332    }
333
334    /// Retrieves an [`Asset`] stored for the given `id` if it exists. If it does not exist, it will be inserted using `insert_fn`.
335    // PERF: Optimize this or remove it
336    pub fn get_or_insert_with(
337        &mut self,
338        id: impl Into<AssetId<A>>,
339        insert_fn: impl FnOnce() -> A,
340    ) -> &mut A {
341        let id: AssetId<A> = id.into();
342        if self.get(id).is_none() {
343            self.insert(id, insert_fn());
344        }
345        self.get_mut(id).unwrap()
346    }
347
348    /// Returns `true` if the `id` exists in this collection. Otherwise it returns `false`.
349    pub fn contains(&self, id: impl Into<AssetId<A>>) -> bool {
350        match id.into() {
351            AssetId::Index { index, .. } => self.dense_storage.get(index).is_some(),
352            AssetId::Uuid { uuid } => self.hash_map.contains_key(&uuid),
353        }
354    }
355
356    pub(crate) fn insert_with_uuid(&mut self, uuid: Uuid, asset: A) -> Option<A> {
357        let result = self.hash_map.insert(uuid, asset);
358        if result.is_some() {
359            self.queued_events
360                .push(AssetEvent::Modified { id: uuid.into() });
361        } else {
362            self.queued_events
363                .push(AssetEvent::Added { id: uuid.into() });
364        }
365        result
366    }
367    pub(crate) fn insert_with_index(
368        &mut self,
369        index: AssetIndex,
370        asset: A,
371    ) -> Result<bool, InvalidGenerationError> {
372        let replaced = self.dense_storage.insert(index, asset)?;
373        if replaced {
374            self.queued_events
375                .push(AssetEvent::Modified { id: index.into() });
376        } else {
377            self.queued_events
378                .push(AssetEvent::Added { id: index.into() });
379        }
380        Ok(replaced)
381    }
382
383    /// Adds the given `asset` and allocates a new strong [`Handle`] for it.
384    #[inline]
385    pub fn add(&mut self, asset: impl Into<A>) -> Handle<A> {
386        let index = self.dense_storage.allocator.reserve();
387        self.insert_with_index(index, asset.into()).unwrap();
388        Handle::Strong(
389            self.handle_provider
390                .get_handle(index.into(), false, None, None),
391        )
392    }
393
394    /// Upgrade an `AssetId` into a strong `Handle` that will prevent asset drop.
395    ///
396    /// Returns `None` if the provided `id` is not part of this `Assets` collection.
397    /// For example, it may have been dropped earlier.
398    #[inline]
399    pub fn get_strong_handle(&mut self, id: AssetId<A>) -> Option<Handle<A>> {
400        if !self.contains(id) {
401            return None;
402        }
403        *self.duplicate_handles.entry(id).or_insert(0) += 1;
404        let index = match id {
405            AssetId::Index { index, .. } => index.into(),
406            AssetId::Uuid { uuid } => uuid.into(),
407        };
408        Some(Handle::Strong(
409            self.handle_provider.get_handle(index, false, None, None),
410        ))
411    }
412
413    /// Retrieves a reference to the [`Asset`] with the given `id`, if it exists.
414    /// Note that this supports anything that implements `Into<AssetId<A>>`, which includes [`Handle`] and [`AssetId`].
415    #[inline]
416    pub fn get(&self, id: impl Into<AssetId<A>>) -> Option<&A> {
417        match id.into() {
418            AssetId::Index { index, .. } => self.dense_storage.get(index),
419            AssetId::Uuid { uuid } => self.hash_map.get(&uuid),
420        }
421    }
422
423    /// Retrieves a mutable reference to the [`Asset`] with the given `id`, if it exists.
424    /// Note that this supports anything that implements `Into<AssetId<A>>`, which includes [`Handle`] and [`AssetId`].
425    #[inline]
426    pub fn get_mut(&mut self, id: impl Into<AssetId<A>>) -> Option<&mut A> {
427        let id: AssetId<A> = id.into();
428        let result = match id {
429            AssetId::Index { index, .. } => self.dense_storage.get_mut(index),
430            AssetId::Uuid { uuid } => self.hash_map.get_mut(&uuid),
431        };
432        if result.is_some() {
433            self.queued_events.push(AssetEvent::Modified { id });
434        }
435        result
436    }
437
438    /// Removes (and returns) the [`Asset`] with the given `id`, if it exists.
439    /// Note that this supports anything that implements `Into<AssetId<A>>`, which includes [`Handle`] and [`AssetId`].
440    pub fn remove(&mut self, id: impl Into<AssetId<A>>) -> Option<A> {
441        let id: AssetId<A> = id.into();
442        let result = self.remove_untracked(id);
443        if result.is_some() {
444            self.queued_events.push(AssetEvent::Removed { id });
445        }
446        result
447    }
448
449    /// Removes (and returns) the [`Asset`] with the given `id`, if it exists. This skips emitting [`AssetEvent::Removed`].
450    /// Note that this supports anything that implements `Into<AssetId<A>>`, which includes [`Handle`] and [`AssetId`].
451    pub fn remove_untracked(&mut self, id: impl Into<AssetId<A>>) -> Option<A> {
452        let id: AssetId<A> = id.into();
453        self.duplicate_handles.remove(&id);
454        match id {
455            AssetId::Index { index, .. } => self.dense_storage.remove_still_alive(index),
456            AssetId::Uuid { uuid } => self.hash_map.remove(&uuid),
457        }
458    }
459
460    /// Removes the [`Asset`] with the given `id`.
461    pub(crate) fn remove_dropped(&mut self, id: AssetId<A>) {
462        match self.duplicate_handles.get_mut(&id) {
463            None | Some(0) => {}
464            Some(value) => {
465                *value -= 1;
466                return;
467            }
468        }
469        let existed = match id {
470            AssetId::Index { index, .. } => self.dense_storage.remove_dropped(index).is_some(),
471            AssetId::Uuid { uuid } => self.hash_map.remove(&uuid).is_some(),
472        };
473        if existed {
474            self.queued_events.push(AssetEvent::Removed { id });
475        }
476    }
477
478    /// Returns `true` if there are no assets in this collection.
479    pub fn is_empty(&self) -> bool {
480        self.dense_storage.is_empty() && self.hash_map.is_empty()
481    }
482
483    /// Returns the number of assets currently stored in the collection.
484    pub fn len(&self) -> usize {
485        self.dense_storage.len() + self.hash_map.len()
486    }
487
488    /// Returns an iterator over the [`AssetId`] of every [`Asset`] stored in this collection.
489    pub fn ids(&self) -> impl Iterator<Item = AssetId<A>> + '_ {
490        self.dense_storage
491            .ids()
492            .chain(self.hash_map.keys().map(|uuid| AssetId::from(*uuid)))
493    }
494
495    /// Returns an iterator over the [`AssetId`] and [`Asset`] ref of every asset in this collection.
496    // PERF: this could be accelerated if we implement a skip list. Consider the cost/benefits
497    pub fn iter(&self) -> impl Iterator<Item = (AssetId<A>, &A)> {
498        self.dense_storage
499            .storage
500            .iter()
501            .enumerate()
502            .filter_map(|(i, v)| match v {
503                Entry::None => None,
504                Entry::Some { value, generation } => value.as_ref().map(|v| {
505                    let id = AssetId::Index {
506                        index: AssetIndex {
507                            generation: *generation,
508                            index: i as u32,
509                        },
510                        marker: PhantomData,
511                    };
512                    (id, v)
513                }),
514            })
515            .chain(
516                self.hash_map
517                    .iter()
518                    .map(|(i, v)| (AssetId::Uuid { uuid: *i }, v)),
519            )
520    }
521
522    /// Returns an iterator over the [`AssetId`] and mutable [`Asset`] ref of every asset in this collection.
523    // PERF: this could be accelerated if we implement a skip list. Consider the cost/benefits
524    pub fn iter_mut(&mut self) -> AssetsMutIterator<'_, A> {
525        AssetsMutIterator {
526            dense_storage: self.dense_storage.storage.iter_mut().enumerate(),
527            hash_map: self.hash_map.iter_mut(),
528            queued_events: &mut self.queued_events,
529        }
530    }
531
532    /// A system that synchronizes the state of assets in this collection with the [`AssetServer`]. This manages
533    /// [`Handle`] drop events.
534    pub fn track_assets(mut assets: ResMut<Self>, asset_server: Res<AssetServer>) {
535        let assets = &mut *assets;
536        // note that we must hold this lock for the entire duration of this function to ensure
537        // that `asset_server.load` calls that occur during it block, which ensures that
538        // re-loads are kicked off appropriately. This function must be "transactional" relative
539        // to other asset info operations
540        let mut infos = asset_server.data.infos.write();
541        while let Ok(drop_event) = assets.handle_provider.drop_receiver.try_recv() {
542            let id = drop_event.id.typed();
543
544            if drop_event.asset_server_managed {
545                let untyped_id = id.untyped();
546
547                // the process_handle_drop call checks whether new handles have been created since the drop event was fired, before removing the asset
548                if !infos.process_handle_drop(untyped_id) {
549                    // a new handle has been created, or the asset doesn't exist
550                    continue;
551                }
552            }
553
554            assets.queued_events.push(AssetEvent::Unused { id });
555            assets.remove_dropped(id);
556        }
557    }
558
559    /// A system that applies accumulated asset change events to the [`Events`] resource.
560    ///
561    /// [`Events`]: bevy_ecs::event::Events
562    pub fn asset_events(mut assets: ResMut<Self>, mut events: EventWriter<AssetEvent<A>>) {
563        events.send_batch(assets.queued_events.drain(..));
564    }
565
566    /// A run condition for [`asset_events`]. The system will not run if there are no events to
567    /// flush.
568    ///
569    /// [`asset_events`]: Self::asset_events
570    pub(crate) fn asset_events_condition(assets: Res<Self>) -> bool {
571        !assets.queued_events.is_empty()
572    }
573}
574
575/// A mutable iterator over [`Assets`].
576pub struct AssetsMutIterator<'a, A: Asset> {
577    queued_events: &'a mut Vec<AssetEvent<A>>,
578    dense_storage: Enumerate<core::slice::IterMut<'a, Entry<A>>>,
579    hash_map: bevy_utils::hashbrown::hash_map::IterMut<'a, Uuid, A>,
580}
581
582impl<'a, A: Asset> Iterator for AssetsMutIterator<'a, A> {
583    type Item = (AssetId<A>, &'a mut A);
584
585    fn next(&mut self) -> Option<Self::Item> {
586        for (i, entry) in &mut self.dense_storage {
587            match entry {
588                Entry::None => {
589                    continue;
590                }
591                Entry::Some { value, generation } => {
592                    let id = AssetId::Index {
593                        index: AssetIndex {
594                            generation: *generation,
595                            index: i as u32,
596                        },
597                        marker: PhantomData,
598                    };
599                    self.queued_events.push(AssetEvent::Modified { id });
600                    if let Some(value) = value {
601                        return Some((id, value));
602                    }
603                }
604            }
605        }
606        if let Some((key, value)) = self.hash_map.next() {
607            let id = AssetId::Uuid { uuid: *key };
608            self.queued_events.push(AssetEvent::Modified { id });
609            Some((id, value))
610        } else {
611            None
612        }
613    }
614}
615
616#[derive(Error, Display, Debug)]
617#[display("AssetIndex {index:?} has an invalid generation. The current generation is: '{current_generation}'.")]
618pub struct InvalidGenerationError {
619    index: AssetIndex,
620    current_generation: u32,
621}
622
623#[cfg(test)]
624mod test {
625    use crate::AssetIndex;
626
627    #[test]
628    fn asset_index_round_trip() {
629        let asset_index = AssetIndex {
630            generation: 42,
631            index: 1337,
632        };
633        let roundtripped = AssetIndex::from_bits(asset_index.to_bits());
634        assert_eq!(asset_index, roundtripped);
635    }
636}