2
0

BsProjectLibrary.cpp 56 KB

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