BsResources.h 9.4 KB

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