BsProjectLibrary.cpp 44 KB

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