CmResources.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. namespace BansheeEngine
  5. {
  6. class CM_EXPORT Resources : public Module<Resources>
  7. {
  8. public:
  9. /**
  10. * @brief Constructor.
  11. *
  12. * @param resMetaPath Folder where the resource meta-data will be stored. If the folder doesn't exist
  13. * it will be created.
  14. */
  15. Resources();
  16. ~Resources();
  17. /**
  18. * @brief Loads the resource from a given path. Returns null if resource can't be loaded.
  19. *
  20. * @param filePath The path of the file to load. The file is searched for in
  21. * the AssetDatabase first, and if it cannot be found it is
  22. * loaded as a temporary resource object. You can't save
  23. * references to temporary resource objects because they won't
  24. * persist after application shut-down, but otherwise they act
  25. * the same as normal resources.
  26. *
  27. * @return Loaded resource, or null if it cannot be found.
  28. */
  29. HResource load(const WString& filePath);
  30. /**
  31. * @brief Loads the resource asynchronously. Initially returned resource should not be used
  32. * until BaseResourceHandle.isLoaded gets set to true.
  33. *
  34. * @param filePath Full pathname of the file.
  35. *
  36. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  37. */
  38. HResource loadAsync(const WString& filePath);
  39. /**
  40. * @brief Loads the resource with the given uuid.
  41. *
  42. * @param uuid UUID of the resource to load.
  43. *
  44. * @return Loaded resource, or null if it cannot be found.
  45. */
  46. HResource loadFromUUID(const String& uuid);
  47. /**
  48. * @brief Loads the resource with the given UUID asynchronously. Initially returned resource should not be used
  49. * until BaseResourceHandle.isLoaded gets set to true.
  50. *
  51. * @param uuid UUID of the resource to load.
  52. *
  53. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  54. */
  55. HResource loadFromUUIDAsync(const String& uuid);
  56. /**
  57. * @brief Unloads the resource that is referenced by the handle.
  58. *
  59. * @param resourceHandle Handle of the resource.
  60. *
  61. * @note GPU resources held by the resource will be scheduled to be destroyed on the core thread.
  62. * Actual resource pointer wont be deleted until all user-held references to it are removed.
  63. */
  64. void unload(HResource resource);
  65. /**
  66. * @brief Finds all resources that aren't being referenced anywhere and unloads them.
  67. */
  68. void unloadAllUnused();
  69. /**
  70. * @brief Saves the resource at the specified location.
  71. *
  72. * @param resource Handle to the resource.
  73. * @param filePath Full pathname of the file.
  74. * @param overwrite (optional) If true, any existing resource at the specified location will
  75. * be overwritten.
  76. *
  77. * @note If the resource is a GpuResource and you are in some way modifying it from the Core thread, make
  78. * sure all those commands are submitted before you call this method. Otherwise an obsolete
  79. * version of the resource might get saved.
  80. */
  81. void save(HResource resource, const WString& filePath, bool overwrite);
  82. /**
  83. * @brief Creates a new resource handle from a resource pointer.
  84. * You will almost never need to call this manually and should instead use
  85. * resource-specific methods that return a resource handle in the first place.
  86. */
  87. HResource createResourceHandle(const ResourcePtr& obj);
  88. /**
  89. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  90. * used when resolving resource references.
  91. *
  92. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  93. * find that resource even after application restart, then you must save the resource
  94. * manifest before closing the application and restore it upon startup.
  95. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  96. */
  97. void registerResourceManifest(const ResourceManifestPtr& manifest);
  98. /**
  99. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  100. * used when resolving resource references.
  101. *
  102. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  103. * a resource is saved.
  104. *
  105. * @see registerResourceManifest
  106. */
  107. ResourceManifestPtr getResourceManifest(const String& name) const;
  108. bool getFilePathFromUUID(const String& uuid, WString& filePath) const;
  109. bool getUUIDFromFilePath(const WString& path, String& uuid) const;
  110. private:
  111. Vector<ResourceManifestPtr>::type mResourceManifests;
  112. ResourceManifestPtr mDefaultResourceManifest;
  113. CM_MUTEX(mInProgressResourcesMutex);
  114. CM_MUTEX(mLoadedResourceMutex);
  115. UnorderedMap<String, HResource>::type mLoadedResources;
  116. UnorderedMap<String, HResource>::type mInProgressResources; // Resources that are being asynchronously loaded
  117. HResource loadInternal(const WString& filePath, bool synchronous);
  118. ResourcePtr loadFromDiskAndDeserialize(const WString& filePath);
  119. void loadCallback(const WString& filePath, HResource& resource);
  120. };
  121. CM_EXPORT Resources& gResources();
  122. }