Plasma Engine  2.0
Loading...
Searching...
No Matches
plResourceManager Class Reference

The central class for managing all types derived from plResource. More...

#include <ResourceManager.h>

Static Public Member Functions

Events
static const plEvent< const plResourceEvent &, plMutex > & GetResourceEvents ()
 
static const plEvent< const plResourceManagerEvent &, plMutex > & GetManagerEvents ()
 Events for the resource manager that affect broader things.
 
static void BroadcastExistsEvent ()
 Goes through all existing resources and broadcasts the 'Exists' event.
 
Loading and creating resources
template<typename ResourceType >
static plTypedResourceHandle< ResourceType > LoadResource (plStringView sResourceID)
 Returns a handle to the requested resource. szResourceID must uniquely identify the resource, different spellings / casing will result in different resources.
 
template<typename ResourceType >
static plTypedResourceHandle< ResourceType > LoadResource (plStringView sResourceID, plTypedResourceHandle< ResourceType > hLoadingFallback)
 Same as LoadResource(), but additionally allows to set a priority on the resource and a custom fallback resource for this instance.
 
static plTypelessResourceHandle LoadResourceByType (const plRTTI *pResourceType, plStringView sResourceID)
 Same as LoadResource(), but instead of a template argument, the resource type to use is given as plRTTI info. Returns a typeless handle due to the missing template argument.
 
static bool IsAnyLoadingInProgress ()
 Checks whether any resource loading is in progress.
 
static plString GenerateUniqueResourceID (plStringView sResourceIDPrefix)
 Generates a unique resource ID with the given prefix.
 
template<typename ResourceType , typename DescriptorType >
static plTypedResourceHandle< ResourceType > CreateResource (plStringView sResourceID, DescriptorType &&descriptor, plStringView sResourceDescription=nullptr)
 Creates a resource from a descriptor.
 
template<typename ResourceType , typename DescriptorType >
static plTypedResourceHandle< ResourceType > GetOrCreateResource (plStringView sResourceID, DescriptorType &&descriptor, plStringView sResourceDescription=nullptr)
 Returns a handle to the resource with the given ID if it exists or creates it from a descriptor.
 
template<typename ResourceType >
static plTypedResourceHandle< ResourceType > GetExistingResource (plStringView sResourceID)
 Returns a handle to the resource with the given ID. If the resource does not exist, the handle is invalid.
 
static plTypelessResourceHandle GetExistingResourceByType (const plRTTI *pResourceType, plStringView sResourceID)
 Same as GetExistingResourceByType() but allows to specify the resource type as an plRTTI.
 
template<typename ResourceType >
static plTypedResourceHandle< ResourceType > GetExistingResourceOrCreateAsync (plStringView sResourceID, plUniquePtr< plResourceTypeLoader > &&pLoader, plTypedResourceHandle< ResourceType > hLoadingFallback={})
 
static plTypelessResourceHandle GetExistingResourceOrCreateAsync (const plRTTI *pResourceType, plStringView sResourceID, plUniquePtr< plResourceTypeLoader > &&pLoader)
 
static void PreloadResource (const plTypelessResourceHandle &hResource)
 Triggers loading of the given resource. tShouldBeAvailableIn specifies how long the resource is not yet needed, thus allowing other resources to be loaded first. This is only a hint and there are no guarantees when the resource is available.
 
static void ForceLoadResourceNow (const plTypelessResourceHandle &hResource)
 Similar to locking a resource with 'BlockTillLoaded' acquire mode, but can be done with a typeless handle and does not return a result.
 
static plResourceState GetLoadingState (const plTypelessResourceHandle &hResource)
 Returns the current loading state of the given resource.
 
Reloading resources
static plUInt32 ReloadAllResources (bool bForce)
 Goes through all resources and makes sure they are reloaded, if they have changed. If bForce is true, all resources are updated, even if there is no indication that they have changed.
 
template<typename ResourceType >
static plUInt32 ReloadResourcesOfType (bool bForce)
 Goes through all resources of the given type and makes sure they are reloaded, if they have changed. If bForce is true, resources are updated, even if there is no indication that they have changed.
 
static plUInt32 ReloadResourcesOfType (const plRTTI *pType, bool bForce)
 Goes through all resources of the given type and makes sure they are reloaded, if they have changed. If bForce is true, resources are updated, even if there is no indication that they have changed.
 
