assets_manager 0.13.9

Conveniently load, cache, and reload external resources
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! Values loadable from a cache.
//!
//! Assets are data that are used in a program. They are usually loaded from the
//! filesystem or from an archive.
//!
//! In `assets_manager`, assets are types that implement the [`Asset`] trait.
//! This trait specifies how to load an asset from an [`AssetCache`] given its
//! ID. The `AssetCache` gives access to other assets (eg your render pipeline
//! may want to get some shaders) and to a [`Source`] (to give you access to the
//! filesystem, a ZIP archive, or wherever you store your assets).
//!
//! Asset IDs are strings that represent paths using dots as separators (e.g.,
//! `"example.common.name"`). Unlike filesystem paths, they always use dots
//! regardless of the platform. File extensions are handled separately.
//!
//! The [`FileAsset`] trait provides an easy way to implement the `Asset` trait
//! for assets that can be loaded from a single file (eg an image, a sound,
//! etc). This module provides utilities to easily implement `FileAsset` for
//! common formats, such as JSON, TOML or RON.
//!
//! Additionally, [`DirLoadable`] assets can be loaded by directory. All types
//! implementing `FileAsset` also get this trait out of the box. It can be
//! implemented for any `Asset`, but it requires a manual implementation.
//!
//! # Hot-reloading
//!
//! If the `Source` supports hot-reloading, each asset is automatically reloaded
//! when any file, directory or asset it depends on is modified. Dependencies of
//! assets are automatically recorded when they are (re)loaded.
//!
//! Additionally, one can explicitly disable hot-reloading for a type.
//!
//! Note that hot-reloading is not atomic: if asset `A` depends on `B`, you can
//! observe a state where `B` is reloaded but `A` is not reloaded yet.
//! Additionally, if `A` fails to reload, the inconsistent state is kept as is.

#[cfg(feature = "ab_glyph")]
mod fonts;
#[cfg(feature = "gltf")]
#[cfg_attr(docsrs, doc(cfg(feature = "gltf")))]
mod gltf;

#[cfg(test)]
mod tests;

pub use crate::dirs::DirLoadable;

use crate::{AssetCache, BoxedError, error::ErrorKind, source::Source, utils::SharedString};
use std::{borrow::Cow, sync::Arc};

#[cfg(feature = "gltf")]
pub use self::gltf::Gltf;

#[cfg(doc)]
use crate::Handle;

/// An asset that can be loaded from a single file.
///
/// Implementing this trait provides an implementation of [`Asset`].
pub trait FileAsset: Storable {
    /// Use this field if your asset only uses one extension.
    ///
    /// This value is ignored if you set `EXTENSIONS` too.
    const EXTENSION: &'static str = "";

    /// This field enables you to specify multiple extension for an asset.
    ///
    /// If `EXTENSION` is provided, you don't have to set this constant.
    ///
    /// If this array is empty, loading an asset of this type returns an error
    /// unless a default value is provided with the `default_value` method.
    const EXTENSIONS: &'static [&'static str] = &[Self::EXTENSION];

    /// Creates a value of this type from raw bytes.
    fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError>;

    /// Specifies a eventual default value to use if an asset fails to load. If
    /// this method returns `Ok`, the returned value is used as an asset. In
    /// particular, if this method always returns `Ok`, `AssetCache::load` is
    /// guaranteed not to fail.
    ///
    /// The `id` parameter is given to easily report the error.
    ///
    /// By default, this method always returns an error.
    ///
    /// # Example
    ///
    /// On error, log it and return a default value:
    ///
    /// ```no_run
    /// # cfg_if::cfg_if! { if #[cfg(feature = "json")] {
    /// use assets_manager::{BoxedError, FileAsset, SharedString};
    /// use std::borrow::Cow;
    ///
    /// #[derive(serde::Deserialize, Default)]
    /// struct Item {
    ///     name: String,
    ///     kind: String,
    /// }
    ///
    /// impl FileAsset for Item {
    ///     const EXTENSION: &'static str = "json";
    ///
    ///     fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError> {
    ///         assets_manager::asset::load_json(&bytes)
    ///     }
    ///
    ///     fn default_value(id: &SharedString, error: BoxedError) -> Result<Item, BoxedError> {
    ///         log::warn!("Error loading {}: {}. Using default value", id, error);
    ///         Ok(Item::default())
    ///     }
    /// }
    /// # }}
    /// ```
    #[inline]
    #[expect(unused_variables)]
    fn default_value(id: &SharedString, error: BoxedError) -> Result<Self, BoxedError> {
        Err(error)
    }

    /// If `false`, disables hot-reloading of assets of this type (`true` by
    /// default).
    const HOT_RELOADED: bool = true;
}

