BsProjectLibrary.cpp 38 KB

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