BsProjectLibrary.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. current = nullptr;
  448. for (auto& child : dirEntry->mChildren)
  449. {
  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. // If the source is outside of Resources folder, just plain import the copy
  634. LibraryEntry* oldEntry = findEntry(oldFullPath);
  635. if (oldEntry == nullptr)
  636. {
  637. checkForModifications(newFullPath);
  638. return;
  639. }
  640. // Both source and destination are within Resources folder, need to preserve import options on the copies
  641. LibraryEntry* newEntry = nullptr;
  642. if (FileSystem::isFile(newFullPath))
  643. {
  644. assert(oldEntry->type == LibraryEntryType::File);
  645. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  646. newEntry = addResourceInternal(newEntryParent, newFullPath, oldResEntry->meta->getImportOptions(), true);
  647. }
  648. else
  649. {
  650. assert(oldEntry->type == LibraryEntryType::File);
  651. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  652. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  653. newEntry = newDirEntry;
  654. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  655. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  656. while (!todo.empty())
  657. {
  658. auto current = todo.top();
  659. todo.pop();
  660. DirectoryEntry* sourceDir = std::get<0>(current);
  661. DirectoryEntry* destDir = std::get<1>(current);
  662. for (auto& child : sourceDir->mChildren)
  663. {
  664. Path childDestPath = destDir->path;
  665. childDestPath.append(child->path.getWTail());
  666. if (child->type == LibraryEntryType::File)
  667. {
  668. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  669. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  670. }
  671. else // Directory
  672. {
  673. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  674. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  675. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  676. }
  677. }
  678. }
  679. }
  680. }
  681. void ProjectLibrary::deleteEntry(const Path& path)
  682. {
  683. Path fullPath = path;
  684. if (!fullPath.isAbsolute())
  685. fullPath.makeAbsolute(mResourcesFolder);
  686. if(FileSystem::exists(fullPath))
  687. FileSystem::remove(fullPath);
  688. LibraryEntry* entry = findEntry(fullPath);
  689. if(entry != nullptr)
  690. {
  691. if(entry->type == LibraryEntryType::File)
  692. {
  693. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  694. Path metaPath = getMetaPath(fullPath);
  695. if(FileSystem::isFile(metaPath))
  696. FileSystem::remove(metaPath);
  697. }
  698. else if(entry->type == LibraryEntryType::Directory)
  699. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  700. }
  701. }
  702. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  703. {
  704. LibraryEntry* entry = findEntry(path);
  705. if (entry != nullptr)
  706. {
  707. if (entry->type == LibraryEntryType::File)
  708. {
  709. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  710. reimportResourceInternal(resEntry, importOptions, forceReimport);
  711. queueDependantForReimport(resEntry);
  712. }
  713. }
  714. }
  715. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  716. {
  717. Path parentPath = fullPath;
  718. DirectoryEntry* newEntryParent = nullptr;
  719. Stack<Path> parentPaths;
  720. do
  721. {
  722. Path newParentPath = parentPath.getParent();
  723. if(newParentPath == parentPath)
  724. break;
  725. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  726. if(newEntryParentLib != nullptr)
  727. {
  728. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  729. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  730. break;
  731. }
  732. parentPaths.push(newParentPath);
  733. parentPath = newParentPath;
  734. } while (true);
  735. assert(newEntryParent != nullptr); // Must exist
  736. if(newHierarchyRoot != nullptr)
  737. *newHierarchyRoot = newEntryParent;
  738. while(!parentPaths.empty())
  739. {
  740. Path curPath = parentPaths.top();
  741. parentPaths.pop();
  742. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  743. }
  744. if(newHierarchyLeaf != nullptr)
  745. *newHierarchyLeaf = newEntryParent;
  746. }
  747. Path ProjectLibrary::getMetaPath(const Path& path) const
  748. {
  749. Path metaPath = path;
  750. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  751. return metaPath;
  752. }
  753. bool ProjectLibrary::isMeta(const Path& fullPath) const
  754. {
  755. return fullPath.getWExtension() == L".meta";
  756. }
  757. void ProjectLibrary::onMonitorFileModified(const Path& path)
  758. {
  759. if(!isMeta(path))
  760. checkForModifications(path);
  761. else
  762. {
  763. Path resourcePath = path;
  764. resourcePath.setExtension(L"");
  765. checkForModifications(resourcePath);
  766. }
  767. }
  768. void ProjectLibrary::save()
  769. {
  770. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  771. Path libraryEntriesPath = mProjectFolder;
  772. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  773. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  774. FileEncoder fs(libraryEntriesPath);
  775. fs.encode(libEntries.get());
  776. Path resourceManifestPath = mProjectFolder;
  777. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  778. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  779. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  780. }
  781. void ProjectLibrary::load()
  782. {
  783. if(mRootEntry != nullptr)
  784. {
  785. deleteDirectoryInternal(mRootEntry);
  786. mRootEntry = nullptr;
  787. }
  788. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  789. Path libraryEntriesPath = mProjectFolder;
  790. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  791. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  792. if(FileSystem::exists(libraryEntriesPath))
  793. {
  794. FileDecoder fs(libraryEntriesPath);
  795. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  796. *mRootEntry = libEntries->getRootEntry();
  797. for(auto& child : mRootEntry->mChildren)
  798. child->parent = mRootEntry;
  799. mRootEntry->parent = nullptr;
  800. }
  801. // Load all meta files
  802. Stack<DirectoryEntry*> todo;
  803. todo.push(mRootEntry);
  804. while(!todo.empty())
  805. {
  806. DirectoryEntry* curDir = todo.top();
  807. todo.pop();
  808. for(auto& child : curDir->mChildren)
  809. {
  810. if(child->type == LibraryEntryType::File)
  811. {
  812. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  813. if(resEntry->meta == nullptr)
  814. {
  815. Path metaPath = resEntry->path;
  816. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  817. if(FileSystem::isFile(metaPath))
  818. {
  819. FileDecoder fs(metaPath);
  820. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  821. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  822. {
  823. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  824. resEntry->meta = resourceMeta;
  825. }
  826. }
  827. }
  828. addDependencies(resEntry);
  829. }
  830. else if(child->type == LibraryEntryType::Directory)
  831. {
  832. todo.push(static_cast<DirectoryEntry*>(child));
  833. }
  834. }
  835. }
  836. // Load resource manifest
  837. Path resourceManifestPath = mProjectFolder;
  838. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  839. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  840. if(FileSystem::exists(resourceManifestPath))
  841. {
  842. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  843. }
  844. }
  845. void ProjectLibrary::doOnEntryRemoved(const LibraryEntry* entry)
  846. {
  847. if (!onEntryRemoved.empty())
  848. onEntryRemoved(entry->path);
  849. if (entry->type == LibraryEntryType::File)
  850. {
  851. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  852. queueDependantForReimport(resEntry);
  853. removeDependencies(resEntry);
  854. }
  855. }
  856. void ProjectLibrary::doOnEntryAdded(const LibraryEntry* entry)
  857. {
  858. if (!onEntryAdded.empty())
  859. onEntryAdded(entry->path);
  860. if (entry->type == LibraryEntryType::File)
  861. {
  862. const ResourceEntry* resEntry = static_cast<const ResourceEntry*>(entry);
  863. queueDependantForReimport(resEntry);
  864. }
  865. }
  866. Vector<Path> ProjectLibrary::getImportDependencies(const ResourceEntry* entry)
  867. {
  868. Vector<Path> output;
  869. if (entry->meta == nullptr)
  870. return output;
  871. if (entry->meta->getTypeID() == TID_Shader)
  872. {
  873. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(entry->meta->getResourceMetaData());
  874. for (auto& include : metaData->includes)
  875. output.push_back(include);
  876. }
  877. return output;
  878. }
  879. void ProjectLibrary::addDependencies(const ResourceEntry* entry)
  880. {
  881. Vector<Path> dependencies = getImportDependencies(entry);
  882. for (auto& dependency : dependencies)
  883. mDependencies[dependency].push_back(entry->path);
  884. }
  885. void ProjectLibrary::removeDependencies(const ResourceEntry* entry)
  886. {
  887. Vector<Path> dependencies = getImportDependencies(entry);
  888. for (auto& dependency : dependencies)
  889. {
  890. Vector<Path>& curDependencies = mDependencies[dependency];
  891. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  892. [&](const Path& x)
  893. {
  894. return x == entry->path;
  895. });
  896. curDependencies.erase(iterRemove, curDependencies.end());
  897. }
  898. }
  899. void ProjectLibrary::queueDependantForReimport(const ResourceEntry* entry)
  900. {
  901. auto iterFind = mDependencies.find(entry->path);
  902. if (iterFind == mDependencies.end())
  903. return;
  904. for (auto& dependency : iterFind->second)
  905. mReimportQueue.insert(dependency);
  906. }
  907. }