/// An asset type that can load other kinds of assets.
///
/// `Asset`s can be loaded and retrieved by an [`AssetCache`].
///
/// Note that all [`FileAsset`]s implement `Asset`.
pub trait Asset: Storable {
    /// Loads an asset from the cache.
    ///
    /// The cache gives access to its underlying [`Source`].
    ///
    /// # Hot-reloading
    ///
    /// Any file, directory or asset loaded from `cache` is registered as a
    /// dependency. When a dependency is modified (through direct modification
    /// or hot-reloading), the asset will be reloaded.
    ///
    /// If you don't use threads in this method, you don't need to write
    /// hot-reloading-specific code.
    ///
    /// An asset cannot depend on itself.
    ///
    /// To opt out of dependencies recording, use [`AssetCache::no_record`].
    fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError>;

    /// If `false`, disable hot-reloading for assets of this type (`true` by
    /// default). This avoids having to lock the asset to read it (ie it makes
    /// [`Handle::read`] a noop)
    const HOT_RELOADED: bool = true;
}

/// Deprecated trait alias for [`Asset`].
#[deprecated = "Use `Asset` instead"]
pub trait Compound: Asset {
    /// Loads an asset from the cache.
    fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError>;

    /// If `false`, disable hot-reloading for assets of this type (`true` by
    /// default). This avoids having to lock the asset to read it (ie it makes
    /// [`Handle::read`] a noop)
    const HOT_RELOADED: bool = true;
}

#[expect(deprecated)]
impl<T> Compound for T
where
    T: FileAsset,
{
    #[inline]
    fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError> {
        let source = cache.source();

        let load_with_ext = |ext| -> Result<T, ErrorKind> {
            let asset = source
                .read(id, ext)?
                .with_cow(|content| T::from_bytes(content))?;
            Ok(asset)
        };

        let mut error = ErrorKind::NoDefaultValue;

        for ext in T::EXTENSIONS {
            match load_with_ext(ext) {
                Err(err) => error = err.or(error),
                Ok(asset) => return Ok(asset),
            }
        }

        T::default_value(id, error.into())
    }

    const HOT_RELOADED: bool = Self::HOT_RELOADED;
}

#[expect(deprecated)]
impl<T: Compound> Asset for T {
    fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError> {
        <T as Compound>::load(cache, id)
    }

    const HOT_RELOADED: bool = <T as Compound>::HOT_RELOADED;
}

impl<T> Asset for Arc<T>
where
    T: Asset,
{
    fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError> {
        let asset = T::load(cache, id)?;
        Ok(Arc::new(asset))
    }

    const HOT_RELOADED: bool = T::HOT_RELOADED;
}

/// Trait marker to store values in a cache.
///
/// This is the set of types that can be stored in a cache.
pub trait Storable: Sized + Send + Sync + 'static {}

impl<T> Storable for T where T: Send + Sync + 'static {}

#[inline]
fn cow_bytes_to_str(bytes: Cow<[u8]>) -> Result<Cow<str>, std::str::Utf8Error> {
    Ok(match bytes {
        Cow::Borrowed(b) => Cow::Borrowed(std::str::from_utf8(b)?),
        Cow::Owned(b) => Cow::Owned(String::from_utf8(b).map_err(|e| e.utf8_error())?),
    })
}

macro_rules! string_assets {
    ( $( $typ:ty, )* ) => {
        $(
            impl FileAsset for $typ {
                const EXTENSION: &'static str = "txt";

                fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError> {
                    Ok(cow_bytes_to_str(bytes)?.into())
                }
            }
        )*
    }
}

