CmResources.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. WString 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();
  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 WString& 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 WString& 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 at the specified location.
  99. *
  100. * @param resource Handle to the resource.
  101. * @param filePath Full pathname of the file.
  102. * @param overwrite (optional) If true, any existing resource at the specified location will
  103. * be overwritten.
  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. void save(HResource resource, const WString& filePath, bool overwrite);
  110. /**
  111. * @brief Creates a new resource handle from a resource pointer.
  112. * You will almost never need to call this manually and should instead use
  113. * resource-specific methods that return a resource handle in the first place.
  114. */
  115. HResource createResourceHandle(const ResourcePtr& obj);
  116. /**
  117. * @brief Allows you to set a resource manifest containing UUID <-> file path mapping that is
  118. * used when resolving resource references.
  119. *
  120. * @note If you want objects that reference resources (using ResourceHandles) to be able to
  121. * find that resource even after application restart, then you must save the resource
  122. * manifest before closing the application and restore it upon startup.
  123. * Otherwise resources will be assigned brand new UUIDs and references will be broken.
  124. */
  125. void registerResourceManifest(const ResourceManifestPtr& manifest);
  126. /**
  127. * @brief Allows you to retrieve resource manifest containing UUID <-> file path mapping that is
  128. * used when resolving resource references.
  129. *
  130. * @note Resources module internally holds a "Default" manifest that it automatically updated whenever
  131. * a resource is saved.
  132. *
  133. * @see registerResourceManifest
  134. */
  135. ResourceManifestPtr getResourceManifest(const String& name) const;
  136. bool getFilePathFromUUID(const String& uuid, WString& filePath) const;
  137. bool getUUIDFromFilePath(const WString& path, String& uuid) const;
  138. private:
  139. Vector<ResourceManifestPtr>::type mResourceManifests;
  140. ResourceManifestPtr mDefaultResourceManifest;
  141. CM_MUTEX(mInProgressResourcesMutex);
  142. CM_MUTEX(mLoadedResourceMutex);
  143. ResourceRequestHandler* mRequestHandler;
  144. ResourceResponseHandler* mResponseHandler;
  145. WorkQueue* mWorkQueue;
  146. UINT16 mWorkQueueChannel;
  147. UnorderedMap<String, HResource>::type mLoadedResources;
  148. UnorderedMap<String, ResourceAsyncOp>::type mInProgressResources; // Resources that are being asynchronously loaded
  149. HResource loadInternal(const WString& filePath, bool synchronous);
  150. ResourcePtr loadFromDiskAndDeserialize(const WString& filePath);
  151. void notifyResourceLoadingFinished(HResource& handle);
  152. void notifyNewResourceLoaded(HResource& handle);
  153. };
  154. CM_EXPORT Resources& gResources();
  155. }