template<typename ResourceType >
static bool ReloadResource (const plTypedResourceHandle< ResourceType > &hResource, bool bForce)
 Reloads only the one specific resource. If bForce is true, it is updated, even if there is no indication that it has changed.
 
static bool ReloadResource (const plRTTI *pType, const plTypelessResourceHandle &hResource, bool bForce)
 Reloads only the one specific resource. If bForce is true, it is updated, even if there is no indication that it has changed.
 
static void UpdateResourceWithCustomLoader (const plTypelessResourceHandle &hResource, plUniquePtr< plResourceTypeLoader > &&pLoader)
 Calls ReloadResource() on the given resource, but makes sure that the reload happens with the given custom loader.
 
static void RestoreResource (const plTypelessResourceHandle &hResource)
 Removes the 'PreventFileReload' flag and forces a reload on the resource.
 
Acquiring resources
template<typename ResourceType >
static ResourceType * BeginAcquireResource (const plTypedResourceHandle< ResourceType > &hResource, plResourceAcquireMode mode, const plTypedResourceHandle< ResourceType > &hLoadingFallback=plTypedResourceHandle< ResourceType >(), plResourceAcquireResult *out_pAcquireResult=nullptr)
 Acquires a resource pointer from a handle. Prefer to use plResourceLock, which wraps BeginAcquireResource / EndAcquireResource.
 
static plResourceBeginAcquireResourcePointer (const plRTTI *pType, const plTypelessResourceHandle &hResource)
 Same as BeginAcquireResource but only for the base resource pointer.
 
template<typename ResourceType >
static void EndAcquireResource (ResourceType *pResource)
 Needs to be called in concert with BeginAcquireResource() after accessing a resource has been finished. Prefer to use plResourceLock instead.
 
static void EndAcquireResourcePointer (plResource *pResource)
 Same as EndAcquireResource but without the template parameter. See also BeginAcquireResourcePointer.
 
static void ForceNoFallbackAcquisition (plUInt32 uiNumFrames=0xFFFFFFFF)
 Forces the resource manager to treat plResourceAcquireMode::AllowLoadingFallback as plResourceAcquireMode::BlockTillLoaded on BeginAcquireResource.
 
static plUInt32 GetForceNoFallbackAcquisition ()
 If the returned number is greater 0 the resource manager treats plResourceAcquireMode::AllowLoadingFallback as plResourceAcquireMode::BlockTillLoaded on BeginAcquireResource.
 
template<typename ResourceType >
static plLockedObject< plMutex, plDynamicArray< plResource * > > GetAllResourcesOfType ()
 Retrieves an array of pointers to resources of the indicated type which are loaded at the moment. Destroy the returned object as soon as possible as it holds the entire resource manager locked.
 
Miscellaneous
static plMutexGetMutex ()
 Returns the resource manager mutex. Allows to lock the manager on a thread when multiple operations need to be done in sequence.
 
static void PerFrameUpdate ()
 Must be called once per frame for some bookkeeping.
 
static void EngineAboutToShutdown ()
 Makes sure that no further resource loading will take place.
 
static void ResetAllResources ()
 Calls plResource::ResetResource() on all resources.
 
static void SetResourceLowResData (const plTypelessResourceHandle &hResource, plStreamReader *pStream)
 Calls plResource::UpdateContent() to fill the resource with 'low resolution' data.
 
Type specific loaders
static void SetDefaultResourceLoader (plResourceTypeLoader *pDefaultLoader)
 Sets the resource loader to use when no type specific resource loader is available.
 
static plResourceTypeLoaderGetDefaultResourceLoader ()
 Returns the resource loader to use when no type specific resource loader is available.
 
template<typename ResourceType >
static void SetResourceTypeLoader (plResourceTypeLoader *pCreator)
 Sets the resource loader to use for the given resource type.
 
Named resources
static void RegisterNamedResource (plStringView sLookupName, plStringView sRedirectionResource)
 Registers a 'named' resource. When a resource is looked up using szLookupName, the lookup will be redirected to szRedirectionResource.
 
static void UnregisterNamedResource (plStringView sLookupName)
 Removes a previously registered name from the redirection table.
 
Asset system interaction
static void RegisterResourceForAssetType (plStringView sAssetTypeName, const plRTTI *pResourceType)
 Registers which resource type to use to load an asset with the given type name.
 
static const plRTTIFindResourceForAssetType (plStringView sAssetTypeName)
 Returns the resource type that was registered to handle the given asset type for loading. nullptr if no resource type was registered for this asset type.
 