string_assets! {
    String, Box<str>, SharedString, Arc<str>,
}

/// Deserializes a value from a bincode-encoded file.
///
/// This function uses the standard bincode format, which is the default in bincode 2.0.
#[cfg(feature = "bincode")]
#[cfg_attr(docsrs, doc(cfg(feature = "bincode")))]
pub fn load_bincode_standard<'de, T: serde_core::Deserialize<'de>>(
    bytes: &'de [u8],
) -> Result<T, BoxedError> {
    let (res, _) = bincode::serde::borrow_decode_from_slice(bytes, bincode::config::standard())?;
    Ok(res)
}

/// Deserializes a value from a bincode-encoded file.
///
/// This function uses the legacy bincode format, which was the default in bincode 1.0.
#[cfg(feature = "bincode")]
#[cfg_attr(docsrs, doc(cfg(feature = "bincode")))]
pub fn load_bincode_legacy<'de, T: serde_core::Deserialize<'de>>(
    bytes: &'de [u8],
) -> Result<T, BoxedError> {
    let (res, _) = bincode::serde::borrow_decode_from_slice(bytes, bincode::config::legacy())?;
    Ok(res)
}

/// Deserializes a value from a JSON file.
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn load_json<'de, T: serde_core::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, BoxedError> {
    serde_json::from_slice(bytes).map_err(Box::from)
}

/// Deserializes a value from a msgpack-encoded file.
#[cfg(feature = "msgpack")]
#[cfg_attr(docsrs, doc(cfg(feature = "msgpack")))]
pub fn load_msgpack<'de, T: serde_core::Deserialize<'de>>(
    bytes: &'de [u8],
) -> Result<T, BoxedError> {
    rmp_serde::from_slice(bytes).map_err(Box::from)
}

/// Deserializes a value from a RON file.
#[cfg(feature = "ron")]
#[cfg_attr(docsrs, doc(cfg(feature = "ron")))]
pub fn load_ron<'de, T: serde_core::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, BoxedError> {
    ron::de::from_bytes(bytes).map_err(Box::from)
}

/// Deserializes a value from a TOML file.
#[cfg(feature = "toml")]
#[cfg_attr(docsrs, doc(cfg(feature = "toml")))]
pub fn load_toml<'de, T: serde_core::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, BoxedError> {
    toml::from_slice(bytes).map_err(Box::from)
}

/// Deserializes a value from a YAML file.
#[cfg(feature = "yaml")]
#[cfg_attr(docsrs, doc(cfg(feature = "yaml")))]
pub fn load_yaml<'de, T: serde_core::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, BoxedError> {
    serde_yaml::from_slice(bytes).map_err(Box::from)
}

/// Deserializes a value from text.
///
/// Leading and trailing whitespaces are trimmed from the input before
/// processing.
pub fn load_text<T>(bytes: &[u8]) -> Result<T, BoxedError>
where
    T: std::str::FromStr,
    T::Err: Into<BoxedError>,
{
    let str = std::str::from_utf8(bytes)?;
    str.trim().parse().map_err(Into::into)
}

