2
0

BsProjectLibrary.cpp 45 KB

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