Export mode
static void EnableExportMode (bool bEnable)
 Enables export mode. In this mode the resource manager will assert when it actually tries to load a resource. This can be useful when exporting resource handles but the actual resource content is not needed.
 
static bool IsExportModeEnabled ()
 Returns whether export mode is active.
 
template<typename ResourceType >
static plTypedResourceHandle< ResourceType > GetResourceHandleForExport (plStringView sResourceID)
 Creates a resource handle for the given resource ID. This method can only be used if export mode is enabled. Internally it will create a resource but does not load the content. This way it can be ensured that the resource handle is always only the size of a pointer.
 
Resource Type Overrides
static void RegisterResourceOverrideType (const plRTTI *pDerivedTypeToUse, plDelegate< bool(const plStringBuilder &)> overrideDecider)
 Registers a resource type to be used instead of any of it's base classes, when loading specific data.
 
static void UnregisterResourceOverrideType (const plRTTI *pDerivedTypeToUse)
 Unregisters pDerivedTypeToUse as an override resource.
 

Friends

class plResourceManagerState
 
class plResource
 
class plResourceManagerWorkerDataLoad
 
class plResourceManagerWorkerUpdateContent
 
class plResourceHandleReadContext
 

Resource Fallbacks

using ResourceCleanupCB = plDelegate<void()>
 
template<typename RESOURCE_TYPE >
static void SetResourceTypeLoadingFallback (const plTypedResourceHandle< RESOURCE_TYPE > &hResource)
 Specifies which resource to use as a loading fallback for the given type, while a resource is not yet loaded.
 
template<typename RESOURCE_TYPE >
static const plTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeLoadingFallback ()
 
template<typename RESOURCE_TYPE >
static void SetResourceTypeMissingFallback (const plTypedResourceHandle< RESOURCE_TYPE > &hResource)
 Specifies which resource to use as a missing fallback for the given type, when a resource cannot be loaded.
 
template<typename RESOURCE_TYPE >
static const plTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeMissingFallback ()
 
static void AddResourceCleanupCallback (ResourceCleanupCB cb)
 [internal] Used by plResource to register a cleanup function to be called at resource manager shutdown.
 
static void ClearResourceCleanupCallback (ResourceCleanupCB cb)
 
static void ExecuteAllResourceCleanupCallbacks ()
 This will clear ALL resources that were registered as 'missing' or 'loading' fallback resources. This is called early during system shutdown to clean up resources.
 

Unloading resources

static plUInt32 FreeAllUnusedResources ()
 Deallocates all resources whose refcount has reached 0. Returns the number of deleted resources.
 
static plUInt32 FreeUnusedResources (plTime timeout, plTime lastAcquireThreshold)
 Deallocates resources whose refcount has reached 0. Returns the number of deleted resources.
 
static void SetAutoFreeUnused (plTime timeout, plTime lastAcquireThreshold)
 If timeout is not zero, FreeUnusedResources() is called once every frame with the given parameters.
 
template<typename ResourceType >
static void SetIncrementalUnloadForResourceType (bool bActive)
 If set to 'false' resources of the given type will not be incrementally unloaded in the background, when they are not referenced anymore.
 
template<typename TypeBeingUpdated , typename TypeItWantsToAcquire >
static void AllowResourceTypeAcquireDuringUpdateContent ()
 
static void AllowResourceTypeAcquireDuringUpdateContent (const plRTTI *pTypeBeingUpdated, const plRTTI *pTypeItWantsToAcquire)
 
static bool IsResourceTypeAcquireDuringUpdateContentAllowed (const plRTTI *pTypeBeingUpdated, const plRTTI *pTypeItWantsToAcquire)
 

Resource Priorities

template<typename RESOURCE_TYPE >
static void SetResourceTypeDefaultPriority (plResourcePriority priority)
 Specifies which resource to use as a loading fallback for the given type, while a resource is not yet loaded.
 

Detailed Description

The central class for managing all types derived from plResource.

Member Function Documentation

◆ BeginAcquireResource()

template<typename ResourceType >
ResourceType * plResourceManager::BeginAcquireResource ( const plTypedResourceHandle< ResourceType > & hResource,
plResourceAcquireMode mode,
const plTypedResourceHandle< ResourceType > & hLoadingFallback = plTypedResourceHandle<ResourceType>(),
plResourceAcquireResult * out_pAcquireResult = nullptr )
static

Acquires a resource pointer from a handle. Prefer to use plResourceLock, which wraps BeginAcquireResource / EndAcquireResource.

