BsProjectLibrary.cpp 32 KB

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