BsProjectLibrary.cpp 51 KB

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