BsProjectLibrary.cpp 45 KB

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