BsProjectLibrary.cpp 32 KB

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