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