class Resources

Inherits: Module<Resources>

Manager for dealing with all engine resources.

It allows you to save new resources and load existing ones.

Public

Constructors

Resources

Resources() = default

Methods

~Resources

~Resources() noexcept

Load

HResource Load(const Path &resourcePath, const ResourceLoadOptions &loadOptions)

Loads a resource at the specified path.

Resources are searched in all currently loaded packages within the PackageManager, as well as any in-memory resources registered with the Resources manager.

resourcePath
Path to the resource. This may be a virtual or physical path. e.g.: Virtual path: '/game/textures/path/to/resource' Physical path: 'D:/path/to/package.b3d/path/to/resource'
loadOptions
Options to control the loading process.

Returns: Handle to the resource. Note if performing async loading this method will return immediately, but the resource may not yet be loaded. Returns null if resource cannot be loaded, and logs why it failed.

Load

HResource Load(const UUID &resourceId, const ResourceLoadOptions &loadOptions)

Loads a resource with the specified ID.

Resources are searched in all currently loaded packages within the PackageManager, as well as any in-memory resources registered with the Resources manager.

resourceId
ID of the resource.
loadOptions
Options to control the loading process.

Returns: Handle to the resource. Note if performing async loading this method will return immediately, but the resource may not yet be loaded. Returns null if resource cannot be loaded, and logs why it failed.

Exists

bool Exists(const Path &resourcePath) const

Checks if the resource at the provided path exists.

resourcePath
Path to the resource. This may be a virtual or physical path. e.g.: Virtual path: '/game/textures/path/to/resource' Physical path: 'D:/path/to/package.b3d/path/to/resource'

Returns: True if the resource can be located, false otherwise.

Exists

bool Exists(const UUID &resourceId) const

Checks if the resource with the provided ID exists.

resourceId
ID of the resource.

Returns: True if the resource can be located, false otherwise.

ReleaseInternalReference

void ReleaseInternalReference(const HResource &resource)

Releases an internal reference to the resource held by the resources system.

This allows the resource to be unloaded when it goes out of scope, if the resource was loaded with option.

Alternatively you can also skip manually calling ReleaseInternalReference() and call UnloadAllUnused() which will unload all resources that do not have any external references, but you lose the fine grained control of what will be unloaded.

ReleaseInternalReference

void ReleaseInternalReference(ResourceHandle &resource)

UnloadAllUnused

void UnloadAllUnused()

Finds all resources that aren't being referenced outside of the resources system and unloads them.

UnloadAll

void UnloadAll()

Forces unload of all resources, whether they are being used or not.

SaveAsSinglePackage

void SaveAsSinglePackage(const HResource &resource, const Path &folder, const String &name, const ResourceSaveOptions &saveOptions = ResourceSaveOptions())

Saves a resource into its own package.

The package will be created in , with as the package name. There will be a single resource in the package, also named .

resource
Resource to save.
folder
Absolute path to a folder in which to create the package.
name
Name of the package to create, as well as the name of the resource within the package.
saveOptions
Options to control the save operation.

IsLoaded

bool IsLoaded(const UUID &uuid, bool checkInProgress = true)

Checks is the resource with the specified UUID loaded.

uuid
UUID of the resource to check.
checkInProgress
Should this method also check resources that are in progress of being asynchronously loaded.

Returns: True if loaded or loading in progress, false otherwise.

GetLoadProgress

float GetLoadProgress(const HResource &resource)

Returns the loading progress of a resource that's being loaded

resource
Resource whose load progress to check.

Returns: Load progress in range [0, 1].

staticInstance

static T &Instance()

Returns a reference to the module instance.

Module has to have been started up first otherwise an exception will be thrown.

staticInstancePtr

static T *InstancePtr()

Returns a pointer to the module instance.

Module has to have been started up first otherwise an exception will be thrown.

staticShutDown

static void ShutDown()

Shuts down this module and frees any resources it is using.

staticIsStarted

static bool IsStarted()

Query if the module has been started.

Fields

OnResourceLoaded

Event<void (const HResource &)> OnResourceLoaded

Called when the resource has been successfully loaded.

OnResourceDestroyed

Event<void (const UUID &)> OnResourceDestroyed

