BsProjectLibrary.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 "BsResourceImporter.h"
  16. #include "BsShader.h"
  17. #include <regex>
  18. using namespace std::placeholders;
  19. namespace BansheeEngine
  20. {
  21. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  22. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = L"Internal\\Resources\\";
  23. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  24. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  25. ProjectLibrary::LibraryEntry::LibraryEntry()
  26. :parent(nullptr), type(LibraryEntryType::Directory)
  27. { }
  28. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  29. :path(path), parent(parent), type(type), elementName(name)
  30. { }
  31. ProjectLibrary::ResourceEntry::ResourceEntry()
  32. : lastUpdateTime(0)
  33. { }
  34. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  35. : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  36. { }
  37. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  38. { }
  39. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  40. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  41. { }
  42. ProjectLibrary::ProjectLibrary(const Path& projectFolder)
  43. :mRootEntry(nullptr), mProjectFolder(projectFolder)
  44. {
  45. mResourcesFolder = mProjectFolder;
  46. mResourcesFolder.append(RESOURCES_DIR);
  47. load();
  48. if(mResourceManifest == nullptr)
  49. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  50. gResources().registerResourceManifest(mResourceManifest);
  51. }
  52. ProjectLibrary::~ProjectLibrary()
  53. {
  54. save();
  55. if(mRootEntry != nullptr)
  56. deleteDirectoryInternal(mRootEntry);
  57. }
  58. void ProjectLibrary::update()
  59. {
  60. while (!mReimportQueue.empty())
  61. {
  62. Path toReimport = *mReimportQueue.begin();
  63. LibraryEntry* entry = findEntry(toReimport);
  64. if (entry != nullptr && entry->type == LibraryEntryType::File)
  65. {
  66. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  67. reimportResourceInternal(resEntry, resEntry->meta->getImportOptions(), true);
  68. queueDependantForReimport(resEntry);
  69. }
  70. mReimportQueue.erase(mReimportQueue.begin());
  71. }
  72. }
  73. void ProjectLibrary::checkForModifications(const Path& fullPath)
  74. {
  75. Vector<Path> dirtyResources;
  76. checkForModifications(fullPath, true, dirtyResources);
  77. }
  78. void ProjectLibrary::checkForModifications(const Path& fullPath, bool import, Vector<Path>& dirtyResources)
  79. {
  80. if (!mResourcesFolder.includes(fullPath))
  81. return; // Folder not part of our resources path, so no modifications
  82. if(mRootEntry == nullptr)
  83. {
  84. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  85. }
  86. Path pathToSearch = fullPath;
  87. LibraryEntry* entry = findEntry(pathToSearch);
  88. if(entry == nullptr) // File could be new, try to find parent directory entry
  89. {
  90. if (isMeta(pathToSearch))
  91. {
  92. Path sourceFilePath = pathToSearch;
  93. sourceFilePath.setExtension(L"");
  94. if (!FileSystem::isFile(sourceFilePath))
  95. {
  96. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  97. FileSystem::remove(pathToSearch);
  98. }
  99. }
  100. else
  101. {
  102. Path parentDirPath = pathToSearch.getParent();
  103. entry = findEntry(parentDirPath);
  104. // Cannot find parent directory. Create the needed hierarchy.
  105. DirectoryEntry* entryParent = nullptr;
  106. DirectoryEntry* newHierarchyParent = nullptr;
  107. if (entry == nullptr)
  108. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  109. else
  110. entryParent = static_cast<DirectoryEntry*>(entry);
  111. if (FileSystem::isFile(pathToSearch))
  112. {
  113. if (import)
  114. addResourceInternal(entryParent, pathToSearch);
  115. dirtyResources.push_back(pathToSearch);
  116. }
  117. else if (FileSystem::isDirectory(pathToSearch))
  118. {
  119. addDirectoryInternal(entryParent, pathToSearch);
  120. checkForModifications(pathToSearch);
  121. }
  122. }
  123. }
  124. else if(entry->type == LibraryEntryType::File)
  125. {
  126. if(FileSystem::isFile(entry->path))
  127. {
  128. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  129. if (import)
  130. {
  131. reimportResourceInternal(resEntry);
  132. queueDependantForReimport(resEntry);
  133. }
  134. if (!isUpToDate(resEntry))
  135. dirtyResources.push_back(entry->path);
  136. }
  137. else
  138. {
  139. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  140. }
  141. }
  142. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  143. {
  144. if(!FileSystem::isDirectory(entry->path))
  145. {
  146. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  147. }
  148. else
  149. {
  150. Stack<DirectoryEntry*> todo;
  151. todo.push(static_cast<DirectoryEntry*>(entry));
  152. Vector<Path> childFiles;
  153. Vector<Path> childDirectories;
  154. Vector<bool> existingEntries;
  155. Vector<LibraryEntry*> toDelete;
  156. while(!todo.empty())
  157. {
  158. DirectoryEntry* currentDir = todo.top();
  159. todo.pop();
  160. existingEntries.clear();
  161. existingEntries.resize(currentDir->mChildren.size());
  162. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  163. existingEntries[i] = false;
  164. childFiles.clear();
  165. childDirectories.clear();
  166. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  167. for(auto& filePath : childFiles)
  168. {
  169. if(isMeta(filePath))
  170. {
  171. Path sourceFilePath = filePath;
  172. sourceFilePath.setExtension(L"");
  173. if(!FileSystem::isFile(sourceFilePath))
  174. {
  175. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  176. FileSystem::remove(filePath);
  177. }
  178. }
  179. else
  180. {
  181. ResourceEntry* existingEntry = nullptr;
  182. UINT32 idx = 0;
  183. for(auto& child : currentDir->mChildren)
  184. {
  185. if(child->type == LibraryEntryType::File && child->path == filePath)
  186. {
  187. existingEntries[idx] = true;
  188. existingEntry = static_cast<ResourceEntry*>(child);
  189. break;
  190. }
  191. idx++;
  192. }
  193. if(existingEntry != nullptr)
  194. {
  195. if (import)
  196. {
  197. reimportResourceInternal(existingEntry);
  198. queueDependantForReimport(existingEntry);
  199. }
  200. if (!isUpToDate(existingEntry))
  201. dirtyResources.push_back(existingEntry->path);
  202. }
  203. else
  204. {
  205. if (import)
  206. addResourceInternal(currentDir, filePath);
  207. dirtyResources.push_back(filePath);
  208. }
  209. }
  210. }
  211. for(auto& dirPath : childDirectories)
  212. {
  213. DirectoryEntry* existingEntry = nullptr;
  214. UINT32 idx = 0;
  215. for(auto& child : currentDir->mChildren)
  216. {
  217. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  218. {
  219. existingEntries[idx] = true;
  220. existingEntry = static_cast<DirectoryEntry*>(child);
  221. break;
  222. }
  223. idx++;
  224. }
  225. if(existingEntry == nullptr)
  226. addDirectoryInternal(currentDir, dirPath);
  227. }
  228. {
  229. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  230. {
  231. if(existingEntries[i])
  232. continue;
  233. toDelete.push_back(currentDir->mChildren[i]);
  234. }
  235. for(auto& child : toDelete)
  236. {
  237. if(child->type == LibraryEntryType::Directory)
  238. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  239. else if(child->type == LibraryEntryType::File)
  240. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  241. }
  242. toDelete.clear();
  243. }
  244. for(auto& child : currentDir->mChildren)
  245. {
  246. if(child->type == LibraryEntryType::Directory)
  247. todo.push(static_cast<DirectoryEntry*>(child));
  248. }
  249. }
  250. }
  251. }
  252. }
  253. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  254. const ImportOptionsPtr& importOptions, bool forceReimport)
  255. {
  256. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  257. parent->mChildren.push_back(newResource);
  258. reimportResourceInternal(newResource, importOptions, forceReimport);
  259. doOnEntryAdded(newResource);
  260. return newResource;
  261. }
  262. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  263. {
  264. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  265. parent->mChildren.push_back(newEntry);
  266. doOnEntryAdded(newEntry);
  267. return newEntry;
  268. }
  269. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  270. {
  271. if(resource->meta != nullptr)
  272. {
  273. Path path;
  274. if(mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  275. {
  276. if(FileSystem::isFile(path))
  277. FileSystem::remove(path);
  278. mResourceManifest->unregisterResource(resource->meta->getUUID());
  279. }
  280. }
  281. DirectoryEntry* parent = resource->parent;
  282. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  283. [&] (const LibraryEntry* entry) { return entry == resource; });
  284. parent->mChildren.erase(findIter);
  285. doOnEntryRemoved(resource);
  286. bs_delete(resource);
  287. }
  288. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  289. {
  290. if(directory == mRootEntry)
  291. mRootEntry = nullptr;
  292. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  293. for(auto& child : childrenToDestroy)
  294. {
  295. if(child->type == LibraryEntryType::Directory)
  296. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  297. else
  298. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  299. }
  300. DirectoryEntry* parent = directory->parent;
  301. if(parent != nullptr)
  302. {
  303. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  304. [&] (const LibraryEntry* entry) { return entry == directory; });
  305. parent->mChildren.erase(findIter);
  306. }
  307. doOnEntryRemoved(directory);
  308. bs_delete(directory);
  309. }
  310. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  311. {
  312. WString ext = resource->path.getWExtension();
  313. Path metaPath = resource->path;
  314. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  315. ext = ext.substr(1, ext.size() - 1); // Remove the .
  316. if (!Importer::instance().supportsFileType(ext))
  317. return;
  318. if(resource->meta == nullptr)
  319. {
  320. if(FileSystem::isFile(metaPath))
  321. {
  322. FileDecoder fs(metaPath);
  323. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  324. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  325. {
  326. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  327. resource->meta = resourceMeta;
  328. }
  329. }
  330. }
  331. if (!isUpToDate(resource) || forceReimport)
  332. {
  333. ImportOptionsPtr curImportOptions = nullptr;
  334. if (importOptions == nullptr)
  335. {
  336. if (resource->meta != nullptr)
  337. curImportOptions = resource->meta->getImportOptions();
  338. else
  339. curImportOptions = Importer::instance().createImportOptions(resource->path);
  340. }
  341. else
  342. curImportOptions = importOptions;
  343. HResource importedResource;
  344. if(resource->meta == nullptr)
  345. {
  346. importedResource = Importer::instance().import(resource->path, curImportOptions);
  347. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  348. UINT32 typeId = importedResource->getTypeId();
  349. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  350. FileEncoder fs(metaPath);
  351. fs.encode(resource->meta.get());
  352. }
  353. else
  354. {
  355. removeDependencies(resource);
  356. importedResource = HResource(resource->meta->getUUID());
  357. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  358. }
  359. addDependencies(resource);
  360. Path internalResourcesPath = mProjectFolder;
  361. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  362. if(!FileSystem::isDirectory(internalResourcesPath))
  363. FileSystem::createDir(internalResourcesPath);
  364. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  365. gResources().save(importedResource, internalResourcesPath, true);
  366. gResources().unload(importedResource);
  367. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  368. resource->lastUpdateTime = std::time(nullptr);
  369. }
  370. }
  371. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  372. {
  373. if(resource->meta == nullptr)
  374. return false;
  375. Path path;
  376. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  377. return false;
  378. if(!FileSystem::isFile(path))
  379. return false;
  380. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  381. return lastModifiedTime <= resource->lastUpdateTime;
  382. }
  383. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  384. {
  385. return search(pattern, {});
  386. }
  387. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  388. {
  389. Vector<LibraryEntry*> foundEntries;
  390. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  391. WString replace(L"\\\\&");
  392. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  393. std::wregex wildcard(L"\\\\\\*");
  394. WString wildcardReplace(L".*");
  395. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  396. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  397. Stack<DirectoryEntry*> todo;
  398. todo.push(mRootEntry);
  399. while (!todo.empty())
  400. {
  401. DirectoryEntry* dirEntry = todo.top();
  402. todo.pop();
  403. for (auto& child : dirEntry->mChildren)
  404. {
  405. if (std::regex_match(child->elementName, searchRegex))
  406. {
  407. if (typeIds.size() == 0)
  408. foundEntries.push_back(child);
  409. else
  410. {
  411. if (child->type == LibraryEntryType::File)
  412. {
  413. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  414. for (auto& typeId : typeIds)
  415. {
  416. if (childResEntry->meta->getTypeID() == typeId)
  417. {
  418. foundEntries.push_back(child);
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. }
  425. if (child->type == LibraryEntryType::Directory)
  426. {
  427. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  428. todo.push(childDirEntry);
  429. }
  430. }
  431. }
  432. std::sort(foundEntries.begin(), foundEntries.end(),
  433. [&](const LibraryEntry* a, const LibraryEntry* b)
  434. {
  435. return a->elementName.compare(b->elementName) < 0;
  436. });
  437. return foundEntries;
  438. }
  439. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  440. {
  441. Path fullPath = path;
  442. if (fullPath.isAbsolute())
  443. {
  444. if (!mResourcesFolder.includes(fullPath))
  445. return nullptr;
  446. }
  447. else
  448. fullPath.makeAbsolute(mResourcesFolder);
  449. Path relPath = fullPath.getRelative(mRootEntry->path);
  450. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  451. UINT32 idx = 0;
  452. LibraryEntry* current = mRootEntry;
  453. while (current != nullptr)
  454. {
  455. if (idx == numElems)
  456. return current;
  457. WString curElem;
  458. if (relPath.isFile() && idx == (numElems - 1))
  459. curElem = relPath.getWFilename();
  460. else
  461. curElem = relPath[idx];
  462. if (current->type == LibraryEntryType::Directory)
  463. {
  464. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  465. current = nullptr;
  466. for (auto& child : dirEntry->mChildren)
  467. {
  468. if (Path::comparePathElem(curElem, child->elementName))
  469. {
  470. idx++;
  471. current = child;
  472. break;
  473. }
  474. }
  475. }
  476. else
  477. break;
  478. }
  479. return nullptr;
  480. }
  481. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  482. {
  483. if (mResourceManifest == nullptr)
  484. return nullptr;
  485. Path filePath;
  486. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  487. return nullptr;
  488. LibraryEntry* libEntry = findEntry(filePath);
  489. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  490. return nullptr;
  491. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  492. return resEntry->meta;
  493. }
  494. Path ProjectLibrary::uuidToPath(const String& uuid) const
  495. {
  496. if (mResourceManifest == nullptr)
  497. return Path();
  498. Path filePath;
  499. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  500. return Path();
  501. return filePath;
  502. }
  503. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  504. {
  505. if (resource == nullptr)
  506. return;
  507. Path assetPath = path;
  508. assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
  509. LibraryEntry* existingEntry = findEntry(assetPath);
  510. if (existingEntry != nullptr)
  511. BS_EXCEPT(InvalidParametersException, "Resource already exists at the specified path: " + assetPath.toString());
  512. Resources::instance().save(resource, assetPath, false);
  513. checkForModifications(assetPath);
  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, false);
  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. HResource ProjectLibrary::load(const Path& path)
  732. {
  733. LibraryEntry* entry = findEntry(path);
  734. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  735. return HResource();
  736. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  737. String resUUID = resEntry->meta->getUUID();
  738. return Resources::instance().loadFromUUID(resUUID);
  739. }
  740. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  741. {
  742. Path parentPath = fullPath;
  743. DirectoryEntry* newEntryParent = nullptr;
  744. Stack<Path> parentPaths;
  745. do
  746. {
  747. Path newParentPath = parentPath.getParent();
  748. if(newParentPath == parentPath)
  749. break;
  750. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  751. if(newEntryParentLib != nullptr)
  752. {
  753. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  754. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  755. break;
  756. }
  757. parentPaths.push(newParentPath);
  758. parentPath = newParentPath;
  759. } while (true);
  760. assert(newEntryParent != nullptr); // Must exist
  761. if(newHierarchyRoot != nullptr)
  762. *newHierarchyRoot = newEntryParent;
  763. while(!parentPaths.empty())
  764. {
  765. Path curPath = parentPaths.top();
  766. parentPaths.pop();
  767. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  768. }
  769. if(newHierarchyLeaf != nullptr)
  770. *newHierarchyLeaf = newEntryParent;
  771. }
  772. Path ProjectLibrary::getMetaPath(const Path& path) const
  773. {
  774. Path metaPath = path;
  775. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  776. return metaPath;
  777. }
  778. bool ProjectLibrary::isMeta(const Path& fullPath) const
  779. {
  780. return fullPath.getWExtension() == L".meta";
  781. }
  782. void ProjectLibrary::save()
  783. {
  784. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  785. Path libraryEntriesPath = mProjectFolder;
  786. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  787. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  788. FileEncoder fs(libraryEntriesPath);
  789. fs.encode(libEntries.get());
  790. Path resourceManifestPath = mProjectFolder;
  791. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  792. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  793. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  794. }
  795. void ProjectLibrary::load()
  796. {
  797. if(mRootEntry != nullptr)
  798. {
  799. deleteDirectoryInternal(mRootEntry);
  800. mRootEntry = nullptr;
  801. }
  802. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  803. Path libraryEntriesPath = mProjectFolder;
  804. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  805. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  806. if(FileSystem::exists(libraryEntriesPath))
  807. {
  808. FileDecoder fs(libraryEntriesPath);
  809. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  810. *mRootEntry = libEntries->getRootEntry();
  811. for(auto& child : mRootEntry->mChildren)
  812. child->parent = mRootEntry;
  813. mRootEntry->parent = nullptr;
  814. }
  815. // Load resource manifest
  816. Path resourceManifestPath = mProjectFolder;
  817. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  818. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  819. if (FileSystem::exists(resourceManifestPath))
  820. {
  821. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  822. }
  823. // Load all meta files
  824. Stack<DirectoryEntry*> todo;
  825. todo.push(mRootEntry);
  826. while(!todo.empty())
  827. {
  828. DirectoryEntry* curDir = todo.top();
  829. todo.pop();
  830. for(auto& child : curDir->mChildren)
  831. {
  832. if(child->type == LibraryEntryType::File)
  833. {
  834. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  835. bool doAddDependencies = true;
  836. if (FileSystem::isFile(resEntry->path))
  837. {
  838. if (resEntry->meta == nullptr)
  839. {
  840. Path metaPath = resEntry->path;
  841. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  842. if (FileSystem::isFile(metaPath))
  843. {
  844. FileDecoder fs(metaPath);
  845. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  846. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  847. {
  848. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  849. resEntry->meta = resourceMeta;
  850. }
  851. }
  852. else
  853. {
  854. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  855. reimportResourceInternal(resEntry);
  856. doAddDependencies = false;
  857. }
  858. }
  859. }
  860. if (doAddDependencies)
  861. addDependencies(resEntry);
  862. }
  863. else if(child->type == LibraryEntryType::Directory)
  864. {
  865. todo.push(static_cast<DirectoryEntry*>(child));
  866. }
  867. }
  868. }
  869. }
  870. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  871. {
  872. if (!onEntryRemoved.empty())
  873. onEntryRemoved(entry->path);
  874. if (entry->type == LibraryEntryType::File)
  875. {
  876. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  877. queueDependantForReimport(resEntry);
  878. removeDependencies(resEntry);
  879. }
  880. }
  881. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  882. {
  883. if (!onEntryAdded.empty())
  884. onEntryAdded(entry->path);
  885. if (entry->type == LibraryEntryType::File)
  886. {
  887. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  888. queueDependantForReimport(resEntry);
  889. }
  890. }
  891. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  892. {
  893. Vector<Path> output;
  894. if (entry->meta == nullptr)
  895. return output;
  896. if (entry->meta->getTypeID() == TID_Shader)
  897. {
  898. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  899. for (auto& include : metaData->includes)
  900. output.push_back(include);
  901. }
  902. return output;
  903. }
  904. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  905. {
  906. Vector<Path> dependencies = getImportDependencies(entry);
  907. for (auto& dependency : dependencies)
  908. mDependencies[dependency].push_back(entry->path);
  909. }
  910. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  911. {
  912. Vector<Path> dependencies = getImportDependencies(entry);
  913. for (auto& dependency : dependencies)
  914. {
  915. Vector<Path>& curDependencies = mDependencies[dependency];
  916. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  917. [&](const Path& x)
  918. {
  919. return x == entry->path;
  920. });
  921. curDependencies.erase(iterRemove, curDependencies.end());
  922. }
  923. }
  924. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  925. {
  926. auto iterFind = mDependencies.find(entry->path);
  927. if (iterFind == mDependencies.end())
  928. return;
  929. for (auto& dependency : iterFind->second)
  930. mReimportQueue.insert(dependency);
  931. }
  932. }