BsProjectLibrary.cpp 36 KB

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