BsProjectLibrary.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. #include "BsProjectLibrary.h"
  2. #include "BsFileSystem.h"
  3. #include "BsException.h"
  4. #include "BsResources.h"
  5. #include "BsResourceManifest.h"
  6. #include "BsImporter.h"
  7. #include "BsProjectResourceMeta.h"
  8. #include "BsResources.h"
  9. #include "BsImporter.h"
  10. #include "BsImportOptions.h"
  11. #include "BsFileSerializer.h"
  12. #include "BsDebug.h"
  13. #include "BsProjectLibraryEntries.h"
  14. #include "BsResource.h"
  15. #include "BsEditorApplication.h"
  16. #include "BsShader.h"
  17. #include <regex>
  18. using namespace std::placeholders;
  19. namespace BansheeEngine
  20. {
  21. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  22. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + GAME_RESOURCES_FOLDER_NAME;
  23. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  24. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  25. ProjectLibrary::LibraryEntry::LibraryEntry()
  26. :parent(nullptr), type(LibraryEntryType::Directory)
  27. { }
  28. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  29. :path(path), parent(parent), type(type), elementName(name)
  30. { }
  31. ProjectLibrary::ResourceEntry::ResourceEntry()
  32. : lastUpdateTime(0)
  33. { }
  34. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  35. : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  36. { }
  37. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  38. { }
  39. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  40. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  41. { }
  42. ProjectLibrary::ProjectLibrary()
  43. : mRootEntry(nullptr), mIsLoaded(false)
  44. {
  45. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  46. }
  47. ProjectLibrary::~ProjectLibrary()
  48. {
  49. clearEntries();
  50. }
  51. void ProjectLibrary::checkForModifications(const Path& fullPath)
  52. {
  53. Vector<Path> dirtyResources;
  54. checkForModifications(fullPath, true, dirtyResources);
  55. }
  56. void ProjectLibrary::checkForModifications(const Path& fullPath, bool import, Vector<Path>& dirtyResources)
  57. {
  58. if (!mResourcesFolder.includes(fullPath))
  59. return; // Folder not part of our resources path, so no modifications
  60. if(mRootEntry == nullptr)
  61. {
  62. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  63. }
  64. Path pathToSearch = fullPath;
  65. LibraryEntry* entry = findEntry(pathToSearch);
  66. if (entry == nullptr) // File could be new, try to find parent directory entry
  67. {
  68. if (FileSystem::exists(pathToSearch))
  69. {
  70. if (isMeta(pathToSearch))
  71. {
  72. Path sourceFilePath = pathToSearch;
  73. sourceFilePath.setExtension(L"");
  74. if (!FileSystem::isFile(sourceFilePath))
  75. {
  76. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  77. FileSystem::remove(pathToSearch);
  78. }
  79. }
  80. else
  81. {
  82. Path parentDirPath = pathToSearch.getParent();
  83. entry = findEntry(parentDirPath);
  84. // Cannot find parent directory. Create the needed hierarchy.
  85. DirectoryEntry* entryParent = nullptr;
  86. DirectoryEntry* newHierarchyParent = nullptr;
  87. if (entry == nullptr)
  88. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  89. else
  90. entryParent = static_cast<DirectoryEntry*>(entry);
  91. if (FileSystem::isFile(pathToSearch))
  92. {
  93. if (import)
  94. addResourceInternal(entryParent, pathToSearch);
  95. dirtyResources.push_back(pathToSearch);
  96. }
  97. else if (FileSystem::isDirectory(pathToSearch))
  98. {
  99. addDirectoryInternal(entryParent, pathToSearch);
  100. checkForModifications(pathToSearch, import, dirtyResources);
  101. }
  102. }
  103. }
  104. }
  105. else if(entry->type == LibraryEntryType::File)
  106. {
  107. if(FileSystem::isFile(entry->path))
  108. {
  109. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  110. if (import)
  111. reimportResourceInternal(resEntry);
  112. if (!isUpToDate(resEntry))
  113. dirtyResources.push_back(entry->path);
  114. }
  115. else
  116. {
  117. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  118. }
  119. }
  120. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  121. {
  122. if(!FileSystem::isDirectory(entry->path))
  123. {
  124. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  125. }
  126. else
  127. {
  128. Stack<DirectoryEntry*> todo;
  129. todo.push(static_cast<DirectoryEntry*>(entry));
  130. Vector<Path> childFiles;
  131. Vector<Path> childDirectories;
  132. Vector<bool> existingEntries;
  133. Vector<LibraryEntry*> toDelete;
  134. while(!todo.empty())
  135. {
  136. DirectoryEntry* currentDir = todo.top();
  137. todo.pop();
  138. existingEntries.clear();
  139. existingEntries.resize(currentDir->mChildren.size());
  140. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  141. existingEntries[i] = false;
  142. childFiles.clear();
  143. childDirectories.clear();
  144. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  145. for(auto& filePath : childFiles)
  146. {
  147. if(isMeta(filePath))
  148. {
  149. Path sourceFilePath = filePath;
  150. sourceFilePath.setExtension(L"");
  151. if(!FileSystem::isFile(sourceFilePath))
  152. {
  153. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  154. FileSystem::remove(filePath);
  155. }
  156. }
  157. else
  158. {
  159. ResourceEntry* existingEntry = nullptr;
  160. UINT32 idx = 0;
  161. for(auto& child : currentDir->mChildren)
  162. {
  163. if(child->type == LibraryEntryType::File && child->path == filePath)
  164. {
  165. existingEntries[idx] = true;
  166. existingEntry = static_cast<ResourceEntry*>(child);
  167. break;
  168. }
  169. idx++;
  170. }
  171. if(existingEntry != nullptr)
  172. {
  173. if (import)
  174. reimportResourceInternal(existingEntry);
  175. if (!isUpToDate(existingEntry))
  176. dirtyResources.push_back(existingEntry->path);
  177. }
  178. else
  179. {
  180. if (import)
  181. addResourceInternal(currentDir, filePath);
  182. dirtyResources.push_back(filePath);
  183. }
  184. }
  185. }
  186. for(auto& dirPath : childDirectories)
  187. {
  188. DirectoryEntry* existingEntry = nullptr;
  189. UINT32 idx = 0;
  190. for(auto& child : currentDir->mChildren)
  191. {
  192. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  193. {
  194. existingEntries[idx] = true;
  195. existingEntry = static_cast<DirectoryEntry*>(child);
  196. break;
  197. }
  198. idx++;
  199. }
  200. if(existingEntry == nullptr)
  201. addDirectoryInternal(currentDir, dirPath);
  202. }
  203. {
  204. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  205. {
  206. if(existingEntries[i])
  207. continue;
  208. toDelete.push_back(currentDir->mChildren[i]);
  209. }
  210. for(auto& child : toDelete)
  211. {
  212. if(child->type == LibraryEntryType::Directory)
  213. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  214. else if(child->type == LibraryEntryType::File)
  215. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  216. }
  217. toDelete.clear();
  218. }
  219. for(auto& child : currentDir->mChildren)
  220. {
  221. if(child->type == LibraryEntryType::Directory)
  222. todo.push(static_cast<DirectoryEntry*>(child));
  223. }
  224. }
  225. }
  226. }
  227. }
  228. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  229. const ImportOptionsPtr& importOptions, bool forceReimport)
  230. {
  231. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  232. parent->mChildren.push_back(newResource);
  233. reimportResourceInternal(newResource, importOptions, forceReimport);
  234. onEntryAdded(newResource->path);
  235. return newResource;
  236. }
  237. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  238. {
  239. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  240. parent->mChildren.push_back(newEntry);
  241. onEntryAdded(newEntry->path);
  242. return newEntry;
  243. }
  244. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  245. {
  246. if(resource->meta != nullptr)
  247. {
  248. String uuid = resource->meta->getUUID();
  249. Path path;
  250. if (mResourceManifest->uuidToFilePath(uuid, path))
  251. {
  252. if(FileSystem::isFile(path))
  253. FileSystem::remove(path);
  254. mResourceManifest->unregisterResource(uuid);
  255. }
  256. mUUIDToPath.erase(uuid);
  257. }
  258. Path metaPath = getMetaPath(resource->path);
  259. if (FileSystem::isFile(metaPath))
  260. FileSystem::remove(metaPath);
  261. DirectoryEntry* parent = resource->parent;
  262. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  263. [&] (const LibraryEntry* entry) { return entry == resource; });
  264. parent->mChildren.erase(findIter);
  265. Path originalPath = resource->path;
  266. onEntryRemoved(originalPath);
  267. removeDependencies(resource);
  268. bs_delete(resource);
  269. reimportDependants(originalPath);
  270. }
  271. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  272. {
  273. if(directory == mRootEntry)
  274. mRootEntry = nullptr;
  275. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  276. for(auto& child : childrenToDestroy)
  277. {
  278. if(child->type == LibraryEntryType::Directory)
  279. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  280. else
  281. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  282. }
  283. DirectoryEntry* parent = directory->parent;
  284. if(parent != nullptr)
  285. {
  286. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  287. [&] (const LibraryEntry* entry) { return entry == directory; });
  288. parent->mChildren.erase(findIter);
  289. }
  290. onEntryRemoved(directory->path);
  291. bs_delete(directory);
  292. }
  293. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  294. {
  295. Path metaPath = resource->path;
  296. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  297. if(resource->meta == nullptr)
  298. {
  299. if(FileSystem::isFile(metaPath))
  300. {
  301. FileDecoder fs(metaPath);
  302. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  303. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  304. {
  305. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  306. resource->meta = resourceMeta;
  307. mUUIDToPath[resourceMeta->getUUID()] = resource->path;
  308. }
  309. }
  310. }
  311. if (!isUpToDate(resource) || forceReimport)
  312. {
  313. // Note: If resource is native we just copy it to the internal folder. We could avoid the copy and
  314. // load the resource directly from the Resources folder but that requires complicating library code.
  315. bool isNativeResource = isNative(resource);
  316. ImportOptionsPtr curImportOptions = nullptr;
  317. if (importOptions == nullptr && !isNativeResource)
  318. {
  319. if (resource->meta != nullptr)
  320. curImportOptions = resource->meta->getImportOptions();
  321. else
  322. curImportOptions = Importer::instance().createImportOptions(resource->path);
  323. }
  324. else
  325. curImportOptions = importOptions;
  326. bool unloadWhenDone = false;
  327. HResource importedResource;
  328. if (isNativeResource)
  329. {
  330. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  331. // This can happen if library isn't properly saved before exiting the application.
  332. if (resource->meta != nullptr)
  333. {
  334. mResourceManifest->registerResource(resource->meta->getUUID(), resource->path);
  335. unloadWhenDone = !gResources().isLoaded(resource->meta->getUUID());
  336. }
  337. // Don't load dependencies because we don't need them, but also because they might not be in the manifest
  338. // which would screw up their UUIDs.
  339. importedResource = gResources().load(resource->path, false);
  340. }
  341. if(resource->meta == nullptr)
  342. {
  343. if (!isNativeResource)
  344. {
  345. importedResource = Importer::instance().import(resource->path, curImportOptions);
  346. unloadWhenDone = true;
  347. }
  348. if (importedResource != nullptr)
  349. {
  350. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  351. UINT32 typeId = importedResource->getTypeId();
  352. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  353. }
  354. else
  355. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), 0, nullptr, curImportOptions);
  356. FileEncoder fs(metaPath);
  357. fs.encode(resource->meta.get());
  358. mUUIDToPath[resource->meta->getUUID()] = resource->path;
  359. }
  360. else
  361. {
  362. removeDependencies(resource);
  363. if (!isNativeResource)
  364. {
  365. importedResource = gResources()._createResourceHandle(resource->meta->getUUID());
  366. if (!importedResource.isLoaded(false))
  367. unloadWhenDone = true;
  368. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  369. }
  370. }
  371. addDependencies(resource);
  372. if (importedResource != nullptr)
  373. {
  374. Path internalResourcesPath = mProjectFolder;
  375. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  376. if (!FileSystem::isDirectory(internalResourcesPath))
  377. FileSystem::createDir(internalResourcesPath);
  378. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  379. gResources().save(importedResource, internalResourcesPath, true);
  380. if (unloadWhenDone)
  381. gResources().unload(importedResource);
  382. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  383. }
  384. resource->lastUpdateTime = std::time(nullptr);
  385. onEntryImported(resource->path);
  386. reimportDependants(resource->path);
  387. }
  388. }
  389. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  390. {
  391. if(resource->meta == nullptr)
  392. return false;
  393. Path internalPath;
  394. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), internalPath))
  395. return false;
  396. if(!FileSystem::isFile(internalPath))
  397. return false;
  398. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  399. return lastModifiedTime <= resource->lastUpdateTime;
  400. }
  401. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  402. {
  403. return search(pattern, {});
  404. }
  405. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  406. {
  407. Vector<LibraryEntry*> foundEntries;
  408. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  409. WString replace(L"\\\\&");
  410. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  411. std::wregex wildcard(L"\\\\\\*");
  412. WString wildcardReplace(L".*");
  413. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  414. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  415. Stack<DirectoryEntry*> todo;
  416. todo.push(mRootEntry);
  417. while (!todo.empty())
  418. {
  419. DirectoryEntry* dirEntry = todo.top();
  420. todo.pop();
  421. for (auto& child : dirEntry->mChildren)
  422. {
  423. if (std::regex_match(child->elementName, searchRegex))
  424. {
  425. if (typeIds.size() == 0)
  426. foundEntries.push_back(child);
  427. else
  428. {
  429. if (child->type == LibraryEntryType::File)
  430. {
  431. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  432. for (auto& typeId : typeIds)
  433. {
  434. if (childResEntry->meta != nullptr && childResEntry->meta->getTypeID() == typeId)
  435. {
  436. foundEntries.push_back(child);
  437. break;
  438. }
  439. }
  440. }
  441. }
  442. }
  443. if (child->type == LibraryEntryType::Directory)
  444. {
  445. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  446. todo.push(childDirEntry);
  447. }
  448. }
  449. }
  450. std::sort(foundEntries.begin(), foundEntries.end(),
  451. [&](const LibraryEntry* a, const LibraryEntry* b)
  452. {
  453. return a->elementName.compare(b->elementName) < 0;
  454. });
  455. return foundEntries;
  456. }
  457. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  458. {
  459. Path fullPath = path;
  460. if (fullPath.isAbsolute())
  461. {
  462. if (!mResourcesFolder.includes(fullPath))
  463. return nullptr;
  464. }
  465. else
  466. fullPath.makeAbsolute(mResourcesFolder);
  467. Path relPath = fullPath.getRelative(mRootEntry->path);
  468. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  469. UINT32 idx = 0;
  470. LibraryEntry* current = mRootEntry;
  471. while (current != nullptr)
  472. {
  473. if (idx == numElems)
  474. return current;
  475. WString curElem;
  476. if (relPath.isFile() && idx == (numElems - 1))
  477. curElem = relPath.getWFilename();
  478. else
  479. curElem = relPath[idx];
  480. if (current->type == LibraryEntryType::Directory)
  481. {
  482. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  483. current = nullptr;
  484. for (auto& child : dirEntry->mChildren)
  485. {
  486. if (Path::comparePathElem(curElem, child->elementName))
  487. {
  488. idx++;
  489. current = child;
  490. break;
  491. }
  492. }
  493. }
  494. else
  495. break;
  496. }
  497. return nullptr;
  498. }
  499. Path ProjectLibrary::uuidToPath(const String& uuid) const
  500. {
  501. auto iterFind = mUUIDToPath.find(uuid);
  502. if (iterFind != mUUIDToPath.end())
  503. return iterFind->second;
  504. return Path::BLANK;
  505. }
  506. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  507. {
  508. if (resource == nullptr)
  509. return;
  510. Path assetPath = path;
  511. if (path.isAbsolute())
  512. {
  513. if (!getResourcesFolder().includes(path))
  514. return;
  515. assetPath = path.getRelative(getResourcesFolder());
  516. }
  517. LibraryEntry* existingEntry = findEntry(assetPath);
  518. if (existingEntry != nullptr)
  519. {
  520. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  521. return;
  522. }
  523. resource->setName(path.getWFilename(false));
  524. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  525. Resources::instance().save(resource, absPath, false);
  526. checkForModifications(absPath);
  527. }
  528. void ProjectLibrary::saveEntry(const HResource& resource)
  529. {
  530. if (resource == nullptr)
  531. return;
  532. Path filePath = uuidToPath(resource.getUUID());
  533. filePath.makeAbsolute(getResourcesFolder());
  534. Resources::instance().save(resource, filePath, true);
  535. checkForModifications(filePath);
  536. }
  537. void ProjectLibrary::createFolderEntry(const Path& path)
  538. {
  539. Path fullPath = path;
  540. if (fullPath.isAbsolute())
  541. {
  542. if (!mResourcesFolder.includes(fullPath))
  543. return;
  544. }
  545. else
  546. fullPath.makeAbsolute(mResourcesFolder);
  547. if (FileSystem::isDirectory(fullPath))
  548. return; // Already exists
  549. FileSystem::createDir(fullPath);
  550. Path parentPath = fullPath.getParent();
  551. DirectoryEntry* newEntryParent = nullptr;
  552. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  553. if (newEntryParentLib != nullptr)
  554. {
  555. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  556. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  557. }
  558. DirectoryEntry* newHierarchyParent = nullptr;
  559. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  560. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  561. addDirectoryInternal(newEntryParent, fullPath);
  562. }
  563. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  564. {
  565. Path oldFullPath = oldPath;
  566. if (!oldFullPath.isAbsolute())
  567. oldFullPath.makeAbsolute(mResourcesFolder);
  568. Path newFullPath = newPath;
  569. if (!newFullPath.isAbsolute())
  570. newFullPath.makeAbsolute(mResourcesFolder);
  571. Path parentPath = newFullPath.getParent();
  572. if (!FileSystem::isDirectory(parentPath))
  573. {
  574. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  575. return;
  576. }
  577. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  578. FileSystem::move(oldFullPath, newFullPath, overwrite);
  579. Path oldMetaPath = getMetaPath(oldFullPath);
  580. Path newMetaPath = getMetaPath(newFullPath);
  581. LibraryEntry* oldEntry = findEntry(oldFullPath);
  582. if(oldEntry != nullptr) // Moving from the Resources folder
  583. {
  584. // Moved outside of Resources, delete entry & meta file
  585. if (!mResourcesFolder.includes(newFullPath))
  586. {
  587. if(oldEntry->type == LibraryEntryType::File)
  588. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  589. else if(oldEntry->type == LibraryEntryType::Directory)
  590. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  591. }
  592. else // Just moving internally
  593. {
  594. onEntryRemoved(oldEntry->path);
  595. ResourceEntry* resEntry = nullptr;
  596. if (oldEntry->type == LibraryEntryType::File)
  597. {
  598. resEntry = static_cast<ResourceEntry*>(oldEntry);
  599. removeDependencies(resEntry);
  600. }
  601. if(FileSystem::isFile(oldMetaPath))
  602. FileSystem::move(oldMetaPath, newMetaPath);
  603. DirectoryEntry* parent = oldEntry->parent;
  604. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  605. if(findIter != parent->mChildren.end())
  606. parent->mChildren.erase(findIter);
  607. Path parentPath = newFullPath.getParent();
  608. DirectoryEntry* newEntryParent = nullptr;
  609. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  610. if(newEntryParentLib != nullptr)
  611. {
  612. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  613. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  614. }
  615. DirectoryEntry* newHierarchyParent = nullptr;
  616. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  617. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  618. newEntryParent->mChildren.push_back(oldEntry);
  619. oldEntry->parent = newEntryParent;
  620. oldEntry->path = newFullPath;
  621. oldEntry->elementName = newFullPath.getWTail();
  622. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  623. {
  624. Stack<LibraryEntry*> todo;
  625. todo.push(oldEntry);
  626. while(!todo.empty())
  627. {
  628. LibraryEntry* curEntry = todo.top();
  629. todo.pop();
  630. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  631. for(auto& child : curDirEntry->mChildren)
  632. {
  633. child->path = child->parent->path;
  634. child->path.append(child->elementName);
  635. if(child->type == LibraryEntryType::Directory)
  636. todo.push(child);
  637. }
  638. }
  639. }
  640. onEntryAdded(oldEntry->path);
  641. if (resEntry != nullptr)
  642. {
  643. reimportDependants(oldFullPath);
  644. reimportDependants(newFullPath);
  645. }
  646. }
  647. }
  648. else // Moving from outside of the Resources folder (likely adding a new resource)
  649. {
  650. checkForModifications(newFullPath);
  651. }
  652. }
  653. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  654. {
  655. Path oldFullPath = oldPath;
  656. if (!oldFullPath.isAbsolute())
  657. oldFullPath.makeAbsolute(mResourcesFolder);
  658. Path newFullPath = newPath;
  659. if (!newFullPath.isAbsolute())
  660. newFullPath.makeAbsolute(mResourcesFolder);
  661. if (!FileSystem::exists(oldFullPath))
  662. return;
  663. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  664. // Copying a file/folder outside of the Resources folder, no special handling needed
  665. if (!mResourcesFolder.includes(newFullPath))
  666. return;
  667. Path parentPath = newFullPath.getParent();
  668. DirectoryEntry* newEntryParent = nullptr;
  669. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  670. if (newEntryParentLib != nullptr)
  671. {
  672. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  673. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  674. }
  675. // If the source is outside of Resources folder, just plain import the copy
  676. LibraryEntry* oldEntry = findEntry(oldFullPath);
  677. if (oldEntry == nullptr)
  678. {
  679. checkForModifications(newFullPath);
  680. return;
  681. }
  682. // Both source and destination are within Resources folder, need to preserve import options on the copies
  683. LibraryEntry* newEntry = nullptr;
  684. if (FileSystem::isFile(newFullPath))
  685. {
  686. assert(oldEntry->type == LibraryEntryType::File);
  687. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  688. ImportOptionsPtr importOptions;
  689. if (oldResEntry->meta != nullptr)
  690. importOptions = oldResEntry->meta->getImportOptions();
  691. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  692. }
  693. else
  694. {
  695. assert(oldEntry->type == LibraryEntryType::File);
  696. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  697. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  698. newEntry = newDirEntry;
  699. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  700. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  701. while (!todo.empty())
  702. {
  703. auto current = todo.top();
  704. todo.pop();
  705. DirectoryEntry* sourceDir = std::get<0>(current);
  706. DirectoryEntry* destDir = std::get<1>(current);
  707. for (auto& child : sourceDir->mChildren)
  708. {
  709. Path childDestPath = destDir->path;
  710. childDestPath.append(child->path.getWTail());
  711. if (child->type == LibraryEntryType::File)
  712. {
  713. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  714. ImportOptionsPtr importOptions;
  715. if (childResEntry->meta != nullptr)
  716. importOptions = childResEntry->meta->getImportOptions();
  717. addResourceInternal(destDir, childDestPath, importOptions, true);
  718. }
  719. else // Directory
  720. {
  721. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  722. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  723. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  724. }
  725. }
  726. }
  727. }
  728. }
  729. void ProjectLibrary::deleteEntry(const Path& path)
  730. {
  731. Path fullPath = path;
  732. if (!fullPath.isAbsolute())
  733. fullPath.makeAbsolute(mResourcesFolder);
  734. if(FileSystem::exists(fullPath))
  735. FileSystem::remove(fullPath);
  736. LibraryEntry* entry = findEntry(fullPath);
  737. if(entry != nullptr)
  738. {
  739. if(entry->type == LibraryEntryType::File)
  740. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  741. else if(entry->type == LibraryEntryType::Directory)
  742. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  743. }
  744. }
  745. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  746. {
  747. LibraryEntry* entry = findEntry(path);
  748. if (entry != nullptr)
  749. {
  750. if (entry->type == LibraryEntryType::File)
  751. {
  752. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  753. reimportResourceInternal(resEntry, importOptions, forceReimport);
  754. }
  755. }
  756. }
  757. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  758. {
  759. LibraryEntry* entry = findEntry(path);
  760. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  761. return;
  762. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  763. if (resEntry->meta == nullptr)
  764. return;
  765. resEntry->meta->setIncludeInBuild(include);
  766. Path metaPath = resEntry->path;
  767. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  768. FileEncoder fs(metaPath);
  769. fs.encode(resEntry->meta.get());
  770. }
  771. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  772. {
  773. Vector<ResourceEntry*> output;
  774. Stack<DirectoryEntry*> todo;
  775. todo.push(mRootEntry);
  776. while (!todo.empty())
  777. {
  778. DirectoryEntry* directory = todo.top();
  779. todo.pop();
  780. for (auto& child : directory->mChildren)
  781. {
  782. if (child->type == LibraryEntryType::File)
  783. {
  784. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  785. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  786. output.push_back(resEntry);
  787. }
  788. else if (child->type == LibraryEntryType::Directory)
  789. {
  790. todo.push(static_cast<DirectoryEntry*>(child));
  791. }
  792. }
  793. }
  794. return output;
  795. }
  796. HResource ProjectLibrary::load(const Path& path)
  797. {
  798. LibraryEntry* entry = findEntry(path);
  799. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  800. return HResource();
  801. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  802. if (resEntry->meta == nullptr)
  803. return HResource();
  804. String resUUID = resEntry->meta->getUUID();
  805. return gResources().loadFromUUID(resUUID);
  806. }
  807. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  808. {
  809. Path parentPath = fullPath;
  810. DirectoryEntry* newEntryParent = nullptr;
  811. Stack<Path> parentPaths;
  812. do
  813. {
  814. Path newParentPath = parentPath.getParent();
  815. if(newParentPath == parentPath)
  816. break;
  817. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  818. if(newEntryParentLib != nullptr)
  819. {
  820. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  821. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  822. break;
  823. }
  824. parentPaths.push(newParentPath);
  825. parentPath = newParentPath;
  826. } while (true);
  827. assert(newEntryParent != nullptr); // Must exist
  828. if(newHierarchyRoot != nullptr)
  829. *newHierarchyRoot = newEntryParent;
  830. while(!parentPaths.empty())
  831. {
  832. Path curPath = parentPaths.top();
  833. parentPaths.pop();
  834. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  835. }
  836. if(newHierarchyLeaf != nullptr)
  837. *newHierarchyLeaf = newEntryParent;
  838. }
  839. Path ProjectLibrary::getMetaPath(const Path& path) const
  840. {
  841. Path metaPath = path;
  842. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  843. return metaPath;
  844. }
  845. bool ProjectLibrary::isMeta(const Path& fullPath) const
  846. {
  847. return fullPath.getWExtension() == L".meta";
  848. }
  849. bool ProjectLibrary::isNative(ResourceEntry* resource) const
  850. {
  851. WString extension = resource->path.getWExtension();
  852. return extension == L".asset" || extension == L".prefab";
  853. }
  854. void ProjectLibrary::unloadLibrary()
  855. {
  856. if (!mIsLoaded)
  857. return;
  858. mProjectFolder = Path::BLANK;
  859. mResourcesFolder = Path::BLANK;
  860. clearEntries();
  861. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  862. mDependencies.clear();
  863. gResources().unregisterResourceManifest(mResourceManifest);
  864. mResourceManifest = nullptr;
  865. mIsLoaded = false;
  866. }
  867. void ProjectLibrary::makeEntriesRelative()
  868. {
  869. // Make all paths relative before saving
  870. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  871. [&](LibraryEntry* entry, const Path& root)
  872. {
  873. entry->path.makeRelative(root);
  874. if (entry->type == LibraryEntryType::Directory)
  875. {
  876. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  877. for (auto& child : dirEntry->mChildren)
  878. makeRelative(child, root);
  879. }
  880. };
  881. Path root = getResourcesFolder();
  882. makeRelative(mRootEntry, root);
  883. }
  884. void ProjectLibrary::makeEntriesAbsolute()
  885. {
  886. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  887. [&](LibraryEntry* entry, const Path& root)
  888. {
  889. entry->path.makeAbsolute(root);
  890. if (entry->type == LibraryEntryType::Directory)
  891. {
  892. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  893. for (auto& child : dirEntry->mChildren)
  894. makeAbsolute(child, root);
  895. }
  896. };
  897. Path root = getResourcesFolder();
  898. makeAbsolute(mRootEntry, root);
  899. }
  900. void ProjectLibrary::saveLibrary()
  901. {
  902. if (!mIsLoaded)
  903. return;
  904. // Make all paths relative before saving
  905. makeEntriesRelative();
  906. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  907. Path libraryEntriesPath = mProjectFolder;
  908. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  909. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  910. FileEncoder fs(libraryEntriesPath);
  911. fs.encode(libEntries.get());
  912. // Restore absolute entry paths
  913. makeEntriesAbsolute();
  914. Path resourceManifestPath = mProjectFolder;
  915. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  916. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  917. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  918. }
  919. void ProjectLibrary::loadLibrary()
  920. {
  921. unloadLibrary();
  922. mProjectFolder = gEditorApplication().getProjectPath();
  923. mResourcesFolder = mProjectFolder;
  924. mResourcesFolder.append(RESOURCES_DIR);
  925. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  926. Path libraryEntriesPath = mProjectFolder;
  927. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  928. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  929. if(FileSystem::exists(libraryEntriesPath))
  930. {
  931. FileDecoder fs(libraryEntriesPath);
  932. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  933. *mRootEntry = libEntries->getRootEntry();
  934. for(auto& child : mRootEntry->mChildren)
  935. child->parent = mRootEntry;
  936. mRootEntry->parent = nullptr;
  937. }
  938. // Entries are stored relative to project folder, but we want their absolute paths now
  939. makeEntriesAbsolute();
  940. // Load resource manifest
  941. Path resourceManifestPath = mProjectFolder;
  942. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  943. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  944. if (FileSystem::exists(resourceManifestPath))
  945. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  946. else
  947. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  948. gResources().registerResourceManifest(mResourceManifest);
  949. // Load all meta files
  950. Stack<DirectoryEntry*> todo;
  951. todo.push(mRootEntry);
  952. Vector<LibraryEntry*> deletedEntries;
  953. while(!todo.empty())
  954. {
  955. DirectoryEntry* curDir = todo.top();
  956. todo.pop();
  957. for(auto& child : curDir->mChildren)
  958. {
  959. if(child->type == LibraryEntryType::File)
  960. {
  961. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  962. if (FileSystem::isFile(resEntry->path))
  963. {
  964. if (resEntry->meta == nullptr)
  965. {
  966. Path metaPath = resEntry->path;
  967. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  968. if (FileSystem::isFile(metaPath))
  969. {
  970. FileDecoder fs(metaPath);
  971. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  972. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  973. {
  974. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  975. resEntry->meta = resourceMeta;
  976. }
  977. }
  978. }
  979. if (resEntry->meta != nullptr)
  980. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  981. addDependencies(resEntry);
  982. }
  983. else
  984. deletedEntries.push_back(resEntry);
  985. }
  986. else if(child->type == LibraryEntryType::Directory)
  987. {
  988. if (FileSystem::isDirectory(child->path))
  989. todo.push(static_cast<DirectoryEntry*>(child));
  990. else
  991. deletedEntries.push_back(child);
  992. }
  993. }
  994. }
  995. // Remove entries that no longer have corresponding files
  996. for (auto& deletedEntry : deletedEntries)
  997. {
  998. if (deletedEntry->type == LibraryEntryType::File)
  999. {
  1000. ResourceEntry* resEntry = static_cast<ResourceEntry*>(deletedEntry);
  1001. deleteResourceInternal(resEntry);
  1002. }
  1003. else
  1004. {
  1005. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1006. deleteDirectoryInternal(dirEntry);
  1007. }
  1008. }
  1009. // Clean up internal library folder from obsolete files
  1010. Path internalResourcesFolder = mProjectFolder;
  1011. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1012. Vector<Path> toDelete;
  1013. auto processFile = [&](const Path& file)
  1014. {
  1015. String uuid = file.getFilename(false);
  1016. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1017. {
  1018. mResourceManifest->unregisterResource(uuid);
  1019. toDelete.push_back(file);
  1020. }
  1021. return true;
  1022. };
  1023. FileSystem::iterate(internalResourcesFolder, processFile);
  1024. for (auto& entry : toDelete)
  1025. FileSystem::remove(entry);
  1026. mIsLoaded = true;
  1027. }
  1028. void ProjectLibrary::clearEntries()
  1029. {
  1030. if (mRootEntry == nullptr)
  1031. return;
  1032. std::function<void(LibraryEntry*)> deleteRecursive =
  1033. [&](LibraryEntry* entry)
  1034. {
  1035. if (entry->type == LibraryEntryType::Directory)
  1036. {
  1037. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1038. for (auto& child : dirEntry->mChildren)
  1039. deleteRecursive(child);
  1040. }
  1041. bs_delete(entry);
  1042. };
  1043. deleteRecursive(mRootEntry);
  1044. mRootEntry = nullptr;
  1045. }
  1046. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1047. {
  1048. Vector<Path> output;
  1049. if (entry->meta == nullptr)
  1050. return output;
  1051. if (entry->meta->getTypeID() == TID_Shader)
  1052. {
  1053. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1054. for (auto& include : metaData->includes)
  1055. output.push_back(include);
  1056. }
  1057. return output;
  1058. }
  1059. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1060. {
  1061. Vector<Path> dependencies = getImportDependencies(entry);
  1062. for (auto& dependency : dependencies)
  1063. mDependencies[dependency].push_back(entry->path);
  1064. }
  1065. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1066. {
  1067. Vector<Path> dependencies = getImportDependencies(entry);
  1068. for (auto& dependency : dependencies)
  1069. {
  1070. Vector<Path>& curDependencies = mDependencies[dependency];
  1071. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1072. [&](const Path& x)
  1073. {
  1074. return x == entry->path;
  1075. });
  1076. curDependencies.erase(iterRemove, curDependencies.end());
  1077. }
  1078. }
  1079. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1080. {
  1081. auto iterFind = mDependencies.find(entryPath);
  1082. if (iterFind == mDependencies.end())
  1083. return;
  1084. // Make a copy since we might modify this list during reimport
  1085. Vector<Path> dependencies = iterFind->second;
  1086. for (auto& dependency : dependencies)
  1087. {
  1088. LibraryEntry* entry = findEntry(dependency);
  1089. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1090. {
  1091. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  1092. ImportOptionsPtr importOptions;
  1093. if (resEntry->meta != nullptr)
  1094. importOptions = resEntry->meta->getImportOptions();
  1095. reimportResourceInternal(resEntry, importOptions, true);
  1096. }
  1097. }
  1098. }
  1099. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1100. {
  1101. return ProjectLibrary::instance();
  1102. }
  1103. }