BsResources.cpp 22 KB

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