bevy/crates/bevy_asset/src/info.rs
Mark Nokalt f9c1a8a3d5 docs: Full documentation for bevy_asset (#3536)
# Objective

This PR aims to document the `bevy_asset` crate to complete coverage, while also trying to improve some bits of UX.

### Progress

- [x] Root items
- [x] `handle` module
- [x] `info` module
- [x] `path` module
- [x] `loader` module
- [x] `io` and `filesystem_watcher` module
- [x] `assets` module
- [x] `asset_server` module
- [x] `diagnostic` module
- [x] `debug_asset_server` module
- [x] Crate level documentation
- [x] Add `#![warn(missing_docs)]` lint

Coverage: 100%

## Migration Guide

- Rename `FileAssetIo::get_root_path` uses to `FileAssetIo::get_base_path`

    `FileAssetIo::root_path()` is a getter for the `root_path` field, while `FileAssetIo::get_root_path` returned the parent directory of the asset root path, which was the executable's directory unless `CARGO_MANIFEST_DIR` was set. This change solves the ambiguity between the two methods.
2022-07-12 15:44:09 +00:00

69 lines
2.2 KiB
Rust

use crate::{path::AssetPath, LabelId};
use bevy_utils::{HashMap, HashSet, Uuid};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
/// Metadata for an asset source.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SourceMeta {
/// A collection of asset metadata.
pub assets: Vec<AssetMeta>,
}
/// Metadata for an asset.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AssetMeta {
/// Asset label.
pub label: Option<String>,
/// Asset dependencies.
pub dependencies: Vec<AssetPath<'static>>,
/// An unique identifier for an asset type.
pub type_uuid: Uuid,
}
/// Information about an asset source, such as its path, load state and asset metadata.
#[derive(Clone, Debug)]
pub struct SourceInfo {
/// Metadata for the source.
pub meta: Option<SourceMeta>,
/// The path of the source.
pub path: PathBuf,
/// A map of assets and their type identifiers.
pub asset_types: HashMap<LabelId, Uuid>,
/// The load state of the source.
pub load_state: LoadState,
/// A collection to track which assets were sent to their asset storages.
pub committed_assets: HashSet<LabelId>,
/// Current version of the source.
pub version: usize,
}
impl SourceInfo {
/// Returns `true` if all assets tracked by the source were loaded into their asset storages.
pub fn is_loaded(&self) -> bool {
self.meta.as_ref().map_or(false, |meta| {
self.committed_assets.len() == meta.assets.len()
})
}
/// Gets the type identifier for an asset identified by `label_id`.
pub fn get_asset_type(&self, label_id: LabelId) -> Option<Uuid> {
self.asset_types.get(&label_id).cloned()
}
}
/// The load state of an asset.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum LoadState {
/// The asset has not been loaded.
NotLoaded,
/// The asset is in the process of loading.
Loading,
/// The asset has been loaded and is living inside an [`Assets`](crate::Assets) collection.
Loaded,
/// The asset failed to load.
Failed,
/// The asset was previously loaded, however all handles were dropped and the asset was removed
/// from the [`Assets`](crate::Assets) collection.
Unloaded,
}