BsProjectLibrary.cpp 39 KB

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