Write resource managers request function, needs testing

This commit is contained in:
SeanOMik 2023-09-12 19:07:03 -04:00
parent e21490cd6a
commit a1c3cd6d99
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
6 changed files with 47 additions and 9 deletions

3
Cargo.lock generated
View File

@ -1222,6 +1222,7 @@ dependencies = [
"glam",
"image",
"instant",
"lyra-resource",
"petgraph",
"quote",
"stopwatch",
@ -1237,7 +1238,7 @@ dependencies = [
[[package]]
name = "lyra-resource"
version = "0.1.0"
version = "0.0.1"
dependencies = [
"anyhow",
"image",

View File

@ -9,6 +9,8 @@ members = [
]
[dependencies]
lyra-resource = { path = "lyra-resource", version = "0.0.1" }
winit = "0.28.1"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = [ "tracing-log" ] }

View File

@ -1,6 +1,6 @@
[package]
name = "lyra-resource"
version = "0.1.0"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,7 +1,14 @@
pub mod resource_manager;
pub use resource_manager::*;
pub mod resource;
pub use resource::*;
pub mod texture;
pub use texture::*;
pub mod loader;
pub use loader::*;
pub fn add(left: usize, right: usize) -> usize {
left + right

View File

@ -13,6 +13,7 @@ impl From<ImageError> for LoaderError {
}
/// A struct that implements the `ResourceLoader` trait used for loading textures.
#[derive(Default)]
pub struct TextureLoader;
impl ResourceLoader for TextureLoader {

View File

@ -1,6 +1,8 @@
use std::{sync::Arc, collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}, any::Any};
use crate::{resource::Resource, loader::ResourceLoader};
use thiserror::Error;
use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, texture::TextureLoader}};
pub trait ResourceStorage: Send + Sync + Any + 'static {
fn as_any(&self) -> &dyn Any;
@ -23,33 +25,58 @@ impl<T: Send + Sync + 'static> ResourceStorage for T {
}
}
#[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)
}
}
#[derive(Default)]
pub struct ResourceManager {
resources: HashMap<String, Arc<dyn ResourceStorage>>,
loader: HashMap<String, Box<dyn ResourceLoader>>,
loaders: Vec<Box<dyn ResourceLoader>>,
}
impl ResourceManager {
pub fn new() -> Self {
Self {
resources: HashMap::new(),
loader: HashMap::new(),
loaders: vec![ Box::new(TextureLoader::default()) ],
}
}
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Option<Arc<Resource<T>>> {
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Result<Arc<Resource<T>>, RequestError> {
match self.resources.get(&path.to_string()) {
Some(res) => {
let res = res.clone().as_arc_any();
let res = res.downcast::<Resource<T>>().ok();
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
res
Ok(res)
},
None => {
if let Some(loader) = self.loaders.iter()
.find(|l| l.does_support_file(path)) {
// Load the resource and store it
let res = loader.load(path)?;
self.resources.insert(path.to_string(), res.clone());
// convert Arc<dyn ResourceStorage> to Arc<Resource<T>
let res = res.as_arc_any();
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
todo!()
Ok(res)
} else {
Err(RequestError::UnsupportedFileExtension(path.to_string()))
}
}
}
}