2024-03-10 05:11:15 +00:00
|
|
|
use std::{any::{Any, TypeId}, marker::PhantomData, ops::{Deref, DerefMut}, sync::{Arc, Condvar, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard}};
|
2023-09-12 18:25:33 +00:00
|
|
|
|
2024-03-08 05:20:29 +00:00
|
|
|
use lyra_ecs::Component;
|
2024-03-10 05:11:15 +00:00
|
|
|
use crate::{loader::LoaderError, lyra_engine, DependencyState};
|
2023-09-12 18:25:33 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-02-23 21:38:38 +00:00
|
|
|
use crate::ResourceStorage;
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
pub fn optionally_add_to_dep<R: ResourceData>(deps: &mut Vec<UntypedResHandle>, handle: &Option<ResHandle<R>>) {
|
|
|
|
if let Some(h) = handle {
|
|
|
|
deps.push(h.untyped_clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 05:25:13 +00:00
|
|
|
/// A trait that that each resource type should implement.
|
|
|
|
pub trait ResourceData: Send + Sync + Any + 'static {
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
/// Collect the dependencies of the Resource.
|
|
|
|
fn dependencies(&self) -> Vec<UntypedResHandle>;
|
|
|
|
|
|
|
|
/// Recursively collect the dependencies of the Resource.
|
|
|
|
///
|
|
|
|
/// If a dependency has a child dependency, it will not show up in this list until its
|
|
|
|
/// parent is loaded.
|
|
|
|
fn recur_dependencies(&self) -> Vec<UntypedResHandle> {
|
|
|
|
let deps = self.dependencies();
|
|
|
|
let mut all_deps = deps.clone();
|
|
|
|
|
|
|
|
for dep in deps.into_iter() {
|
|
|
|
let dep = dep.read();
|
|
|
|
match &dep.state {
|
|
|
|
ResourceState::Ready(data) => {
|
|
|
|
let mut deps_dep = data.dependencies();
|
|
|
|
all_deps.append(&mut deps_dep);
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 05:25:13 +00:00
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
all_deps
|
2024-03-09 05:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
//impl<T: Send + Sync + Reflect> ResourceData for T { }
|
|
|
|
|
2023-09-12 18:25:33 +00:00
|
|
|
pub enum ResourceState {
|
|
|
|
Loading,
|
2024-03-09 05:25:13 +00:00
|
|
|
Error(Arc<LoaderError>),
|
|
|
|
Ready(Box<dyn ResourceData>),
|
2023-09-12 18:25:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
impl ResourceState {
|
|
|
|
/// Returns a boolean indicating if the state of still loading
|
|
|
|
pub fn is_loading(&self) -> bool {
|
|
|
|
matches!(self, ResourceState::Loading)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
matches!(self, ResourceState::Error(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_ready(&self) -> bool {
|
|
|
|
matches!(self, ResourceState::Ready(_))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:07:47 +00:00
|
|
|
pub struct ResourceDataRef<'a, T> {
|
2024-03-10 05:11:15 +00:00
|
|
|
guard: std::sync::RwLockReadGuard<'a, UntypedResource>,
|
|
|
|
_marker: PhantomData<T>,
|
2023-09-12 18:25:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 05:25:13 +00:00
|
|
|
impl<'a, T: 'static> std::ops::Deref for ResourceDataRef<'a, T> {
|
2024-01-15 23:07:47 +00:00
|
|
|
type Target = T;
|
2023-09-29 17:00:33 +00:00
|
|
|
|
2024-01-15 23:07:47 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
2024-03-09 05:25:13 +00:00
|
|
|
match &self.guard.state {
|
|
|
|
ResourceState::Ready(d) => {
|
|
|
|
// for some reason, if I didn't use `.as_ref`, the downcast would fail.
|
|
|
|
let d = d.as_ref().as_any();
|
|
|
|
d.downcast_ref::<T>().unwrap()
|
|
|
|
},
|
|
|
|
_ => unreachable!() // ResHandler::data_ref shouldn't allow this to run
|
|
|
|
}
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
pub struct UntypedResource {
|
2024-01-15 23:07:47 +00:00
|
|
|
pub(crate) version: usize,
|
|
|
|
pub(crate) state: ResourceState,
|
|
|
|
uuid: Uuid,
|
2024-03-09 05:25:13 +00:00
|
|
|
path: Option<String>,
|
2024-01-16 04:22:21 +00:00
|
|
|
pub(crate) is_watched: bool,
|
2024-03-10 05:11:15 +00:00
|
|
|
/// can be used to wait for the resource to load.
|
|
|
|
pub(crate) condvar: Arc<(Mutex<bool>, Condvar)>,
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 17:37:25 +00:00
|
|
|
#[derive(Clone, Component)]
|
2024-03-10 05:11:15 +00:00
|
|
|
pub struct UntypedResHandle{
|
|
|
|
pub(crate) res: Arc<RwLock<UntypedResource>>,
|
|
|
|
#[allow(dead_code)]
|
|
|
|
tyid: TypeId,
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
impl UntypedResHandle {
|
|
|
|
pub fn new(res: UntypedResource, tyid: TypeId) -> Self {
|
2024-03-09 05:25:13 +00:00
|
|
|
Self {
|
2024-03-10 05:11:15 +00:00
|
|
|
res: Arc::new(RwLock::new(res)),
|
|
|
|
tyid
|
2024-03-09 05:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
pub fn read(&self) -> RwLockReadGuard<UntypedResource> {
|
|
|
|
self.res.read().unwrap()
|
|
|
|
}
|
2024-01-15 23:07:47 +00:00
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
pub fn write(&self) -> RwLockWriteGuard<UntypedResource> {
|
|
|
|
self.res.write().unwrap()
|
2023-09-12 18:25:33 +00:00
|
|
|
}
|
2024-01-15 23:07:47 +00:00
|
|
|
|
2024-01-16 04:22:21 +00:00
|
|
|
/// Returns a boolean indicating if this resource's path is being watched.
|
|
|
|
pub fn is_watched(&self) -> bool {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.read();
|
2024-01-16 04:22:21 +00:00
|
|
|
d.is_watched
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a boolean indicating if this resource is loaded
|
2024-01-15 23:07:47 +00:00
|
|
|
pub fn is_loaded(&self) -> bool {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.read();
|
2024-03-09 05:25:13 +00:00
|
|
|
matches!(d.state, ResourceState::Ready(_))
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the uuid of the resource.
|
|
|
|
pub fn uuid(&self) -> Uuid {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.read();
|
2024-01-15 23:07:47 +00:00
|
|
|
d.uuid
|
|
|
|
}
|
|
|
|
|
2024-03-09 05:25:13 +00:00
|
|
|
pub fn path(&self) -> Option<String> {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.read();
|
2024-03-09 05:25:13 +00:00
|
|
|
d.path.clone()
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:07:47 +00:00
|
|
|
/// Retrieves the current version of the resource. This gets incremented when the resource
|
|
|
|
/// is reloaded.
|
|
|
|
pub fn version(&self) -> usize {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.read();
|
2024-01-15 23:07:47 +00:00
|
|
|
d.version
|
|
|
|
}
|
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
/// Wait for the resource to be loaded, not including its dependencies
|
|
|
|
/// (see[`UntypedResHandle::wait_recurse_dependencies_load`]).
|
|
|
|
///
|
|
|
|
/// This blocks the thread without consuming CPU time; its backed by a
|
|
|
|
/// [`Condvar`](std::sync::Condvar).
|
|
|
|
pub fn wait_for_load(&self) {
|
|
|
|
let d = self.read();
|
|
|
|
|
|
|
|
if matches!(d.state, ResourceState::Ready(_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cv = d.condvar.clone();
|
|
|
|
drop(d);
|
|
|
|
|
|
|
|
let l = cv.0.lock().unwrap();
|
|
|
|
let _unused = cv.1.wait(l).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for the entire resource, including its dependencies to be loaded.
|
|
|
|
///
|
|
|
|
/// This blocks the thread without consuming CPU time; its backed by a
|
|
|
|
/// [`Condvar`](std::sync::Condvar).
|
|
|
|
pub fn wait_recurse_dependencies_load(&self) {
|
|
|
|
self.wait_for_load();
|
|
|
|
|
|
|
|
let res = self.read();
|
|
|
|
match &res.state {
|
|
|
|
ResourceState::Ready(data) => {
|
|
|
|
// `recur_dependencies` wont return resources that are not loaded in yet
|
|
|
|
// if we did not check if the resource was finished loading, we could miss
|
|
|
|
// waiting for some resources and finish early.
|
|
|
|
while self.recurse_dependency_state().is_loading() {
|
|
|
|
for dep in data.recur_dependencies() {
|
|
|
|
dep.wait_for_load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => unreachable!() // the self.wait_for_load ensures that the state is ready
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursively get the state of the dependencies.
|
|
|
|
///
|
|
|
|
/// This doesn't return any resource data, it can be used to check if the resource and its
|
|
|
|
/// dependencies are loaded.
|
|
|
|
pub fn recurse_dependency_state(&self) -> DependencyState {
|
|
|
|
DependencyState::from_res_recurse(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_typed<T: ResourceData>(&self) -> ResHandle<T> {
|
|
|
|
ResHandle {
|
|
|
|
handle: self.clone(),
|
|
|
|
_marker: PhantomData::<T>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A handle to a resource.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
/// This struct has an inner [`RwLock`] to the resource data, so most methods may be blocking.
|
|
|
|
/// However, the only times it will be blocking is if another thread is reloading the resource
|
|
|
|
/// and has a write lock on the data. This means that most of the time, it is not blocking.
|
|
|
|
#[derive(Component)]
|
2024-03-31 04:32:31 +00:00
|
|
|
pub struct ResHandle<T: ResourceData> {
|
2024-03-10 05:11:15 +00:00
|
|
|
pub(crate) handle: UntypedResHandle,
|
|
|
|
_marker: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2024-03-31 04:32:31 +00:00
|
|
|
impl<T: ResourceData> Clone for ResHandle<T> {
|
2024-03-10 05:11:15 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
handle: self.handle.clone(),
|
|
|
|
_marker: PhantomData::<T>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 04:32:31 +00:00
|
|
|
impl<T: ResourceData> Deref for ResHandle<T> {
|
2024-03-10 05:11:15 +00:00
|
|
|
type Target = UntypedResHandle;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 04:32:31 +00:00
|
|
|
impl<T: ResourceData> DerefMut for ResHandle<T> {
|
2024-03-10 05:11:15 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ResourceData> ResHandle<T> {
|
|
|
|
pub fn new_loading(path: Option<&str>) -> Self {
|
|
|
|
let res_version = UntypedResource {
|
|
|
|
version: 0,
|
|
|
|
path: path.map(str::to_string),
|
|
|
|
state: ResourceState::Loading,
|
|
|
|
uuid: Uuid::new_v4(),
|
|
|
|
is_watched: false,
|
|
|
|
condvar: Arc::new((Mutex::new(false), Condvar::new())),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
handle: UntypedResHandle::new(res_version, TypeId::of::<T>()),
|
|
|
|
_marker: PhantomData::<T>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the resource with data, its assumed the state is `Ready`
|
|
|
|
pub fn new_ready(path: Option<&str>, data: T) -> Self {
|
|
|
|
let han = Self::new_loading(path);
|
|
|
|
han.set_state(ResourceState::Ready(Box::new(data)));
|
|
|
|
han
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve an untyped clone of the handle
|
|
|
|
pub fn untyped_clone(&self) -> UntypedResHandle {
|
|
|
|
self.handle.clone()
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:07:47 +00:00
|
|
|
/// Get a reference to the data in the resource
|
2024-03-08 05:20:29 +00:00
|
|
|
pub fn data_ref<'a>(&'a self) -> Option<ResourceDataRef<'a, T>> {
|
2024-01-15 23:07:47 +00:00
|
|
|
if self.is_loaded() {
|
2024-03-10 05:11:15 +00:00
|
|
|
let d = self.handle.read();
|
2024-01-15 23:07:47 +00:00
|
|
|
Some(ResourceDataRef {
|
2024-03-10 05:11:15 +00:00
|
|
|
guard: d,
|
|
|
|
_marker: PhantomData::<T>
|
2024-01-15 23:07:47 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
2024-01-15 23:07:47 +00:00
|
|
|
|
2024-03-31 04:32:31 +00:00
|
|
|
impl<T: ResourceData> ResourceStorage for ResHandle<T> {
|
2024-02-23 21:38:38 +00:00
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:38:38 +00:00
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:38:38 +00:00
|
|
|
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
|
|
|
|
self.clone()
|
2024-01-15 23:07:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:38:38 +00:00
|
|
|
fn as_box_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_watched(&self, watched: bool) {
|
2024-03-10 05:11:15 +00:00
|
|
|
let mut d = self.handle.write();
|
|
|
|
d.is_watched = watched;
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn version(&self) -> usize {
|
2024-03-10 05:11:15 +00:00
|
|
|
self.handle.version()
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn uuid(&self) -> Uuid {
|
2024-03-10 05:11:15 +00:00
|
|
|
self.handle.uuid()
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 05:25:13 +00:00
|
|
|
fn path(&self) -> Option<String> {
|
2024-03-10 05:11:15 +00:00
|
|
|
self.handle.path()
|
2024-03-09 05:25:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:38:38 +00:00
|
|
|
fn is_watched(&self) -> bool {
|
2024-03-10 05:11:15 +00:00
|
|
|
self.handle.is_watched()
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_loaded(&self) -> bool {
|
2024-03-10 05:11:15 +00:00
|
|
|
self.handle.is_loaded()
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|
2024-03-09 05:25:13 +00:00
|
|
|
|
|
|
|
fn set_state(&self, new: ResourceState) {
|
2024-03-10 05:11:15 +00:00
|
|
|
let mut d = self.handle.write();
|
2024-03-09 05:25:13 +00:00
|
|
|
d.state = new;
|
|
|
|
}
|
2024-03-10 05:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{path::PathBuf, str::FromStr, sync::Arc};
|
|
|
|
|
|
|
|
use async_std::task;
|
|
|
|
use instant::Duration;
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
use crate::{loader::ResourceLoader, ResHandle, ResourceData, ResourceManager};
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct SimpleDepend {
|
|
|
|
file_name: String,
|
|
|
|
ext: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceData for SimpleDepend {
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dependencies(&self) -> Vec<crate::UntypedResHandle> {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct SlowSimpleDependLoader;
|
|
|
|
|
|
|
|
impl ResourceLoader for SlowSimpleDependLoader {
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
&["txt", "buf"]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mime_types(&self) -> &[&str] {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(&self, _: crate::ResourceManager, path: &str) -> crate::loader::PinedBoxLoaderFuture {
|
|
|
|
let path = path.to_string();
|
|
|
|
Box::pin(async move {
|
|
|
|
let path = PathBuf::from_str(&path).unwrap();
|
|
|
|
|
|
|
|
let file_name = path.file_name()
|
|
|
|
.and_then(|os| os.to_str())
|
|
|
|
.unwrap();
|
|
|
|
let path_ext = path.extension()
|
|
|
|
.and_then(|os| os.to_str())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let res = rand::thread_rng().gen_range(500..1000);
|
|
|
|
|
|
|
|
task::sleep(Duration::from_millis(res)).await;
|
|
|
|
|
|
|
|
let simple = SimpleDepend {
|
|
|
|
file_name: file_name.to_string(),
|
|
|
|
ext: path_ext.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Box::new(simple) as Box<dyn ResourceData>)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_bytes(&self, _: crate::ResourceManager, _: Vec<u8>, _: usize, _: usize) -> crate::loader::PinedBoxLoaderFuture {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_erased_handle(&self) -> std::sync::Arc<dyn crate::ResourceStorage> {
|
|
|
|
Arc::from(ResHandle::<SimpleDepend>::new_loading(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct SimpleResource {
|
|
|
|
depend_a: ResHandle<SimpleDepend>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceData for SimpleResource {
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dependencies(&self) -> Vec<crate::UntypedResHandle> {
|
|
|
|
vec![self.depend_a.untyped_clone()]
|
|
|
|
}
|
|
|
|
}
|
2024-03-09 05:25:13 +00:00
|
|
|
|
2024-03-10 05:11:15 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct SlowSimpleResourceLoader;
|
|
|
|
|
|
|
|
impl ResourceLoader for SlowSimpleResourceLoader {
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
&["res", "large"]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mime_types(&self) -> &[&str] {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(&self, res_man: crate::ResourceManager, _: &str) -> crate::loader::PinedBoxLoaderFuture {
|
|
|
|
Box::pin(async move {
|
|
|
|
let res = rand::thread_rng().gen_range(500..1000);
|
|
|
|
|
|
|
|
task::sleep(Duration::from_millis(res)).await;
|
|
|
|
|
|
|
|
// load dummy dependency that will take a bit
|
|
|
|
let depend_path = "depend.txt";
|
|
|
|
let depend_han = res_man.request::<SimpleDepend>(depend_path).unwrap();
|
|
|
|
|
|
|
|
let simple = SimpleResource {
|
|
|
|
depend_a: depend_han,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Box::new(simple) as Box<dyn ResourceData>)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_bytes(&self, _: crate::ResourceManager, _: Vec<u8>, _: usize, _: usize) -> crate::loader::PinedBoxLoaderFuture {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_erased_handle(&self) -> std::sync::Arc<dyn crate::ResourceStorage> {
|
|
|
|
Arc::from(ResHandle::<SimpleResource>::new_loading(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive() {
|
|
|
|
let man = ResourceManager::new();
|
|
|
|
man.register_loader::<SlowSimpleDependLoader>();
|
|
|
|
man.register_loader::<SlowSimpleResourceLoader>();
|
|
|
|
|
|
|
|
let res = man.request::<SimpleResource>("massive_asset.res").unwrap();
|
|
|
|
|
|
|
|
let state = res.recurse_dependency_state();
|
|
|
|
assert!(state.is_loading());
|
|
|
|
|
|
|
|
// this will take a bit
|
|
|
|
res.wait_recurse_dependencies_load();
|
|
|
|
|
|
|
|
let state = res.recurse_dependency_state();
|
|
|
|
assert!(!state.is_loading());
|
|
|
|
}
|
2024-02-23 21:38:38 +00:00
|
|
|
}
|