BsResources.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 Updates an existing resource handle with a new resource. Caller must ensure that
  115. * new resource type matches the original resource type.
  116. */
  117. void update(HResource& handle, const ResourcePtr& resource);
  118. /**
  119. * @brief Checks is the resource with the specified UUID loaded.
  120. *
  121. * @param uuid UUID of the resource to check.
  122. * @param checkInProgress Should this method also check resources that are in progress of being asynchronously loaded.
  123. *
  124. * @return True if loaded or loading in progress, false otherwise.
  125. */
  126. bool isLoaded(const String& uuid, bool checkInProgress = true);
  127. /**
  128. * @brief Creates a new resource handle from a resource pointer.
  129. *
  130. * @note Internal method used primarily be resource factory methods.
  131. */
  132. HResource _createResourceHandle(const ResourcePtr& obj);
  133. /**
  134. * @brief Returns an existing handle of a resource that has already been loaded,
  135. * or is currently being loaded.
  136. */
  137. HResource _getResourceHandle(const String& uuid);
  138. /**
  139. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  140. * used when resolving resource references.
  141. *
  142. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  143. * find that resource even after application restart, then you must save the resource
  144. * manifest before closing the application and restore it upon startup.
  145. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  146. */
  147. void registerResourceManifest(const ResourceManifestPtr& manifest);
  148. /**
  149. * @brief Unregisters a resource manifest previously registered with ::registerResourceManifest.
  150. */
  151. void unregisterResourceManifest(const ResourceManifestPtr& manifest);
  152. /**
  153. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  154. * used when resolving resource references.
  155. *
  156. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  157. * a resource is saved.
  158. *
  159. * @see registerResourceManifest
  160. */
  161. ResourceManifestPtr getResourceManifest(const String& name) const;
  162. /**
  163. * @brief Attempts to retrieve file path from the provided UUID. Returns true
  164. * if successful, false otherwise.
  165. */
  166. bool getFilePathFromUUID(const String& uuid, Path& filePath) const;
  167. /**
  168. * @brief Attempts to retrieve UUID from the provided file path. Returns true
  169. * if successful, false otherwise.
  170. */
  171. bool getUUIDFromFilePath(const Path& path, String& uuid) const;
  172. /**
  173. * @brief Called when the resource has been successfully loaded.
  174. *
  175. * @note It is undefined from which thread this will get called from.
  176. * Most definitely not the sim thread if resource was being loaded
  177. * asynchronously.
  178. */
  179. Event<void(const HResource&)> onResourceLoaded;
  180. /**
  181. * @brief Called when the resource has been destroyed.
  182. *
  183. * @note It is undefined from which thread this will get called from.
  184. */
  185. Event<void(const HResource&)> onResourceDestroyed;
  186. /**
  187. * @brief Called when the internal resource the handle is pointing to has changed.
  188. *
  189. * @note It is undefined from which thread this will get called from.
  190. */
  191. Event<void(const HResource&)> onResourceModified;
  192. private:
  193. /**
  194. * @brief Starts resource loading or returns an already loaded resource. Both UUID and filePath must match the
  195. * same resource, although you may provide an empty path in which case the resource will be retrieved
  196. * from memory if its currently loaded.
  197. */
  198. HResource loadInternal(const String& UUID, const Path& filePath, bool synchronous, bool loadDependencies);
  199. /**
  200. * @brief Performs actually reading and deserializing of the resource file.
  201. * Called from various worker threads.
  202. */
  203. ResourcePtr loadFromDiskAndDeserialize(const Path& filePath);
  204. /**
  205. * @brief Triggered when individual resource has finished loading.
  206. */
  207. void loadComplete(HResource& resource);
  208. /**
  209. * @brief Callback triggered when the task manager is ready to process the loading task.
  210. */
  211. void loadCallback(const Path& filePath, HResource& resource);
  212. private:
  213. Vector<ResourceManifestPtr> mResourceManifests;
  214. ResourceManifestPtr mDefaultResourceManifest;
  215. BS_MUTEX(mInProgressResourcesMutex);
  216. BS_MUTEX(mLoadedResourceMutex);
  217. UnorderedMap<String, HResource> mLoadedResources;
  218. UnorderedMap<String, ResourceLoadData*> mInProgressResources; // Resources that are being asynchronously loaded
  219. UnorderedMap<String, Vector<ResourceLoadData*>> mDependantLoads;
  220. };
  221. /**
  222. * @brief Provides global access to the resource manager.
  223. */
  224. BS_CORE_EXPORT Resources& gResources();
  225. }