2
0

BsProjectLibrary.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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 bs
  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->getAllResourceMetaData();
  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->addInactive(entry);
  425. }
  426. // Update UUID to path mapping
  427. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  428. if (resourceMetas.size() > 0)
  429. {
  430. mUUIDToPath[resourceMetas[0]->getUUID()] = fileEntry->path;
  431. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  432. {
  433. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  434. mUUIDToPath[entry->getUUID()] = fileEntry->path + entry->getUniqueName();
  435. }
  436. }
  437. }
  438. fileEntry->meta->mImportOptions = curImportOptions;
  439. FileEncoder fs(metaPath);
  440. fs.encode(fileEntry->meta.get());
  441. }
  442. addDependencies(fileEntry);
  443. if (importedResources.size() > 0)
  444. {
  445. Path internalResourcesPath = mProjectFolder;
  446. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  447. if (!FileSystem::isDirectory(internalResourcesPath))
  448. FileSystem::createDir(internalResourcesPath);
  449. for (auto& entry : importedResources)
  450. {
  451. internalResourcesPath.setFilename(toWString(entry.value.getUUID()) + L".asset");
  452. gResources().save(entry.value, internalResourcesPath, true);
  453. String uuid = entry.value.getUUID();
  454. mResourceManifest->registerResource(uuid, internalResourcesPath);
  455. }
  456. }
  457. fileEntry->lastUpdateTime = std::time(nullptr);
  458. onEntryImported(fileEntry->path);
  459. reimportDependants(fileEntry->path);
  460. }
  461. }
  462. bool ProjectLibrary::isUpToDate(FileEntry* resource) const
  463. {
  464. if(resource->meta == nullptr)
  465. return false;
  466. auto& resourceMetas = resource->meta->getResourceMetaData();
  467. for (auto& resMeta : resourceMetas)
  468. {
  469. Path internalPath;
  470. if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
  471. return false;
  472. if (!FileSystem::isFile(internalPath))
  473. return false;
  474. }
  475. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  476. return lastModifiedTime <= resource->lastUpdateTime;
  477. }
  478. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  479. {
  480. return search(pattern, {});
  481. }
  482. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  483. {
  484. Vector<LibraryEntry*> foundEntries;
  485. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  486. WString replace(L"\\\\&");
  487. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  488. std::wregex wildcard(L"\\\\\\*");
  489. WString wildcardReplace(L".*");
  490. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  491. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  492. Stack<DirectoryEntry*> todo;
  493. todo.push(mRootEntry);
  494. while (!todo.empty())
  495. {
  496. DirectoryEntry* dirEntry = todo.top();
  497. todo.pop();
  498. for (auto& child : dirEntry->mChildren)
  499. {
  500. if (std::regex_match(child->elementName, searchRegex))
  501. {
  502. if (typeIds.size() == 0)
  503. foundEntries.push_back(child);
  504. else
  505. {
  506. if (child->type == LibraryEntryType::File)
  507. {
  508. FileEntry* childFileEntry = static_cast<FileEntry*>(child);
  509. if (childFileEntry->meta != nullptr)
  510. {
  511. auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
  512. for (auto& typeId : typeIds)
  513. {
  514. bool found = false;
  515. for (auto& resMeta : resourceMetas)
  516. {
  517. if (resMeta->getTypeID() == typeId)
  518. {
  519. foundEntries.push_back(child);
  520. found = true;
  521. break;
  522. }
  523. }
  524. if (found)
  525. break;
  526. }
  527. }
  528. }
  529. }
  530. }
  531. if (child->type == LibraryEntryType::Directory)
  532. {
  533. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  534. todo.push(childDirEntry);
  535. }
  536. }
  537. }
  538. std::sort(foundEntries.begin(), foundEntries.end(),
  539. [&](const LibraryEntry* a, const LibraryEntry* b)
  540. {
  541. return a->elementName.compare(b->elementName) < 0;
  542. });
  543. return foundEntries;
  544. }
  545. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  546. {
  547. Path fullPath = path;
  548. if (fullPath.isAbsolute())
  549. {
  550. if (!mResourcesFolder.includes(fullPath))
  551. return nullptr;
  552. }
  553. else
  554. fullPath.makeAbsolute(mResourcesFolder);
  555. Path relPath = fullPath.getRelative(mRootEntry->path);
  556. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  557. UINT32 idx = 0;
  558. LibraryEntry* current = mRootEntry;
  559. while (current != nullptr)
  560. {
  561. if (idx == numElems)
  562. return current;
  563. WString curElem;
  564. if (relPath.isFile() && idx == (numElems - 1))
  565. curElem = relPath.getWFilename();
  566. else
  567. curElem = relPath[idx];
  568. if (current->type == LibraryEntryType::Directory)
  569. {
  570. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  571. current = nullptr;
  572. for (auto& child : dirEntry->mChildren)
  573. {
  574. if (Path::comparePathElem(curElem, child->elementName))
  575. {
  576. idx++;
  577. current = child;
  578. break;
  579. }
  580. }
  581. }
  582. else // Found file
  583. {
  584. // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
  585. if (idx == (numElems - 1))
  586. return current;
  587. else
  588. break; // Not a valid path
  589. }
  590. }
  591. return nullptr;
  592. }
  593. bool ProjectLibrary::isSubresource(const Path& path) const
  594. {
  595. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  596. if (numElems <= 1)
  597. return false;
  598. Path filePath = path;
  599. filePath.makeParent();
  600. LibraryEntry* entry = findEntry(filePath);
  601. if (entry != nullptr && entry->type == LibraryEntryType::File)
  602. return true;
  603. return false;
  604. }
  605. SPtr<ProjectResourceMeta> ProjectLibrary::findResourceMeta(const Path& path) const
  606. {
  607. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  608. // Check if it is a subresource path
  609. if(numElems > 1)
  610. {
  611. Path filePath = path;
  612. filePath.makeParent();
  613. LibraryEntry* entry = findEntry(filePath);
  614. if (entry == nullptr)
  615. return nullptr;
  616. // Entry is a subresource
  617. if (entry->type == LibraryEntryType::File)
  618. {
  619. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  620. if (fileEntry->meta == nullptr)
  621. return nullptr;
  622. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  623. for(auto& resMeta : resourceMetas)
  624. {
  625. if (resMeta->getUniqueName() == path.getWTail())
  626. return resMeta;
  627. }
  628. // Found the file but no subresource or meta information
  629. return nullptr;
  630. }
  631. else // Entry not a subresource
  632. {
  633. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  634. for (auto& child : dirEntry->mChildren)
  635. {
  636. if (Path::comparePathElem(path.getWTail(), child->elementName))
  637. {
  638. if (child->type == LibraryEntryType::File)
  639. {
  640. FileEntry* fileEntry = static_cast<FileEntry*>(child);
  641. if (fileEntry->meta == nullptr)
  642. return nullptr;
  643. return fileEntry->meta->getResourceMetaData()[0];
  644. }
  645. }
  646. }
  647. return nullptr;
  648. }
  649. }
  650. // Not a subresource path, load directly
  651. {
  652. LibraryEntry* entry = findEntry(path);
  653. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  654. return nullptr;
  655. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  656. if (fileEntry->meta == nullptr)
  657. return nullptr;
  658. return fileEntry->meta->getResourceMetaData()[0];
  659. }
  660. }
  661. Path ProjectLibrary::uuidToPath(const String& uuid) const
  662. {
  663. auto iterFind = mUUIDToPath.find(uuid);
  664. if (iterFind != mUUIDToPath.end())
  665. return iterFind->second;
  666. return Path::BLANK;
  667. }
  668. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  669. {
  670. if (resource == nullptr)
  671. return;
  672. Path assetPath = path;
  673. if (path.isAbsolute())
  674. {
  675. if (!getResourcesFolder().includes(path))
  676. return;
  677. assetPath = path.getRelative(getResourcesFolder());
  678. }
  679. deleteEntry(assetPath);
  680. resource->setName(path.getWFilename(false));
  681. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  682. Resources::instance().save(resource, absPath, false);
  683. checkForModifications(absPath);
  684. }
  685. void ProjectLibrary::saveEntry(const HResource& resource)
  686. {
  687. if (resource == nullptr)
  688. return;
  689. Path filePath = uuidToPath(resource.getUUID());
  690. if(filePath.isEmpty())
  691. {
  692. LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
  693. return;
  694. }
  695. filePath.makeAbsolute(getResourcesFolder());
  696. Resources::instance().save(resource, filePath, true);
  697. checkForModifications(filePath);
  698. }
  699. void ProjectLibrary::createFolderEntry(const Path& path)
  700. {
  701. Path fullPath = path;
  702. if (fullPath.isAbsolute())
  703. {
  704. if (!mResourcesFolder.includes(fullPath))
  705. return;
  706. }
  707. else
  708. fullPath.makeAbsolute(mResourcesFolder);
  709. if (FileSystem::isDirectory(fullPath))
  710. return; // Already exists
  711. FileSystem::createDir(fullPath);
  712. Path parentPath = fullPath.getParent();
  713. DirectoryEntry* newEntryParent = nullptr;
  714. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  715. if (newEntryParentLib != nullptr)
  716. {
  717. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  718. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  719. }
  720. DirectoryEntry* newHierarchyParent = nullptr;
  721. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  722. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  723. addDirectoryInternal(newEntryParent, fullPath);
  724. }
  725. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  726. {
  727. Path oldFullPath = oldPath;
  728. if (!oldFullPath.isAbsolute())
  729. oldFullPath.makeAbsolute(mResourcesFolder);
  730. Path newFullPath = newPath;
  731. if (!newFullPath.isAbsolute())
  732. newFullPath.makeAbsolute(mResourcesFolder);
  733. Path parentPath = newFullPath.getParent();
  734. if (!FileSystem::isDirectory(parentPath))
  735. {
  736. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  737. return;
  738. }
  739. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  740. FileSystem::move(oldFullPath, newFullPath, overwrite);
  741. Path oldMetaPath = getMetaPath(oldFullPath);
  742. Path newMetaPath = getMetaPath(newFullPath);
  743. LibraryEntry* oldEntry = findEntry(oldFullPath);
  744. if(oldEntry != nullptr) // Moving from the Resources folder
  745. {
  746. // Moved outside of Resources, delete entry & meta file
  747. if (!mResourcesFolder.includes(newFullPath))
  748. {
  749. if(oldEntry->type == LibraryEntryType::File)
  750. deleteResourceInternal(static_cast<FileEntry*>(oldEntry));
  751. else if(oldEntry->type == LibraryEntryType::Directory)
  752. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  753. }
  754. else // Just moving internally
  755. {
  756. onEntryRemoved(oldEntry->path);
  757. FileEntry* fileEntry = nullptr;
  758. if (oldEntry->type == LibraryEntryType::File)
  759. {
  760. fileEntry = static_cast<FileEntry*>(oldEntry);
  761. removeDependencies(fileEntry);
  762. // Update uuid <-> path mapping
  763. if(fileEntry->meta != nullptr)
  764. {
  765. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  766. if (resourceMetas.size() > 0)
  767. {
  768. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  769. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  770. {
  771. SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
  772. const String& UUID = resMeta->getUUID();
  773. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  774. }
  775. }
  776. }
  777. }
  778. if(FileSystem::isFile(oldMetaPath))
  779. FileSystem::move(oldMetaPath, newMetaPath);
  780. DirectoryEntry* parent = oldEntry->parent;
  781. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  782. if(findIter != parent->mChildren.end())
  783. parent->mChildren.erase(findIter);
  784. Path parentPath = newFullPath.getParent();
  785. DirectoryEntry* newEntryParent = nullptr;
  786. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  787. if(newEntryParentLib != nullptr)
  788. {
  789. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  790. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  791. }
  792. DirectoryEntry* newHierarchyParent = nullptr;
  793. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  794. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  795. newEntryParent->mChildren.push_back(oldEntry);
  796. oldEntry->parent = newEntryParent;
  797. oldEntry->path = newFullPath;
  798. oldEntry->elementName = newFullPath.getWTail();
  799. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  800. {
  801. Stack<LibraryEntry*> todo;
  802. todo.push(oldEntry);
  803. while(!todo.empty())
  804. {
  805. LibraryEntry* curEntry = todo.top();
  806. todo.pop();
  807. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  808. for(auto& child : curDirEntry->mChildren)
  809. {
  810. child->path = child->parent->path;
  811. child->path.append(child->elementName);
  812. if(child->type == LibraryEntryType::Directory)
  813. todo.push(child);
  814. }
  815. }
  816. }
  817. onEntryAdded(oldEntry->path);
  818. if (fileEntry != nullptr)
  819. {
  820. reimportDependants(oldFullPath);
  821. reimportDependants(newFullPath);
  822. }
  823. }
  824. }
  825. else // Moving from outside of the Resources folder (likely adding a new resource)
  826. {
  827. checkForModifications(newFullPath);
  828. }
  829. }
  830. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  831. {
  832. Path oldFullPath = oldPath;
  833. if (!oldFullPath.isAbsolute())
  834. oldFullPath.makeAbsolute(mResourcesFolder);
  835. Path newFullPath = newPath;
  836. if (!newFullPath.isAbsolute())
  837. newFullPath.makeAbsolute(mResourcesFolder);
  838. if (!FileSystem::exists(oldFullPath))
  839. return;
  840. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  841. // Copying a file/folder outside of the Resources folder, no special handling needed
  842. if (!mResourcesFolder.includes(newFullPath))
  843. return;
  844. Path parentPath = newFullPath.getParent();
  845. DirectoryEntry* newEntryParent = nullptr;
  846. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  847. if (newEntryParentLib != nullptr)
  848. {
  849. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  850. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  851. }
  852. // If the source is outside of Resources folder, just plain import the copy
  853. LibraryEntry* oldEntry = findEntry(oldFullPath);
  854. if (oldEntry == nullptr)
  855. {
  856. checkForModifications(newFullPath);
  857. return;
  858. }
  859. // Both source and destination are within Resources folder, need to preserve import options on the copies
  860. LibraryEntry* newEntry = nullptr;
  861. if (FileSystem::isFile(newFullPath))
  862. {
  863. assert(oldEntry->type == LibraryEntryType::File);
  864. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  865. SPtr<ImportOptions> importOptions;
  866. if (oldResEntry->meta != nullptr)
  867. importOptions = oldResEntry->meta->getImportOptions();
  868. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  869. }
  870. else
  871. {
  872. assert(oldEntry->type == LibraryEntryType::File);
  873. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  874. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  875. newEntry = newDirEntry;
  876. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  877. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  878. while (!todo.empty())
  879. {
  880. auto current = todo.top();
  881. todo.pop();
  882. DirectoryEntry* sourceDir = std::get<0>(current);
  883. DirectoryEntry* destDir = std::get<1>(current);
  884. for (auto& child : sourceDir->mChildren)
  885. {
  886. Path childDestPath = destDir->path;
  887. childDestPath.append(child->path.getWTail());
  888. if (child->type == LibraryEntryType::File)
  889. {
  890. FileEntry* childResEntry = static_cast<FileEntry*>(child);
  891. SPtr<ImportOptions> importOptions;
  892. if (childResEntry->meta != nullptr)
  893. importOptions = childResEntry->meta->getImportOptions();
  894. addResourceInternal(destDir, childDestPath, importOptions, true);
  895. }
  896. else // Directory
  897. {
  898. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  899. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  900. todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
  901. }
  902. }
  903. }
  904. }
  905. }
  906. void ProjectLibrary::deleteEntry(const Path& path)
  907. {
  908. Path fullPath = path;
  909. if (!fullPath.isAbsolute())
  910. fullPath.makeAbsolute(mResourcesFolder);
  911. if(FileSystem::exists(fullPath))
  912. FileSystem::remove(fullPath);
  913. LibraryEntry* entry = findEntry(fullPath);
  914. if(entry != nullptr)
  915. {
  916. if(entry->type == LibraryEntryType::File)
  917. deleteResourceInternal(static_cast<FileEntry*>(entry));
  918. else if(entry->type == LibraryEntryType::Directory)
  919. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  920. }
  921. }
  922. void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport)
  923. {
  924. LibraryEntry* entry = findEntry(path);
  925. if (entry != nullptr)
  926. {
  927. if (entry->type == LibraryEntryType::File)
  928. {
  929. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  930. reimportResourceInternal(resEntry, importOptions, forceReimport);
  931. }
  932. }
  933. }
  934. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  935. {
  936. LibraryEntry* entry = findEntry(path);
  937. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  938. return;
  939. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  940. if (resEntry->meta == nullptr)
  941. return;
  942. resEntry->meta->setIncludeInBuild(include);
  943. Path metaPath = resEntry->path;
  944. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  945. FileEncoder fs(metaPath);
  946. fs.encode(resEntry->meta.get());
  947. }
  948. void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
  949. {
  950. LibraryEntry* entry = findEntry(path);
  951. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  952. return;
  953. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  954. SPtr<ProjectResourceMeta> resMeta = findResourceMeta(path);
  955. if (resMeta == nullptr)
  956. return;
  957. resMeta->mUserData = userData;
  958. Path metaPath = fileEntry->path;
  959. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  960. FileEncoder fs(metaPath);
  961. fs.encode(fileEntry->meta.get());
  962. }
  963. Vector<ProjectLibrary::FileEntry*> ProjectLibrary::getResourcesForBuild() const
  964. {
  965. Vector<FileEntry*> output;
  966. Stack<DirectoryEntry*> todo;
  967. todo.push(mRootEntry);
  968. while (!todo.empty())
  969. {
  970. DirectoryEntry* directory = todo.top();
  971. todo.pop();
  972. for (auto& child : directory->mChildren)
  973. {
  974. if (child->type == LibraryEntryType::File)
  975. {
  976. FileEntry* resEntry = static_cast<FileEntry*>(child);
  977. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  978. output.push_back(resEntry);
  979. }
  980. else if (child->type == LibraryEntryType::Directory)
  981. {
  982. todo.push(static_cast<DirectoryEntry*>(child));
  983. }
  984. }
  985. }
  986. return output;
  987. }
  988. HResource ProjectLibrary::load(const Path& path)
  989. {
  990. SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
  991. if (meta == nullptr)
  992. return HResource();
  993. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  994. String resUUID = meta->getUUID();
  995. return gResources().loadFromUUID(resUUID, false, loadFlags);
  996. }
  997. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  998. {
  999. Path parentPath = fullPath;
  1000. DirectoryEntry* newEntryParent = nullptr;
  1001. Stack<Path> parentPaths;
  1002. do
  1003. {
  1004. Path newParentPath = parentPath.getParent();
  1005. if(newParentPath == parentPath)
  1006. break;
  1007. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  1008. if(newEntryParentLib != nullptr)
  1009. {
  1010. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1011. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1012. break;
  1013. }
  1014. parentPaths.push(newParentPath);
  1015. parentPath = newParentPath;
  1016. } while (true);
  1017. assert(newEntryParent != nullptr); // Must exist
  1018. if(newHierarchyRoot != nullptr)
  1019. *newHierarchyRoot = newEntryParent;
  1020. while(!parentPaths.empty())
  1021. {
  1022. Path curPath = parentPaths.top();
  1023. parentPaths.pop();
  1024. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  1025. }
  1026. if(newHierarchyLeaf != nullptr)
  1027. *newHierarchyLeaf = newEntryParent;
  1028. }
  1029. Path ProjectLibrary::getMetaPath(const Path& path) const
  1030. {
  1031. Path metaPath = path;
  1032. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1033. return metaPath;
  1034. }
  1035. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1036. {
  1037. return fullPath.getWExtension() == L".meta";
  1038. }
  1039. bool ProjectLibrary::isNative(const Path& path) const
  1040. {
  1041. WString extension = path.getWExtension();
  1042. return extension == L".asset" || extension == L".prefab";
  1043. }
  1044. void ProjectLibrary::unloadLibrary()
  1045. {
  1046. if (!mIsLoaded)
  1047. return;
  1048. mProjectFolder = Path::BLANK;
  1049. mResourcesFolder = Path::BLANK;
  1050. clearEntries();
  1051. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1052. mDependencies.clear();
  1053. gResources().unregisterResourceManifest(mResourceManifest);
  1054. mResourceManifest = nullptr;
  1055. mIsLoaded = false;
  1056. }
  1057. void ProjectLibrary::makeEntriesRelative()
  1058. {
  1059. // Make all paths relative before saving
  1060. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1061. [&](LibraryEntry* entry, const Path& root)
  1062. {
  1063. entry->path.makeRelative(root);
  1064. if (entry->type == LibraryEntryType::Directory)
  1065. {
  1066. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1067. for (auto& child : dirEntry->mChildren)
  1068. makeRelative(child, root);
  1069. }
  1070. };
  1071. Path root = getResourcesFolder();
  1072. makeRelative(mRootEntry, root);
  1073. }
  1074. void ProjectLibrary::makeEntriesAbsolute()
  1075. {
  1076. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1077. [&](LibraryEntry* entry, const Path& root)
  1078. {
  1079. entry->path.makeAbsolute(root);
  1080. if (entry->type == LibraryEntryType::Directory)
  1081. {
  1082. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1083. for (auto& child : dirEntry->mChildren)
  1084. makeAbsolute(child, root);
  1085. }
  1086. };
  1087. Path root = getResourcesFolder();
  1088. makeAbsolute(mRootEntry, root);
  1089. }
  1090. void ProjectLibrary::saveLibrary()
  1091. {
  1092. if (!mIsLoaded)
  1093. return;
  1094. // Make all paths relative before saving
  1095. makeEntriesRelative();
  1096. SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  1097. Path libraryEntriesPath = mProjectFolder;
  1098. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1099. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1100. FileEncoder fs(libraryEntriesPath);
  1101. fs.encode(libEntries.get());
  1102. // Restore absolute entry paths
  1103. makeEntriesAbsolute();
  1104. Path resourceManifestPath = mProjectFolder;
  1105. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1106. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1107. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1108. }
  1109. void ProjectLibrary::loadLibrary()
  1110. {
  1111. unloadLibrary();
  1112. mProjectFolder = gEditorApplication().getProjectPath();
  1113. mResourcesFolder = mProjectFolder;
  1114. mResourcesFolder.append(RESOURCES_DIR);
  1115. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1116. Path libraryEntriesPath = mProjectFolder;
  1117. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1118. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1119. if(FileSystem::exists(libraryEntriesPath))
  1120. {
  1121. FileDecoder fs(libraryEntriesPath);
  1122. SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1123. *mRootEntry = libEntries->getRootEntry();
  1124. for(auto& child : mRootEntry->mChildren)
  1125. child->parent = mRootEntry;
  1126. mRootEntry->parent = nullptr;
  1127. }
  1128. // Entries are stored relative to project folder, but we want their absolute paths now
  1129. makeEntriesAbsolute();
  1130. // Load resource manifest
  1131. Path resourceManifestPath = mProjectFolder;
  1132. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1133. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1134. if (FileSystem::exists(resourceManifestPath))
  1135. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1136. else
  1137. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1138. gResources().registerResourceManifest(mResourceManifest);
  1139. // Load all meta files
  1140. Stack<DirectoryEntry*> todo;
  1141. todo.push(mRootEntry);
  1142. Vector<LibraryEntry*> deletedEntries;
  1143. while(!todo.empty())
  1144. {
  1145. DirectoryEntry* curDir = todo.top();
  1146. todo.pop();
  1147. for(auto& child : curDir->mChildren)
  1148. {
  1149. if(child->type == LibraryEntryType::File)
  1150. {
  1151. FileEntry* resEntry = static_cast<FileEntry*>(child);
  1152. if (FileSystem::isFile(resEntry->path))
  1153. {
  1154. if (resEntry->meta == nullptr)
  1155. {
  1156. Path metaPath = resEntry->path;
  1157. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1158. if (FileSystem::isFile(metaPath))
  1159. {
  1160. FileDecoder fs(metaPath);
  1161. SPtr<IReflectable> loadedMeta = fs.decode();
  1162. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1163. {
  1164. SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1165. resEntry->meta = fileMeta;
  1166. }
  1167. }
  1168. }
  1169. if (resEntry->meta != nullptr)
  1170. {
  1171. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1172. if (resourceMetas.size() > 0)
  1173. {
  1174. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1175. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1176. {
  1177. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  1178. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1179. }
  1180. }
  1181. }
  1182. addDependencies(resEntry);
  1183. }
  1184. else
  1185. deletedEntries.push_back(resEntry);
  1186. }
  1187. else if(child->type == LibraryEntryType::Directory)
  1188. {
  1189. if (FileSystem::isDirectory(child->path))
  1190. todo.push(static_cast<DirectoryEntry*>(child));
  1191. else
  1192. deletedEntries.push_back(child);
  1193. }
  1194. }
  1195. }
  1196. // Remove entries that no longer have corresponding files
  1197. for (auto& deletedEntry : deletedEntries)
  1198. {
  1199. if (deletedEntry->type == LibraryEntryType::File)
  1200. {
  1201. FileEntry* resEntry = static_cast<FileEntry*>(deletedEntry);
  1202. deleteResourceInternal(resEntry);
  1203. }
  1204. else
  1205. {
  1206. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1207. deleteDirectoryInternal(dirEntry);
  1208. }
  1209. }
  1210. // Clean up internal library folder from obsolete files
  1211. Path internalResourcesFolder = mProjectFolder;
  1212. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1213. if (FileSystem::exists(internalResourcesFolder))
  1214. {
  1215. Vector<Path> toDelete;
  1216. auto processFile = [&](const Path& file)
  1217. {
  1218. String uuid = file.getFilename(false);
  1219. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1220. {
  1221. mResourceManifest->unregisterResource(uuid);
  1222. toDelete.push_back(file);
  1223. }
  1224. return true;
  1225. };
  1226. FileSystem::iterate(internalResourcesFolder, processFile);
  1227. for (auto& entry : toDelete)
  1228. FileSystem::remove(entry);
  1229. }
  1230. mIsLoaded = true;
  1231. }
  1232. void ProjectLibrary::clearEntries()
  1233. {
  1234. if (mRootEntry == nullptr)
  1235. return;
  1236. std::function<void(LibraryEntry*)> deleteRecursive =
  1237. [&](LibraryEntry* entry)
  1238. {
  1239. if (entry->type == LibraryEntryType::Directory)
  1240. {
  1241. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1242. for (auto& child : dirEntry->mChildren)
  1243. deleteRecursive(child);
  1244. }
  1245. bs_delete(entry);
  1246. };
  1247. deleteRecursive(mRootEntry);
  1248. mRootEntry = nullptr;
  1249. }
  1250. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1251. {
  1252. Vector<Path> output;
  1253. if (entry->meta == nullptr)
  1254. return output;
  1255. auto& resourceMetas = entry->meta->getResourceMetaData();
  1256. for(auto& resMeta : resourceMetas)
  1257. {
  1258. if (resMeta->getTypeID() == TID_Shader)
  1259. {
  1260. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1261. for (auto& include : metaData->includes)
  1262. output.push_back(include);
  1263. }
  1264. }
  1265. return output;
  1266. }
  1267. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1268. {
  1269. Vector<Path> dependencies = getImportDependencies(entry);
  1270. for (auto& dependency : dependencies)
  1271. mDependencies[dependency].push_back(entry->path);
  1272. }
  1273. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1274. {
  1275. Vector<Path> dependencies = getImportDependencies(entry);
  1276. for (auto& dependency : dependencies)
  1277. {
  1278. Vector<Path>& curDependencies = mDependencies[dependency];
  1279. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1280. [&](const Path& x)
  1281. {
  1282. return x == entry->path;
  1283. });
  1284. curDependencies.erase(iterRemove, curDependencies.end());
  1285. }
  1286. }
  1287. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1288. {
  1289. auto iterFind = mDependencies.find(entryPath);
  1290. if (iterFind == mDependencies.end())
  1291. return;
  1292. // Make a copy since we might modify this list during reimport
  1293. Vector<Path> dependencies = iterFind->second;
  1294. for (auto& dependency : dependencies)
  1295. {
  1296. LibraryEntry* entry = findEntry(dependency);
  1297. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1298. {
  1299. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1300. SPtr<ImportOptions> importOptions;
  1301. if (resEntry->meta != nullptr)
  1302. importOptions = resEntry->meta->getImportOptions();
  1303. reimportResourceInternal(resEntry, importOptions, true);
  1304. }
  1305. }
  1306. }
  1307. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1308. {
  1309. return ProjectLibrary::instance();
  1310. }
  1311. }