Called when the resource has been destroyed.

Provides UUID of the destroyed resource.

OnResourceModified

Event<void (const HResource &)> OnResourceModified

Called when the internal resource the handle is pointing to has changed.

Internal

Methods

CreateResourceHandle

HResource CreateResourceHandle(const SPtr<Resource> &resource)

Creates a new resource handle from a resource pointer.

CreateResourceHandle

HResource CreateResourceHandle(const SPtr<Resource> &resource, const UUID &resourceId)

Creates a new resource handle from a resource pointer, with a user defined UUID.

GetOrCreateResourceHandle

HResource GetOrCreateResourceHandle(const UUID &resourceId)

Returns an existing handle for the specified UUID if one exists, or creates a new one.

UpdateResourcesFromPackage

void UpdateResourcesFromPackage(const UPtr<PackageWriteLock> &packageWriteLock)

Updates all resources from the resource data in the package locked by the provided write lock.

This means if a resource is already loaded by the resource system, the resource will be retrieved from the package and handle to the resource updated with the new resource. This may involve loading the resource, if the new package doesn't have the resource loaded. If the resource is loaded in the package, but not marked as loaded by the resource system, it will be unloaded from the package.

UpdateHandle

void UpdateHandle(HResource &handle, const SPtr<Resource> &resource)

Updates an existing resource handle with a new resource.

Caller must ensure that new resource type matches the original resource type.

DestroyHandleData

void DestroyHandleData(ResourceHandleData &handleData)

Triggered when the last resource handle for a particular resource goes out of scope.

Protected

Methods

~Module<T>

virtual ~Module<T>() = default

OnStartUp

virtual void OnStartUp()

Override if you want your module to be notified once it has been constructed and started.

OnShutDown

virtual void OnShutDown()

Override if you want your module to be notified just before it is deleted.

staticInstanceInternal

static T *&InstanceInternal()

Returns a singleton instance of this module.

staticIsDestroyed

static bool &IsDestroyed()

Checks has the Module been shut down.

staticIsStartedUp

static bool &IsStartedUp()

Checks has the Module been started up.

Private

Methods

GetLoadProgressRecursive

void GetLoadProgressRecursive(const HResource &resource, UnorderedMap<UUID, LoadProgress> &loadProgressMap)

Calculates the load progress of the provided resource and appends the progress information to the .

Then calls this method recursively for any in-progress dependency loads. If the resource is already fully loaded, or not being loaded at all the LoadProgress structure will not contain a valid size value.

LoadFromPackage

HResource LoadFromPackage(UPtr<PackageReadLock> packageReadLock, const UUID &resourceId, const ResourceLoadOptions &loadOptions)

Performs a load of the provided resource from the package locked by the write lock.

If requested this will also trigger a load of all dependencies of the resource. Triggers TryFinalizeLoad() when a resource or any of its dependencies complete loading.

This is an internal method to be shared by public Load() overloads.

TryFinalizeLoad

void TryFinalizeLoad(const SPtr<InProgressLoadInformation> &inProgressLoadInformation)

Checks if the provided in-progress load has completed any finalizes the operation.

Operation is deemed complete once its primary resource and all dependencies (and their dependencies) have finished loading. At that point we will clear the in-progress load map and add the resource into the loaded resource map. External code will be notified that load completed, and if any other resource is waiting on this resource to finish loading, they will be notified so they may try to finalize their operations as well.

Destroy

void Destroy(ResourceHandleData &handleData)

Destroys a resource, freeing its memory.

Fields

mLoadedResourceMutex

Mutex mLoadedResourceMutex

mResourceHandleMutex

Mutex mResourceHandleMutex

mHandles

UnorderedMap<UUID, ResourceHandleData *> mHandles

mLoadedResourceInformation

UnorderedMap<UUID, UPtr<LoadedResourceInformation>> mLoadedResourceInformation

mInProgressLoadInformation

UnorderedMap<UUID, TInlineArray<SPtr<InProgressLoadInformation>, 1>> mInProgressLoadInformation

mDependantResourceLoads

UnorderedMap<UUID, TInlineArray<SPtr<InProgressLoadInformation>, 4>> mDependantResourceLoads