Parameters
hResourceThe resource to acquire
modeThe desired way to acquire the resource. See plResourceAcquireMode for details.
hLoadingFallbackA custom fallback resource that should be returned if hResource is not yet available. Allows to use domain specific knowledge to get a better fallback.
PriorityAllows to adjust the priority of the resource. This will affect how fast the resource is loaded, in case it is not yet available.
out_AcquireResultReturns how successful the acquisition was. See plResourceAcquireResult for details.

◆ BroadcastExistsEvent()

void plResourceManager::BroadcastExistsEvent ( )
static

Goes through all existing resources and broadcasts the 'Exists' event.

Used to announce all currently existing resources to interested event listeners (ie tools).

◆ ClearResourceCleanupCallback()

void plResourceManager::ClearResourceCleanupCallback ( ResourceCleanupCB cb)
static

◆ CreateResource()

template<typename ResourceType , typename DescriptorType >
plTypedResourceHandle< ResourceType > plResourceManager::CreateResource ( plStringView sResourceID,
DescriptorType && descriptor,
plStringView sResourceDescription = nullptr )
static

Creates a resource from a descriptor.

Parameters
szResourceIDThe unique ID by which the resource is identified. E.g. in GetExistingResource()
descriptorA type specific descriptor that holds all the information to create the resource.
szResourceDescriptionAn optional description that might help during debugging. Often a human readable name or path is stored here, to make it easier to identify this resource.

◆ GenerateUniqueResourceID()

plString plResourceManager::GenerateUniqueResourceID ( plStringView sResourceIDPrefix)
static

Generates a unique resource ID with the given prefix.

Provide a prefix that is preferably not used anywhere else (i.e., closely related to your code). If the prefix is not also used to manually generate resource IDs, this function is guaranteed to return a unique resource ID.

◆ GetExistingResource()

template<typename ResourceType >
plTypedResourceHandle< ResourceType > plResourceManager::GetExistingResource ( plStringView sResourceID)
static

Returns a handle to the resource with the given ID. If the resource does not exist, the handle is invalid.

Use this if a resource needs to be created procedurally (with CreateResource()), but might already have been created. If the returned handle is invalid, then just go through the resource creation step.

◆ GetOrCreateResource()

template<typename ResourceType , typename DescriptorType >
plTypedResourceHandle< ResourceType > plResourceManager::GetOrCreateResource ( plStringView sResourceID,
DescriptorType && descriptor,
plStringView sResourceDescription = nullptr )
static

Returns a handle to the resource with the given ID if it exists or creates it from a descriptor.

Parameters
szResourceIDThe unique ID by which the resource is identified. E.g. in GetExistingResource()
descriptorA type specific descriptor that holds all the information to create the resource.
szResourceDescriptionAn optional description that might help during debugging. Often a human readable name or path is stored here, to make it easier to identify this resource.

◆ GetResourceEvents()

const plEvent< const plResourceEvent &, plMutex > & plResourceManager::GetResourceEvents ( )
static

Events on individual resources. Subscribe to this to get a notification for events happening on any resource. If you are only interested in events for a specific resource, subscribe on directly on that instance.

◆ GetResourceTypeLoadingFallback()

template<typename RESOURCE_TYPE >
static const plTypedResourceHandle< RESOURCE_TYPE > & plResourceManager::GetResourceTypeLoadingFallback ( )
inlinestatic

◆ GetResourceTypeMissingFallback()

template<typename RESOURCE_TYPE >
static const plTypedResourceHandle< RESOURCE_TYPE > & plResourceManager::GetResourceTypeMissingFallback ( )
inlinestatic

◆ LoadResource() [1/2]

template<typename ResourceType >
plTypedResourceHandle< ResourceType > plResourceManager::LoadResource ( plStringView sResourceID)
static

Returns a handle to the requested resource. szResourceID must uniquely identify the resource, different spellings / casing will result in different resources.

After the call to this function the resource definitely exists in memory. Upon access through BeginAcquireResource / plResourceLock the resource will be loaded. If it is not possible to load the resource it will change to a 'missing' state. If the code accessing the resource cannot handle that case, the application will 'terminate' (that means crash).

◆ LoadResource() [2/2]

template<typename ResourceType >
plTypedResourceHandle< ResourceType > plResourceManager::LoadResource ( plStringView sResourceID,
plTypedResourceHandle< ResourceType > hLoadingFallback )
static

