BsProjectLibrary.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. #include "BsProjectLibrary.h"
  2. #include "BsFileSystem.h"
  3. #include "BsException.h"
  4. #include "BsResources.h"
  5. #include "BsResourceManifest.h"
  6. #include "BsImporter.h"
  7. #include "BsProjectResourceMeta.h"
  8. #include "BsResources.h"
  9. #include "BsImporter.h"
  10. #include "BsImportOptions.h"
  11. #include "BsFileSerializer.h"
  12. #include "BsDebug.h"
  13. #include "BsProjectLibraryEntries.h"
  14. #include "BsResource.h"
  15. #include "BsEditorApplication.h"
  16. #include "BsResourceImporter.h"
  17. #include "BsShader.h"
  18. #include <regex>
  19. using namespace std::placeholders;
  20. namespace BansheeEngine
  21. {
  22. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  23. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + L"Resources\\";
  24. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  25. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  26. ProjectLibrary::LibraryEntry::LibraryEntry()
  27. :parent(nullptr), type(LibraryEntryType::Directory)
  28. { }
  29. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  30. :path(path), parent(parent), type(type), elementName(name)
  31. { }
  32. ProjectLibrary::ResourceEntry::ResourceEntry()
  33. : lastUpdateTime(0)
  34. { }
  35. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  36. : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  37. { }
  38. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  39. { }
  40. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  41. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  42. { }
  43. ProjectLibrary::ProjectLibrary()
  44. : mRootEntry(nullptr), mIsLoaded(false)
  45. {
  46. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  47. }
  48. ProjectLibrary::~ProjectLibrary()
  49. {
  50. clearEntries();
  51. }
  52. void ProjectLibrary::update()
  53. {
  54. while (!mReimportQueue.empty())
  55. {
  56. Path toReimport = *mReimportQueue.begin();
  57. LibraryEntry* entry = findEntry(toReimport);
  58. if (entry != nullptr && entry->type == LibraryEntryType::File)
  59. {
  60. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  61. reimportResourceInternal(resEntry, resEntry->meta->getImportOptions(), true);
  62. queueDependantForReimport(resEntry);
  63. }
  64. mReimportQueue.erase(mReimportQueue.begin());
  65. }
  66. }
  67. void ProjectLibrary::checkForModifications(const Path& fullPath)
  68. {
  69. Vector<Path> dirtyResources;
  70. checkForModifications(fullPath, true, dirtyResources);
  71. }
  72. void ProjectLibrary::checkForModifications(const Path& fullPath, bool import, Vector<Path>& dirtyResources)
  73. {
  74. if (!mResourcesFolder.includes(fullPath))
  75. return; // Folder not part of our resources path, so no modifications
  76. if(mRootEntry == nullptr)
  77. {
  78. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  79. }
  80. Path pathToSearch = fullPath;
  81. LibraryEntry* entry = findEntry(pathToSearch);
  82. if (entry == nullptr) // File could be new, try to find parent directory entry
  83. {
  84. if (FileSystem::exists(pathToSearch))
  85. {
  86. if (isMeta(pathToSearch))
  87. {
  88. Path sourceFilePath = pathToSearch;
  89. sourceFilePath.setExtension(L"");
  90. if (!FileSystem::isFile(sourceFilePath))
  91. {
  92. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  93. FileSystem::remove(pathToSearch);
  94. }
  95. }
  96. else
  97. {
  98. Path parentDirPath = pathToSearch.getParent();
  99. entry = findEntry(parentDirPath);
  100. // Cannot find parent directory. Create the needed hierarchy.
  101. DirectoryEntry* entryParent = nullptr;
  102. DirectoryEntry* newHierarchyParent = nullptr;
  103. if (entry == nullptr)
  104. createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
  105. else
  106. entryParent = static_cast<DirectoryEntry*>(entry);
  107. if (FileSystem::isFile(pathToSearch))
  108. {
  109. if (import)
  110. addResourceInternal(entryParent, pathToSearch);
  111. dirtyResources.push_back(pathToSearch);
  112. }
  113. else if (FileSystem::isDirectory(pathToSearch))
  114. {
  115. addDirectoryInternal(entryParent, pathToSearch);
  116. checkForModifications(pathToSearch);
  117. }
  118. }
  119. }
  120. }
  121. else if(entry->type == LibraryEntryType::File)
  122. {
  123. if(FileSystem::isFile(entry->path))
  124. {
  125. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  126. if (import)
  127. {
  128. reimportResourceInternal(resEntry);
  129. queueDependantForReimport(resEntry);
  130. }
  131. if (!isUpToDate(resEntry))
  132. dirtyResources.push_back(entry->path);
  133. }
  134. else
  135. {
  136. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  137. }
  138. }
  139. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  140. {
  141. if(!FileSystem::isDirectory(entry->path))
  142. {
  143. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  144. }
  145. else
  146. {
  147. Stack<DirectoryEntry*> todo;
  148. todo.push(static_cast<DirectoryEntry*>(entry));
  149. Vector<Path> childFiles;
  150. Vector<Path> childDirectories;
  151. Vector<bool> existingEntries;
  152. Vector<LibraryEntry*> toDelete;
  153. while(!todo.empty())
  154. {
  155. DirectoryEntry* currentDir = todo.top();
  156. todo.pop();
  157. existingEntries.clear();
  158. existingEntries.resize(currentDir->mChildren.size());
  159. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  160. existingEntries[i] = false;
  161. childFiles.clear();
  162. childDirectories.clear();
  163. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  164. for(auto& filePath : childFiles)
  165. {
  166. if(isMeta(filePath))
  167. {
  168. Path sourceFilePath = filePath;
  169. sourceFilePath.setExtension(L"");
  170. if(!FileSystem::isFile(sourceFilePath))
  171. {
  172. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  173. FileSystem::remove(filePath);
  174. }
  175. }
  176. else
  177. {
  178. ResourceEntry* existingEntry = nullptr;
  179. UINT32 idx = 0;
  180. for(auto& child : currentDir->mChildren)
  181. {
  182. if(child->type == LibraryEntryType::File && child->path == filePath)
  183. {
  184. existingEntries[idx] = true;
  185. existingEntry = static_cast<ResourceEntry*>(child);
  186. break;
  187. }
  188. idx++;
  189. }
  190. if(existingEntry != nullptr)
  191. {
  192. if (import)
  193. {
  194. reimportResourceInternal(existingEntry);
  195. queueDependantForReimport(existingEntry);
  196. }
  197. if (!isUpToDate(existingEntry))
  198. dirtyResources.push_back(existingEntry->path);
  199. }
  200. else
  201. {
  202. if (import)
  203. addResourceInternal(currentDir, filePath);
  204. dirtyResources.push_back(filePath);
  205. }
  206. }
  207. }
  208. for(auto& dirPath : childDirectories)
  209. {
  210. DirectoryEntry* existingEntry = nullptr;
  211. UINT32 idx = 0;
  212. for(auto& child : currentDir->mChildren)
  213. {
  214. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  215. {
  216. existingEntries[idx] = true;
  217. existingEntry = static_cast<DirectoryEntry*>(child);
  218. break;
  219. }
  220. idx++;
  221. }
  222. if(existingEntry == nullptr)
  223. addDirectoryInternal(currentDir, dirPath);
  224. }
  225. {
  226. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  227. {
  228. if(existingEntries[i])
  229. continue;
  230. toDelete.push_back(currentDir->mChildren[i]);
  231. }
  232. for(auto& child : toDelete)
  233. {
  234. if(child->type == LibraryEntryType::Directory)
  235. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  236. else if(child->type == LibraryEntryType::File)
  237. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  238. }
  239. toDelete.clear();
  240. }
  241. for(auto& child : currentDir->mChildren)
  242. {
  243. if(child->type == LibraryEntryType::Directory)
  244. todo.push(static_cast<DirectoryEntry*>(child));
  245. }
  246. }
  247. }
  248. }
  249. }
  250. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  251. const ImportOptionsPtr& importOptions, bool forceReimport)
  252. {
  253. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  254. parent->mChildren.push_back(newResource);
  255. reimportResourceInternal(newResource, importOptions, forceReimport);
  256. doOnEntryAdded(newResource);
  257. return newResource;
  258. }
  259. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  260. {
  261. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  262. parent->mChildren.push_back(newEntry);
  263. doOnEntryAdded(newEntry);
  264. return newEntry;
  265. }
  266. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  267. {
  268. if(resource->meta != nullptr)
  269. {
  270. Path path;
  271. if(mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  272. {
  273. if(FileSystem::isFile(path))
  274. FileSystem::remove(path);
  275. mResourceManifest->unregisterResource(resource->meta->getUUID());
  276. }
  277. }
  278. DirectoryEntry* parent = resource->parent;
  279. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  280. [&] (const LibraryEntry* entry) { return entry == resource; });
  281. parent->mChildren.erase(findIter);
  282. doOnEntryRemoved(resource);
  283. bs_delete(resource);
  284. }
  285. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  286. {
  287. if(directory == mRootEntry)
  288. mRootEntry = nullptr;
  289. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  290. for(auto& child : childrenToDestroy)
  291. {
  292. if(child->type == LibraryEntryType::Directory)
  293. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  294. else
  295. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  296. }
  297. DirectoryEntry* parent = directory->parent;
  298. if(parent != nullptr)
  299. {
  300. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  301. [&] (const LibraryEntry* entry) { return entry == directory; });
  302. parent->mChildren.erase(findIter);
  303. }
  304. doOnEntryRemoved(directory);
  305. bs_delete(directory);
  306. }
  307. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  308. {
  309. WString ext = resource->path.getWExtension();
  310. Path metaPath = resource->path;
  311. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  312. if (ext.size() > 0)
  313. ext = ext.substr(1, ext.size() - 1); // Remove the .
  314. if (!Importer::instance().supportsFileType(ext))
  315. return;
  316. if(resource->meta == nullptr)
  317. {
  318. if(FileSystem::isFile(metaPath))
  319. {
  320. FileDecoder fs(metaPath);
  321. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  322. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  323. {
  324. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  325. resource->meta = resourceMeta;
  326. }
  327. }
  328. }
  329. if (!isUpToDate(resource) || forceReimport)
  330. {
  331. ImportOptionsPtr curImportOptions = nullptr;
  332. if (importOptions == nullptr)
  333. {
  334. if (resource->meta != nullptr)
  335. curImportOptions = resource->meta->getImportOptions();
  336. else
  337. curImportOptions = Importer::instance().createImportOptions(resource->path);
  338. }
  339. else
  340. curImportOptions = importOptions;
  341. HResource importedResource;
  342. if(resource->meta == nullptr)
  343. {
  344. importedResource = Importer::instance().import(resource->path, curImportOptions);
  345. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  346. UINT32 typeId = importedResource->getTypeId();
  347. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  348. FileEncoder fs(metaPath);
  349. fs.encode(resource->meta.get());
  350. }
  351. else
  352. {
  353. removeDependencies(resource);
  354. importedResource = HResource(resource->meta->getUUID());
  355. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  356. }
  357. addDependencies(resource);
  358. Path internalResourcesPath = mProjectFolder;
  359. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  360. if(!FileSystem::isDirectory(internalResourcesPath))
  361. FileSystem::createDir(internalResourcesPath);
  362. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  363. gResources().save(importedResource, internalResourcesPath, true);
  364. gResources().unload(importedResource);
  365. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  366. resource->lastUpdateTime = std::time(nullptr);
  367. }
  368. }
  369. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  370. {
  371. if(resource->meta == nullptr)
  372. return false;
  373. Path path;
  374. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  375. return false;
  376. if(!FileSystem::isFile(path))
  377. return false;
  378. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  379. return lastModifiedTime <= resource->lastUpdateTime;
  380. }
  381. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  382. {
  383. return search(pattern, {});
  384. }
  385. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  386. {
  387. Vector<LibraryEntry*> foundEntries;
  388. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  389. WString replace(L"\\\\&");
  390. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  391. std::wregex wildcard(L"\\\\\\*");
  392. WString wildcardReplace(L".*");
  393. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  394. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  395. Stack<DirectoryEntry*> todo;
  396. todo.push(mRootEntry);
  397. while (!todo.empty())
  398. {
  399. DirectoryEntry* dirEntry = todo.top();
  400. todo.pop();
  401. for (auto& child : dirEntry->mChildren)
  402. {
  403. if (std::regex_match(child->elementName, searchRegex))
  404. {
  405. if (typeIds.size() == 0)
  406. foundEntries.push_back(child);
  407. else
  408. {
  409. if (child->type == LibraryEntryType::File)
  410. {
  411. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  412. for (auto& typeId : typeIds)
  413. {
  414. if (childResEntry->meta->getTypeID() == typeId)
  415. {
  416. foundEntries.push_back(child);
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. }
  423. if (child->type == LibraryEntryType::Directory)
  424. {
  425. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  426. todo.push(childDirEntry);
  427. }
  428. }
  429. }
  430. std::sort(foundEntries.begin(), foundEntries.end(),
  431. [&](const LibraryEntry* a, const LibraryEntry* b)
  432. {
  433. return a->elementName.compare(b->elementName) < 0;
  434. });
  435. return foundEntries;
  436. }
  437. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  438. {
  439. Path fullPath = path;
  440. if (fullPath.isAbsolute())
  441. {
  442. if (!mResourcesFolder.includes(fullPath))
  443. return nullptr;
  444. }
  445. else
  446. fullPath.makeAbsolute(mResourcesFolder);
  447. Path relPath = fullPath.getRelative(mRootEntry->path);
  448. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  449. UINT32 idx = 0;
  450. LibraryEntry* current = mRootEntry;
  451. while (current != nullptr)
  452. {
  453. if (idx == numElems)
  454. return current;
  455. WString curElem;
  456. if (relPath.isFile() && idx == (numElems - 1))
  457. curElem = relPath.getWFilename();
  458. else
  459. curElem = relPath[idx];
  460. if (current->type == LibraryEntryType::Directory)
  461. {
  462. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  463. current = nullptr;
  464. for (auto& child : dirEntry->mChildren)
  465. {
  466. if (Path::comparePathElem(curElem, child->elementName))
  467. {
  468. idx++;
  469. current = child;
  470. break;
  471. }
  472. }
  473. }
  474. else
  475. break;
  476. }
  477. return nullptr;
  478. }
  479. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  480. {
  481. if (mResourceManifest == nullptr)
  482. return nullptr;
  483. Path filePath;
  484. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  485. return nullptr;
  486. LibraryEntry* libEntry = findEntry(filePath);
  487. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  488. return nullptr;
  489. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  490. return resEntry->meta;
  491. }
  492. Path ProjectLibrary::uuidToPath(const String& uuid) const
  493. {
  494. if (mResourceManifest == nullptr)
  495. return Path();
  496. Path filePath;
  497. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  498. return Path();
  499. return filePath;
  500. }
  501. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  502. {
  503. if (resource == nullptr)
  504. return;
  505. Path assetPath = path;
  506. if (path.isAbsolute())
  507. {
  508. if (!getResourcesFolder().includes(path))
  509. return;
  510. assetPath = path.getRelative(getResourcesFolder());
  511. }
  512. LibraryEntry* existingEntry = findEntry(assetPath);
  513. if (existingEntry != nullptr)
  514. {
  515. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  516. return;
  517. }
  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::unloadLibrary()
  826. {
  827. if (!mIsLoaded)
  828. return;
  829. mProjectFolder = Path::BLANK;
  830. mResourcesFolder = Path::BLANK;
  831. clearEntries();
  832. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  833. mReimportQueue.clear();
  834. mDependencies.clear();
  835. gResources().unregisterResourceManifest(mResourceManifest);
  836. mResourceManifest = nullptr;
  837. mIsLoaded = false;
  838. }
  839. void ProjectLibrary::makeEntriesRelative()
  840. {
  841. // Make all paths relative before saving
  842. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  843. [&](LibraryEntry* entry, const Path& root)
  844. {
  845. entry->path.makeRelative(root);
  846. if (entry->type == LibraryEntryType::Directory)
  847. {
  848. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  849. for (auto& child : dirEntry->mChildren)
  850. makeRelative(child, root);
  851. }
  852. };
  853. Path root = getResourcesFolder();
  854. makeRelative(mRootEntry, root);
  855. }
  856. void ProjectLibrary::makeEntriesAbsolute()
  857. {
  858. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  859. [&](LibraryEntry* entry, const Path& root)
  860. {
  861. entry->path.makeAbsolute(root);
  862. if (entry->type == LibraryEntryType::Directory)
  863. {
  864. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  865. for (auto& child : dirEntry->mChildren)
  866. makeAbsolute(child, root);
  867. }
  868. };
  869. Path root = getResourcesFolder();
  870. makeAbsolute(mRootEntry, root);
  871. }
  872. void ProjectLibrary::saveLibrary()
  873. {
  874. if (!mIsLoaded)
  875. return;
  876. // Make all paths relative before saving
  877. makeEntriesRelative();
  878. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  879. Path libraryEntriesPath = mProjectFolder;
  880. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  881. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  882. FileEncoder fs(libraryEntriesPath);
  883. fs.encode(libEntries.get());
  884. // Restore absolute entry paths
  885. makeEntriesAbsolute();
  886. Path resourceManifestPath = mProjectFolder;
  887. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  888. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  889. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  890. }
  891. void ProjectLibrary::loadLibrary()
  892. {
  893. unloadLibrary();
  894. mProjectFolder = gEditorApplication().getProjectPath();
  895. mResourcesFolder = mProjectFolder;
  896. mResourcesFolder.append(RESOURCES_DIR);
  897. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  898. Path libraryEntriesPath = mProjectFolder;
  899. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  900. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  901. if(FileSystem::exists(libraryEntriesPath))
  902. {
  903. FileDecoder fs(libraryEntriesPath);
  904. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  905. *mRootEntry = libEntries->getRootEntry();
  906. for(auto& child : mRootEntry->mChildren)
  907. child->parent = mRootEntry;
  908. mRootEntry->parent = nullptr;
  909. }
  910. // Entries are stored relative to project folder, but we want their absolute paths now
  911. makeEntriesAbsolute();
  912. // Load resource manifest
  913. Path resourceManifestPath = mProjectFolder;
  914. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  915. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  916. if (FileSystem::exists(resourceManifestPath))
  917. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  918. else
  919. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  920. gResources().registerResourceManifest(mResourceManifest);
  921. // Load all meta files
  922. Stack<DirectoryEntry*> todo;
  923. todo.push(mRootEntry);
  924. while(!todo.empty())
  925. {
  926. DirectoryEntry* curDir = todo.top();
  927. todo.pop();
  928. for(auto& child : curDir->mChildren)
  929. {
  930. if(child->type == LibraryEntryType::File)
  931. {
  932. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  933. bool doAddDependencies = true;
  934. if (FileSystem::isFile(resEntry->path))
  935. {
  936. if (resEntry->meta == nullptr)
  937. {
  938. Path metaPath = resEntry->path;
  939. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  940. if (FileSystem::isFile(metaPath))
  941. {
  942. FileDecoder fs(metaPath);
  943. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  944. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  945. {
  946. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  947. resEntry->meta = resourceMeta;
  948. }
  949. }
  950. else
  951. {
  952. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  953. reimportResourceInternal(resEntry);
  954. doAddDependencies = false;
  955. }
  956. }
  957. }
  958. if (doAddDependencies)
  959. addDependencies(resEntry);
  960. }
  961. else if(child->type == LibraryEntryType::Directory)
  962. {
  963. todo.push(static_cast<DirectoryEntry*>(child));
  964. }
  965. }
  966. }
  967. mIsLoaded = true;
  968. }
  969. void ProjectLibrary::clearEntries()
  970. {
  971. if (mRootEntry == nullptr)
  972. return;
  973. std::function<void(LibraryEntry*)> deleteRecursive =
  974. [&](LibraryEntry* entry)
  975. {
  976. if (entry->type == LibraryEntryType::Directory)
  977. {
  978. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  979. for (auto& child : dirEntry->mChildren)
  980. deleteRecursive(child);
  981. }
  982. bs_delete(entry);
  983. };
  984. deleteRecursive(mRootEntry);
  985. mRootEntry = nullptr;
  986. }
  987. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  988. {
  989. if (!onEntryRemoved.empty())
  990. onEntryRemoved(entry->path);
  991. if (entry->type == LibraryEntryType::File)
  992. {
  993. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  994. queueDependantForReimport(resEntry);
  995. removeDependencies(resEntry);
  996. }
  997. }
  998. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  999. {
  1000. if (!onEntryAdded.empty())
  1001. onEntryAdded(entry->path);
  1002. if (entry->type == LibraryEntryType::File)
  1003. {
  1004. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  1005. queueDependantForReimport(resEntry);
  1006. }
  1007. }
  1008. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1009. {
  1010. Vector<Path> output;
  1011. if (entry->meta == nullptr)
  1012. return output;
  1013. if (entry->meta->getTypeID() == TID_Shader)
  1014. {
  1015. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1016. for (auto& include : metaData->includes)
  1017. output.push_back(include);
  1018. }
  1019. return output;
  1020. }
  1021. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1022. {
  1023. Vector<Path> dependencies = getImportDependencies(entry);
  1024. for (auto& dependency : dependencies)
  1025. mDependencies[dependency].push_back(entry->path);
  1026. }
  1027. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1028. {
  1029. Vector<Path> dependencies = getImportDependencies(entry);
  1030. for (auto& dependency : dependencies)
  1031. {
  1032. Vector<Path>& curDependencies = mDependencies[dependency];
  1033. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1034. [&](const Path& x)
  1035. {
  1036. return x == entry->path;
  1037. });
  1038. curDependencies.erase(iterRemove, curDependencies.end());
  1039. }
  1040. }
  1041. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  1042. {
  1043. auto iterFind = mDependencies.find(entry->path);
  1044. if (iterFind == mDependencies.end())
  1045. return;
  1046. for (auto& dependency : iterFind->second)
  1047. mReimportQueue.insert(dependency);
  1048. }
  1049. }