BsProjectLibrary.cpp 44 KB

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