BsResources.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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(const Path&, bool)
  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 for the provided weak resource handle, or returns a loaded resource if already loaded.
  45. */
  46. HResource load(const WeakResourceHandle<Resource>& handle, bool loadDependencies = true);
  47. /**
  48. * @copydoc load(const WeakResourceHandle<T>&, bool)
  49. */
  50. template <class T>
  51. ResourceHandle<T> load(const WeakResourceHandle<T>& handle, bool loadDependencies = true)
  52. {
  53. return static_resource_cast<T>(load((const WeakResourceHandle<Resource>&)handle, loadDependencies));
  54. }
  55. /**
  56. * @brief Loads the resource asynchronously. Initially returned resource handle will be invalid
  57. * until resource loading is done.
  58. *
  59. * @param filePath Full pathname of the file.
  60. *
  61. * @note You can use returned invalid handle in engine systems as the engine will check for handle
  62. * validity before using it.
  63. */
  64. HResource loadAsync(const Path& filePath, bool loadDependencies = true);
  65. /**
  66. * @copydoc loadAsync
  67. */
  68. template <class T>
  69. ResourceHandle<T> loadAsync(const Path& filePath, bool loadDependencies = true)
  70. {
  71. return static_resource_cast<T>(loadAsync(filePath));
  72. }
  73. /**
  74. * @brief Loads the resource with the given UUID. Returns an empty handle if resource can't be loaded.
  75. *
  76. * @param uuid UUID of the resource to load.
  77. * @param async If true resource will be loaded asynchronously. Handle to non-loaded
  78. * resource will be returned immediately while loading will continue in the background.
  79. */
  80. HResource loadFromUUID(const String& uuid, bool async = false, bool loadDependencies = true);
  81. /**
  82. * @brief Unloads the resource that is referenced by the handle. Any dependencies held by the resource will also
  83. * be unloaded, but only if the resource is holding the last reference to them. This method will unload a
  84. * resource even if it is being referenced and the caller must ensure whatever is referencing it can
  85. * handle a null resource, or ensure there are no references.
  86. *
  87. * @param resourceHandle Handle of the resource to unload.
  88. *
  89. * @note GPU resources held by the resource will be scheduled to be destroyed on the core thread.
  90. * Actual resource pointer wont be deleted until all user-held references to it are removed.
  91. */
  92. void unload(HResource resource);
  93. /**
  94. * @copydoc unload(HResource&)
  95. */
  96. void unload(WeakResourceHandle<Resource> resource);
  97. /**
  98. * @brief Finds all resources that aren't being referenced anywhere and unloads them.
  99. */
  100. void unloadAllUnused();
  101. /**
  102. * @brief Saves the resource at the specified location.
  103. *
  104. * @param resource Handle to the resource.
  105. * @param filePath Full pathname of the file to save as.
  106. * @param overwrite (optional) If true, any existing resource at the specified location will
  107. * be overwritten.
  108. *
  109. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  110. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  111. * version of the resource might get saved.
  112. *
  113. * If saving a core thread resource this is a potentially very slow operation as we must wait on the
  114. * core thread and the GPU in order to read the resource.
  115. */
  116. void save(const HResource& resource, const Path& filePath, bool overwrite);
  117. /**
  118. * @brief Saves an existing resource to its previous location.
  119. *
  120. * @param resource Handle to the resource.
  121. *
  122. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  123. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  124. * version of the resource might get saved.
  125. *
  126. * If saving a core thread resource this is a potentially very slow operation as we must wait on the
  127. * core thread and the GPU in order to read the resource.
  128. */
  129. void save(const HResource& resource);
  130. /**
  131. * @brief Updates an existing resource handle with a new resource. Caller must ensure that
  132. * new resource type matches the original resource type.
  133. */
  134. void update(HResource& handle, const ResourcePtr& resource);
  135. /**
  136. * @brief Returns a list of dependencies from the resources at the specified path. Resource will not be loaded
  137. * or parsed, but instead the saved list of dependencies will be read from the file and returned.
  138. *
  139. * @param filePath Full path to the resource to get dependencies for.
  140. *
  141. * @returns List of dependencies represented as UUIDs.
  142. */
  143. Vector<String> getDependencies(const Path& filePath);
  144. /**
  145. * @brief Checks is the resource with the specified UUID loaded.
  146. *
  147. * @param uuid UUID of the resource to check.
  148. * @param checkInProgress Should this method also check resources that are in progress of being asynchronously loaded.
  149. *
  150. * @return True if loaded or loading in progress, false otherwise.
  151. */
  152. bool isLoaded(const String& uuid, bool checkInProgress = true);
  153. /**
  154. * @brief Creates a new resource handle from a resource pointer.
  155. *
  156. * @note Internal method used primarily be resource factory methods.
  157. */
  158. HResource _createResourceHandle(const ResourcePtr& obj);
  159. /**
  160. * @brief Returns an existing handle for the specified UUID if one exists, or creates a new one.
  161. */
  162. HResource _getResourceHandle(const String& uuid);
  163. /**
  164. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  165. * used when resolving resource references.
  166. *
  167. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  168. * find that resource even after application restart, then you must save the resource
  169. * manifest before closing the application and restore it upon startup.
  170. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  171. */
  172. void registerResourceManifest(const ResourceManifestPtr& manifest);
  173. /**
  174. * @brief Unregisters a resource manifest previously registered with ::registerResourceManifest.
  175. */
  176. void unregisterResourceManifest(const ResourceManifestPtr& manifest);
  177. /**
  178. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  179. * used when resolving resource references.
  180. *
  181. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  182. * a resource is saved.
  183. *
  184. * @see registerResourceManifest
  185. */
  186. ResourceManifestPtr getResourceManifest(const String& name) const;
  187. /**
  188. * @brief Attempts to retrieve file path from the provided UUID. Returns true
  189. * if successful, false otherwise.
  190. */
  191. bool getFilePathFromUUID(const String& uuid, Path& filePath) const;
  192. /**
  193. * @brief Attempts to retrieve UUID from the provided file path. Returns true
  194. * if successful, false otherwise.
  195. */
  196. bool getUUIDFromFilePath(const Path& path, String& uuid) const;
  197. /**
  198. * @brief Called when the resource has been successfully loaded.
  199. *
  200. * @note It is undefined from which thread this will get called from.
  201. * Most definitely not the sim thread if resource was being loaded
  202. * asynchronously.
  203. */
  204. Event<void(const HResource&)> onResourceLoaded;
  205. /**
  206. * @brief Called when the resource has been destroyed. Subscriber should not hold on to the provided resource
  207. * reference as it will be destroyed.
  208. *
  209. * @note It is undefined from which thread this will get called from.
  210. */
  211. Event<void(const HResource&)> onResourceDestroyed;
  212. /**
  213. * @brief Called when the internal resource the handle is pointing to has changed.
  214. *
  215. * @note It is undefined from which thread this will get called from.
  216. */
  217. Event<void(const HResource&)> onResourceModified;
  218. private:
  219. /**
  220. * @brief Starts resource loading or returns an already loaded resource. Both UUID and filePath must match the
  221. * same resource, although you may provide an empty path in which case the resource will be retrieved
  222. * from memory if its currently loaded.
  223. */
  224. HResource loadInternal(const String& UUID, const Path& filePath, bool synchronous, bool loadDependencies);
  225. /**
  226. * @brief Performs actually reading and deserializing of the resource file.
  227. * Called from various worker threads.
  228. */
  229. ResourcePtr loadFromDiskAndDeserialize(const Path& filePath);
  230. /**
  231. * @brief Triggered when individual resource has finished loading.
  232. */
  233. void loadComplete(HResource& resource);
  234. /**
  235. * @brief Callback triggered when the task manager is ready to process the loading task.
  236. */
  237. void loadCallback(const Path& filePath, HResource& resource);
  238. private:
  239. Vector<ResourceManifestPtr> mResourceManifests;
  240. ResourceManifestPtr mDefaultResourceManifest;
  241. BS_MUTEX(mInProgressResourcesMutex);
  242. BS_MUTEX(mLoadedResourceMutex);
  243. UnorderedMap<String, WeakResourceHandle<Resource>> mHandles;
  244. UnorderedMap<String, HResource> mLoadedResources;
  245. UnorderedMap<String, ResourceLoadData*> mInProgressResources; // Resources that are being asynchronously loaded
  246. UnorderedMap<String, Vector<ResourceLoadData*>> mDependantLoads;
  247. };
  248. /**
  249. * @brief Provides global access to the resource manager.
  250. */
  251. BS_CORE_EXPORT Resources& gResources();
  252. }