BsResources.cpp 22 KB

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