BsProjectLibrary.cpp 42 KB

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