BsResources.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. BS_EXCEPT(InternalErrorException, "Unable to load resource.");
  283. if(!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
  284. BS_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
  285. ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
  286. return resource;
  287. }
  288. void Resources::release(ResourceHandleBase& resource)
  289. {
  290. const String& UUID = resource.getUUID();
  291. {
  292. bool loadInProgress = false;
  293. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  294. auto iterFind2 = mInProgressResources.find(UUID);
  295. if (iterFind2 != mInProgressResources.end())
  296. loadInProgress = true;
  297. // Technically we should be able to just cancel a load in progress instead of blocking until it finishes.
  298. // However that would mean the last reference could get lost on whatever thread did the loading, which
  299. // isn't something that's supported. If this ends up being a problem either make handle counting atomic
  300. // or add a separate queue for objects destroyed from the load threads.
  301. if (loadInProgress)
  302. resource.blockUntilLoaded();
  303. {
  304. BS_LOCK_MUTEX(mLoadedResourceMutex);
  305. auto iterFind = mLoadedResources.find(UUID);
  306. if (iterFind != mLoadedResources.end()) // Resource is already loaded
  307. {
  308. LoadedResourceData& resData = iterFind->second;
  309. assert(resData.numInternalRefs > 0);
  310. resData.numInternalRefs--;
  311. resource.removeInternalRef();
  312. return;
  313. }
  314. }
  315. }
  316. }
  317. void Resources::unloadAllUnused()
  318. {
  319. Vector<HResource> resourcesToUnload;
  320. {
  321. BS_LOCK_MUTEX(mLoadedResourceMutex);
  322. for(auto iter = mLoadedResources.begin(); iter != mLoadedResources.end(); ++iter)
  323. {
  324. const LoadedResourceData& resData = iter->second;
  325. if (resData.resource.mData->mRefCount == resData.numInternalRefs) // Only internal references exist, free it
  326. resourcesToUnload.push_back(resData.resource.lock());
  327. }
  328. }
  329. // Note: When unloading multiple resources it's possible that unloading one will also unload
  330. // another resource in "resourcesToUnload". This is fine because "unload" deals with invalid
  331. // handles gracefully.
  332. for(auto iter = resourcesToUnload.begin(); iter != resourcesToUnload.end(); ++iter)
  333. {
  334. release(*iter);
  335. }
  336. }
  337. void Resources::destroy(ResourceHandleBase& resource)
  338. {
  339. if (resource.mData == nullptr)
  340. return;
  341. const String& uuid = resource.getUUID();
  342. if (!resource.isLoaded(false))
  343. {
  344. bool loadInProgress = false;
  345. {
  346. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  347. auto iterFind2 = mInProgressResources.find(uuid);
  348. if (iterFind2 != mInProgressResources.end())
  349. loadInProgress = true;
  350. }
  351. if (loadInProgress) // If it's still loading wait until that finishes
  352. resource.blockUntilLoaded();
  353. else
  354. return; // Already unloaded
  355. }
  356. // Notify external systems before we actually destroy it
  357. onResourceDestroyed(uuid);
  358. resource.mData->mPtr->destroy();
  359. {
  360. BS_LOCK_MUTEX(mLoadedResourceMutex);
  361. auto iterFind = mLoadedResources.find(uuid);
  362. if (iterFind != mLoadedResources.end())
  363. {
  364. LoadedResourceData& resData = iterFind->second;
  365. while (resData.numInternalRefs > 0)
  366. {
  367. resData.numInternalRefs--;
  368. resData.resource.removeInternalRef();
  369. }
  370. mLoadedResources.erase(iterFind);
  371. }
  372. else
  373. {
  374. assert(false); // This should never happen but in case it does fail silently in release mode
  375. }
  376. }
  377. resource.setHandleData(nullptr, uuid);
  378. }
  379. void Resources::save(const HResource& resource, const Path& filePath, bool overwrite)
  380. {
  381. if (resource == nullptr)
  382. return;
  383. if (!resource.isLoaded(false))
  384. {
  385. bool loadInProgress = false;
  386. {
  387. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  388. auto iterFind2 = mInProgressResources.find(resource.getUUID());
  389. if (iterFind2 != mInProgressResources.end())
  390. loadInProgress = true;
  391. }
  392. if (loadInProgress) // If it's still loading wait until that finishes
  393. resource.blockUntilLoaded();
  394. else
  395. return; // Nothing to save
  396. }
  397. bool fileExists = FileSystem::isFile(filePath);
  398. if(fileExists)
  399. {
  400. if(overwrite)
  401. FileSystem::remove(filePath);
  402. else
  403. BS_EXCEPT(InvalidParametersException, "Another file exists at the specified location.");
  404. }
  405. mDefaultResourceManifest->registerResource(resource.getUUID(), filePath);
  406. Vector<ResourceDependency> dependencyList = Utility::findResourceDependencies(*resource.get());
  407. Vector<String> dependencyUUIDs(dependencyList.size());
  408. for (UINT32 i = 0; i < (UINT32)dependencyList.size(); i++)
  409. dependencyUUIDs[i] = dependencyList[i].resource.getUUID();
  410. SPtr<SavedResourceData> resourceData = bs_shared_ptr_new<SavedResourceData>(dependencyUUIDs, resource->allowAsyncLoading());
  411. FileEncoder fs(filePath);
  412. fs.encode(resourceData.get());
  413. fs.encode(resource.get());
  414. }
  415. void Resources::save(const HResource& resource)
  416. {
  417. if (resource == nullptr)
  418. return;
  419. Path path;
  420. if (getFilePathFromUUID(resource.getUUID(), path))
  421. save(resource, path, true);
  422. }
  423. void Resources::update(HResource& handle, const ResourcePtr& resource)
  424. {
  425. const String& uuid = handle.getUUID();
  426. handle.setHandleData(resource, uuid);
  427. {
  428. BS_LOCK_MUTEX(mLoadedResourceMutex);
  429. auto iterFind = mLoadedResources.find(uuid);
  430. if (iterFind == mLoadedResources.end())
  431. {
  432. LoadedResourceData& resData = mLoadedResources[uuid];
  433. resData.resource = handle.getWeak();
  434. }
  435. }
  436. onResourceModified(handle);
  437. }
  438. Vector<String> Resources::getDependencies(const Path& filePath)
  439. {
  440. SPtr<SavedResourceData> savedResourceData;
  441. if (!filePath.isEmpty())
  442. {
  443. FileDecoder fs(filePath);
  444. savedResourceData = std::static_pointer_cast<SavedResourceData>(fs.decode());
  445. }
  446. return savedResourceData->getDependencies();
  447. }
  448. void Resources::registerResourceManifest(const ResourceManifestPtr& manifest)
  449. {
  450. if(manifest->getName() == "Default")
  451. return;
  452. auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
  453. if(findIter == mResourceManifests.end())
  454. mResourceManifests.push_back(manifest);
  455. else
  456. *findIter = manifest;
  457. }
  458. void Resources::unregisterResourceManifest(const ResourceManifestPtr& manifest)
  459. {
  460. if (manifest->getName() == "Default")
  461. return;
  462. auto findIter = std::find(mResourceManifests.begin(), mResourceManifests.end(), manifest);
  463. if (findIter != mResourceManifests.end())
  464. mResourceManifests.erase(findIter);
  465. }
  466. ResourceManifestPtr Resources::getResourceManifest(const String& name) const
  467. {
  468. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  469. {
  470. if(name == (*iter)->getName())
  471. return (*iter);
  472. }
  473. return nullptr;
  474. }
  475. bool Resources::isLoaded(const String& uuid, bool checkInProgress)
  476. {
  477. if (checkInProgress)
  478. {
  479. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  480. auto iterFind2 = mInProgressResources.find(uuid);
  481. if (iterFind2 != mInProgressResources.end())
  482. {
  483. return true;
  484. }
  485. {
  486. BS_LOCK_MUTEX(mLoadedResourceMutex);
  487. auto iterFind = mLoadedResources.find(uuid);
  488. if (iterFind != mLoadedResources.end())
  489. {
  490. return true;
  491. }
  492. }
  493. }
  494. return false;
  495. }
  496. HResource Resources::_createResourceHandle(const ResourcePtr& obj)
  497. {
  498. String uuid = UUIDGenerator::generateRandom();
  499. HResource newHandle(obj, uuid);
  500. {
  501. BS_LOCK_MUTEX(mLoadedResourceMutex);
  502. LoadedResourceData& resData = mLoadedResources[uuid];
  503. resData.resource = newHandle.getWeak();
  504. mHandles[uuid] = newHandle.getWeak();
  505. }
  506. return newHandle;
  507. }
  508. HResource Resources::_getResourceHandle(const String& uuid)
  509. {
  510. BS_LOCK_MUTEX(mLoadedResourceMutex);
  511. auto iterFind3 = mHandles.find(uuid);
  512. if (iterFind3 != mHandles.end()) // Not loaded, but handle does exist
  513. {
  514. return iterFind3->second.lock();
  515. }
  516. // Create new handle
  517. HResource handle(uuid);
  518. mHandles[uuid] = handle.getWeak();
  519. return handle;
  520. }
  521. bool Resources::getFilePathFromUUID(const String& uuid, Path& filePath) const
  522. {
  523. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  524. {
  525. if((*iter)->uuidToFilePath(uuid, filePath))
  526. return true;
  527. }
  528. return false;
  529. }
  530. bool Resources::getUUIDFromFilePath(const Path& path, String& uuid) const
  531. {
  532. Path manifestPath = path;
  533. if (!manifestPath.isAbsolute())
  534. manifestPath.makeAbsolute(FileSystem::getWorkingDirectoryPath());
  535. for(auto iter = mResourceManifests.rbegin(); iter != mResourceManifests.rend(); ++iter)
  536. {
  537. if ((*iter)->filePathToUUID(manifestPath, uuid))
  538. return true;
  539. }
  540. return false;
  541. }
  542. void Resources::loadComplete(HResource& resource)
  543. {
  544. String uuid = resource.getUUID();
  545. ResourceLoadData* myLoadData = nullptr;
  546. bool finishLoad = true;
  547. Vector<ResourceLoadData*> dependantLoads;
  548. {
  549. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  550. auto iterFind = mInProgressResources.find(uuid);
  551. if (iterFind != mInProgressResources.end())
  552. {
  553. myLoadData = iterFind->second;
  554. finishLoad = myLoadData->remainingDependencies == 0;
  555. if (finishLoad)
  556. mInProgressResources.erase(iterFind);
  557. }
  558. auto iterFind2 = mDependantLoads.find(uuid);
  559. if (iterFind2 != mDependantLoads.end())
  560. dependantLoads = iterFind2->second;
  561. if (finishLoad)
  562. {
  563. mDependantLoads.erase(uuid);
  564. // If loadedData is null then we're probably completing load on an already loaded resource, triggered
  565. // by its dependencies.
  566. if (myLoadData != nullptr && myLoadData->loadedData != nullptr)
  567. {
  568. BS_LOCK_MUTEX(mLoadedResourceMutex);
  569. mLoadedResources[uuid] = myLoadData->resData;
  570. resource.setHandleData(myLoadData->loadedData, uuid);
  571. }
  572. for (auto& dependantLoad : dependantLoads)
  573. dependantLoad->remainingDependencies--;
  574. }
  575. }
  576. for (auto& dependantLoad : dependantLoads)
  577. {
  578. HResource dependant = dependantLoad->resData.resource.lock();
  579. loadComplete(dependant);
  580. }
  581. if (finishLoad && myLoadData != nullptr)
  582. {
  583. onResourceLoaded(resource);
  584. // This should only ever be true on the main thread
  585. if (myLoadData->notifyImmediately)
  586. ResourceListenerManager::instance().notifyListeners(uuid);
  587. bs_delete(myLoadData);
  588. }
  589. }
  590. void Resources::loadCallback(const Path& filePath, HResource& resource)
  591. {
  592. ResourcePtr rawResource = loadFromDiskAndDeserialize(filePath);
  593. {
  594. BS_LOCK_MUTEX(mInProgressResourcesMutex);
  595. // Check if all my dependencies are loaded
  596. ResourceLoadData* myLoadData = mInProgressResources[resource.getUUID()];
  597. myLoadData->loadedData = rawResource;
  598. myLoadData->remainingDependencies--;
  599. }
  600. loadComplete(resource);
  601. }
  602. BS_CORE_EXPORT Resources& gResources()
  603. {
  604. return Resources::instance();
  605. }
  606. }