Skip to main content

ResourceManagerState

Struct ResourceManagerState 

Source
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: ResourceEventBroadcaster

Event broadcaster can be used to “subscribe” for events happening inside the container.

§constructors_container: ResourceConstructorContainer

A container for resource constructors.

§built_in_resources: BuiltInResourcesContainer

A 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

Source

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.

Source

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.

Source

pub fn task_pool(&self) -> Arc<TaskPool>

Returns the task pool used by this resource manager.

Source

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

Source

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.

Source

pub fn count_registered_resources(&self) -> usize

Returns total amount of registered resources.

Source

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.

Source

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.

Source

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.

Source

pub fn find_by_uuid(&self, uuid: Uuid) -> Option<&UntypedResource>

Tries to find a resource by its path. Returns None if no resource was found.

§Complexity

O(n)

Source

pub fn find_by_path(&self, path: &Path) -> Option<&UntypedResource>

Tries to find a resource by a path. Returns None if no resource was found.

§Complexity

O(n)

Source

pub fn len(&self) -> usize

Returns total amount of resources in the container.

Source

pub fn is_empty(&self) -> bool

Returns true if the resource manager has no resources.

Source

pub fn iter(&self) -> impl Iterator<Item = &UntypedResource>

Creates an iterator over resources in the manager.

Source

pub fn destroy_unused_resources(&mut self)

Immediately destroys all resources in the manager that are not used anywhere else.

Source

pub fn count_pending_resources(&self) -> usize

Returns total amount of resources that still loading.

Source

pub fn count_loaded_resources(&self) -> usize

Returns total amount of completely loaded resources.

Source

pub fn resources(&self) -> Vec<UntypedResource>

Returns a set of resource handled by this container.

Source

pub fn register_built_in_resource<T>( &mut self, resource: BuiltInResource<T>, ) -> Option<UntypedBuiltInResource>

Registers a new built-in resource, so it becomes accessible via Self::request.

Source

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.

Source

pub fn find<P>(&mut self, path: P) -> UntypedResource
where P: AsRef<Path>,

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.

Source

pub fn request<P>(&mut self, path: P) -> UntypedResource
where P: AsRef<Path>,

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.

Source

pub fn request_uuid(&mut self, uuid: Uuid) -> UntypedResource

Tries to load the resource for the given UUID.

Source

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:

  1. The resource wasn’t registered in the resource registry.
  2. 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.

Source

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.

Source

pub fn registry_folder(&self) -> PathBuf

Returns an absolute path to the registry folder (absolute version of ./data by default).

Source

pub fn add_loader<T>(&self, loader: T) -> Option<T>
where T: ResourceLoader,

Adds a new resource loader of the given type.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_wait_context(&self) -> ResourceWaitContext

Wait until all resources are loaded (or failed to load).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_supported_resource(&self, path: &Path) -> bool

Checks if there’s a loader for the given resource path.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsyncTaskResult for T
where T: Any + Send + 'static,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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 T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts 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>

Converts 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)

Converts &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)

Converts &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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> ObjectOrVariant<T> for U

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more