BsProjectLibrary.cpp 36 KB

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