BsProjectLibrary.cpp 44 KB

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