engine/resources/
assets.rs

1// SPDX-FileCopyrightText: 2024 Jens Pitkänen <jens.pitkanen@helsinki.fi>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5pub mod audio_clip;
6pub mod sprite;
7
8use core::ops::Range;
9
10macro_rules! gen_asset_handle_code {
11    ($asset_type:ident, $handle_name:ident, $find_fn:ident, $get_fn:ident, $field:ident) => {
12        pub use handle_impl::$handle_name;
13        mod handle_impl {
14            #[allow(unused_imports)] // used by docs
15            use $crate::resources::ResourceDatabase;
16
17            use super::*;
18
19            #[doc = "Handle for [`"]
20            #[doc = stringify!($asset_type)]
21            #[doc = "`].\n\nCreated with [`"]
22            #[doc = concat!("ResourceDatabase::", stringify!($find_fn))]
23            #[doc = "`], and can be resolved into a borrow of the asset itself with [`"]
24            #[doc = concat!("ResourceDatabase::", stringify!($get_fn))]
25            #[doc = "`]."]
26            #[derive(Clone, Copy, Debug)]
27            pub struct $handle_name(usize);
28            impl $crate::resources::ResourceDatabase {
29                #[doc = "Returns a [`"]
30                #[doc = stringify!($handle_name)]
31                #[doc = "`] if the database contains a [`"]
32                #[doc = stringify!($asset_type)]
33                #[doc = "`] with this name. Cache this, and use [`"]
34                #[doc = concat!("ResourceDatabase::", stringify!($get_fn))]
35                #[doc = "`] to access the actual asset at runtime."]
36                pub fn $find_fn(&self, name: &str) -> Option<$handle_name> {
37                    profiling::function_scope!();
38                    let Ok(index) = self
39                        .$field
40                        .binary_search_by(|asset| asset.name.as_str().cmp(name))
41                    else {
42                        return None;
43                    };
44                    Some($handle_name(index))
45                }
46
47                #[doc = "Returns the [`"]
48                #[doc = stringify!($asset_type)]
49                #[doc = "`] behind a handle previously queried with [`"]
50                #[doc = concat!("ResourceDatabase::", stringify!($find_fn))]
51                #[doc = "`]. Note that reusing handles between separate [`ResourceDatabase`]s will cause panics."]
52                pub fn $get_fn(&self, handle: $handle_name) -> &$asset_type {
53                    &self.$field[handle.0].asset
54                }
55            }
56        }
57    };
58}
59
60pub(crate) use gen_asset_handle_code;
61
62/// Trait for operations relevant to any assets, for writing asset management
63/// code which is generic over the particular asset type.
64pub trait Asset {
65    /// If this asset refers to any regular chunks, returns the range
66    /// referenced.
67    fn get_chunks(&self) -> Option<Range<u32>>;
68    /// Applies an offset to all regular chunk references in the asset.
69    fn offset_chunks(&mut self, offset: i32);
70    /// If this asset refers to any sprite chunks, returns the range referenced.
71    fn get_sprite_chunks(&self) -> Option<Range<u32>>;
72    /// Applies an offset to all sprite chunk references in the asset.
73    fn offset_sprite_chunks(&mut self, offset: i32);
74}