ezEngine  Milestone 9
ResourceManager.h
1 #pragma once
2 
3 #include <Core/ResourceManager/Implementation/WorkerTasks.h>
4 #include <Core/ResourceManager/Resource.h>
5 #include <Core/ResourceManager/ResourceHandle.h>
6 #include <Core/ResourceManager/ResourceTypeLoader.h>
8 #include <Foundation/Containers/HashTable.h>
9 
11 class EZ_CORE_DLL ezResourceManager
12 {
15 
16 public:
20 
23 
27  static void BroadcastExistsEvent();
28 
32 
33 public:
40  template <typename ResourceType>
41  static ezTypedResourceHandle<ResourceType> LoadResource(const char* szResourceID);
42 
50  template <typename ResourceType>
51  static ezTypedResourceHandle<ResourceType> LoadResource(const char* szResourceID, ezResourcePriority Priority,
52  ezTypedResourceHandle<ResourceType> hLoadingFallback);
53 
54 
57  static ezTypelessResourceHandle LoadResourceByType(const ezRTTI* pResourceType, const char* szResourceID);
58 
63  static ezString GenerateUniqueResourceID(const char* szResourceIDPrefix);
64 
71  template <typename ResourceType, typename DescriptorType>
72  static ezTypedResourceHandle<ResourceType> CreateResource(const char* szResourceID, DescriptorType&& descriptor,
73  const char* szResourceDescription = nullptr);
74 
79  template <typename ResourceType>
80  static ezTypedResourceHandle<ResourceType> GetExistingResource(const char* szResourceID);
81 
84  static void PreloadResource(const ezTypelessResourceHandle& hResource, ezTime tShouldBeAvailableIn);
85 
93  static bool FinishLoadingOfResources();
94 
98 
99 public:
102  static ezUInt32 ReloadAllResources(bool bForce);
103 
106  template <typename ResourceType>
107  static ezUInt32 ReloadResourcesOfType(bool bForce);
108 
111  static ezUInt32 ReloadResourcesOfType(const ezRTTI* pType, bool bForce);
112 
114  template <typename ResourceType>
115  static bool ReloadResource(const ezTypedResourceHandle<ResourceType>& hResource, bool bForce);
116 
123  static void UpdateResourceWithCustomLoader(const ezTypelessResourceHandle& hResource, ezUniquePtr<ezResourceTypeLoader>&& loader);
124 
128  template <typename ResourceType>
129  static void RestoreResource(const ezTypedResourceHandle<ResourceType>& hResource);
130 
134 
135 public:
145  template <typename ResourceType>
146  static ResourceType*
147  BeginAcquireResource(const ezTypedResourceHandle<ResourceType>& hResource, ezResourceAcquireMode mode,
149  ezResourcePriority Priority = ezResourcePriority::Unchanged, ezResourceAcquireResult* out_AcquireResult = nullptr);
150 
153  template <typename ResourceType>
154  static void EndAcquireResource(ResourceType* pResource);
155 
159 
160 public:
166  static ezUInt32 FreeUnusedResources(bool bFreeAllUnused);
167 
171 
172 public:
175  static ezMutex& GetMutex() { return s_ResourceMutex; }
176 
178  static void PerFrameUpdate();
179 
181  static void EngineAboutToShutdown();
182 
186  static void ResetAllResources();
187 
197  static void SetResourceLowResData(const ezTypelessResourceHandle& hResource, ezStreamReader* pStream);
198 
202 
203 public:
205  static void SetDefaultResourceLoader(ezResourceTypeLoader* pDefaultLoader);
206 
208  static ezResourceTypeLoader* GetDefaultResourceLoader() { return s_pDefaultResourceLoader; }
209 
213  template <typename ResourceType>
214  static void SetResourceTypeLoader(ezResourceTypeLoader* pCreator);
215 
219 
220 public:
226  static void RegisterNamedResource(const char* szLookupName, const char* szRedirectionResource);
227 
229  static void UnregisterNamedResource(const char* szLookupName);
230 
231 
235 
236 public:
238  static void RegisterResourceForAssetType(const char* szAssetTypeName, const ezRTTI* pResourceType);
239 
242  static const ezRTTI* FindResourceForAssetType(const char* szAssetTypeName);
243 
247 
248 public:
251  static void EnableExportMode();
252 
256  template <typename ResourceType>
257  static ezTypedResourceHandle<ResourceType> GetResourceHandleForExport(const char* szResourceID);
258 
259 
263 
264 public:
277  static void RegisterResourceOverrideType(const ezRTTI* pDerivedTypeToUse, ezDelegate<bool(const ezStringBuilder&)> OverrideDecider);
278 
282  static void UnregisterResourceOverrideType(const ezRTTI* pDerivedTypeToUse);
283 
287 
288 public:
290  template <typename RESOURCE_TYPE>
292  {
293  RESOURCE_TYPE::SetResourceTypeLoadingFallback(hResource);
294  }
295 
297  template <typename RESOURCE_TYPE>
299  {
300  return RESOURCE_TYPE::GetResourceTypeLoadingFallback();
301  }
302 
306  template <typename RESOURCE_TYPE>
308  {
309  RESOURCE_TYPE::SetResourceTypeMissingFallback(hResource);
310  }
311 
313  template <typename RESOURCE_TYPE>
315  {
316  return RESOURCE_TYPE::GetResourceTypeMissingFallback();
317  }
318 
319  using ResourceCleanupCB = ezDelegate<void()>;
320 
322  static void AddResourceCleanupCallback(ResourceCleanupCB cb);
323 
325  static void ClearResourceCleanupCallback(ResourceCleanupCB cb);
326 
329  static void ExecuteAllResourceCleanupCallbacks();
330 
331 private:
332  static ezDynamicArray<ResourceCleanupCB> s_ResourceCleanupCallbacks;
333 
335 
339 
340 private:
341  friend class ezResource;
342  friend class ezResourceManagerWorkerDiskRead;
344  friend class ezResourceHandleReadContext;
345 
346  // Events
347 private:
348  static void BroadcastResourceEvent(const ezResourceEvent& e);
349 
350  static bool s_bBroadcastExistsEvent;
351 
352  // Miscellaneous
353 private:
354  static ezMutex s_ResourceMutex;
355 
356  // Startup / shutdown
357 private:
358  EZ_MAKE_SUBSYSTEM_STARTUP_FRIEND(Core, ResourceManager);
359  static void OnEngineShutdown();
360  static void OnCoreShutdown();
361  static void OnCoreStartup();
362  static void PluginEventHandler(const ezPlugin::PluginEvent& e);
363 
364  // Loading / reloading / creating resources
365 private:
366  struct LoadedResources
367  {
369  };
370 
371  struct LoadingInfo
372  {
373  ezTime m_DueDate;
374  ezResource* m_pResource;
375 
376  EZ_ALWAYS_INLINE bool operator==(const LoadingInfo& rhs) const { return m_pResource == rhs.m_pResource; }
377 
378  inline bool operator<(const LoadingInfo& rhs) const
379  {
380  if (m_DueDate < rhs.m_DueDate)
381  return true;
382  if (m_DueDate > rhs.m_DueDate)
383  return false;
384 
385  return m_pResource < rhs.m_pResource;
386  }
387  };
388 
389  static void EnsureResourceLoadingState(ezResource* pResource, const ezResourceState RequestedState);
390  static bool HelpResourceLoading();
391  static void PreloadResource(ezResource* pResource, ezTime tShouldBeAvailableIn);
392  static void InternalPreloadResource(ezResource* pResource, bool bHighestPriority);
393 
394  template <typename ResourceType>
395  static ResourceType* GetResource(const char* szResourceID, bool bIsReloadable);
396  static ezResource* GetResource(const ezRTTI* pRtti, const char* szResourceID, bool bIsReloadable);
397  static void RunWorkerTask(ezResource* pResource);
398  static void UpdateLoadingDeadlines();
399  static bool ReloadResource(ezResource* pResource, bool bForce);
400 
401  // this is the resource preload queue
402  static ezDeque<LoadingInfo> s_RequireLoading;
403  static ezHashTable<const ezRTTI*, LoadedResources> s_LoadedResources;
404  static const ezUInt32 MaxDiskReadTasks = 2;
405  static const ezUInt32 MaxMainThreadTasks = 16;
406  static bool s_bTaskRunning;
407  static bool s_bShutdown;
408  static ezResourceManagerWorkerDiskRead s_WorkerTasksDiskRead[MaxDiskReadTasks];
409  static ezResourceManagerWorkerMainThread s_WorkerTasksMainThread[MaxMainThreadTasks];
410  static ezUInt8 s_uiCurrentWorkerMainThread;
411  static ezUInt8 s_uiCurrentWorkerDiskRead;
412  static ezTime s_LastDeadlineUpdate;
413  static ezTime s_LastFrameUpdate;
414  static ezAtomicInteger32 s_ResourcesLoadedRecently;
415  static ezAtomicInteger32 s_ResourcesInLoadingLimbo; // not in the loading queue anymore but not yet finished loading either (typically now
416  // a task in the task system)
417 
418  // Type loaders
419 private:
420  static ezResourceTypeLoader* GetResourceTypeLoader(const ezRTTI* pRTTI);
421 
422  static ezMap<ezString, ezResourceTypeLoader*> s_ResourceTypeLoader;
423  static ezResourceLoaderFromFile s_FileResourceLoader;
424  static ezResourceTypeLoader* s_pDefaultResourceLoader;
426 
427  // Override / derived resources
428 private:
429  struct DerivedTypeInfo
430  {
431  const ezRTTI* m_pDerivedType = nullptr;
433  };
434 
436  static const ezRTTI* FindResourceTypeOverride(const ezRTTI* pRtti, const char* szResourceID);
437 
439 
440  // Named resources
441 
442 private:
443  static ezHashTable<ezTempHashedString, ezHashedString> s_NamedResources;
444 
445  // Asset system interaction
446 private:
447  static ezMap<ezString, const ezRTTI*> s_AssetToResourceType;
448 
449  // Export mode
450 private:
451  static bool s_bExportMode;
452 
453  static ezUInt32 s_uiNextResourceID;
454 };
455 
456 #include <Core/ResourceManager/Implementation/ResourceLock.h>
457 #include <Core/ResourceManager/Implementation/ResourceManager_inl.h>
458 
[internal] Worker thread/task for loading resources from disk.
Definition: WorkerTasks.h:9
[internal] Worker thread/task for loading on the main thread.
Definition: WorkerTasks.h:22
Definition: Map.h:322
The data that is broadcast whenever a plugin is (un-) loaded.
Definition: Plugin.h:87
Interface for binary in (read) streams.
Definition: Stream.h:14
static void SetResourceTypeLoadingFallback(const ezTypedResourceHandle< RESOURCE_TYPE > &hResource)
Specifies which resource to use as a loading fallback for the given type, while a resource is not yet...
Definition: ResourceManager.h:291
static ezEvent< const ezResourceEvent & > s_ResourceEvents
Definition: ResourceManager.h:19
static const ezTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeMissingFallback()
Definition: ResourceManager.h:314
This enumerable class holds information about reflected types. Each instance represents one type that...
Definition: RTTI.h:30
Definition: Event.h:136
static void SetResourceTypeMissingFallback(const ezTypedResourceHandle< RESOURCE_TYPE > &hResource)
Specifies which resource to use as a missing fallback for the given type, when a resource cannot be l...
Definition: ResourceManager.h:307
ezStringBuilder is a class that is meant for creating and modifying strings.
Definition: StringBuilder.h:34
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition: Mutex.h:10
The typeless implementation of resource handles. A typed interface is provided by ezTypedResourceHand...
Definition: ResourceHandle.h:17
Definition: HashTable.h:232
The time class encapsulates a double value storing the time in seconds.
Definition: Time.h:10
static ezEvent< const ezResourceManagerEvent & > s_ManagerEvents
Events for the resource manager that affect broader things.
Definition: ResourceManager.h:22
The base class for all resources.
Definition: Resource.h:9
Definition: DynamicArray.h:70
#define EZ_MAKE_SUBSYSTEM_STARTUP_FRIEND(GroupName, SubsystemName)
This inserts a friend declaration into a class, such that the given group/subsystem can access privat...
Definition: StaticSubSystem.h:107
The central class for managing all types derived from ezResource.
Definition: ResourceManager.h:11
A default implementation of ezResourceTypeLoader for standard file loading.
Definition: ResourceTypeLoader.h:59
static const ezTypedResourceHandle< RESOURCE_TYPE > & GetResourceTypeLoadingFallback()
Definition: ResourceManager.h:298
These events may be sent by a specific ezResource or by the ezResourceManager.
Definition: Declarations.h:19
Base class for all resource loaders.
Definition: ResourceTypeLoader.h:28
static ezResourceTypeLoader * GetDefaultResourceLoader()
Returns the resource loader to use when no type specific resource loader is available.
Definition: ResourceManager.h:208
static ezMutex & GetMutex()
Returns the resource manager mutex. Allows to lock the manager on a thread when multiple operations n...
Definition: ResourceManager.h:175
A generic delegate class which supports static functions and member functions.
Definition: MathExpression.h:11
The ezTypedResourceHandle controls access to an ezResource.
Definition: Declarations.h:14
Used in conjunction with ezResoruceHandleWriteContext to restore serialized resource handles...
Definition: ResourceHandleReader.h:19