BsProjectLibrary.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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. :type(LibraryEntryType::Directory), parent(nullptr)
  29. { }
  30. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  31. :type(type), path(path), elementName(name), parent(parent)
  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 SPtr<ImportOptions>& 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 SPtr<ImportOptions>& 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. SPtr<IReflectable> loadedMeta = fs.decode();
  310. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  311. {
  312. SPtr<ProjectFileMeta> 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. SPtr<ProjectResourceMeta> 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. SPtr<ImportOptions> 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, ResourceLoadFlag::KeepSourceData) });
  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. SPtr<ResourceMetaData> subMeta = entry.value->getMetaData();
  364. UINT32 typeId = entry.value->getTypeId();
  365. const String& UUID = entry.value.getUUID();
  366. SPtr<ProjectResourceMeta> 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<SPtr<ProjectResourceMeta>> 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. SPtr<ProjectResourceMeta> 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. SPtr<ResourceMetaData> subMeta = resEntry.value->getMetaData();
  413. UINT32 typeId = resEntry.value->getTypeId();
  414. const String& UUID = importedResource.getUUID();
  415. SPtr<ProjectResourceMeta> 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. SPtr<ProjectResourceMeta> 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. if(filePath.isEmpty())
  685. {
  686. LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
  687. return;
  688. }
  689. filePath.makeAbsolute(getResourcesFolder());
  690. Resources::instance().save(resource, filePath, true);
  691. checkForModifications(filePath);
  692. }
  693. void ProjectLibrary::createFolderEntry(const Path& path)
  694. {
  695. Path fullPath = path;
  696. if (fullPath.isAbsolute())
  697. {
  698. if (!mResourcesFolder.includes(fullPath))
  699. return;
  700. }
  701. else
  702. fullPath.makeAbsolute(mResourcesFolder);
  703. if (FileSystem::isDirectory(fullPath))
  704. return; // Already exists
  705. FileSystem::createDir(fullPath);
  706. Path parentPath = fullPath.getParent();
  707. DirectoryEntry* newEntryParent = nullptr;
  708. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  709. if (newEntryParentLib != nullptr)
  710. {
  711. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  712. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  713. }
  714. DirectoryEntry* newHierarchyParent = nullptr;
  715. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  716. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  717. addDirectoryInternal(newEntryParent, fullPath);
  718. }
  719. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  720. {
  721. Path oldFullPath = oldPath;
  722. if (!oldFullPath.isAbsolute())
  723. oldFullPath.makeAbsolute(mResourcesFolder);
  724. Path newFullPath = newPath;
  725. if (!newFullPath.isAbsolute())
  726. newFullPath.makeAbsolute(mResourcesFolder);
  727. Path parentPath = newFullPath.getParent();
  728. if (!FileSystem::isDirectory(parentPath))
  729. {
  730. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  731. return;
  732. }
  733. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  734. FileSystem::move(oldFullPath, newFullPath, overwrite);
  735. Path oldMetaPath = getMetaPath(oldFullPath);
  736. Path newMetaPath = getMetaPath(newFullPath);
  737. LibraryEntry* oldEntry = findEntry(oldFullPath);
  738. if(oldEntry != nullptr) // Moving from the Resources folder
  739. {
  740. // Moved outside of Resources, delete entry & meta file
  741. if (!mResourcesFolder.includes(newFullPath))
  742. {
  743. if(oldEntry->type == LibraryEntryType::File)
  744. deleteResourceInternal(static_cast<FileEntry*>(oldEntry));
  745. else if(oldEntry->type == LibraryEntryType::Directory)
  746. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  747. }
  748. else // Just moving internally
  749. {
  750. onEntryRemoved(oldEntry->path);
  751. FileEntry* fileEntry = nullptr;
  752. if (oldEntry->type == LibraryEntryType::File)
  753. {
  754. fileEntry = static_cast<FileEntry*>(oldEntry);
  755. removeDependencies(fileEntry);
  756. // Update uuid <-> path mapping
  757. if(fileEntry->meta != nullptr)
  758. {
  759. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  760. if (resourceMetas.size() > 0)
  761. {
  762. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  763. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  764. {
  765. SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
  766. const String& UUID = resMeta->getUUID();
  767. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  768. }
  769. }
  770. }
  771. }
  772. if(FileSystem::isFile(oldMetaPath))
  773. FileSystem::move(oldMetaPath, newMetaPath);
  774. DirectoryEntry* parent = oldEntry->parent;
  775. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  776. if(findIter != parent->mChildren.end())
  777. parent->mChildren.erase(findIter);
  778. Path parentPath = newFullPath.getParent();
  779. DirectoryEntry* newEntryParent = nullptr;
  780. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  781. if(newEntryParentLib != nullptr)
  782. {
  783. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  784. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  785. }
  786. DirectoryEntry* newHierarchyParent = nullptr;
  787. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  788. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  789. newEntryParent->mChildren.push_back(oldEntry);
  790. oldEntry->parent = newEntryParent;
  791. oldEntry->path = newFullPath;
  792. oldEntry->elementName = newFullPath.getWTail();
  793. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  794. {
  795. Stack<LibraryEntry*> todo;
  796. todo.push(oldEntry);
  797. while(!todo.empty())
  798. {
  799. LibraryEntry* curEntry = todo.top();
  800. todo.pop();
  801. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  802. for(auto& child : curDirEntry->mChildren)
  803. {
  804. child->path = child->parent->path;
  805. child->path.append(child->elementName);
  806. if(child->type == LibraryEntryType::Directory)
  807. todo.push(child);
  808. }
  809. }
  810. }
  811. onEntryAdded(oldEntry->path);
  812. if (fileEntry != nullptr)
  813. {
  814. reimportDependants(oldFullPath);
  815. reimportDependants(newFullPath);
  816. }
  817. }
  818. }
  819. else // Moving from outside of the Resources folder (likely adding a new resource)
  820. {
  821. checkForModifications(newFullPath);
  822. }
  823. }
  824. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  825. {
  826. Path oldFullPath = oldPath;
  827. if (!oldFullPath.isAbsolute())
  828. oldFullPath.makeAbsolute(mResourcesFolder);
  829. Path newFullPath = newPath;
  830. if (!newFullPath.isAbsolute())
  831. newFullPath.makeAbsolute(mResourcesFolder);
  832. if (!FileSystem::exists(oldFullPath))
  833. return;
  834. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  835. // Copying a file/folder outside of the Resources folder, no special handling needed
  836. if (!mResourcesFolder.includes(newFullPath))
  837. return;
  838. Path parentPath = newFullPath.getParent();
  839. DirectoryEntry* newEntryParent = nullptr;
  840. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  841. if (newEntryParentLib != nullptr)
  842. {
  843. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  844. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  845. }
  846. // If the source is outside of Resources folder, just plain import the copy
  847. LibraryEntry* oldEntry = findEntry(oldFullPath);
  848. if (oldEntry == nullptr)
  849. {
  850. checkForModifications(newFullPath);
  851. return;
  852. }
  853. // Both source and destination are within Resources folder, need to preserve import options on the copies
  854. LibraryEntry* newEntry = nullptr;
  855. if (FileSystem::isFile(newFullPath))
  856. {
  857. assert(oldEntry->type == LibraryEntryType::File);
  858. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  859. SPtr<ImportOptions> importOptions;
  860. if (oldResEntry->meta != nullptr)
  861. importOptions = oldResEntry->meta->getImportOptions();
  862. newEntry = addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  863. }
  864. else
  865. {
  866. assert(oldEntry->type == LibraryEntryType::File);
  867. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  868. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  869. newEntry = newDirEntry;
  870. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  871. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  872. while (!todo.empty())
  873. {
  874. auto current = todo.top();
  875. todo.pop();
  876. DirectoryEntry* sourceDir = std::get<0>(current);
  877. DirectoryEntry* destDir = std::get<1>(current);
  878. for (auto& child : sourceDir->mChildren)
  879. {
  880. Path childDestPath = destDir->path;
  881. childDestPath.append(child->path.getWTail());
  882. if (child->type == LibraryEntryType::File)
  883. {
  884. FileEntry* childResEntry = static_cast<FileEntry*>(child);
  885. SPtr<ImportOptions> importOptions;
  886. if (childResEntry->meta != nullptr)
  887. importOptions = childResEntry->meta->getImportOptions();
  888. addResourceInternal(destDir, childDestPath, importOptions, true);
  889. }
  890. else // Directory
  891. {
  892. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  893. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  894. todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
  895. }
  896. }
  897. }
  898. }
  899. }
  900. void ProjectLibrary::deleteEntry(const Path& path)
  901. {
  902. Path fullPath = path;
  903. if (!fullPath.isAbsolute())
  904. fullPath.makeAbsolute(mResourcesFolder);
  905. if(FileSystem::exists(fullPath))
  906. FileSystem::remove(fullPath);
  907. LibraryEntry* entry = findEntry(fullPath);
  908. if(entry != nullptr)
  909. {
  910. if(entry->type == LibraryEntryType::File)
  911. deleteResourceInternal(static_cast<FileEntry*>(entry));
  912. else if(entry->type == LibraryEntryType::Directory)
  913. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  914. }
  915. }
  916. void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport)
  917. {
  918. LibraryEntry* entry = findEntry(path);
  919. if (entry != nullptr)
  920. {
  921. if (entry->type == LibraryEntryType::File)
  922. {
  923. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  924. reimportResourceInternal(resEntry, importOptions, forceReimport);
  925. }
  926. }
  927. }
  928. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  929. {
  930. LibraryEntry* entry = findEntry(path);
  931. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  932. return;
  933. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  934. if (resEntry->meta == nullptr)
  935. return;
  936. resEntry->meta->setIncludeInBuild(include);
  937. Path metaPath = resEntry->path;
  938. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  939. FileEncoder fs(metaPath);
  940. fs.encode(resEntry->meta.get());
  941. }
  942. void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
  943. {
  944. LibraryEntry* entry = findEntry(path);
  945. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  946. return;
  947. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  948. if (fileEntry->meta == nullptr)
  949. return;
  950. SPtr<ProjectResourceMeta> resMeta;
  951. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  952. for (auto& resMetaEntry : resourceMetas)
  953. {
  954. if (resMeta->getUniqueName() == path.getWTail())
  955. resMeta = resMetaEntry;
  956. }
  957. if (resMeta == nullptr)
  958. return;
  959. resMeta->mUserData = userData;
  960. Path metaPath = fileEntry->path;
  961. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  962. FileEncoder fs(metaPath);
  963. fs.encode(fileEntry->meta.get());
  964. }
  965. Vector<ProjectLibrary::FileEntry*> ProjectLibrary::getResourcesForBuild() const
  966. {
  967. Vector<FileEntry*> output;
  968. Stack<DirectoryEntry*> todo;
  969. todo.push(mRootEntry);
  970. while (!todo.empty())
  971. {
  972. DirectoryEntry* directory = todo.top();
  973. todo.pop();
  974. for (auto& child : directory->mChildren)
  975. {
  976. if (child->type == LibraryEntryType::File)
  977. {
  978. FileEntry* resEntry = static_cast<FileEntry*>(child);
  979. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  980. output.push_back(resEntry);
  981. }
  982. else if (child->type == LibraryEntryType::Directory)
  983. {
  984. todo.push(static_cast<DirectoryEntry*>(child));
  985. }
  986. }
  987. }
  988. return output;
  989. }
  990. HResource ProjectLibrary::load(const Path& path)
  991. {
  992. SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
  993. if (meta == nullptr)
  994. return HResource();
  995. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  996. String resUUID = meta->getUUID();
  997. return gResources().loadFromUUID(resUUID, false, loadFlags);
  998. }
  999. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  1000. {
  1001. Path parentPath = fullPath;
  1002. DirectoryEntry* newEntryParent = nullptr;
  1003. Stack<Path> parentPaths;
  1004. do
  1005. {
  1006. Path newParentPath = parentPath.getParent();
  1007. if(newParentPath == parentPath)
  1008. break;
  1009. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  1010. if(newEntryParentLib != nullptr)
  1011. {
  1012. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1013. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1014. break;
  1015. }
  1016. parentPaths.push(newParentPath);
  1017. parentPath = newParentPath;
  1018. } while (true);
  1019. assert(newEntryParent != nullptr); // Must exist
  1020. if(newHierarchyRoot != nullptr)
  1021. *newHierarchyRoot = newEntryParent;
  1022. while(!parentPaths.empty())
  1023. {
  1024. Path curPath = parentPaths.top();
  1025. parentPaths.pop();
  1026. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  1027. }
  1028. if(newHierarchyLeaf != nullptr)
  1029. *newHierarchyLeaf = newEntryParent;
  1030. }
  1031. Path ProjectLibrary::getMetaPath(const Path& path) const
  1032. {
  1033. Path metaPath = path;
  1034. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1035. return metaPath;
  1036. }
  1037. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1038. {
  1039. return fullPath.getWExtension() == L".meta";
  1040. }
  1041. bool ProjectLibrary::isNative(const Path& path) const
  1042. {
  1043. WString extension = path.getWExtension();
  1044. return extension == L".asset" || extension == L".prefab";
  1045. }
  1046. void ProjectLibrary::unloadLibrary()
  1047. {
  1048. if (!mIsLoaded)
  1049. return;
  1050. mProjectFolder = Path::BLANK;
  1051. mResourcesFolder = Path::BLANK;
  1052. clearEntries();
  1053. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1054. mDependencies.clear();
  1055. gResources().unregisterResourceManifest(mResourceManifest);
  1056. mResourceManifest = nullptr;
  1057. mIsLoaded = false;
  1058. }
  1059. void ProjectLibrary::makeEntriesRelative()
  1060. {
  1061. // Make all paths relative before saving
  1062. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1063. [&](LibraryEntry* entry, const Path& root)
  1064. {
  1065. entry->path.makeRelative(root);
  1066. if (entry->type == LibraryEntryType::Directory)
  1067. {
  1068. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1069. for (auto& child : dirEntry->mChildren)
  1070. makeRelative(child, root);
  1071. }
  1072. };
  1073. Path root = getResourcesFolder();
  1074. makeRelative(mRootEntry, root);
  1075. }
  1076. void ProjectLibrary::makeEntriesAbsolute()
  1077. {
  1078. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1079. [&](LibraryEntry* entry, const Path& root)
  1080. {
  1081. entry->path.makeAbsolute(root);
  1082. if (entry->type == LibraryEntryType::Directory)
  1083. {
  1084. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1085. for (auto& child : dirEntry->mChildren)
  1086. makeAbsolute(child, root);
  1087. }
  1088. };
  1089. Path root = getResourcesFolder();
  1090. makeAbsolute(mRootEntry, root);
  1091. }
  1092. void ProjectLibrary::saveLibrary()
  1093. {
  1094. if (!mIsLoaded)
  1095. return;
  1096. // Make all paths relative before saving
  1097. makeEntriesRelative();
  1098. SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  1099. Path libraryEntriesPath = mProjectFolder;
  1100. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1101. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1102. FileEncoder fs(libraryEntriesPath);
  1103. fs.encode(libEntries.get());
  1104. // Restore absolute entry paths
  1105. makeEntriesAbsolute();
  1106. Path resourceManifestPath = mProjectFolder;
  1107. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1108. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1109. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1110. }
  1111. void ProjectLibrary::loadLibrary()
  1112. {
  1113. unloadLibrary();
  1114. mProjectFolder = gEditorApplication().getProjectPath();
  1115. mResourcesFolder = mProjectFolder;
  1116. mResourcesFolder.append(RESOURCES_DIR);
  1117. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  1118. Path libraryEntriesPath = mProjectFolder;
  1119. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1120. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1121. if(FileSystem::exists(libraryEntriesPath))
  1122. {
  1123. FileDecoder fs(libraryEntriesPath);
  1124. SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1125. *mRootEntry = libEntries->getRootEntry();
  1126. for(auto& child : mRootEntry->mChildren)
  1127. child->parent = mRootEntry;
  1128. mRootEntry->parent = nullptr;
  1129. }
  1130. // Entries are stored relative to project folder, but we want their absolute paths now
  1131. makeEntriesAbsolute();
  1132. // Load resource manifest
  1133. Path resourceManifestPath = mProjectFolder;
  1134. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1135. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1136. if (FileSystem::exists(resourceManifestPath))
  1137. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1138. else
  1139. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1140. gResources().registerResourceManifest(mResourceManifest);
  1141. // Load all meta files
  1142. Stack<DirectoryEntry*> todo;
  1143. todo.push(mRootEntry);
  1144. Vector<LibraryEntry*> deletedEntries;
  1145. while(!todo.empty())
  1146. {
  1147. DirectoryEntry* curDir = todo.top();
  1148. todo.pop();
  1149. for(auto& child : curDir->mChildren)
  1150. {
  1151. if(child->type == LibraryEntryType::File)
  1152. {
  1153. FileEntry* resEntry = static_cast<FileEntry*>(child);
  1154. if (FileSystem::isFile(resEntry->path))
  1155. {
  1156. if (resEntry->meta == nullptr)
  1157. {
  1158. Path metaPath = resEntry->path;
  1159. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  1160. if (FileSystem::isFile(metaPath))
  1161. {
  1162. FileDecoder fs(metaPath);
  1163. SPtr<IReflectable> loadedMeta = fs.decode();
  1164. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1165. {
  1166. SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1167. resEntry->meta = fileMeta;
  1168. }
  1169. }
  1170. }
  1171. if (resEntry->meta != nullptr)
  1172. {
  1173. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1174. if (resourceMetas.size() > 0)
  1175. {
  1176. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1177. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1178. {
  1179. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  1180. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1181. }
  1182. }
  1183. }
  1184. addDependencies(resEntry);
  1185. }
  1186. else
  1187. deletedEntries.push_back(resEntry);
  1188. }
  1189. else if(child->type == LibraryEntryType::Directory)
  1190. {
  1191. if (FileSystem::isDirectory(child->path))
  1192. todo.push(static_cast<DirectoryEntry*>(child));
  1193. else
  1194. deletedEntries.push_back(child);
  1195. }
  1196. }
  1197. }
  1198. // Remove entries that no longer have corresponding files
  1199. for (auto& deletedEntry : deletedEntries)
  1200. {
  1201. if (deletedEntry->type == LibraryEntryType::File)
  1202. {
  1203. FileEntry* resEntry = static_cast<FileEntry*>(deletedEntry);
  1204. deleteResourceInternal(resEntry);
  1205. }
  1206. else
  1207. {
  1208. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1209. deleteDirectoryInternal(dirEntry);
  1210. }
  1211. }
  1212. // Clean up internal library folder from obsolete files
  1213. Path internalResourcesFolder = mProjectFolder;
  1214. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1215. if (FileSystem::exists(internalResourcesFolder))
  1216. {
  1217. Vector<Path> toDelete;
  1218. auto processFile = [&](const Path& file)
  1219. {
  1220. String uuid = file.getFilename(false);
  1221. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1222. {
  1223. mResourceManifest->unregisterResource(uuid);
  1224. toDelete.push_back(file);
  1225. }
  1226. return true;
  1227. };
  1228. FileSystem::iterate(internalResourcesFolder, processFile);
  1229. for (auto& entry : toDelete)
  1230. FileSystem::remove(entry);
  1231. }
  1232. mIsLoaded = true;
  1233. }
  1234. void ProjectLibrary::clearEntries()
  1235. {
  1236. if (mRootEntry == nullptr)
  1237. return;
  1238. std::function<void(LibraryEntry*)> deleteRecursive =
  1239. [&](LibraryEntry* entry)
  1240. {
  1241. if (entry->type == LibraryEntryType::Directory)
  1242. {
  1243. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1244. for (auto& child : dirEntry->mChildren)
  1245. deleteRecursive(child);
  1246. }
  1247. bs_delete(entry);
  1248. };
  1249. deleteRecursive(mRootEntry);
  1250. mRootEntry = nullptr;
  1251. }
  1252. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1253. {
  1254. Vector<Path> output;
  1255. if (entry->meta == nullptr)
  1256. return output;
  1257. auto& resourceMetas = entry->meta->getResourceMetaData();
  1258. for(auto& resMeta : resourceMetas)
  1259. {
  1260. if (resMeta->getTypeID() == TID_Shader)
  1261. {
  1262. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1263. for (auto& include : metaData->includes)
  1264. output.push_back(include);
  1265. }
  1266. }
  1267. return output;
  1268. }
  1269. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1270. {
  1271. Vector<Path> dependencies = getImportDependencies(entry);
  1272. for (auto& dependency : dependencies)
  1273. mDependencies[dependency].push_back(entry->path);
  1274. }
  1275. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1276. {
  1277. Vector<Path> dependencies = getImportDependencies(entry);
  1278. for (auto& dependency : dependencies)
  1279. {
  1280. Vector<Path>& curDependencies = mDependencies[dependency];
  1281. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1282. [&](const Path& x)
  1283. {
  1284. return x == entry->path;
  1285. });
  1286. curDependencies.erase(iterRemove, curDependencies.end());
  1287. }
  1288. }
  1289. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1290. {
  1291. auto iterFind = mDependencies.find(entryPath);
  1292. if (iterFind == mDependencies.end())
  1293. return;
  1294. // Make a copy since we might modify this list during reimport
  1295. Vector<Path> dependencies = iterFind->second;
  1296. for (auto& dependency : dependencies)
  1297. {
  1298. LibraryEntry* entry = findEntry(dependency);
  1299. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1300. {
  1301. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1302. SPtr<ImportOptions> importOptions;
  1303. if (resEntry->meta != nullptr)
  1304. importOptions = resEntry->meta->getImportOptions();
  1305. reimportResourceInternal(resEntry, importOptions, true);
  1306. }
  1307. }
  1308. }
  1309. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1310. {
  1311. return ProjectLibrary::instance();
  1312. }
  1313. }