bevy_asset/
meta.rs

1use alloc::{
2    boxed::Box,
3    string::{String, ToString},
4    vec::Vec,
5};
6use futures_lite::AsyncReadExt;
7
8use crate::{
9    io::{AssetReaderError, Reader},
10    loader::AssetLoader,
11    processor::Process,
12    Asset, AssetPath, DeserializeMetaError, VisitAssetDependencies,
13};
14use downcast_rs::{impl_downcast, Downcast};
15use ron::ser::PrettyConfig;
16use serde::{Deserialize, Serialize};
17use tracing::error;
18
19pub const META_FORMAT_VERSION: &str = "1.0";
20pub type MetaTransform = Box<dyn Fn(&mut dyn AssetMetaDyn) + Send + Sync>;
21
22/// Asset metadata that informs how an [`Asset`] should be handled by the asset system.
23///
24/// `L` is the [`AssetLoader`] (if one is configured) for the [`AssetAction`]. This can be `()` if it is not required.
25/// `P` is the [`Process`] processor, if one is configured for the [`AssetAction`]. This can be `()` if it is not required.
26#[derive(Serialize, Deserialize)]
27pub struct AssetMeta<L: AssetLoader, P: Process> {
28    /// The version of the meta format being used. This will change whenever a breaking change is made to
29    /// the meta format.
30    pub meta_format_version: String,
31    /// Information produced by the [`AssetProcessor`] _after_ processing this asset.
32    /// This will only exist alongside processed versions of assets. You should not manually set it in your asset source files.
33    ///
34    /// [`AssetProcessor`]: crate::processor::AssetProcessor
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub processed_info: Option<ProcessedInfo>,
37    /// How to handle this asset in the asset system. See [`AssetAction`].
38    pub asset: AssetAction<L::Settings, P::Settings>,
39}
40
41impl<L: AssetLoader, P: Process> AssetMeta<L, P> {
42    pub fn new(asset: AssetAction<L::Settings, P::Settings>) -> Self {
43        Self {
44            meta_format_version: META_FORMAT_VERSION.to_string(),
45            processed_info: None,
46            asset,
47        }
48    }
49
50    /// Deserializes the given serialized byte representation of the asset meta.
51    pub fn deserialize(bytes: &[u8]) -> Result<Self, DeserializeMetaError> {
52        Ok(ron::de::from_bytes(bytes)?)
53    }
54}
55
56/// Configures how an asset source file should be handled by the asset system.
57#[derive(Serialize, Deserialize)]
58pub enum AssetAction<LoaderSettings, ProcessSettings> {
59    /// Load the asset with the given loader and settings
60    /// See [`AssetLoader`].
61    Load {
62        loader: String,
63        settings: LoaderSettings,
64    },
65    /// Process the asset with the given processor and settings.
66    /// See [`Process`] and [`AssetProcessor`].
67    ///
68    /// [`AssetProcessor`]: crate::processor::AssetProcessor
69    Process {
70        processor: String,
71        settings: ProcessSettings,
72    },
73    /// Do nothing with the asset
74    Ignore,
75}
76
77/// Info produced by the [`AssetProcessor`] for a given processed asset. This is used to determine if an
78/// asset source file (or its dependencies) has changed.
79///
80/// [`AssetProcessor`]: crate::processor::AssetProcessor
81#[derive(Serialize, Deserialize, Default, Debug, Clone)]
82pub struct ProcessedInfo {
83    /// A hash of the asset bytes and the asset .meta data
84    pub hash: AssetHash,
85    /// A hash of the asset bytes, the asset .meta data, and the `full_hash` of every `process_dependency`
86    pub full_hash: AssetHash,
87    /// Information about the "process dependencies" used to process this asset.
88    pub process_dependencies: Vec<ProcessDependencyInfo>,
89}
90
91/// Information about a dependency used to process an asset. This is used to determine whether an asset's "process dependency"
92/// has changed.
93#[derive(Serialize, Deserialize, Debug, Clone)]
94pub struct ProcessDependencyInfo {
95    pub full_hash: AssetHash,
96    pub path: AssetPath<'static>,
97}
98
99/// This is a minimal counterpart to [`AssetMeta`] that exists to speed up (or enable) serialization in cases where the whole [`AssetMeta`] isn't
100/// necessary.
101// PERF:
102// Currently, this is used when retrieving asset loader and processor information (when the actual type is not known yet). This could probably
103// be replaced (and made more efficient) by a custom deserializer that reads the loader/processor information _first_, then deserializes the contents
104// using a type registry.
105#[derive(Serialize, Deserialize)]
106pub struct AssetMetaMinimal {
107    pub asset: AssetActionMinimal,
108}
109
110/// This is a minimal counterpart to [`AssetAction`] that exists to speed up (or enable) serialization in cases where the whole [`AssetAction`]
111/// isn't necessary.
112#[derive(Serialize, Deserialize)]
113pub enum AssetActionMinimal {
114    Load { loader: String },
115    Process { processor: String },
116    Ignore,
117}
118
119/// This is a minimal counterpart to [`ProcessedInfo`] that exists to speed up serialization in cases where the whole [`ProcessedInfo`] isn't
120/// necessary.
121#[derive(Serialize, Deserialize)]
122pub struct ProcessedInfoMinimal {
123    pub processed_info: Option<ProcessedInfo>,
124}
125
126/// A dynamic type-erased counterpart to [`AssetMeta`] that enables passing around and interacting with [`AssetMeta`] without knowing
127/// its type.
128pub trait AssetMetaDyn: Downcast + Send + Sync {
129    /// Returns a reference to the [`AssetLoader`] settings, if they exist.
130    fn loader_settings(&self) -> Option<&dyn Settings>;
131    /// Returns a mutable reference to the [`AssetLoader`] settings, if they exist.
132    fn loader_settings_mut(&mut self) -> Option<&mut dyn Settings>;
133    /// Returns a reference to the [`Process`] settings, if they exist.
134    fn process_settings(&self) -> Option<&dyn Settings>;
135    /// Serializes the internal [`AssetMeta`].
136    fn serialize(&self) -> Vec<u8>;
137    /// Returns a reference to the [`ProcessedInfo`] if it exists.
138    fn processed_info(&self) -> &Option<ProcessedInfo>;
139    /// Returns a mutable reference to the [`ProcessedInfo`] if it exists.
140    fn processed_info_mut(&mut self) -> &mut Option<ProcessedInfo>;
141}
142
143impl<L: AssetLoader, P: Process> AssetMetaDyn for AssetMeta<L, P> {
144    fn loader_settings(&self) -> Option<&dyn Settings> {
145        if let AssetAction::Load { settings, .. } = &self.asset {
146            Some(settings)
147        } else {
148            None
149        }
150    }
151    fn loader_settings_mut(&mut self) -> Option<&mut dyn Settings> {
152        if let AssetAction::Load { settings, .. } = &mut self.asset {
153            Some(settings)
154        } else {
155            None
156        }
157    }
158    fn process_settings(&self) -> Option<&dyn Settings> {
159        if let AssetAction::Process { settings, .. } = &self.asset {
160            Some(settings)
161        } else {
162            None
163        }
164    }
165    fn serialize(&self) -> Vec<u8> {
166        ron::ser::to_string_pretty(&self, PrettyConfig::default())
167            .expect("type is convertible to ron")
168            .into_bytes()
169    }
170    fn processed_info(&self) -> &Option<ProcessedInfo> {
171        &self.processed_info
172    }
173    fn processed_info_mut(&mut self) -> &mut Option<ProcessedInfo> {
174        &mut self.processed_info
175    }
176}
177
178impl_downcast!(AssetMetaDyn);
179
180/// Settings used by the asset system, such as by [`AssetLoader`], [`Process`], and [`AssetSaver`]
181///
182/// [`AssetSaver`]: crate::saver::AssetSaver
183pub trait Settings: Downcast + Send + Sync + 'static {}
184
185impl<T: 'static> Settings for T where T: Send + Sync {}
186
187impl_downcast!(Settings);
188
189/// The () processor should never be called. This implementation exists to make the meta format nicer to work with.
190impl Process for () {
191    type Settings = ();
192    type OutputLoader = ();
193
194    async fn process(
195        &self,
196        _context: &mut bevy_asset::processor::ProcessContext<'_>,
197        _settings: &Self::Settings,
198        _writer: &mut bevy_asset::io::Writer,
199    ) -> Result<(), bevy_asset::processor::ProcessError> {
200        unreachable!()
201    }
202}
203
204impl Asset for () {}
205
206impl VisitAssetDependencies for () {
207    fn visit_dependencies(&self, _visit: &mut impl FnMut(bevy_asset::UntypedAssetId)) {
208        unreachable!()
209    }
210}
211
212/// The () loader should never be called. This implementation exists to make the meta format nicer to work with.
213impl AssetLoader for () {
214    type Asset = ();
215    type Settings = ();
216    type Error = std::io::Error;
217    async fn load(
218        &self,
219        _reader: &mut dyn Reader,
220        _settings: &Self::Settings,
221        _load_context: &mut crate::LoadContext<'_>,
222    ) -> Result<Self::Asset, Self::Error> {
223        unreachable!();
224    }
225
226    fn reader_required_features(_settings: &Self::Settings) -> crate::io::ReaderRequiredFeatures {
227        unreachable!();
228    }
229
230    fn extensions(&self) -> &[&str] {
231        unreachable!();
232    }
233}
234
235pub(crate) fn meta_transform_settings<S: Settings>(
236    meta: &mut dyn AssetMetaDyn,
237    settings: &(impl Fn(&mut S) + Send + Sync + 'static),
238) {
239    if let Some(loader_settings) = meta.loader_settings_mut() {
240        if let Some(loader_settings) = loader_settings.downcast_mut::<S>() {
241            settings(loader_settings);
242        } else {
243            error!(
244                "Configured settings type {} does not match AssetLoader settings type",
245                core::any::type_name::<S>(),
246            );
247        }
248    }
249}
250
251pub(crate) fn loader_settings_meta_transform<S: Settings>(
252    settings: impl Fn(&mut S) + Send + Sync + 'static,
253) -> MetaTransform {
254    Box::new(move |meta| meta_transform_settings(meta, &settings))
255}
256
257pub type AssetHash = [u8; 32];
258
259/// NOTE: changing the hashing logic here is a _breaking change_ that requires a [`META_FORMAT_VERSION`] bump.
260pub(crate) async fn get_asset_hash(
261    meta_bytes: &[u8],
262    asset_reader: &mut impl Reader,
263) -> Result<AssetHash, AssetReaderError> {
264    let mut hasher = blake3::Hasher::new();
265    hasher.update(meta_bytes);
266    let mut buffer = [0; blake3::CHUNK_LEN];
267    loop {
268        let bytes_read = asset_reader.read(&mut buffer).await?;
269        hasher.update(&buffer[..bytes_read]);
270        if bytes_read == 0 {
271            // This means we've reached EOF, so we're done consuming asset bytes.
272            break;
273        }
274    }
275    Ok(*hasher.finalize().as_bytes())
276}
277
278/// NOTE: changing the hashing logic here is a _breaking change_ that requires a [`META_FORMAT_VERSION`] bump.
279pub(crate) fn get_full_asset_hash(
280    asset_hash: AssetHash,
281    dependency_hashes: impl Iterator<Item = AssetHash>,
282) -> AssetHash {
283    let mut hasher = blake3::Hasher::new();
284    hasher.update(&asset_hash);
285    for hash in dependency_hashes {
286        hasher.update(&hash);
287    }
288    *hasher.finalize().as_bytes()
289}