BsProjectLibrary.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsProjectLibrary.h"
  4. #include "BsFileSystem.h"
  5. #include "BsException.h"
  6. #include "BsResources.h"
  7. #include "BsResourceManifest.h"
  8. #include "BsImporter.h"
  9. #include "BsProjectResourceMeta.h"
  10. #include "BsResources.h"
  11. #include "BsImporter.h"
  12. #include "BsImportOptions.h"
  13. #include "BsFileSerializer.h"
  14. #include "BsDebug.h"
  15. #include "BsProjectLibraryEntries.h"
  16. #include "BsResource.h"
  17. #include "BsEditorApplication.h"
  18. #include "BsShader.h"
  19. #include <regex>
  20. using namespace std::placeholders;
  21. namespace BansheeEngine
  22. {
  23. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  24. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + GAME_RESOURCES_FOLDER_NAME;
  25. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  26. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  27. ProjectLibrary::LibraryEntry::LibraryEntry()
  28. :parent(nullptr), type(LibraryEntryType::Directory)
  29. { }
  30. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  31. :path(path), parent(parent), type(type), elementName(name)
  32. { }
  33. ProjectLibrary::ResourceEntry::ResourceEntry()
  34. : lastUpdateTime(0)
  35. { }
  36. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  37. : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  38. { }
  39. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  40. { }
  41. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  42. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  43. { }
  44. ProjectLibrary::ProjectLibrary()
  45. : mRootEntry(nullptr), mIsLoaded(false)
  46. {
  47. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  48. }
  49. ProjectLibrary::~ProjectLibrary()
  50. {
  51. clearEntries();
  52. }
  53. void ProjectLibrary::checkForModifications(const Path& fullPath)
  54. {
  55. Vector<Path> dirtyResources;
  56. checkForModifications(fullPath, true, dirtyResources);
  57. }
  58. void ProjectLibrary::checkForModifications(const Path& fullPath, bool import, Vector<Path>& dirtyResources)
  59. {
  60. if (!mResourcesFolder.includes(fullPath))
  61. return; // Folder not part of our resources path, so no modifications
  62. if(mRootEntry == nullptr)
  63. {
  64. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  65. }
  66. Path pathToSearch = fullPath;
  67. LibraryEntry* entry = findEntry(pathToSearch);
  68. if (entry == nullptr) // File could be new, try to find parent directory entry
  69. {
  70. if (FileSystem::exists(pathToSearch))
  71. {
  72. if (isMeta(pathToSearch))
  73. {
  74. Path sourceFilePath = pathToSearch;
  75. sourceFilePath.setExtension(L"");
  76. if (!FileSystem::isFile(sourceFilePath))
  77. {
  78. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  79. FileSystem::remove(pathToSearch);
  80. }
  81. }
  82. else
  83. {
  84. Path parentDirPath = pathToSearch.getParent();
  85. entry = findEntry(parentDirPath);
  86. // Cannot find parent directory. Create the needed hierarchy.
  87. DirectoryEntry* entryParent = nullptr;
  88. DirectoryEntry* newHierarchyParent = nullptr;
  89. if (entry == nullptr)
  90. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  91. else
  92. entryParent = static_cast<DirectoryEntry*>(entry);
  93. if (FileSystem::isFile(pathToSearch))
  94. {
  95. if (import)
  96. addResourceInternal(entryParent, pathToSearch);
  97. dirtyResources.push_back(pathToSearch);
  98. }
  99. else if (FileSystem::isDirectory(pathToSearch))
  100. {
  101. addDirectoryInternal(entryParent, pathToSearch);
  102. checkForModifications(pathToSearch, import, dirtyResources);
  103. }
  104. }
  105. }
  106. }
  107. else if(entry->type == LibraryEntryType::File)
  108. {
  109. if(FileSystem::isFile(entry->path))
  110. {
  111. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  112. if (import)
  113. reimportResourceInternal(resEntry);
  114. if (!isUpToDate(resEntry))
  115. dirtyResources.push_back(entry->path);
  116. }
  117. else
  118. {
  119. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  120. }
  121. }
  122. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  123. {
  124. if(!FileSystem::isDirectory(entry->path))
  125. {
  126. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  127. }
  128. else
  129. {
  130. Stack<DirectoryEntry*> todo;
  131. todo.push(static_cast<DirectoryEntry*>(entry));
  132. Vector<Path> childFiles;
  133. Vector<Path> childDirectories;
  134. Vector<bool> existingEntries;
  135. Vector<LibraryEntry*> toDelete;
  136. while(!todo.empty())
  137. {
  138. DirectoryEntry* currentDir = todo.top();
  139. todo.pop();
  140. existingEntries.clear();
  141. existingEntries.resize(currentDir->mChildren.size());
  142. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  143. existingEntries[i] = false;
  144. childFiles.clear();
  145. childDirectories.clear();
  146. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  147. for(auto& filePath : childFiles)
  148. {
  149. if(isMeta(filePath))
  150. {
  151. Path sourceFilePath = filePath;
  152. sourceFilePath.setExtension(L"");
  153. if(!FileSystem::isFile(sourceFilePath))
  154. {
  155. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  156. FileSystem::remove(filePath);
  157. }
  158. }
  159. else
  160. {
  161. ResourceEntry* existingEntry = nullptr;
  162. UINT32 idx = 0;
  163. for(auto& child : currentDir->mChildren)
  164. {
  165. if(child->type == LibraryEntryType::File && child->path == filePath)
  166. {
  167. existingEntries[idx] = true;
  168. existingEntry = static_cast<ResourceEntry*>(child);
  169. break;
  170. }
  171. idx++;
  172. }
  173. if(existingEntry != nullptr)
  174. {
  175. if (import)
  176. reimportResourceInternal(existingEntry);
  177. if (!isUpToDate(existingEntry))
  178. dirtyResources.push_back(existingEntry->path);
  179. }
  180. else
  181. {
  182. if (import)
  183. addResourceInternal(currentDir, filePath);
  184. dirtyResources.push_back(filePath);
  185. }
  186. }
  187. }
  188. for(auto& dirPath : childDirectories)
  189. {
  190. DirectoryEntry* existingEntry = nullptr;
  191. UINT32 idx = 0;
  192. for(auto& child : currentDir->mChildren)
  193. {
  194. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  195. {
  196. existingEntries[idx] = true;
  197. existingEntry = static_cast<DirectoryEntry*>(child);
  198. break;
  199. }
  200. idx++;
  201. }
  202. if(existingEntry == nullptr)
  203. addDirectoryInternal(currentDir, dirPath);
  204. }
  205. {
  206. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  207. {
  208. if(existingEntries[i])
  209. continue;
  210. toDelete.push_back(currentDir->mChildren[i]);
  211. }
  212. for(auto& child : toDelete)
  213. {
  214. if(child->type == LibraryEntryType::Directory)
  215. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  216. else if(child->type == LibraryEntryType::File)
  217. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  218. }
  219. toDelete.clear();
  220. }
  221. for(auto& child : currentDir->mChildren)
  222. {
  223. if(child->type == LibraryEntryType::Directory)
  224. todo.push(static_cast<DirectoryEntry*>(child));
  225. }
  226. }
  227. }
  228. }
  229. }
  230. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  231. const ImportOptionsPtr& importOptions, bool forceReimport)
  232. {
  233. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  234. parent->mChildren.push_back(newResource);
  235. reimportResourceInternal(newResource, importOptions, forceReimport);
  236. onEntryAdded(newResource->path);
  237. return newResource;
  238. }
  239. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  240. {
  241. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  242. parent->mChildren.push_back(newEntry);
  243. onEntryAdded(newEntry->path);
  244. return newEntry;
  245. }
  246. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  247. {
  248. if(resource->meta != nullptr)
  249. {
  250. String uuid = resource->meta->getUUID();
  251. Path path;
  252. if (mResourceManifest->uuidToFilePath(uuid, path))
  253. {
  254. if(FileSystem::isFile(path))
  255. FileSystem::remove(path);
  256. mResourceManifest->unregisterResource(uuid);
  257. }
  258. mUUIDToPath.erase(uuid);
  259. }
  260. Path metaPath = getMetaPath(resource->path);
  261. if (FileSystem::isFile(metaPath))
  262. FileSystem::remove(metaPath);
  263. DirectoryEntry* parent = resource->parent;
  264. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  265. [&] (const LibraryEntry* entry) { return entry == resource; });
  266. parent->mChildren.erase(findIter);
  267. Path originalPath = resource->path;
  268. onEntryRemoved(originalPath);
  269. removeDependencies(resource);
  270. bs_delete(resource);
  271. reimportDependants(originalPath);
  272. }
  273. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  274. {
  275. if(directory == mRootEntry)
  276. mRootEntry = nullptr;
  277. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  278. for(auto& child : childrenToDestroy)
  279. {
  280. if(child->type == LibraryEntryType::Directory)
  281. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  282. else
  283. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  284. }
  285. DirectoryEntry* parent = directory->parent;
  286. if(parent != nullptr)
  287. {
  288. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  289. [&] (const LibraryEntry* entry) { return entry == directory; });
  290. parent->mChildren.erase(findIter);
  291. }
  292. onEntryRemoved(directory->path);
  293. bs_delete(directory);
  294. }
  295. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  296. {
  297. Path metaPath = resource->path;
  298. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  299. if(resource->meta == nullptr)
  300. {
  301. if(FileSystem::isFile(metaPath))
  302. {
  303. FileDecoder fs(metaPath);
  304. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  305. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  306. {
  307. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  308. resource->meta = resourceMeta;
  309. mUUIDToPath[resourceMeta->getUUID()] = resource->path;
  310. }
  311. }
  312. }
  313. if (!isUpToDate(resource) || forceReimport)
  314. {
  315. // Note: If resource is native we just copy it to the internal folder. We could avoid the copy and
  316. // load the resource directly from the Resources folder but that requires complicating library code.
  317. bool isNativeResource = isNative(resource->path);
  318. ImportOptionsPtr curImportOptions = nullptr;
  319. if (importOptions == nullptr && !isNativeResource)
  320. {
  321. if (resource->meta != nullptr)
  322. curImportOptions = resource->meta->getImportOptions();
  323. else
  324. curImportOptions = Importer::instance().createImportOptions(resource->path);
  325. }
  326. else
  327. curImportOptions = importOptions;
  328. HResource importedResource;
  329. if (isNativeResource)
  330. {
  331. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  332. // This can happen if library isn't properly saved before exiting the application.
  333. if (resource->meta != nullptr)
  334. {
  335. mResourceManifest->registerResource(resource->meta->getUUID(), resource->path);
  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, false);
  340. }
  341. if(resource->meta == nullptr)
  342. {
  343. if (!isNativeResource)
  344. importedResource = Importer::instance().import(resource->path, curImportOptions);
  345. if (importedResource != nullptr)
  346. {
  347. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  348. UINT32 typeId = importedResource->getTypeId();
  349. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  350. }
  351. else
  352. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), 0, nullptr, curImportOptions);
  353. FileEncoder fs(metaPath);
  354. fs.encode(resource->meta.get());
  355. mUUIDToPath[resource->meta->getUUID()] = resource->path;
  356. }
  357. else
  358. {
  359. removeDependencies(resource);
  360. if (!isNativeResource)
  361. {
  362. importedResource = gResources()._getResourceHandle(resource->meta->getUUID());
  363. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  364. }
  365. resource->meta->mImportOptions = curImportOptions;
  366. FileEncoder fs(metaPath);
  367. fs.encode(resource->meta.get());
  368. }
  369. addDependencies(resource);
  370. if (importedResource != nullptr)
  371. {
  372. Path internalResourcesPath = mProjectFolder;
  373. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  374. if (!FileSystem::isDirectory(internalResourcesPath))
  375. FileSystem::createDir(internalResourcesPath);
  376. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  377. gResources().save(importedResource, internalResourcesPath, true);
  378. String uuid = importedResource.getUUID();
  379. mResourceManifest->registerResource(uuid, internalResourcesPath);
  380. }
  381. resource->lastUpdateTime = std::time(nullptr);
  382. onEntryImported(resource->path);
  383. reimportDependants(resource->path);
  384. }
  385. }
  386. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  387. {
  388. if(resource->meta == nullptr)
  389. return false;
  390. Path internalPath;
  391. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), internalPath))
  392. return false;
  393. if(!FileSystem::isFile(internalPath))
  394. return false;
  395. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  396. return lastModifiedTime <= resource->lastUpdateTime;
  397. }
  398. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  399. {
  400. return search(pattern, {});
  401. }
  402. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  403. {
  404. Vector<LibraryEntry*> foundEntries;
  405. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  406. WString replace(L"\\\\&");
  407. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  408. std::wregex wildcard(L"\\\\\\*");
  409. WString wildcardReplace(L".*");
  410. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  411. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  412. Stack<DirectoryEntry*> todo;
  413. todo.push(mRootEntry);
  414. while (!todo.empty())
  415. {
  416. DirectoryEntry* dirEntry = todo.top();
  417. todo.pop();
  418. for (auto& child : dirEntry->mChildren)
  419. {
  420. if (std::regex_match(child->elementName, searchRegex))
  421. {
  422. if (typeIds.size() == 0)
  423. foundEntries.push_back(child);
  424. else
  425. {
  426. if (child->type == LibraryEntryType::File)
  427. {
  428. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  429. for (auto& typeId : typeIds)
  430. {
  431. if (childResEntry->meta != nullptr && childResEntry->meta->getTypeID() == typeId)
  432. {
  433. foundEntries.push_back(child);
  434. break;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. if (child->type == LibraryEntryType::Directory)
  441. {
  442. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  443. todo.push(childDirEntry);
  444. }
  445. }
  446. }
  447. std::sort(foundEntries.begin(), foundEntries.end(),
  448. [&](const LibraryEntry* a, const LibraryEntry* b)
  449. {
  450. return a->elementName.compare(b->elementName) < 0;
  451. });
  452. return foundEntries;
  453. }
  454. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  455. {
  456. Path fullPath = path;
  457. if (fullPath.isAbsolute())
  458. {
  459. if (!mResourcesFolder.includes(fullPath))
  460. return nullptr;
  461. }
  462. else
  463. fullPath.makeAbsolute(mResourcesFolder);
  464. Path relPath = fullPath.getRelative(mRootEntry->path);
  465. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  466. UINT32 idx = 0;
  467. LibraryEntry* current = mRootEntry;
  468. while (current != nullptr)
  469. {
  470. if (idx == numElems)
  471. return current;
  472. WString curElem;
  473. if (relPath.isFile() && idx == (numElems - 1))
  474. curElem = relPath.getWFilename();
  475. else
  476. curElem = relPath[idx];
  477. if (current->type == LibraryEntryType::Directory)
  478. {
  479. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  480. current = nullptr;
  481. for (auto& child : dirEntry->mChildren)
  482. {
  483. if (Path::comparePathElem(curElem, child->elementName))
  484. {
  485. idx++;
  486. current = child;
  487. break;
  488. }
  489. }
  490. }
  491. else
  492. break;
  493. }
  494. return nullptr;
  495. }
  496. Path ProjectLibrary::uuidToPath(const String& uuid) const
  497. {
  498. auto iterFind = mUUIDToPath.find(uuid);
  499. if (iterFind != mUUIDToPath.end())
  500. return iterFind->second;
  501. return Path::BLANK;
  502. }
  503. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  504. {
  505. if (resource == nullptr)
  506. return;
  507. Path assetPath = path;
  508. if (path.isAbsolute())
  509. {
  510. if (!getResourcesFolder().includes(path))
  511. return;
  512. assetPath = path.getRelative(getResourcesFolder());
  513. }
  514. LibraryEntry* existingEntry = findEntry(assetPath);
  515. if (existingEntry != nullptr)
  516. {
  517. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  518. return;
  519. }
  520. resource->setName(path.getWFilename(false));
  521. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  522. Resources::instance().save(resource, absPath, false);
  523. checkForModifications(absPath);
  524. }
  525. void ProjectLibrary::saveEntry(const HResource& resource)
  526. {
  527. if (resource == nullptr)
  528. return;
  529. Path filePath = uuidToPath(resource.getUUID());
  530. filePath.makeAbsolute(getResourcesFolder());
  531. Resources::instance().save(resource, filePath, true);
  532. checkForModifications(filePath);
  533. }
  534. void ProjectLibrary::createFolderEntry(const Path& path)
  535. {
  536. Path fullPath = path;
  537. if (fullPath.isAbsolute())
  538. {
  539. if (!mResourcesFolder.includes(fullPath))
  540. return;
  541. }
  542. else
  543. fullPath.makeAbsolute(mResourcesFolder);
  544. if (FileSystem::isDirectory(fullPath))
  545. return; // Already exists
  546. FileSystem::createDir(fullPath);
  547. Path parentPath = fullPath.getParent();
  548. DirectoryEntry* newEntryParent = nullptr;
  549. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  550. if (newEntryParentLib != nullptr)
  551. {
  552. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  553. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  554. }
  555. DirectoryEntry* newHierarchyParent = nullptr;
  556. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  557. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  558. addDirectoryInternal(newEntryParent, fullPath);
  559. }
  560. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  561. {
  562. Path oldFullPath = oldPath;
  563. if (!oldFullPath.isAbsolute())
  564. oldFullPath.makeAbsolute(mResourcesFolder);
  565. Path newFullPath = newPath;
  566. if (!newFullPath.isAbsolute())
  567. newFullPath.makeAbsolute(mResourcesFolder);
  568. Path parentPath = newFullPath.getParent();
  569. if (!FileSystem::isDirectory(parentPath))
  570. {
  571. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  572. return;
  573. }
  574. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  575. FileSystem::move(oldFullPath, newFullPath, overwrite);
  576. Path oldMetaPath = getMetaPath(oldFullPath);
  577. Path newMetaPath = getMetaPath(newFullPath);
  578. LibraryEntry* oldEntry = findEntry(oldFullPath);
  579. if(oldEntry != nullptr) // Moving from the Resources folder
  580. {
  581. // Moved outside of Resources, delete entry & meta file
  582. if (!mResourcesFolder.includes(newFullPath))
  583. {
  584. if(oldEntry->type == LibraryEntryType::File)
  585. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  586. else if(oldEntry->type == LibraryEntryType::Directory)
  587. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  588. }
  589. else // Just moving internally
  590. {
  591. onEntryRemoved(oldEntry->path);
  592. ResourceEntry* resEntry = nullptr;
  593. if (oldEntry->type == LibraryEntryType::File)
  594. {
  595. resEntry = static_cast<ResourceEntry*>(oldEntry);
  596. removeDependencies(resEntry);
  597. // Update uuid <-> path mapping
  598. if(resEntry->meta != nullptr)
  599. {
  600. const String& UUID = resEntry->meta->getUUID();
  601. mUUIDToPath[UUID] = newFullPath;
  602. }
  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(const Path& path) const
  853. {
  854. WString extension = 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. }