BsProjectLibrary.cpp 36 KB

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