Skip to content

Commit

Permalink
Updated resource loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Feb 25, 2024
1 parent 43ee9ae commit c465026
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 371 deletions.
4 changes: 3 additions & 1 deletion resource_management/src/asset/asset_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::{AssetResolver, StorageBackend};
use crate::StorageBackend;

use super::AssetResolver;

/// An asset handler is responsible for loading assets of a certain type from a url.
pub trait AssetHandler {
Expand Down
58 changes: 6 additions & 52 deletions resource_management/src/asset/asset_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{asset::{AssetResolver, StorageBackend}, GenericResourceSerialization};
use crate::{asset::AssetResolver, resource::resource_handler::ResourceReader, DbStorageBackend, GenericResourceResponse, GenericResourceSerialization};

use super::asset_handler::AssetHandler;

Expand All @@ -24,43 +24,9 @@ impl AssetManager {
}
}

let mut args = std::env::args();
// let mut args = std::env::args();

let mut memory_only = args.find(|arg| arg == "--ResourceManager.memory_only").is_some();

if cfg!(test) { // If we are running tests we want to use memory database. This way we can run tests in parallel.
memory_only = true;
}

let db_res = if !memory_only {
polodb_core::Database::open_file(resolve_internal_path(std::path::Path::new("assets.db")))
} else {
log::info!("Using memory database instead of file database.");
polodb_core::Database::open_memory()
};

match db_res {
Ok(db) => db,
Err(_) => {
// Delete file and try again
std::fs::remove_file(resolve_internal_path(std::path::Path::new("assets.db"))).unwrap();

log::warn!("Database file was corrupted, deleting and trying again.");

let db_res = polodb_core::Database::open_file(resolve_internal_path(std::path::Path::new("assets.db")));

match db_res {
Ok(db) => db,
Err(_) => match polodb_core::Database::open_memory() { // If we can't create a file database, create a memory database. This way we can still run the application.
Ok(db) => {
log::error!("Could not create database file, using memory database instead.");
db
},
Err(_) => panic!("Could not create database"),
}
}
}
};
// let mut memory_only = args.find(|arg| arg == "--ResourceManager.memory_only").is_some();

