BsProjectLibrary.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. #include "BsProjectLibrary.h"
  2. #include "BsFileSystem.h"
  3. #include "BsException.h"
  4. #include "BsResources.h"
  5. #include "BsResourceManifest.h"
  6. #include "BsImporter.h"
  7. #include "BsProjectResourceMeta.h"
  8. #include "BsResources.h"
  9. #include "BsImporter.h"
  10. #include "BsImportOptions.h"
  11. #include "BsFileSerializer.h"
  12. #include "BsDebug.h"
  13. #include "BsProjectLibraryEntries.h"
  14. #include "BsResource.h"
  15. #include "BsEditorApplication.h"
  16. #include "BsResourceImporter.h"
  17. #include "BsShader.h"
  18. #include <regex>
  19. using namespace std::placeholders;
  20. namespace BansheeEngine
  21. {
  22. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  23. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + L"Resources\\";
  24. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  25. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  26. ProjectLibrary::LibraryEntry::LibraryEntry()
  27. :parent(nullptr), type(LibraryEntryType::Directory)
  28. { }
  29. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  30. :path(path), parent(parent), type(type), elementName(name)
  31. { }
  32. ProjectLibrary::ResourceEntry::ResourceEntry()
  33. : lastUpdateTime(0)
  34. { }
  35. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  36. : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  37. { }
  38. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  39. { }
  40. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  41. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  42. { }
  43. ProjectLibrary::ProjectLibrary()
  44. : mRootEntry(nullptr), mIsLoaded(false)
  45. {
  46. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  47. }
  48. ProjectLibrary::~ProjectLibrary()
  49. {
  50. clearEntries();
  51. }
  52. void ProjectLibrary::update()
  53. {
  54. while (!mReimportQueue.empty())
  55. {
  56. Path toReimport = *mReimportQueue.begin();
  57. LibraryEntry* entry = findEntry(toReimport);
  58. if (entry != nullptr && entry->type == LibraryEntryType::File)
  59. {
  60. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  61. reimportResourceInternal(resEntry, resEntry->meta->getImportOptions(), true);
  62. queueDependantForReimport(resEntry);
  63. }
  64. mReimportQueue.erase(mReimportQueue.begin());
  65. }
  66. }
  67. void ProjectLibrary::checkForModifications(const Path& fullPath)
  68. {
  69. Vector<Path> dirtyResources;
  70. checkForModifications(fullPath, true, dirtyResources);
  71. }
  72. void ProjectLibrary::checkForModifications(const Path& fullPath, bool import, Vector<Path>& dirtyResources)
  73. {
  74. if (!mResourcesFolder.includes(fullPath))
  75. return; // Folder not part of our resources path, so no modifications
  76. if(mRootEntry == nullptr)
  77. {
  78. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  79. }
  80. Path pathToSearch = fullPath;
  81. LibraryEntry* entry = findEntry(pathToSearch);
  82. if (entry == nullptr) // File could be new, try to find parent directory entry
  83. {
  84. if (FileSystem::exists(pathToSearch))
  85. {
  86. if (isMeta(pathToSearch))
  87. {
  88. Path sourceFilePath = pathToSearch;
  89. sourceFilePath.setExtension(L"");
  90. if (!FileSystem::isFile(sourceFilePath))
  91. {
  92. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  93. FileSystem::remove(pathToSearch);
  94. }
  95. }
  96. else
  97. {
  98. Path parentDirPath = pathToSearch.getParent();
  99. entry = findEntry(parentDirPath);
  100. // Cannot find parent directory. Create the needed hierarchy.
  101. DirectoryEntry* entryParent = nullptr;
  102. DirectoryEntry* newHierarchyParent = nullptr;
  103. if (entry == nullptr)
  104. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  105. else
  106. entryParent = static_cast<DirectoryEntry*>(entry);
  107. if (FileSystem::isFile(pathToSearch))
  108. {
  109. if (import)
  110. addResourceInternal(entryParent, pathToSearch);
  111. dirtyResources.push_back(pathToSearch);
  112. }
  113. else if (FileSystem::isDirectory(pathToSearch))
  114. {
  115. addDirectoryInternal(entryParent, pathToSearch);
  116. checkForModifications(pathToSearch);
  117. }
  118. }
  119. }
  120. }
  121. else if(entry->type == LibraryEntryType::File)
  122. {
  123. if(FileSystem::isFile(entry->path))
  124. {
  125. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  126. if (import)
  127. {
  128. reimportResourceInternal(resEntry);
  129. queueDependantForReimport(resEntry);
  130. }
  131. if (!isUpToDate(resEntry))
  132. dirtyResources.push_back(entry->path);
  133. }
  134. else
  135. {
  136. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  137. }
  138. }
  139. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  140. {
  141. if(!FileSystem::isDirectory(entry->path))
  142. {
  143. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  144. }
  145. else
  146. {
  147. Stack<DirectoryEntry*> todo;
  148. todo.push(static_cast<DirectoryEntry*>(entry));
  149. Vector<Path> childFiles;
  150. Vector<Path> childDirectories;
  151. Vector<bool> existingEntries;
  152. Vector<LibraryEntry*> toDelete;
  153. while(!todo.empty())
  154. {
  155. DirectoryEntry* currentDir = todo.top();
  156. todo.pop();
  157. existingEntries.clear();
  158. existingEntries.resize(currentDir->mChildren.size());
  159. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  160. existingEntries[i] = false;
  161. childFiles.clear();
  162. childDirectories.clear();
  163. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  164. for(auto& filePath : childFiles)
  165. {
  166. if(isMeta(filePath))
  167. {
  168. Path sourceFilePath = filePath;
  169. sourceFilePath.setExtension(L"");
  170. if(!FileSystem::isFile(sourceFilePath))
  171. {
  172. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  173. FileSystem::remove(filePath);
  174. }
  175. }
  176. else
  177. {
  178. ResourceEntry* existingEntry = nullptr;
  179. UINT32 idx = 0;
  180. for(auto& child : currentDir->mChildren)
  181. {
  182. if(child->type == LibraryEntryType::File && child->path == filePath)
  183. {
  184. existingEntries[idx] = true;
  185. existingEntry = static_cast<ResourceEntry*>(child);
  186. break;
  187. }
  188. idx++;
  189. }
  190. if(existingEntry != nullptr)
  191. {
  192. if (import)
  193. {
  194. reimportResourceInternal(existingEntry);
  195. queueDependantForReimport(existingEntry);
  196. }
  197. if (!isUpToDate(existingEntry))
  198. dirtyResources.push_back(existingEntry->path);
  199. }
  200. else
  201. {
  202. if (import)
  203. addResourceInternal(currentDir, filePath);
  204. dirtyResources.push_back(filePath);
  205. }
  206. }
  207. }
  208. for(auto& dirPath : childDirectories)
  209. {
  210. DirectoryEntry* existingEntry = nullptr;
  211. UINT32 idx = 0;
  212. for(auto& child : currentDir->mChildren)
  213. {
  214. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  215. {
  216. existingEntries[idx] = true;
  217. existingEntry = static_cast<DirectoryEntry*>(child);
  218. break;
  219. }
  220. idx++;
  221. }
  222. if(existingEntry == nullptr)
  223. addDirectoryInternal(currentDir, dirPath);
  224. }
  225. {
  226. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  227. {
  228. if(existingEntries[i])
  229. continue;
  230. toDelete.push_back(currentDir->mChildren[i]);
  231. }
  232. for(auto& child : toDelete)
  233. {
  234. if(child->type == LibraryEntryType::Directory)
  235. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  236. else if(child->type == LibraryEntryType::File)
  237. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  238. }
  239. toDelete.clear();
  240. }
  241. for(auto& child : currentDir->mChildren)
  242. {
  243. if(child->type == LibraryEntryType::Directory)
  244. todo.push(static_cast<DirectoryEntry*>(child));
  245. }
  246. }
  247. }
  248. }
  249. }
  250. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  251. const ImportOptionsPtr& importOptions, bool forceReimport)
  252. {
  253. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  254. parent->mChildren.push_back(newResource);
  255. reimportResourceInternal(newResource, importOptions, forceReimport);
  256. doOnEntryAdded(newResource);
  257. return newResource;
  258. }
  259. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  260. {
  261. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  262. parent->mChildren.push_back(newEntry);
  263. doOnEntryAdded(newEntry);
  264. return newEntry;
  265. }
  266. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  267. {
  268. if(resource->meta != nullptr)
  269. {
  270. String uuid = resource->meta->getUUID();
  271. Path path;
  272. if (mResourceManifest->uuidToFilePath(uuid, path))
  273. {
  274. if(FileSystem::isFile(path))
  275. FileSystem::remove(path);
  276. mResourceManifest->unregisterResource(uuid);
  277. }
  278. mUUIDToPath.erase(uuid);
  279. }
  280. DirectoryEntry* parent = resource->parent;
  281. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  282. [&] (const LibraryEntry* entry) { return entry == resource; });
  283. parent->mChildren.erase(findIter);
  284. doOnEntryRemoved(resource);
  285. bs_delete(resource);
  286. }
  287. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  288. {
  289. if(directory == mRootEntry)
  290. mRootEntry = nullptr;
  291. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  292. for(auto& child : childrenToDestroy)
  293. {
  294. if(child->type == LibraryEntryType::Directory)
  295. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  296. else
  297. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  298. }
  299. DirectoryEntry* parent = directory->parent;
  300. if(parent != nullptr)
  301. {
  302. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  303. [&] (const LibraryEntry* entry) { return entry == directory; });
  304. parent->mChildren.erase(findIter);
  305. }
  306. doOnEntryRemoved(directory);
  307. bs_delete(directory);
  308. }
  309. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  310. {
  311. Path metaPath = resource->path;
  312. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  313. if(resource->meta == nullptr)
  314. {
  315. if(FileSystem::isFile(metaPath))
  316. {
  317. FileDecoder fs(metaPath);
  318. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  319. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  320. {
  321. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  322. resource->meta = resourceMeta;
  323. mUUIDToPath[resourceMeta->getUUID()] = resource->path;
  324. }
  325. }
  326. }
  327. if (!isUpToDate(resource) || forceReimport)
  328. {
  329. ImportOptionsPtr curImportOptions = nullptr;
  330. if (importOptions == nullptr)
  331. {
  332. if (resource->meta != nullptr)
  333. curImportOptions = resource->meta->getImportOptions();
  334. else
  335. curImportOptions = Importer::instance().createImportOptions(resource->path);
  336. }
  337. else
  338. curImportOptions = importOptions;
  339. HResource importedResource;
  340. if(resource->meta == nullptr)
  341. {
  342. importedResource = Importer::instance().import(resource->path, curImportOptions);
  343. if (importedResource != nullptr)
  344. {
  345. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  346. UINT32 typeId = importedResource->getTypeId();
  347. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  348. }
  349. else
  350. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), 0, nullptr, curImportOptions);
  351. FileEncoder fs(metaPath);
  352. fs.encode(resource->meta.get());
  353. mUUIDToPath[resource->meta->getUUID()] = resource->path;
  354. }
  355. else
  356. {
  357. removeDependencies(resource);
  358. importedResource = HResource(resource->meta->getUUID());
  359. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  360. }
  361. addDependencies(resource);
  362. if (importedResource != nullptr)
  363. {
  364. Path internalResourcesPath = mProjectFolder;
  365. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  366. if (!FileSystem::isDirectory(internalResourcesPath))
  367. FileSystem::createDir(internalResourcesPath);
  368. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  369. gResources().save(importedResource, internalResourcesPath, true);
  370. gResources().unload(importedResource);
  371. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  372. }
  373. resource->lastUpdateTime = std::time(nullptr);
  374. }
  375. }
  376. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  377. {
  378. if(resource->meta == nullptr)
  379. return false;
  380. Path path;
  381. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  382. return false;
  383. if(!FileSystem::isFile(path))
  384. return false;
  385. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  386. return lastModifiedTime <= resource->lastUpdateTime;
  387. }
  388. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  389. {
  390. return search(pattern, {});
  391. }
  392. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  393. {
  394. Vector<LibraryEntry*> foundEntries;
  395. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  396. WString replace(L"\\\\&");
  397. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  398. std::wregex wildcard(L"\\\\\\*");
  399. WString wildcardReplace(L".*");
  400. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  401. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  402. Stack<DirectoryEntry*> todo;
  403. todo.push(mRootEntry);
  404. while (!todo.empty())
  405. {
  406. DirectoryEntry* dirEntry = todo.top();
  407. todo.pop();
  408. for (auto& child : dirEntry->mChildren)
  409. {
  410. if (std::regex_match(child->elementName, searchRegex))
  411. {
  412. if (typeIds.size() == 0)
  413. foundEntries.push_back(child);
  414. else
  415. {
  416. if (child->type == LibraryEntryType::File)
  417. {
  418. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  419. for (auto& typeId : typeIds)
  420. {
  421. if (childResEntry->meta->getTypeID() == typeId)
  422. {
  423. foundEntries.push_back(child);
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. if (child->type == LibraryEntryType::Directory)
  431. {
  432. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  433. todo.push(childDirEntry);
  434. }
  435. }
  436. }
  437. std::sort(foundEntries.begin(), foundEntries.end(),
  438. [&](const LibraryEntry* a, const LibraryEntry* b)
  439. {
  440. return a->elementName.compare(b->elementName) < 0;
  441. });
  442. return foundEntries;
  443. }
  444. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  445. {
  446. Path fullPath = path;
  447. if (fullPath.isAbsolute())
  448. {
  449. if (!mResourcesFolder.includes(fullPath))
  450. return nullptr;
  451. }
  452. else
  453. fullPath.makeAbsolute(mResourcesFolder);
  454. Path relPath = fullPath.getRelative(mRootEntry->path);
  455. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  456. UINT32 idx = 0;
  457. LibraryEntry* current = mRootEntry;
  458. while (current != nullptr)
  459. {
  460. if (idx == numElems)
  461. return current;
  462. WString curElem;
  463. if (relPath.isFile() && idx == (numElems - 1))
  464. curElem = relPath.getWFilename();
  465. else
  466. curElem = relPath[idx];
  467. if (current->type == LibraryEntryType::Directory)
  468. {
  469. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  470. current = nullptr;
  471. for (auto& child : dirEntry->mChildren)
  472. {
  473. if (Path::comparePathElem(curElem, child->elementName))
  474. {
  475. idx++;
  476. current = child;
  477. break;
  478. }
  479. }
  480. }
  481. else
  482. break;
  483. }
  484. return nullptr;
  485. }
  486. Path ProjectLibrary::uuidToPath(const String& uuid) const
  487. {
  488. auto iterFind = mUUIDToPath.find(uuid);
  489. if (iterFind != mUUIDToPath.end())
  490. return iterFind->second;
  491. return Path::BLANK;
  492. }
  493. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  494. {
  495. if (resource == nullptr)
  496. return;
  497. Path assetPath = path;
  498. if (path.isAbsolute())
  499. {
  500. if (!getResourcesFolder().includes(path))
  501. return;
  502. assetPath = path.getRelative(getResourcesFolder());
  503. }
  504. LibraryEntry* existingEntry = findEntry(assetPath);
  505. if (existingEntry != nullptr)
  506. {
  507. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  508. return;
  509. }
  510. resource->setName(path.getWFilename(false));
  511. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  512. Resources::instance().save(resource, absPath, false);
  513. checkForModifications(absPath);
  514. }
  515. void ProjectLibrary::saveEntry(const HResource& resource)
  516. {
  517. if (resource == nullptr)
  518. return;
  519. Path filePath;
  520. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  521. return;
  522. Resources::instance().save(resource, filePath, true);
  523. }
  524. void ProjectLibrary::createFolderEntry(const Path& path)
  525. {
  526. Path fullPath = path;
  527. if (fullPath.isAbsolute())
  528. {
  529. if (!mResourcesFolder.includes(fullPath))
  530. return;
  531. }
  532. else
  533. fullPath.makeAbsolute(mResourcesFolder);
  534. if (FileSystem::isDirectory(fullPath))
  535. return; // Already exists
  536. FileSystem::createDir(fullPath);
  537. Path parentPath = fullPath.getParent();
  538. DirectoryEntry* newEntryParent = nullptr;
  539. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  540. if (newEntryParentLib != nullptr)
  541. {
  542. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  543. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  544. }
  545. DirectoryEntry* newHierarchyParent = nullptr;
  546. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  547. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  548. addDirectoryInternal(newEntryParent, fullPath);
  549. }
  550. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  551. {
  552. Path oldFullPath = oldPath;
  553. if (!oldFullPath.isAbsolute())
  554. oldFullPath.makeAbsolute(mResourcesFolder);
  555. Path newFullPath = newPath;
  556. if (!newFullPath.isAbsolute())
  557. newFullPath.makeAbsolute(mResourcesFolder);
  558. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  559. FileSystem::move(oldFullPath, newFullPath, overwrite);
  560. Path oldMetaPath = getMetaPath(oldFullPath);
  561. Path newMetaPath = getMetaPath(newFullPath);
  562. LibraryEntry* oldEntry = findEntry(oldFullPath);
  563. if(oldEntry != nullptr) // Moving from the Resources folder
  564. {
  565. // Moved outside of Resources, delete entry & meta file
  566. if (!mResourcesFolder.includes(newFullPath))
  567. {
  568. if(oldEntry->type == LibraryEntryType::File)
  569. {
  570. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  571. if(FileSystem::isFile(oldMetaPath))
  572. FileSystem::remove(oldMetaPath);
  573. }
  574. else if(oldEntry->type == LibraryEntryType::Directory)
  575. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  576. }
  577. else // Just moving internally
  578. {
  579. doOnEntryRemoved(oldEntry);
  580. if(FileSystem::isFile(oldMetaPath))
  581. FileSystem::move(oldMetaPath, newMetaPath);
  582. DirectoryEntry* parent = oldEntry->parent;
  583. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  584. if(findIter != parent->mChildren.end())
  585. parent->mChildren.erase(findIter);
  586. Path parentPath = newFullPath.getParent();
  587. DirectoryEntry* newEntryParent = nullptr;
  588. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  589. if(newEntryParentLib != nullptr)
  590. {
  591. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  592. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  593. }
  594. DirectoryEntry* newHierarchyParent = nullptr;
  595. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  596. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  597. newEntryParent->mChildren.push_back(oldEntry);
  598. oldEntry->parent = newEntryParent;
  599. oldEntry->path = newFullPath;
  600. oldEntry->elementName = newFullPath.getWTail();
  601. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  602. {
  603. Stack<LibraryEntry*> todo;
  604. todo.push(oldEntry);
  605. while(!todo.empty())
  606. {
  607. LibraryEntry* curEntry = todo.top();
  608. todo.pop();
  609. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  610. for(auto& child : curDirEntry->mChildren)
  611. {
  612. child->path = child->parent->path;
  613. child->path.append(child->elementName);
  614. if(child->type == LibraryEntryType::Directory)
  615. todo.push(child);
  616. }
  617. }
  618. }
  619. doOnEntryAdded(oldEntry);
  620. }
  621. }
  622. else // Moving from outside of the Resources folder (likely adding a new resource)
  623. {
  624. checkForModifications(newFullPath);
  625. }
  626. }
  627. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  628. {
  629. Path oldFullPath = oldPath;
  630. if (!oldFullPath.isAbsolute())
  631. oldFullPath.makeAbsolute(mResourcesFolder);
  632. Path newFullPath = newPath;
  633. if (!newFullPath.isAbsolute())
  634. newFullPath.makeAbsolute(mResourcesFolder);
  635. if (!FileSystem::exists(oldFullPath))
  636. return;
  637. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  638. // Copying a file/folder outside of the Resources folder, no special handling needed
  639. if (!mResourcesFolder.includes(newFullPath))
  640. return;
  641. Path parentPath = newFullPath.getParent();
  642. DirectoryEntry* newEntryParent = nullptr;
  643. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  644. if (newEntryParentLib != nullptr)
  645. {
  646. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  647. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  648. }
  649. // If the source is outside of Resources folder, just plain import the copy
  650. LibraryEntry* oldEntry = findEntry(oldFullPath);
  651. if (oldEntry == nullptr)
  652. {
  653. checkForModifications(newFullPath);
  654. return;
  655. }
  656. // Both source and destination are within Resources folder, need to preserve import options on the copies
  657. LibraryEntry* newEntry = nullptr;
  658. if (FileSystem::isFile(newFullPath))
  659. {
  660. assert(oldEntry->type == LibraryEntryType::File);
  661. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  662. newEntry = addResourceInternal(newEntryParent, newFullPath, oldResEntry->meta->getImportOptions(), true);
  663. }
  664. else
  665. {
  666. assert(oldEntry->type == LibraryEntryType::File);
  667. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  668. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  669. newEntry = newDirEntry;
  670. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  671. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  672. while (!todo.empty())
  673. {
  674. auto current = todo.top();
  675. todo.pop();
  676. DirectoryEntry* sourceDir = std::get<0>(current);
  677. DirectoryEntry* destDir = std::get<1>(current);
  678. for (auto& child : sourceDir->mChildren)
  679. {
  680. Path childDestPath = destDir->path;
  681. childDestPath.append(child->path.getWTail());
  682. if (child->type == LibraryEntryType::File)
  683. {
  684. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  685. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  686. }
  687. else // Directory
  688. {
  689. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  690. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  691. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  692. }
  693. }
  694. }
  695. }
  696. }
  697. void ProjectLibrary::deleteEntry(const Path& path)
  698. {
  699. Path fullPath = path;
  700. if (!fullPath.isAbsolute())
  701. fullPath.makeAbsolute(mResourcesFolder);
  702. if(FileSystem::exists(fullPath))
  703. FileSystem::remove(fullPath);
  704. LibraryEntry* entry = findEntry(fullPath);
  705. if(entry != nullptr)
  706. {
  707. if(entry->type == LibraryEntryType::File)
  708. {
  709. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  710. Path metaPath = getMetaPath(fullPath);
  711. if(FileSystem::isFile(metaPath))
  712. FileSystem::remove(metaPath);
  713. }
  714. else if(entry->type == LibraryEntryType::Directory)
  715. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  716. }
  717. }
  718. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  719. {
  720. LibraryEntry* entry = findEntry(path);
  721. if (entry != nullptr)
  722. {
  723. if (entry->type == LibraryEntryType::File)
  724. {
  725. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  726. reimportResourceInternal(resEntry, importOptions, forceReimport);
  727. queueDependantForReimport(resEntry);
  728. }
  729. }
  730. }
  731. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  732. {
  733. LibraryEntry* entry = findEntry(path);
  734. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  735. return;
  736. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  737. resEntry->meta->setIncludeInBuild(include);
  738. Path metaPath = resEntry->path;
  739. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  740. FileEncoder fs(metaPath);
  741. fs.encode(resEntry->meta.get());
  742. }
  743. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  744. {
  745. Vector<ResourceEntry*> output;
  746. Stack<DirectoryEntry*> todo;
  747. todo.push(mRootEntry);
  748. while (!todo.empty())
  749. {
  750. DirectoryEntry* directory = todo.top();
  751. todo.pop();
  752. for (auto& child : directory->mChildren)
  753. {
  754. if (child->type == LibraryEntryType::File)
  755. {
  756. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  757. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  758. output.push_back(resEntry);
  759. }
  760. else if (child->type == LibraryEntryType::Directory)
  761. {
  762. todo.push(static_cast<DirectoryEntry*>(child));
  763. }
  764. }
  765. }
  766. return output;
  767. }
  768. HResource ProjectLibrary::load(const Path& path)
  769. {
  770. LibraryEntry* entry = findEntry(path);
  771. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  772. return HResource();
  773. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  774. String resUUID = resEntry->meta->getUUID();
  775. return gResources().loadFromUUID(resUUID);
  776. }
  777. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  778. {
  779. Path parentPath = fullPath;
  780. DirectoryEntry* newEntryParent = nullptr;
  781. Stack<Path> parentPaths;
  782. do
  783. {
  784. Path newParentPath = parentPath.getParent();
  785. if(newParentPath == parentPath)
  786. break;
  787. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  788. if(newEntryParentLib != nullptr)
  789. {
  790. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  791. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  792. break;
  793. }
  794. parentPaths.push(newParentPath);
  795. parentPath = newParentPath;
  796. } while (true);
  797. assert(newEntryParent != nullptr); // Must exist
  798. if(newHierarchyRoot != nullptr)
  799. *newHierarchyRoot = newEntryParent;
  800. while(!parentPaths.empty())
  801. {
  802. Path curPath = parentPaths.top();
  803. parentPaths.pop();
  804. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  805. }
  806. if(newHierarchyLeaf != nullptr)
  807. *newHierarchyLeaf = newEntryParent;
  808. }
  809. Path ProjectLibrary::getMetaPath(const Path& path) const
  810. {
  811. Path metaPath = path;
  812. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  813. return metaPath;
  814. }
  815. bool ProjectLibrary::isMeta(const Path& fullPath) const
  816. {
  817. return fullPath.getWExtension() == L".meta";
  818. }
  819. void ProjectLibrary::unloadLibrary()
  820. {
  821. if (!mIsLoaded)
  822. return;
  823. mProjectFolder = Path::BLANK;
  824. mResourcesFolder = Path::BLANK;
  825. clearEntries();
  826. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  827. mReimportQueue.clear();
  828. mDependencies.clear();
  829. gResources().unregisterResourceManifest(mResourceManifest);
  830. mResourceManifest = nullptr;
  831. mIsLoaded = false;
  832. }
  833. void ProjectLibrary::makeEntriesRelative()
  834. {
  835. // Make all paths relative before saving
  836. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  837. [&](LibraryEntry* entry, const Path& root)
  838. {
  839. entry->path.makeRelative(root);
  840. if (entry->type == LibraryEntryType::Directory)
  841. {
  842. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  843. for (auto& child : dirEntry->mChildren)
  844. makeRelative(child, root);
  845. }
  846. };
  847. Path root = getResourcesFolder();
  848. makeRelative(mRootEntry, root);
  849. }
  850. void ProjectLibrary::makeEntriesAbsolute()
  851. {
  852. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  853. [&](LibraryEntry* entry, const Path& root)
  854. {
  855. entry->path.makeAbsolute(root);
  856. if (entry->type == LibraryEntryType::Directory)
  857. {
  858. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  859. for (auto& child : dirEntry->mChildren)
  860. makeAbsolute(child, root);
  861. }
  862. };
  863. Path root = getResourcesFolder();
  864. makeAbsolute(mRootEntry, root);
  865. }
  866. void ProjectLibrary::saveLibrary()
  867. {
  868. if (!mIsLoaded)
  869. return;
  870. // Make all paths relative before saving
  871. makeEntriesRelative();
  872. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  873. Path libraryEntriesPath = mProjectFolder;
  874. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  875. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  876. FileEncoder fs(libraryEntriesPath);
  877. fs.encode(libEntries.get());
  878. // Restore absolute entry paths
  879. makeEntriesAbsolute();
  880. Path resourceManifestPath = mProjectFolder;
  881. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  882. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  883. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  884. }
  885. void ProjectLibrary::loadLibrary()
  886. {
  887. unloadLibrary();
  888. mProjectFolder = gEditorApplication().getProjectPath();
  889. mResourcesFolder = mProjectFolder;
  890. mResourcesFolder.append(RESOURCES_DIR);
  891. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  892. Path libraryEntriesPath = mProjectFolder;
  893. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  894. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  895. if(FileSystem::exists(libraryEntriesPath))
  896. {
  897. FileDecoder fs(libraryEntriesPath);
  898. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  899. *mRootEntry = libEntries->getRootEntry();
  900. for(auto& child : mRootEntry->mChildren)
  901. child->parent = mRootEntry;
  902. mRootEntry->parent = nullptr;
  903. }
  904. // Entries are stored relative to project folder, but we want their absolute paths now
  905. makeEntriesAbsolute();
  906. // Load resource manifest
  907. Path resourceManifestPath = mProjectFolder;
  908. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  909. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  910. if (FileSystem::exists(resourceManifestPath))
  911. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  912. else
  913. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  914. gResources().registerResourceManifest(mResourceManifest);
  915. // Load all meta files
  916. Stack<DirectoryEntry*> todo;
  917. todo.push(mRootEntry);
  918. while(!todo.empty())
  919. {
  920. DirectoryEntry* curDir = todo.top();
  921. todo.pop();
  922. for(auto& child : curDir->mChildren)
  923. {
  924. if(child->type == LibraryEntryType::File)
  925. {
  926. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  927. bool doAddDependencies = true;
  928. if (FileSystem::isFile(resEntry->path))
  929. {
  930. if (resEntry->meta == nullptr)
  931. {
  932. Path metaPath = resEntry->path;
  933. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  934. if (FileSystem::isFile(metaPath))
  935. {
  936. FileDecoder fs(metaPath);
  937. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  938. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  939. {
  940. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  941. resEntry->meta = resourceMeta;
  942. }
  943. }
  944. else
  945. {
  946. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  947. reimportResourceInternal(resEntry);
  948. doAddDependencies = false;
  949. }
  950. }
  951. }
  952. if (resEntry->meta != nullptr)
  953. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  954. if (doAddDependencies)
  955. addDependencies(resEntry);
  956. }
  957. else if(child->type == LibraryEntryType::Directory)
  958. {
  959. todo.push(static_cast<DirectoryEntry*>(child));
  960. }
  961. }
  962. }
  963. mIsLoaded = true;
  964. }
  965. void ProjectLibrary::clearEntries()
  966. {
  967. if (mRootEntry == nullptr)
  968. return;
  969. std::function<void(LibraryEntry*)> deleteRecursive =
  970. [&](LibraryEntry* entry)
  971. {
  972. if (entry->type == LibraryEntryType::Directory)
  973. {
  974. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  975. for (auto& child : dirEntry->mChildren)
  976. deleteRecursive(child);
  977. }
  978. bs_delete(entry);
  979. };
  980. deleteRecursive(mRootEntry);
  981. mRootEntry = nullptr;
  982. }
  983. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  984. {
  985. if (!onEntryRemoved.empty())
  986. onEntryRemoved(entry->path);
  987. if (entry->type == LibraryEntryType::File)
  988. {
  989. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  990. queueDependantForReimport(resEntry);
  991. removeDependencies(resEntry);
  992. }
  993. }
  994. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  995. {
  996. if (!onEntryAdded.empty())
  997. onEntryAdded(entry->path);
  998. if (entry->type == LibraryEntryType::File)
  999. {
  1000. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  1001. queueDependantForReimport(resEntry);
  1002. }
  1003. }
  1004. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1005. {
  1006. Vector<Path> output;
  1007. if (entry->meta == nullptr)
  1008. return output;
  1009. if (entry->meta->getTypeID() == TID_Shader)
  1010. {
  1011. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1012. for (auto& include : metaData->includes)
  1013. output.push_back(include);
  1014. }
  1015. return output;
  1016. }
  1017. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1018. {
  1019. Vector<Path> dependencies = getImportDependencies(entry);
  1020. for (auto& dependency : dependencies)
  1021. mDependencies[dependency].push_back(entry->path);
  1022. }
  1023. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1024. {
  1025. Vector<Path> dependencies = getImportDependencies(entry);
  1026. for (auto& dependency : dependencies)
  1027. {
  1028. Vector<Path>& curDependencies = mDependencies[dependency];
  1029. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1030. [&](const Path& x)
  1031. {
  1032. return x == entry->path;
  1033. });
  1034. curDependencies.erase(iterRemove, curDependencies.end());
  1035. }
  1036. }
  1037. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  1038. {
  1039. auto iterFind = mDependencies.find(entry->path);
  1040. if (iterFind == mDependencies.end())
  1041. return;
  1042. for (auto& dependency : iterFind->second)
  1043. mReimportQueue.insert(dependency);
  1044. }
  1045. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1046. {
  1047. return ProjectLibrary::instance();
  1048. }
  1049. }