BsProjectLibrary.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. Path parentDirPath = pathToSearch.getParent();
  91. entry = findEntry(parentDirPath);
  92. // Cannot find parent directory. Create the needed hierarchy.
  93. DirectoryEntry* entryParent = nullptr;
  94. DirectoryEntry* newHierarchyParent = nullptr;
  95. if(entry == nullptr)
  96. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  97. else
  98. entryParent = static_cast<DirectoryEntry*>(entry);
  99. if(FileSystem::isFile(pathToSearch))
  100. {
  101. if (import)
  102. addResourceInternal(entryParent, pathToSearch);
  103. dirtyResources.push_back(pathToSearch);
  104. }
  105. else if(FileSystem::isDirectory(pathToSearch))
  106. {
  107. addDirectoryInternal(entryParent, pathToSearch);
  108. checkForModifications(pathToSearch);
  109. }
  110. }
  111. else if(entry->type == LibraryEntryType::File)
  112. {
  113. if(FileSystem::isFile(entry->path))
  114. {
  115. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  116. if (import)
  117. {
  118. reimportResourceInternal(resEntry);
  119. queueDependantForReimport(resEntry);
  120. }
  121. if (!isUpToDate(resEntry))
  122. dirtyResources.push_back(entry->path);
  123. }
  124. else
  125. {
  126. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  127. }
  128. }
  129. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  130. {
  131. if(!FileSystem::isDirectory(entry->path))
  132. {
  133. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  134. }
  135. else
  136. {
  137. Stack<DirectoryEntry*> todo;
  138. todo.push(static_cast<DirectoryEntry*>(entry));
  139. Vector<Path> childFiles;
  140. Vector<Path> childDirectories;
  141. Vector<bool> existingEntries;
  142. Vector<LibraryEntry*> toDelete;
  143. while(!todo.empty())
  144. {
  145. DirectoryEntry* currentDir = todo.top();
  146. todo.pop();
  147. existingEntries.clear();
  148. existingEntries.resize(currentDir->mChildren.size());
  149. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  150. existingEntries[i] = false;
  151. childFiles.clear();
  152. childDirectories.clear();
  153. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  154. for(auto& filePath : childFiles)
  155. {
  156. if(isMeta(filePath))
  157. {
  158. Path sourceFilePath = filePath;
  159. sourceFilePath.setExtension(L"");
  160. if(!FileSystem::isFile(sourceFilePath))
  161. {
  162. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  163. FileSystem::remove(filePath);
  164. }
  165. }
  166. else
  167. {
  168. ResourceEntry* existingEntry = nullptr;
  169. UINT32 idx = 0;
  170. for(auto& child : currentDir->mChildren)
  171. {
  172. if(child->type == LibraryEntryType::File && child->path == filePath)
  173. {
  174. existingEntries[idx] = true;
  175. existingEntry = static_cast<ResourceEntry*>(child);
  176. break;
  177. }
  178. idx++;
  179. }
  180. if(existingEntry != nullptr)
  181. {
  182. if (import)
  183. {
  184. reimportResourceInternal(existingEntry);
  185. queueDependantForReimport(existingEntry);
  186. }
  187. if (!isUpToDate(existingEntry))
  188. dirtyResources.push_back(existingEntry->path);
  189. }
  190. else
  191. {
  192. addResourceInternal(currentDir, filePath);
  193. }
  194. }
  195. }
  196. for(auto& dirPath : childDirectories)
  197. {
  198. DirectoryEntry* existingEntry = nullptr;
  199. UINT32 idx = 0;
  200. for(auto& child : currentDir->mChildren)
  201. {
  202. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  203. {
  204. existingEntries[idx] = true;
  205. existingEntry = static_cast<DirectoryEntry*>(child);
  206. break;
  207. }
  208. idx++;
  209. }
  210. if(existingEntry == nullptr)
  211. addDirectoryInternal(currentDir, dirPath);
  212. }
  213. {
  214. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  215. {
  216. if(existingEntries[i])
  217. continue;
  218. toDelete.push_back(currentDir->mChildren[i]);
  219. }
  220. for(auto& child : toDelete)
  221. {
  222. if(child->type == LibraryEntryType::Directory)
  223. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  224. else if(child->type == LibraryEntryType::File)
  225. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  226. }
  227. }
  228. for(auto& child : currentDir->mChildren)
  229. {
  230. if(child->type == LibraryEntryType::Directory)
  231. todo.push(static_cast<DirectoryEntry*>(child));
  232. }
  233. }
  234. }
  235. }
  236. }
  237. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  238. const ImportOptionsPtr& importOptions, bool forceReimport)
  239. {
  240. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  241. parent->mChildren.push_back(newResource);
  242. reimportResourceInternal(newResource, importOptions, forceReimport);
  243. doOnEntryAdded(newResource);
  244. return newResource;
  245. }
  246. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  247. {
  248. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  249. parent->mChildren.push_back(newEntry);
  250. doOnEntryAdded(newEntry);
  251. return newEntry;
  252. }
  253. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  254. {
  255. if(resource->meta != nullptr)
  256. {
  257. Path path;
  258. if(mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  259. {
  260. if(FileSystem::isFile(path))
  261. FileSystem::remove(path);
  262. mResourceManifest->unregisterResource(resource->meta->getUUID());
  263. }
  264. }
  265. DirectoryEntry* parent = resource->parent;
  266. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  267. [&] (const LibraryEntry* entry) { return entry == resource; });
  268. parent->mChildren.erase(findIter);
  269. doOnEntryRemoved(resource);
  270. bs_delete(resource);
  271. }
  272. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  273. {
  274. if(directory == mRootEntry)
  275. mRootEntry = nullptr;
  276. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  277. for(auto& child : childrenToDestroy)
  278. {
  279. if(child->type == LibraryEntryType::Directory)
  280. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  281. else
  282. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  283. }
  284. DirectoryEntry* parent = directory->parent;
  285. if(parent != nullptr)
  286. {
  287. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  288. [&] (const LibraryEntry* entry) { return entry == directory; });
  289. parent->mChildren.erase(findIter);
  290. }
  291. doOnEntryRemoved(directory);
  292. bs_delete(directory);
  293. }
  294. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  295. {
  296. WString ext = resource->path.getWExtension();
  297. Path metaPath = resource->path;
  298. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  299. ext = ext.substr(1, ext.size() - 1); // Remove the .
  300. if (!Importer::instance().supportsFileType(ext))
  301. return;
  302. if(resource->meta == nullptr)
  303. {
  304. if(FileSystem::isFile(metaPath))
  305. {
  306. FileDecoder fs(metaPath);
  307. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  308. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  309. {
  310. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  311. resource->meta = resourceMeta;
  312. }
  313. }
  314. }
  315. if (!isUpToDate(resource) || forceReimport)
  316. {
  317. ImportOptionsPtr curImportOptions = nullptr;
  318. if (importOptions == nullptr)
  319. {
  320. if (resource->meta != nullptr)
  321. curImportOptions = resource->meta->getImportOptions();
  322. else
  323. curImportOptions = Importer::instance().createImportOptions(resource->path);
  324. }
  325. else
  326. curImportOptions = importOptions;
  327. HResource importedResource;
  328. if(resource->meta == nullptr)
  329. {
  330. importedResource = Importer::instance().import(resource->path, curImportOptions);
  331. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  332. UINT32 typeId = importedResource->getTypeId();
  333. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  334. FileEncoder fs(metaPath);
  335. fs.encode(resource->meta.get());
  336. }
  337. else
  338. {
  339. removeDependencies(resource);
  340. importedResource = HResource(resource->meta->getUUID());
  341. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  342. }
  343. addDependencies(resource);
  344. Path internalResourcesPath = mProjectFolder;
  345. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  346. if(!FileSystem::isDirectory(internalResourcesPath))
  347. FileSystem::createDir(internalResourcesPath);
  348. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  349. gResources().save(importedResource, internalResourcesPath, true);
  350. gResources().unload(importedResource);
  351. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  352. resource->lastUpdateTime = std::time(nullptr);
  353. }
  354. }
  355. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  356. {
  357. if(resource->meta == nullptr)
  358. return false;
  359. Path path;
  360. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  361. return false;
  362. if(!FileSystem::isFile(path))
  363. return false;
  364. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  365. return lastModifiedTime <= resource->lastUpdateTime;
  366. }
  367. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  368. {
  369. return search(pattern, {});
  370. }
  371. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  372. {
  373. Vector<LibraryEntry*> foundEntries;
  374. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  375. WString replace(L"\\\\&");
  376. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  377. std::wregex wildcard(L"\\\\\\*");
  378. WString wildcardReplace(L".*");
  379. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  380. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  381. Stack<DirectoryEntry*> todo;
  382. todo.push(mRootEntry);
  383. while (!todo.empty())
  384. {
  385. DirectoryEntry* dirEntry = todo.top();
  386. todo.pop();
  387. for (auto& child : dirEntry->mChildren)
  388. {
  389. if (std::regex_match(child->elementName, searchRegex))
  390. {
  391. if (typeIds.size() == 0)
  392. foundEntries.push_back(child);
  393. else
  394. {
  395. if (child->type == LibraryEntryType::File)
  396. {
  397. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  398. for (auto& typeId : typeIds)
  399. {
  400. if (childResEntry->meta->getTypeID() == typeId)
  401. {
  402. foundEntries.push_back(child);
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. if (child->type == LibraryEntryType::Directory)
  410. {
  411. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  412. todo.push(childDirEntry);
  413. }
  414. }
  415. }
  416. std::sort(foundEntries.begin(), foundEntries.end(),
  417. [&](const LibraryEntry* a, const LibraryEntry* b)
  418. {
  419. return a->elementName.compare(b->elementName) < 0;
  420. });
  421. return foundEntries;
  422. }
  423. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  424. {
  425. Path fullPath = path;
  426. if (fullPath.isAbsolute())
  427. {
  428. if (!mResourcesFolder.includes(fullPath))
  429. return nullptr;
  430. }
  431. else
  432. fullPath.makeAbsolute(mResourcesFolder);
  433. Path relPath = fullPath.getRelative(mRootEntry->path);
  434. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  435. UINT32 idx = 0;
  436. LibraryEntry* current = mRootEntry;
  437. while (current != nullptr)
  438. {
  439. if (idx == numElems)
  440. return current;
  441. WString curElem;
  442. if (relPath.isFile() && idx == (numElems - 1))
  443. curElem = relPath.getWFilename();
  444. else
  445. curElem = relPath[idx];
  446. if (current->type == LibraryEntryType::Directory)
  447. {
  448. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  449. current = nullptr;
  450. for (auto& child : dirEntry->mChildren)
  451. {
  452. if (Path::comparePathElem(curElem, child->elementName))
  453. {
  454. idx++;
  455. current = child;
  456. break;
  457. }
  458. }
  459. }
  460. else
  461. break;
  462. }
  463. return nullptr;
  464. }
  465. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  466. {
  467. if (mResourceManifest == nullptr)
  468. return nullptr;
  469. Path filePath;
  470. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  471. return nullptr;
  472. LibraryEntry* libEntry = findEntry(filePath);
  473. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  474. return nullptr;
  475. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  476. return resEntry->meta;
  477. }
  478. Path ProjectLibrary::uuidToPath(const String& uuid) const
  479. {
  480. if (mResourceManifest == nullptr)
  481. return Path();
  482. Path filePath;
  483. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  484. return Path();
  485. return filePath;
  486. }
  487. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  488. {
  489. if (resource == nullptr)
  490. return;
  491. Path assetPath = path;
  492. assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
  493. LibraryEntry* existingEntry = findEntry(assetPath);
  494. if (existingEntry != nullptr)
  495. BS_EXCEPT(InvalidParametersException, "Resource already exists at the specified path: " + assetPath.toString());
  496. Resources::instance().save(resource, assetPath, false);
  497. checkForModifications(assetPath);
  498. }
  499. void ProjectLibrary::saveEntry(const HResource& resource)
  500. {
  501. if (resource == nullptr)
  502. return;
  503. Path filePath;
  504. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  505. return;
  506. Resources::instance().save(resource, filePath, false);
  507. }
  508. void ProjectLibrary::createFolderEntry(const Path& path)
  509. {
  510. Path fullPath = path;
  511. if (fullPath.isAbsolute())
  512. {
  513. if (!mResourcesFolder.includes(fullPath))
  514. return;
  515. }
  516. else
  517. fullPath.makeAbsolute(mResourcesFolder);
  518. if (FileSystem::isDirectory(fullPath))
  519. return; // Already exists
  520. FileSystem::createDir(fullPath);
  521. Path parentPath = fullPath.getParent();
  522. DirectoryEntry* newEntryParent = nullptr;
  523. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  524. if (newEntryParentLib != nullptr)
  525. {
  526. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  527. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  528. }
  529. DirectoryEntry* newHierarchyParent = nullptr;
  530. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  531. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  532. addDirectoryInternal(newEntryParent, fullPath);
  533. }
  534. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  535. {
  536. Path oldFullPath = oldPath;
  537. if (!oldFullPath.isAbsolute())
  538. oldFullPath.makeAbsolute(mResourcesFolder);
  539. Path newFullPath = newPath;
  540. if (!newFullPath.isAbsolute())
  541. newFullPath.makeAbsolute(mResourcesFolder);
  542. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  543. FileSystem::move(oldFullPath, newFullPath, overwrite);
  544. Path oldMetaPath = getMetaPath(oldFullPath);
  545. Path newMetaPath = getMetaPath(newFullPath);
  546. LibraryEntry* oldEntry = findEntry(oldFullPath);
  547. if(oldEntry != nullptr) // Moving from the Resources folder
  548. {
  549. // Moved outside of Resources, delete entry & meta file
  550. if (!mResourcesFolder.includes(newFullPath))
  551. {
  552. if(oldEntry->type == LibraryEntryType::File)
  553. {
  554. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  555. if(FileSystem::isFile(oldMetaPath))
  556. FileSystem::remove(oldMetaPath);
  557. }
  558. else if(oldEntry->type == LibraryEntryType::Directory)
  559. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  560. }
  561. else // Just moving internally
  562. {
  563. doOnEntryRemoved(oldEntry);
  564. if(FileSystem::isFile(oldMetaPath))
  565. FileSystem::move(oldMetaPath, newMetaPath);
  566. DirectoryEntry* parent = oldEntry->parent;
  567. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  568. if(findIter != parent->mChildren.end())
  569. parent->mChildren.erase(findIter);
  570. Path parentPath = newFullPath.getParent();
  571. DirectoryEntry* newEntryParent = nullptr;
  572. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  573. if(newEntryParentLib != nullptr)
  574. {
  575. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  576. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  577. }
  578. DirectoryEntry* newHierarchyParent = nullptr;
  579. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  580. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  581. newEntryParent->mChildren.push_back(oldEntry);
  582. oldEntry->parent = newEntryParent;
  583. oldEntry->path = newFullPath;
  584. oldEntry->elementName = newFullPath.getWTail();
  585. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  586. {
  587. Stack<LibraryEntry*> todo;
  588. todo.push(oldEntry);
  589. while(!todo.empty())
  590. {
  591. LibraryEntry* curEntry = todo.top();
  592. todo.pop();
  593. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  594. for(auto& child : curDirEntry->mChildren)
  595. {
  596. child->path = child->parent->path;
  597. child->path.append(child->elementName);
  598. if(child->type == LibraryEntryType::Directory)
  599. todo.push(child);
  600. }
  601. }
  602. }
  603. doOnEntryAdded(oldEntry);
  604. }
  605. }
  606. else // Moving from outside of the Resources folder (likely adding a new resource)
  607. {
  608. checkForModifications(newFullPath);
  609. }
  610. }
  611. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  612. {
  613. Path oldFullPath = oldPath;
  614. if (!oldFullPath.isAbsolute())
  615. oldFullPath.makeAbsolute(mResourcesFolder);
  616. Path newFullPath = newPath;
  617. if (!newFullPath.isAbsolute())
  618. newFullPath.makeAbsolute(mResourcesFolder);
  619. if (!FileSystem::exists(oldFullPath))
  620. return;
  621. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  622. // Copying a file/folder outside of the Resources folder, no special handling needed
  623. if (!mResourcesFolder.includes(newFullPath))
  624. return;
  625. Path parentPath = newFullPath.getParent();
  626. DirectoryEntry* newEntryParent = nullptr;
  627. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  628. if (newEntryParentLib != nullptr)
  629. {
  630. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  631. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  632. }
  633. // If the source is outside of Resources folder, just plain import the copy
  634. LibraryEntry* oldEntry = findEntry(oldFullPath);
  635. if (oldEntry == nullptr)
  636. {
  637. checkForModifications(newFullPath);
  638. return;
  639. }
  640. // Both source and destination are within Resources folder, need to preserve import options on the copies
  641. LibraryEntry* newEntry = nullptr;
  642. if (FileSystem::isFile(newFullPath))
  643. {
  644. assert(oldEntry->type == LibraryEntryType::File);
  645. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  646. newEntry = addResourceInternal(newEntryParent, newFullPath, oldResEntry->meta->getImportOptions(), true);
  647. }
  648. else
  649. {
  650. assert(oldEntry->type == LibraryEntryType::File);
  651. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  652. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  653. newEntry = newDirEntry;
  654. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  655. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  656. while (!todo.empty())
  657. {
  658. auto current = todo.top();
  659. todo.pop();
  660. DirectoryEntry* sourceDir = std::get<0>(current);
  661. DirectoryEntry* destDir = std::get<1>(current);
  662. for (auto& child : sourceDir->mChildren)
  663. {
  664. Path childDestPath = destDir->path;
  665. childDestPath.append(child->path.getWTail());
  666. if (child->type == LibraryEntryType::File)
  667. {
  668. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  669. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  670. }
  671. else // Directory
  672. {
  673. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  674. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  675. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  676. }
  677. }
  678. }
  679. }
  680. }
  681. void ProjectLibrary::deleteEntry(const Path& path)
  682. {
  683. Path fullPath = path;
  684. if (!fullPath.isAbsolute())
  685. fullPath.makeAbsolute(mResourcesFolder);
  686. if(FileSystem::exists(fullPath))
  687. FileSystem::remove(fullPath);
  688. LibraryEntry* entry = findEntry(fullPath);
  689. if(entry != nullptr)
  690. {
  691. if(entry->type == LibraryEntryType::File)
  692. {
  693. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  694. Path metaPath = getMetaPath(fullPath);
  695. if(FileSystem::isFile(metaPath))
  696. FileSystem::remove(metaPath);
  697. }
  698. else if(entry->type == LibraryEntryType::Directory)
  699. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  700. }
  701. }
  702. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  703. {
  704. LibraryEntry* entry = findEntry(path);
  705. if (entry != nullptr)
  706. {
  707. if (entry->type == LibraryEntryType::File)
  708. {
  709. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  710. reimportResourceInternal(resEntry, importOptions, forceReimport);
  711. queueDependantForReimport(resEntry);
  712. }
  713. }
  714. }
  715. HResource ProjectLibrary::load(const Path& path)
  716. {
  717. LibraryEntry* entry = findEntry(path);
  718. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  719. return HResource();
  720. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  721. String resUUID = resEntry->meta->getUUID();
  722. return Resources::instance().loadFromUUID(resUUID);
  723. }
  724. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  725. {
  726. Path parentPath = fullPath;
  727. DirectoryEntry* newEntryParent = nullptr;
  728. Stack<Path> parentPaths;
  729. do
  730. {
  731. Path newParentPath = parentPath.getParent();
  732. if(newParentPath == parentPath)
  733. break;
  734. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  735. if(newEntryParentLib != nullptr)
  736. {
  737. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  738. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  739. break;
  740. }
  741. parentPaths.push(newParentPath);
  742. parentPath = newParentPath;
  743. } while (true);
  744. assert(newEntryParent != nullptr); // Must exist
  745. if(newHierarchyRoot != nullptr)
  746. *newHierarchyRoot = newEntryParent;
  747. while(!parentPaths.empty())
  748. {
  749. Path curPath = parentPaths.top();
  750. parentPaths.pop();
  751. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  752. }
  753. if(newHierarchyLeaf != nullptr)
  754. *newHierarchyLeaf = newEntryParent;
  755. }
  756. Path ProjectLibrary::getMetaPath(const Path& path) const
  757. {
  758. Path metaPath = path;
  759. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  760. return metaPath;
  761. }
  762. bool ProjectLibrary::isMeta(const Path& fullPath) const
  763. {
  764. return fullPath.getWExtension() == L".meta";
  765. }
  766. void ProjectLibrary::save()
  767. {
  768. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  769. Path libraryEntriesPath = mProjectFolder;
  770. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  771. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  772. FileEncoder fs(libraryEntriesPath);
  773. fs.encode(libEntries.get());
  774. Path resourceManifestPath = mProjectFolder;
  775. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  776. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  777. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  778. }
  779. void ProjectLibrary::load()
  780. {
  781. if(mRootEntry != nullptr)
  782. {
  783. deleteDirectoryInternal(mRootEntry);
  784. mRootEntry = nullptr;
  785. }
  786. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  787. Path libraryEntriesPath = mProjectFolder;
  788. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  789. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  790. if(FileSystem::exists(libraryEntriesPath))
  791. {
  792. FileDecoder fs(libraryEntriesPath);
  793. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  794. *mRootEntry = libEntries->getRootEntry();
  795. for(auto& child : mRootEntry->mChildren)
  796. child->parent = mRootEntry;
  797. mRootEntry->parent = nullptr;
  798. }
  799. // Load all meta files
  800. Stack<DirectoryEntry*> todo;
  801. todo.push(mRootEntry);
  802. while(!todo.empty())
  803. {
  804. DirectoryEntry* curDir = todo.top();
  805. todo.pop();
  806. for(auto& child : curDir->mChildren)
  807. {
  808. if(child->type == LibraryEntryType::File)
  809. {
  810. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  811. bool doAddDependencies = true;
  812. if (FileSystem::isFile(resEntry->path))
  813. {
  814. if (resEntry->meta == nullptr)
  815. {
  816. Path metaPath = resEntry->path;
  817. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  818. if (FileSystem::isFile(metaPath))
  819. {
  820. FileDecoder fs(metaPath);
  821. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  822. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  823. {
  824. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  825. resEntry->meta = resourceMeta;
  826. }
  827. }
  828. else
  829. {
  830. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  831. reimportResourceInternal(resEntry);
  832. doAddDependencies = false;
  833. }
  834. }
  835. }
  836. if (doAddDependencies)
  837. addDependencies(resEntry);
  838. }
  839. else if(child->type == LibraryEntryType::Directory)
  840. {
  841. todo.push(static_cast<DirectoryEntry*>(child));
  842. }
  843. }
  844. }
  845. // Load resource manifest
  846. Path resourceManifestPath = mProjectFolder;
  847. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  848. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  849. if(FileSystem::exists(resourceManifestPath))
  850. {
  851. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  852. }
  853. }
  854. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  855. {
  856. if (!onEntryRemoved.empty())
  857. onEntryRemoved(entry->path);
  858. if (entry->type == LibraryEntryType::File)
  859. {
  860. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  861. queueDependantForReimport(resEntry);
  862. removeDependencies(resEntry);
  863. }
  864. }
  865. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  866. {
  867. if (!onEntryAdded.empty())
  868. onEntryAdded(entry->path);
  869. if (entry->type == LibraryEntryType::File)
  870. {
  871. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  872. queueDependantForReimport(resEntry);
  873. }
  874. }
  875. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  876. {
  877. Vector<Path> output;
  878. if (entry->meta == nullptr)
  879. return output;
  880. if (entry->meta->getTypeID() == TID_Shader)
  881. {
  882. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  883. for (auto& include : metaData->includes)
  884. output.push_back(include);
  885. }
  886. return output;
  887. }
  888. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  889. {
  890. Vector<Path> dependencies = getImportDependencies(entry);
  891. for (auto& dependency : dependencies)
  892. mDependencies[dependency].push_back(entry->path);
  893. }
  894. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  895. {
  896. Vector<Path> dependencies = getImportDependencies(entry);
  897. for (auto& dependency : dependencies)
  898. {
  899. Vector<Path>& curDependencies = mDependencies[dependency];
  900. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  901. [&](const Path& x)
  902. {
  903. return x == entry->path;
  904. });
  905. curDependencies.erase(iterRemove, curDependencies.end());
  906. }
  907. }
  908. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  909. {
  910. auto iterFind = mDependencies.find(entry->path);
  911. if (iterFind == mDependencies.end())
  912. return;
  913. for (auto& dependency : iterFind->second)
  914. mReimportQueue.insert(dependency);
  915. }
  916. }