BsProjectLibrary.cpp 34 KB

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