fennel_engine/resources/mod.rs
1use std::{any::Any, cell::Ref, collections::HashMap, path::PathBuf};
2
3use crate::graphics::Graphics;
4
5/// Module containing implementations of [`LoadableResource`] such as [`Image`]
6pub mod loadable;
7
8/// Manages a collection of loadable resources indexed by their name
9pub struct ResourceManager {
10 /// Map resource name to a type that implements [`LoadableResource`] trait
11 pub resources: HashMap<String, Box<dyn LoadableResource>>,
12}
13
14/// Trait that all loadable assets must implement
15pub trait LoadableResource: Any {
16 /// Load a resource from `path` and return it boxed
17 ///
18 /// # Arguments
19 /// `path`: path to the resoruce file
20 /// `graphics`: current [`Graphics`] instance which holds `texture_creator` and `ttf_context`
21 /// `size`: optional size for any resoruce that needs it
22 ///
23 /// # Errors
24 /// Returns an error if the file cannot be read or parsed
25 fn load(
26 path: PathBuf,
27 graphics: &mut Graphics,
28 size: Option<f32>
29 ) -> anyhow::Result<Box<dyn LoadableResource>>
30 where
31 Self: Sized;
32
33 /// Eaasy-to-use identifier for the resource
34 fn name(&self) -> String;
35
36 /// Return a mutable slice that the graphics thread can pass to SDL
37 ///
38 /// If the resource does not have a buffer, then it mustn't implement this function
39 fn as_mut_slice(&self) -> Option<&mut [u8]> {
40 None
41 }
42
43 /// Return an immutable slice for read‑only access
44 ///
45 /// If the resource does not have a buffer, then it mustn't implement this function
46 fn as_slice(&self) -> Option<Ref<'_, [u8]>> {
47 None
48 }
49}
50
51/// evil &Box<dyn LoadableResource> to &T
52#[allow(clippy::borrowed_box)] // i have no idea how can this be done better because here we box a
53 // trait
54pub fn as_concrete<T: 'static + LoadableResource>(b: &Box<dyn LoadableResource>) -> anyhow::Result<&T> {
55 let dyn_ref: &dyn LoadableResource = b.as_ref();
56
57 let any_ref = dyn_ref as &dyn Any;
58
59 Ok(any_ref
60 .downcast_ref::<T>()
61 .expect("incorrect concrete type"))
62}
63
64impl ResourceManager {
65 /// Create a new manager with empty `resources` field
66 pub fn new() -> Self {
67 Self {
68 resources: HashMap::new(),
69 }
70 }
71
72 /// Insert a loaded asset into the cache
73 ///
74 /// The asset is stored under the key returned by `asset.name()`
75 pub fn cache_asset(&mut self, asset: Box<dyn LoadableResource>) -> anyhow::Result<()> {
76 self.resources.insert(asset.name(), asset);
77 Ok(())
78 }
79
80 // here i have NO fucking idea should it be `&Box<dyn LoadableResource>` or whatever
81 // self.resources.get returns a reference to the resource, so basically a reference to Box
82 // but afaik Box is a pointer, and for me it feels a bit fucking wrong to uh return a
83 // reference to a pointer >:3 and also clippy is angry at me for doing this
84 #[allow(clippy::borrowed_box)] // same reason as in `as_concrete`
85 pub fn get_asset(&self, name: String) -> anyhow::Result<&Box<dyn LoadableResource>> {
86 let asset = self.resources.get(&name).unwrap();
87 Ok(asset)
88 }
89
90 /// Check if a resource is cached
91 pub fn is_cached(&self, name: String) -> bool {
92 self.resources.contains_key(&name)
93 }
94}
95
96impl Default for ResourceManager {
97 /// `default()` is equivalent to `ResourceManager::new()`.
98 fn default() -> Self {
99 Self::new()
100 }
101}