CmResources.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmWorkQueue.h"
  5. namespace CamelotEngine
  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( const 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. BaseResourceHandle resource;
  24. };
  25. struct CM_EXPORT ResourceLoadResponse
  26. {
  27. ResourcePtr rawResource;
  28. };
  29. struct CM_EXPORT ResourceAsyncOp
  30. {
  31. BaseResourceHandle 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 Called every frame by the main loop.
  47. */
  48. void update();
  49. /**
  50. * @brief Loads the resource from a given path. Returns null if resource can't be loaded.
  51. *
  52. * @param filePath The path of the file to load. The file is searched for in
  53. * the AssetDatabase first, and if it cannot be found it is
  54. * loaded as a temporary resource object. You can't save
  55. * references to temporary resource objects because they won't
  56. * persist after application shut-down, but otherwise they act
  57. * the same as normal resources.
  58. *
  59. * @return Loaded resource, or null if it cannot be found.
  60. */
  61. BaseResourceHandle load(const String& filePath);
  62. /**
  63. * @brief Loads the resource asynchronously. Initially returned resource should not be used
  64. * until BaseResourceHandle.isResolved gets set to true. (Set only at the end of each frame)
  65. *
  66. * @param filePath Full pathname of the file.
  67. *
  68. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  69. */
  70. BaseResourceHandle loadAsync(const String& filePath);
  71. /**
  72. * @brief Loads the resource with the given uuid.
  73. *
  74. * @param uuid UUID of the resource to load.
  75. *
  76. * @return Loaded resource, or null if it cannot be found.
  77. */
  78. BaseResourceHandle loadFromUUID(const String& uuid);
  79. /**
  80. * @brief Loads the resource with the given UUID asynchronously. Initially returned resource should not be used
  81. * until BaseResourceHandle.isResolved gets set to true. (Set only at the end of each frame)
  82. *
  83. * @param uuid UUID of the resource to load.
  84. *
  85. * @return Resource where the data will eventually be loaded, or null if the file cannot be found.
  86. */
  87. BaseResourceHandle loadFromUUIDAsync(const String& uuid);
  88. /**
  89. * @brief Saves the resource. Resource must be registered using Resources::create beforehand.
  90. *
  91. * @param resource The resource.
  92. */
  93. void save(BaseResourceHandle resource);
  94. /**
  95. * @brief Creates a new resource at the specified location. Throws an exception if resource already exists.
  96. * Automatically calls Resources::save.
  97. *
  98. * @param resource The resource.
  99. * @param filePath Full pathname of the file.
  100. * @param overwrite (optional) If true, any existing resource at the specified location
  101. * will be overwritten.
  102. */
  103. void create(BaseResourceHandle resource, const String& filePath, bool overwrite = false);
  104. public:
  105. struct ResourceMetaData : public IReflectable
  106. {
  107. String mUUID;
  108. String mPath;
  109. /************************************************************************/
  110. /* SERIALIZATION */
  111. /************************************************************************/
  112. public:
  113. friend class ResourceMetaDataRTTI;
  114. static RTTITypeBase* getRTTIStatic();
  115. virtual RTTITypeBase* getRTTI() const;
  116. };
  117. private:
  118. typedef std::shared_ptr<ResourceMetaData> ResourceMetaDataPtr;
  119. map<String, ResourceMetaDataPtr>::type mResourceMetaData;
  120. map<String, ResourceMetaDataPtr>::type mResourceMetaData_FilePath;
  121. ResourceRequestHandler* mRequestHandler;
  122. ResourceResponseHandler* mResponseHandler;
  123. WorkQueuePtr mWorkQueue; // TODO Low priority - I might want to make this more global so other classes can use it
  124. UINT16 mWorkQueueChannel;
  125. unordered_map<String, BaseResourceHandle>::type mLoadedResources; // TODO Low priority - I'm not sure how will UUID (a string) do as key do performance wise
  126. unordered_map<String, ResourceAsyncOp>::type mInProgressResources; // Resources that are being asynchronously loaded
  127. BaseResourceHandle loadInternal(const String& filePath, bool synchronous);
  128. ResourcePtr loadFromDiskAndDeserialize(const String& filePath);
  129. void loadMetaData();
  130. void saveMetaData(const ResourceMetaDataPtr metaData);
  131. void createMetaData(const String& uuid, const String& filePath);
  132. void addMetaData(const String& uuid, const String& filePath);
  133. void updateMetaData(const String& uuid, const String& newFilePath);
  134. void removeMetaData(const String& uuid);
  135. bool metaExists_UUID(const String& uuid) const;
  136. bool metaExists_Path(const String& path) const;
  137. const String& getPathFromUUID(const String& uuid) const;
  138. const String& getUUIDFromPath(const String& path) const;
  139. String mMetaDataFolderPath;
  140. private:
  141. friend class SpecificImporter;
  142. /**
  143. * @brief Registers the newly loaded resource in the global Resources library.
  144. */
  145. void registerLoadedResource(BaseResourceHandle resource);
  146. };
  147. CM_EXPORT Resources& gResources();
  148. }