BsProjectLibrary.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Library/BsProjectLibrary.h"
  4. #include "FileSystem/BsFileSystem.h"
  5. #include "Error/BsException.h"
  6. #include "Resources/BsResources.h"
  7. #include "Resources/BsResourceManifest.h"
  8. #include "Importer/BsImporter.h"
  9. #include "Library/BsProjectResourceMeta.h"
  10. #include "Resources/BsResources.h"
  11. #include "Importer/BsImporter.h"
  12. #include "Importer/BsImportOptions.h"
  13. #include "Serialization/BsFileSerializer.h"
  14. #include "Debug/BsDebug.h"
  15. #include "Library/BsProjectLibraryEntries.h"
  16. #include "Resources/BsResource.h"
  17. #include "BsEditorApplication.h"
  18. #include "Material/BsShader.h"
  19. #include "Image/BsTexture.h"
  20. #include "String/BsUnicode.h"
  21. #include "CoreThread/BsCoreThread.h"
  22. #include <regex>
  23. #include "Threading/BsTaskScheduler.h"
  24. using namespace std::placeholders;
  25. namespace bs
  26. {
  27. ProjectResourceIcons generatePreviewIcons(Resource& resource)
  28. {
  29. ProjectResourceIcons icons;
  30. const UINT32 typeId = resource.getTypeId();
  31. if(typeId == TID_Texture)
  32. {
  33. Texture& texture = static_cast<Texture&>(resource);
  34. const TextureProperties& props = texture.getProperties();
  35. const SPtr<PixelData> srcData = props.allocBuffer(0, 0);
  36. AsyncOp readOp = texture.readData(srcData);
  37. gCoreThread().submitAll(true);
  38. // 256
  39. const SPtr<PixelData> data256 = PixelData::create(256, 256, 1, props.getFormat());
  40. PixelUtil::scale(*srcData, *data256);
  41. // 192
  42. const SPtr<PixelData> data192 = PixelData::create(192, 192, 1, props.getFormat());
  43. PixelUtil::scale(*data256, *data192);
  44. // 128
  45. const SPtr<PixelData> data128 = PixelData::create(128, 128, 1, props.getFormat());
  46. PixelUtil::scale(*data192, *data128);
  47. // 96
  48. const SPtr<PixelData> data96 = PixelData::create(96, 96, 1, props.getFormat());
  49. PixelUtil::scale(*data128, *data96);
  50. // 64
  51. const SPtr<PixelData> data64 = PixelData::create(64, 64, 1, props.getFormat());
  52. PixelUtil::scale(*data96, *data64);
  53. // 48
  54. const SPtr<PixelData> data48 = PixelData::create(48, 48, 1, props.getFormat());
  55. PixelUtil::scale(*data64, *data48);
  56. // 32
  57. const SPtr<PixelData> data32 = PixelData::create(32, 32, 1, props.getFormat());
  58. PixelUtil::scale(*data48, *data32);
  59. // 16
  60. const SPtr<PixelData> data16 = PixelData::create(16, 16, 1, props.getFormat());
  61. PixelUtil::scale(*data32, *data16);
  62. icons.icon16 = Texture::create(data16);
  63. icons.icon32 = Texture::create(data32);
  64. icons.icon48 = Texture::create(data48);
  65. icons.icon64 = Texture::create(data64);
  66. icons.icon96 = Texture::create(data96);
  67. icons.icon128 = Texture::create(data128);
  68. icons.icon192 = Texture::create(data192);
  69. icons.icon256 = Texture::create(data256);
  70. }
  71. return icons;
  72. }
  73. const Path TEMP_DIR = "Temp/";
  74. const Path INTERNAL_TEMP_DIR = PROJECT_INTERNAL_DIR + TEMP_DIR;
  75. const Path ProjectLibrary::RESOURCES_DIR = "Resources/";
  76. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + RESOURCES_DIR;
  77. const char* ProjectLibrary::LIBRARY_ENTRIES_FILENAME = "ProjectLibrary.asset";
  78. const char* ProjectLibrary::RESOURCE_MANIFEST_FILENAME = "ResourceManifest.asset";
  79. ProjectLibrary::LibraryEntry::LibraryEntry()
  80. :type(LibraryEntryType::Directory)
  81. { }
  82. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const String& name, DirectoryEntry* parent,
  83. LibraryEntryType type)
  84. :type(type), path(path), elementName(name), elementNameHash(bs_hash(UTF8::toLower(name))), parent(parent)
  85. { }
  86. ProjectLibrary::FileEntry::FileEntry(const Path& path, const String& name, DirectoryEntry* parent)
  87. :LibraryEntry(path, name, parent, LibraryEntryType::File)
  88. { }
  89. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const String& name, DirectoryEntry* parent)
  90. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  91. { }
  92. ProjectLibrary::ProjectLibrary()
  93. : mRootEntry(nullptr), mIsLoaded(false)
  94. {
  95. mRootEntry = bs_shared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  96. }
  97. ProjectLibrary::~ProjectLibrary()
  98. {
  99. _finishQueuedImports(true);
  100. clearEntries();
  101. }
  102. UINT32 ProjectLibrary::checkForModifications(const Path& fullPath)
  103. {
  104. UINT32 resourcesToImport = 0;
  105. if (!mResourcesFolder.includes(fullPath))
  106. return resourcesToImport; // Folder not part of our resources path, so no modifications
  107. if(mRootEntry == nullptr)
  108. mRootEntry = bs_shared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  109. Path pathToSearch = fullPath;
  110. USPtr<LibraryEntry> entry = findEntry(pathToSearch);
  111. if (entry == nullptr) // File could be new, try to find parent directory entry
  112. {
  113. if (FileSystem::exists(pathToSearch))
  114. {
  115. if (isMeta(pathToSearch))
  116. {
  117. Path sourceFilePath = pathToSearch;
  118. sourceFilePath.setExtension("");
  119. if (!FileSystem::isFile(sourceFilePath))
  120. {
  121. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  122. FileSystem::remove(pathToSearch);
  123. }
  124. }
  125. else
  126. {
  127. Path parentDirPath = pathToSearch.getParent();
  128. entry = findEntry(parentDirPath);
  129. // Cannot find parent directory. Create the needed hierarchy.
  130. DirectoryEntry* entryParent = nullptr;
  131. DirectoryEntry* newHierarchyParent = nullptr;
  132. if (entry == nullptr)
  133. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  134. else
  135. entryParent = static_cast<DirectoryEntry*>(entry.get());
  136. if (FileSystem::isFile(pathToSearch))
  137. addResourceInternal(entryParent, pathToSearch);
  138. else if (FileSystem::isDirectory(pathToSearch))
  139. {
  140. addDirectoryInternal(entryParent, pathToSearch);
  141. resourcesToImport += checkForModifications(pathToSearch);
  142. }
  143. }
  144. }
  145. }
  146. else if(entry->type == LibraryEntryType::File)
  147. {
  148. if(FileSystem::isFile(entry->path))
  149. {
  150. FileEntry* resEntry = static_cast<FileEntry*>(entry.get());
  151. if(reimportResourceInternal(resEntry))
  152. resourcesToImport++;
  153. }
  154. else
  155. deleteResourceInternal(std::static_pointer_cast<FileEntry>(entry));
  156. }
  157. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  158. {
  159. if(!FileSystem::isDirectory(entry->path))
  160. {
  161. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(entry));
  162. }
  163. else
  164. {
  165. Stack<DirectoryEntry*> todo;
  166. todo.push(static_cast<DirectoryEntry*>(entry.get()));
  167. Vector<Path> childFiles;
  168. Vector<Path> childDirectories;
  169. Vector<bool> existingEntries;
  170. Vector<USPtr<LibraryEntry>> toDelete;
  171. while(!todo.empty())
  172. {
  173. DirectoryEntry* currentDir = todo.top();
  174. todo.pop();
  175. existingEntries.clear();
  176. existingEntries.resize(currentDir->mChildren.size());
  177. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  178. existingEntries[i] = false;
  179. childFiles.clear();
  180. childDirectories.clear();
  181. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  182. for(auto& filePath : childFiles)
  183. {
  184. if(isMeta(filePath))
  185. {
  186. Path sourceFilePath = filePath;
  187. sourceFilePath.setExtension("");
  188. if(!FileSystem::isFile(sourceFilePath))
  189. {
  190. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  191. FileSystem::remove(filePath);
  192. }
  193. }
  194. else
  195. {
  196. FileEntry* existingEntry = nullptr;
  197. UINT32 idx = 0;
  198. for(auto& child : currentDir->mChildren)
  199. {
  200. if(child->type == LibraryEntryType::File && child->path == filePath)
  201. {
  202. existingEntries[idx] = true;
  203. existingEntry = static_cast<FileEntry*>(child.get());
  204. break;
  205. }
  206. idx++;
  207. }
  208. if(existingEntry != nullptr)
  209. {
  210. if(reimportResourceInternal(existingEntry))
  211. resourcesToImport++;
  212. }
  213. else
  214. {
  215. addResourceInternal(currentDir, filePath);
  216. resourcesToImport++;
  217. }
  218. }
  219. }
  220. for(auto& dirPath : childDirectories)
  221. {
  222. DirectoryEntry* existingEntry = nullptr;
  223. UINT32 idx = 0;
  224. for(auto& child : currentDir->mChildren)
  225. {
  226. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  227. {
  228. existingEntries[idx] = true;
  229. existingEntry = static_cast<DirectoryEntry*>(child.get());
  230. break;
  231. }
  232. idx++;
  233. }
  234. if(existingEntry == nullptr)
  235. addDirectoryInternal(currentDir, dirPath);
  236. }
  237. {
  238. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  239. {
  240. if(existingEntries[i])
  241. continue;
  242. toDelete.push_back(currentDir->mChildren[i]);
  243. }
  244. for(auto& child : toDelete)
  245. {
  246. if(child->type == LibraryEntryType::Directory)
  247. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(child));
  248. else if(child->type == LibraryEntryType::File)
  249. deleteResourceInternal(std::static_pointer_cast<FileEntry>(child));
  250. }
  251. toDelete.clear();
  252. }
  253. for(auto& child : currentDir->mChildren)
  254. {
  255. if(child->type == LibraryEntryType::Directory)
  256. todo.push(static_cast<DirectoryEntry*>(child.get()));
  257. }
  258. }
  259. }
  260. }
  261. return resourcesToImport;
  262. }
  263. USPtr<ProjectLibrary::FileEntry> ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  264. const SPtr<ImportOptions>& importOptions, bool forceReimport, bool synchronous)
  265. {
  266. USPtr<FileEntry> newResource = bs_shared_ptr_new<FileEntry>(filePath, filePath.getTail(), parent);
  267. parent->mChildren.push_back(newResource);
  268. reimportResourceInternal(newResource.get(), importOptions, forceReimport, false, synchronous);
  269. onEntryAdded(newResource->path);
  270. return newResource;
  271. }
  272. USPtr<ProjectLibrary::DirectoryEntry> ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  273. {
  274. USPtr<DirectoryEntry> newEntry = bs_shared_ptr_new<DirectoryEntry>(dirPath, dirPath.getTail(), parent);
  275. parent->mChildren.push_back(newEntry);
  276. onEntryAdded(newEntry->path);
  277. return newEntry;
  278. }
  279. void ProjectLibrary::deleteResourceInternal(USPtr<FileEntry> resource)
  280. {
  281. if(resource->meta != nullptr)
  282. {
  283. auto& resourceMetas = resource->meta->getResourceMetaData();
  284. for(auto& entry : resourceMetas)
  285. {
  286. const UUID& uuid = entry->getUUID();
  287. Path path;
  288. if (mResourceManifest->uuidToFilePath(uuid, path))
  289. {
  290. if (FileSystem::isFile(path))
  291. FileSystem::remove(path);
  292. mResourceManifest->unregisterResource(uuid);
  293. }
  294. mUUIDToPath.erase(uuid);
  295. }
  296. }
  297. Path metaPath = getMetaPath(resource->path);
  298. if (FileSystem::isFile(metaPath))
  299. FileSystem::remove(metaPath);
  300. DirectoryEntry* parent = resource->parent;
  301. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  302. [&] (const USPtr<LibraryEntry>& entry) { return entry == resource; });
  303. parent->mChildren.erase(findIter);
  304. Path originalPath = resource->path;
  305. onEntryRemoved(originalPath);
  306. const auto iterQueuedImport = mQueuedImports.find(resource.get());
  307. if(iterQueuedImport != mQueuedImports.end())
  308. iterQueuedImport->second->canceled = true;
  309. removeDependencies(resource.get());
  310. *resource = FileEntry();
  311. reimportDependants(originalPath);
  312. }
  313. void ProjectLibrary::deleteDirectoryInternal(USPtr<DirectoryEntry> directory)
  314. {
  315. if(directory == mRootEntry)
  316. mRootEntry = nullptr;
  317. Vector<USPtr<LibraryEntry>> childrenToDestroy = directory->mChildren;
  318. for(auto& child : childrenToDestroy)
  319. {
  320. if(child->type == LibraryEntryType::Directory)
  321. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(child));
  322. else
  323. deleteResourceInternal(std::static_pointer_cast<FileEntry>(child));
  324. }
  325. DirectoryEntry* parent = directory->parent;
  326. if(parent != nullptr)
  327. {
  328. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  329. [&] (const USPtr<LibraryEntry>& entry) { return entry == directory; });
  330. parent->mChildren.erase(findIter);
  331. }
  332. onEntryRemoved(directory->path);
  333. *directory = DirectoryEntry();
  334. }
  335. bool ProjectLibrary::reimportResourceInternal(FileEntry* fileEntry, const SPtr<ImportOptions>& importOptions,
  336. bool forceReimport, bool pruneResourceMetas, bool synchronous)
  337. {
  338. Path metaPath = fileEntry->path;
  339. metaPath.setFilename(metaPath.getFilename() + ".meta");
  340. // If the file doesn't have meta-data, attempt to read it from a meta-file, if one exists. This can only happen
  341. // if library data is obsolete (e.g. when adding files from another copy of the project)
  342. if(fileEntry->meta == nullptr)
  343. {
  344. if(FileSystem::isFile(metaPath))
  345. {
  346. FileDecoder fs(metaPath);
  347. SPtr<IReflectable> loadedMeta = fs.decode();
  348. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  349. {
  350. const SPtr<ProjectFileMeta>& fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  351. fileEntry->meta = fileMeta;
  352. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  353. if (!resourceMetas.empty())
  354. {
  355. mUUIDToPath[resourceMetas[0]->getUUID()] = fileEntry->path;
  356. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  357. {
  358. const SPtr<ProjectResourceMeta>& entry = resourceMetas[i];
  359. mUUIDToPath[entry->getUUID()] = fileEntry->path + entry->getUniqueName();
  360. }
  361. }
  362. }
  363. }
  364. }
  365. if (!isUpToDate(fileEntry) || forceReimport)
  366. {
  367. // Note: If resource is native we just copy it to the internal folder. We could avoid the copy and
  368. // load the resource directly from the Resources folder but that requires complicating library code.
  369. const bool isNativeResource = isNative(fileEntry->path);
  370. SPtr<ImportOptions> curImportOptions = nullptr;
  371. if (importOptions == nullptr && !isNativeResource)
  372. {
  373. if (fileEntry->meta != nullptr)
  374. curImportOptions = fileEntry->meta->getImportOptions();
  375. else
  376. curImportOptions = Importer::instance().createImportOptions(fileEntry->path);
  377. }
  378. else
  379. curImportOptions = importOptions;
  380. SPtr<QueuedImport> queuedImport = bs_shared_ptr_new<QueuedImport>();
  381. queuedImport->filePath = fileEntry->path;
  382. queuedImport->importOptions = curImportOptions;
  383. queuedImport->pruneMetas = pruneResourceMetas;
  384. queuedImport->native = isNativeResource;
  385. queuedImport->timestamp = std::time(nullptr);
  386. // If import is already queued for this file make the tasks dependant so they don't execute at the same time,
  387. // and so they execute in the proper order
  388. SPtr<Task> dependency;
  389. const auto iterFind = mQueuedImports.find(fileEntry);
  390. if (iterFind != mQueuedImports.end())
  391. {
  392. dependency = iterFind->second->importTask;
  393. // Note: We should cancel the task here so it doesn't run unnecessarily. But if the task is already
  394. // running it shouldn't be canceled as dependencies still need to wait on it (since cancelling a
  395. // running task doesn't actually stop it). Yet there is currently no good wait to check if task
  396. // is currently running.
  397. // Dependency being imported async but we want the current resource right away. Wait until dependency is
  398. // done otherwise when dependency finishes it will overwrite whatever we write now.
  399. if(synchronous && dependency)
  400. {
  401. if (finishQueuedImport(fileEntry, *iterFind->second, true))
  402. mQueuedImports.erase(iterFind);
  403. }
  404. }
  405. // Needs to be pass a weak pointer to worker methods since internally it holds a reference to the task itself,
  406. // and we can't have the task closure holding a reference back, otherwise it leaks
  407. std::weak_ptr<QueuedImport> queuedImportWeak = queuedImport;
  408. if(!isNativeResource)
  409. {
  410. // Find UUIDs for any existing sub-resources
  411. if (fileEntry->meta != nullptr)
  412. {
  413. const Vector<SPtr<ProjectResourceMeta>>& resourceMetas = fileEntry->meta->getAllResourceMetaData();
  414. for (auto& entry : resourceMetas)
  415. queuedImport->resources.emplace_back(entry->getUniqueName(), nullptr, entry->getUUID());
  416. }
  417. // Perform import, register the resources and their UUID in the QueuedImport structure and save the
  418. // resource on disk
  419. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  420. {
  421. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  422. Vector<SubResourceRaw> importedResources = gImporter()._importAll(queuedImport->filePath,
  423. queuedImport->importOptions);
  424. if (!importedResources.empty())
  425. {
  426. Path outputPath = projectFolder;
  427. outputPath.append(INTERNAL_TEMP_DIR);
  428. if (!FileSystem::isDirectory(outputPath))
  429. FileSystem::createDir(outputPath);
  430. for (auto& entry : importedResources)
  431. {
  432. String subresourceName = entry.name;
  433. Path::stripInvalid(subresourceName);
  434. UUID uuid;
  435. {
  436. // Any access to queuedImport->resources must be locked
  437. Lock lock(mutex);
  438. auto iterFind = std::find_if(queuedImport->resources.begin(), queuedImport->resources.end(),
  439. [&subresourceName](const QueuedImportResource& importResource)
  440. {
  441. return importResource.name == subresourceName;
  442. });
  443. if (iterFind != queuedImport->resources.end())
  444. iterFind->resource = entry.value;
  445. else
  446. {
  447. queuedImport->resources.push_back(QueuedImportResource(entry.name, entry.value,
  448. UUID::EMPTY));
  449. iterFind = queuedImport->resources.end() - 1;
  450. }
  451. if (iterFind->uuid.empty())
  452. iterFind->uuid = UUIDGenerator::generateRandom();
  453. uuid = iterFind->uuid;
  454. }
  455. const String uuidStr = uuid.toString();
  456. outputPath.setFilename(uuidStr + ".asset");
  457. gResources()._save(entry.value, outputPath, true);
  458. }
  459. }
  460. };
  461. if(!synchronous)
  462. {
  463. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  464. dependency);
  465. }
  466. else
  467. importAsync();
  468. }
  469. else
  470. {
  471. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  472. // This can happen if library isn't properly saved before exiting the application.
  473. if (fileEntry->meta != nullptr)
  474. {
  475. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  476. mResourceManifest->registerResource(resourceMetas[0]->getUUID(), fileEntry->path);
  477. }
  478. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  479. {
  480. // Don't load dependencies because we don't need them, but also because they might not be in the
  481. // manifest which would screw up their UUIDs.
  482. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  483. HResource resource = gResources().load(queuedImport->filePath, ResourceLoadFlag::KeepSourceData);
  484. if (resource)
  485. {
  486. Path outputPath = projectFolder;
  487. outputPath.append(INTERNAL_TEMP_DIR);
  488. if (!FileSystem::isDirectory(outputPath))
  489. FileSystem::createDir(outputPath);
  490. {
  491. // Any access to queuedImport->resources must be locked
  492. Lock lock(mutex);
  493. queuedImport->resources.push_back(QueuedImportResource("primary", resource.getInternalPtr(),
  494. resource.getUUID()));
  495. }
  496. const String uuidStr = resource.getUUID().toString();
  497. outputPath.setFilename(uuidStr + ".asset");
  498. gResources()._save(resource.getInternalPtr(), outputPath, true);
  499. }
  500. };
  501. if(!synchronous)
  502. {
  503. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  504. dependency);
  505. }
  506. else
  507. importAsync();
  508. }
  509. if(!synchronous)
  510. {
  511. TaskScheduler::instance().addTask(queuedImport->importTask);
  512. mQueuedImports[fileEntry] = queuedImport;
  513. }
  514. if(synchronous)
  515. finishQueuedImport(fileEntry, *queuedImport, true);
  516. return true;
  517. }
  518. return false;
  519. }
  520. bool ProjectLibrary::finishQueuedImport(FileEntry* fileEntry, const QueuedImport& import, bool wait)
  521. {
  522. if (import.importTask != nullptr && !import.importTask->isComplete())
  523. {
  524. if (wait)
  525. import.importTask->wait();
  526. else
  527. return false;
  528. }
  529. // We wait on canceled task to finish and then just discard the results because any dependant tasks need to be
  530. // aware this tasks exists, so we can't just remove it straight away.
  531. if (import.canceled)
  532. return true;
  533. Path metaPath = fileEntry->path;
  534. metaPath.setFilename(metaPath.getFilename() + ".meta");
  535. Vector<SPtr<ProjectResourceMeta>> existingMetas;
  536. if (fileEntry->meta == nullptr) // Build a brand new meta-file
  537. fileEntry->meta = ProjectFileMeta::create(import.importOptions);
  538. else // Existing meta-file, which needs to be updated
  539. {
  540. // Remove existing dependencies (they will be re-added later)
  541. removeDependencies(fileEntry);
  542. existingMetas = fileEntry->meta->getAllResourceMetaData();
  543. fileEntry->meta->clearResourceMetaData();
  544. fileEntry->meta->mImportOptions = import.importOptions;
  545. }
  546. fileEntry->lastUpdateTime = import.timestamp;
  547. Path internalResourcesPath = mProjectFolder;
  548. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  549. if (!FileSystem::isDirectory(internalResourcesPath))
  550. FileSystem::createDir(internalResourcesPath);
  551. Path tempResourcesPath = mProjectFolder;
  552. tempResourcesPath.append(INTERNAL_TEMP_DIR);
  553. // See which sub-resource metas need to be updated, removed or added based on the new resource set
  554. bool isFirst = true;
  555. for (const auto& entry : import.resources)
  556. {
  557. // Entries with no resources are sub-resources that used to exist in this file, but haven't been imported
  558. // this time
  559. if (!entry.resource)
  560. continue;
  561. // Copy the resource file from the temporary directory
  562. const String uuidStr = entry.uuid.toString();
  563. tempResourcesPath.setFilename(uuidStr + ".asset");
  564. internalResourcesPath.setFilename(uuidStr + ".asset");
  565. FileSystem::move(tempResourcesPath, internalResourcesPath);
  566. String name = entry.name;
  567. Path::stripInvalid(name);
  568. const ProjectResourceIcons icons = generatePreviewIcons(*entry.resource);
  569. bool foundMeta = false;
  570. for (auto iterMeta = existingMetas.begin(); iterMeta != existingMetas.end(); ++iterMeta)
  571. {
  572. const SPtr<ProjectResourceMeta>& metaEntry = *iterMeta;
  573. if (name == metaEntry->getUniqueName())
  574. {
  575. // Make sure the UUID we used for saving the resource matches the current one (should always
  576. // be true unless the meta-data somehow changes while the async import is happening)
  577. assert(entry.uuid == metaEntry->getUUID());
  578. HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
  579. gResources().update(importedResource, entry.resource);
  580. metaEntry->setPreviewIcons(icons);
  581. fileEntry->meta->add(metaEntry);
  582. iterMeta = existingMetas.erase(iterMeta);
  583. foundMeta = true;
  584. break;
  585. }
  586. }
  587. if (!foundMeta)
  588. {
  589. HResource importedResource;
  590. // Native resources are always expected to have a handle since Resources::load was called during
  591. // the 'import' step
  592. if (import.native)
  593. {
  594. importedResource = gResources()._getResourceHandle(entry.uuid);
  595. gResources().update(importedResource, entry.resource);
  596. }
  597. else
  598. importedResource = gResources()._createResourceHandle(entry.resource, entry.uuid);
  599. SPtr<ResourceMetaData> subMeta = entry.resource->getMetaData();
  600. const UINT32 typeId = entry.resource->getTypeId();
  601. const UUID& UUID = importedResource.getUUID();
  602. SPtr<ProjectResourceMeta> resMeta = ProjectResourceMeta::create(name, UUID, typeId,
  603. icons, subMeta);
  604. fileEntry->meta->add(resMeta);
  605. }
  606. // Keep resource metas that we are not currently using, in case they get restored so their references
  607. // don't get broken
  608. if (!import.pruneMetas)
  609. {
  610. for (auto& metaEntry : existingMetas)
  611. fileEntry->meta->addInactive(metaEntry);
  612. }
  613. // Update UUID to path mapping
  614. if (isFirst)
  615. mUUIDToPath[entry.uuid] = fileEntry->path;
  616. else
  617. mUUIDToPath[entry.uuid] = fileEntry->path + name;
  618. isFirst = false;
  619. // Register path in manifest
  620. mResourceManifest->registerResource(entry.uuid, internalResourcesPath);
  621. }
  622. // Save the meta file
  623. FileEncoder fs(metaPath);
  624. fs.encode(fileEntry->meta.get());
  625. // Register any dependencies this resource depends on
  626. addDependencies(fileEntry);
  627. // Notify the outside world import is doen
  628. onEntryImported(fileEntry->path);
  629. // Queue any resources dependant on this one for import
  630. reimportDependants(fileEntry->path);
  631. return true;
  632. }
  633. void ProjectLibrary::_finishQueuedImports(bool wait)
  634. {
  635. for(auto iter = mQueuedImports.begin(); iter != mQueuedImports.end();)
  636. {
  637. if(finishQueuedImport(iter->first, *iter->second, wait))
  638. iter = mQueuedImports.erase(iter);
  639. else
  640. ++iter;
  641. }
  642. }
  643. bool ProjectLibrary::isUpToDate(FileEntry* resource) const
  644. {
  645. SPtr<QueuedImport> queuedImport;
  646. if(resource->meta == nullptr)
  647. {
  648. // Allow no meta if import in progress
  649. const auto iterFind = mQueuedImports.find(resource);
  650. if(iterFind == mQueuedImports.end())
  651. return false;
  652. queuedImport = iterFind->second;
  653. }
  654. else
  655. {
  656. auto& resourceMetas = resource->meta->getResourceMetaData();
  657. for (auto& resMeta : resourceMetas)
  658. {
  659. Path internalPath;
  660. if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
  661. return false;
  662. if (!FileSystem::isFile(internalPath))
  663. return false;
  664. }
  665. }
  666. // Note: We're keeping separate update times for queued imports. This allows the import to be cancelled (either by
  667. // user or by app crashing), without updating the actual update time. This way the systems knows to try to reimport
  668. // the resource on the next check. At the same time we don't want our checkForModifications function to keep
  669. // trying to reimport a resource if it's already been queued for import.
  670. const std::time_t lastUpdateTime = queuedImport ? queuedImport->timestamp : resource->lastUpdateTime;
  671. const std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  672. return lastModifiedTime <= lastUpdateTime;
  673. }
  674. Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern)
  675. {
  676. return search(pattern, {});
  677. }
  678. Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern, const Vector<UINT32>& typeIds)
  679. {
  680. Vector<USPtr<LibraryEntry>> foundEntries;
  681. std::regex escape("[.^$|()\\[\\]{}*+?\\\\]");
  682. String replace("\\\\&");
  683. String escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  684. // For some reason MSVC stdlib implementation requires a different pattern than stdlib one
  685. #if BS_PLATFORM == BS_PLATFORM_WIN32
  686. std::regex wildcard("\\\\\\*");
  687. #else
  688. std::regex wildcard("\\\\\\\\\\*");
  689. #endif
  690. String wildcardReplace(".*");
  691. String searchPattern = std::regex_replace(escapedPattern, wildcard, ".*");
  692. std::regex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  693. Stack<DirectoryEntry*> todo;
  694. todo.push(mRootEntry.get());
  695. while (!todo.empty())
  696. {
  697. DirectoryEntry* dirEntry = todo.top();
  698. todo.pop();
  699. for (auto& child : dirEntry->mChildren)
  700. {
  701. if (std::regex_match(child->elementName, searchRegex))
  702. {
  703. if (typeIds.empty())
  704. foundEntries.push_back(child);
  705. else
  706. {
  707. if (child->type == LibraryEntryType::File)
  708. {
  709. FileEntry* childFileEntry = static_cast<FileEntry*>(child.get());
  710. if (childFileEntry->meta != nullptr)
  711. {
  712. auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
  713. for (auto& typeId : typeIds)
  714. {
  715. bool found = false;
  716. for (auto& resMeta : resourceMetas)
  717. {
  718. if (resMeta->getTypeID() == typeId)
  719. {
  720. foundEntries.push_back(child);
  721. found = true;
  722. break;
  723. }
  724. }
  725. if (found)
  726. break;
  727. }
  728. }
  729. }
  730. }
  731. }
  732. if (child->type == LibraryEntryType::Directory)
  733. {
  734. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child.get());
  735. todo.push(childDirEntry);
  736. }
  737. }
  738. }
  739. std::sort(foundEntries.begin(), foundEntries.end(),
  740. [&](const USPtr<LibraryEntry>& a, const USPtr<LibraryEntry>& b)
  741. {
  742. return a->elementName.compare(b->elementName) < 0;
  743. });
  744. return foundEntries;
  745. }
  746. USPtr<ProjectLibrary::LibraryEntry> ProjectLibrary::findEntry(const Path& path) const
  747. {
  748. Path relPath;
  749. const Path* searchPath;
  750. if (path.isAbsolute())
  751. {
  752. if (!mResourcesFolder.includes(path))
  753. return nullptr;
  754. relPath = path.getRelative(mRootEntry->path);
  755. searchPath = &relPath;
  756. }
  757. else
  758. searchPath = &path;
  759. BS_ASSERT(mRootEntry->path == mResourcesFolder);
  760. UINT32 numElems = searchPath->getNumDirectories() + (searchPath->isFile() ? 1 : 0);
  761. UINT32 idx = 0;
  762. USPtr<LibraryEntry> rootLibEntry = mRootEntry;
  763. USPtr<LibraryEntry>* current = &rootLibEntry;
  764. while (current != nullptr)
  765. {
  766. if (idx == numElems)
  767. return *current;
  768. const String& curElem =
  769. (searchPath->isFile() && idx == (numElems - 1)) ? searchPath->getFilename() : (*searchPath)[idx];
  770. if ((*current)->type == LibraryEntryType::Directory)
  771. {
  772. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current->get());
  773. size_t curElemHash = bs_hash(UTF8::toLower(curElem));
  774. current = nullptr;
  775. for (auto& child : dirEntry->mChildren)
  776. {
  777. if(curElemHash != child->elementNameHash)
  778. continue;
  779. if (Path::comparePathElem(curElem, child->elementName))
  780. {
  781. idx++;
  782. current = &child;
  783. break;
  784. }
  785. }
  786. }
  787. else // Found file
  788. {
  789. // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
  790. if (idx == (numElems - 1))
  791. return *current;
  792. else
  793. break; // Not a valid path
  794. }
  795. }
  796. return nullptr;
  797. }
  798. bool ProjectLibrary::isSubresource(const Path& path) const
  799. {
  800. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  801. if (numElems <= 1)
  802. return false;
  803. Path filePath = path;
  804. filePath.makeParent();
  805. LibraryEntry* entry = findEntry(filePath).get();
  806. return entry != nullptr && entry->type == LibraryEntryType::File;
  807. }
  808. SPtr<ProjectResourceMeta> ProjectLibrary::findResourceMeta(const Path& path) const
  809. {
  810. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  811. // Check if it is a subresource path
  812. if(numElems > 1)
  813. {
  814. Path filePath = path;
  815. filePath.makeParent();
  816. LibraryEntry* entry = findEntry(filePath).get();
  817. if (entry == nullptr)
  818. return nullptr;
  819. // Entry is a subresource
  820. if (entry->type == LibraryEntryType::File)
  821. {
  822. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  823. if (fileEntry->meta == nullptr)
  824. return nullptr;
  825. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  826. for(auto& resMeta : resourceMetas)
  827. {
  828. if (resMeta->getUniqueName() == path.getTail())
  829. return resMeta;
  830. }
  831. // Found the file but no subresource or meta information
  832. return nullptr;
  833. }
  834. else // Entry not a subresource
  835. {
  836. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  837. for (auto& child : dirEntry->mChildren)
  838. {
  839. if (Path::comparePathElem(path.getTail(), child->elementName))
  840. {
  841. if (child->type == LibraryEntryType::File)
  842. {
  843. FileEntry* fileEntry = static_cast<FileEntry*>(child.get());
  844. if (fileEntry->meta == nullptr)
  845. return nullptr;
  846. return fileEntry->meta->getResourceMetaData()[0];
  847. }
  848. }
  849. }
  850. return nullptr;
  851. }
  852. }
  853. // Not a subresource path, load directly
  854. {
  855. LibraryEntry* entry = findEntry(path).get();
  856. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  857. return nullptr;
  858. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  859. if (fileEntry->meta == nullptr)
  860. return nullptr;
  861. return fileEntry->meta->getResourceMetaData()[0];
  862. }
  863. }
  864. Path ProjectLibrary::uuidToPath(const UUID& uuid) const
  865. {
  866. auto iterFind = mUUIDToPath.find(uuid);
  867. if (iterFind != mUUIDToPath.end())
  868. return iterFind->second;
  869. return Path::BLANK;
  870. }
  871. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  872. {
  873. if (resource == nullptr)
  874. return;
  875. Path assetPath = path;
  876. if (path.isAbsolute())
  877. {
  878. if (!getResourcesFolder().includes(path))
  879. return;
  880. assetPath = path.getRelative(getResourcesFolder());
  881. }
  882. deleteEntry(assetPath);
  883. resource->setName(path.getFilename(false));
  884. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  885. Resources::instance().save(resource, absPath, false);
  886. Path parentDirPath = absPath.getParent();
  887. USPtr<LibraryEntry> parentEntry = findEntry(parentDirPath);
  888. // Register parent hierarchy if not found
  889. DirectoryEntry* entryParent = nullptr;
  890. if (parentEntry == nullptr)
  891. createInternalParentHierarchy(absPath, nullptr, &entryParent);
  892. else
  893. entryParent = static_cast<DirectoryEntry*>(parentEntry.get());
  894. addResourceInternal(entryParent, absPath, nullptr, true, true);
  895. }
  896. void ProjectLibrary::saveEntry(const HResource& resource)
  897. {
  898. if (resource == nullptr)
  899. return;
  900. Path filePath = uuidToPath(resource.getUUID());
  901. if(filePath.isEmpty())
  902. {
  903. LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
  904. return;
  905. }
  906. filePath.makeAbsolute(getResourcesFolder());
  907. Resources::instance().save(resource, filePath, true);
  908. LibraryEntry* fileEntry = findEntry(filePath).get();
  909. if(fileEntry)
  910. reimportResourceInternal(static_cast<FileEntry*>(fileEntry), nullptr, true, false, true);
  911. }
  912. void ProjectLibrary::createFolderEntry(const Path& path)
  913. {
  914. Path fullPath = path;
  915. if (fullPath.isAbsolute())
  916. {
  917. if (!mResourcesFolder.includes(fullPath))
  918. return;
  919. }
  920. else
  921. fullPath.makeAbsolute(mResourcesFolder);
  922. if (FileSystem::isDirectory(fullPath))
  923. return; // Already exists
  924. FileSystem::createDir(fullPath);
  925. Path parentPath = fullPath.getParent();
  926. DirectoryEntry* newEntryParent = nullptr;
  927. USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
  928. if (newEntryParentLib != nullptr)
  929. {
  930. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  931. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
  932. }
  933. DirectoryEntry* newHierarchyParent = nullptr;
  934. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  935. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  936. addDirectoryInternal(newEntryParent, fullPath);
  937. }
  938. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  939. {
  940. Path oldFullPath = oldPath;
  941. if (!oldFullPath.isAbsolute())
  942. oldFullPath.makeAbsolute(mResourcesFolder);
  943. Path newFullPath = newPath;
  944. if (!newFullPath.isAbsolute())
  945. newFullPath.makeAbsolute(mResourcesFolder);
  946. Path parentPath = newFullPath.getParent();
  947. if (!FileSystem::isDirectory(parentPath))
  948. {
  949. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  950. return;
  951. }
  952. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  953. FileSystem::move(oldFullPath, newFullPath, overwrite);
  954. Path oldMetaPath = getMetaPath(oldFullPath);
  955. Path newMetaPath = getMetaPath(newFullPath);
  956. USPtr<LibraryEntry> oldEntry = findEntry(oldFullPath);
  957. if(oldEntry != nullptr) // Moving from the Resources folder
  958. {
  959. // Moved outside of Resources, delete entry & meta file
  960. if (!mResourcesFolder.includes(newFullPath))
  961. {
  962. if(oldEntry->type == LibraryEntryType::File)
  963. deleteResourceInternal(std::static_pointer_cast<FileEntry>(oldEntry));
  964. else if(oldEntry->type == LibraryEntryType::Directory)
  965. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(oldEntry));
  966. }
  967. else // Just moving internally
  968. {
  969. onEntryRemoved(oldEntry->path);
  970. USPtr<FileEntry> fileEntry = nullptr;
  971. if (oldEntry->type == LibraryEntryType::File)
  972. {
  973. fileEntry = std::static_pointer_cast<FileEntry>(oldEntry);
  974. removeDependencies(fileEntry.get());
  975. // Update uuid <-> path mapping
  976. if(fileEntry->meta != nullptr)
  977. {
  978. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  979. if (!resourceMetas.empty())
  980. {
  981. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  982. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  983. {
  984. SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
  985. const UUID& UUID = resMeta->getUUID();
  986. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  987. }
  988. }
  989. }
  990. }
  991. if(FileSystem::isFile(oldMetaPath))
  992. FileSystem::move(oldMetaPath, newMetaPath);
  993. DirectoryEntry* parent = oldEntry->parent;
  994. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  995. if(findIter != parent->mChildren.end())
  996. parent->mChildren.erase(findIter);
  997. Path parentPath = newFullPath.getParent();
  998. DirectoryEntry* newEntryParent = nullptr;
  999. USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
  1000. if(newEntryParentLib != nullptr)
  1001. {
  1002. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1003. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
  1004. }
  1005. DirectoryEntry* newHierarchyParent = nullptr;
  1006. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  1007. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  1008. newEntryParent->mChildren.push_back(oldEntry);
  1009. oldEntry->parent = newEntryParent;
  1010. oldEntry->path = newFullPath;
  1011. oldEntry->elementName = newFullPath.getTail();
  1012. oldEntry->elementNameHash = bs_hash(UTF8::toLower(oldEntry->elementName));
  1013. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  1014. {
  1015. Stack<LibraryEntry*> todo;
  1016. todo.push(oldEntry.get());
  1017. while(!todo.empty())
  1018. {
  1019. LibraryEntry* curEntry = todo.top();
  1020. todo.pop();
  1021. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  1022. for(auto& child : curDirEntry->mChildren)
  1023. {
  1024. child->path = child->parent->path;
  1025. child->path.append(child->elementName);
  1026. if(child->type == LibraryEntryType::Directory)
  1027. todo.push(child.get());
  1028. }
  1029. }
  1030. }
  1031. onEntryAdded(oldEntry->path);
  1032. if (fileEntry != nullptr)
  1033. {
  1034. reimportDependants(oldFullPath);
  1035. reimportDependants(newFullPath);
  1036. }
  1037. }
  1038. }
  1039. else // Moving from outside of the Resources folder (likely adding a new resource)
  1040. {
  1041. checkForModifications(newFullPath);
  1042. }
  1043. }
  1044. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  1045. {
  1046. Path oldFullPath = oldPath;
  1047. if (!oldFullPath.isAbsolute())
  1048. oldFullPath.makeAbsolute(mResourcesFolder);
  1049. Path newFullPath = newPath;
  1050. if (!newFullPath.isAbsolute())
  1051. newFullPath.makeAbsolute(mResourcesFolder);
  1052. if (!FileSystem::exists(oldFullPath))
  1053. return;
  1054. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  1055. // Copying a file/folder outside of the Resources folder, no special handling needed
  1056. if (!mResourcesFolder.includes(newFullPath))
  1057. return;
  1058. Path parentPath = newFullPath.getParent();
  1059. DirectoryEntry* newEntryParent = nullptr;
  1060. LibraryEntry* newEntryParentLib = findEntry(parentPath).get();
  1061. if (newEntryParentLib != nullptr)
  1062. {
  1063. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1064. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1065. }
  1066. // If the source is outside of Resources folder, just plain import the copy
  1067. LibraryEntry* oldEntry = findEntry(oldFullPath).get();
  1068. if (oldEntry == nullptr)
  1069. {
  1070. checkForModifications(newFullPath);
  1071. return;
  1072. }
  1073. // Both source and destination are within Resources folder, need to preserve import options on the copies
  1074. if (FileSystem::isFile(newFullPath))
  1075. {
  1076. assert(oldEntry->type == LibraryEntryType::File);
  1077. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  1078. SPtr<ImportOptions> importOptions;
  1079. if (oldResEntry->meta != nullptr)
  1080. importOptions = oldResEntry->meta->getImportOptions();
  1081. addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  1082. }
  1083. else
  1084. {
  1085. assert(oldEntry->type == LibraryEntryType::File);
  1086. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  1087. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath).get();
  1088. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  1089. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  1090. while (!todo.empty())
  1091. {
  1092. auto current = todo.top();
  1093. todo.pop();
  1094. DirectoryEntry* sourceDir = std::get<0>(current);
  1095. DirectoryEntry* destDir = std::get<1>(current);
  1096. for (auto& child : sourceDir->mChildren)
  1097. {
  1098. Path childDestPath = destDir->path;
  1099. childDestPath.append(child->path.getTail());
  1100. if (child->type == LibraryEntryType::File)
  1101. {
  1102. FileEntry* childResEntry = static_cast<FileEntry*>(child.get());
  1103. SPtr<ImportOptions> importOptions;
  1104. if (childResEntry->meta != nullptr)
  1105. importOptions = childResEntry->meta->getImportOptions();
  1106. addResourceInternal(destDir, childDestPath, importOptions, true);
  1107. }
  1108. else // Directory
  1109. {
  1110. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child.get());
  1111. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath).get();
  1112. todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. void ProjectLibrary::deleteEntry(const Path& path)
  1119. {
  1120. Path fullPath = path;
  1121. if (!fullPath.isAbsolute())
  1122. fullPath.makeAbsolute(mResourcesFolder);
  1123. if(FileSystem::exists(fullPath))
  1124. FileSystem::remove(fullPath);
  1125. USPtr<LibraryEntry> entry = findEntry(fullPath);
  1126. if(entry != nullptr)
  1127. {
  1128. if(entry->type == LibraryEntryType::File)
  1129. deleteResourceInternal(std::static_pointer_cast<FileEntry>(entry));
  1130. else if(entry->type == LibraryEntryType::Directory)
  1131. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(entry));
  1132. }
  1133. }
  1134. void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport,
  1135. bool synchronous)
  1136. {
  1137. LibraryEntry* entry = findEntry(path).get();
  1138. if (entry != nullptr)
  1139. {
  1140. if (entry->type == LibraryEntryType::File)
  1141. {
  1142. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1143. reimportResourceInternal(resEntry, importOptions, forceReimport, synchronous);
  1144. }
  1145. }
  1146. }
  1147. float ProjectLibrary::getImportProgress(const Path& path) const
  1148. {
  1149. LibraryEntry* entry = findEntry(path).get();
  1150. if (entry == nullptr)
  1151. return 0.0f;
  1152. if(entry->type == LibraryEntryType::Directory)
  1153. return 1.0f;
  1154. // Note: Only supporting binary progress reporting for now
  1155. const auto iterFind = mQueuedImports.find(static_cast<FileEntry*>(entry));
  1156. return iterFind != mQueuedImports.end() ? 0.0f : 1.0f;
  1157. }
  1158. void ProjectLibrary::cancelImport()
  1159. {
  1160. for(auto& entry : mQueuedImports)
  1161. entry.second->canceled = true;
  1162. }
  1163. void ProjectLibrary::waitForQueuedImport(FileEntry* fileEntry)
  1164. {
  1165. const auto iterFind = mQueuedImports.find(fileEntry);
  1166. if (iterFind != mQueuedImports.end())
  1167. {
  1168. if (finishQueuedImport(fileEntry, *iterFind->second, true))
  1169. mQueuedImports.erase(iterFind);
  1170. }
  1171. }
  1172. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  1173. {
  1174. LibraryEntry* entry = findEntry(path).get();
  1175. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1176. return;
  1177. auto fileEntry = static_cast<FileEntry*>(entry);
  1178. // Any queued imports will overwrite the meta file, so make sure they finish first
  1179. waitForQueuedImport(fileEntry);
  1180. if (fileEntry->meta == nullptr)
  1181. return;
  1182. fileEntry->meta->setIncludeInBuild(include);
  1183. Path metaPath = fileEntry->path;
  1184. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1185. FileEncoder fs(metaPath);
  1186. fs.encode(fileEntry->meta.get());
  1187. }
  1188. void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
  1189. {
  1190. LibraryEntry* entry = findEntry(path).get();
  1191. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1192. return;
  1193. auto fileEntry = static_cast<FileEntry*>(entry);
  1194. // Any queued imports will overwrite the meta file, so make sure they finish first
  1195. waitForQueuedImport(fileEntry);
  1196. SPtr<ProjectResourceMeta> resMeta = findResourceMeta(path);
  1197. if (resMeta == nullptr)
  1198. return;
  1199. resMeta->mUserData = userData;
  1200. Path metaPath = fileEntry->path;
  1201. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1202. FileEncoder fs(metaPath);
  1203. fs.encode(fileEntry->meta.get());
  1204. }
  1205. Vector<USPtr<ProjectLibrary::FileEntry>> ProjectLibrary::getResourcesForBuild() const
  1206. {
  1207. Vector<USPtr<FileEntry>> output;
  1208. Stack<DirectoryEntry*> todo;
  1209. todo.push(mRootEntry.get());
  1210. while (!todo.empty())
  1211. {
  1212. DirectoryEntry* directory = todo.top();
  1213. todo.pop();
  1214. for (auto& child : directory->mChildren)
  1215. {
  1216. if (child->type == LibraryEntryType::File)
  1217. {
  1218. FileEntry* resEntry = static_cast<FileEntry*>(child.get());
  1219. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  1220. output.push_back(std::static_pointer_cast<FileEntry>(child));
  1221. }
  1222. else if (child->type == LibraryEntryType::Directory)
  1223. {
  1224. todo.push(static_cast<DirectoryEntry*>(child.get()));
  1225. }
  1226. }
  1227. }
  1228. return output;
  1229. }
  1230. HResource ProjectLibrary::load(const Path& path)
  1231. {
  1232. SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
  1233. if (meta == nullptr)
  1234. return HResource();
  1235. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  1236. const UUID& resUUID = meta->getUUID();
  1237. return gResources().loadFromUUID(resUUID, false, loadFlags);
  1238. }
  1239. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot,
  1240. DirectoryEntry** newHierarchyLeaf)
  1241. {
  1242. Path parentPath = fullPath;
  1243. DirectoryEntry* newEntryParent = nullptr;
  1244. Stack<Path> parentPaths;
  1245. do
  1246. {
  1247. Path newParentPath = parentPath.getParent();
  1248. if(newParentPath == parentPath)
  1249. break;
  1250. LibraryEntry* newEntryParentLib = findEntry(newParentPath).get();
  1251. if(newEntryParentLib != nullptr)
  1252. {
  1253. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1254. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1255. break;
  1256. }
  1257. parentPaths.push(newParentPath);
  1258. parentPath = newParentPath;
  1259. } while (true);
  1260. assert(newEntryParent != nullptr); // Must exist
  1261. if(newHierarchyRoot != nullptr)
  1262. *newHierarchyRoot = newEntryParent;
  1263. while(!parentPaths.empty())
  1264. {
  1265. Path curPath = parentPaths.top();
  1266. parentPaths.pop();
  1267. newEntryParent = addDirectoryInternal(newEntryParent, curPath).get();
  1268. }
  1269. if(newHierarchyLeaf != nullptr)
  1270. *newHierarchyLeaf = newEntryParent;
  1271. }
  1272. Path ProjectLibrary::getMetaPath(const Path& path) const
  1273. {
  1274. Path metaPath = path;
  1275. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1276. return metaPath;
  1277. }
  1278. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1279. {
  1280. return fullPath.getExtension() == ".meta";
  1281. }
  1282. bool ProjectLibrary::isNative(const Path& path) const
  1283. {
  1284. String extension = path.getExtension();
  1285. return extension == ".asset" || extension == ".prefab";
  1286. }
  1287. void ProjectLibrary::unloadLibrary()
  1288. {
  1289. if (!mIsLoaded)
  1290. return;
  1291. _finishQueuedImports(true);
  1292. mProjectFolder = Path::BLANK;
  1293. mResourcesFolder = Path::BLANK;
  1294. clearEntries();
  1295. mRootEntry = bs_shared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1296. mDependencies.clear();
  1297. gResources().unregisterResourceManifest(mResourceManifest);
  1298. mResourceManifest = nullptr;
  1299. mIsLoaded = false;
  1300. }
  1301. void ProjectLibrary::makeEntriesRelative()
  1302. {
  1303. // Make all paths relative before saving
  1304. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1305. [&](LibraryEntry* entry, const Path& root)
  1306. {
  1307. entry->path.makeRelative(root);
  1308. if (entry->type == LibraryEntryType::Directory)
  1309. {
  1310. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1311. for (auto& child : dirEntry->mChildren)
  1312. makeRelative(child.get(), root);
  1313. }
  1314. };
  1315. Path root = getResourcesFolder();
  1316. makeRelative(mRootEntry.get(), root);
  1317. }
  1318. void ProjectLibrary::makeEntriesAbsolute()
  1319. {
  1320. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1321. [&](LibraryEntry* entry, const Path& root)
  1322. {
  1323. entry->path.makeAbsolute(root);
  1324. if (entry->type == LibraryEntryType::Directory)
  1325. {
  1326. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1327. for (auto& child : dirEntry->mChildren)
  1328. makeAbsolute(child.get(), root);
  1329. }
  1330. };
  1331. Path root = getResourcesFolder();
  1332. makeAbsolute(mRootEntry.get(), root);
  1333. }
  1334. void ProjectLibrary::saveLibrary()
  1335. {
  1336. if (!mIsLoaded)
  1337. return;
  1338. // Make all paths relative before saving
  1339. makeEntriesRelative();
  1340. SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(mRootEntry);
  1341. Path libraryEntriesPath = mProjectFolder;
  1342. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1343. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1344. FileEncoder fs(libraryEntriesPath);
  1345. fs.encode(libEntries.get());
  1346. // Restore absolute entry paths
  1347. makeEntriesAbsolute();
  1348. Path resourceManifestPath = mProjectFolder;
  1349. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1350. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1351. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1352. }
  1353. void ProjectLibrary::loadLibrary()
  1354. {
  1355. unloadLibrary();
  1356. mProjectFolder = gEditorApplication().getProjectPath();
  1357. mResourcesFolder = mProjectFolder;
  1358. mResourcesFolder.append(RESOURCES_DIR);
  1359. mRootEntry = bs_shared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1360. Path libraryEntriesPath = mProjectFolder;
  1361. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1362. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1363. if(FileSystem::exists(libraryEntriesPath))
  1364. {
  1365. FileDecoder fs(libraryEntriesPath);
  1366. SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1367. mRootEntry = libEntries->getRootEntry();
  1368. mRootEntry->parent = nullptr;
  1369. }
  1370. // Entries are stored relative to project folder, but we want their absolute paths now
  1371. makeEntriesAbsolute();
  1372. // Load resource manifest
  1373. Path resourceManifestPath = mProjectFolder;
  1374. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1375. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1376. if (FileSystem::exists(resourceManifestPath))
  1377. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1378. else
  1379. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1380. gResources().registerResourceManifest(mResourceManifest);
  1381. // Load all meta files
  1382. Stack<DirectoryEntry*> todo;
  1383. todo.push(mRootEntry.get());
  1384. Vector<USPtr<LibraryEntry>> deletedEntries;
  1385. while(!todo.empty())
  1386. {
  1387. DirectoryEntry* curDir = todo.top();
  1388. todo.pop();
  1389. for(auto& child : curDir->mChildren)
  1390. {
  1391. if(child->type == LibraryEntryType::File)
  1392. {
  1393. USPtr<FileEntry> resEntry = std::static_pointer_cast<FileEntry>(child);
  1394. if (FileSystem::isFile(resEntry->path))
  1395. {
  1396. if (resEntry->meta == nullptr)
  1397. {
  1398. Path metaPath = resEntry->path;
  1399. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1400. if (FileSystem::isFile(metaPath))
  1401. {
  1402. FileDecoder fs(metaPath);
  1403. SPtr<IReflectable> loadedMeta = fs.decode();
  1404. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1405. {
  1406. SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1407. resEntry->meta = fileMeta;
  1408. }
  1409. }
  1410. }
  1411. if (resEntry->meta != nullptr)
  1412. {
  1413. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1414. if (!resourceMetas.empty())
  1415. {
  1416. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1417. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1418. {
  1419. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  1420. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1421. }
  1422. }
  1423. }
  1424. addDependencies(resEntry.get());
  1425. }
  1426. else
  1427. deletedEntries.push_back(resEntry);
  1428. }
  1429. else if(child->type == LibraryEntryType::Directory)
  1430. {
  1431. if (FileSystem::isDirectory(child->path))
  1432. todo.push(static_cast<DirectoryEntry*>(child.get()));
  1433. else
  1434. deletedEntries.push_back(child);
  1435. }
  1436. }
  1437. }
  1438. // Remove entries that no longer have corresponding files
  1439. for (auto& deletedEntry : deletedEntries)
  1440. {
  1441. if (deletedEntry->type == LibraryEntryType::File)
  1442. deleteResourceInternal(std::static_pointer_cast<FileEntry>(deletedEntry));
  1443. else
  1444. deleteDirectoryInternal(std::static_pointer_cast<DirectoryEntry>(deletedEntry));
  1445. }
  1446. // Clean up internal library folder from obsolete files
  1447. Path internalResourcesFolder = mProjectFolder;
  1448. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1449. if (FileSystem::exists(internalResourcesFolder))
  1450. {
  1451. Vector<Path> toDelete;
  1452. auto processFile = [&](const Path& file)
  1453. {
  1454. UUID uuid = UUID(file.getFilename(false));
  1455. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1456. {
  1457. mResourceManifest->unregisterResource(uuid);
  1458. toDelete.push_back(file);
  1459. }
  1460. return true;
  1461. };
  1462. FileSystem::iterate(internalResourcesFolder, processFile);
  1463. for (auto& entry : toDelete)
  1464. FileSystem::remove(entry);
  1465. }
  1466. mIsLoaded = true;
  1467. }
  1468. void ProjectLibrary::clearEntries()
  1469. {
  1470. if (mRootEntry == nullptr)
  1471. return;
  1472. std::function<void(LibraryEntry*)> invalidateRecursive =
  1473. [&](LibraryEntry* entry)
  1474. {
  1475. if (entry->type == LibraryEntryType::Directory)
  1476. {
  1477. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1478. for (auto& child : dirEntry->mChildren)
  1479. invalidateRecursive(child.get());
  1480. *dirEntry = DirectoryEntry();
  1481. }
  1482. else
  1483. {
  1484. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  1485. *fileEntry = FileEntry();
  1486. }
  1487. };
  1488. assert(mQueuedImports.empty());
  1489. invalidateRecursive(mRootEntry.get());
  1490. mRootEntry = nullptr;
  1491. }
  1492. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1493. {
  1494. Vector<Path> output;
  1495. if (entry->meta == nullptr)
  1496. return output;
  1497. auto& resourceMetas = entry->meta->getResourceMetaData();
  1498. for(auto& resMeta : resourceMetas)
  1499. {
  1500. if (resMeta->getTypeID() == TID_Shader)
  1501. {
  1502. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1503. for (auto& include : metaData->includes)
  1504. output.push_back(include);
  1505. }
  1506. }
  1507. return output;
  1508. }
  1509. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1510. {
  1511. Vector<Path> dependencies = getImportDependencies(entry);
  1512. for (auto& dependency : dependencies)
  1513. mDependencies[dependency].push_back(entry->path);
  1514. }
  1515. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1516. {
  1517. Vector<Path> dependencies = getImportDependencies(entry);
  1518. for (auto& dependency : dependencies)
  1519. {
  1520. Vector<Path>& curDependencies = mDependencies[dependency];
  1521. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1522. [&](const Path& x)
  1523. {
  1524. return x == entry->path;
  1525. });
  1526. curDependencies.erase(iterRemove, curDependencies.end());
  1527. }
  1528. }
  1529. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1530. {
  1531. auto iterFind = mDependencies.find(entryPath);
  1532. if (iterFind == mDependencies.end())
  1533. return;
  1534. // Make a copy since we might modify this list during reimport
  1535. Vector<Path> dependencies = iterFind->second;
  1536. for (auto& dependency : dependencies)
  1537. {
  1538. LibraryEntry* entry = findEntry(dependency).get();
  1539. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1540. {
  1541. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1542. SPtr<ImportOptions> importOptions;
  1543. if (resEntry->meta != nullptr)
  1544. importOptions = resEntry->meta->getImportOptions();
  1545. reimportResourceInternal(resEntry, importOptions, true);
  1546. }
  1547. }
  1548. }
  1549. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1550. {
  1551. return ProjectLibrary::instance();
  1552. }
  1553. }