2023-09-12 14:25:33 -04:00
|
|
|
use std::{sync::Arc, collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}, any::Any};
|
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2023-09-26 17:14:38 -04:00
|
|
|
use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, texture::TextureLoader, model::ModelLoader}};
|
2023-09-12 14:25:33 -04:00
|
|
|
|
|
|
|
pub trait ResourceStorage: Send + Sync + Any + 'static {
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
|
|
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implements this trait for anything that fits the type bounds
|
|
|
|
impl<T: Send + Sync + 'static> ResourceStorage for T {
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum RequestError {
|
|
|
|
#[error("{0}")]
|
|
|
|
Loader(LoaderError),
|
|
|
|
#[error("The file extension is unsupported: '{0}'")]
|
|
|
|
UnsupportedFileExtension(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LoaderError> for RequestError {
|
|
|
|
fn from(value: LoaderError) -> Self {
|
|
|
|
RequestError::Loader(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:25:33 -04:00
|
|
|
pub struct ResourceManager {
|
|
|
|
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
2023-09-12 19:07:03 -04:00
|
|
|
loaders: Vec<Box<dyn ResourceLoader>>,
|
2023-09-12 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceManager {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
resources: HashMap::new(),
|
2023-09-26 17:14:38 -04:00
|
|
|
loaders: vec![ Box::new(TextureLoader::default()), Box::new(ModelLoader::default()) ],
|
2023-09-12 14:25:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Result<Arc<Resource<T>>, RequestError> {
|
2023-09-12 14:25:33 -04:00
|
|
|
match self.resources.get(&path.to_string()) {
|
|
|
|
Some(res) => {
|
|
|
|
let res = res.clone().as_arc_any();
|
2023-09-12 19:07:03 -04:00
|
|
|
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
|
2023-09-12 14:25:33 -04:00
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
Ok(res)
|
2023-09-12 14:25:33 -04:00
|
|
|
},
|
|
|
|
None => {
|
2023-09-12 19:07:03 -04:00
|
|
|
if let Some(loader) = self.loaders.iter()
|
|
|
|
.find(|l| l.does_support_file(path)) {
|
2023-09-12 14:25:33 -04:00
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
// Load the resource and store it
|
|
|
|
let res = loader.load(path)?;
|
|
|
|
self.resources.insert(path.to_string(), res.clone());
|
2023-09-12 14:25:33 -04:00
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
// convert Arc<dyn ResourceStorage> to Arc<Resource<T>
|
|
|
|
let res = res.as_arc_any();
|
2023-09-26 17:14:38 -04:00
|
|
|
let res = res.downcast::<Resource<T>>()
|
|
|
|
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
|
2023-09-12 14:25:33 -04:00
|
|
|
|
2023-09-12 19:07:03 -04:00
|
|
|
Ok(res)
|
|
|
|
} else {
|
|
|
|
Err(RequestError::UnsupportedFileExtension(path.to_string()))
|
|
|
|
}
|
2023-09-12 14:25:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 09:36:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use crate::{Texture, ResourceState};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn get_image(path: &str) -> String {
|
|
|
|
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
|
|
|
|
|
|
|
|
format!("{manifest}/test_files/img/{path}")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_image() {
|
|
|
|
let mut man = ResourceManager::new();
|
|
|
|
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
|
|
|
assert_eq!(res.state, ResourceState::Ready);
|
|
|
|
let img = res.data.as_ref();
|
|
|
|
img.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensures that only one copy of the same thing made
|
|
|
|
#[test]
|
|
|
|
fn ensure_single() {
|
|
|
|
let mut man = ResourceManager::new();
|
|
|
|
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
|
|
|
assert_eq!(Arc::strong_count(&res), 2);
|
|
|
|
|
|
|
|
let resagain = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
|
|
|
assert_eq!(Arc::strong_count(&resagain), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensures that an error is returned when a file that doesn't exist is requested
|
|
|
|
#[test]
|
|
|
|
fn ensure_none() {
|
|
|
|
let mut man = ResourceManager::new();
|
|
|
|
let res = man.request::<Texture>(&get_image("squigglesfff.png"));
|
|
|
|
let err = res.err().unwrap();
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
match err {
|
|
|
|
// make sure the error is NotFound
|
2023-09-21 14:22:46 -04:00
|
|
|
RequestError::Loader(LoaderError::IoError(e)) if e.kind() == io::ErrorKind::NotFound => true,
|
2023-09-21 09:36:44 -04:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-09-12 14:25:33 -04:00
|
|
|
}
|