Write resource managers request function, needs testing
This commit is contained in:
parent
e21490cd6a
commit
a1c3cd6d99
|
@ -1222,6 +1222,7 @@ dependencies = [
|
||||||
"glam",
|
"glam",
|
||||||
"image",
|
"image",
|
||||||
"instant",
|
"instant",
|
||||||
|
"lyra-resource",
|
||||||
"petgraph",
|
"petgraph",
|
||||||
"quote",
|
"quote",
|
||||||
"stopwatch",
|
"stopwatch",
|
||||||
|
@ -1237,7 +1238,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lyra-resource"
|
name = "lyra-resource"
|
||||||
version = "0.1.0"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"image",
|
"image",
|
||||||
|
|
|
@ -9,6 +9,8 @@ members = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
lyra-resource = { path = "lyra-resource", version = "0.0.1" }
|
||||||
|
|
||||||
winit = "0.28.1"
|
winit = "0.28.1"
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
tracing-subscriber = { version = "0.3.16", features = [ "tracing-log" ] }
|
tracing-subscriber = { version = "0.3.16", features = [ "tracing-log" ] }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "lyra-resource"
|
name = "lyra-resource"
|
||||||
version = "0.1.0"
|
version = "0.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
pub mod resource_manager;
|
pub mod resource_manager;
|
||||||
|
pub use resource_manager::*;
|
||||||
|
|
||||||
pub mod resource;
|
pub mod resource;
|
||||||
|
pub use resource::*;
|
||||||
|
|
||||||
pub mod texture;
|
pub mod texture;
|
||||||
|
pub use texture::*;
|
||||||
|
|
||||||
pub mod loader;
|
pub mod loader;
|
||||||
|
pub use loader::*;
|
||||||
|
|
||||||
pub fn add(left: usize, right: usize) -> usize {
|
pub fn add(left: usize, right: usize) -> usize {
|
||||||
left + right
|
left + right
|
||||||
|
|
|
@ -13,6 +13,7 @@ impl From<ImageError> for LoaderError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A struct that implements the `ResourceLoader` trait used for loading textures.
|
/// A struct that implements the `ResourceLoader` trait used for loading textures.
|
||||||
|
#[derive(Default)]
|
||||||
pub struct TextureLoader;
|
pub struct TextureLoader;
|
||||||
|
|
||||||
impl ResourceLoader for TextureLoader {
|
impl ResourceLoader for TextureLoader {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use std::{sync::Arc, collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}, any::Any};
|
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 {
|
pub trait ResourceStorage: Send + Sync + Any + 'static {
|
||||||
fn as_any(&self) -> &dyn Any;
|
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)]
|
#[derive(Default)]
|
||||||
pub struct ResourceManager {
|
pub struct ResourceManager {
|
||||||
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
||||||
loader: HashMap<String, Box<dyn ResourceLoader>>,
|
loaders: Vec<Box<dyn ResourceLoader>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResourceManager {
|
impl ResourceManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
resources: HashMap::new(),
|
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()) {
|
match self.resources.get(&path.to_string()) {
|
||||||
Some(res) => {
|
Some(res) => {
|
||||||
let res = res.clone().as_arc_any();
|
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 => {
|
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()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue