BsProjectLibrary.cpp 48 KB

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