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#[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 pub fn to_bits(self) -> u64 {
33 let Self { generation, index } = self;
34 ((generation as u64) << 32) | index as u64
35 }
36 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
45pub(crate) struct AssetIndexAllocator {
47 next_index: AtomicU32,
49 recycled_queue_sender: Sender<AssetIndex>,
50 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 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 pub fn recycle(&self, index: AssetIndex) {
90 self.recycled_queue_sender.send(index).unwrap();
91 }
92}
93
94#[derive(Asset, TypePath)]
98pub struct LoadedUntypedAsset {
99 #[dependency]
100 pub handle: UntypedHandle,
101}
102
103#[derive(Default)]
105enum Entry<A: Asset> {
106 #[default]
108 None,
109 Some { value: Option<A>, generation: u32 },
111}
112
113struct 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 pub(crate) fn len(&self) -> usize {
133 self.len as usize
134 }
135
136 pub(crate) fn is_empty(&self) -> bool {
138 self.len == 0
139 }
140
141 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 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 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 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#[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 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 pub fn get_handle_provider(&self) -> AssetHandleProvider {
314 self.handle_provider.clone()
315 }
316
317 pub fn reserve_handle(&self) -> Handle<A> {
319 self.handle_provider.reserve_handle().typed::<A>()
320 }
321
322 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 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 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 #[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 #[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 #[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 #[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 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 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 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 pub fn is_empty(&self) -> bool {
480 self.dense_storage.is_empty() && self.hash_map.is_empty()
481 }
482
483 pub fn len(&self) -> usize {
485 self.dense_storage.len() + self.hash_map.len()
486 }
487
488 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 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 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 pub fn track_assets(mut assets: ResMut<Self>, asset_server: Res<AssetServer>) {
535 let assets = &mut *assets;
536 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 if !infos.process_handle_drop(untyped_id) {
549 continue;
551 }
552 }
553
554 assets.queued_events.push(AssetEvent::Unused { id });
555 assets.remove_dropped(id);
556 }
557 }
558
559 pub fn asset_events(mut assets: ResMut<Self>, mut events: EventWriter<AssetEvent<A>>) {
563 events.send_batch(assets.queued_events.drain(..));
564 }
565
566 pub(crate) fn asset_events_condition(assets: Res<Self>) -> bool {
571 !assets.queued_events.is_empty()
572 }
573}
574
575pub 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}