CmResources.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmWorkQueue.h"
  5. namespace CamelotFramework
  6. {
  7. class CM_EXPORT Resources : public Module<Resources>
  8. {
  9. private:
  10. class CM_EXPORT ResourceRequestHandler : public WorkQueue::RequestHandler
  11. {
  12. virtual bool canHandleRequest( const WorkQueue::Request* req, const WorkQueue* srcQ );
  13. virtual WorkQueue::Response* handleRequest(WorkQueue::Request* req, const WorkQueue* srcQ );
  14. };
  15. class CM_EXPORT ResourceResponseHandler : public WorkQueue::ResponseHandler
  16. {
  17. virtual bool canHandleResponse( const WorkQueue::Response* res, const WorkQueue* srcQ );
  18. virtual void handleResponse( const WorkQueue::Response* res, const WorkQueue* srcQ );
  19. };
  20. struct CM_EXPORT ResourceLoadRequest
  21. {
  22. String filePath;
  23. HResource resource;
  24. };
  25. struct CM_EXPORT ResourceLoadResponse
  26. {
  27. ResourcePtr rawResource;
  28. };
  29. struct CM_EXPORT ResourceAsyncOp
  30. {
  31. HResource resource;
  32. WorkQueue::RequestID requestID;
  33. };
  34. typedef std::shared_ptr<ResourceLoadRequest> ResourceLoadRequestPtr;
  35. typedef std::shared_ptr<ResourceLoadResponse> ResourceLoadResponsePtr;
  36. public:
  37. /**
  38. * @brief Constructor.
  39. *
  40. * @param resMetaPath Folder where the resource meta-data will be stored. If the folder doesn't exist
  41. * it will be created.
  42. */
  43. Resources(const String& metaDataFolder);
  44. ~Resources();
  45. /**
  46. * @brief Loads the resource from a given path. Returns null if resource can't be loaded.
  47. *
  48. * @param filePath The path of the file to load. The file is searched for in
  49. * the AssetDatabase first, and if it cannot be found it is
  50. * loaded as a temporary resource object. You can't save
  51. * references to temporary resource objects because they won't
  52. * persist after application shut-down, but otherwise they act
  53. * the same as normal resources.
  54. *
  55. * @return Loaded resource, or null if it cannot be found.
  56. */
  57. HResource load(const String& filePath);
  58. /**
  59. * @brief Loads the resource asynchronously. Initially returned resource should not be used
  60. * until BaseResourceHandle.isLoaded gets set to true.
  61. *
  62. * @param filePath Full pathname of the file.
  63. *
  64. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  65. */
  66. HResource loadAsync(const String& filePath);
  67. /**
  68. * @brief Loads the resource with the given uuid.
  69. *
  70. * @param uuid UUID of the resource to load.
  71. *
  72. * @return Loaded resource, or null if it cannot be found.
  73. */
  74. HResource loadFromUUID(const String& uuid);
  75. /**
  76. * @brief Loads the resource with the given UUID asynchronously. Initially returned resource should not be used
  77. * until BaseResourceHandle.isLoaded gets set to true.
  78. *
  79. * @param uuid UUID of the resource to load.
  80. *
  81. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  82. */
  83. HResource loadFromUUIDAsync(const String& uuid);
  84. /**
  85. * @brief Unloads the resource that is referenced by the handle.
  86. *
  87. * @param resourceHandle Handle of the resource.
  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. * @brief Finds all resources that aren't being referenced anywhere and unloads them.
  95. */
  96. void unloadAllUnused();
  97. /**
  98. * @brief Saves the resource. Resource must be registered using Resources::create beforehand.
  99. *
  100. * @param resource The resource.
  101. */
  102. void save(HResource resource);
  103. /**
  104. * @brief Creates a new resource at the specified location. Throws an exception if resource
  105. * already exists. Automatically calls Resources::save.
  106. *
  107. * @param resource Handle to the resource.
  108. * @param filePath Full pathname of the file.
  109. * @param overwrite (optional) If true, any existing resource at the specified location will
  110. * be overwritten.
  111. */
  112. void create(HResource resource, const String& filePath, bool overwrite = false);
  113. public:
  114. struct ResourceMetaData : public IReflectable
  115. {
  116. String mUUID;
  117. String mPath;
  118. /************************************************************************/
  119. /* SERIALIZATION */
  120. /************************************************************************/
  121. public:
  122. friend class ResourceMetaDataRTTI;
  123. static RTTITypeBase* getRTTIStatic();
  124. virtual RTTITypeBase* getRTTI() const;
  125. };
  126. private:
  127. typedef std::shared_ptr<ResourceMetaData> ResourceMetaDataPtr;
  128. Map<String, ResourceMetaDataPtr>::type mResourceMetaData;
  129. Map<String, ResourceMetaDataPtr>::type mResourceMetaData_FilePath;
  130. CM_MUTEX(mInProgressResourcesMutex);
  131. CM_MUTEX(mLoadedResourceMutex);
  132. ResourceRequestHandler* mRequestHandler;
  133. ResourceResponseHandler* mResponseHandler;
  134. WorkQueue* mWorkQueue;
  135. UINT16 mWorkQueueChannel;
  136. UnorderedMap<String, HResource>::type mLoadedResources;
  137. UnorderedMap<String, ResourceAsyncOp>::type mInProgressResources; // Resources that are being asynchronously loaded
  138. HResource loadInternal(const String& filePath, bool synchronous);
  139. ResourcePtr loadFromDiskAndDeserialize(const String& filePath);
  140. void loadMetaData();
  141. void saveMetaData(const ResourceMetaDataPtr metaData);
  142. void createMetaData(const String& uuid, const String& filePath);
  143. void addMetaData(const String& uuid, const String& filePath);
  144. void updateMetaData(const String& uuid, const String& newFilePath);
  145. void removeMetaData(const String& uuid);
  146. bool metaExists_UUID(const String& uuid) const;
  147. bool metaExists_Path(const String& path) const;
  148. const String& getPathFromUUID(const String& uuid) const;
  149. const String& getUUIDFromPath(const String& path) const;
  150. void notifyResourceLoadingFinished(HResource& handle);
  151. void notifyNewResourceLoaded(HResource& handle);
  152. String mMetaDataFolderPath;
  153. };
  154. CM_EXPORT Resources& gResources();
  155. }