BsProjectLibrary.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. Resources::instance().save(resource, assetPath.getAbsolute(getResourcesFolder()), false);
  508. checkForModifications(assetPath);
  509. }
  510. void ProjectLibrary::saveEntry(const HResource& resource)
  511. {
  512. if (resource == nullptr)
  513. return;
  514. Path filePath;
  515. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  516. return;
  517. Resources::instance().save(resource, filePath, false);
  518. }
  519. void ProjectLibrary::createFolderEntry(const Path& path)
  520. {
  521. Path fullPath = path;
  522. if (fullPath.isAbsolute())
  523. {
  524. if (!mResourcesFolder.includes(fullPath))
  525. return;
  526. }
  527. else
  528. fullPath.makeAbsolute(mResourcesFolder);
  529. if (FileSystem::isDirectory(fullPath))
  530. return; // Already exists
  531. FileSystem::createDir(fullPath);
  532. Path parentPath = fullPath.getParent();
  533. DirectoryEntry* newEntryParent = nullptr;
  534. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  535. if (newEntryParentLib != nullptr)
  536. {
  537. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  538. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  539. }
  540. DirectoryEntry* newHierarchyParent = nullptr;
  541. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  542. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  543. addDirectoryInternal(newEntryParent, fullPath);
  544. }
  545. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  546. {
  547. Path oldFullPath = oldPath;
  548. if (!oldFullPath.isAbsolute())
  549. oldFullPath.makeAbsolute(mResourcesFolder);
  550. Path newFullPath = newPath;
  551. if (!newFullPath.isAbsolute())
  552. newFullPath.makeAbsolute(mResourcesFolder);
  553. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  554. FileSystem::move(oldFullPath, newFullPath, overwrite);
  555. Path oldMetaPath = getMetaPath(oldFullPath);
  556. Path newMetaPath = getMetaPath(newFullPath);
  557. LibraryEntry* oldEntry = findEntry(oldFullPath);
  558. if(oldEntry != nullptr) // Moving from the Resources folder
  559. {
  560. // Moved outside of Resources, delete entry & meta file
  561. if (!mResourcesFolder.includes(newFullPath))
  562. {
  563. if(oldEntry->type == LibraryEntryType::File)
  564. {
  565. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  566. if(FileSystem::isFile(oldMetaPath))
  567. FileSystem::remove(oldMetaPath);
  568. }
  569. else if(oldEntry->type == LibraryEntryType::Directory)
  570. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  571. }
  572. else // Just moving internally
  573. {
  574. doOnEntryRemoved(oldEntry);
  575. if(FileSystem::isFile(oldMetaPath))
  576. FileSystem::move(oldMetaPath, newMetaPath);
  577. DirectoryEntry* parent = oldEntry->parent;
  578. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  579. if(findIter != parent->mChildren.end())
  580. parent->mChildren.erase(findIter);
  581. Path parentPath = newFullPath.getParent();
  582. DirectoryEntry* newEntryParent = nullptr;
  583. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  584. if(newEntryParentLib != nullptr)
  585. {
  586. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  587. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  588. }
  589. DirectoryEntry* newHierarchyParent = nullptr;
  590. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  591. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  592. newEntryParent->mChildren.push_back(oldEntry);
  593. oldEntry->parent = newEntryParent;
  594. oldEntry->path = newFullPath;
  595. oldEntry->elementName = newFullPath.getWTail();
  596. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  597. {
  598. Stack<LibraryEntry*> todo;
  599. todo.push(oldEntry);
  600. while(!todo.empty())
  601. {
  602. LibraryEntry* curEntry = todo.top();
  603. todo.pop();
  604. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  605. for(auto& child : curDirEntry->mChildren)
  606. {
  607. child->path = child->parent->path;
  608. child->path.append(child->elementName);
  609. if(child->type == LibraryEntryType::Directory)
  610. todo.push(child);
  611. }
  612. }
  613. }
  614. doOnEntryAdded(oldEntry);
  615. }
  616. }
  617. else // Moving from outside of the Resources folder (likely adding a new resource)
  618. {
  619. checkForModifications(newFullPath);
  620. }
  621. }
  622. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  623. {
  624. Path oldFullPath = oldPath;
  625. if (!oldFullPath.isAbsolute())
  626. oldFullPath.makeAbsolute(mResourcesFolder);
  627. Path newFullPath = newPath;
  628. if (!newFullPath.isAbsolute())
  629. newFullPath.makeAbsolute(mResourcesFolder);
  630. if (!FileSystem::exists(oldFullPath))
  631. return;
  632. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  633. // Copying a file/folder outside of the Resources folder, no special handling needed
  634. if (!mResourcesFolder.includes(newFullPath))
  635. return;
  636. Path parentPath = newFullPath.getParent();
  637. DirectoryEntry* newEntryParent = nullptr;
  638. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  639. if (newEntryParentLib != nullptr)
  640. {
  641. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  642. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  643. }
  644. // If the source is outside of Resources folder, just plain import the copy
  645. LibraryEntry* oldEntry = findEntry(oldFullPath);
  646. if (oldEntry == nullptr)
  647. {
  648. checkForModifications(newFullPath);
  649. return;
  650. }
  651. // Both source and destination are within Resources folder, need to preserve import options on the copies
  652. LibraryEntry* newEntry = nullptr;
  653. if (FileSystem::isFile(newFullPath))
  654. {
  655. assert(oldEntry->type == LibraryEntryType::File);
  656. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  657. newEntry = addResourceInternal(newEntryParent, newFullPath, oldResEntry->meta->getImportOptions(), true);
  658. }
  659. else
  660. {
  661. assert(oldEntry->type == LibraryEntryType::File);
  662. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  663. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  664. newEntry = newDirEntry;
  665. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  666. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  667. while (!todo.empty())
  668. {
  669. auto current = todo.top();
  670. todo.pop();
  671. DirectoryEntry* sourceDir = std::get<0>(current);
  672. DirectoryEntry* destDir = std::get<1>(current);
  673. for (auto& child : sourceDir->mChildren)
  674. {
  675. Path childDestPath = destDir->path;
  676. childDestPath.append(child->path.getWTail());
  677. if (child->type == LibraryEntryType::File)
  678. {
  679. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  680. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  681. }
  682. else // Directory
  683. {
  684. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  685. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  686. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  687. }
  688. }
  689. }
  690. }
  691. }
  692. void ProjectLibrary::deleteEntry(const Path& path)
  693. {
  694. Path fullPath = path;
  695. if (!fullPath.isAbsolute())
  696. fullPath.makeAbsolute(mResourcesFolder);
  697. if(FileSystem::exists(fullPath))
  698. FileSystem::remove(fullPath);
  699. LibraryEntry* entry = findEntry(fullPath);
  700. if(entry != nullptr)
  701. {
  702. if(entry->type == LibraryEntryType::File)
  703. {
  704. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  705. Path metaPath = getMetaPath(fullPath);
  706. if(FileSystem::isFile(metaPath))
  707. FileSystem::remove(metaPath);
  708. }
  709. else if(entry->type == LibraryEntryType::Directory)
  710. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  711. }
  712. }
  713. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  714. {
  715. LibraryEntry* entry = findEntry(path);
  716. if (entry != nullptr)
  717. {
  718. if (entry->type == LibraryEntryType::File)
  719. {
  720. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  721. reimportResourceInternal(resEntry, importOptions, forceReimport);
  722. queueDependantForReimport(resEntry);
  723. }
  724. }
  725. }
  726. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  727. {
  728. LibraryEntry* entry = findEntry(path);
  729. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  730. return;
  731. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  732. resEntry->meta->setIncludeInBuild(include);
  733. Path metaPath = resEntry->path;
  734. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  735. FileEncoder fs(metaPath);
  736. fs.encode(resEntry->meta.get());
  737. }
  738. Vector<ProjectLibrary::ResourceEntry*> ProjectLibrary::getResourcesForBuild() const
  739. {
  740. Vector<ResourceEntry*> output;
  741. Stack<DirectoryEntry*> todo;
  742. todo.push(mRootEntry);
  743. while (!todo.empty())
  744. {
  745. DirectoryEntry* directory = todo.top();
  746. todo.pop();
  747. for (auto& child : directory->mChildren)
  748. {
  749. if (child->type == LibraryEntryType::File)
  750. {
  751. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  752. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  753. output.push_back(resEntry);
  754. }
  755. else if (child->type == LibraryEntryType::Directory)
  756. {
  757. todo.push(static_cast<DirectoryEntry*>(child));
  758. }
  759. }
  760. }
  761. return output;
  762. }
  763. HResource ProjectLibrary::load(const Path& path)
  764. {
  765. LibraryEntry* entry = findEntry(path);
  766. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  767. return HResource();
  768. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  769. String resUUID = resEntry->meta->getUUID();
  770. return gResources().loadFromUUID(resUUID);
  771. }
  772. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  773. {
  774. Path parentPath = fullPath;
  775. DirectoryEntry* newEntryParent = nullptr;
  776. Stack<Path> parentPaths;
  777. do
  778. {
  779. Path newParentPath = parentPath.getParent();
  780. if(newParentPath == parentPath)
  781. break;
  782. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  783. if(newEntryParentLib != nullptr)
  784. {
  785. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  786. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  787. break;
  788. }
  789. parentPaths.push(newParentPath);
  790. parentPath = newParentPath;
  791. } while (true);
  792. assert(newEntryParent != nullptr); // Must exist
  793. if(newHierarchyRoot != nullptr)
  794. *newHierarchyRoot = newEntryParent;
  795. while(!parentPaths.empty())
  796. {
  797. Path curPath = parentPaths.top();
  798. parentPaths.pop();
  799. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  800. }
  801. if(newHierarchyLeaf != nullptr)
  802. *newHierarchyLeaf = newEntryParent;
  803. }
  804. Path ProjectLibrary::getMetaPath(const Path& path) const
  805. {
  806. Path metaPath = path;
  807. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  808. return metaPath;
  809. }
  810. bool ProjectLibrary::isMeta(const Path& fullPath) const
  811. {
  812. return fullPath.getWExtension() == L".meta";
  813. }
  814. void ProjectLibrary::unloadLibrary()
  815. {
  816. if (!mIsLoaded)
  817. return;
  818. mProjectFolder = Path::BLANK;
  819. mResourcesFolder = Path::BLANK;
  820. clearEntries();
  821. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  822. mReimportQueue.clear();
  823. mDependencies.clear();
  824. gResources().unregisterResourceManifest(mResourceManifest);
  825. mResourceManifest = nullptr;
  826. mIsLoaded = false;
  827. }
  828. void ProjectLibrary::makeEntriesRelative()
  829. {
  830. // Make all paths relative before saving
  831. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  832. [&](LibraryEntry* entry, const Path& root)
  833. {
  834. entry->path.makeRelative(root);
  835. if (entry->type == LibraryEntryType::Directory)
  836. {
  837. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  838. for (auto& child : dirEntry->mChildren)
  839. makeRelative(child, root);
  840. }
  841. };
  842. Path root = getResourcesFolder();
  843. makeRelative(mRootEntry, root);
  844. }
  845. void ProjectLibrary::makeEntriesAbsolute()
  846. {
  847. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  848. [&](LibraryEntry* entry, const Path& root)
  849. {
  850. entry->path.makeAbsolute(root);
  851. if (entry->type == LibraryEntryType::Directory)
  852. {
  853. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  854. for (auto& child : dirEntry->mChildren)
  855. makeAbsolute(child, root);
  856. }
  857. };
  858. Path root = getResourcesFolder();
  859. makeAbsolute(mRootEntry, root);
  860. }
  861. void ProjectLibrary::saveLibrary()
  862. {
  863. if (!mIsLoaded)
  864. return;
  865. // Make all paths relative before saving
  866. makeEntriesRelative();
  867. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  868. Path libraryEntriesPath = mProjectFolder;
  869. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  870. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  871. FileEncoder fs(libraryEntriesPath);
  872. fs.encode(libEntries.get());
  873. // Restore absolute entry paths
  874. makeEntriesAbsolute();
  875. Path resourceManifestPath = mProjectFolder;
  876. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  877. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  878. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  879. }
  880. void ProjectLibrary::loadLibrary()
  881. {
  882. unloadLibrary();
  883. mProjectFolder = gEditorApplication().getProjectPath();
  884. mResourcesFolder = mProjectFolder;
  885. mResourcesFolder.append(RESOURCES_DIR);
  886. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  887. Path libraryEntriesPath = mProjectFolder;
  888. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  889. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  890. if(FileSystem::exists(libraryEntriesPath))
  891. {
  892. FileDecoder fs(libraryEntriesPath);
  893. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  894. *mRootEntry = libEntries->getRootEntry();
  895. for(auto& child : mRootEntry->mChildren)
  896. child->parent = mRootEntry;
  897. mRootEntry->parent = nullptr;
  898. }
  899. // Entries are stored relative to project folder, but we want their absolute paths now
  900. makeEntriesAbsolute();
  901. // Load resource manifest
  902. Path resourceManifestPath = mProjectFolder;
  903. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  904. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  905. if (FileSystem::exists(resourceManifestPath))
  906. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  907. else
  908. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  909. gResources().registerResourceManifest(mResourceManifest);
  910. // Load all meta files
  911. Stack<DirectoryEntry*> todo;
  912. todo.push(mRootEntry);
  913. while(!todo.empty())
  914. {
  915. DirectoryEntry* curDir = todo.top();
  916. todo.pop();
  917. for(auto& child : curDir->mChildren)
  918. {
  919. if(child->type == LibraryEntryType::File)
  920. {
  921. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  922. bool doAddDependencies = true;
  923. if (FileSystem::isFile(resEntry->path))
  924. {
  925. if (resEntry->meta == nullptr)
  926. {
  927. Path metaPath = resEntry->path;
  928. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  929. if (FileSystem::isFile(metaPath))
  930. {
  931. FileDecoder fs(metaPath);
  932. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  933. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  934. {
  935. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  936. resEntry->meta = resourceMeta;
  937. }
  938. }
  939. else
  940. {
  941. LOGWRN("Missing meta file: " + metaPath.toString() + ". Triggering reimport.");
  942. reimportResourceInternal(resEntry);
  943. doAddDependencies = false;
  944. }
  945. }
  946. }
  947. if (resEntry->meta != nullptr)
  948. mUUIDToPath[resEntry->meta->getUUID()] = resEntry->path;
  949. if (doAddDependencies)
  950. addDependencies(resEntry);
  951. }
  952. else if(child->type == LibraryEntryType::Directory)
  953. {
  954. todo.push(static_cast<DirectoryEntry*>(child));
  955. }
  956. }
  957. }
  958. mIsLoaded = true;
  959. }
  960. void ProjectLibrary::clearEntries()
  961. {
  962. if (mRootEntry == nullptr)
  963. return;
  964. std::function<void(LibraryEntry*)> deleteRecursive =
  965. [&](LibraryEntry* entry)
  966. {
  967. if (entry->type == LibraryEntryType::Directory)
  968. {
  969. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  970. for (auto& child : dirEntry->mChildren)
  971. deleteRecursive(child);
  972. }
  973. bs_delete(entry);
  974. };
  975. deleteRecursive(mRootEntry);
  976. mRootEntry = nullptr;
  977. }
  978. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  979. {
  980. if (!onEntryRemoved.empty())
  981. onEntryRemoved(entry->path);
  982. if (entry->type == LibraryEntryType::File)
  983. {
  984. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  985. queueDependantForReimport(resEntry);
  986. removeDependencies(resEntry);
  987. }
  988. }
  989. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  990. {
  991. if (!onEntryAdded.empty())
  992. onEntryAdded(entry->path);
  993. if (entry->type == LibraryEntryType::File)
  994. {
  995. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  996. queueDependantForReimport(resEntry);
  997. }
  998. }
  999. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  1000. {
  1001. Vector<Path> output;
  1002. if (entry->meta == nullptr)
  1003. return output;
  1004. if (entry->meta->getTypeID() == TID_Shader)
  1005. {
  1006. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  1007. for (auto& include : metaData->includes)
  1008. output.push_back(include);
  1009. }
  1010. return output;
  1011. }
  1012. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  1013. {
  1014. Vector<Path> dependencies = getImportDependencies(entry);
  1015. for (auto& dependency : dependencies)
  1016. mDependencies[dependency].push_back(entry->path);
  1017. }
  1018. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  1019. {
  1020. Vector<Path> dependencies = getImportDependencies(entry);
  1021. for (auto& dependency : dependencies)
  1022. {
  1023. Vector<Path>& curDependencies = mDependencies[dependency];
  1024. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1025. [&](const Path& x)
  1026. {
  1027. return x == entry->path;
  1028. });
  1029. curDependencies.erase(iterRemove, curDependencies.end());
  1030. }
  1031. }
  1032. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  1033. {
  1034. auto iterFind = mDependencies.find(entry->path);
  1035. if (iterFind == mDependencies.end())
  1036. return;
  1037. for (auto& dependency : iterFind->second)
  1038. mReimportQueue.insert(dependency);
  1039. }
  1040. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1041. {
  1042. return ProjectLibrary::instance();
  1043. }
  1044. }