BsProjectLibrary.cpp 55 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_ushared_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_ushared_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(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(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(static_pointer_cast<DirectoryEntry>(child));
  248. else if(child->type == LibraryEntryType::File)
  249. deleteResourceInternal(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_ushared_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_ushared_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(static_pointer_cast<DirectoryEntry>(child));
  322. else
  323. deleteResourceInternal(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. // Need this reference just so the dependency is kept alive, otherwise it goes out of scope when we
  394. // remove or overwrite it from mQueuedImports map
  395. queuedImport->dependsOn = iterFind->second;
  396. // Note: We should cancel the task here so it doesn't run unnecessarily. But if the task is already
  397. // running it shouldn't be canceled as dependencies still need to wait on it (since cancelling a
  398. // running task doesn't actually stop it). Yet there is currently no good wait to check if task
  399. // is currently running.
  400. // Dependency being imported async but we want the current resource right away. Wait until dependency is
  401. // done otherwise when dependency finishes it will overwrite whatever we write now.
  402. if(synchronous && dependency)
  403. {
  404. if (finishQueuedImport(fileEntry, *iterFind->second, true))
  405. mQueuedImports.erase(iterFind);
  406. }
  407. }
  408. // Needs to be pass a weak pointer to worker methods since internally it holds a reference to the task itself,
  409. // and we can't have the task closure holding a reference back, otherwise it leaks
  410. std::weak_ptr<QueuedImport> queuedImportWeak = queuedImport;
  411. if(!isNativeResource)
  412. {
  413. // Find UUIDs for any existing sub-resources
  414. if (fileEntry->meta != nullptr)
  415. {
  416. const Vector<SPtr<ProjectResourceMeta>>& resourceMetas = fileEntry->meta->getAllResourceMetaData();
  417. for (auto& entry : resourceMetas)
  418. queuedImport->resources.emplace_back(entry->getUniqueName(), nullptr, entry->getUUID());
  419. }
  420. // Perform import, register the resources and their UUID in the QueuedImport structure and save the
  421. // resource on disk
  422. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  423. {
  424. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  425. Vector<SubResourceRaw> importedResources = gImporter()._importAll(queuedImport->filePath,
  426. queuedImport->importOptions);
  427. if (!importedResources.empty())
  428. {
  429. Path outputPath = projectFolder;
  430. outputPath.append(INTERNAL_TEMP_DIR);
  431. if (!FileSystem::isDirectory(outputPath))
  432. FileSystem::createDir(outputPath);
  433. for (auto& entry : importedResources)
  434. {
  435. String subresourceName = entry.name;
  436. Path::stripInvalid(subresourceName);
  437. UUID uuid;
  438. {
  439. // Any access to queuedImport->resources must be locked
  440. Lock lock(mutex);
  441. auto iterFind = std::find_if(queuedImport->resources.begin(), queuedImport->resources.end(),
  442. [&subresourceName](const QueuedImportResource& importResource)
  443. {
  444. return importResource.name == subresourceName;
  445. });
  446. if (iterFind != queuedImport->resources.end())
  447. iterFind->resource = entry.value;
  448. else
  449. {
  450. queuedImport->resources.push_back(QueuedImportResource(entry.name, entry.value,
  451. UUID::EMPTY));
  452. iterFind = queuedImport->resources.end() - 1;
  453. }
  454. if (iterFind->uuid.empty())
  455. iterFind->uuid = UUIDGenerator::generateRandom();
  456. uuid = iterFind->uuid;
  457. }
  458. const String uuidStr = uuid.toString();
  459. outputPath.setFilename(uuidStr + ".asset");
  460. gResources()._save(entry.value, outputPath, true);
  461. }
  462. }
  463. };
  464. if(!synchronous)
  465. {
  466. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  467. dependency);
  468. }
  469. else
  470. importAsync();
  471. }
  472. else
  473. {
  474. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  475. // This can happen if library isn't properly saved before exiting the application.
  476. if (fileEntry->meta != nullptr)
  477. {
  478. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  479. mResourceManifest->registerResource(resourceMetas[0]->getUUID(), fileEntry->path);
  480. }
  481. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  482. {
  483. // Don't load dependencies because we don't need them, but also because they might not be in the
  484. // manifest which would screw up their UUIDs.
  485. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  486. HResource resource = gResources().load(queuedImport->filePath, ResourceLoadFlag::KeepSourceData);
  487. if (resource)
  488. {
  489. Path outputPath = projectFolder;
  490. outputPath.append(INTERNAL_TEMP_DIR);
  491. if (!FileSystem::isDirectory(outputPath))
  492. FileSystem::createDir(outputPath);
  493. {
  494. // Any access to queuedImport->resources must be locked
  495. Lock lock(mutex);
  496. queuedImport->resources.push_back(QueuedImportResource("primary", resource));
  497. }
  498. const String uuidStr = resource.getUUID().toString();
  499. outputPath.setFilename(uuidStr + ".asset");
  500. gResources()._save(resource.getInternalPtr(), outputPath, true);
  501. }
  502. };
  503. if(!synchronous)
  504. {
  505. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  506. dependency);
  507. }
  508. else
  509. importAsync();
  510. }
  511. if(!synchronous)
  512. {
  513. TaskScheduler::instance().addTask(queuedImport->importTask);
  514. mQueuedImports[fileEntry] = queuedImport;
  515. }
  516. if(synchronous)
  517. finishQueuedImport(fileEntry, *queuedImport, true);
  518. return true;
  519. }
  520. return false;
  521. }
  522. bool ProjectLibrary::finishQueuedImport(FileEntry* fileEntry, const QueuedImport& import, bool wait)
  523. {
  524. if (import.importTask != nullptr && !import.importTask->isComplete())
  525. {
  526. if (wait)
  527. import.importTask->wait();
  528. else
  529. return false;
  530. }
  531. // We wait on canceled task to finish and then just discard the results because any dependant tasks need to be
  532. // aware this tasks exists, so we can't just remove it straight away.
  533. if (import.canceled)
  534. return true;
  535. Path metaPath = fileEntry->path;
  536. metaPath.setFilename(metaPath.getFilename() + ".meta");
  537. Vector<SPtr<ProjectResourceMeta>> existingMetas;
  538. if (fileEntry->meta == nullptr) // Build a brand new meta-file
  539. fileEntry->meta = ProjectFileMeta::create(import.importOptions);
  540. else // Existing meta-file, which needs to be updated
  541. {
  542. // Remove existing dependencies (they will be re-added later)
  543. removeDependencies(fileEntry);
  544. existingMetas = fileEntry->meta->getAllResourceMetaData();
  545. fileEntry->meta->clearResourceMetaData();
  546. fileEntry->meta->mImportOptions = import.importOptions;
  547. }
  548. fileEntry->lastUpdateTime = import.timestamp;
  549. Path internalResourcesPath = mProjectFolder;
  550. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  551. if (!FileSystem::isDirectory(internalResourcesPath))
  552. FileSystem::createDir(internalResourcesPath);
  553. Path tempResourcesPath = mProjectFolder;
  554. tempResourcesPath.append(INTERNAL_TEMP_DIR);
  555. // See which sub-resource metas need to be updated, removed or added based on the new resource set
  556. bool isFirst = true;
  557. for (const auto& entry : import.resources)
  558. {
  559. // Entries with no resources are sub-resources that used to exist in this file, but haven't been imported
  560. // this time
  561. if (!entry.resource)
  562. continue;
  563. // Copy the resource file from the temporary directory
  564. const String uuidStr = entry.uuid.toString();
  565. tempResourcesPath.setFilename(uuidStr + ".asset");
  566. internalResourcesPath.setFilename(uuidStr + ".asset");
  567. FileSystem::move(tempResourcesPath, internalResourcesPath);
  568. String name = entry.name;
  569. Path::stripInvalid(name);
  570. const ProjectResourceIcons icons = generatePreviewIcons(*entry.resource);
  571. bool foundMeta = false;
  572. for (auto iterMeta = existingMetas.begin(); iterMeta != existingMetas.end(); ++iterMeta)
  573. {
  574. const SPtr<ProjectResourceMeta>& metaEntry = *iterMeta;
  575. if (name == metaEntry->getUniqueName())
  576. {
  577. // Make sure the UUID we used for saving the resource matches the current one (should always
  578. // be true unless the meta-data somehow changes while the async import is happening)
  579. assert(entry.uuid == metaEntry->getUUID());
  580. HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
  581. gResources().update(importedResource, entry.resource);
  582. metaEntry->setPreviewIcons(icons);
  583. fileEntry->meta->add(metaEntry);
  584. iterMeta = existingMetas.erase(iterMeta);
  585. foundMeta = true;
  586. break;
  587. }
  588. }
  589. if (!foundMeta)
  590. {
  591. HResource importedResource;
  592. // Native resources are always expected to have a handle since Resources::load was called during
  593. // the 'import' step
  594. if (import.native)
  595. importedResource = entry.handle;
  596. else
  597. importedResource = gResources()._createResourceHandle(entry.resource, entry.uuid);
  598. SPtr<ResourceMetaData> subMeta = entry.resource->getMetaData();
  599. const UINT32 typeId = entry.resource->getTypeId();
  600. const UUID& UUID = importedResource.getUUID();
  601. SPtr<ProjectResourceMeta> resMeta = ProjectResourceMeta::create(name, UUID, typeId,
  602. icons, subMeta);
  603. fileEntry->meta->add(resMeta);
  604. }
  605. // Keep resource metas that we are not currently using, in case they get restored so their references
  606. // don't get broken
  607. if (!import.pruneMetas)
  608. {
  609. for (auto& metaEntry : existingMetas)
  610. fileEntry->meta->addInactive(metaEntry);
  611. }
  612. // Update UUID to path mapping
  613. if (isFirst)
  614. mUUIDToPath[entry.uuid] = fileEntry->path;
  615. else
  616. mUUIDToPath[entry.uuid] = fileEntry->path + name;
  617. isFirst = false;
  618. // Register path in manifest
  619. mResourceManifest->registerResource(entry.uuid, internalResourcesPath);
  620. }
  621. // Save the meta file
  622. FileEncoder fs(metaPath);
  623. fs.encode(fileEntry->meta.get());
  624. // Register any dependencies this resource depends on
  625. addDependencies(fileEntry);
  626. // Notify the outside world import is doen
  627. onEntryImported(fileEntry->path);
  628. // Queue any resources dependant on this one for import
  629. reimportDependants(fileEntry->path);
  630. return true;
  631. }
  632. void ProjectLibrary::_finishQueuedImports(bool wait)
  633. {
  634. for(auto iter = mQueuedImports.begin(); iter != mQueuedImports.end();)
  635. {
  636. if(finishQueuedImport(iter->first, *iter->second, wait))
  637. iter = mQueuedImports.erase(iter);
  638. else
  639. ++iter;
  640. }
  641. }
  642. bool ProjectLibrary::isUpToDate(FileEntry* resource) const
  643. {
  644. SPtr<QueuedImport> queuedImport;
  645. if(resource->meta == nullptr)
  646. {
  647. // Allow no meta if import in progress
  648. const auto iterFind = mQueuedImports.find(resource);
  649. if(iterFind == mQueuedImports.end())
  650. return false;
  651. queuedImport = iterFind->second;
  652. }
  653. else
  654. {
  655. auto& resourceMetas = resource->meta->getResourceMetaData();
  656. for (auto& resMeta : resourceMetas)
  657. {
  658. Path internalPath;
  659. if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
  660. return false;
  661. if (!FileSystem::isFile(internalPath))
  662. return false;
  663. }
  664. }
  665. // Note: We're keeping separate update times for queued imports. This allows the import to be cancelled (either by
  666. // user or by app crashing), without updating the actual update time. This way the systems knows to try to reimport
  667. // the resource on the next check. At the same time we don't want our checkForModifications function to keep
  668. // trying to reimport a resource if it's already been queued for import.
  669. const std::time_t lastUpdateTime = queuedImport ? queuedImport->timestamp : resource->lastUpdateTime;
  670. const std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  671. return lastModifiedTime <= lastUpdateTime;
  672. }
  673. Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern)
  674. {
  675. return search(pattern, {});
  676. }
  677. Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern, const Vector<UINT32>& typeIds)
  678. {
  679. Vector<USPtr<LibraryEntry>> foundEntries;
  680. std::regex escape("[.^$|()\\[\\]{}*+?\\\\]");
  681. String replace("\\\\&");
  682. String escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  683. // For some reason MSVC stdlib implementation requires a different pattern than stdlib one
  684. #if BS_PLATFORM == BS_PLATFORM_WIN32
  685. std::regex wildcard("\\\\\\*");
  686. #else
  687. std::regex wildcard("\\\\\\\\\\*");
  688. #endif
  689. String wildcardReplace(".*");
  690. String searchPattern = std::regex_replace(escapedPattern, wildcard, ".*");
  691. std::regex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  692. Stack<DirectoryEntry*> todo;
  693. todo.push(mRootEntry.get());
  694. while (!todo.empty())
  695. {
  696. DirectoryEntry* dirEntry = todo.top();
  697. todo.pop();
  698. for (auto& child : dirEntry->mChildren)
  699. {
  700. if (std::regex_match(child->elementName, searchRegex))
  701. {
  702. if (typeIds.empty())
  703. foundEntries.push_back(child);
  704. else
  705. {
  706. if (child->type == LibraryEntryType::File)
  707. {
  708. FileEntry* childFileEntry = static_cast<FileEntry*>(child.get());
  709. if (childFileEntry->meta != nullptr)
  710. {
  711. auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
  712. for (auto& typeId : typeIds)
  713. {
  714. bool found = false;
  715. for (auto& resMeta : resourceMetas)
  716. {
  717. if (resMeta->getTypeID() == typeId)
  718. {
  719. foundEntries.push_back(child);
  720. found = true;
  721. break;
  722. }
  723. }
  724. if (found)
  725. break;
  726. }
  727. }
  728. }
  729. }
  730. }
  731. if (child->type == LibraryEntryType::Directory)
  732. {
  733. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child.get());
  734. todo.push(childDirEntry);
  735. }
  736. }
  737. }
  738. std::sort(foundEntries.begin(), foundEntries.end(),
  739. [&](const USPtr<LibraryEntry>& a, const USPtr<LibraryEntry>& b)
  740. {
  741. return a->elementName.compare(b->elementName) < 0;
  742. });
  743. return foundEntries;
  744. }
  745. USPtr<ProjectLibrary::LibraryEntry> ProjectLibrary::findEntry(const Path& path) const
  746. {
  747. Path relPath;
  748. const Path* searchPath;
  749. if (path.isAbsolute())
  750. {
  751. if (!mResourcesFolder.includes(path))
  752. return nullptr;
  753. relPath = path.getRelative(mRootEntry->path);
  754. searchPath = &relPath;
  755. }
  756. else
  757. searchPath = &path;
  758. BS_ASSERT(mRootEntry->path == mResourcesFolder);
  759. UINT32 numElems = searchPath->getNumDirectories() + (searchPath->isFile() ? 1 : 0);
  760. UINT32 idx = 0;
  761. USPtr<LibraryEntry> rootLibEntry = mRootEntry;
  762. USPtr<LibraryEntry>* current = &rootLibEntry;
  763. while (current != nullptr)
  764. {
  765. if (idx == numElems)
  766. return *current;
  767. const String& curElem =
  768. (searchPath->isFile() && idx == (numElems - 1)) ? searchPath->getFilename() : (*searchPath)[idx];
  769. if ((*current)->type == LibraryEntryType::Directory)
  770. {
  771. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current->get());
  772. size_t curElemHash = bs_hash(UTF8::toLower(curElem));
  773. current = nullptr;
  774. for (auto& child : dirEntry->mChildren)
  775. {
  776. if(curElemHash != child->elementNameHash)
  777. continue;
  778. if (Path::comparePathElem(curElem, child->elementName))
  779. {
  780. idx++;
  781. current = &child;
  782. break;
  783. }
  784. }
  785. }
  786. else // Found file
  787. {
  788. // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
  789. if (idx == (numElems - 1))
  790. return *current;
  791. else
  792. break; // Not a valid path
  793. }
  794. }
  795. return nullptr;
  796. }
  797. bool ProjectLibrary::isSubresource(const Path& path) const
  798. {
  799. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  800. if (numElems <= 1)
  801. return false;
  802. Path filePath = path;
  803. filePath.makeParent();
  804. LibraryEntry* entry = findEntry(filePath).get();
  805. return entry != nullptr && entry->type == LibraryEntryType::File;
  806. }
  807. SPtr<ProjectResourceMeta> ProjectLibrary::findResourceMeta(const Path& path) const
  808. {
  809. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  810. // Check if it is a subresource path
  811. if(numElems > 1)
  812. {
  813. Path filePath = path;
  814. filePath.makeParent();
  815. LibraryEntry* entry = findEntry(filePath).get();
  816. if (entry == nullptr)
  817. return nullptr;
  818. // Entry is a subresource
  819. if (entry->type == LibraryEntryType::File)
  820. {
  821. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  822. if (fileEntry->meta == nullptr)
  823. return nullptr;
  824. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  825. for(auto& resMeta : resourceMetas)
  826. {
  827. if (resMeta->getUniqueName() == path.getTail())
  828. return resMeta;
  829. }
  830. // Found the file but no subresource or meta information
  831. return nullptr;
  832. }
  833. else // Entry not a subresource
  834. {
  835. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  836. for (auto& child : dirEntry->mChildren)
  837. {
  838. if (Path::comparePathElem(path.getTail(), child->elementName))
  839. {
  840. if (child->type == LibraryEntryType::File)
  841. {
  842. FileEntry* fileEntry = static_cast<FileEntry*>(child.get());
  843. if (fileEntry->meta == nullptr)
  844. return nullptr;
  845. return fileEntry->meta->getResourceMetaData()[0];
  846. }
  847. }
  848. }
  849. return nullptr;
  850. }
  851. }
  852. // Not a subresource path, load directly
  853. {
  854. LibraryEntry* entry = findEntry(path).get();
  855. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  856. return nullptr;
  857. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  858. if (fileEntry->meta == nullptr)
  859. return nullptr;
  860. return fileEntry->meta->getResourceMetaData()[0];
  861. }
  862. }
  863. Path ProjectLibrary::uuidToPath(const UUID& uuid) const
  864. {
  865. auto iterFind = mUUIDToPath.find(uuid);
  866. if (iterFind != mUUIDToPath.end())
  867. return iterFind->second;
  868. return Path::BLANK;
  869. }
  870. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  871. {
  872. if (resource == nullptr)
  873. return;
  874. Path assetPath = path;
  875. if (path.isAbsolute())
  876. {
  877. if (!getResourcesFolder().includes(path))
  878. return;
  879. assetPath = path.getRelative(getResourcesFolder());
  880. }
  881. deleteEntry(assetPath);
  882. resource->setName(path.getFilename(false));
  883. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  884. Resources::instance().save(resource, absPath, false);
  885. Path parentDirPath = absPath.getParent();
  886. USPtr<LibraryEntry> parentEntry = findEntry(parentDirPath);
  887. // Register parent hierarchy if not found
  888. DirectoryEntry* entryParent = nullptr;
  889. if (parentEntry == nullptr)
  890. createInternalParentHierarchy(absPath, nullptr, &entryParent);
  891. else
  892. entryParent = static_cast<DirectoryEntry*>(parentEntry.get());
  893. addResourceInternal(entryParent, absPath, nullptr, true, true);
  894. }
  895. void ProjectLibrary::saveEntry(const HResource& resource)
  896. {
  897. if (resource == nullptr)
  898. return;
  899. Path filePath = uuidToPath(resource.getUUID());
  900. if(filePath.isEmpty())
  901. {
  902. LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
  903. return;
  904. }
  905. filePath.makeAbsolute(getResourcesFolder());
  906. Resources::instance().save(resource, filePath, true);
  907. LibraryEntry* fileEntry = findEntry(filePath).get();
  908. if(fileEntry)
  909. reimportResourceInternal(static_cast<FileEntry*>(fileEntry), nullptr, true, false, true);
  910. }
  911. void ProjectLibrary::createFolderEntry(const Path& path)
  912. {
  913. Path fullPath = path;
  914. if (fullPath.isAbsolute())
  915. {
  916. if (!mResourcesFolder.includes(fullPath))
  917. return;
  918. }
  919. else
  920. fullPath.makeAbsolute(mResourcesFolder);
  921. if (FileSystem::isDirectory(fullPath))
  922. return; // Already exists
  923. FileSystem::createDir(fullPath);
  924. Path parentPath = fullPath.getParent();
  925. DirectoryEntry* newEntryParent = nullptr;
  926. USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
  927. if (newEntryParentLib != nullptr)
  928. {
  929. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  930. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
  931. }
  932. DirectoryEntry* newHierarchyParent = nullptr;
  933. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  934. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  935. addDirectoryInternal(newEntryParent, fullPath);
  936. }
  937. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  938. {
  939. Path oldFullPath = oldPath;
  940. if (!oldFullPath.isAbsolute())
  941. oldFullPath.makeAbsolute(mResourcesFolder);
  942. Path newFullPath = newPath;
  943. if (!newFullPath.isAbsolute())
  944. newFullPath.makeAbsolute(mResourcesFolder);
  945. Path parentPath = newFullPath.getParent();
  946. if (!FileSystem::isDirectory(parentPath))
  947. {
  948. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  949. return;
  950. }
  951. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  952. FileSystem::move(oldFullPath, newFullPath, overwrite);
  953. Path oldMetaPath = getMetaPath(oldFullPath);
  954. Path newMetaPath = getMetaPath(newFullPath);
  955. USPtr<LibraryEntry> oldEntry = findEntry(oldFullPath);
  956. if(oldEntry != nullptr) // Moving from the Resources folder
  957. {
  958. // Moved outside of Resources, delete entry & meta file
  959. if (!mResourcesFolder.includes(newFullPath))
  960. {
  961. if(oldEntry->type == LibraryEntryType::File)
  962. deleteResourceInternal(static_pointer_cast<FileEntry>(oldEntry));
  963. else if(oldEntry->type == LibraryEntryType::Directory)
  964. deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(oldEntry));
  965. }
  966. else // Just moving internally
  967. {
  968. onEntryRemoved(oldEntry->path);
  969. USPtr<FileEntry> fileEntry = nullptr;
  970. if (oldEntry->type == LibraryEntryType::File)
  971. {
  972. fileEntry = static_pointer_cast<FileEntry>(oldEntry);
  973. removeDependencies(fileEntry.get());
  974. // Update uuid <-> path mapping
  975. if(fileEntry->meta != nullptr)
  976. {
  977. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  978. if (!resourceMetas.empty())
  979. {
  980. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  981. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  982. {
  983. SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
  984. const UUID& UUID = resMeta->getUUID();
  985. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  986. }
  987. }
  988. }
  989. }
  990. if(FileSystem::isFile(oldMetaPath))
  991. FileSystem::move(oldMetaPath, newMetaPath);
  992. DirectoryEntry* parent = oldEntry->parent;
  993. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  994. if(findIter != parent->mChildren.end())
  995. parent->mChildren.erase(findIter);
  996. Path parentPath = newFullPath.getParent();
  997. DirectoryEntry* newEntryParent = nullptr;
  998. USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
  999. if(newEntryParentLib != nullptr)
  1000. {
  1001. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1002. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
  1003. }
  1004. DirectoryEntry* newHierarchyParent = nullptr;
  1005. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  1006. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  1007. newEntryParent->mChildren.push_back(oldEntry);
  1008. oldEntry->parent = newEntryParent;
  1009. oldEntry->path = newFullPath;
  1010. oldEntry->elementName = newFullPath.getTail();
  1011. oldEntry->elementNameHash = bs_hash(UTF8::toLower(oldEntry->elementName));
  1012. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  1013. {
  1014. Stack<LibraryEntry*> todo;
  1015. todo.push(oldEntry.get());
  1016. while(!todo.empty())
  1017. {
  1018. LibraryEntry* curEntry = todo.top();
  1019. todo.pop();
  1020. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  1021. for(auto& child : curDirEntry->mChildren)
  1022. {
  1023. child->path = child->parent->path;
  1024. child->path.append(child->elementName);
  1025. if(child->type == LibraryEntryType::Directory)
  1026. todo.push(child.get());
  1027. }
  1028. }
  1029. }
  1030. onEntryAdded(oldEntry->path);
  1031. if (fileEntry != nullptr)
  1032. {
  1033. reimportDependants(oldFullPath);
  1034. reimportDependants(newFullPath);
  1035. }
  1036. }
  1037. }
  1038. else // Moving from outside of the Resources folder (likely adding a new resource)
  1039. {
  1040. checkForModifications(newFullPath);
  1041. }
  1042. }
  1043. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  1044. {
  1045. Path oldFullPath = oldPath;
  1046. if (!oldFullPath.isAbsolute())
  1047. oldFullPath.makeAbsolute(mResourcesFolder);
  1048. Path newFullPath = newPath;
  1049. if (!newFullPath.isAbsolute())
  1050. newFullPath.makeAbsolute(mResourcesFolder);
  1051. if (!FileSystem::exists(oldFullPath))
  1052. return;
  1053. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  1054. // Copying a file/folder outside of the Resources folder, no special handling needed
  1055. if (!mResourcesFolder.includes(newFullPath))
  1056. return;
  1057. Path parentPath = newFullPath.getParent();
  1058. DirectoryEntry* newEntryParent = nullptr;
  1059. LibraryEntry* newEntryParentLib = findEntry(parentPath).get();
  1060. if (newEntryParentLib != nullptr)
  1061. {
  1062. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1063. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1064. }
  1065. // If the source is outside of Resources folder, just plain import the copy
  1066. LibraryEntry* oldEntry = findEntry(oldFullPath).get();
  1067. if (oldEntry == nullptr)
  1068. {
  1069. checkForModifications(newFullPath);
  1070. return;
  1071. }
  1072. // Both source and destination are within Resources folder, need to preserve import options on the copies
  1073. if (FileSystem::isFile(newFullPath))
  1074. {
  1075. assert(oldEntry->type == LibraryEntryType::File);
  1076. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  1077. SPtr<ImportOptions> importOptions;
  1078. if (oldResEntry->meta != nullptr)
  1079. importOptions = oldResEntry->meta->getImportOptions();
  1080. addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  1081. }
  1082. else
  1083. {
  1084. assert(oldEntry->type == LibraryEntryType::File);
  1085. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  1086. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath).get();
  1087. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  1088. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  1089. while (!todo.empty())
  1090. {
  1091. auto current = todo.top();
  1092. todo.pop();
  1093. DirectoryEntry* sourceDir = std::get<0>(current);
  1094. DirectoryEntry* destDir = std::get<1>(current);
  1095. for (auto& child : sourceDir->mChildren)
  1096. {
  1097. Path childDestPath = destDir->path;
  1098. childDestPath.append(child->path.getTail());
  1099. if (child->type == LibraryEntryType::File)
  1100. {
  1101. FileEntry* childResEntry = static_cast<FileEntry*>(child.get());
  1102. SPtr<ImportOptions> importOptions;
  1103. if (childResEntry->meta != nullptr)
  1104. importOptions = childResEntry->meta->getImportOptions();
  1105. addResourceInternal(destDir, childDestPath, importOptions, true);
  1106. }
  1107. else // Directory
  1108. {
  1109. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child.get());
  1110. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath).get();
  1111. todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. void ProjectLibrary::deleteEntry(const Path& path)
  1118. {
  1119. Path fullPath = path;
  1120. if (!fullPath.isAbsolute())
  1121. fullPath.makeAbsolute(mResourcesFolder);
  1122. if(FileSystem::exists(fullPath))
  1123. FileSystem::remove(fullPath);
  1124. USPtr<LibraryEntry> entry = findEntry(fullPath);
  1125. if(entry != nullptr)
  1126. {
  1127. if(entry->type == LibraryEntryType::File)
  1128. deleteResourceInternal(static_pointer_cast<FileEntry>(entry));
  1129. else if(entry->type == LibraryEntryType::Directory)
  1130. deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(entry));
  1131. }
  1132. }
  1133. void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport,
  1134. bool synchronous)
  1135. {
  1136. LibraryEntry* entry = findEntry(path).get();
  1137. if (entry != nullptr)
  1138. {
  1139. if (entry->type == LibraryEntryType::File)
  1140. {
  1141. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1142. reimportResourceInternal(resEntry, importOptions, forceReimport, synchronous);
  1143. }
  1144. }
  1145. }
  1146. float ProjectLibrary::getImportProgress(const Path& path) const
  1147. {
  1148. LibraryEntry* entry = findEntry(path).get();
  1149. if (entry == nullptr)
  1150. return 0.0f;
  1151. if(entry->type == LibraryEntryType::Directory)
  1152. return 1.0f;
  1153. // Note: Only supporting binary progress reporting for now
  1154. const auto iterFind = mQueuedImports.find(static_cast<FileEntry*>(entry));
  1155. return iterFind != mQueuedImports.end() ? 0.0f : 1.0f;
  1156. }
  1157. void ProjectLibrary::cancelImport()
  1158. {
  1159. for(auto& entry : mQueuedImports)
  1160. entry.second->canceled = true;
  1161. }
  1162. void ProjectLibrary::waitForQueuedImport(FileEntry* fileEntry)
  1163. {
  1164. const auto iterFind = mQueuedImports.find(fileEntry);
  1165. if (iterFind != mQueuedImports.end())
  1166. {
  1167. if (finishQueuedImport(fileEntry, *iterFind->second, true))
  1168. mQueuedImports.erase(iterFind);
  1169. }
  1170. }
  1171. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  1172. {
  1173. LibraryEntry* entry = findEntry(path).get();
  1174. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1175. return;
  1176. auto fileEntry = static_cast<FileEntry*>(entry);
  1177. // Any queued imports will overwrite the meta file, so make sure they finish first
  1178. waitForQueuedImport(fileEntry);
  1179. if (fileEntry->meta == nullptr)
  1180. return;
  1181. fileEntry->meta->setIncludeInBuild(include);
  1182. Path metaPath = fileEntry->path;
  1183. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1184. FileEncoder fs(metaPath);
  1185. fs.encode(fileEntry->meta.get());
  1186. }
  1187. void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
  1188. {
  1189. LibraryEntry* entry = findEntry(path).get();
  1190. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1191. return;
  1192. auto fileEntry = static_cast<FileEntry*>(entry);
  1193. // Any queued imports will overwrite the meta file, so make sure they finish first
  1194. waitForQueuedImport(fileEntry);
  1195. SPtr<ProjectResourceMeta> resMeta = findResourceMeta(path);
  1196. if (resMeta == nullptr)
  1197. return;
  1198. resMeta->mUserData = userData;
  1199. Path metaPath = fileEntry->path;
  1200. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1201. FileEncoder fs(metaPath);
  1202. fs.encode(fileEntry->meta.get());
  1203. }
  1204. Vector<USPtr<ProjectLibrary::FileEntry>> ProjectLibrary::getResourcesForBuild() const
  1205. {
  1206. Vector<USPtr<FileEntry>> output;
  1207. Stack<DirectoryEntry*> todo;
  1208. todo.push(mRootEntry.get());
  1209. while (!todo.empty())
  1210. {
  1211. DirectoryEntry* directory = todo.top();
  1212. todo.pop();
  1213. for (auto& child : directory->mChildren)
  1214. {
  1215. if (child->type == LibraryEntryType::File)
  1216. {
  1217. FileEntry* resEntry = static_cast<FileEntry*>(child.get());
  1218. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  1219. output.push_back(static_pointer_cast<FileEntry>(child));
  1220. }
  1221. else if (child->type == LibraryEntryType::Directory)
  1222. {
  1223. todo.push(static_cast<DirectoryEntry*>(child.get()));
  1224. }
  1225. }
  1226. }
  1227. return output;
  1228. }
  1229. HResource ProjectLibrary::load(const Path& path)
  1230. {
  1231. SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
  1232. if (meta == nullptr)
  1233. return HResource();
  1234. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  1235. const UUID& resUUID = meta->getUUID();
  1236. return gResources().loadFromUUID(resUUID, false, loadFlags);
  1237. }
  1238. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot,
  1239. DirectoryEntry** newHierarchyLeaf)
  1240. {
  1241. Path parentPath = fullPath;
  1242. DirectoryEntry* newEntryParent = nullptr;
  1243. Stack<Path> parentPaths;
  1244. do
  1245. {
  1246. Path newParentPath = parentPath.getParent();
  1247. if(newParentPath == parentPath)
  1248. break;
  1249. LibraryEntry* newEntryParentLib = findEntry(newParentPath).get();
  1250. if(newEntryParentLib != nullptr)
  1251. {
  1252. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1253. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1254. break;
  1255. }
  1256. parentPaths.push(newParentPath);
  1257. parentPath = newParentPath;
  1258. } while (true);
  1259. assert(newEntryParent != nullptr); // Must exist
  1260. if(newHierarchyRoot != nullptr)
  1261. *newHierarchyRoot = newEntryParent;
  1262. while(!parentPaths.empty())
  1263. {
  1264. Path curPath = parentPaths.top();
  1265. parentPaths.pop();
  1266. newEntryParent = addDirectoryInternal(newEntryParent, curPath).get();
  1267. }
  1268. if(newHierarchyLeaf != nullptr)
  1269. *newHierarchyLeaf = newEntryParent;
  1270. }
  1271. Path ProjectLibrary::getMetaPath(const Path& path) const
  1272. {
  1273. Path metaPath = path;
  1274. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1275. return metaPath;
  1276. }
  1277. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1278. {
  1279. return fullPath.getExtension() == ".meta";
  1280. }
  1281. bool ProjectLibrary::isNative(const Path& path) const
  1282. {
  1283. String extension = path.getExtension();
  1284. return extension == ".asset" || extension == ".prefab";
  1285. }
  1286. void ProjectLibrary::unloadLibrary()
  1287. {
  1288. if (!mIsLoaded)
  1289. return;
  1290. _finishQueuedImports(true);
  1291. mProjectFolder = Path::BLANK;
  1292. mResourcesFolder = Path::BLANK;
  1293. clearEntries();
  1294. mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1295. mDependencies.clear();
  1296. gResources().unregisterResourceManifest(mResourceManifest);
  1297. mResourceManifest = nullptr;
  1298. mIsLoaded = false;
  1299. }
  1300. void ProjectLibrary::makeEntriesRelative()
  1301. {
  1302. // Make all paths relative before saving
  1303. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1304. [&](LibraryEntry* entry, const Path& root)
  1305. {
  1306. entry->path.makeRelative(root);
  1307. if (entry->type == LibraryEntryType::Directory)
  1308. {
  1309. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1310. for (auto& child : dirEntry->mChildren)
  1311. makeRelative(child.get(), root);
  1312. }
  1313. };
  1314. Path root = getResourcesFolder();
  1315. makeRelative(mRootEntry.get(), root);
  1316. }
  1317. void ProjectLibrary::makeEntriesAbsolute()
  1318. {
  1319. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1320. [&](LibraryEntry* entry, const Path& root)
  1321. {
  1322. entry->path.makeAbsolute(root);
  1323. if (entry->type == LibraryEntryType::Directory)
  1324. {
  1325. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1326. for (auto& child : dirEntry->mChildren)
  1327. makeAbsolute(child.get(), root);
  1328. }
  1329. };
  1330. Path root = getResourcesFolder();
  1331. makeAbsolute(mRootEntry.get(), root);
  1332. }
  1333. void ProjectLibrary::saveLibrary()
  1334. {
  1335. if (!mIsLoaded)
  1336. return;
  1337. // Make all paths relative before saving
  1338. makeEntriesRelative();
  1339. SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(mRootEntry);
  1340. Path libraryEntriesPath = mProjectFolder;
  1341. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1342. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1343. FileEncoder fs(libraryEntriesPath);
  1344. fs.encode(libEntries.get());
  1345. // Restore absolute entry paths
  1346. makeEntriesAbsolute();
  1347. Path resourceManifestPath = mProjectFolder;
  1348. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1349. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1350. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1351. }
  1352. void ProjectLibrary::loadLibrary()
  1353. {
  1354. unloadLibrary();
  1355. mProjectFolder = gEditorApplication().getProjectPath();
  1356. mResourcesFolder = mProjectFolder;
  1357. mResourcesFolder.append(RESOURCES_DIR);
  1358. mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1359. Path libraryEntriesPath = mProjectFolder;
  1360. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1361. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1362. if(FileSystem::exists(libraryEntriesPath))
  1363. {
  1364. FileDecoder fs(libraryEntriesPath);
  1365. SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1366. mRootEntry = libEntries->getRootEntry();
  1367. mRootEntry->parent = nullptr;
  1368. }
  1369. // Entries are stored relative to project folder, but we want their absolute paths now
  1370. makeEntriesAbsolute();
  1371. // Load resource manifest
  1372. Path resourceManifestPath = mProjectFolder;
  1373. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1374. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1375. if (FileSystem::exists(resourceManifestPath))
  1376. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1377. else
  1378. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1379. gResources().registerResourceManifest(mResourceManifest);
  1380. // Load all meta files
  1381. Stack<DirectoryEntry*> todo;
  1382. todo.push(mRootEntry.get());
  1383. Vector<USPtr<LibraryEntry>> deletedEntries;
  1384. while(!todo.empty())
  1385. {
  1386. DirectoryEntry* curDir = todo.top();
  1387. todo.pop();
  1388. for(auto& child : curDir->mChildren)
  1389. {
  1390. if(child->type == LibraryEntryType::File)
  1391. {
  1392. USPtr<FileEntry> resEntry = static_pointer_cast<FileEntry>(child);
  1393. if (FileSystem::isFile(resEntry->path))
  1394. {
  1395. if (resEntry->meta == nullptr)
  1396. {
  1397. Path metaPath = resEntry->path;
  1398. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1399. if (FileSystem::isFile(metaPath))
  1400. {
  1401. FileDecoder fs(metaPath);
  1402. SPtr<IReflectable> loadedMeta = fs.decode();
  1403. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1404. {
  1405. SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1406. resEntry->meta = fileMeta;
  1407. }
  1408. }
  1409. }
  1410. if (resEntry->meta != nullptr)
  1411. {
  1412. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1413. if (!resourceMetas.empty())
  1414. {
  1415. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1416. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1417. {
  1418. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  1419. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1420. }
  1421. }
  1422. }
  1423. addDependencies(resEntry.get());
  1424. }
  1425. else
  1426. deletedEntries.push_back(resEntry);
  1427. }
  1428. else if(child->type == LibraryEntryType::Directory)
  1429. {
  1430. if (FileSystem::isDirectory(child->path))
  1431. todo.push(static_cast<DirectoryEntry*>(child.get()));
  1432. else
  1433. deletedEntries.push_back(child);
  1434. }
  1435. }
  1436. }
  1437. // Remove entries that no longer have corresponding files
  1438. for (auto& deletedEntry : deletedEntries)
  1439. {
  1440. if (deletedEntry->type == LibraryEntryType::File)
  1441. deleteResourceInternal(static_pointer_cast<FileEntry>(deletedEntry));
  1442. else
  1443. deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(deletedEntry));
  1444. }
  1445. // Clean up internal library folder from obsolete files
  1446. Path internalResourcesFolder = mProjectFolder;
  1447. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1448. if (FileSystem::exists(internalResourcesFolder))
  1449. {
  1450. Vector<Path> toDelete;
  1451. auto processFile = [&](const Path& file)
  1452. {
  1453. UUID uuid = UUID(file.getFilename(false));
  1454. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1455. {
  1456. mResourceManifest->unregisterResource(uuid);
  1457. toDelete.push_back(file);
  1458. }
  1459. return true;
  1460. };
  1461. FileSystem::iterate(internalResourcesFolder, processFile);
  1462. for (auto& entry : toDelete)
  1463. FileSystem::remove(entry);
  1464. }
  1465. mIsLoaded = true;
  1466. }
  1467. void ProjectLibrary::clearEntries()
  1468. {
  1469. if (mRootEntry == nullptr)
  1470. return;
  1471. std::function<void(LibraryEntry*)> invalidateRecursive =
  1472. [&](LibraryEntry* entry)
  1473. {
  1474. if (entry->type == LibraryEntryType::Directory)
  1475. {
  1476. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1477. for (auto& child : dirEntry->mChildren)
  1478. invalidateRecursive(child.get());
  1479. *dirEntry = DirectoryEntry();
  1480. }
  1481. else
  1482. {
  1483. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  1484. *fileEntry = FileEntry();
  1485. }
  1486. };
  1487. assert(mQueuedImports.empty());
  1488. invalidateRecursive(mRootEntry.get());
  1489. mRootEntry = nullptr;
  1490. }
  1491. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1492. {
  1493. Vector<Path> output;
  1494. if (entry->meta == nullptr)
  1495. return output;
  1496. auto& resourceMetas = entry->meta->getResourceMetaData();
  1497. for(auto& resMeta : resourceMetas)
  1498. {
  1499. if (resMeta->getTypeID() == TID_Shader)
  1500. {
  1501. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1502. for (auto& include : metaData->includes)
  1503. output.push_back(include);
  1504. }
  1505. }
  1506. return output;
  1507. }
  1508. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1509. {
  1510. Vector<Path> dependencies = getImportDependencies(entry);
  1511. for (auto& dependency : dependencies)
  1512. mDependencies[dependency].push_back(entry->path);
  1513. }
  1514. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1515. {
  1516. Vector<Path> dependencies = getImportDependencies(entry);
  1517. for (auto& dependency : dependencies)
  1518. {
  1519. Vector<Path>& curDependencies = mDependencies[dependency];
  1520. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1521. [&](const Path& x)
  1522. {
  1523. return x == entry->path;
  1524. });
  1525. curDependencies.erase(iterRemove, curDependencies.end());
  1526. }
  1527. }
  1528. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1529. {
  1530. auto iterFind = mDependencies.find(entryPath);
  1531. if (iterFind == mDependencies.end())
  1532. return;
  1533. // Make a copy since we might modify this list during reimport
  1534. Vector<Path> dependencies = iterFind->second;
  1535. for (auto& dependency : dependencies)
  1536. {
  1537. LibraryEntry* entry = findEntry(dependency).get();
  1538. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1539. {
  1540. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1541. SPtr<ImportOptions> importOptions;
  1542. if (resEntry->meta != nullptr)
  1543. importOptions = resEntry->meta->getImportOptions();
  1544. reimportResourceInternal(resEntry, importOptions, true);
  1545. }
  1546. }
  1547. }
  1548. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1549. {
  1550. return ProjectLibrary::instance();
  1551. }
  1552. }