BsProjectLibrary.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. Path metaPath = getMetaPath(resource->path);
  281. if (FileSystem::isFile(metaPath))
  282. FileSystem::remove(metaPath);
  283. DirectoryEntry* parent = resource->parent;
  284. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  285. [&] (const LibraryEntry* entry) { return entry == resource; });
  286. parent->mChildren.erase(findIter);
  287. doOnEntryRemoved(resource);
  288. bs_delete(resource);
  289. }
  290. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  291. {
  292. if(directory == mRootEntry)
  293. mRootEntry = nullptr;
  294. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  295. for(auto& child : childrenToDestroy)
  296. {
  297. if(child->type == LibraryEntryType::Directory)
  298. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  299. else
  300. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  301. }
  302. DirectoryEntry* parent = directory->parent;
  303. if(parent != nullptr)
  304. {
  305. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  306. [&] (const LibraryEntry* entry) { return entry == directory; });
  307. parent->mChildren.erase(findIter);
  308. }
  309. doOnEntryRemoved(directory);
  310. bs_delete(directory);
  311. }
  312. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  313. {
  314. Path metaPath = resource->path;
  315. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  316. if(resource->meta == nullptr)
  317. {
  318. if(FileSystem::isFile(metaPath))
  319. {
  320. FileDecoder fs(metaPath);
  321. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  322. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  323. {
  324. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  325. resource->meta = resourceMeta;
  326. mUUIDToPath[resourceMeta->getUUID()] = resource->path;
  327. }
  328. }
  329. }
  330. if (!isUpToDate(resource) || forceReimport)
  331. {
  332. ImportOptionsPtr curImportOptions = nullptr;
  333. if (importOptions == nullptr)
  334. {
  335. if (resource->meta != nullptr)
  336. curImportOptions = resource->meta->getImportOptions();
  337. else
  338. curImportOptions = Importer::instance().createImportOptions(resource->path);
  339. }
  340. else
  341. curImportOptions = importOptions;
  342. HResource importedResource;
  343. if(resource->meta == nullptr)
  344. {
  345. importedResource = Importer::instance().import(resource->path, curImportOptions);
  346. if (importedResource != nullptr)
  347. {
  348. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  349. UINT32 typeId = importedResource->getTypeId();
  350. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  351. }
  352. else
  353. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), 0, nullptr, curImportOptions);
  354. FileEncoder fs(metaPath);
  355. fs.encode(resource->meta.get());
  356. mUUIDToPath[resource->meta->getUUID()] = resource->path;
  357. }
  358. else
  359. {
  360. removeDependencies(resource);
  361. importedResource = HResource(resource->meta->getUUID());
  362. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  363. }
  364. addDependencies(resource);
  365. if (importedResource != nullptr)
  366. {
  367. Path internalResourcesPath = mProjectFolder;
  368. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  369. if (!FileSystem::isDirectory(internalResourcesPath))
  370. FileSystem::createDir(internalResourcesPath);
  371. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  372. gResources().save(importedResource, internalResourcesPath, true);
  373. gResources().unload(importedResource);
  374. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  375. }
  376. resource->lastUpdateTime = std::time(nullptr);
  377. }
  378. }
  379. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  380. {
  381. if(resource->meta == nullptr)
  382. return false;
  383. Path path;
  384. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  385. return false;
  386. if(!FileSystem::isFile(path))
  387. return false;
  388. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  389. return lastModifiedTime <= resource->lastUpdateTime;
  390. }
  391. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  392. {
  393. return search(pattern, {});
  394. }
  395. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  396. {
  397. Vector<LibraryEntry*> foundEntries;
  398. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  399. WString replace(L"\\\\&");
  400. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  401. std::wregex wildcard(L"\\\\\\*");
  402. WString wildcardReplace(L".*");
  403. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  404. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  405. Stack<DirectoryEntry*> todo;
  406. todo.push(mRootEntry);
  407. while (!todo.empty())
  408. {
  409. DirectoryEntry* dirEntry = todo.top();
  410. todo.pop();
  411. for (auto& child : dirEntry->mChildren)
  412. {
  413. if (std::regex_match(child->elementName, searchRegex))
  414. {
  415. if (typeIds.size() == 0)
  416. foundEntries.push_back(child);
  417. else
  418. {
  419. if (child->type == LibraryEntryType::File)
  420. {
  421. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  422. for (auto& typeId : typeIds)
  423. {
  424. if (childResEntry->meta->getTypeID() == typeId)
  425. {
  426. foundEntries.push_back(child);
  427. break;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. if (child->type == LibraryEntryType::Directory)
  434. {
  435. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  436. todo.push(childDirEntry);
  437. }
  438. }
  439. }
  440. std::sort(foundEntries.begin(), foundEntries.end(),
  441. [&](const LibraryEntry* a, const LibraryEntry* b)
  442. {
  443. return a->elementName.compare(b->elementName) < 0;
  444. });
  445. return foundEntries;
  446. }
  447. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  448. {
  449. Path fullPath = path;
  450. if (fullPath.isAbsolute())
  451. {
  452. if (!mResourcesFolder.includes(fullPath))
  453. return nullptr;
  454. }
  455. else
  456. fullPath.makeAbsolute(mResourcesFolder);
  457. Path relPath = fullPath.getRelative(mRootEntry->path);
  458. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  459. UINT32 idx = 0;
  460. LibraryEntry* current = mRootEntry;
  461. while (current != nullptr)
  462. {
  463. if (idx == numElems)
  464. return current;
  465. WString curElem;
  466. if (relPath.isFile() && idx == (numElems - 1))
  467. curElem = relPath.getWFilename();
  468. else
  469. curElem = relPath[idx];
  470. if (current->type == LibraryEntryType::Directory)
  471. {
  472. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  473. current = nullptr;
  474. for (auto& child : dirEntry->mChildren)
  475. {
  476. if (Path::comparePathElem(curElem, child->elementName))
  477. {
  478. idx++;
  479. current = child;
  480. break;
  481. }
  482. }
  483. }
  484. else
  485. break;
  486. }
  487. return nullptr;
  488. }
  489. Path ProjectLibrary::uuidToPath(const String& uuid) const
  490. {
  491. auto iterFind = mUUIDToPath.find(uuid);
  492. if (iterFind != mUUIDToPath.end())
  493. return iterFind->second;
  494. return Path::BLANK;
  495. }
  496. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  497. {
  498. if (resource == nullptr)
  499. return;
  500. Path assetPath = path;
  501. if (path.isAbsolute())
  502. {
  503. if (!getResourcesFolder().includes(path))
  504. return;
  505. assetPath = path.getRelative(getResourcesFolder());
  506. }
  507. LibraryEntry* existingEntry = findEntry(assetPath);
  508. if (existingEntry != nullptr)
  509. {
  510. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  511. return;
  512. }
  513. resource->setName(path.getWFilename(false));
  514. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  515. Resources::instance().save(resource, absPath, false);
  516. checkForModifications(absPath);
  517. }
  518. void ProjectLibrary::saveEntry(const HResource& resource)
  519. {
  520. if (resource == nullptr)
  521. return;
  522. Path filePath;
  523. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  524. return;
  525. Resources::instance().save(resource, filePath, true);
  526. }
  527. void ProjectLibrary::createFolderEntry(const Path& path)
  528. {
  529. Path fullPath = path;
  530. if (fullPath.isAbsolute())
  531. {
  532. if (!mResourcesFolder.includes(fullPath))
  533. return;
  534. }
  535. else
  536. fullPath.makeAbsolute(mResourcesFolder);
  537. if (FileSystem::isDirectory(fullPath))
  538. return; // Already exists
  539. FileSystem::createDir(fullPath);
  540. Path parentPath = fullPath.getParent();
  541. DirectoryEntry* newEntryParent = nullptr;
  542. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  543. if (newEntryParentLib != nullptr)
  544. {
  545. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  546. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  547. }
  548. DirectoryEntry* newHierarchyParent = nullptr;
  549. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  550. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  551. addDirectoryInternal(newEntryParent, fullPath);
  552. }
  553. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  554. {
  555. Path oldFullPath = oldPath;
  556. if (!oldFullPath.isAbsolute())
  557. oldFullPath.makeAbsolute(mResourcesFolder);
  558. Path newFullPath = newPath;
  559. if (!newFullPath.isAbsolute())
  560. newFullPath.makeAbsolute(mResourcesFolder);
  561. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  562. FileSystem::move(oldFullPath, newFullPath, overwrite);
  563. Path oldMetaPath = getMetaPath(oldFullPath);
  564. Path newMetaPath = getMetaPath(newFullPath);
  565. LibraryEntry* oldEntry = findEntry(oldFullPath);
  566. if(oldEntry != nullptr) // Moving from the Resources folder
  567. {
  568. // Moved outside of Resources, delete entry & meta file
  569. if (!mResourcesFolder.includes(newFullPath))
  570. {
  571. if(oldEntry->type == LibraryEntryType::File)
  572. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  573. else if(oldEntry->type == LibraryEntryType::Directory)
  574. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  575. }
  576. else // Just moving internally
  577. {
  578. doOnEntryRemoved(oldEntry);
  579. if(FileSystem::isFile(oldMetaPath))
  580. FileSystem::move(oldMetaPath, newMetaPath);
  581. DirectoryEntry* parent = oldEntry->parent;
  582. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  583. if(findIter != parent->mChildren.end())
  584. parent->mChildren.erase(findIter);
  585. Path parentPath = newFullPath.getParent();
  586. DirectoryEntry* newEntryParent = nullptr;
  587. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  588. if(newEntryParentLib != nullptr)
  589. {
  590. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  591. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  592. }
  593. DirectoryEntry* newHierarchyParent = nullptr;
  594. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  595. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  596. newEntryParent->mChildren.push_back(oldEntry);
  597. oldEntry->parent = newEntryParent;
  598. oldEntry->path = newFullPath;
  599. oldEntry->elementName = newFullPath.getWTail();
  600. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  601. {
  602. Stack<LibraryEntry*> todo;
  603. todo.push(oldEntry);
  604. while(!todo.empty())
  605. {
  606. LibraryEntry* curEntry = todo.top();
  607. todo.pop();
  608. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  609. for(auto& child : curDirEntry->mChildren)
  610. {
  611. child->path = child->parent->path;
  612. child->path.append(child->elementName);
  613. if(child->type == LibraryEntryType::Directory)
  614. todo.push(child);
  615. }
  616. }
  617. }
  618. doOnEntryAdded(oldEntry);
  619. }
  620. }
  621. else // Moving from outside of the Resources folder (likely adding a new resource)
  622. {
  623. checkForModifications(newFullPath);
  624. }
  625. }
  626. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  627. {
  628. Path oldFullPath = oldPath;
  629. if (!oldFullPath.isAbsolute())
  630. oldFullPath.makeAbsolute(mResourcesFolder);
  631. Path newFullPath = newPath;
  632. if (!newFullPath.isAbsolute())
  633. newFullPath.makeAbsolute(mResourcesFolder);
  634. if (!FileSystem::exists(oldFullPath))
  635. return;
  636. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  637. // Copying a file/folder outside of the Resources folder, no special handling needed
  638. if (!mResourcesFolder.includes(newFullPath))
  639. return;
  640. Path parentPath = newFullPath.getParent();
  641. DirectoryEntry* newEntryParent = nullptr;
  642. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  643. if (newEntryParentLib != nullptr)
  644. {
  645. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  646. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  647. }
  648. // If the source is outside of Resources folder, just plain import the copy
  649. LibraryEntry* oldEntry = findEntry(oldFullPath);
  650. if (oldEntry == nullptr)
  651. {
  652. checkForModifications(newFullPath);
  653. return;
  654. }
  655. // Both source and destination are within Resources folder, need to preserve import options on the copies
  656. LibraryEntry* newEntry = nullptr;
  657. if (FileSystem::isFile(newFullPath))
  658. {
  659. assert(oldEntry->type == LibraryEntryType::File);
  660. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  661. newEntry = addResourceInternal(newEntryParent, newFullPath, oldResEntry->meta->getImportOptions(), true);
  662. }
  663. else
  664. {
  665. assert(oldEntry->type == LibraryEntryType::File);
  666. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  667. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  668. newEntry = newDirEntry;
  669. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  670. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  671. while (!todo.empty())
  672. {
  673. auto current = todo.top();
  674. todo.pop();
  675. DirectoryEntry* sourceDir = std::get<0>(current);
  676. DirectoryEntry* destDir = std::get<1>(current);
  677. for (auto& child : sourceDir->mChildren)
  678. {
  679. Path childDestPath = destDir->path;
  680. childDestPath.append(child->path.getWTail());
  681. if (child->type == LibraryEntryType::File)
  682. {
  683. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  684. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  685. }
  686. else // Directory
  687. {
  688. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  689. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  690. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  691. }
  692. }
  693. }
  694. }
  695. }
  696. void ProjectLibrary::deleteEntry(const Path& path)
  697. {
  698. Path fullPath = path;
  699. if (!fullPath.isAbsolute())
  700. fullPath.makeAbsolute(mResourcesFolder);
  701. if(FileSystem::exists(fullPath))
  702. FileSystem::remove(fullPath);
  703. LibraryEntry* entry = findEntry(fullPath);
  704. if(entry != nullptr)
  705. {
  706. if(entry->type == LibraryEntryType::File)
  707. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  708. else if(entry->type == LibraryEntryType::Directory)
  709. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  710. }
  711. }
  712. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  713. {
  714. LibraryEntry* entry = findEntry(path);
  715. if (entry != nullptr)
  716. {
  717. if (entry->type == LibraryEntryType::File)
  718. {
  719. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  720. reimportResourceInternal(resEntry, importOptions, forceReimport);
  721. queueDependantForReimport(resEntry);
  722. }
  723. }
  724. }
  725. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  726. {
  727. LibraryEntry* entry = findEntry(path);
  728. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  729. return;
  730. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  731. resEntry->meta->setIncludeInBuild(include);
  732. Path metaPath = resEntry->path;
  733. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  734. FileEncoder fs(metaPath);
  735. fs.encode(resEntry->meta.get());
  736. }
  737. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  738. {
  739. Vector<ResourceEntry*> output;
  740. Stack<DirectoryEntry*> todo;
  741. todo.push(mRootEntry);
  742. while (!todo.empty())
  743. {
  744. DirectoryEntry* directory = todo.top();
  745. todo.pop();
  746. for (auto& child : directory->mChildren)
  747. {
  748. if (child->type == LibraryEntryType::File)
  749. {
  750. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  751. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  752. output.push_back(resEntry);
  753. }
  754. else if (child->type == LibraryEntryType::Directory)
  755. {
  756. todo.push(static_cast<DirectoryEntry*>(child));
  757. }
  758. }
  759. }
  760. return output;
  761. }
  762. HResource ProjectLibrary::load(const Path& path)
  763. {
  764. LibraryEntry* entry = findEntry(path);
  765. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  766. return HResource();
  767. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  768. String resUUID = resEntry->meta->getUUID();
  769. return gResources().loadFromUUID(resUUID);
  770. }
  771. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  772. {
  773. Path parentPath = fullPath;
  774. DirectoryEntry* newEntryParent = nullptr;
  775. Stack<Path> parentPaths;
  776. do
  777. {
  778. Path newParentPath = parentPath.getParent();
  779. if(newParentPath == parentPath)
  780. break;
  781. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  782. if(newEntryParentLib != nullptr)
  783. {
  784. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  785. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  786. break;
  787. }
  788. parentPaths.push(newParentPath);
  789. parentPath = newParentPath;
  790. } while (true);
  791. assert(newEntryParent != nullptr); // Must exist
  792. if(newHierarchyRoot != nullptr)
  793. *newHierarchyRoot = newEntryParent;
  794. while(!parentPaths.empty())
  795. {
  796. Path curPath = parentPaths.top();
  797. parentPaths.pop();
  798. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  799. }
  800. if(newHierarchyLeaf != nullptr)
  801. *newHierarchyLeaf = newEntryParent;
  802. }
  803. Path ProjectLibrary::getMetaPath(const Path& path) const
  804. {
  805. Path metaPath = path;
  806. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  807. return metaPath;
  808. }
  809. bool ProjectLibrary::isMeta(const Path& fullPath) const
  810. {
  811. return fullPath.getWExtension() == L".meta";
  812. }
  813. void ProjectLibrary::unloadLibrary()
  814. {
  815. if (!mIsLoaded)
  816. return;
  817. mProjectFolder = Path::BLANK;
  818. mResourcesFolder = Path::BLANK;
  819. clearEntries();
  820. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  821. mReimportQueue.clear();
  822. mDependencies.clear();
  823. gResources().unregisterResourceManifest(mResourceManifest);
  824. mResourceManifest = nullptr;
  825. mIsLoaded = false;
  826. }
  827. void ProjectLibrary::makeEntriesRelative()
  828. {
  829. // Make all paths relative before saving
  830. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  831. [&](LibraryEntry* entry, const Path& root)
  832. {
  833. entry->path.makeRelative(root);
  834. if (entry->type == LibraryEntryType::Directory)
  835. {
  836. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  837. for (auto& child : dirEntry->mChildren)
  838. makeRelative(child, root);
  839. }
  840. };
  841. Path root = getResourcesFolder();
  842. makeRelative(mRootEntry, root);
  843. }
  844. void ProjectLibrary::makeEntriesAbsolute()
  845. {
  846. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  847. [&](LibraryEntry* entry, const Path& root)
  848. {
  849. entry->path.makeAbsolute(root);
  850. if (entry->type == LibraryEntryType::Directory)
  851. {
  852. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  853. for (auto& child : dirEntry->mChildren)
  854. makeAbsolute(child, root);
  855. }
  856. };
  857. Path root = getResourcesFolder();
  858. makeAbsolute(mRootEntry, root);
  859. }
  860. void ProjectLibrary::saveLibrary()
  861. {
  862. if (!mIsLoaded)
  863. return;
  864. // Make all paths relative before saving
  865. makeEntriesRelative();
  866. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  867. Path libraryEntriesPath = mProjectFolder;
  868. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  869. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  870. FileEncoder fs(libraryEntriesPath);
  871. fs.encode(libEntries.get());
  872. // Restore absolute entry paths
  873. makeEntriesAbsolute();
  874. Path resourceManifestPath = mProjectFolder;
  875. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  876. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  877. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  878. }
  879. void ProjectLibrary::loadLibrary()
  880. {
  881. unloadLibrary();
  882. mProjectFolder = gEditorApplication().getProjectPath();
  883. mResourcesFolder = mProjectFolder;
  884. mResourcesFolder.append(RESOURCES_DIR);
  885. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  886. Path libraryEntriesPath = mProjectFolder;
  887. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  888. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  889. if(FileSystem::exists(libraryEntriesPath))
  890. {
  891. FileDecoder fs(libraryEntriesPath);
  892. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  893. *mRootEntry = libEntries->getRootEntry();
  894. for(auto& child : mRootEntry->mChildren)
  895. child->parent = mRootEntry;
  896. mRootEntry->parent = nullptr;
  897. }
  898. // Entries are stored relative to project folder, but we want their absolute paths now
  899. makeEntriesAbsolute();
  900. // Load resource manifest
  901. Path resourceManifestPath = mProjectFolder;
  902. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  903. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  904. if (FileSystem::exists(resourceManifestPath))
  905. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  906. else
  907. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  908. gResources().registerResourceManifest(mResourceManifest);
  909. // Load all meta files
  910. Stack<DirectoryEntry*> todo;
  911. todo.push(mRootEntry);
  912. Vector<LibraryEntry*> deletedEntries;
  913. while(!todo.empty())
  914. {
  915. DirectoryEntry* curDir = todo.top();
  916. todo.pop();
  917. for(auto& child : curDir->mChildren)
  918. {
  919. if(child->type == LibraryEntryType::File)
  920. {
  921. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  922. if (FileSystem::isFile(resEntry->path))
  923. {
  924. bool doAddDependencies = true;
  925. if (resEntry->meta == nullptr)
  926. {
  927. Path metaPath = resEntry->path;
  928. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  929. if (FileSystem::isFile(metaPath))
  930. {
  931. FileDecoder fs(metaPath);
  932. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  933. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  934. {
  935. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  936. resEntry->meta = resourceMeta;
  937. }
  938. }
  939. else
  940. {
  941. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  942. reimportResourceInternal(resEntry);
  943. doAddDependencies = false;
  944. }
  945. }
  946. if (resEntry->meta != nullptr)
  947. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  948. if (doAddDependencies)
  949. addDependencies(resEntry);
  950. }
  951. else
  952. deletedEntries.push_back(resEntry);
  953. }
  954. else if(child->type == LibraryEntryType::Directory)
  955. {
  956. if (FileSystem::isDirectory(child->path))
  957. todo.push(static_cast<DirectoryEntry*>(child));
  958. else
  959. deletedEntries.push_back(child);
  960. }
  961. }
  962. }
  963. // Remove entries that no longer have corresponding files
  964. for (auto& deletedEntry : deletedEntries)
  965. {
  966. if (deletedEntry->type == LibraryEntryType::File)
  967. {
  968. ResourceEntry* resEntry = static_cast<ResourceEntry*>(deletedEntry);
  969. deleteResourceInternal(resEntry);
  970. }
  971. else
  972. {
  973. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  974. deleteDirectoryInternal(dirEntry);
  975. }
  976. }
  977. mIsLoaded = true;
  978. }
  979. void ProjectLibrary::clearEntries()
  980. {
  981. if (mRootEntry == nullptr)
  982. return;
  983. std::function<void(LibraryEntry*)> deleteRecursive =
  984. [&](LibraryEntry* entry)
  985. {
  986. if (entry->type == LibraryEntryType::Directory)
  987. {
  988. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  989. for (auto& child : dirEntry->mChildren)
  990. deleteRecursive(child);
  991. }
  992. bs_delete(entry);
  993. };
  994. deleteRecursive(mRootEntry);
  995. mRootEntry = nullptr;
  996. }
  997. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  998. {
  999. if (!onEntryRemoved.empty())
  1000. onEntryRemoved(entry->path);
  1001. if (entry->type == LibraryEntryType::File)
  1002. {
  1003. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  1004. queueDependantForReimport(resEntry);
  1005. removeDependencies(resEntry);
  1006. }
  1007. }
  1008. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  1009. {
  1010. if (!onEntryAdded.empty())
  1011. onEntryAdded(entry->path);
  1012. if (entry->type == LibraryEntryType::File)
  1013. {
  1014. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  1015. queueDependantForReimport(resEntry);
  1016. }
  1017. }
  1018. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1019. {
  1020. Vector<Path> output;
  1021. if (entry->meta == nullptr)
  1022. return output;
  1023. if (entry->meta->getTypeID() == TID_Shader)
  1024. {
  1025. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1026. for (auto& include : metaData->includes)
  1027. output.push_back(include);
  1028. }
  1029. return output;
  1030. }
  1031. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1032. {
  1033. Vector<Path> dependencies = getImportDependencies(entry);
  1034. for (auto& dependency : dependencies)
  1035. mDependencies[dependency].push_back(entry->path);
  1036. }
  1037. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1038. {
  1039. Vector<Path> dependencies = getImportDependencies(entry);
  1040. for (auto& dependency : dependencies)
  1041. {
  1042. Vector<Path>& curDependencies = mDependencies[dependency];
  1043. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1044. [&](const Path& x)
  1045. {
  1046. return x == entry->path;
  1047. });
  1048. curDependencies.erase(iterRemove, curDependencies.end());
  1049. }
  1050. }
  1051. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  1052. {
  1053. auto iterFind = mDependencies.find(entry->path);
  1054. if (iterFind == mDependencies.end())
  1055. return;
  1056. for (auto& dependency : iterFind->second)
  1057. mReimportQueue.insert(dependency);
  1058. }
  1059. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1060. {
  1061. return ProjectLibrary::instance();
  1062. }
  1063. }