lyra-resource: Make it possible to add resource loaders to the resource manager

This commit is contained in:
SeanOMik 2024-01-04 20:42:56 -05:00
parent 52e58b1ca5
commit 67c5876443
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
3 changed files with 11 additions and 3 deletions

View File

@ -29,7 +29,7 @@ impl From<io::Error> for LoaderError {
} }
} }
pub trait ResourceLoader: Send + Sync { pub trait ResourceLoader {
/// Returns the extensions that this loader supports. /// Returns the extensions that this loader supports.
fn extensions(&self) -> &[&str]; fn extensions(&self) -> &[&str];
/// Returns the mime types that this loader supports. /// Returns the mime types that this loader supports.

View File

@ -9,7 +9,7 @@ pub enum ResourceState {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Resource<T: Send + Sync + 'static> { pub struct Resource<T> {
pub path: String, pub path: String,
pub data: Option<Arc<T>>, pub data: Option<Arc<T>>,
pub uuid: Uuid, pub uuid: Uuid,
@ -19,7 +19,7 @@ pub struct Resource<T: Send + Sync + 'static> {
/// A helper type to make it easier to use resources /// A helper type to make it easier to use resources
pub type ResHandle<T> = Arc<Resource<T>>; pub type ResHandle<T> = Arc<Resource<T>>;
impl<T: Send + Sync + 'static> Resource<T> { impl<T> Resource<T> {
/// Create the resource with data, its assumed the state is `Ready` /// Create the resource with data, its assumed the state is `Ready`
pub fn with_data(path: &str, data: T) -> Self { pub fn with_data(path: &str, data: T) -> Self {
Self { Self {

View File

@ -65,6 +65,14 @@ impl ResourceManager {
} }
} }
/// Registers a loader to the manager.
pub fn register_loader<L>(&mut self)
where
L: ResourceLoader + Default + 'static
{
self.loaders.push(Arc::new(L::default()));
}
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Result<Arc<Resource<T>>, RequestError> { 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) => {