BsProjectLibrary.cpp 45 KB

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