BsProjectLibrary.cpp 53 KB

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