Same as LoadResource(), but additionally allows to set a priority on the resource and a custom fallback resource for this instance.

Pass in plResourcePriority::Unchanged, if you only want to specify a custom fallback resource. If a resource priority is specified, the target resource will get that priority. If a valid fallback resource is specified, the resource will store that as its instance specific fallback resource. This will be used when trying to acquire the resource later.

◆ RegisterNamedResource()

void plResourceManager::RegisterNamedResource ( plStringView sLookupName,
plStringView sRedirectionResource )
static

Registers a 'named' resource. When a resource is looked up using szLookupName, the lookup will be redirected to szRedirectionResource.

This can be used to register a resource under an easier to use name. For example one can register "MenuBackground" as the name for "{ E50DCC85-D375-4999-9CFE-42F1377FAC85 }". If the lookup name already exists, it will be overwritten.

◆ RegisterResourceOverrideType()

plResourceManager::RegisterResourceOverrideType ( const plRTTI * pDerivedTypeToUse,
plDelegate< bool(const plStringBuilder &)> overrideDecider )
static

Registers a resource type to be used instead of any of it's base classes, when loading specific data.

When resource B is derived from A it can be registered to be instantiated when loading data, even if the code specifies to use a resource of type A. Whenever LoadResource<A>() is executed, the registered callback OverrideDecider is run to figure out whether B should be instantiated instead. If OverrideDecider returns true, B is used.

OverrideDecider is given the resource ID after it has been resolved by the plFileSystem. So it has to be able to make its decision from the file path, name or extension. The override is registered for all base classes of pDerivedTypeToUse, in case the derivation hierarchy is longer.

Without calling this at startup, a derived resource type has to be manually requested in code.

◆ ResetAllResources()

void plResourceManager::ResetAllResources ( )
static

Calls plResource::ResetResource() on all resources.

This is mostly for usage in tools to reset resource whose state can be modified at runtime, to reset them to their original state.

◆ RestoreResource()

void plResourceManager::RestoreResource ( const plTypelessResourceHandle & hResource)
static

Removes the 'PreventFileReload' flag and forces a reload on the resource.

See also
UpdateResourceWithCustomLoader()

◆ SetResourceLowResData()

void plResourceManager::SetResourceLowResData ( const plTypelessResourceHandle & hResource,
plStreamReader * pStream )
static

Calls plResource::UpdateContent() to fill the resource with 'low resolution' data.

This will early out, if the resource has gotten low-res data before. The resource itself may ignore the data, if it has already gotten low/high res data before.

The typical use case is, that some other piece of code stores a low-res version of a resource to be able to get a resource into a usable state. For instance, a material may store low resolution texture data for every texture that it references. Then when 'loading' the textures, it can pass this low-res data to the textures, such that rendering can give decent results right away. If the textures have already been loaded before, or some other material already had low-res data, the call exits quickly.

◆ SetResourceTypeLoader()

template<typename ResourceType >
plResourceManager::SetResourceTypeLoader< plConfigFileResource > ( plResourceTypeLoader * pCreator)
static

Sets the resource loader to use for the given resource type.

Note
This is bound to one specific type. Derived types do not inherit the type loader.

◆ SetResourceTypeMissingFallback()

template<typename RESOURCE_TYPE >
plResourceManager::SetResourceTypeMissingFallback< plProcGenGraphResource > ( const plTypedResourceHandle< RESOURCE_TYPE > & hResource)
inlinestatic

Specifies which resource to use as a missing fallback for the given type, when a resource cannot be loaded.

Note
If no missing fallback is specified, trying to load a resource that does not exist will assert at runtime.

◆ UnregisterResourceOverrideType()

void plResourceManager::UnregisterResourceOverrideType ( const plRTTI * pDerivedTypeToUse)
static

Unregisters pDerivedTypeToUse as an override resource.

See also
RegisterResourceOverrideType()

◆ UpdateResourceWithCustomLoader()

void plResourceManager::UpdateResourceWithCustomLoader ( const plTypelessResourceHandle & hResource,
plUniquePtr< plResourceTypeLoader > && pLoader )
static

Calls ReloadResource() on the given resource, but makes sure that the reload happens with the given custom loader.

Use this e.g. with a plResourceLoaderFromMemory to replace an existing resource with new data that was created on-the-fly. Using this function will set the 'PreventFileReload' flag on the resource and thus prevent further reload actions.

See also
RestoreResource()

The documentation for this class was generated from the following files: