3#include <Foundation/Logging/Log.h>
5template <
typename ResourceType>
6ResourceType* plResourceManager::GetResource(
plStringView sResourceID,
bool bIsReloadable)
8 return static_cast<ResourceType*
>(GetResource(plGetStaticRTTI<ResourceType>(), sResourceID, bIsReloadable));
11template <
typename ResourceType>
15 PL_LOCK(s_ResourceMutex);
19template <
typename ResourceType>
25 PL_LOCK(s_ResourceMutex);
31 hResource.m_pResource->SetLoadingFallbackResource(hLoadingFallback);
37template <
typename ResourceType>
44 PL_LOCK(s_ResourceMutex);
46 const plRTTI* pRtti = FindResourceTypeOverride(plGetStaticRTTI<ResourceType>(), sResourceID);
48 if (GetLoadedResources()[pRtti].m_Resources.TryGetValue(sResourceHash, pResource))
54template <
typename ResourceType,
typename DescriptorType>
57 static_assert(std::is_rvalue_reference<DescriptorType&&>::value,
"Please std::move the descriptor into this function");
59 PL_LOG_BLOCK(
"plResourceManager::CreateResource", sResourceID);
61 PL_LOCK(s_ResourceMutex);
66 pResource->SetResourceDescription(sResourceDescription);
69 PL_ASSERT_DEV(pResource->GetLoadingState() == plResourceState::Unloaded,
"CreateResource was called on a resource that is already created");
74 auto localDescriptor = std::move(descriptor);
76 pResource->VerifyAfterCreateResource(ld);
79 PL_ASSERT_DEV(pResource->GetLoadingState() != plResourceState::Unloaded,
"CreateResource did not set the loading state properly.");
86template <
typename ResourceType,
typename DescriptorType>
90 PL_LOCK(s_ResourceMutex);
92 if (!hResource.IsValid())
102 PL_ASSERT_DEV(hResource.IsValid(),
"Cannot acquire a resource through an invalid handle!");
106 PL_ASSERT_DEBUG(pResource->GetDynamicRTTI()->
IsDerivedFrom(pType),
107 "The requested resource does not have the same type ('{0}') as the resource handle ('{1}').", pResource->GetDynamicRTTI()->
GetTypeName(),
114template <
typename ResourceType>
118 PL_ASSERT_DEV(hResource.IsValid(),
"Cannot acquire a resource through an invalid handle!");
120#if PL_ENABLED(PL_COMPILE_FOR_DEVELOPMENT)
121 const plResource* pCurrentlyUpdatingContent = plResource::GetCurrentlyUpdatingContent();
122 if (pCurrentlyUpdatingContent !=
nullptr)
124 PL_LOCK(s_ResourceMutex);
125 PL_ASSERT_DEV(mode == plResourceAcquireMode::PointerOnly || IsResourceTypeAcquireDuringUpdateContentAllowed(pCurrentlyUpdatingContent->GetDynamicRTTI(), plGetStaticRTTI<ResourceType>()),
126 "Trying to acquire a resource of type '{0}' during '{1}::UpdateContent()'. This has to be enabled by calling "
127 "plResourceManager::AllowResourceTypeAcquireDuringUpdateContent<{1}, {0}>(); at engine startup, for example in "
128 "plGameApplication::Init_SetupDefaultResources().",
129 plGetStaticRTTI<ResourceType>()->GetTypeName(), pCurrentlyUpdatingContent->GetDynamicRTTI()->
GetTypeName());
133 ResourceType* pResource = (ResourceType*)hResource.m_hTypeless.m_pResource;
136 PL_ASSERT_DEBUG(pResource->GetDynamicRTTI()->template IsDerivedFrom<ResourceType>(),
137 "The requested resource does not have the same type ('{0}') as the resource handle ('{1}').", pResource->GetDynamicRTTI()->GetTypeName(),
138 plGetStaticRTTI<ResourceType>()->GetTypeName());
142 mode = plResourceAcquireMode::BlockTillLoaded;
145 if (mode == plResourceAcquireMode::PointerOnly)
147 if (out_pAcquireResult)
148 *out_pAcquireResult = plResourceAcquireResult::Final;
156 pResource->m_LastAcquire = GetLastFrameUpdate();
158 if (pResource->GetLoadingState() != plResourceState::LoadedResourceMissing)
160 if (pResource->GetLoadingState() != plResourceState::Loaded)
163 InternalPreloadResource(pResource, mode >= plResourceAcquireMode::BlockTillLoaded);
165 if (mode == plResourceAcquireMode::AllowLoadingFallback &&
169 if (out_pAcquireResult)
170 *out_pAcquireResult = plResourceAcquireResult::LoadingFallback;
177 if (pResource->m_hLoadingFallback.IsValid())
178 return (ResourceType*)
BeginAcquireResource(pResource->m_hLoadingFallback, plResourceAcquireMode::BlockTillLoaded);
179 else if (hFallbackResource.
IsValid())
180 return (ResourceType*)
BeginAcquireResource(hFallbackResource, plResourceAcquireMode::BlockTillLoaded);
185 EnsureResourceLoadingState(pResource, plResourceState::Loaded);
192 if (IsQueuedForLoading(pResource) ==
false && pResource->GetNumQualityLevelsLoadable() > 0)
193 InternalPreloadResource(pResource,
false);
197 if (pResource->GetLoadingState() == plResourceState::LoadedResourceMissing)
204 if (out_pAcquireResult)
205 *out_pAcquireResult = plResourceAcquireResult::MissingFallback;
211 if (mode != plResourceAcquireMode::AllowLoadingFallback_NeverFail && mode != plResourceAcquireMode::BlockTillLoaded_NeverFail)
213 PL_REPORT_FAILURE(
"The resource '{0}' of type '{1}' is missing and no fallback is available", pResource->GetResourceID(),
214 plGetStaticRTTI<ResourceType>()->GetTypeName());
217 if (out_pAcquireResult)
218 *out_pAcquireResult = plResourceAcquireResult::None;
223 if (out_pAcquireResult)
224 *out_pAcquireResult = plResourceAcquireResult::Final;
230template <
typename ResourceType>
243template <
typename ResourceType>
246 const plRTTI* pBaseType = plGetStaticRTTI<ResourceType>();
248 auto& container = GetLoadedResourceOfTypeTempContainer();
259 for (
auto itType = GetLoadedResources().GetIterator(); itType.IsValid(); itType.Next())
261 const plRTTI* pDerivedType = itType.Key();
265 const LoadedResources& lr = GetLoadedResources()[pDerivedType];
267 container.Reserve(container.GetCount() + lr.m_Resources.
GetCount());
269 for (
auto itResource : lr.m_Resources)
271 container.PushBack(itResource.Value());
276 return loadedResourcesLock;
279template <
typename ResourceType>
302template <
typename ResourceType>
308template <
typename ResourceType>
311 PL_LOCK(s_ResourceMutex);
313 GetResourceTypeLoaders()[plGetStaticRTTI<ResourceType>()] = pCreator;
316template <
typename ResourceType>
324template <
typename ResourceType>
327 GetResourceTypeInfo(plGetStaticRTTI<ResourceType>()).m_bIncrementalUnload = bActive;
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashTable_inl.h:343
Provides access to an object while managing a lock (e.g. a mutex) that ensures that during its lifeti...
Definition LockedObject.h:7
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
PL_ALWAYS_INLINE plStringView GetTypeName() const
Returns the name of this type.
Definition RTTI.h:48
PL_ALWAYS_INLINE bool IsDerivedFrom(const plRTTI *pBaseType) const
Returns true if this type is derived from the given type (or of the same type).
Definition RTTI.h:60
The base class for all resources.
Definition Resource.h:10
static void EndAcquireResource(ResourceType *pResource)
Needs to be called in concert with BeginAcquireResource() after accessing a resource has been finishe...
Definition ResourceManager_inl.h:231
static bool IsExportModeEnabled()
Returns whether export mode is active.
Definition ResourceManager.cpp:902
static const plTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeMissingFallback()
Definition ResourceManager.h:391
static void SetResourceTypeLoader(plResourceTypeLoader *pCreator)
Sets the resource loader to use for the given resource type.
Definition ResourceManager_inl.h:309
static plLockedObject< plMutex, plDynamicArray< plResource * > > GetAllResourcesOfType()
Retrieves an array of pointers to resources of the indicated type which are loaded at the moment....
Definition ResourceManager_inl.h:244
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 e...
Definition ResourceManager_inl.h:317
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 BeginAcquireReso...
Definition ResourceManager_inl.h:115
static plUInt32 GetForceNoFallbackAcquisition()
If the returned number is greater 0 the resource manager treats plResourceAcquireMode::AllowLoadingFa...
Definition ResourceManager.cpp:919
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 indicat...
Definition ResourceManager_inl.h:280
static plResource * BeginAcquireResourcePointer(const plRTTI *pType, const plTypelessResourceHandle &hResource)
Same as BeginAcquireResource but only for the base resource pointer.
Definition ResourceManager_inl.h:100
static void SetIncrementalUnloadForResourceType(bool bActive)
If set to 'false' resources of the given type will not be incrementally unloaded in the background,...
Definition ResourceManager_inl.h:325
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.
Definition ResourceManager_inl.h:88
static plTypedResourceHandle< ResourceType > LoadResource(plStringView sResourceID)
Returns a handle to the requested resource. szResourceID must uniquely identify the resource,...
Definition ResourceManager_inl.h:12
static plUInt32 ReloadResourcesOfType(bool bForce)
Goes through all resources of the given type and makes sure they are reloaded, if they have changed....
Definition ResourceManager_inl.h:303
static plTypedResourceHandle< ResourceType > GetExistingResource(plStringView sResourceID)
Returns a handle to the resource with the given ID. If the resource does not exist,...
Definition ResourceManager_inl.h:38
static const plTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeLoadingFallback()
Definition ResourceManager.h:375
static void EndAcquireResourcePointer(plResource *pResource)
Same as EndAcquireResource but without the template parameter. See also BeginAcquireResourcePointer.
Definition ResourceManager_inl.h:237
static plTypedResourceHandle< ResourceType > CreateResource(plStringView sResourceID, DescriptorType &&descriptor, plStringView sResourceDescription=nullptr)
Creates a resource from a descriptor.
Definition ResourceManager_inl.h:55
Base class for all resource loaders.
Definition ResourceTypeLoader.h:29
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
The plTypedResourceHandle controls access to an plResource.
Definition ResourceHandle.h:136
PL_ALWAYS_INLINE bool IsValid() const
Returns whether the handle stores a valid pointer to a resource.
Definition ResourceHandle.h:206
The typeless implementation of resource handles. A typed interface is provided by plTypedResourceHand...
Definition ResourceHandle.h:32
@ IsCreatedResource
When this is set, the resource was created and not loaded from file.
Definition Declarations.h:70
Describes in which loading state a resource currently is, and how many different quality levels there...
Definition Declarations.h:102