BsResources.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Manager for dealing with all engine resources. It allows you to save
  8. * new resources and load existing ones.
  9. *
  10. * Used for manually dealing with resources but also for automatic resolving of
  11. * resource handles.
  12. *
  13. * @note Sim thread only.
  14. */
  15. class BS_CORE_EXPORT Resources : public Module<Resources>
  16. {
  17. public:
  18. Resources();
  19. ~Resources();
  20. /**
  21. * @brief Loads the resource from a given path. Returns an empty handle if resource can't be loaded.
  22. * Resource is loaded synchronously.
  23. */
  24. HResource load(const Path& filePath);
  25. /**
  26. * @copydoc load
  27. */
  28. template <class T>
  29. ResourceHandle<T> load(const Path& filePath)
  30. {
  31. return static_resource_cast<T>(load(filePath));
  32. }
  33. /**
  34. * @brief Loads the resource asynchronously. Initially returned resource handle will be invalid
  35. * until resource loading is done.
  36. *
  37. * @param filePath Full pathname of the file.
  38. *
  39. * @note You can use returned invalid handle in engine systems as the engine will check for handle
  40. * validity before using it.
  41. */
  42. HResource loadAsync(const Path& filePath);
  43. /**
  44. * @copydoc loadAsync
  45. */
  46. template <class T>
  47. ResourceHandle<T> loadAsync(const Path& filePath)
  48. {
  49. return static_resource_cast<T>(loadAsync(filePath));
  50. }
  51. /**
  52. * @brief Loads the resource with the given UUID. Returns an empty handle if resource can't be loaded.
  53. * Resource is loaded synchronously.
  54. */
  55. HResource loadFromUUID(const String& uuid);
  56. /**
  57. * @brief Loads the resource with the given UUID asynchronously. Initially returned resource handle will be invalid
  58. * until resource loading is done.
  59. *
  60. * @param uuid UUID of the resource to load.
  61. *
  62. * @note You can use returned invalid handle in engine systems as the engine will check for handle
  63. * validity before using it.
  64. */
  65. HResource loadFromUUIDAsync(const String& uuid);
  66. /**
  67. * @brief Unloads the resource that is referenced by the handle.
  68. *
  69. * @param resourceHandle Handle of the resource to unload.
  70. *
  71. * @note GPU resources held by the resource will be scheduled to be destroyed on the core thread.
  72. * Actual resource pointer wont be deleted until all user-held references to it are removed.
  73. */
  74. void unload(HResource resource);
  75. /**
  76. * @brief Finds all resources that aren't being referenced anywhere and unloads them.
  77. */
  78. void unloadAllUnused();
  79. /**
  80. * @brief Saves the resource at the specified location.
  81. *
  82. * @param resource Handle to the resource.
  83. * @param filePath Full pathname of the file to save as.
  84. * @param overwrite (optional) If true, any existing resource at the specified location will
  85. * be overwritten.
  86. *
  87. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  88. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  89. * version of the resource might get saved.
  90. *
  91. * If saving a core thread resource this is a potentially very slow operation as we must wait on the
  92. * core thread and the GPU in order to read the resource.
  93. */
  94. void save(HResource resource, const Path& filePath, bool overwrite);
  95. /**
  96. * @brief Creates a new resource handle from a resource pointer.
  97. *
  98. * @note Internal method used primarily be resource factory methods.
  99. */
  100. HResource _createResourceHandle(const ResourcePtr& obj);
  101. /**
  102. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  103. * used when resolving resource references.
  104. *
  105. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  106. * find that resource even after application restart, then you must save the resource
  107. * manifest before closing the application and restore it upon startup.
  108. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  109. */
  110. void registerResourceManifest(const ResourceManifestPtr& manifest);
  111. /**
  112. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  113. * used when resolving resource references.
  114. *
  115. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  116. * a resource is saved.
  117. *
  118. * @see registerResourceManifest
  119. */
  120. ResourceManifestPtr getResourceManifest(const String& name) const;
  121. /**
  122. * @brief Attempts to retrieve file path from the provided UUID. Returns true
  123. * if successful, false otherwise.
  124. */
  125. bool getFilePathFromUUID(const String& uuid, Path& filePath) const;
  126. /**
  127. * @brief Attempts to retrieve UUID from the provided file path. Returns true
  128. * if successful, false otherwise.
  129. */
  130. bool getUUIDFromFilePath(const Path& path, String& uuid) const;
  131. private:
  132. /**
  133. * @brief Starts resource loading or returns an already loaded resource.
  134. */
  135. HResource loadInternal(const Path& filePath, bool synchronous);
  136. /**
  137. * @brief Performs actually reading and deserializing of the resource file.
  138. * Called from various worker threads.
  139. */
  140. ResourcePtr loadFromDiskAndDeserialize(const Path& filePath);
  141. /**
  142. * @brief Callback triggered when the task manager is ready to process the loading task.
  143. */
  144. void loadCallback(const Path& filePath, HResource& resource);
  145. private:
  146. Vector<ResourceManifestPtr> mResourceManifests;
  147. ResourceManifestPtr mDefaultResourceManifest;
  148. BS_MUTEX(mInProgressResourcesMutex);
  149. BS_MUTEX(mLoadedResourceMutex);
  150. UnorderedMap<String, HResource> mLoadedResources;
  151. UnorderedMap<String, HResource> mInProgressResources; // Resources that are being asynchronously loaded
  152. };
  153. /**
  154. * @brief Provides easy access to the resource manager.
  155. */
  156. BS_CORE_EXPORT Resources& gResources();
  157. }