BsProjectLibrary.cpp 33 KB

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