BsResources.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsResources.h"
  4. #include "BsResource.h"
  5. #include "BsResourceManifest.h"
  6. #include "BsException.h"
  7. #include "BsFileSerializer.h"
  8. #include "BsFileSystem.h"
  9. #include "BsTaskScheduler.h"
  10. #include "BsUUID.h"
  11. #include "BsDebug.h"
  12. #include "BsUtility.h"
  13. #include "BsSavedResourceData.h"
  14. #include "BsResourceListenerManager.h"
  15. namespace BansheeEngine
  16. {
  17. Resources::Resources()
  18. {
  19. mDefaultResourceManifest = ResourceManifest::create("Default");
  20. mResourceManifests.push_back(mDefaultResourceManifest);
  21. }
  22. Resources::~Resources()
  23. {
  24. // Unload and invalidate all resources
  25. UnorderedMap<String, LoadedResourceData> loadedResourcesCopy;
  26. {
  27. BS_LOCK_MUTEX(mLoadedResourceMutex)
  28. loadedResourcesCopy = mLoadedResources;
  29. }
  30. for (auto& loadedResourcePair : loadedResourcesCopy)
  31. destroy(loadedResourcePair.second.resource);
  32. }
  33. HResource Resources::load(const Path& filePath, bool loadDependencies, bool keepInternalReference)
  34. {
  35. if (!FileSystem::isFile(filePath))
  36. {
  37. LOGWRN_VERBOSE("Cannot load resource. Specified file: " + filePath.toString() + " doesn't exist.");
  38. return HResource();
  39. }
  40. String uuid;
  41. bool foundUUID = getUUIDFromFilePath(filePath, uuid);
  42. if (!foundUUID)
  43. uuid = UUIDGenerator::generateRandom();
  44. return loadInternal(uuid, filePath, true, loadDependencies, keepInternalReference);
  45. }
  46. HResource Resources::load(const WeakResourceHandle<Resource>& handle, bool loadDependencies, bool keepInternalReference)
  47. {
  48. if (handle.mData == nullptr)
  49. return HResource();
  50. String uuid = handle.getUUID();
  51. return loadFromUUID(uuid, false, loadDependencies, keepInternalReference);
  52. }
  53. HResource Resources::loadAsync(const Path& filePath, bool loadDependencies, bool keepInternalReference)
  54. {
  55. if (!FileSystem::isFile(filePath))
  56. {
  57. LOGWRN_VERBOSE("Cannot load resource. Specified file: " + filePath.toString() + " doesn't exist.");
  58. return HResource();
  59. }
  60. String uuid;
  61. bool foundUUID = getUUIDFromFilePath(filePath, uuid);
  62. if (!foundUUID)
  63. uuid = UUIDGenerator::generateRandom();
  64. return loadInternal(uuid, filePath, false, loadDependencies, keepInternalReference);
  65. }
  66. HResource Resources::loadFromUUID(const String& uuid, bool async, bool loadDependencies, bool keepInternalReference)
  67. {
  68. Path filePath;
  69. // Default manifest is at 0th index but all other take priority since Default manifest could
  70. // contain obsolete data.
  71. for (auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  72. {
  73. if ((*iter)->uuidToFilePath(uuid, filePath))
  74. break;
  75. }
  76. return loadInternal(uuid, filePath, !async, loadDependencies, keepInternalReference);
  77. }
  78. HResource Resources::loadInternal(const String& UUID, const Path& filePath, bool synchronous, bool loadDependencies, bool keepInternalReference)
  79. {
  80. HResource outputResource;
  81. bool alreadyLoading = false;
  82. bool loadInProgress = false;
  83. {
  84. // Check if resource is already being loaded on a worker thread
  85. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  86. auto iterFind2 = mInProgressResources.find(UUID);
  87. if (iterFind2 != mInProgressResources.end())
  88. {
  89. LoadedResourceData& resData = iterFind2->second->resData;
  90. outputResource = resData.resource.lock();
  91. if (keepInternalReference)
  92. {
  93. resData.numInternalRefs++;
  94. outputResource.addInternalRef();
  95. }
  96. alreadyLoading = true;
  97. loadInProgress = true;
  98. }
  99. // Previously being loaded as async but now we want it synced, so we wait
  100. if (loadInProgress && synchronous)
  101. outputResource.blockUntilLoaded();
  102. if (!alreadyLoading)
  103. {
  104. BS_LOCK_MUTEX(mLoadedResourceMutex);
  105. auto iterFind = mLoadedResources.find(UUID);
  106. if (iterFind != mLoadedResources.end()) // Resource is already loaded
  107. {
  108. LoadedResourceData& resData = iterFind->second;
  109. outputResource = resData.resource.lock();
  110. if (keepInternalReference)
  111. {
  112. resData.numInternalRefs++;
  113. outputResource.addInternalRef();
  114. }
  115. alreadyLoading = true;
  116. }
  117. }
  118. }
  119. // Not loaded and not in progress, start loading of new resource
  120. // (or if already loaded or in progress, load any dependencies)
  121. if (!alreadyLoading)
  122. {
  123. // Check if the handle already exists
  124. BS_LOCK_MUTEX(mLoadedResourceMutex);
  125. auto iterFind = mHandles.find(UUID);
  126. if (iterFind != mHandles.end())
  127. outputResource = iterFind->second.lock();
  128. else
  129. {
  130. outputResource = HResource(UUID);
  131. mHandles[UUID] = outputResource.getWeak();
  132. }
  133. }
  134. // We have nowhere to load from, warn and complete load if a file path was provided,
  135. // otherwise pass through as we might just want to load from memory.
  136. if (filePath.isEmpty())
  137. {
  138. if (!alreadyLoading)
  139. {
  140. LOGWRN_VERBOSE("Cannot load resource. Resource with UUID '" + UUID + "' doesn't exist.");
  141. // Complete the load as that the depedency counter is properly reduced, in case this
  142. // is a dependency of some other resource.
  143. loadComplete(outputResource);
  144. return outputResource;
  145. }
  146. }
  147. else if (!FileSystem::isFile(filePath))
  148. {
  149. LOGWRN_VERBOSE("Cannot load resource. Specified file: " + filePath.toString() + " doesn't exist.");
  150. // Complete the load as that the depedency counter is properly reduced, in case this
  151. // is a dependency of some other resource.
  152. loadComplete(outputResource);
  153. assert(!loadInProgress); // Resource already being loaded but we can't find its path now?
  154. return outputResource;
  155. }
  156. // Load dependency data if a file path is provided
  157. SPtr<SavedResourceData> savedResourceData;
  158. if (!filePath.isEmpty())
  159. {
  160. FileDecoder fs(filePath);
  161. savedResourceData = std::static_pointer_cast<SavedResourceData>(fs.decode());
  162. }
  163. // If already loading keep the old load operation active, otherwise create a new one
  164. if (!alreadyLoading)
  165. {
  166. {
  167. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  168. ResourceLoadData* loadData = bs_new<ResourceLoadData>(outputResource.getWeak(), 0);
  169. mInProgressResources[UUID] = loadData;
  170. loadData->resData = outputResource.getWeak();
  171. if (keepInternalReference)
  172. {
  173. loadData->resData.numInternalRefs++;
  174. outputResource.addInternalRef();
  175. }
  176. loadData->remainingDependencies = 1;
  177. loadData->notifyImmediately = synchronous; // Make resource listener trigger before exit if loading synchronously
  178. // Register dependencies and count them so we know when the resource is fully loaded
  179. if (loadDependencies && savedResourceData != nullptr)
  180. {
  181. for (auto& dependency : savedResourceData->getDependencies())
  182. {
  183. if (dependency != UUID)
  184. {
  185. mDependantLoads[dependency].push_back(loadData);
  186. loadData->remainingDependencies++;
  187. }
  188. }
  189. }
  190. }
  191. if (loadDependencies && savedResourceData != nullptr)
  192. {
  193. const Vector<String>& dependencyUUIDs = savedResourceData->getDependencies();
  194. UINT32 numDependencies = (UINT32)dependencyUUIDs.size();
  195. Vector<HResource> dependencies(numDependencies);
  196. for (UINT32 i = 0; i < numDependencies; i++)
  197. dependencies[i] = loadFromUUID(dependencyUUIDs[i], !synchronous, true, false);
  198. // Keep dependencies alive until the parent is done loading
  199. {
  200. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  201. // At this point the resource is guaranteed to still be in-progress, so it's safe to update its dependency list
  202. mInProgressResources[UUID]->dependencies = dependencies;
  203. }
  204. }
  205. }
  206. else if (loadDependencies && savedResourceData != nullptr) // Queue dependencies in case they aren't already loaded
  207. {
  208. const Vector<String>& dependencies = savedResourceData->getDependencies();
  209. if (!dependencies.empty())
  210. {
  211. {
  212. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  213. ResourceLoadData* loadData = nullptr;
  214. auto iterFind = mInProgressResources.find(UUID);
  215. if (iterFind == mInProgressResources.end()) // Fully loaded
  216. {
  217. loadData = bs_new<ResourceLoadData>(outputResource.getWeak(), 0);
  218. loadData->resData = outputResource.getWeak();
  219. loadData->remainingDependencies = 0;
  220. loadData->notifyImmediately = synchronous; // Make resource listener trigger before exit if loading synchronously
  221. mInProgressResources[UUID] = loadData;
  222. }
  223. else
  224. {
  225. loadData = iterFind->second;
  226. }
  227. // Register dependencies and count them so we know when the resource is fully loaded
  228. for (auto& dependency : dependencies)
  229. {
  230. if (dependency != UUID)
  231. {
  232. bool registerDependency = true;
  233. auto iterFind2 = mDependantLoads.find(dependency);
  234. if (iterFind2 != mDependantLoads.end())
  235. {
  236. Vector<ResourceLoadData*>& dependantData = iterFind2->second;
  237. auto iterFind3 = std::find_if(dependantData.begin(), dependantData.end(),
  238. [&](ResourceLoadData* x)
  239. {
  240. return x->resData.resource.getUUID() == outputResource.getUUID();
  241. });
  242. registerDependency = iterFind3 == dependantData.end();
  243. }
  244. if (registerDependency)
  245. {
  246. mDependantLoads[dependency].push_back(loadData);
  247. loadData->remainingDependencies++;
  248. loadData->dependencies.push_back(_getResourceHandle(dependency));
  249. }
  250. }
  251. }
  252. }
  253. for (auto& dependency : dependencies)
  254. loadFromUUID(dependency, !synchronous, true, false);
  255. }
  256. }
  257. // Actually start the file read operation if not already loaded or in progress
  258. if (!alreadyLoading && !filePath.isEmpty())
  259. {
  260. // Synchronous or the resource doesn't support async, read the file immediately
  261. if (synchronous || !savedResourceData->allowAsyncLoading())
  262. {
  263. loadCallback(filePath, outputResource);
  264. }
  265. else // Asynchronous, read the file on a worker thread
  266. {
  267. String fileName = filePath.getFilename();
  268. String taskName = "Resource load: " + fileName;
  269. TaskPtr task = Task::create(taskName, std::bind(&Resources::loadCallback, this, filePath, outputResource));
  270. TaskScheduler::instance().addTask(task);
  271. }
  272. }
  273. else // File already loaded or in progress
  274. {
  275. // Complete the load unless its in progress in which case we wait for its worker thread to complete it.
  276. // In case file is already loaded this will only decrement dependency count in case this resource is a dependency.
  277. if (!loadInProgress)
  278. loadComplete(outputResource);
  279. else
  280. {
  281. // In case loading finished in the meantime we cannot be sure at what point ::loadComplete was triggered,
  282. // so trigger it manually so that the dependency count is properly decremented in case this resource
  283. // is a dependency.
  284. BS_LOCK_MUTEX(mLoadedResourceMutex);
  285. auto iterFind = mLoadedResources.find(UUID);
  286. if (iterFind != mLoadedResources.end())
  287. loadComplete(outputResource);
  288. }
  289. }
  290. return outputResource;
  291. }
  292. ResourcePtr Resources::loadFromDiskAndDeserialize(const Path& filePath)
  293. {
  294. FileDecoder fs(filePath);
  295. fs.skip(); // Skipped over saved resource data
  296. std::shared_ptr<IReflectable> loadedData = fs.decode();
  297. if (loadedData == nullptr)
  298. {
  299. LOGERR("Unable to load resource at path \"" + filePath.toString() + "\"");
  300. }
  301. else
  302. {
  303. if (!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
  304. BS_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
  305. }
  306. ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
  307. return resource;
  308. }
  309. void Resources::release(ResourceHandleBase& resource)
  310. {
  311. const String& UUID = resource.getUUID();
  312. {
  313. bool loadInProgress = false;
  314. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  315. auto iterFind2 = mInProgressResources.find(UUID);
  316. if (iterFind2 != mInProgressResources.end())
  317. loadInProgress = true;
  318. // Technically we should be able to just cancel a load in progress instead of blocking until it finishes.
  319. // However that would mean the last reference could get lost on whatever thread did the loading, which
  320. // isn't something that's supported. If this ends up being a problem either make handle counting atomic
  321. // or add a separate queue for objects destroyed from the load threads.
  322. if (loadInProgress)
  323. resource.blockUntilLoaded();
  324. {
  325. BS_LOCK_MUTEX(mLoadedResourceMutex);
  326. auto iterFind = mLoadedResources.find(UUID);
  327. if (iterFind != mLoadedResources.end()) // Resource is already loaded
  328. {
  329. LoadedResourceData& resData = iterFind->second;
  330. assert(resData.numInternalRefs > 0);
  331. resData.numInternalRefs--;
  332. resource.removeInternalRef();
  333. return;
  334. }
  335. }
  336. }
  337. }
  338. void Resources::unloadAllUnused()
  339. {
  340. Vector<HResource> resourcesToUnload;
  341. {
  342. BS_LOCK_MUTEX(mLoadedResourceMutex);
  343. for(auto iter = mLoadedResources.begin(); iter != mLoadedResources.end(); ++iter)
  344. {
  345. const LoadedResourceData& resData = iter->second;
  346. if (resData.resource.mData->mRefCount == resData.numInternalRefs) // Only internal references exist, free it
  347. resourcesToUnload.push_back(resData.resource.lock());
  348. }
  349. }
  350. // Note: When unloading multiple resources it's possible that unloading one will also unload
  351. // another resource in "resourcesToUnload". This is fine because "unload" deals with invalid
  352. // handles gracefully.
  353. for(auto iter = resourcesToUnload.begin(); iter != resourcesToUnload.end(); ++iter)
  354. {
  355. release(*iter);
  356. }
  357. }
  358. void Resources::destroy(ResourceHandleBase& resource)
  359. {
  360. if (resource.mData == nullptr)
  361. return;
  362. const String& uuid = resource.getUUID();
  363. if (!resource.isLoaded(false))
  364. {
  365. bool loadInProgress = false;
  366. {
  367. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  368. auto iterFind2 = mInProgressResources.find(uuid);
  369. if (iterFind2 != mInProgressResources.end())
  370. loadInProgress = true;
  371. }
  372. if (loadInProgress) // If it's still loading wait until that finishes
  373. resource.blockUntilLoaded();
  374. else
  375. return; // Already unloaded
  376. }
  377. // Notify external systems before we actually destroy it
  378. onResourceDestroyed(uuid);
  379. resource.mData->mPtr->destroy();
  380. {
  381. BS_LOCK_MUTEX(mLoadedResourceMutex);
  382. auto iterFind = mLoadedResources.find(uuid);
  383. if (iterFind != mLoadedResources.end())
  384. {
  385. LoadedResourceData& resData = iterFind->second;
  386. while (resData.numInternalRefs > 0)
  387. {
  388. resData.numInternalRefs--;
  389. resData.resource.removeInternalRef();
  390. }
  391. mLoadedResources.erase(iterFind);
  392. }
  393. else
  394. {
  395. assert(false); // This should never happen but in case it does fail silently in release mode
  396. }
  397. }
  398. resource.setHandleData(nullptr, uuid);
  399. }
  400. void Resources::save(const HResource& resource, const Path& filePath, bool overwrite)
  401. {
  402. if (resource == nullptr)
  403. return;
  404. if (!resource.isLoaded(false))
  405. {
  406. bool loadInProgress = false;
  407. {
  408. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  409. auto iterFind2 = mInProgressResources.find(resource.getUUID());
  410. if (iterFind2 != mInProgressResources.end())
  411. loadInProgress = true;
  412. }
  413. if (loadInProgress) // If it's still loading wait until that finishes
  414. resource.blockUntilLoaded();
  415. else
  416. return; // Nothing to save
  417. }
  418. bool fileExists = FileSystem::isFile(filePath);
  419. if(fileExists)
  420. {
  421. if(overwrite)
  422. FileSystem::remove(filePath);
  423. else
  424. BS_EXCEPT(InvalidParametersException, "Another file exists at the specified location.");
  425. }
  426. mDefaultResourceManifest->registerResource(resource.getUUID(), filePath);
  427. Vector<ResourceDependency> dependencyList = Utility::findResourceDependencies(*resource.get());
  428. Vector<String> dependencyUUIDs(dependencyList.size());
  429. for (UINT32 i = 0; i < (UINT32)dependencyList.size(); i++)
  430. dependencyUUIDs[i] = dependencyList[i].resource.getUUID();
  431. SPtr<SavedResourceData> resourceData = bs_shared_ptr_new<SavedResourceData>(dependencyUUIDs, resource->allowAsyncLoading());
  432. FileEncoder fs(filePath);
  433. fs.encode(resourceData.get());
  434. fs.encode(resource.get());
  435. }
  436. void Resources::save(const HResource& resource)
  437. {
  438. if (resource == nullptr)
  439. return;
  440. Path path;
  441. if (getFilePathFromUUID(resource.getUUID(), path))
  442. save(resource, path, true);
  443. }
  444. void Resources::update(HResource& handle, const ResourcePtr& resource)
  445. {
  446. const String& uuid = handle.getUUID();
  447. handle.setHandleData(resource, uuid);
  448. {
  449. BS_LOCK_MUTEX(mLoadedResourceMutex);
  450. auto iterFind = mLoadedResources.find(uuid);
  451. if (iterFind == mLoadedResources.end())
  452. {
  453. LoadedResourceData& resData = mLoadedResources[uuid];
  454. resData.resource = handle.getWeak();
  455. }
  456. }
  457. onResourceModified(handle);
  458. }
  459. Vector<String> Resources::getDependencies(const Path& filePath)
  460. {
  461. SPtr<SavedResourceData> savedResourceData;
  462. if (!filePath.isEmpty())
  463. {
  464. FileDecoder fs(filePath);
  465. savedResourceData = std::static_pointer_cast<SavedResourceData>(fs.decode());
  466. }
  467. return savedResourceData->getDependencies();
  468. }
  469. void Resources::registerResourceManifest(const ResourceManifestPtr& manifest)
  470. {
  471. if(manifest->getName() == "Default")
  472. return;
  473. auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
  474. if(findIter == mResourceManifests.end())
  475. mResourceManifests.push_back(manifest);
  476. else
  477. *findIter = manifest;
  478. }
  479. void Resources::unregisterResourceManifest(const ResourceManifestPtr& manifest)
  480. {
  481. if (manifest->getName() == "Default")
  482. return;
  483. auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
  484. if (findIter != mResourceManifests.end())
  485. mResourceManifests.erase(findIter);
  486. }
  487. ResourceManifestPtr Resources::getResourceManifest(const String& name) const
  488. {
  489. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  490. {
  491. if(name == (*iter)->getName())
  492. return (*iter);
  493. }
  494. return nullptr;
  495. }
  496. bool Resources::isLoaded(const String& uuid, bool checkInProgress)
  497. {
  498. if (checkInProgress)
  499. {
  500. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  501. auto iterFind2 = mInProgressResources.find(uuid);
  502. if (iterFind2 != mInProgressResources.end())
  503. {
  504. return true;
  505. }
  506. {
  507. BS_LOCK_MUTEX(mLoadedResourceMutex);
  508. auto iterFind = mLoadedResources.find(uuid);
  509. if (iterFind != mLoadedResources.end())
  510. {
  511. return true;
  512. }
  513. }
  514. }
  515. return false;
  516. }
  517. HResource Resources::_createResourceHandle(const ResourcePtr& obj)
  518. {
  519. String uuid = UUIDGenerator::generateRandom();
  520. HResource newHandle(obj, uuid);
  521. {
  522. BS_LOCK_MUTEX(mLoadedResourceMutex);
  523. LoadedResourceData& resData = mLoadedResources[uuid];
  524. resData.resource = newHandle.getWeak();
  525. mHandles[uuid] = newHandle.getWeak();
  526. }
  527. return newHandle;
  528. }
  529. HResource Resources::_getResourceHandle(const String& uuid)
  530. {
  531. BS_LOCK_MUTEX(mLoadedResourceMutex);
  532. auto iterFind3 = mHandles.find(uuid);
  533. if (iterFind3 != mHandles.end()) // Not loaded, but handle does exist
  534. {
  535. return iterFind3->second.lock();
  536. }
  537. // Create new handle
  538. HResource handle(uuid);
  539. mHandles[uuid] = handle.getWeak();
  540. return handle;
  541. }
  542. bool Resources::getFilePathFromUUID(const String& uuid, Path& filePath) const
  543. {
  544. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  545. {
  546. if((*iter)->uuidToFilePath(uuid, filePath))
  547. return true;
  548. }
  549. return false;
  550. }
  551. bool Resources::getUUIDFromFilePath(const Path& path, String& uuid) const
  552. {
  553. Path manifestPath = path;
  554. if (!manifestPath.isAbsolute())
  555. manifestPath.makeAbsolute(FileSystem::getWorkingDirectoryPath());
  556. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  557. {
  558. if ((*iter)->filePathToUUID(manifestPath, uuid))
  559. return true;
  560. }
  561. return false;
  562. }
  563. void Resources::loadComplete(HResource& resource)
  564. {
  565. String uuid = resource.getUUID();
  566. ResourceLoadData* myLoadData = nullptr;
  567. bool finishLoad = true;
  568. Vector<ResourceLoadData*> dependantLoads;
  569. {
  570. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  571. auto iterFind = mInProgressResources.find(uuid);
  572. if (iterFind != mInProgressResources.end())
  573. {
  574. myLoadData = iterFind->second;
  575. finishLoad = myLoadData->remainingDependencies == 0;
  576. if (finishLoad)
  577. mInProgressResources.erase(iterFind);
  578. }
  579. auto iterFind2 = mDependantLoads.find(uuid);
  580. if (iterFind2 != mDependantLoads.end())
  581. dependantLoads = iterFind2->second;
  582. if (finishLoad)
  583. {
  584. mDependantLoads.erase(uuid);
  585. // If loadedData is null then we're probably completing load on an already loaded resource, triggered
  586. // by its dependencies.
  587. if (myLoadData != nullptr && myLoadData->loadedData != nullptr)
  588. {
  589. BS_LOCK_MUTEX(mLoadedResourceMutex);
  590. mLoadedResources[uuid] = myLoadData->resData;
  591. resource.setHandleData(myLoadData->loadedData, uuid);
  592. }
  593. for (auto& dependantLoad : dependantLoads)
  594. dependantLoad->remainingDependencies--;
  595. }
  596. }
  597. for (auto& dependantLoad : dependantLoads)
  598. {
  599. HResource dependant = dependantLoad->resData.resource.lock();
  600. loadComplete(dependant);
  601. }
  602. if (finishLoad && myLoadData != nullptr)
  603. {
  604. onResourceLoaded(resource);
  605. // This should only ever be true on the main thread
  606. if (myLoadData->notifyImmediately)
  607. ResourceListenerManager::instance().notifyListeners(uuid);
  608. bs_delete(myLoadData);
  609. }
  610. }
  611. void Resources::loadCallback(const Path& filePath, HResource& resource)
  612. {
  613. ResourcePtr rawResource = loadFromDiskAndDeserialize(filePath);
  614. {
  615. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  616. // Check if all my dependencies are loaded
  617. ResourceLoadData* myLoadData = mInProgressResources[resource.getUUID()];
  618. myLoadData->loadedData = rawResource;
  619. myLoadData->remainingDependencies--;
  620. }
  621. loadComplete(resource);
  622. }
  623. BS_CORE_EXPORT Resources& gResources()
  624. {
  625. return Resources::instance();
  626. }
  627. }