BsProjectLibrary.cpp 39 KB

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