BsProjectLibrary.cpp 39 KB

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