BsResources.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. struct ResourceLoadData
  18. {
  19. ResourceLoadData(const HResource& resource, UINT32 numDependencies)
  20. :resource(resource), remainingDependencies(numDependencies)
  21. { }
  22. HResource resource;
  23. ResourcePtr loadedData;
  24. UINT32 remainingDependencies;
  25. bool notifyImmediately;
  26. };
  27. public:
  28. Resources();
  29. ~Resources();
  30. /**
  31. * @brief Loads the resource from a given path. Returns an empty handle if resource can't be loaded.
  32. * Resource is loaded synchronously.
  33. */
  34. HResource load(const Path& filePath, bool loadDependencies = true);
  35. /**
  36. * @copydoc load
  37. */
  38. template <class T>
  39. ResourceHandle<T> load(const Path& filePath, bool loadDependencies = true)
  40. {
  41. return static_resource_cast<T>(load(filePath, loadDependencies));
  42. }
  43. /**
  44. * @brief Loads the resource asynchronously. Initially returned resource handle will be invalid
  45. * until resource loading is done.
  46. *
  47. * @param filePath Full pathname of the file.
  48. *
  49. * @note You can use returned invalid handle in engine systems as the engine will check for handle
  50. * validity before using it.
  51. */
  52. HResource loadAsync(const Path& filePath, bool loadDependencies = true);
  53. /**
  54. * @copydoc loadAsync
  55. */
  56. template <class T>
  57. ResourceHandle<T> loadAsync(const Path& filePath, bool loadDependencies = true)
  58. {
  59. return static_resource_cast<T>(loadAsync(filePath));
  60. }
  61. /**
  62. * @brief Loads the resource with the given UUID. Returns an empty handle if resource can't be loaded.
  63. *
  64. * @param uuid UUID of the resource to load.
  65. * @param async If true resource will be loaded asynchronously. Handle to non-loaded
  66. * resource will be returned immediately while loading will continue in the background.
  67. */
  68. HResource loadFromUUID(const String& uuid, bool async = false, bool loadDependencies = true);
  69. /**
  70. * @brief Unloads the resource that is referenced by the handle. Resource is unloaded regardless if it is
  71. * referenced or not. Any dependencies held by the resource will also be unloaded, but only if the
  72. * resource is holding the last reference to them.
  73. *
  74. * @param resourceHandle Handle of the resource to unload.
  75. *
  76. * @note GPU resources held by the resource will be scheduled to be destroyed on the core thread.
  77. * Actual resource pointer wont be deleted until all user-held references to it are removed.
  78. */
  79. void unload(HResource resource);
  80. /**
  81. * @brief Finds all resources that aren't being referenced anywhere and unloads them.
  82. */
  83. void unloadAllUnused();
  84. /**
  85. * @brief Saves the resource at the specified location.
  86. *
  87. * @param resource Handle to the resource.
  88. * @param filePath Full pathname of the file to save as.
  89. * @param overwrite (optional) If true, any existing resource at the specified location will
  90. * be overwritten.
  91. *
  92. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  93. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  94. * version of the resource might get saved.
  95. *
  96. * If saving a core thread resource this is a potentially very slow operation as we must wait on the
  97. * core thread and the GPU in order to read the resource.
  98. */
  99. void save(HResource resource, const Path& filePath, bool overwrite);
  100. /**
  101. * @brief Saves an existing resource to its previous location.
  102. *
  103. * @param resource Handle to the resource.
  104. *
  105. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  106. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  107. * version of the resource might get saved.
  108. *
  109. * If saving a core thread resource this is a potentially very slow operation as we must wait on the
  110. * core thread and the GPU in order to read the resource.
  111. */
  112. void save(HResource resource);
  113. /**
  114. * @brief Creates a new resource handle from a resource pointer.
  115. *
  116. * @note Internal method used primarily be resource factory methods.
  117. */
  118. HResource _createResourceHandle(const ResourcePtr& obj);
  119. /**
  120. * @brief Returns an existing handle of a resource that has already been loaded,
  121. * or is currently being loaded.
  122. */
  123. HResource _getResourceHandle(const String& uuid);
  124. /**
  125. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  126. * used when resolving resource references.
  127. *
  128. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  129. * find that resource even after application restart, then you must save the resource
  130. * manifest before closing the application and restore it upon startup.
  131. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  132. */
  133. void registerResourceManifest(const ResourceManifestPtr& manifest);
  134. /**
  135. * @brief Unregisters a resource manifest previously registered with ::registerResourceManifest.
  136. */
  137. void unregisterResourceManifest(const ResourceManifestPtr& manifest);
  138. /**
  139. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  140. * used when resolving resource references.
  141. *
  142. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  143. * a resource is saved.
  144. *
  145. * @see registerResourceManifest
  146. */
  147. ResourceManifestPtr getResourceManifest(const String& name) const;
  148. /**
  149. * @brief Attempts to retrieve file path from the provided UUID. Returns true
  150. * if successful, false otherwise.
  151. */
  152. bool getFilePathFromUUID(const String& uuid, Path& filePath) const;
  153. /**
  154. * @brief Attempts to retrieve UUID from the provided file path. Returns true
  155. * if successful, false otherwise.
  156. */
  157. bool getUUIDFromFilePath(const Path& path, String& uuid) const;
  158. /**
  159. * @brief Called when the resource has been successfully loaded.
  160. *
  161. * @note It is undefined from which thread this will get called from.
  162. * Most definitely not the sim thread if resource was being loaded
  163. * asynchronously.
  164. */
  165. Event<void(const HResource&)> onResourceLoaded;
  166. /**
  167. * @brief Called when the resource has been destroyed.
  168. *
  169. * @note It is undefined from which thread this will get called from.
  170. */
  171. Event<void(const HResource&)> onResourceDestroyed;
  172. /**
  173. * @brief Called when the internal resource the handle is pointing to has changed.
  174. *
  175. * @note It is undefined from which thread this will get called from.
  176. */
  177. Event<void(const HResource&)> onResourceModified; // TODO - Not used, implement when I add hot-swapping
  178. private:
  179. /**
  180. * @brief Starts resource loading or returns an already loaded resource.
  181. */
  182. HResource loadInternal(const Path& filePath, bool synchronous, bool loadDependencies);
  183. /**
  184. * @brief Performs actually reading and deserializing of the resource file.
  185. * Called from various worker threads.
  186. */
  187. ResourcePtr loadFromDiskAndDeserialize(const Path& filePath);
  188. /**
  189. * @brief Triggered when individual resource has finished loading.
  190. */
  191. void loadComplete(HResource& resource);
  192. /**
  193. * @brief Callback triggered when the task manager is ready to process the loading task.
  194. */
  195. void loadCallback(const Path& filePath, HResource& resource);
  196. private:
  197. Vector<ResourceManifestPtr> mResourceManifests;
  198. ResourceManifestPtr mDefaultResourceManifest;
  199. BS_MUTEX(mInProgressResourcesMutex);
  200. BS_MUTEX(mLoadedResourceMutex);
  201. UnorderedMap<String, HResource> mLoadedResources;
  202. UnorderedMap<String, ResourceLoadData*> mInProgressResources; // Resources that are being asynchronously loaded
  203. UnorderedMap<String, Vector<ResourceLoadData*>> mDependantLoads;
  204. };
  205. /**
  206. * @brief Provides global access to the resource manager.
  207. */
  208. BS_CORE_EXPORT Resources& gResources();
  209. }