BsProjectLibrary.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 = L"Internal\\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::saveLibrary()
  838. {
  839. if (!mIsLoaded)
  840. return;
  841. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  842. Path libraryEntriesPath = mProjectFolder;
  843. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  844. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  845. FileEncoder fs(libraryEntriesPath);
  846. fs.encode(libEntries.get());
  847. Path resourceManifestPath = mProjectFolder;
  848. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  849. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  850. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  851. }
  852. void ProjectLibrary::loadLibrary()
  853. {
  854. unloadLibrary();
  855. mProjectFolder = gEditorApplication().getProjectPath();
  856. mResourcesFolder = mProjectFolder;
  857. mResourcesFolder.append(RESOURCES_DIR);
  858. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  859. Path libraryEntriesPath = mProjectFolder;
  860. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  861. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  862. if(FileSystem::exists(libraryEntriesPath))
  863. {
  864. FileDecoder fs(libraryEntriesPath);
  865. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  866. *mRootEntry = libEntries->getRootEntry();
  867. for(auto& child : mRootEntry->mChildren)
  868. child->parent = mRootEntry;
  869. mRootEntry->parent = nullptr;
  870. }
  871. // Load resource manifest
  872. Path resourceManifestPath = mProjectFolder;
  873. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  874. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  875. if (FileSystem::exists(resourceManifestPath))
  876. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  877. else
  878. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  879. gResources().registerResourceManifest(mResourceManifest);
  880. // Load all meta files
  881. Stack<DirectoryEntry*> todo;
  882. todo.push(mRootEntry);
  883. while(!todo.empty())
  884. {
  885. DirectoryEntry* curDir = todo.top();
  886. todo.pop();
  887. for(auto& child : curDir->mChildren)
  888. {
  889. if(child->type == LibraryEntryType::File)
  890. {
  891. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  892. bool doAddDependencies = true;
  893. if (FileSystem::isFile(resEntry->path))
  894. {
  895. if (resEntry->meta == nullptr)
  896. {
  897. Path metaPath = resEntry->path;
  898. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  899. if (FileSystem::isFile(metaPath))
  900. {
  901. FileDecoder fs(metaPath);
  902. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  903. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  904. {
  905. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  906. resEntry->meta = resourceMeta;
  907. }
  908. }
  909. else
  910. {
  911. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  912. reimportResourceInternal(resEntry);
  913. doAddDependencies = false;
  914. }
  915. }
  916. }
  917. if (doAddDependencies)
  918. addDependencies(resEntry);
  919. }
  920. else if(child->type == LibraryEntryType::Directory)
  921. {
  922. todo.push(static_cast<DirectoryEntry*>(child));
  923. }
  924. }
  925. }
  926. mIsLoaded = true;
  927. }
  928. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  929. {
  930. if (!onEntryRemoved.empty())
  931. onEntryRemoved(entry->path);
  932. if (entry->type == LibraryEntryType::File)
  933. {
  934. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  935. queueDependantForReimport(resEntry);
  936. removeDependencies(resEntry);
  937. }
  938. }
  939. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  940. {
  941. if (!onEntryAdded.empty())
  942. onEntryAdded(entry->path);
  943. if (entry->type == LibraryEntryType::File)
  944. {
  945. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  946. queueDependantForReimport(resEntry);
  947. }
  948. }
  949. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  950. {
  951. Vector<Path> output;
  952. if (entry->meta == nullptr)
  953. return output;
  954. if (entry->meta->getTypeID() == TID_Shader)
  955. {
  956. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  957. for (auto& include : metaData->includes)
  958. output.push_back(include);
  959. }
  960. return output;
  961. }
  962. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  963. {
  964. Vector<Path> dependencies = getImportDependencies(entry);
  965. for (auto& dependency : dependencies)
  966. mDependencies[dependency].push_back(entry->path);
  967. }
  968. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  969. {
  970. Vector<Path> dependencies = getImportDependencies(entry);
  971. for (auto& dependency : dependencies)
  972. {
  973. Vector<Path>& curDependencies = mDependencies[dependency];
  974. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  975. [&](const Path& x)
  976. {
  977. return x == entry->path;
  978. });
  979. curDependencies.erase(iterRemove, curDependencies.end());
  980. }
  981. }
  982. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  983. {
  984. auto iterFind = mDependencies.find(entry->path);
  985. if (iterFind == mDependencies.end())
  986. return;
  987. for (auto& dependency : iterFind->second)
  988. mReimportQueue.insert(dependency);
  989. }
  990. }