BsProjectLibrary.cpp 38 KB

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