macro_rules! serde_assets {
    (
        $(
            #[doc = $doc:literal]
            #[cfg(feature = $feature:literal)]
            struct $name:ident => (
                [$($ext:literal),*],
                $load:expr,
            );
        )*
    ) => {
        $(
            #[doc = $doc]
            ///
            /// This type can directly be used as a [`FileAsset`] to load values
            /// from an [`AssetCache`]. This is useful to load assets external
            /// types without a newtype wrapper (eg [`Vec`]).
            #[cfg(feature = $feature)]
            #[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
            #[derive(Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
            #[repr(transparent)]
            pub struct $name<T>(pub T);

            #[cfg(feature = $feature)]
            impl<T> Clone for $name<T>
            where
                T: Clone
            {
                fn clone(&self) -> Self {
                    Self(self.0.clone())
                }

                fn clone_from(&mut self, other: &Self) {
                    self.0.clone_from(&other.0)
                }
            }

            #[cfg(feature = $feature)]
            impl<T> From<T> for $name<T> {
                #[inline]
                fn from(t: T) -> Self {
                    Self(t)
                }
            }

            #[cfg(feature = $feature)]
            impl<T> $name<T> {
                /// Unwraps the inner value.
                #[inline]
                pub fn into_inner(self) -> T {
                    self.0
                }
            }

            #[cfg(feature = $feature)]
            impl<T> serde_core::Serialize for $name<T>
            where
                T: serde_core::Serialize,
            {
                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where
                    S: serde_core::Serializer,
                {
                    self.0.serialize(serializer)
                }
            }

            #[cfg(feature = $feature)]
            impl<'de, T> serde_core::Deserialize<'de> for $name<T>
            where
                T: serde_core::Deserialize<'de>,
            {
                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                where
                    D: serde_core::Deserializer<'de>,
                {
                    T::deserialize(deserializer).map($name)
                }

                fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
                where
                    D: serde_core::Deserializer<'de>,
                {
                    T::deserialize_in_place(deserializer, &mut place.0)
                }
            }

            #[cfg(feature = $feature)]
            #[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
            impl<T> FileAsset for $name<T>
            where
                T: for<'de> serde_core::Deserialize<'de> + Send + Sync + 'static,
            {
                const EXTENSIONS: &'static [&'static str] = &[$( $ext ),*];

                fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError> {
                    $load(&*bytes).map(Self)
                }
            }

            #[cfg(feature = $feature)]
            impl<T> AsRef<T> for $name<T> {
                #[inline]
                fn as_ref(&self) -> &T {
                    &self.0
                }
            }

            #[cfg(feature = $feature)]
            impl<T> Default for $name<T>
            where
                T: Default,
            {
                #[inline]
                fn default() -> Self {
                    Self(T::default())
                }
            }
        )*
    }
}

serde_assets! {
    /// Loads a value from a RON file.
    #[cfg(feature = "json")]
    struct Json => (
        ["json"],
        load_json,
    );

    /// Loads a value from a JSON file.
    #[cfg(feature = "ron")]
    struct Ron => (
        ["ron"],
        load_ron,
    );

    /// Loads a value from a TOML file.
    #[cfg(feature = "toml")]
    struct Toml => (
        ["toml"],
        load_toml,
    );

    /// Loads a value from a YAML file.
    #[cfg(feature = "yaml")]
    struct Yaml => (
        ["yaml", "yml"],
        load_yaml,
    );
}

macro_rules! image_assets {
    (
        $(
            #[doc = $doc:literal]
            #[cfg(feature = $feature:literal)]
            struct $name:ident => (
                $format:path,
                [$($ext:literal),*],
            );
        )*
    ) => {
        $(
            #[doc = $doc]
            #[cfg(feature = $feature)]
            #[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
            #[derive(Clone, Debug)]
            #[repr(transparent)]
            pub struct $name(pub image::DynamicImage);

            #[cfg(feature = $feature)]
            #[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
            impl FileAsset for $name {
                const EXTENSIONS: &'static [&'static str] = &[$( $ext ),*];

                fn from_bytes(data: Cow<[u8]>) -> Result<Self, BoxedError> {
                    let img = image::load_from_memory_with_format(&data, $format)?;
                    Ok(Self(img))
                }
            }
        )*
    }
}

image_assets! {
    /// An asset to load BMP images.
    #[cfg(feature = "bmp")]
    struct Bmp => (
        image::ImageFormat::Bmp,
        ["bmp"],
    );

    /// An asset to load JPEG images.
    #[cfg(feature = "jpeg")]
    struct Jpeg => (
        image::ImageFormat::Jpeg,
        ["jpg", "jpeg"],
    );

    /// An asset to load PNG images.
    #[cfg(feature = "png")]
    struct Png => (
        image::ImageFormat::Png,
        ["png"],
    );

    /// An asset to load WebP images.
    #[cfg(feature = "webp")]
    struct Webp => (
        image::ImageFormat::WebP,
        ["webp"],
    );
}