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->path);
  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. // Update uuid <-> path mapping
  596. if(resEntry->meta != nullptr)
  597. {
  598. const String& UUID = resEntry->meta->getUUID();
  599. mUUIDToPath[UUID] = newFullPath;
  600. mResourceManifest->registerResource(UUID, newFullPath);
  601. }
  602. }
  603. if(FileSystem::isFile(oldMetaPath))
  604. FileSystem::move(oldMetaPath, newMetaPath);
  605. DirectoryEntry* parent = oldEntry->parent;
  606. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  607. if(findIter != parent->mChildren.end())
  608. parent->mChildren.erase(findIter);
  609. Path parentPath = newFullPath.getParent();
  610. DirectoryEntry* newEntryParent = nullptr;
  611. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  612. if(newEntryParentLib != nullptr)
  613. {
  614. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  615. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  616. }
  617. DirectoryEntry* newHierarchyParent = nullptr;
  618. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  619. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  620. newEntryParent->mChildren.push_back(oldEntry);
  621. oldEntry->parent = newEntryParent;
  622. oldEntry->path = newFullPath;
  623. oldEntry->elementName = newFullPath.getWTail();
  624. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  625. {
  626. Stack<LibraryEntry*> todo;
  627. todo.push(oldEntry);
  628. while(!todo.empty())
  629. {
  630. LibraryEntry* curEntry = todo.top();
  631. todo.pop();
  632. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  633. for(auto& child : curDirEntry->mChildren)
  634. {
  635. child->path = child->parent->path;
  636. child->path.append(child->elementName);
  637. if(child->type == LibraryEntryType::Directory)
  638. todo.push(child);
  639. }
  640. }
  641. }
  642. onEntryAdded(oldEntry->path);
  643. if (resEntry != nullptr)
  644. {
  645. reimportDependants(oldFullPath);
  646. reimportDependants(newFullPath);
  647. }
  648. }
  649. }
  650. else // Moving from outside of the Resources folder (likely adding a new resource)
  651. {
  652. checkForModifications(newFullPath);
  653. }
  654. }
  655. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  656. {
  657. Path oldFullPath = oldPath;
  658. if (!oldFullPath.isAbsolute())
  659. oldFullPath.makeAbsolute(mResourcesFolder);
  660. Path newFullPath = newPath;
  661. if (!newFullPath.isAbsolute())
  662. newFullPath.makeAbsolute(mResourcesFolder);
  663. if (!FileSystem::exists(oldFullPath))
  664. return;
  665. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  666. // Copying a file/folder outside of the Resources folder, no special handling needed
  667. if (!mResourcesFolder.includes(newFullPath))
  668. return;
  669. Path parentPath = newFullPath.getParent();
  670. DirectoryEntry* newEntryParent = nullptr;
  671. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  672. if (newEntryParentLib != nullptr)
  673. {
  674. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  675. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  676. }
  677. // If the source is outside of Resources folder, just plain import the copy
  678. LibraryEntry* oldEntry = findEntry(oldFullPath);
  679. if (oldEntry == nullptr)
  680. {
  681. checkForModifications(newFullPath);
  682. return;
  683. }
  684. // Both source and destination are within Resources folder, need to preserve import options on the copies
  685. LibraryEntry* newEntry = nullptr;
  686. if (FileSystem::isFile(newFullPath))
  687. {
  688. assert(oldEntry->type == LibraryEntryType::File);
  689. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  690. ImportOptionsPtr importOptions;
  691. if (oldResEntry->meta != nullptr)
  692. importOptions = oldResEntry->meta->getImportOptions();
  693. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  694. }
  695. else
  696. {
  697. assert(oldEntry->type == LibraryEntryType::File);
  698. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  699. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  700. newEntry = newDirEntry;
  701. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  702. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  703. while (!todo.empty())
  704. {
  705. auto current = todo.top();
  706. todo.pop();
  707. DirectoryEntry* sourceDir = std::get<0>(current);
  708. DirectoryEntry* destDir = std::get<1>(current);
  709. for (auto& child : sourceDir->mChildren)
  710. {
  711. Path childDestPath = destDir->path;
  712. childDestPath.append(child->path.getWTail());
  713. if (child->type == LibraryEntryType::File)
  714. {
  715. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  716. ImportOptionsPtr importOptions;
  717. if (childResEntry->meta != nullptr)
  718. importOptions = childResEntry->meta->getImportOptions();
  719. addResourceInternal(destDir, childDestPath, importOptions, true);
  720. }
  721. else // Directory
  722. {
  723. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  724. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  725. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  726. }
  727. }
  728. }
  729. }
  730. }
  731. void ProjectLibrary::deleteEntry(const Path& path)
  732. {
  733. Path fullPath = path;
  734. if (!fullPath.isAbsolute())
  735. fullPath.makeAbsolute(mResourcesFolder);
  736. if(FileSystem::exists(fullPath))
  737. FileSystem::remove(fullPath);
  738. LibraryEntry* entry = findEntry(fullPath);
  739. if(entry != nullptr)
  740. {
  741. if(entry->type == LibraryEntryType::File)
  742. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  743. else if(entry->type == LibraryEntryType::Directory)
  744. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  745. }
  746. }
  747. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  748. {
  749. LibraryEntry* entry = findEntry(path);
  750. if (entry != nullptr)
  751. {
  752. if (entry->type == LibraryEntryType::File)
  753. {
  754. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  755. reimportResourceInternal(resEntry, importOptions, forceReimport);
  756. }
  757. }
  758. }
  759. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  760. {
  761. LibraryEntry* entry = findEntry(path);
  762. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  763. return;
  764. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  765. if (resEntry->meta == nullptr)
  766. return;
  767. resEntry->meta->setIncludeInBuild(include);
  768. Path metaPath = resEntry->path;
  769. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  770. FileEncoder fs(metaPath);
  771. fs.encode(resEntry->meta.get());
  772. }
  773. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  774. {
  775. Vector<ResourceEntry*> output;
  776. Stack<DirectoryEntry*> todo;
  777. todo.push(mRootEntry);
  778. while (!todo.empty())
  779. {
  780. DirectoryEntry* directory = todo.top();
  781. todo.pop();
  782. for (auto& child : directory->mChildren)
  783. {
  784. if (child->type == LibraryEntryType::File)
  785. {
  786. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  787. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  788. output.push_back(resEntry);
  789. }
  790. else if (child->type == LibraryEntryType::Directory)
  791. {
  792. todo.push(static_cast<DirectoryEntry*>(child));
  793. }
  794. }
  795. }
  796. return output;
  797. }
  798. HResource ProjectLibrary::load(const Path& path)
  799. {
  800. LibraryEntry* entry = findEntry(path);
  801. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  802. return HResource();
  803. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  804. if (resEntry->meta == nullptr)
  805. return HResource();
  806. String resUUID = resEntry->meta->getUUID();
  807. return gResources().loadFromUUID(resUUID);
  808. }
  809. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  810. {
  811. Path parentPath = fullPath;
  812. DirectoryEntry* newEntryParent = nullptr;
  813. Stack<Path> parentPaths;
  814. do
  815. {
  816. Path newParentPath = parentPath.getParent();
  817. if(newParentPath == parentPath)
  818. break;
  819. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  820. if(newEntryParentLib != nullptr)
  821. {
  822. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  823. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  824. break;
  825. }
  826. parentPaths.push(newParentPath);
  827. parentPath = newParentPath;
  828. } while (true);
  829. assert(newEntryParent != nullptr); // Must exist
  830. if(newHierarchyRoot != nullptr)
  831. *newHierarchyRoot = newEntryParent;
  832. while(!parentPaths.empty())
  833. {
  834. Path curPath = parentPaths.top();
  835. parentPaths.pop();
  836. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  837. }
  838. if(newHierarchyLeaf != nullptr)
  839. *newHierarchyLeaf = newEntryParent;
  840. }
  841. Path ProjectLibrary::getMetaPath(const Path& path) const
  842. {
  843. Path metaPath = path;
  844. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  845. return metaPath;
  846. }
  847. bool ProjectLibrary::isMeta(const Path& fullPath) const
  848. {
  849. return fullPath.getWExtension() == L".meta";
  850. }
  851. bool ProjectLibrary::isNative(const Path& path) const
  852. {
  853. WString extension = path.getWExtension();
  854. return extension == L".asset" || extension == L".prefab";
  855. }
  856. void ProjectLibrary::unloadLibrary()
  857. {
  858. if (!mIsLoaded)
  859. return;
  860. mProjectFolder = Path::BLANK;
  861. mResourcesFolder = Path::BLANK;
  862. clearEntries();
  863. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  864. mDependencies.clear();
  865. gResources().unregisterResourceManifest(mResourceManifest);
  866. mResourceManifest = nullptr;
  867. mIsLoaded = false;
  868. }
  869. void ProjectLibrary::makeEntriesRelative()
  870. {
  871. // Make all paths relative before saving
  872. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  873. [&](LibraryEntry* entry, const Path& root)
  874. {
  875. entry->path.makeRelative(root);
  876. if (entry->type == LibraryEntryType::Directory)
  877. {
  878. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  879. for (auto& child : dirEntry->mChildren)
  880. makeRelative(child, root);
  881. }
  882. };
  883. Path root = getResourcesFolder();
  884. makeRelative(mRootEntry, root);
  885. }
  886. void ProjectLibrary::makeEntriesAbsolute()
  887. {
  888. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  889. [&](LibraryEntry* entry, const Path& root)
  890. {
  891. entry->path.makeAbsolute(root);
  892. if (entry->type == LibraryEntryType::Directory)
  893. {
  894. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  895. for (auto& child : dirEntry->mChildren)
  896. makeAbsolute(child, root);
  897. }
  898. };
  899. Path root = getResourcesFolder();
  900. makeAbsolute(mRootEntry, root);
  901. }
  902. void ProjectLibrary::saveLibrary()
  903. {
  904. if (!mIsLoaded)
  905. return;
  906. // Make all paths relative before saving
  907. makeEntriesRelative();
  908. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  909. Path libraryEntriesPath = mProjectFolder;
  910. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  911. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  912. FileEncoder fs(libraryEntriesPath);
  913. fs.encode(libEntries.get());
  914. // Restore absolute entry paths
  915. makeEntriesAbsolute();
  916. Path resourceManifestPath = mProjectFolder;
  917. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  918. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  919. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  920. }
  921. void ProjectLibrary::loadLibrary()
  922. {
  923. unloadLibrary();
  924. mProjectFolder = gEditorApplication().getProjectPath();
  925. mResourcesFolder = mProjectFolder;
  926. mResourcesFolder.append(RESOURCES_DIR);
  927. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  928. Path libraryEntriesPath = mProjectFolder;
  929. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  930. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  931. if(FileSystem::exists(libraryEntriesPath))
  932. {
  933. FileDecoder fs(libraryEntriesPath);
  934. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  935. *mRootEntry = libEntries->getRootEntry();
  936. for(auto& child : mRootEntry->mChildren)
  937. child->parent = mRootEntry;
  938. mRootEntry->parent = nullptr;
  939. }
  940. // Entries are stored relative to project folder, but we want their absolute paths now
  941. makeEntriesAbsolute();
  942. // Load resource manifest
  943. Path resourceManifestPath = mProjectFolder;
  944. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  945. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  946. if (FileSystem::exists(resourceManifestPath))
  947. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  948. else
  949. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  950. gResources().registerResourceManifest(mResourceManifest);
  951. // Load all meta files
  952. Stack<DirectoryEntry*> todo;
  953. todo.push(mRootEntry);
  954. Vector<LibraryEntry*> deletedEntries;
  955. while(!todo.empty())
  956. {
  957. DirectoryEntry* curDir = todo.top();
  958. todo.pop();
  959. for(auto& child : curDir->mChildren)
  960. {
  961. if(child->type == LibraryEntryType::File)
  962. {
  963. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  964. if (FileSystem::isFile(resEntry->path))
  965. {
  966. if (resEntry->meta == nullptr)
  967. {
  968. Path metaPath = resEntry->path;
  969. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  970. if (FileSystem::isFile(metaPath))
  971. {
  972. FileDecoder fs(metaPath);
  973. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  974. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  975. {
  976. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  977. resEntry->meta = resourceMeta;
  978. }
  979. }
  980. }
  981. if (resEntry->meta != nullptr)
  982. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  983. addDependencies(resEntry);
  984. }
  985. else
  986. deletedEntries.push_back(resEntry);
  987. }
  988. else if(child->type == LibraryEntryType::Directory)
  989. {
  990. if (FileSystem::isDirectory(child->path))
  991. todo.push(static_cast<DirectoryEntry*>(child));
  992. else
  993. deletedEntries.push_back(child);
  994. }
  995. }
  996. }
  997. // Remove entries that no longer have corresponding files
  998. for (auto& deletedEntry : deletedEntries)
  999. {
  1000. if (deletedEntry->type == LibraryEntryType::File)
  1001. {
  1002. ResourceEntry* resEntry = static_cast<ResourceEntry*>(deletedEntry);
  1003. deleteResourceInternal(resEntry);
  1004. }
  1005. else
  1006. {
  1007. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1008. deleteDirectoryInternal(dirEntry);
  1009. }
  1010. }
  1011. // Clean up internal library folder from obsolete files
  1012. Path internalResourcesFolder = mProjectFolder;
  1013. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1014. Vector<Path> toDelete;
  1015. auto processFile = [&](const Path& file)
  1016. {
  1017. String uuid = file.getFilename(false);
  1018. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1019. {
  1020. mResourceManifest->unregisterResource(uuid);
  1021. toDelete.push_back(file);
  1022. }
  1023. return true;
  1024. };
  1025. FileSystem::iterate(internalResourcesFolder, processFile);
  1026. for (auto& entry : toDelete)
  1027. FileSystem::remove(entry);
  1028. mIsLoaded = true;
  1029. }
  1030. void ProjectLibrary::clearEntries()
  1031. {
  1032. if (mRootEntry == nullptr)
  1033. return;
  1034. std::function<void(LibraryEntry*)> deleteRecursive =
  1035. [&](LibraryEntry* entry)
  1036. {
  1037. if (entry->type == LibraryEntryType::Directory)
  1038. {
  1039. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1040. for (auto& child : dirEntry->mChildren)
  1041. deleteRecursive(child);
  1042. }
  1043. bs_delete(entry);
  1044. };
  1045. deleteRecursive(mRootEntry);
  1046. mRootEntry = nullptr;
  1047. }
  1048. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1049. {
  1050. Vector<Path> output;
  1051. if (entry->meta == nullptr)
  1052. return output;
  1053. if (entry->meta->getTypeID() == TID_Shader)
  1054. {
  1055. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1056. for (auto& include : metaData->includes)
  1057. output.push_back(include);
  1058. }
  1059. return output;
  1060. }
  1061. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1062. {
  1063. Vector<Path> dependencies = getImportDependencies(entry);
  1064. for (auto& dependency : dependencies)
  1065. mDependencies[dependency].push_back(entry->path);
  1066. }
  1067. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1068. {
  1069. Vector<Path> dependencies = getImportDependencies(entry);
  1070. for (auto& dependency : dependencies)
  1071. {
  1072. Vector<Path>& curDependencies = mDependencies[dependency];
  1073. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1074. [&](const Path& x)
  1075. {
  1076. return x == entry->path;
  1077. });
  1078. curDependencies.erase(iterRemove, curDependencies.end());
  1079. }
  1080. }
  1081. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1082. {
  1083. auto iterFind = mDependencies.find(entryPath);
  1084. if (iterFind == mDependencies.end())
  1085. return;
  1086. // Make a copy since we might modify this list during reimport
  1087. Vector<Path> dependencies = iterFind->second;
  1088. for (auto& dependency : dependencies)
  1089. {
  1090. LibraryEntry* entry = findEntry(dependency);
  1091. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1092. {
  1093. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  1094. ImportOptionsPtr importOptions;
  1095. if (resEntry->meta != nullptr)
  1096. importOptions = resEntry->meta->getImportOptions();
  1097. reimportResourceInternal(resEntry, importOptions, true);
  1098. }
  1099. }
  1100. }
  1101. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1102. {
  1103. return ProjectLibrary::instance();
  1104. }
  1105. }