BsProjectLibrary.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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()._getResourceHandle(resource->meta->getUUID());
  366. if (importedResource == nullptr)
  367. {
  368. importedResource = HResource(resource->meta->getUUID());
  369. unloadWhenDone = true;
  370. }
  371. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  372. }
  373. }
  374. addDependencies(resource);
  375. if (importedResource != nullptr)
  376. {
  377. Path internalResourcesPath = mProjectFolder;
  378. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  379. if (!FileSystem::isDirectory(internalResourcesPath))
  380. FileSystem::createDir(internalResourcesPath);
  381. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  382. gResources().save(importedResource, internalResourcesPath, true);
  383. if (unloadWhenDone)
  384. gResources().unload(importedResource);
  385. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  386. }
  387. resource->lastUpdateTime = std::time(nullptr);
  388. onEntryImported(resource->path);
  389. reimportDependants(resource->path);
  390. }
  391. }
  392. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  393. {
  394. if(resource->meta == nullptr)
  395. return false;
  396. Path internalPath;
  397. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), internalPath))
  398. return false;
  399. if(!FileSystem::isFile(internalPath))
  400. return false;
  401. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  402. return lastModifiedTime <= resource->lastUpdateTime;
  403. }
  404. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  405. {
  406. return search(pattern, {});
  407. }
  408. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  409. {
  410. Vector<LibraryEntry*> foundEntries;
  411. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  412. WString replace(L"\\\\&");
  413. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  414. std::wregex wildcard(L"\\\\\\*");
  415. WString wildcardReplace(L".*");
  416. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  417. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  418. Stack<DirectoryEntry*> todo;
  419. todo.push(mRootEntry);
  420. while (!todo.empty())
  421. {
  422. DirectoryEntry* dirEntry = todo.top();
  423. todo.pop();
  424. for (auto& child : dirEntry->mChildren)
  425. {
  426. if (std::regex_match(child->elementName, searchRegex))
  427. {
  428. if (typeIds.size() == 0)
  429. foundEntries.push_back(child);
  430. else
  431. {
  432. if (child->type == LibraryEntryType::File)
  433. {
  434. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  435. for (auto& typeId : typeIds)
  436. {
  437. if (childResEntry->meta != nullptr && childResEntry->meta->getTypeID() == typeId)
  438. {
  439. foundEntries.push_back(child);
  440. break;
  441. }
  442. }
  443. }
  444. }
  445. }
  446. if (child->type == LibraryEntryType::Directory)
  447. {
  448. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  449. todo.push(childDirEntry);
  450. }
  451. }
  452. }
  453. std::sort(foundEntries.begin(), foundEntries.end(),
  454. [&](const LibraryEntry* a, const LibraryEntry* b)
  455. {
  456. return a->elementName.compare(b->elementName) < 0;
  457. });
  458. return foundEntries;
  459. }
  460. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  461. {
  462. Path fullPath = path;
  463. if (fullPath.isAbsolute())
  464. {
  465. if (!mResourcesFolder.includes(fullPath))
  466. return nullptr;
  467. }
  468. else
  469. fullPath.makeAbsolute(mResourcesFolder);
  470. Path relPath = fullPath.getRelative(mRootEntry->path);
  471. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  472. UINT32 idx = 0;
  473. LibraryEntry* current = mRootEntry;
  474. while (current != nullptr)
  475. {
  476. if (idx == numElems)
  477. return current;
  478. WString curElem;
  479. if (relPath.isFile() && idx == (numElems - 1))
  480. curElem = relPath.getWFilename();
  481. else
  482. curElem = relPath[idx];
  483. if (current->type == LibraryEntryType::Directory)
  484. {
  485. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  486. current = nullptr;
  487. for (auto& child : dirEntry->mChildren)
  488. {
  489. if (Path::comparePathElem(curElem, child->elementName))
  490. {
  491. idx++;
  492. current = child;
  493. break;
  494. }
  495. }
  496. }
  497. else
  498. break;
  499. }
  500. return nullptr;
  501. }
  502. Path ProjectLibrary::uuidToPath(const String& uuid) const
  503. {
  504. auto iterFind = mUUIDToPath.find(uuid);
  505. if (iterFind != mUUIDToPath.end())
  506. return iterFind->second;
  507. return Path::BLANK;
  508. }
  509. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  510. {
  511. if (resource == nullptr)
  512. return;
  513. Path assetPath = path;
  514. if (path.isAbsolute())
  515. {
  516. if (!getResourcesFolder().includes(path))
  517. return;
  518. assetPath = path.getRelative(getResourcesFolder());
  519. }
  520. LibraryEntry* existingEntry = findEntry(assetPath);
  521. if (existingEntry != nullptr)
  522. {
  523. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  524. return;
  525. }
  526. resource->setName(path.getWFilename(false));
  527. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  528. Resources::instance().save(resource, absPath, false);
  529. checkForModifications(absPath);
  530. }
  531. void ProjectLibrary::saveEntry(const HResource& resource)
  532. {
  533. if (resource == nullptr)
  534. return;
  535. Path filePath = uuidToPath(resource.getUUID());
  536. filePath.makeAbsolute(getResourcesFolder());
  537. Resources::instance().save(resource, filePath, true);
  538. checkForModifications(filePath);
  539. }
  540. void ProjectLibrary::createFolderEntry(const Path& path)
  541. {
  542. Path fullPath = path;
  543. if (fullPath.isAbsolute())
  544. {
  545. if (!mResourcesFolder.includes(fullPath))
  546. return;
  547. }
  548. else
  549. fullPath.makeAbsolute(mResourcesFolder);
  550. if (FileSystem::isDirectory(fullPath))
  551. return; // Already exists
  552. FileSystem::createDir(fullPath);
  553. Path parentPath = fullPath.getParent();
  554. DirectoryEntry* newEntryParent = nullptr;
  555. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  556. if (newEntryParentLib != nullptr)
  557. {
  558. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  559. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  560. }
  561. DirectoryEntry* newHierarchyParent = nullptr;
  562. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  563. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  564. addDirectoryInternal(newEntryParent, fullPath);
  565. }
  566. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  567. {
  568. Path oldFullPath = oldPath;
  569. if (!oldFullPath.isAbsolute())
  570. oldFullPath.makeAbsolute(mResourcesFolder);
  571. Path newFullPath = newPath;
  572. if (!newFullPath.isAbsolute())
  573. newFullPath.makeAbsolute(mResourcesFolder);
  574. Path parentPath = newFullPath.getParent();
  575. if (!FileSystem::isDirectory(parentPath))
  576. {
  577. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  578. return;
  579. }
  580. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  581. FileSystem::move(oldFullPath, newFullPath, overwrite);
  582. Path oldMetaPath = getMetaPath(oldFullPath);
  583. Path newMetaPath = getMetaPath(newFullPath);
  584. LibraryEntry* oldEntry = findEntry(oldFullPath);
  585. if(oldEntry != nullptr) // Moving from the Resources folder
  586. {
  587. // Moved outside of Resources, delete entry & meta file
  588. if (!mResourcesFolder.includes(newFullPath))
  589. {
  590. if(oldEntry->type == LibraryEntryType::File)
  591. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  592. else if(oldEntry->type == LibraryEntryType::Directory)
  593. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  594. }
  595. else // Just moving internally
  596. {
  597. onEntryRemoved(oldEntry->path);
  598. ResourceEntry* resEntry = nullptr;
  599. if (oldEntry->type == LibraryEntryType::File)
  600. {
  601. resEntry = static_cast<ResourceEntry*>(oldEntry);
  602. removeDependencies(resEntry);
  603. }
  604. if(FileSystem::isFile(oldMetaPath))
  605. FileSystem::move(oldMetaPath, newMetaPath);
  606. DirectoryEntry* parent = oldEntry->parent;
  607. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  608. if(findIter != parent->mChildren.end())
  609. parent->mChildren.erase(findIter);
  610. Path parentPath = newFullPath.getParent();
  611. DirectoryEntry* newEntryParent = nullptr;
  612. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  613. if(newEntryParentLib != nullptr)
  614. {
  615. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  616. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  617. }
  618. DirectoryEntry* newHierarchyParent = nullptr;
  619. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  620. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  621. newEntryParent->mChildren.push_back(oldEntry);
  622. oldEntry->parent = newEntryParent;
  623. oldEntry->path = newFullPath;
  624. oldEntry->elementName = newFullPath.getWTail();
  625. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  626. {
  627. Stack<LibraryEntry*> todo;
  628. todo.push(oldEntry);
  629. while(!todo.empty())
  630. {
  631. LibraryEntry* curEntry = todo.top();
  632. todo.pop();
  633. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  634. for(auto& child : curDirEntry->mChildren)
  635. {
  636. child->path = child->parent->path;
  637. child->path.append(child->elementName);
  638. if(child->type == LibraryEntryType::Directory)
  639. todo.push(child);
  640. }
  641. }
  642. }
  643. onEntryAdded(oldEntry->path);
  644. if (resEntry != nullptr)
  645. {
  646. reimportDependants(oldFullPath);
  647. reimportDependants(newFullPath);
  648. }
  649. }
  650. }
  651. else // Moving from outside of the Resources folder (likely adding a new resource)
  652. {
  653. checkForModifications(newFullPath);
  654. }
  655. }
  656. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  657. {
  658. Path oldFullPath = oldPath;
  659. if (!oldFullPath.isAbsolute())
  660. oldFullPath.makeAbsolute(mResourcesFolder);
  661. Path newFullPath = newPath;
  662. if (!newFullPath.isAbsolute())
  663. newFullPath.makeAbsolute(mResourcesFolder);
  664. if (!FileSystem::exists(oldFullPath))
  665. return;
  666. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  667. // Copying a file/folder outside of the Resources folder, no special handling needed
  668. if (!mResourcesFolder.includes(newFullPath))
  669. return;
  670. Path parentPath = newFullPath.getParent();
  671. DirectoryEntry* newEntryParent = nullptr;
  672. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  673. if (newEntryParentLib != nullptr)
  674. {
  675. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  676. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  677. }
  678. // If the source is outside of Resources folder, just plain import the copy
  679. LibraryEntry* oldEntry = findEntry(oldFullPath);
  680. if (oldEntry == nullptr)
  681. {
  682. checkForModifications(newFullPath);
  683. return;
  684. }
  685. // Both source and destination are within Resources folder, need to preserve import options on the copies
  686. LibraryEntry* newEntry = nullptr;
  687. if (FileSystem::isFile(newFullPath))
  688. {
  689. assert(oldEntry->type == LibraryEntryType::File);
  690. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  691. ImportOptionsPtr importOptions;
  692. if (oldResEntry->meta != nullptr)
  693. importOptions = oldResEntry->meta->getImportOptions();
  694. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  695. }
  696. else
  697. {
  698. assert(oldEntry->type == LibraryEntryType::File);
  699. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  700. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  701. newEntry = newDirEntry;
  702. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  703. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  704. while (!todo.empty())
  705. {
  706. auto current = todo.top();
  707. todo.pop();
  708. DirectoryEntry* sourceDir = std::get<0>(current);
  709. DirectoryEntry* destDir = std::get<1>(current);
  710. for (auto& child : sourceDir->mChildren)
  711. {
  712. Path childDestPath = destDir->path;
  713. childDestPath.append(child->path.getWTail());
  714. if (child->type == LibraryEntryType::File)
  715. {
  716. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  717. ImportOptionsPtr importOptions;
  718. if (childResEntry->meta != nullptr)
  719. importOptions = childResEntry->meta->getImportOptions();
  720. addResourceInternal(destDir, childDestPath, importOptions, true);
  721. }
  722. else // Directory
  723. {
  724. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  725. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  726. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  727. }
  728. }
  729. }
  730. }
  731. }
  732. void ProjectLibrary::deleteEntry(const Path& path)
  733. {
  734. Path fullPath = path;
  735. if (!fullPath.isAbsolute())
  736. fullPath.makeAbsolute(mResourcesFolder);
  737. if(FileSystem::exists(fullPath))
  738. FileSystem::remove(fullPath);
  739. LibraryEntry* entry = findEntry(fullPath);
  740. if(entry != nullptr)
  741. {
  742. if(entry->type == LibraryEntryType::File)
  743. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  744. else if(entry->type == LibraryEntryType::Directory)
  745. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  746. }
  747. }
  748. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  749. {
  750. LibraryEntry* entry = findEntry(path);
  751. if (entry != nullptr)
  752. {
  753. if (entry->type == LibraryEntryType::File)
  754. {
  755. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  756. reimportResourceInternal(resEntry, importOptions, forceReimport);
  757. }
  758. }
  759. }
  760. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  761. {
  762. LibraryEntry* entry = findEntry(path);
  763. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  764. return;
  765. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  766. if (resEntry->meta == nullptr)
  767. return;
  768. resEntry->meta->setIncludeInBuild(include);
  769. Path metaPath = resEntry->path;
  770. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  771. FileEncoder fs(metaPath);
  772. fs.encode(resEntry->meta.get());
  773. }
  774. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  775. {
  776. Vector<ResourceEntry*> output;
  777. Stack<DirectoryEntry*> todo;
  778. todo.push(mRootEntry);
  779. while (!todo.empty())
  780. {
  781. DirectoryEntry* directory = todo.top();
  782. todo.pop();
  783. for (auto& child : directory->mChildren)
  784. {
  785. if (child->type == LibraryEntryType::File)
  786. {
  787. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  788. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  789. output.push_back(resEntry);
  790. }
  791. else if (child->type == LibraryEntryType::Directory)
  792. {
  793. todo.push(static_cast<DirectoryEntry*>(child));
  794. }
  795. }
  796. }
  797. return output;
  798. }
  799. HResource ProjectLibrary::load(const Path& path)
  800. {
  801. LibraryEntry* entry = findEntry(path);
  802. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  803. return HResource();
  804. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  805. if (resEntry->meta == nullptr)
  806. return HResource();
  807. String resUUID = resEntry->meta->getUUID();
  808. return gResources().loadFromUUID(resUUID);
  809. }
  810. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  811. {
  812. Path parentPath = fullPath;
  813. DirectoryEntry* newEntryParent = nullptr;
  814. Stack<Path> parentPaths;
  815. do
  816. {
  817. Path newParentPath = parentPath.getParent();
  818. if(newParentPath == parentPath)
  819. break;
  820. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  821. if(newEntryParentLib != nullptr)
  822. {
  823. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  824. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  825. break;
  826. }
  827. parentPaths.push(newParentPath);
  828. parentPath = newParentPath;
  829. } while (true);
  830. assert(newEntryParent != nullptr); // Must exist
  831. if(newHierarchyRoot != nullptr)
  832. *newHierarchyRoot = newEntryParent;
  833. while(!parentPaths.empty())
  834. {
  835. Path curPath = parentPaths.top();
  836. parentPaths.pop();
  837. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  838. }
  839. if(newHierarchyLeaf != nullptr)
  840. *newHierarchyLeaf = newEntryParent;
  841. }
  842. Path ProjectLibrary::getMetaPath(const Path& path) const
  843. {
  844. Path metaPath = path;
  845. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  846. return metaPath;
  847. }
  848. bool ProjectLibrary::isMeta(const Path& fullPath) const
  849. {
  850. return fullPath.getWExtension() == L".meta";
  851. }
  852. bool ProjectLibrary::isNative(ResourceEntry* resource) const
  853. {
  854. WString extension = resource->path.getWExtension();
  855. return extension == L".asset" || extension == L".prefab";
  856. }
  857. void ProjectLibrary::unloadLibrary()
  858. {
  859. if (!mIsLoaded)
  860. return;
  861. mProjectFolder = Path::BLANK;
  862. mResourcesFolder = Path::BLANK;
  863. clearEntries();
  864. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  865. mDependencies.clear();
  866. gResources().unregisterResourceManifest(mResourceManifest);
  867. mResourceManifest = nullptr;
  868. mIsLoaded = false;
  869. }
  870. void ProjectLibrary::makeEntriesRelative()
  871. {
  872. // Make all paths relative before saving
  873. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  874. [&](LibraryEntry* entry, const Path& root)
  875. {
  876. entry->path.makeRelative(root);
  877. if (entry->type == LibraryEntryType::Directory)
  878. {
  879. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  880. for (auto& child : dirEntry->mChildren)
  881. makeRelative(child, root);
  882. }
  883. };
  884. Path root = getResourcesFolder();
  885. makeRelative(mRootEntry, root);
  886. }
  887. void ProjectLibrary::makeEntriesAbsolute()
  888. {
  889. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  890. [&](LibraryEntry* entry, const Path& root)
  891. {
  892. entry->path.makeAbsolute(root);
  893. if (entry->type == LibraryEntryType::Directory)
  894. {
  895. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  896. for (auto& child : dirEntry->mChildren)
  897. makeAbsolute(child, root);
  898. }
  899. };
  900. Path root = getResourcesFolder();
  901. makeAbsolute(mRootEntry, root);
  902. }
  903. void ProjectLibrary::saveLibrary()
  904. {
  905. if (!mIsLoaded)
  906. return;
  907. // Make all paths relative before saving
  908. makeEntriesRelative();
  909. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  910. Path libraryEntriesPath = mProjectFolder;
  911. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  912. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  913. FileEncoder fs(libraryEntriesPath);
  914. fs.encode(libEntries.get());
  915. // Restore absolute entry paths
  916. makeEntriesAbsolute();
  917. Path resourceManifestPath = mProjectFolder;
  918. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  919. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  920. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  921. }
  922. void ProjectLibrary::loadLibrary()
  923. {
  924. unloadLibrary();
  925. mProjectFolder = gEditorApplication().getProjectPath();
  926. mResourcesFolder = mProjectFolder;
  927. mResourcesFolder.append(RESOURCES_DIR);
  928. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  929. Path libraryEntriesPath = mProjectFolder;
  930. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  931. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  932. if(FileSystem::exists(libraryEntriesPath))
  933. {
  934. FileDecoder fs(libraryEntriesPath);
  935. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  936. *mRootEntry = libEntries->getRootEntry();
  937. for(auto& child : mRootEntry->mChildren)
  938. child->parent = mRootEntry;
  939. mRootEntry->parent = nullptr;
  940. }
  941. // Entries are stored relative to project folder, but we want their absolute paths now
  942. makeEntriesAbsolute();
  943. // Load resource manifest
  944. Path resourceManifestPath = mProjectFolder;
  945. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  946. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  947. if (FileSystem::exists(resourceManifestPath))
  948. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  949. else
  950. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  951. gResources().registerResourceManifest(mResourceManifest);
  952. // Load all meta files
  953. Stack<DirectoryEntry*> todo;
  954. todo.push(mRootEntry);
  955. Vector<LibraryEntry*> deletedEntries;
  956. while(!todo.empty())
  957. {
  958. DirectoryEntry* curDir = todo.top();
  959. todo.pop();
  960. for(auto& child : curDir->mChildren)
  961. {
  962. if(child->type == LibraryEntryType::File)
  963. {
  964. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  965. if (FileSystem::isFile(resEntry->path))
  966. {
  967. if (resEntry->meta == nullptr)
  968. {
  969. Path metaPath = resEntry->path;
  970. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  971. if (FileSystem::isFile(metaPath))
  972. {
  973. FileDecoder fs(metaPath);
  974. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  975. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  976. {
  977. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  978. resEntry->meta = resourceMeta;
  979. }
  980. }
  981. }
  982. if (resEntry->meta != nullptr)
  983. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  984. addDependencies(resEntry);
  985. }
  986. else
  987. deletedEntries.push_back(resEntry);
  988. }
  989. else if(child->type == LibraryEntryType::Directory)
  990. {
  991. if (FileSystem::isDirectory(child->path))
  992. todo.push(static_cast<DirectoryEntry*>(child));
  993. else
  994. deletedEntries.push_back(child);
  995. }
  996. }
  997. }
  998. // Remove entries that no longer have corresponding files
  999. for (auto& deletedEntry : deletedEntries)
  1000. {
  1001. if (deletedEntry->type == LibraryEntryType::File)
  1002. {
  1003. ResourceEntry* resEntry = static_cast<ResourceEntry*>(deletedEntry);
  1004. deleteResourceInternal(resEntry);
  1005. }
  1006. else
  1007. {
  1008. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1009. deleteDirectoryInternal(dirEntry);
  1010. }
  1011. }
  1012. // Clean up internal library folder from obsolete files
  1013. Path internalResourcesFolder = mProjectFolder;
  1014. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1015. Vector<Path> toDelete;
  1016. auto processFile = [&](const Path& file)
  1017. {
  1018. String uuid = file.getFilename(false);
  1019. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1020. {
  1021. mResourceManifest->unregisterResource(uuid);
  1022. toDelete.push_back(file);
  1023. }
  1024. return true;
  1025. };
  1026. FileSystem::iterate(internalResourcesFolder, processFile);
  1027. for (auto& entry : toDelete)
  1028. FileSystem::remove(entry);
  1029. mIsLoaded = true;
  1030. }
  1031. void ProjectLibrary::clearEntries()
  1032. {
  1033. if (mRootEntry == nullptr)
  1034. return;
  1035. std::function<void(LibraryEntry*)> deleteRecursive =
  1036. [&](LibraryEntry* entry)
  1037. {
  1038. if (entry->type == LibraryEntryType::Directory)
  1039. {
  1040. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1041. for (auto& child : dirEntry->mChildren)
  1042. deleteRecursive(child);
  1043. }
  1044. bs_delete(entry);
  1045. };
  1046. deleteRecursive(mRootEntry);
  1047. mRootEntry = nullptr;
  1048. }
  1049. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1050. {
  1051. Vector<Path> output;
  1052. if (entry->meta == nullptr)
  1053. return output;
  1054. if (entry->meta->getTypeID() == TID_Shader)
  1055. {
  1056. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1057. for (auto& include : metaData->includes)
  1058. output.push_back(include);
  1059. }
  1060. return output;
  1061. }
  1062. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1063. {
  1064. Vector<Path> dependencies = getImportDependencies(entry);
  1065. for (auto& dependency : dependencies)
  1066. mDependencies[dependency].push_back(entry->path);
  1067. }
  1068. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1069. {
  1070. Vector<Path> dependencies = getImportDependencies(entry);
  1071. for (auto& dependency : dependencies)
  1072. {
  1073. Vector<Path>& curDependencies = mDependencies[dependency];
  1074. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1075. [&](const Path& x)
  1076. {
  1077. return x == entry->path;
  1078. });
  1079. curDependencies.erase(iterRemove, curDependencies.end());
  1080. }
  1081. }
  1082. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1083. {
  1084. auto iterFind = mDependencies.find(entryPath);
  1085. if (iterFind == mDependencies.end())
  1086. return;
  1087. // Make a copy since we might modify this list during reimport
  1088. Vector<Path> dependencies = iterFind->second;
  1089. for (auto& dependency : dependencies)
  1090. {
  1091. LibraryEntry* entry = findEntry(dependency);
  1092. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1093. {
  1094. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  1095. ImportOptionsPtr importOptions;
  1096. if (resEntry->meta != nullptr)
  1097. importOptions = resEntry->meta->getImportOptions();
  1098. reimportResourceInternal(resEntry, importOptions, true);
  1099. }
  1100. }
  1101. }
  1102. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1103. {
  1104. return ProjectLibrary::instance();
  1105. }
  1106. }