BsProjectLibrary.cpp 47 KB

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