CmResources.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "CmResources.h"
  2. #include "CmResource.h"
  3. #include "CmResourceManifest.h"
  4. #include "CmException.h"
  5. #include "CmFileSerializer.h"
  6. #include "CmFileSystem.h"
  7. #include "BsTaskScheduler.h"
  8. #include "CmUUID.h"
  9. #include "CmPath.h"
  10. #include "CmDebug.h"
  11. namespace BansheeEngine
  12. {
  13. Resources::Resources()
  14. {
  15. mDefaultResourceManifest = ResourceManifest::create("Default");
  16. mResourceManifests.push_back(mDefaultResourceManifest);
  17. }
  18. Resources::~Resources()
  19. {
  20. }
  21. HResource Resources::load(const Path& filePath)
  22. {
  23. return loadInternal(filePath, true);
  24. }
  25. HResource Resources::loadAsync(const Path& filePath)
  26. {
  27. return loadInternal(filePath, false);
  28. }
  29. HResource Resources::loadFromUUID(const String& uuid)
  30. {
  31. Path filePath;
  32. bool foundPath = false;
  33. // Default manifest is at 0th index but all other take priority since Default manifest could
  34. // contain obsolete data.
  35. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  36. {
  37. if((*iter)->uuidToFilePath(uuid, filePath))
  38. {
  39. foundPath = true;
  40. break;
  41. }
  42. }
  43. if(!foundPath)
  44. {
  45. gDebug().logWarning("Cannot load resource. Resource with UUID '" + uuid + "' doesn't exist.");
  46. return HResource();
  47. }
  48. return load(filePath);
  49. }
  50. HResource Resources::loadFromUUIDAsync(const String& uuid)
  51. {
  52. Path filePath;
  53. bool foundPath = false;
  54. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  55. {
  56. if((*iter)->uuidToFilePath(uuid, filePath))
  57. {
  58. foundPath = true;
  59. break;
  60. }
  61. }
  62. if(!foundPath)
  63. {
  64. gDebug().logWarning("Cannot load resource. Resource with UUID '" + uuid + "' doesn't exist.");
  65. return HResource();
  66. }
  67. return loadAsync(filePath);
  68. }
  69. HResource Resources::loadInternal(const Path& filePath, bool synchronous)
  70. {
  71. String uuid;
  72. bool foundUUID = false;
  73. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  74. {
  75. if((*iter)->filePathToUUID(filePath, uuid))
  76. {
  77. foundUUID = true;
  78. break;
  79. }
  80. }
  81. if(!foundUUID)
  82. uuid = UUIDGenerator::instance().generateRandom();
  83. {
  84. CM_LOCK_MUTEX(mLoadedResourceMutex);
  85. auto iterFind = mLoadedResources.find(uuid);
  86. if(iterFind != mLoadedResources.end()) // Resource is already loaded
  87. {
  88. return iterFind->second;
  89. }
  90. }
  91. bool resourceLoadingInProgress = false;
  92. HResource existingResource;
  93. {
  94. CM_LOCK_MUTEX(mInProgressResourcesMutex);
  95. auto iterFind2 = mInProgressResources.find(uuid);
  96. if(iterFind2 != mInProgressResources.end())
  97. {
  98. existingResource = iterFind2->second;
  99. resourceLoadingInProgress = true;
  100. }
  101. }
  102. if(resourceLoadingInProgress) // We're already loading this resource
  103. {
  104. if(!synchronous)
  105. return existingResource;
  106. else
  107. {
  108. // Previously being loaded as async but now we want it synced, so we wait
  109. existingResource.synchronize();
  110. return existingResource;
  111. }
  112. }
  113. if(!FileSystem::isFile(filePath))
  114. {
  115. gDebug().logWarning("Specified file: " + filePath.toString() + " doesn't exist.");
  116. return HResource();
  117. }
  118. HResource newResource(uuid);
  119. {
  120. CM_LOCK_MUTEX(mInProgressResourcesMutex);
  121. mInProgressResources[uuid] = newResource;
  122. }
  123. if(synchronous)
  124. {
  125. loadCallback(filePath, newResource);
  126. }
  127. else
  128. {
  129. String fileName = filePath.getFilename();
  130. String taskName = "Resource load: " + fileName;
  131. TaskPtr task = Task::create(taskName, std::bind(&Resources::loadCallback, this, filePath, newResource));
  132. TaskScheduler::instance().addTask(task);
  133. }
  134. return newResource;
  135. }
  136. ResourcePtr Resources::loadFromDiskAndDeserialize(const Path& filePath)
  137. {
  138. FileSerializer fs;
  139. std::shared_ptr<IReflectable> loadedData = fs.decode(filePath);
  140. if(loadedData == nullptr)
  141. CM_EXCEPT(InternalErrorException, "Unable to load resource.");
  142. if(!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
  143. CM_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
  144. ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
  145. return resource;
  146. }
  147. void Resources::unload(HResource resource)
  148. {
  149. if(!resource.isLoaded()) // If it's still loading wait until that finishes
  150. resource.synchronize();
  151. resource->destroy();
  152. {
  153. CM_LOCK_MUTEX(mLoadedResourceMutex);
  154. mLoadedResources.erase(resource.getUUID());
  155. }
  156. }
  157. void Resources::unloadAllUnused()
  158. {
  159. Vector<HResource>::type resourcesToUnload;
  160. {
  161. CM_LOCK_MUTEX(mLoadedResourceMutex);
  162. for(auto iter = mLoadedResources.begin(); iter != mLoadedResources.end(); ++iter)
  163. {
  164. if(iter->second.getInternalPtr().unique()) // We just have this one reference, meaning nothing is using this resource
  165. resourcesToUnload.push_back(iter->second);
  166. }
  167. }
  168. for(auto iter = resourcesToUnload.begin(); iter != resourcesToUnload.end(); ++iter)
  169. {
  170. unload(*iter);
  171. }
  172. }
  173. void Resources::save(HResource resource, const Path& filePath, bool overwrite)
  174. {
  175. if(!resource.isLoaded())
  176. resource.synchronize();
  177. bool fileExists = FileSystem::isFile(filePath);
  178. if(fileExists)
  179. {
  180. if(overwrite)
  181. FileSystem::remove(filePath);
  182. else
  183. CM_EXCEPT(InvalidParametersException, "Another file exists at the specified location.");
  184. }
  185. mDefaultResourceManifest->registerResource(resource.getUUID(), filePath);
  186. FileSerializer fs;
  187. fs.encode(resource.get(), filePath);
  188. }
  189. void Resources::registerResourceManifest(const ResourceManifestPtr& manifest)
  190. {
  191. if(manifest->getName() == "Default")
  192. return;
  193. auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
  194. if(findIter == mResourceManifests.end())
  195. mResourceManifests.push_back(manifest);
  196. else
  197. *findIter = manifest;
  198. }
  199. ResourceManifestPtr Resources::getResourceManifest(const String& name) const
  200. {
  201. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  202. {
  203. if(name == (*iter)->getName())
  204. return (*iter);
  205. }
  206. return nullptr;
  207. }
  208. HResource Resources::createResourceHandle(const ResourcePtr& obj)
  209. {
  210. String uuid = UUIDGenerator::instance().generateRandom();
  211. HResource newHandle(obj, uuid);
  212. {
  213. CM_LOCK_MUTEX(mLoadedResourceMutex);
  214. mLoadedResources[uuid] = newHandle;
  215. }
  216. return newHandle;
  217. }
  218. bool Resources::getFilePathFromUUID(const String& uuid, Path& filePath) const
  219. {
  220. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  221. {
  222. if((*iter)->uuidToFilePath(uuid, filePath))
  223. return true;
  224. }
  225. return false;
  226. }
  227. bool Resources::getUUIDFromFilePath(const Path& path, String& uuid) const
  228. {
  229. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  230. {
  231. if((*iter)->filePathToUUID(path, uuid))
  232. return true;
  233. }
  234. return false;
  235. }
  236. void Resources::loadCallback(const Path& filePath, HResource& resource)
  237. {
  238. ResourcePtr rawResource = loadFromDiskAndDeserialize(filePath);
  239. {
  240. CM_LOCK_MUTEX(mInProgressResourcesMutex);
  241. mInProgressResources.erase(resource.getUUID());
  242. }
  243. resource._setHandleData(rawResource, resource.getUUID());
  244. {
  245. CM_LOCK_MUTEX(mLoadedResourceMutex);
  246. mLoadedResources[resource.getUUID()] = resource;
  247. }
  248. }
  249. CM_EXPORT Resources& gResources()
  250. {
  251. return Resources::instance();
  252. }
  253. }