BsProjectLibrary.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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* fileEntry, const ImportOptionsPtr& importOptions,
  300. bool forceReimport, bool pruneResourceMetas)
  301. {
  302. Path metaPath = fileEntry->path;
  303. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  304. if(fileEntry->meta == nullptr)
  305. {
  306. if(FileSystem::isFile(metaPath))
  307. {
  308. FileDecoder fs(metaPath);
  309. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  310. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  311. {
  312. ProjectFileMetaPtr fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  313. fileEntry->meta = fileMeta;
  314. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  315. if (resourceMetas.size() > 0)
  316. {
  317. mUUIDToPath[resourceMetas[0]->getUUID()] = fileEntry->path;
  318. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  319. {
  320. ProjectResourceMetaPtr entry = resourceMetas[i];
  321. mUUIDToPath[entry->getUUID()] = fileEntry->path + entry->getUniqueName();
  322. }
  323. }
  324. }
  325. }
  326. }
  327. if (!isUpToDate(fileEntry) || forceReimport)
  328. {
  329. // Note: If resource is native we just copy it to the internal folder. We could avoid the copy and
  330. // load the resource directly from the Resources folder but that requires complicating library code.
  331. bool isNativeResource = isNative(fileEntry->path);
  332. ImportOptionsPtr curImportOptions = nullptr;
  333. if (importOptions == nullptr && !isNativeResource)
  334. {
  335. if (fileEntry->meta != nullptr)
  336. curImportOptions = fileEntry->meta->getImportOptions();
  337. else
  338. curImportOptions = Importer::instance().createImportOptions(fileEntry->path);
  339. }
  340. else
  341. curImportOptions = importOptions;
  342. Vector<SubResource> importedResources;
  343. if (isNativeResource)
  344. {
  345. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  346. // This can happen if library isn't properly saved before exiting the application.
  347. if (fileEntry->meta != nullptr)
  348. {
  349. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  350. mResourceManifest->registerResource(resourceMetas[0]->getUUID(), fileEntry->path);
  351. }
  352. // Don't load dependencies because we don't need them, but also because they might not be in the manifest
  353. // which would screw up their UUIDs.
  354. importedResources.push_back({ L"primary", gResources().load(fileEntry->path, false, false) });
  355. }
  356. if(fileEntry->meta == nullptr)
  357. {
  358. if (!isNativeResource)
  359. importedResources = Importer::instance().importAll(fileEntry->path, curImportOptions);
  360. fileEntry->meta = ProjectFileMeta::create(curImportOptions);
  361. for(auto& entry : importedResources)
  362. {
  363. ResourceMetaDataPtr subMeta = entry.value->getMetaData();
  364. UINT32 typeId = entry.value->getTypeId();
  365. const String& UUID = entry.value.getUUID();
  366. ProjectResourceMetaPtr resMeta = ProjectResourceMeta::create(entry.name, UUID, typeId, subMeta);
  367. fileEntry->meta->add(resMeta);
  368. }
  369. if(importedResources.size() > 0)
  370. {
  371. HResource primary = importedResources[0].value;
  372. mUUIDToPath[primary.getUUID()] = fileEntry->path;
  373. for (UINT32 i = 1; i < (UINT32)importedResources.size(); i++)
  374. {
  375. SubResource& entry = importedResources[i];
  376. const String& UUID = entry.value.getUUID();
  377. mUUIDToPath[UUID] = fileEntry->path + entry.name;
  378. }
  379. }
  380. FileEncoder fs(metaPath);
  381. fs.encode(fileEntry->meta.get());
  382. }
  383. else
  384. {
  385. removeDependencies(fileEntry);
  386. if (!isNativeResource)
  387. {
  388. Vector<SubResourceRaw> importedResourcesRaw = gImporter()._importAllRaw(fileEntry->path, curImportOptions);
  389. Vector<ProjectResourceMetaPtr> existingResourceMetas = fileEntry->meta->getResourceMetaData();
  390. fileEntry->meta->clearResourceMetaData();
  391. for(auto& resEntry : importedResourcesRaw)
  392. {
  393. bool foundMeta = false;
  394. for (auto iter = existingResourceMetas.begin(); iter != existingResourceMetas.end(); ++iter)
  395. {
  396. ProjectResourceMetaPtr metaEntry = *iter;
  397. if(resEntry.name == metaEntry->getUniqueName())
  398. {
  399. HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
  400. gResources().update(importedResource, resEntry.value);
  401. importedResources.push_back({ resEntry.name, importedResource });
  402. fileEntry->meta->add(metaEntry);
  403. existingResourceMetas.erase(iter);
  404. foundMeta = true;
  405. break;
  406. }
  407. }
  408. if(!foundMeta)
  409. {
  410. HResource importedResource = gResources()._createResourceHandle(resEntry.value);
  411. importedResources.push_back({ resEntry.name, importedResource });
  412. ResourceMetaDataPtr subMeta = resEntry.value->getMetaData();
  413. UINT32 typeId = resEntry.value->getTypeId();
  414. const String& UUID = importedResource.getUUID();
  415. ProjectResourceMetaPtr resMeta = ProjectResourceMeta::create(resEntry.name, UUID, typeId, subMeta);
  416. fileEntry->meta->add(resMeta);
  417. }
  418. }
  419. // Keep resource metas that we are not currently using, in case they get restored so their references
  420. // don't get broken
  421. if(!pruneResourceMetas)
  422. {
  423. for (auto& entry : existingResourceMetas)
  424. fileEntry->meta->add(entry);
  425. }
  426. }
  427. fileEntry->meta->mImportOptions = curImportOptions;
  428. FileEncoder fs(metaPath);
  429. fs.encode(fileEntry->meta.get());
  430. }
  431. addDependencies(fileEntry);
  432. if (importedResources.size() > 0)
  433. {
  434. Path internalResourcesPath = mProjectFolder;
  435. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  436. if (!FileSystem::isDirectory(internalResourcesPath))
  437. FileSystem::createDir(internalResourcesPath);
  438. for (auto& entry : importedResources)
  439. {
  440. internalResourcesPath.setFilename(toWString(entry.value.getUUID()) + L".asset");
  441. gResources().save(entry.value, internalResourcesPath, true);
  442. String uuid = entry.value.getUUID();
  443. mResourceManifest->registerResource(uuid, internalResourcesPath);
  444. }
  445. }
  446. fileEntry->lastUpdateTime = std::time(nullptr);
  447. onEntryImported(fileEntry->path);
  448. reimportDependants(fileEntry->path);
  449. }
  450. }
  451. bool ProjectLibrary::isUpToDate(FileEntry* resource) const
  452. {
  453. if(resource->meta == nullptr)
  454. return false;
  455. auto& resourceMetas = resource->meta->getResourceMetaData();
  456. for (auto& resMeta : resourceMetas)
  457. {
  458. Path internalPath;
  459. if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
  460. return false;
  461. if (!FileSystem::isFile(internalPath))
  462. return false;
  463. }
  464. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  465. return lastModifiedTime <= resource->lastUpdateTime;
  466. }
  467. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  468. {
  469. return search(pattern, {});
  470. }
  471. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  472. {
  473. Vector<LibraryEntry*> foundEntries;
  474. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  475. WString replace(L"\\\\&");
  476. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  477. std::wregex wildcard(L"\\\\\\*");
  478. WString wildcardReplace(L".*");
  479. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  480. std::wregex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  481. Stack<DirectoryEntry*> todo;
  482. todo.push(mRootEntry);
  483. while (!todo.empty())
  484. {
  485. DirectoryEntry* dirEntry = todo.top();
  486. todo.pop();
  487. for (auto& child : dirEntry->mChildren)
  488. {
  489. if (std::regex_match(child->elementName, searchRegex))
  490. {
  491. if (typeIds.size() == 0)
  492. foundEntries.push_back(child);
  493. else
  494. {
  495. if (child->type == LibraryEntryType::File)
  496. {
  497. FileEntry* childFileEntry = static_cast<FileEntry*>(child);
  498. if (childFileEntry->meta != nullptr)
  499. {
  500. auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
  501. for (auto& typeId : typeIds)
  502. {
  503. bool found = false;
  504. for (auto& resMeta : resourceMetas)
  505. {
  506. if (resMeta->getTypeID() == typeId)
  507. {
  508. foundEntries.push_back(child);
  509. found = true;
  510. break;
  511. }
  512. }
  513. if (found)
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. if (child->type == LibraryEntryType::Directory)
  521. {
  522. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  523. todo.push(childDirEntry);
  524. }
  525. }
  526. }
  527. std::sort(foundEntries.begin(), foundEntries.end(),
  528. [&](const LibraryEntry* a, const LibraryEntry* b)
  529. {
  530. return a->elementName.compare(b->elementName) < 0;
  531. });
  532. return foundEntries;
  533. }
  534. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  535. {
  536. Path fullPath = path;
  537. if (fullPath.isAbsolute())
  538. {
  539. if (!mResourcesFolder.includes(fullPath))
  540. return nullptr;
  541. }
  542. else
  543. fullPath.makeAbsolute(mResourcesFolder);
  544. Path relPath = fullPath.getRelative(mRootEntry->path);
  545. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  546. UINT32 idx = 0;
  547. LibraryEntry* current = mRootEntry;
  548. while (current != nullptr)
  549. {
  550. if (idx == numElems)
  551. return current;
  552. WString curElem;
  553. if (relPath.isFile() && idx == (numElems - 1))
  554. curElem = relPath.getWFilename();
  555. else
  556. curElem = relPath[idx];
  557. if (current->type == LibraryEntryType::Directory)
  558. {
  559. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  560. current = nullptr;
  561. for (auto& child : dirEntry->mChildren)
  562. {
  563. if (Path::comparePathElem(curElem, child->elementName))
  564. {
  565. idx++;
  566. current = child;
  567. break;
  568. }
  569. }
  570. }
  571. else // Found file
  572. {
  573. // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
  574. if (idx == (numElems - 1))
  575. return current;
  576. else
  577. break; // Not a valid path
  578. }
  579. }
  580. return nullptr;
  581. }
  582. bool ProjectLibrary::isSubresource(const Path& path) const
  583. {
  584. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  585. if (numElems <= 1)
  586. return false;
  587. Path filePath = path;
  588. filePath.makeParent();
  589. LibraryEntry* entry = findEntry(filePath);
  590. if (entry != nullptr && entry->type == LibraryEntryType::File)
  591. return true;
  592. return false;
  593. }
  594. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const Path& path) const
  595. {
  596. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  597. // Check if it is a subresource path
  598. if(numElems > 1)
  599. {
  600. Path filePath = path;
  601. filePath.makeParent();
  602. LibraryEntry* entry = findEntry(filePath);
  603. if (entry == nullptr)
  604. return nullptr;
  605. // Entry is a subresource
  606. if (entry->type == LibraryEntryType::File)
  607. {
  608. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  609. if (fileEntry->meta == nullptr)
  610. return nullptr;
  611. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  612. for(auto& resMeta : resourceMetas)
  613. {
  614. if (resMeta->getUniqueName() == path.getWTail())
  615. return resMeta;
  616. }
  617. // Found the file but no subresource or meta information
  618. return nullptr;
  619. }
  620. else // Entry not a subresource
  621. {
  622. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  623. for (auto& child : dirEntry->mChildren)
  624. {
  625. if (Path::comparePathElem(path.getWTail(), child->elementName))
  626. {
  627. if (child->type == LibraryEntryType::File)
  628. {
  629. FileEntry* fileEntry = static_cast<FileEntry*>(child);
  630. if (fileEntry->meta == nullptr)
  631. return nullptr;
  632. return fileEntry->meta->getResourceMetaData()[0];
  633. }
  634. }
  635. }
  636. return nullptr;
  637. }
  638. }
  639. // Not a subresource path, load directly
  640. {
  641. LibraryEntry* entry = findEntry(path);
  642. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  643. return nullptr;
  644. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  645. if (fileEntry->meta == nullptr)
  646. return nullptr;
  647. return fileEntry->meta->getResourceMetaData()[0];
  648. }
  649. }
  650. Path ProjectLibrary::uuidToPath(const String& uuid) const
  651. {
  652. auto iterFind = mUUIDToPath.find(uuid);
  653. if (iterFind != mUUIDToPath.end())
  654. return iterFind->second;
  655. return Path::BLANK;
  656. }
  657. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  658. {
  659. if (resource == nullptr)
  660. return;
  661. Path assetPath = path;
  662. if (path.isAbsolute())
  663. {
  664. if (!getResourcesFolder().includes(path))
  665. return;
  666. assetPath = path.getRelative(getResourcesFolder());
  667. }
  668. LibraryEntry* existingEntry = findEntry(assetPath);
  669. if (existingEntry != nullptr)
  670. {
  671. LOGWRN("Resource already exists at the specified path : " + assetPath.toString() + ". Unable to save.");
  672. return;
  673. }
  674. resource->setName(path.getWFilename(false));
  675. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  676. Resources::instance().save(resource, absPath, false);
  677. checkForModifications(absPath);
  678. }
  679. void ProjectLibrary::saveEntry(const HResource& resource)
  680. {
  681. if (resource == nullptr)
  682. return;
  683. Path filePath = uuidToPath(resource.getUUID());
  684. filePath.makeAbsolute(getResourcesFolder());
  685. Resources::instance().save(resource, filePath, true);
  686. checkForModifications(filePath);
  687. }
  688. void ProjectLibrary::createFolderEntry(const Path& path)
  689. {
  690. Path fullPath = path;
  691. if (fullPath.isAbsolute())
  692. {
  693. if (!mResourcesFolder.includes(fullPath))
  694. return;
  695. }
  696. else
  697. fullPath.makeAbsolute(mResourcesFolder);
  698. if (FileSystem::isDirectory(fullPath))
  699. return; // Already exists
  700. FileSystem::createDir(fullPath);
  701. Path parentPath = fullPath.getParent();
  702. DirectoryEntry* newEntryParent = nullptr;
  703. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  704. if (newEntryParentLib != nullptr)
  705. {
  706. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  707. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  708. }
  709. DirectoryEntry* newHierarchyParent = nullptr;
  710. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  711. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  712. addDirectoryInternal(newEntryParent, fullPath);
  713. }
  714. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  715. {
  716. Path oldFullPath = oldPath;
  717. if (!oldFullPath.isAbsolute())
  718. oldFullPath.makeAbsolute(mResourcesFolder);
  719. Path newFullPath = newPath;
  720. if (!newFullPath.isAbsolute())
  721. newFullPath.makeAbsolute(mResourcesFolder);
  722. Path parentPath = newFullPath.getParent();
  723. if (!FileSystem::isDirectory(parentPath))
  724. {
  725. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  726. return;
  727. }
  728. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  729. FileSystem::move(oldFullPath, newFullPath, overwrite);
  730. Path oldMetaPath = getMetaPath(oldFullPath);
  731. Path newMetaPath = getMetaPath(newFullPath);
  732. LibraryEntry* oldEntry = findEntry(oldFullPath);
  733. if(oldEntry != nullptr) // Moving from the Resources folder
  734. {
  735. // Moved outside of Resources, delete entry & meta file
  736. if (!mResourcesFolder.includes(newFullPath))
  737. {
  738. if(oldEntry->type == LibraryEntryType::File)
  739. deleteResourceInternal(static_cast<FileEntry*>(oldEntry));
  740. else if(oldEntry->type == LibraryEntryType::Directory)
  741. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  742. }
  743. else // Just moving internally
  744. {
  745. onEntryRemoved(oldEntry->path);
  746. FileEntry* fileEntry = nullptr;
  747. if (oldEntry->type == LibraryEntryType::File)
  748. {
  749. fileEntry = static_cast<FileEntry*>(oldEntry);
  750. removeDependencies(fileEntry);
  751. // Update uuid <-> path mapping
  752. if(fileEntry->meta != nullptr)
  753. {
  754. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  755. if (resourceMetas.size() > 0)
  756. {
  757. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  758. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  759. {
  760. ProjectResourceMetaPtr resMeta = resourceMetas[i];
  761. const String& UUID = resMeta->getUUID();
  762. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  763. }
  764. }
  765. }
  766. }
  767. if(FileSystem::isFile(oldMetaPath))
  768. FileSystem::move(oldMetaPath, newMetaPath);
  769. DirectoryEntry* parent = oldEntry->parent;
  770. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  771. if(findIter != parent->mChildren.end())
  772. parent->mChildren.erase(findIter);
  773. Path parentPath = newFullPath.getParent();
  774. DirectoryEntry* newEntryParent = nullptr;
  775. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  776. if(newEntryParentLib != nullptr)
  777. {
  778. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  779. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  780. }
  781. DirectoryEntry* newHierarchyParent = nullptr;
  782. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  783. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  784. newEntryParent->mChildren.push_back(oldEntry);
  785. oldEntry->parent = newEntryParent;
  786. oldEntry->path = newFullPath;
  787. oldEntry->elementName = newFullPath.getWTail();
  788. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  789. {
  790. Stack<LibraryEntry*> todo;
  791. todo.push(oldEntry);
  792. while(!todo.empty())
  793. {
  794. LibraryEntry* curEntry = todo.top();
  795. todo.pop();
  796. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  797. for(auto& child : curDirEntry->mChildren)
  798. {
  799. child->path = child->parent->path;
  800. child->path.append(child->elementName);
  801. if(child->type == LibraryEntryType::Directory)
  802. todo.push(child);
  803. }
  804. }
  805. }
  806. onEntryAdded(oldEntry->path);
  807. if (fileEntry != nullptr)
  808. {
  809. reimportDependants(oldFullPath);
  810. reimportDependants(newFullPath);
  811. }
  812. }
  813. }
  814. else // Moving from outside of the Resources folder (likely adding a new resource)
  815. {
  816. checkForModifications(newFullPath);
  817. }
  818. }
  819. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  820. {
  821. Path oldFullPath = oldPath;
  822. if (!oldFullPath.isAbsolute())
  823. oldFullPath.makeAbsolute(mResourcesFolder);
  824. Path newFullPath = newPath;
  825. if (!newFullPath.isAbsolute())
  826. newFullPath.makeAbsolute(mResourcesFolder);
  827. if (!FileSystem::exists(oldFullPath))
  828. return;
  829. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  830. // Copying a file/folder outside of the Resources folder, no special handling needed
  831. if (!mResourcesFolder.includes(newFullPath))
  832. return;
  833. Path parentPath = newFullPath.getParent();
  834. DirectoryEntry* newEntryParent = nullptr;
  835. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  836. if (newEntryParentLib != nullptr)
  837. {
  838. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  839. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  840. }
  841. // If the source is outside of Resources folder, just plain import the copy
  842. LibraryEntry* oldEntry = findEntry(oldFullPath);
  843. if (oldEntry == nullptr)
  844. {
  845. checkForModifications(newFullPath);
  846. return;
  847. }
  848. // Both source and destination are within Resources folder, need to preserve import options on the copies
  849. LibraryEntry* newEntry = nullptr;
  850. if (FileSystem::isFile(newFullPath))
  851. {
  852. assert(oldEntry->type == LibraryEntryType::File);
  853. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  854. ImportOptionsPtr importOptions;
  855. if (oldResEntry->meta != nullptr)
  856. importOptions = oldResEntry->meta->getImportOptions();
  857. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  858. }
  859. else
  860. {
  861. assert(oldEntry->type == LibraryEntryType::File);
  862. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  863. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  864. newEntry = newDirEntry;
  865. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  866. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  867. while (!todo.empty())
  868. {
  869. auto current = todo.top();
  870. todo.pop();
  871. DirectoryEntry* sourceDir = std::get<0>(current);
  872. DirectoryEntry* destDir = std::get<1>(current);
  873. for (auto& child : sourceDir->mChildren)
  874. {
  875. Path childDestPath = destDir->path;
  876. childDestPath.append(child->path.getWTail());
  877. if (child->type == LibraryEntryType::File)
  878. {
  879. FileEntry* childResEntry = static_cast<FileEntry*>(child);
  880. ImportOptionsPtr importOptions;
  881. if (childResEntry->meta != nullptr)
  882. importOptions = childResEntry->meta->getImportOptions();
  883. addResourceInternal(destDir, childDestPath, importOptions, true);
  884. }
  885. else // Directory
  886. {
  887. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  888. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  889. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  890. }
  891. }
  892. }
  893. }
  894. }
  895. void ProjectLibrary::deleteEntry(const Path& path)
  896. {
  897. Path fullPath = path;
  898. if (!fullPath.isAbsolute())
  899. fullPath.makeAbsolute(mResourcesFolder);
  900. if(FileSystem::exists(fullPath))
  901. FileSystem::remove(fullPath);
  902. LibraryEntry* entry = findEntry(fullPath);
  903. if(entry != nullptr)
  904. {
  905. if(entry->type == LibraryEntryType::File)
  906. deleteResourceInternal(static_cast<FileEntry*>(entry));
  907. else if(entry->type == LibraryEntryType::Directory)
  908. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  909. }
  910. }
  911. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  912. {
  913. LibraryEntry* entry = findEntry(path);
  914. if (entry != nullptr)
  915. {
  916. if (entry->type == LibraryEntryType::File)
  917. {
  918. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  919. reimportResourceInternal(resEntry, importOptions, forceReimport);
  920. }
  921. }
  922. }
  923. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  924. {
  925. LibraryEntry* entry = findEntry(path);
  926. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  927. return;
  928. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  929. if (resEntry->meta == nullptr)
  930. return;
  931. resEntry->meta->setIncludeInBuild(include);
  932. Path metaPath = resEntry->path;
  933. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  934. FileEncoder fs(metaPath);
  935. fs.encode(resEntry->meta.get());
  936. }
  937. Vector<ProjectLibrary::FileEntry*> ProjectLibrary::getResourcesForBuild() const
  938. {
  939. Vector<FileEntry*> output;
  940. Stack<DirectoryEntry*> todo;
  941. todo.push(mRootEntry);
  942. while (!todo.empty())
  943. {
  944. DirectoryEntry* directory = todo.top();
  945. todo.pop();
  946. for (auto& child : directory->mChildren)
  947. {
  948. if (child->type == LibraryEntryType::File)
  949. {
  950. FileEntry* resEntry = static_cast<FileEntry*>(child);
  951. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  952. output.push_back(resEntry);
  953. }
  954. else if (child->type == LibraryEntryType::Directory)
  955. {
  956. todo.push(static_cast<DirectoryEntry*>(child));
  957. }
  958. }
  959. }
  960. return output;
  961. }
  962. HResource ProjectLibrary::load(const Path& path)
  963. {
  964. ProjectResourceMetaPtr meta = findResourceMeta(path);
  965. if (meta == nullptr)
  966. return HResource();
  967. String resUUID = meta->getUUID();
  968. return gResources().loadFromUUID(resUUID);
  969. }
  970. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  971. {
  972. Path parentPath = fullPath;
  973. DirectoryEntry* newEntryParent = nullptr;
  974. Stack<Path> parentPaths;
  975. do
  976. {
  977. Path newParentPath = parentPath.getParent();
  978. if(newParentPath == parentPath)
  979. break;
  980. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  981. if(newEntryParentLib != nullptr)
  982. {
  983. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  984. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  985. break;
  986. }
  987. parentPaths.push(newParentPath);
  988. parentPath = newParentPath;
  989. } while (true);
  990. assert(newEntryParent != nullptr); // Must exist
  991. if(newHierarchyRoot != nullptr)
  992. *newHierarchyRoot = newEntryParent;
  993. while(!parentPaths.empty())
  994. {
  995. Path curPath = parentPaths.top();
  996. parentPaths.pop();
  997. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  998. }
  999. if(newHierarchyLeaf != nullptr)
  1000. *newHierarchyLeaf = newEntryParent;
  1001. }
  1002. Path ProjectLibrary::getMetaPath(const Path& path) const
  1003. {
  1004. Path metaPath = path;
  1005. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1006. return metaPath;
  1007. }
  1008. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1009. {
  1010. return fullPath.getWExtension() == L".meta";
  1011. }
  1012. bool ProjectLibrary::isNative(const Path& path) const
  1013. {
  1014. WString extension = path.getWExtension();
  1015. return extension == L".asset" || extension == L".prefab";
  1016. }
  1017. void ProjectLibrary::unloadLibrary()
  1018. {
  1019. if (!mIsLoaded)
  1020. return;
  1021. mProjectFolder = Path::BLANK;
  1022. mResourcesFolder = Path::BLANK;
  1023. clearEntries();
  1024. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1025. mDependencies.clear();
  1026. gResources().unregisterResourceManifest(mResourceManifest);
  1027. mResourceManifest = nullptr;
  1028. mIsLoaded = false;
  1029. }
  1030. void ProjectLibrary::makeEntriesRelative()
  1031. {
  1032. // Make all paths relative before saving
  1033. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1034. [&](LibraryEntry* entry, const Path& root)
  1035. {
  1036. entry->path.makeRelative(root);
  1037. if (entry->type == LibraryEntryType::Directory)
  1038. {
  1039. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1040. for (auto& child : dirEntry->mChildren)
  1041. makeRelative(child, root);
  1042. }
  1043. };
  1044. Path root = getResourcesFolder();
  1045. makeRelative(mRootEntry, root);
  1046. }
  1047. void ProjectLibrary::makeEntriesAbsolute()
  1048. {
  1049. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1050. [&](LibraryEntry* entry, const Path& root)
  1051. {
  1052. entry->path.makeAbsolute(root);
  1053. if (entry->type == LibraryEntryType::Directory)
  1054. {
  1055. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1056. for (auto& child : dirEntry->mChildren)
  1057. makeAbsolute(child, root);
  1058. }
  1059. };
  1060. Path root = getResourcesFolder();
  1061. makeAbsolute(mRootEntry, root);
  1062. }
  1063. void ProjectLibrary::saveLibrary()
  1064. {
  1065. if (!mIsLoaded)
  1066. return;
  1067. // Make all paths relative before saving
  1068. makeEntriesRelative();
  1069. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  1070. Path libraryEntriesPath = mProjectFolder;
  1071. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1072. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1073. FileEncoder fs(libraryEntriesPath);
  1074. fs.encode(libEntries.get());
  1075. // Restore absolute entry paths
  1076. makeEntriesAbsolute();
  1077. Path resourceManifestPath = mProjectFolder;
  1078. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1079. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1080. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1081. }
  1082. void ProjectLibrary::loadLibrary()
  1083. {
  1084. unloadLibrary();
  1085. mProjectFolder = gEditorApplication().getProjectPath();
  1086. mResourcesFolder = mProjectFolder;
  1087. mResourcesFolder.append(RESOURCES_DIR);
  1088. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1089. Path libraryEntriesPath = mProjectFolder;
  1090. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1091. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1092. if(FileSystem::exists(libraryEntriesPath))
  1093. {
  1094. FileDecoder fs(libraryEntriesPath);
  1095. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1096. *mRootEntry = libEntries->getRootEntry();
  1097. for(auto& child : mRootEntry->mChildren)
  1098. child->parent = mRootEntry;
  1099. mRootEntry->parent = nullptr;
  1100. }
  1101. // Entries are stored relative to project folder, but we want their absolute paths now
  1102. makeEntriesAbsolute();
  1103. // Load resource manifest
  1104. Path resourceManifestPath = mProjectFolder;
  1105. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1106. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1107. if (FileSystem::exists(resourceManifestPath))
  1108. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1109. else
  1110. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1111. gResources().registerResourceManifest(mResourceManifest);
  1112. // Load all meta files
  1113. Stack<DirectoryEntry*> todo;
  1114. todo.push(mRootEntry);
  1115. Vector<LibraryEntry*> deletedEntries;
  1116. while(!todo.empty())
  1117. {
  1118. DirectoryEntry* curDir = todo.top();
  1119. todo.pop();
  1120. for(auto& child : curDir->mChildren)
  1121. {
  1122. if(child->type == LibraryEntryType::File)
  1123. {
  1124. FileEntry* resEntry = static_cast<FileEntry*>(child);
  1125. if (FileSystem::isFile(resEntry->path))
  1126. {
  1127. if (resEntry->meta == nullptr)
  1128. {
  1129. Path metaPath = resEntry->path;
  1130. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1131. if (FileSystem::isFile(metaPath))
  1132. {
  1133. FileDecoder fs(metaPath);
  1134. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  1135. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1136. {
  1137. ProjectFileMetaPtr fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1138. resEntry->meta = fileMeta;
  1139. }
  1140. }
  1141. }
  1142. if (resEntry->meta != nullptr)
  1143. {
  1144. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1145. if (resourceMetas.size() > 0)
  1146. {
  1147. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1148. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1149. {
  1150. ProjectResourceMetaPtr entry = resourceMetas[i];
  1151. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1152. }
  1153. }
  1154. }
  1155. addDependencies(resEntry);
  1156. }
  1157. else
  1158. deletedEntries.push_back(resEntry);
  1159. }
  1160. else if(child->type == LibraryEntryType::Directory)
  1161. {
  1162. if (FileSystem::isDirectory(child->path))
  1163. todo.push(static_cast<DirectoryEntry*>(child));
  1164. else
  1165. deletedEntries.push_back(child);
  1166. }
  1167. }
  1168. }
  1169. // Remove entries that no longer have corresponding files
  1170. for (auto& deletedEntry : deletedEntries)
  1171. {
  1172. if (deletedEntry->type == LibraryEntryType::File)
  1173. {
  1174. FileEntry* resEntry = static_cast<FileEntry*>(deletedEntry);
  1175. deleteResourceInternal(resEntry);
  1176. }
  1177. else
  1178. {
  1179. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1180. deleteDirectoryInternal(dirEntry);
  1181. }
  1182. }
  1183. // Clean up internal library folder from obsolete files
  1184. Path internalResourcesFolder = mProjectFolder;
  1185. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1186. Vector<Path> toDelete;
  1187. auto processFile = [&](const Path& file)
  1188. {
  1189. String uuid = file.getFilename(false);
  1190. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1191. {
  1192. mResourceManifest->unregisterResource(uuid);
  1193. toDelete.push_back(file);
  1194. }
  1195. return true;
  1196. };
  1197. FileSystem::iterate(internalResourcesFolder, processFile);
  1198. for (auto& entry : toDelete)
  1199. FileSystem::remove(entry);
  1200. mIsLoaded = true;
  1201. }
  1202. void ProjectLibrary::clearEntries()
  1203. {
  1204. if (mRootEntry == nullptr)
  1205. return;
  1206. std::function<void(LibraryEntry*)> deleteRecursive =
  1207. [&](LibraryEntry* entry)
  1208. {
  1209. if (entry->type == LibraryEntryType::Directory)
  1210. {
  1211. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1212. for (auto& child : dirEntry->mChildren)
  1213. deleteRecursive(child);
  1214. }
  1215. bs_delete(entry);
  1216. };
  1217. deleteRecursive(mRootEntry);
  1218. mRootEntry = nullptr;
  1219. }
  1220. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1221. {
  1222. Vector<Path> output;
  1223. if (entry->meta == nullptr)
  1224. return output;
  1225. auto& resourceMetas = entry->meta->getResourceMetaData();
  1226. for(auto& resMeta : resourceMetas)
  1227. {
  1228. if (resMeta->getTypeID() == TID_Shader)
  1229. {
  1230. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1231. for (auto& include : metaData->includes)
  1232. output.push_back(include);
  1233. }
  1234. }
  1235. return output;
  1236. }
  1237. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1238. {
  1239. Vector<Path> dependencies = getImportDependencies(entry);
  1240. for (auto& dependency : dependencies)
  1241. mDependencies[dependency].push_back(entry->path);
  1242. }
  1243. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1244. {
  1245. Vector<Path> dependencies = getImportDependencies(entry);
  1246. for (auto& dependency : dependencies)
  1247. {
  1248. Vector<Path>& curDependencies = mDependencies[dependency];
  1249. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1250. [&](const Path& x)
  1251. {
  1252. return x == entry->path;
  1253. });
  1254. curDependencies.erase(iterRemove, curDependencies.end());
  1255. }
  1256. }
  1257. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1258. {
  1259. auto iterFind = mDependencies.find(entryPath);
  1260. if (iterFind == mDependencies.end())
  1261. return;
  1262. // Make a copy since we might modify this list during reimport
  1263. Vector<Path> dependencies = iterFind->second;
  1264. for (auto& dependency : dependencies)
  1265. {
  1266. LibraryEntry* entry = findEntry(dependency);
  1267. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1268. {
  1269. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1270. ImportOptionsPtr importOptions;
  1271. if (resEntry->meta != nullptr)
  1272. importOptions = resEntry->meta->getImportOptions();
  1273. reimportResourceInternal(resEntry, importOptions, true);
  1274. }
  1275. }
  1276. }
  1277. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1278. {
  1279. return ProjectLibrary::instance();
  1280. }
  1281. }