AssetManager {
asset_handlers: Vec::new(),
Expand All @@ -82,19 +48,7 @@ impl AssetManager {

let asset_resolver = MyAssetResolver {};

struct MyStorageBackend {}

impl StorageBackend for MyStorageBackend {
fn store(&self, _: GenericResourceSerialization, _: &[u8]) -> Result<(), ()> {
Ok(())
}

fn read(&self, _: &str) -> Result<(GenericResourceSerialization, Box<[u8]>), ()> {
todo!()
}
}

let storage_backend = MyStorageBackend {};
let storage_backend = DbStorageBackend::new(&resolve_asset_path(&std::path::Path::new("assets.db")));

let asset_handler_loads = self.asset_handlers.iter().map(|asset_handler| asset_handler.load(&asset_resolver, &storage_backend, url, &json));

Expand Down Expand Up @@ -130,9 +84,9 @@ fn resolve_asset_path(path: &std::path::Path) -> std::path::PathBuf {
mod tests {
use smol::future::FutureExt;

use crate::asset::StorageBackend;
use crate::StorageBackend;

use super::*;
use super::*;

struct TestAssetHandler {

Expand Down
13 changes: 7 additions & 6 deletions resource_management/src/asset/audio_asset_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use smol::future::FutureExt;

use crate::{types::{Audio, BitDepths}, GenericResourceSerialization};
use crate::{types::{Audio, BitDepths}, GenericResourceSerialization, StorageBackend};

use super::{asset_handler::AssetHandler, AssetResolver, StorageBackend};
use super::{asset_handler::AssetHandler, AssetResolver,};

pub struct AudioAssetHandler {

Expand Down Expand Up @@ -131,9 +131,10 @@ mod tests {

assert_eq!(resource.url, "gun.wav");
assert_eq!(resource.class, "Audio");
assert_eq!(resource.resource.get_str("bit_depth").unwrap(), "Sixteen");
assert_eq!(resource.resource.get_i32("channel_count").unwrap(), 1);
assert_eq!(resource.resource.get_i64("sample_rate").unwrap(), 48000);
assert_eq!(resource.resource.get_i64("sample_count").unwrap(), 152456 / 1 / (16 / 8));
let resource = resource.resource.as_document().expect("Resource is not a document");
assert_eq!(resource.get_str("bit_depth").unwrap(), "Sixteen");
assert_eq!(resource.get_i32("channel_count").unwrap(), 1);
assert_eq!(resource.get_i64("sample_rate").unwrap(), 48000);
assert_eq!(resource.get_i64("sample_count").unwrap(), 152456 / 1 / (16 / 8));
}
}
4 changes: 2 additions & 2 deletions resource_management/src/asset/image_asset_handler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use smol::future::FutureExt;
use utils::Extent;

use crate::{types::{CompressionSchemes, Formats, Image}, GenericResourceSerialization};
use crate::{types::{CompressionSchemes, Formats, Image}, GenericResourceSerialization, StorageBackend};

use super::{asset_handler::AssetHandler, AssetResolver, StorageBackend};
use super::{asset_handler::AssetHandler, AssetResolver,};

pub struct ImageAssetHandler {
}
Expand Down
5 changes: 2 additions & 3 deletions resource_management/src/asset/material_asset_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::{borrow::Borrow, cell::RefCell, ops::Deref};

use smol::future::FutureExt;

use crate::{resource::material_resource_handler::ProgramGenerator, shader_generation::{ShaderGenerationSettings, ShaderGenerator}, types::{AlphaMode, Material, Model, Property, Shader, ShaderTypes, Value, Variant, VariantVariable}, GenericResourceSerialization, ProcessedResources};
use crate::{resource::material_resource_handler::ProgramGenerator, shader_generation::{ShaderGenerationSettings, ShaderGenerator}, types::{AlphaMode, Material, Model, Property, Shader, ShaderTypes, Value, Variant, VariantVariable}, GenericResourceSerialization, ProcessedResources, StorageBackend};

use super::{asset_handler::AssetHandler, AssetResolver, StorageBackend};
use super::{asset_handler::AssetHandler, AssetResolver,};

struct MaterialAssetHandler {
generator: Option<Box<dyn ProgramGenerator>>,
Expand Down Expand Up @@ -179,7 +179,6 @@ mod tests {
use crate::{asset::{asset_handler::AssetHandler, tests::{TestAssetResolver, TestStorageBackend}}, resource::material_resource_handler::ProgramGenerator};

#[test]
#[ignore]
fn load_material() {
let asset_resolver = TestAssetResolver::new();
let storage_backend = TestStorageBackend::new();
Expand Down
6 changes: 3 additions & 3 deletions resource_management/src/asset/mesh_asset_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::{
IntegralTypes, Material, Mesh, MeshletStream, Model, Primitive, Property, SubMesh, Value,
VertexComponent, VertexSemantics,
},
GenericResourceSerialization, ProcessedResources,
GenericResourceSerialization, ProcessedResources, StorageBackend,
};

use super::{asset_handler::AssetHandler, AssetResolver, StorageBackend};
use super::{asset_handler::AssetHandler, AssetResolver,};

pub struct MeshAssetHandler {}

Expand Down Expand Up @@ -533,7 +533,7 @@ mod tests {
],
},
],
}
}.into()
);

// TODO: ASSERT BINARY DATA
Expand Down
34 changes: 17 additions & 17 deletions resource_management/src/asset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use smol::{future::FutureExt, io::AsyncReadExt};

use crate::GenericResourceSerialization;
use crate::{resource::resource_handler::ResourceReader, GenericResourceResponse, GenericResourceSerialization};

pub mod asset_manager;
pub mod asset_handler;
Expand Down Expand Up @@ -69,20 +69,15 @@ pub trait AssetResolver: Sync + Send {
}
}

pub trait StorageBackend: Sync + Send {
fn store(&self, resource: GenericResourceSerialization, data: &[u8]) -> Result<(), ()>;
fn read(&self, id: &str) -> Result<(GenericResourceSerialization, Box<[u8]>), ()>;
}

#[cfg(test)]
pub mod tests {
use std::{collections::HashMap, sync::{Arc, Mutex}};

use smol::future::FutureExt;

use crate::GenericResourceSerialization;
use crate::{resource::{resource_handler::ResourceReader, tests::TestResourceReader}, GenericResourceResponse, GenericResourceSerialization, StorageBackend};

use super::{read_asset_from_source, AssetResolver, StorageBackend};
use super::{read_asset_from_source, AssetResolver,};

pub struct TestAssetResolver {
files: Arc<Mutex<HashMap<&'static str, Box<[u8]>>>>,
Expand Down Expand Up @@ -138,19 +133,24 @@ pub mod tests {
}

impl StorageBackend for TestStorageBackend {
fn store(&self, resource: GenericResourceSerialization, data: &[u8]) -> Result<(), ()> {
fn store<'a>(&'a self, resource: GenericResourceSerialization, data: &[u8]) -> utils::BoxedFuture<'a, Result<(), ()>> {
self.resources.lock().unwrap().push((resource, data.into()));
Ok(())

Box::pin(async move {
Ok(())
})
}

fn read(&self, id: &str) -> Result<(GenericResourceSerialization, Box<[u8]>), ()> {
let resources = self.resources.lock().unwrap();
for resource in resources.iter() {
if resource.0.url == id {
return Ok(resource.clone());
fn read<'a>(&'a self, id: &'a str) -> utils::BoxedFuture<'a, Option<(GenericResourceResponse, Box<dyn ResourceReader>)>> {
Box::pin(async move {
let resources = self.resources.lock().unwrap();
for (resource, data) in resources.iter() {
if resource.url == id {
return Some((GenericResourceResponse::new(resource.url.clone(), resource.class.clone(), data.len(), resource.resource.clone()), Box::new(TestResourceReader::new(data.clone())) as Box<dyn ResourceReader>));
}
}
}
Err(())
None
})
}
}
}
Loading

0 comments on commit c465026

Please sign in to comment.