pub struct ResourceManagerState {
pub loaders: Arc<Mutex<RawMutex, ResourceLoadersContainer>>,
pub event_broadcaster: ResourceEventBroadcaster,
pub constructors_container: ResourceConstructorContainer,
pub built_in_resources: BuiltInResourcesContainer,
pub resource_io: Arc<dyn ResourceIo>,
pub resource_registry: Arc<Mutex<RawMutex, ResourceRegistry>>,
/* private fields */
}Expand description
Internal state of the resource manager.
Fields§
§loaders: Arc<Mutex<RawMutex, ResourceLoadersContainer>>A set of resource loaders. Use this field to register your own resource loader.
event_broadcaster: ResourceEventBroadcasterEvent broadcaster can be used to “subscribe” for events happening inside the container.
constructors_container: ResourceConstructorContainerA container for resource constructors.
built_in_resources: BuiltInResourcesContainerA set of built-in resources, that will be used to resolve references on deserialization.
resource_io: Arc<dyn ResourceIo>File system abstraction interface. Could be used to support virtual file systems.
resource_registry: Arc<Mutex<RawMutex, ResourceRegistry>>Resource registry, contains associations UUID -> File Path. Any access to the registry
must be async, use task pool for this.
Implementations§
Source§impl ResourceManagerState
impl ResourceManagerState
Sourcepub fn update_or_load_registry(&self)
pub fn update_or_load_registry(&self)
Tries to update the registry if possible on the current platform, and if not - try to load an existing one. Some platforms do not have a file system, so the registry must be prepared on a platform that does have it and then saved to be loaded later on. For example, WebAssembly platform does not have a file system and the resource manager will try to load an existing registry instead of updating it.
Sourcepub fn collect_native_resources(&self) -> Vec<PathBuf>
pub fn collect_native_resources(&self) -> Vec<PathBuf>
Tries to find all the native resources registered in the resource registry, returns a collection of paths to such resources.
Sourcepub fn set_resource_io(&mut self, resource_io: Arc<dyn ResourceIo>)
pub fn set_resource_io(&mut self, resource_io: Arc<dyn ResourceIo>)
Set the IO source that the resource manager should use when loading assets
Sourcepub fn set_watcher(&mut self, watcher: Option<FileSystemWatcher>)
pub fn set_watcher(&mut self, watcher: Option<FileSystemWatcher>)
Sets resource watcher which will track any modifications in file system and forcing the manager to reload changed resources. By default there is no watcher, since it may be an undesired effect to reload resources at runtime. This is very useful thing for fast iterative development.
Sourcepub fn count_registered_resources(&self) -> usize
pub fn count_registered_resources(&self) -> usize
Returns total amount of registered resources.
Sourcepub fn loading_progress(&self) -> usize
pub fn loading_progress(&self) -> usize
Returns percentage of loading progress. This method is useful to show progress on loading screen in your game. This method could be used alone if your game depends only on external resources, or if your game doing some heavy calculations this value can be combined with progress of your tasks.
Sourcepub fn process_filesystem_events(&mut self)
pub fn process_filesystem_events(&mut self)
Handle events in the file system relating to adding, removing, or modifying resources. This may involve updating the registry to reflect changes to the resources, and it may involve creating new meta files for resources that are missing meta files.
Sourcepub fn update(&mut self, dt: f32)
pub fn update(&mut self, dt: f32)
Update resource containers and do hot-reloading.
Resources are removed if they’re not used or reloaded if they have changed in disk.
Normally, this is called from Engine::update().
You should only call this manually if you don’t use that method.
Sourcepub fn find_by_uuid(&self, uuid: Uuid) -> Option<&UntypedResource>
pub fn find_by_uuid(&self, uuid: Uuid) -> Option<&UntypedResource>
Sourcepub fn find_by_path(&self, path: &Path) -> Option<&UntypedResource>
pub fn find_by_path(&self, path: &Path) -> Option<&UntypedResource>
Sourcepub fn iter(&self) -> impl Iterator<Item = &UntypedResource>
pub fn iter(&self) -> impl Iterator<Item = &UntypedResource>
Creates an iterator over resources in the manager.
Sourcepub fn destroy_unused_resources(&mut self)
pub fn destroy_unused_resources(&mut self)
Immediately destroys all resources in the manager that are not used anywhere else.
Sourcepub fn count_pending_resources(&self) -> usize
pub fn count_pending_resources(&self) -> usize
Returns total amount of resources that still loading.
Sourcepub fn count_loaded_resources(&self) -> usize
pub fn count_loaded_resources(&self) -> usize
Returns total amount of completely loaded resources.
Sourcepub fn resources(&self) -> Vec<UntypedResource>
pub fn resources(&self) -> Vec<UntypedResource>
Returns a set of resource handled by this container.
Sourcepub fn register_built_in_resource<T>(
&mut self,
resource: BuiltInResource<T>,
) -> Option<UntypedBuiltInResource>where
T: TypedResourceData,
pub fn register_built_in_resource<T>(
&mut self,
resource: BuiltInResource<T>,
) -> Option<UntypedBuiltInResource>where
T: TypedResourceData,
Registers a new built-in resource, so it becomes accessible via Self::request.
Sourcepub fn find_uuid(&mut self, uuid: Uuid) -> UntypedResource ⓘ
pub fn find_uuid(&mut self, uuid: Uuid) -> UntypedResource ⓘ
Find the resource for the given UUID without loading the resource. Searches the resource manager to find a resource with the given UUID, including built-in resources. If no resource is found, a new unloaded external resource is returned for the given UUID, because it is presumed that this is a real UUID for some resource that is not currently managed and therefore it should be added to the manager.
Sourcepub fn find<P>(&mut self, path: P) -> UntypedResource ⓘ
pub fn find<P>(&mut self, path: P) -> UntypedResource ⓘ
Searches the resource manager and the registry to find a resource with the given path, including built-in resources. If no resource is found, a new UUID is generated and the path is added to the registry and an unloaded resource is returned.
§Panics
Panics if the path is invalid, such as if it includes a directory that does not exist or contains invalid characters.
Sourcepub fn request<P>(&mut self, path: P) -> UntypedResource ⓘ
pub fn request<P>(&mut self, path: P) -> UntypedResource ⓘ
Tries to load the resource at the given path.
§Panics
Panics if the path is invalid, such as if it includes a directory that does not exist or contains invalid characters.
Sourcepub fn request_uuid(&mut self, uuid: Uuid) -> UntypedResource ⓘ
pub fn request_uuid(&mut self, uuid: Uuid) -> UntypedResource ⓘ
Tries to load the resource for the given UUID.
Sourcepub fn resource_path(&self, resource: &UntypedResource) -> Option<PathBuf>
pub fn resource_path(&self, resource: &UntypedResource) -> Option<PathBuf>
Tries to fetch a path of the given untyped resource. The path may be missing in a few cases:
- The resource wasn’t registered in the resource registry.
- The resource registry wasn’t loaded.
§Built-in resources
As a last resort, this method tries to find a built-in resource descriptor corresponding to the given resource and returns its “path”. In reality, it is just a string id, since built-in resources are stored inside the binary.
Sourcepub fn uuid_to_resource_path(&self, resource_uuid: Uuid) -> Option<PathBuf>
pub fn uuid_to_resource_path(&self, resource_uuid: Uuid) -> Option<PathBuf>
Tries to fetch a resource path associated with the given UUID. Returns None if there’s
no resource with the given UUID.
§Built-in resources
As a last resort, this method tries to find a built-in resource descriptor corresponding to the given resource uuid and returns its “path”. In reality, it is just a string id, since built-in resources are stored inside the binary.
Sourcepub fn registry_folder(&self) -> PathBuf
pub fn registry_folder(&self) -> PathBuf
Returns an absolute path to the registry folder (absolute version of ./data by default).
Sourcepub fn add_loader<T>(&self, loader: T) -> Option<T>where
T: ResourceLoader,
pub fn add_loader<T>(&self, loader: T) -> Option<T>where
T: ResourceLoader,
Adds a new resource loader of the given type.
Sourcepub fn request_resource(&mut self, resource: &mut UntypedResource)
pub fn request_resource(&mut self, resource: &mut UntypedResource)
Tries to load the given resource, based on the resource’s UUID, and adds the resource to the manager if it is not already in the manager.
Sourcepub fn add_resource(&mut self, resource: &mut UntypedResource)
pub fn add_resource(&mut self, resource: &mut UntypedResource)
Add the given resource to the resource manager, based on the resource’s UUID, without initiating the loading of the resource. The given resource may be modified to be a reference to the shared data of an existing resource with the same UUID.
Sourcepub fn register(
&mut self,
resource: UntypedResource,
path: impl AsRef<Path>,
) -> Result<(), ResourceRegistrationError>
pub fn register( &mut self, resource: UntypedResource, path: impl AsRef<Path>, ) -> Result<(), ResourceRegistrationError>
Add the given resource to the manager and registers the resource as an external resource with the given
path, updating the metadata file with the resource’s UUID and updating the registry file with the resource’s path.
Calling this should only be necessary after newly creating the file in the given path by saving the resource
to the file, otherwise the resource’s path should already have been discovered by
ResourceManagerState::update_or_load_registry.
If the manager already has a resource with this resource’s UUID, return ResourceRegistrationError::AlreadyRegistered.
§Panics
Panics if the path is invalid, such as if it includes a directory that does not exist or contains invalid characters.
Sourcepub fn reload_resource(&mut self, resource: UntypedResource)
pub fn reload_resource(&mut self, resource: UntypedResource)
Reloads a single resource. Does nothing in case of built-in resources. Log an error if the resource cannot be reloaded.
Sourcepub fn reload_resources(&mut self) -> Vec<UntypedResource>
pub fn reload_resources(&mut self) -> Vec<UntypedResource>
Reloads all resources in the container. Returns a list of resources that will be reloaded. You can use the list to wait until all resources are loading.
Sourcepub fn get_wait_context(&self) -> ResourceWaitContext
pub fn get_wait_context(&self) -> ResourceWaitContext
Wait until all resources are loaded (or failed to load).
Sourcepub fn try_reload_resource_from_path(&mut self, path: &Path) -> bool
pub fn try_reload_resource_from_path(&mut self, path: &Path) -> bool
Tries to reload a resource at the given path, and returns true if a reload will actually begin. Returns false if the resource was already loading or cannot be reloaded.
Sourcepub async fn make_resource_move_context(
&self,
src_path: impl AsRef<Path>,
dest_path: impl AsRef<Path>,
overwrite_existing: bool,
) -> Result<ResourceMoveContext, ResourceMovementError>
pub async fn make_resource_move_context( &self, src_path: impl AsRef<Path>, dest_path: impl AsRef<Path>, overwrite_existing: bool, ) -> Result<ResourceMoveContext, ResourceMovementError>
Creates a resource movement context.
Sourcepub async fn can_resource_be_moved(
&self,
src_path: impl AsRef<Path>,
dest_path: impl AsRef<Path>,
overwrite_existing: bool,
) -> bool
pub async fn can_resource_be_moved( &self, src_path: impl AsRef<Path>, dest_path: impl AsRef<Path>, overwrite_existing: bool, ) -> bool
Returns true if a resource at the src_path can be moved to the dest_path, false -
otherwise. Source path must be a valid resource path, and the dest path must have a valid
new directory part of the path.
Sourcepub async fn move_resource_by_path(
&self,
src_path: impl AsRef<Path>,
dest_path: impl AsRef<Path>,
overwrite_existing: bool,
) -> Result<(), ResourceMovementError>
pub async fn move_resource_by_path( &self, src_path: impl AsRef<Path>, dest_path: impl AsRef<Path>, overwrite_existing: bool, ) -> Result<(), ResourceMovementError>
Tries to move a resource at the given path to the new path. The path of the resource must be registered in the resource registry for the resource to be moveable. This method can also be used to rename the source file of a resource.
Sourcepub async fn move_resource(
&self,
resource: impl AsRef<UntypedResource>,
new_path: impl AsRef<Path>,
overwrite_existing: bool,
) -> Result<(), ResourceMovementError>
pub async fn move_resource( &self, resource: impl AsRef<UntypedResource>, new_path: impl AsRef<Path>, overwrite_existing: bool, ) -> Result<(), ResourceMovementError>
Attempts to move a resource from its current location to the new path. The resource must be registered in the resource registry to be moveable. This method can also be used to rename the source file of a resource.
Sourcepub fn is_supported_resource(&self, path: &Path) -> bool
pub fn is_supported_resource(&self, path: &Path) -> bool
Checks if there’s a loader for the given resource path.
Sourcepub fn is_path_in_registry(&self, path: &Path) -> bool
pub fn is_path_in_registry(&self, path: &Path) -> bool
Checks if the given path is located inside the folder tracked by the resource registry.
Sourcepub async fn try_move_folder(
&self,
src_dir: &Path,
dest_dir: &Path,
overwrite_existing: bool,
) -> Result<(), FolderMovementError>
pub async fn try_move_folder( &self, src_dir: &Path, dest_dir: &Path, overwrite_existing: bool, ) -> Result<(), FolderMovementError>
Tries to move a folder to some other folder.
Auto Trait Implementations§
impl !Freeze for ResourceManagerState
impl !RefUnwindSafe for ResourceManagerState
impl Send for ResourceManagerState
impl !Sync for ResourceManagerState
impl Unpin for ResourceManagerState
impl !UnwindSafe for ResourceManagerState
Blanket Implementations§
Source§impl<T> AsyncTaskResult for T
impl<T> AsyncTaskResult for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.