BsResources.h 5.5 KB

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