BsProjectLibrary.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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)
  269. {
  270. FileEntry* newResource = bs_new<FileEntry>(filePath, filePath.getTail(), parent);
  271. parent->mChildren.push_back(newResource);
  272. reimportResourceInternal(newResource, importOptions, forceReimport);
  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)
  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. }
  401. // Needs to be pass a weak pointer to worker methods since internally it holds a reference to the task itself,
  402. // and we can't have the task closure holding a reference back, otherwise it leaks
  403. std::weak_ptr<QueuedImport> queuedImportWeak = queuedImport;
  404. if(!isNativeResource)
  405. {
  406. // Find UUIDs for any existing sub-resources
  407. if (fileEntry->meta != nullptr)
  408. {
  409. const Vector<SPtr<ProjectResourceMeta>>& resourceMetas = fileEntry->meta->getAllResourceMetaData();
  410. for (auto& entry : resourceMetas)
  411. queuedImport->resources.emplace_back(entry->getUniqueName(), nullptr, entry->getUUID());
  412. }
  413. // Perform import, register the resources and their UUID in the QueuedImport structure and save the
  414. // resource on disk
  415. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  416. {
  417. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  418. Vector<SubResourceRaw> importedResources = gImporter()._importAll(queuedImport->filePath,
  419. queuedImport->importOptions);
  420. if (!importedResources.empty())
  421. {
  422. Path outputPath = projectFolder;
  423. outputPath.append(INTERNAL_RESOURCES_DIR);
  424. if (!FileSystem::isDirectory(outputPath))
  425. FileSystem::createDir(outputPath);
  426. for (auto& entry : importedResources)
  427. {
  428. String subresourceName = entry.name;
  429. Path::stripInvalid(subresourceName);
  430. UUID uuid;
  431. {
  432. // Any access to queuedImport->resources must be locked
  433. Lock lock(mutex);
  434. auto iterFind = std::find_if(queuedImport->resources.begin(), queuedImport->resources.end(),
  435. [&subresourceName](const QueuedImportResource& importResource)
  436. {
  437. return importResource.name == subresourceName;
  438. });
  439. if (iterFind != queuedImport->resources.end())
  440. iterFind->resource = entry.value;
  441. else
  442. {
  443. queuedImport->resources.push_back(QueuedImportResource(entry.name, entry.value,
  444. UUID::EMPTY));
  445. iterFind = queuedImport->resources.end() - 1;
  446. }
  447. if (iterFind->uuid.empty())
  448. iterFind->uuid = UUIDGenerator::generateRandom();
  449. uuid = iterFind->uuid;
  450. }
  451. const String uuidStr = uuid.toString();
  452. outputPath.setFilename(uuidStr + ".asset");
  453. gResources()._save(entry.value, outputPath, true);
  454. }
  455. }
  456. };
  457. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  458. dependency);
  459. }
  460. else
  461. {
  462. // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
  463. // This can happen if library isn't properly saved before exiting the application.
  464. if (fileEntry->meta != nullptr)
  465. {
  466. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  467. mResourceManifest->registerResource(resourceMetas[0]->getUUID(), fileEntry->path);
  468. }
  469. const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
  470. {
  471. // Don't load dependencies because we don't need them, but also because they might not be in the
  472. // manifest which would screw up their UUIDs.
  473. SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
  474. HResource resource = gResources().load(queuedImport->filePath, ResourceLoadFlag::KeepSourceData);
  475. if (resource)
  476. {
  477. Path outputPath = projectFolder;
  478. outputPath.append(INTERNAL_RESOURCES_DIR);
  479. if (!FileSystem::isDirectory(outputPath))
  480. FileSystem::createDir(outputPath);
  481. {
  482. // Any access to queuedImport->resources must be locked
  483. Lock lock(mutex);
  484. queuedImport->resources.push_back(QueuedImportResource("primary", resource.getInternalPtr(),
  485. resource.getUUID()));
  486. }
  487. const String uuidStr = resource.getUUID().toString();
  488. outputPath.setFilename(uuidStr + ".asset");
  489. gResources()._save(resource.getInternalPtr(), outputPath, true);
  490. }
  491. };
  492. queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
  493. dependency);
  494. }
  495. TaskScheduler::instance().addTask(queuedImport->importTask);
  496. mQueuedImports[fileEntry] = queuedImport;
  497. fileEntry->lastUpdateTime = std::time(nullptr);
  498. return true;
  499. }
  500. return false;
  501. }
  502. void ProjectLibrary::_finishQueuedImports(bool wait)
  503. {
  504. for(auto iter = mQueuedImports.begin(); iter != mQueuedImports.end();)
  505. {
  506. SPtr<QueuedImport> queuedImport = iter->second;
  507. if (!queuedImport->importTask->isComplete())
  508. {
  509. if (wait)
  510. queuedImport->importTask->wait();
  511. else
  512. {
  513. ++iter;
  514. continue;
  515. }
  516. }
  517. // The task is done, we can remove it
  518. FileEntry* fileEntry = iter->first;
  519. iter = mQueuedImports.erase(iter);
  520. // We wait on canceled task to finish and then just discard the results because any dependant tasks need to be
  521. // aware this tasks exists, so we can't just remove it straight away.
  522. if(queuedImport->canceled)
  523. {
  524. // Just move on to the next import...
  525. continue;
  526. }
  527. Path metaPath = fileEntry->path;
  528. metaPath.setFilename(metaPath.getFilename() + ".meta");
  529. Vector<SPtr<ProjectResourceMeta>> existingMetas;
  530. if(fileEntry->meta == nullptr) // Build a brand new meta-file
  531. fileEntry->meta = ProjectFileMeta::create(queuedImport->importOptions);
  532. else // Existing meta-file, which needs to be updated
  533. {
  534. // Remove existing dependencies (they will be re-added later)
  535. removeDependencies(fileEntry);
  536. existingMetas = fileEntry->meta->getAllResourceMetaData();
  537. fileEntry->meta->clearResourceMetaData();
  538. fileEntry->meta->mImportOptions = queuedImport->importOptions;
  539. }
  540. Path internalResourcesPath = mProjectFolder;
  541. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  542. // See which sub-resource metas need to be updated, removed or added based on the new resource set
  543. bool isFirst = true;
  544. for (auto& entry : queuedImport->resources)
  545. {
  546. // Entries with no resources are sub-resources that used to exist in this file, but haven't been imported
  547. // this time
  548. if(!entry.resource)
  549. continue;
  550. Path::stripInvalid(entry.name);
  551. const ProjectResourceIcons icons = generatePreviewIcons(*entry.resource);
  552. bool foundMeta = false;
  553. for (auto iterMeta = existingMetas.begin(); iterMeta != existingMetas.end(); ++iterMeta)
  554. {
  555. const SPtr<ProjectResourceMeta>& metaEntry = *iterMeta;
  556. if (entry.name == metaEntry->getUniqueName())
  557. {
  558. // Make sure the UUID we used for saving the resource matches the current one (should always
  559. // be true unless the meta-data somehow changes while the async import is happening)
  560. assert(entry.uuid == metaEntry->getUUID());
  561. HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
  562. gResources().update(importedResource, entry.resource);
  563. metaEntry->setPreviewIcons(icons);
  564. fileEntry->meta->add(metaEntry);
  565. iterMeta = existingMetas.erase(iterMeta);
  566. foundMeta = true;
  567. break;
  568. }
  569. }
  570. if (!foundMeta)
  571. {
  572. HResource importedResource;
  573. // Native resources are always expected to have a handle since Resources::load was called during
  574. // the 'import' step
  575. if(queuedImport->native)
  576. {
  577. importedResource = gResources()._getResourceHandle(entry.uuid);
  578. gResources().update(importedResource, entry.resource);
  579. }
  580. else
  581. importedResource = gResources()._createResourceHandle(entry.resource, entry.uuid);
  582. SPtr<ResourceMetaData> subMeta = entry.resource->getMetaData();
  583. const UINT32 typeId = entry.resource->getTypeId();
  584. const UUID& UUID = importedResource.getUUID();
  585. SPtr<ProjectResourceMeta> resMeta = ProjectResourceMeta::create(entry.name, UUID, typeId,
  586. icons, subMeta);
  587. fileEntry->meta->add(resMeta);
  588. }
  589. // Keep resource metas that we are not currently using, in case they get restored so their references
  590. // don't get broken
  591. if (!queuedImport->pruneMetas)
  592. {
  593. for (auto& metaEntry : existingMetas)
  594. fileEntry->meta->addInactive(metaEntry);
  595. }
  596. // Update UUID to path mapping
  597. if(isFirst)
  598. mUUIDToPath[entry.uuid] = fileEntry->path;
  599. else
  600. mUUIDToPath[entry.uuid] = fileEntry->path + entry.name;
  601. isFirst = false;
  602. // Register path in manifest
  603. const String uuidStr = entry.uuid.toString();
  604. internalResourcesPath.setFilename(uuidStr + ".asset");
  605. mResourceManifest->registerResource(entry.uuid, internalResourcesPath);
  606. }
  607. // Save the meta file
  608. FileEncoder fs(metaPath);
  609. fs.encode(fileEntry->meta.get());
  610. // Register any dependencies this resource depends on
  611. addDependencies(fileEntry);
  612. // Notify the outside world import is doen
  613. onEntryImported(fileEntry->path);
  614. // Queue any resources dependant on this one for import
  615. reimportDependants(fileEntry->path);
  616. }
  617. }
  618. bool ProjectLibrary::isUpToDate(FileEntry* resource) const
  619. {
  620. if(resource->meta == nullptr)
  621. {
  622. // Allow no meta if import in progress
  623. const auto iterFind = mQueuedImports.find(resource);
  624. if(iterFind == mQueuedImports.end())
  625. return false;
  626. }
  627. else
  628. {
  629. auto& resourceMetas = resource->meta->getResourceMetaData();
  630. for (auto& resMeta : resourceMetas)
  631. {
  632. Path internalPath;
  633. if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
  634. return false;
  635. if (!FileSystem::isFile(internalPath))
  636. return false;
  637. }
  638. }
  639. const std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
  640. return lastModifiedTime <= resource->lastUpdateTime;
  641. }
  642. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const String& pattern)
  643. {
  644. return search(pattern, {});
  645. }
  646. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const String& pattern, const Vector<UINT32>& typeIds)
  647. {
  648. Vector<LibraryEntry*> foundEntries;
  649. std::regex escape("[.^$|()\\[\\]{}*+?\\\\]");
  650. String replace("\\\\&");
  651. String escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  652. // For some reason MSVC stdlib implementation requires a different pattern than stdlib one
  653. #if BS_PLATFORM == BS_PLATFORM_WIN32
  654. std::regex wildcard("\\\\\\*");
  655. #else
  656. std::regex wildcard("\\\\\\\\\\*");
  657. #endif
  658. String wildcardReplace(".*");
  659. String searchPattern = std::regex_replace(escapedPattern, wildcard, ".*");
  660. std::regex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
  661. Stack<DirectoryEntry*> todo;
  662. todo.push(mRootEntry);
  663. while (!todo.empty())
  664. {
  665. DirectoryEntry* dirEntry = todo.top();
  666. todo.pop();
  667. for (auto& child : dirEntry->mChildren)
  668. {
  669. if (std::regex_match(child->elementName, searchRegex))
  670. {
  671. if (typeIds.size() == 0)
  672. foundEntries.push_back(child);
  673. else
  674. {
  675. if (child->type == LibraryEntryType::File)
  676. {
  677. FileEntry* childFileEntry = static_cast<FileEntry*>(child);
  678. if (childFileEntry->meta != nullptr)
  679. {
  680. auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
  681. for (auto& typeId : typeIds)
  682. {
  683. bool found = false;
  684. for (auto& resMeta : resourceMetas)
  685. {
  686. if (resMeta->getTypeID() == typeId)
  687. {
  688. foundEntries.push_back(child);
  689. found = true;
  690. break;
  691. }
  692. }
  693. if (found)
  694. break;
  695. }
  696. }
  697. }
  698. }
  699. }
  700. if (child->type == LibraryEntryType::Directory)
  701. {
  702. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  703. todo.push(childDirEntry);
  704. }
  705. }
  706. }
  707. std::sort(foundEntries.begin(), foundEntries.end(),
  708. [&](const LibraryEntry* a, const LibraryEntry* b)
  709. {
  710. return a->elementName.compare(b->elementName) < 0;
  711. });
  712. return foundEntries;
  713. }
  714. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
  715. {
  716. Path fullPath = path;
  717. if (fullPath.isAbsolute())
  718. {
  719. if (!mResourcesFolder.includes(fullPath))
  720. return nullptr;
  721. }
  722. else
  723. fullPath.makeAbsolute(mResourcesFolder);
  724. Path relPath = fullPath.getRelative(mRootEntry->path);
  725. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  726. UINT32 idx = 0;
  727. LibraryEntry* current = mRootEntry;
  728. while (current != nullptr)
  729. {
  730. if (idx == numElems)
  731. return current;
  732. String curElem;
  733. if (relPath.isFile() && idx == (numElems - 1))
  734. curElem = relPath.getFilename();
  735. else
  736. curElem = relPath[idx];
  737. if (current->type == LibraryEntryType::Directory)
  738. {
  739. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  740. current = nullptr;
  741. for (auto& child : dirEntry->mChildren)
  742. {
  743. if (Path::comparePathElem(curElem, child->elementName))
  744. {
  745. idx++;
  746. current = child;
  747. break;
  748. }
  749. }
  750. }
  751. else // Found file
  752. {
  753. // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
  754. if (idx == (numElems - 1))
  755. return current;
  756. else
  757. break; // Not a valid path
  758. }
  759. }
  760. return nullptr;
  761. }
  762. bool ProjectLibrary::isSubresource(const Path& path) const
  763. {
  764. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  765. if (numElems <= 1)
  766. return false;
  767. Path filePath = path;
  768. filePath.makeParent();
  769. LibraryEntry* entry = findEntry(filePath);
  770. if (entry != nullptr && entry->type == LibraryEntryType::File)
  771. return true;
  772. return false;
  773. }
  774. SPtr<ProjectResourceMeta> ProjectLibrary::findResourceMeta(const Path& path) const
  775. {
  776. UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
  777. // Check if it is a subresource path
  778. if(numElems > 1)
  779. {
  780. Path filePath = path;
  781. filePath.makeParent();
  782. LibraryEntry* entry = findEntry(filePath);
  783. if (entry == nullptr)
  784. return nullptr;
  785. // Entry is a subresource
  786. if (entry->type == LibraryEntryType::File)
  787. {
  788. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  789. if (fileEntry->meta == nullptr)
  790. return nullptr;
  791. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  792. for(auto& resMeta : resourceMetas)
  793. {
  794. if (resMeta->getUniqueName() == path.getTail())
  795. return resMeta;
  796. }
  797. // Found the file but no subresource or meta information
  798. return nullptr;
  799. }
  800. else // Entry not a subresource
  801. {
  802. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  803. for (auto& child : dirEntry->mChildren)
  804. {
  805. if (Path::comparePathElem(path.getTail(), child->elementName))
  806. {
  807. if (child->type == LibraryEntryType::File)
  808. {
  809. FileEntry* fileEntry = static_cast<FileEntry*>(child);
  810. if (fileEntry->meta == nullptr)
  811. return nullptr;
  812. return fileEntry->meta->getResourceMetaData()[0];
  813. }
  814. }
  815. }
  816. return nullptr;
  817. }
  818. }
  819. // Not a subresource path, load directly
  820. {
  821. LibraryEntry* entry = findEntry(path);
  822. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  823. return nullptr;
  824. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  825. if (fileEntry->meta == nullptr)
  826. return nullptr;
  827. return fileEntry->meta->getResourceMetaData()[0];
  828. }
  829. }
  830. Path ProjectLibrary::uuidToPath(const UUID& uuid) const
  831. {
  832. auto iterFind = mUUIDToPath.find(uuid);
  833. if (iterFind != mUUIDToPath.end())
  834. return iterFind->second;
  835. return Path::BLANK;
  836. }
  837. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  838. {
  839. if (resource == nullptr)
  840. return;
  841. Path assetPath = path;
  842. if (path.isAbsolute())
  843. {
  844. if (!getResourcesFolder().includes(path))
  845. return;
  846. assetPath = path.getRelative(getResourcesFolder());
  847. }
  848. deleteEntry(assetPath);
  849. resource->setName(path.getFilename(false));
  850. Path absPath = assetPath.getAbsolute(getResourcesFolder());
  851. Resources::instance().save(resource, absPath, false);
  852. checkForModifications(absPath);
  853. }
  854. void ProjectLibrary::saveEntry(const HResource& resource)
  855. {
  856. if (resource == nullptr)
  857. return;
  858. Path filePath = uuidToPath(resource.getUUID());
  859. if(filePath.isEmpty())
  860. {
  861. LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
  862. return;
  863. }
  864. filePath.makeAbsolute(getResourcesFolder());
  865. Resources::instance().save(resource, filePath, true);
  866. checkForModifications(filePath);
  867. }
  868. void ProjectLibrary::createFolderEntry(const Path& path)
  869. {
  870. Path fullPath = path;
  871. if (fullPath.isAbsolute())
  872. {
  873. if (!mResourcesFolder.includes(fullPath))
  874. return;
  875. }
  876. else
  877. fullPath.makeAbsolute(mResourcesFolder);
  878. if (FileSystem::isDirectory(fullPath))
  879. return; // Already exists
  880. FileSystem::createDir(fullPath);
  881. Path parentPath = fullPath.getParent();
  882. DirectoryEntry* newEntryParent = nullptr;
  883. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  884. if (newEntryParentLib != nullptr)
  885. {
  886. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  887. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  888. }
  889. DirectoryEntry* newHierarchyParent = nullptr;
  890. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  891. createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
  892. addDirectoryInternal(newEntryParent, fullPath);
  893. }
  894. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  895. {
  896. Path oldFullPath = oldPath;
  897. if (!oldFullPath.isAbsolute())
  898. oldFullPath.makeAbsolute(mResourcesFolder);
  899. Path newFullPath = newPath;
  900. if (!newFullPath.isAbsolute())
  901. newFullPath.makeAbsolute(mResourcesFolder);
  902. Path parentPath = newFullPath.getParent();
  903. if (!FileSystem::isDirectory(parentPath))
  904. {
  905. LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
  906. return;
  907. }
  908. if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
  909. FileSystem::move(oldFullPath, newFullPath, overwrite);
  910. Path oldMetaPath = getMetaPath(oldFullPath);
  911. Path newMetaPath = getMetaPath(newFullPath);
  912. LibraryEntry* oldEntry = findEntry(oldFullPath);
  913. if(oldEntry != nullptr) // Moving from the Resources folder
  914. {
  915. // Moved outside of Resources, delete entry & meta file
  916. if (!mResourcesFolder.includes(newFullPath))
  917. {
  918. if(oldEntry->type == LibraryEntryType::File)
  919. deleteResourceInternal(static_cast<FileEntry*>(oldEntry));
  920. else if(oldEntry->type == LibraryEntryType::Directory)
  921. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  922. }
  923. else // Just moving internally
  924. {
  925. onEntryRemoved(oldEntry->path);
  926. FileEntry* fileEntry = nullptr;
  927. if (oldEntry->type == LibraryEntryType::File)
  928. {
  929. fileEntry = static_cast<FileEntry*>(oldEntry);
  930. removeDependencies(fileEntry);
  931. // Update uuid <-> path mapping
  932. if(fileEntry->meta != nullptr)
  933. {
  934. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  935. if (resourceMetas.size() > 0)
  936. {
  937. mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
  938. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  939. {
  940. SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
  941. const UUID& UUID = resMeta->getUUID();
  942. mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
  943. }
  944. }
  945. }
  946. }
  947. if(FileSystem::isFile(oldMetaPath))
  948. FileSystem::move(oldMetaPath, newMetaPath);
  949. DirectoryEntry* parent = oldEntry->parent;
  950. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  951. if(findIter != parent->mChildren.end())
  952. parent->mChildren.erase(findIter);
  953. Path parentPath = newFullPath.getParent();
  954. DirectoryEntry* newEntryParent = nullptr;
  955. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  956. if(newEntryParentLib != nullptr)
  957. {
  958. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  959. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  960. }
  961. DirectoryEntry* newHierarchyParent = nullptr;
  962. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  963. createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
  964. newEntryParent->mChildren.push_back(oldEntry);
  965. oldEntry->parent = newEntryParent;
  966. oldEntry->path = newFullPath;
  967. oldEntry->elementName = newFullPath.getTail();
  968. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  969. {
  970. Stack<LibraryEntry*> todo;
  971. todo.push(oldEntry);
  972. while(!todo.empty())
  973. {
  974. LibraryEntry* curEntry = todo.top();
  975. todo.pop();
  976. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  977. for(auto& child : curDirEntry->mChildren)
  978. {
  979. child->path = child->parent->path;
  980. child->path.append(child->elementName);
  981. if(child->type == LibraryEntryType::Directory)
  982. todo.push(child);
  983. }
  984. }
  985. }
  986. onEntryAdded(oldEntry->path);
  987. if (fileEntry != nullptr)
  988. {
  989. reimportDependants(oldFullPath);
  990. reimportDependants(newFullPath);
  991. }
  992. }
  993. }
  994. else // Moving from outside of the Resources folder (likely adding a new resource)
  995. {
  996. checkForModifications(newFullPath);
  997. }
  998. }
  999. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  1000. {
  1001. Path oldFullPath = oldPath;
  1002. if (!oldFullPath.isAbsolute())
  1003. oldFullPath.makeAbsolute(mResourcesFolder);
  1004. Path newFullPath = newPath;
  1005. if (!newFullPath.isAbsolute())
  1006. newFullPath.makeAbsolute(mResourcesFolder);
  1007. if (!FileSystem::exists(oldFullPath))
  1008. return;
  1009. FileSystem::copy(oldFullPath, newFullPath, overwrite);
  1010. // Copying a file/folder outside of the Resources folder, no special handling needed
  1011. if (!mResourcesFolder.includes(newFullPath))
  1012. return;
  1013. Path parentPath = newFullPath.getParent();
  1014. DirectoryEntry* newEntryParent = nullptr;
  1015. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  1016. if (newEntryParentLib != nullptr)
  1017. {
  1018. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1019. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1020. }
  1021. // If the source is outside of Resources folder, just plain import the copy
  1022. LibraryEntry* oldEntry = findEntry(oldFullPath);
  1023. if (oldEntry == nullptr)
  1024. {
  1025. checkForModifications(newFullPath);
  1026. return;
  1027. }
  1028. // Both source and destination are within Resources folder, need to preserve import options on the copies
  1029. if (FileSystem::isFile(newFullPath))
  1030. {
  1031. assert(oldEntry->type == LibraryEntryType::File);
  1032. FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
  1033. SPtr<ImportOptions> importOptions;
  1034. if (oldResEntry->meta != nullptr)
  1035. importOptions = oldResEntry->meta->getImportOptions();
  1036. addResourceInternal(newEntryParent, newFullPath, importOptions, true);
  1037. }
  1038. else
  1039. {
  1040. assert(oldEntry->type == LibraryEntryType::File);
  1041. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  1042. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath);
  1043. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  1044. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  1045. while (!todo.empty())
  1046. {
  1047. auto current = todo.top();
  1048. todo.pop();
  1049. DirectoryEntry* sourceDir = std::get<0>(current);
  1050. DirectoryEntry* destDir = std::get<1>(current);
  1051. for (auto& child : sourceDir->mChildren)
  1052. {
  1053. Path childDestPath = destDir->path;
  1054. childDestPath.append(child->path.getTail());
  1055. if (child->type == LibraryEntryType::File)
  1056. {
  1057. FileEntry* childResEntry = static_cast<FileEntry*>(child);
  1058. SPtr<ImportOptions> importOptions;
  1059. if (childResEntry->meta != nullptr)
  1060. importOptions = childResEntry->meta->getImportOptions();
  1061. addResourceInternal(destDir, childDestPath, importOptions, true);
  1062. }
  1063. else // Directory
  1064. {
  1065. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  1066. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  1067. todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. void ProjectLibrary::deleteEntry(const Path& path)
  1074. {
  1075. Path fullPath = path;
  1076. if (!fullPath.isAbsolute())
  1077. fullPath.makeAbsolute(mResourcesFolder);
  1078. if(FileSystem::exists(fullPath))
  1079. FileSystem::remove(fullPath);
  1080. LibraryEntry* entry = findEntry(fullPath);
  1081. if(entry != nullptr)
  1082. {
  1083. if(entry->type == LibraryEntryType::File)
  1084. deleteResourceInternal(static_cast<FileEntry*>(entry));
  1085. else if(entry->type == LibraryEntryType::Directory)
  1086. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  1087. }
  1088. }
  1089. void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport)
  1090. {
  1091. LibraryEntry* entry = findEntry(path);
  1092. if (entry != nullptr)
  1093. {
  1094. if (entry->type == LibraryEntryType::File)
  1095. {
  1096. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1097. reimportResourceInternal(resEntry, importOptions, forceReimport);
  1098. }
  1099. }
  1100. }
  1101. void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
  1102. {
  1103. LibraryEntry* entry = findEntry(path);
  1104. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1105. return;
  1106. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1107. if (resEntry->meta == nullptr)
  1108. return;
  1109. resEntry->meta->setIncludeInBuild(include);
  1110. Path metaPath = resEntry->path;
  1111. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1112. FileEncoder fs(metaPath);
  1113. fs.encode(resEntry->meta.get());
  1114. }
  1115. void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
  1116. {
  1117. LibraryEntry* entry = findEntry(path);
  1118. if (entry == nullptr || entry->type == LibraryEntryType::Directory)
  1119. return;
  1120. FileEntry* fileEntry = static_cast<FileEntry*>(entry);
  1121. SPtr<ProjectResourceMeta> resMeta = findResourceMeta(path);
  1122. if (resMeta == nullptr)
  1123. return;
  1124. resMeta->mUserData = userData;
  1125. Path metaPath = fileEntry->path;
  1126. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1127. FileEncoder fs(metaPath);
  1128. fs.encode(fileEntry->meta.get());
  1129. }
  1130. Vector<ProjectLibrary::FileEntry*> ProjectLibrary::getResourcesForBuild() const
  1131. {
  1132. Vector<FileEntry*> output;
  1133. Stack<DirectoryEntry*> todo;
  1134. todo.push(mRootEntry);
  1135. while (!todo.empty())
  1136. {
  1137. DirectoryEntry* directory = todo.top();
  1138. todo.pop();
  1139. for (auto& child : directory->mChildren)
  1140. {
  1141. if (child->type == LibraryEntryType::File)
  1142. {
  1143. FileEntry* resEntry = static_cast<FileEntry*>(child);
  1144. if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
  1145. output.push_back(resEntry);
  1146. }
  1147. else if (child->type == LibraryEntryType::Directory)
  1148. {
  1149. todo.push(static_cast<DirectoryEntry*>(child));
  1150. }
  1151. }
  1152. }
  1153. return output;
  1154. }
  1155. HResource ProjectLibrary::load(const Path& path)
  1156. {
  1157. SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
  1158. if (meta == nullptr)
  1159. return HResource();
  1160. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  1161. const UUID& resUUID = meta->getUUID();
  1162. return gResources().loadFromUUID(resUUID, false, loadFlags);
  1163. }
  1164. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  1165. {
  1166. Path parentPath = fullPath;
  1167. DirectoryEntry* newEntryParent = nullptr;
  1168. Stack<Path> parentPaths;
  1169. do
  1170. {
  1171. Path newParentPath = parentPath.getParent();
  1172. if(newParentPath == parentPath)
  1173. break;
  1174. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  1175. if(newEntryParentLib != nullptr)
  1176. {
  1177. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  1178. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  1179. break;
  1180. }
  1181. parentPaths.push(newParentPath);
  1182. parentPath = newParentPath;
  1183. } while (true);
  1184. assert(newEntryParent != nullptr); // Must exist
  1185. if(newHierarchyRoot != nullptr)
  1186. *newHierarchyRoot = newEntryParent;
  1187. while(!parentPaths.empty())
  1188. {
  1189. Path curPath = parentPaths.top();
  1190. parentPaths.pop();
  1191. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  1192. }
  1193. if(newHierarchyLeaf != nullptr)
  1194. *newHierarchyLeaf = newEntryParent;
  1195. }
  1196. Path ProjectLibrary::getMetaPath(const Path& path) const
  1197. {
  1198. Path metaPath = path;
  1199. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1200. return metaPath;
  1201. }
  1202. bool ProjectLibrary::isMeta(const Path& fullPath) const
  1203. {
  1204. return fullPath.getExtension() == ".meta";
  1205. }
  1206. bool ProjectLibrary::isNative(const Path& path) const
  1207. {
  1208. String extension = path.getExtension();
  1209. return extension == ".asset" || extension == ".prefab";
  1210. }
  1211. void ProjectLibrary::unloadLibrary()
  1212. {
  1213. if (!mIsLoaded)
  1214. return;
  1215. _finishQueuedImports(true);
  1216. mProjectFolder = Path::BLANK;
  1217. mResourcesFolder = Path::BLANK;
  1218. clearEntries();
  1219. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1220. mDependencies.clear();
  1221. gResources().unregisterResourceManifest(mResourceManifest);
  1222. mResourceManifest = nullptr;
  1223. mIsLoaded = false;
  1224. }
  1225. void ProjectLibrary::makeEntriesRelative()
  1226. {
  1227. // Make all paths relative before saving
  1228. std::function<void(LibraryEntry*, const Path&)> makeRelative =
  1229. [&](LibraryEntry* entry, const Path& root)
  1230. {
  1231. entry->path.makeRelative(root);
  1232. if (entry->type == LibraryEntryType::Directory)
  1233. {
  1234. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1235. for (auto& child : dirEntry->mChildren)
  1236. makeRelative(child, root);
  1237. }
  1238. };
  1239. Path root = getResourcesFolder();
  1240. makeRelative(mRootEntry, root);
  1241. }
  1242. void ProjectLibrary::makeEntriesAbsolute()
  1243. {
  1244. std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
  1245. [&](LibraryEntry* entry, const Path& root)
  1246. {
  1247. entry->path.makeAbsolute(root);
  1248. if (entry->type == LibraryEntryType::Directory)
  1249. {
  1250. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1251. for (auto& child : dirEntry->mChildren)
  1252. makeAbsolute(child, root);
  1253. }
  1254. };
  1255. Path root = getResourcesFolder();
  1256. makeAbsolute(mRootEntry, root);
  1257. }
  1258. void ProjectLibrary::saveLibrary()
  1259. {
  1260. if (!mIsLoaded)
  1261. return;
  1262. // Make all paths relative before saving
  1263. makeEntriesRelative();
  1264. SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  1265. Path libraryEntriesPath = mProjectFolder;
  1266. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1267. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1268. FileEncoder fs(libraryEntriesPath);
  1269. fs.encode(libEntries.get());
  1270. // Restore absolute entry paths
  1271. makeEntriesAbsolute();
  1272. Path resourceManifestPath = mProjectFolder;
  1273. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1274. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1275. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  1276. }
  1277. void ProjectLibrary::loadLibrary()
  1278. {
  1279. unloadLibrary();
  1280. mProjectFolder = gEditorApplication().getProjectPath();
  1281. mResourcesFolder = mProjectFolder;
  1282. mResourcesFolder.append(RESOURCES_DIR);
  1283. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
  1284. Path libraryEntriesPath = mProjectFolder;
  1285. libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
  1286. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  1287. if(FileSystem::exists(libraryEntriesPath))
  1288. {
  1289. FileDecoder fs(libraryEntriesPath);
  1290. SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  1291. *mRootEntry = libEntries->getRootEntry();
  1292. for(auto& child : mRootEntry->mChildren)
  1293. child->parent = mRootEntry;
  1294. mRootEntry->parent = nullptr;
  1295. }
  1296. // Entries are stored relative to project folder, but we want their absolute paths now
  1297. makeEntriesAbsolute();
  1298. // Load resource manifest
  1299. Path resourceManifestPath = mProjectFolder;
  1300. resourceManifestPath.append(PROJECT_INTERNAL_DIR);
  1301. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  1302. if (FileSystem::exists(resourceManifestPath))
  1303. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  1304. else
  1305. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  1306. gResources().registerResourceManifest(mResourceManifest);
  1307. // Load all meta files
  1308. Stack<DirectoryEntry*> todo;
  1309. todo.push(mRootEntry);
  1310. Vector<LibraryEntry*> deletedEntries;
  1311. while(!todo.empty())
  1312. {
  1313. DirectoryEntry* curDir = todo.top();
  1314. todo.pop();
  1315. for(auto& child : curDir->mChildren)
  1316. {
  1317. if(child->type == LibraryEntryType::File)
  1318. {
  1319. FileEntry* resEntry = static_cast<FileEntry*>(child);
  1320. if (FileSystem::isFile(resEntry->path))
  1321. {
  1322. if (resEntry->meta == nullptr)
  1323. {
  1324. Path metaPath = resEntry->path;
  1325. metaPath.setFilename(metaPath.getFilename() + ".meta");
  1326. if (FileSystem::isFile(metaPath))
  1327. {
  1328. FileDecoder fs(metaPath);
  1329. SPtr<IReflectable> loadedMeta = fs.decode();
  1330. if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
  1331. {
  1332. SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
  1333. resEntry->meta = fileMeta;
  1334. }
  1335. }
  1336. }
  1337. if (resEntry->meta != nullptr)
  1338. {
  1339. auto& resourceMetas = resEntry->meta->getResourceMetaData();
  1340. if (resourceMetas.size() > 0)
  1341. {
  1342. mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
  1343. for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
  1344. {
  1345. SPtr<ProjectResourceMeta> entry = resourceMetas[i];
  1346. mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
  1347. }
  1348. }
  1349. }
  1350. addDependencies(resEntry);
  1351. }
  1352. else
  1353. deletedEntries.push_back(resEntry);
  1354. }
  1355. else if(child->type == LibraryEntryType::Directory)
  1356. {
  1357. if (FileSystem::isDirectory(child->path))
  1358. todo.push(static_cast<DirectoryEntry*>(child));
  1359. else
  1360. deletedEntries.push_back(child);
  1361. }
  1362. }
  1363. }
  1364. // Remove entries that no longer have corresponding files
  1365. for (auto& deletedEntry : deletedEntries)
  1366. {
  1367. if (deletedEntry->type == LibraryEntryType::File)
  1368. {
  1369. FileEntry* resEntry = static_cast<FileEntry*>(deletedEntry);
  1370. deleteResourceInternal(resEntry);
  1371. }
  1372. else
  1373. {
  1374. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
  1375. deleteDirectoryInternal(dirEntry);
  1376. }
  1377. }
  1378. // Clean up internal library folder from obsolete files
  1379. Path internalResourcesFolder = mProjectFolder;
  1380. internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
  1381. if (FileSystem::exists(internalResourcesFolder))
  1382. {
  1383. Vector<Path> toDelete;
  1384. auto processFile = [&](const Path& file)
  1385. {
  1386. UUID uuid = UUID(file.getFilename(false));
  1387. if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
  1388. {
  1389. mResourceManifest->unregisterResource(uuid);
  1390. toDelete.push_back(file);
  1391. }
  1392. return true;
  1393. };
  1394. FileSystem::iterate(internalResourcesFolder, processFile);
  1395. for (auto& entry : toDelete)
  1396. FileSystem::remove(entry);
  1397. }
  1398. mIsLoaded = true;
  1399. }
  1400. void ProjectLibrary::clearEntries()
  1401. {
  1402. if (mRootEntry == nullptr)
  1403. return;
  1404. std::function<void(LibraryEntry*)> deleteRecursive =
  1405. [&](LibraryEntry* entry)
  1406. {
  1407. if (entry->type == LibraryEntryType::Directory)
  1408. {
  1409. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
  1410. for (auto& child : dirEntry->mChildren)
  1411. deleteRecursive(child);
  1412. }
  1413. bs_delete(entry);
  1414. };
  1415. assert(mQueuedImports.empty());
  1416. deleteRecursive(mRootEntry);
  1417. mRootEntry = nullptr;
  1418. }
  1419. Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
  1420. {
  1421. Vector<Path> output;
  1422. if (entry->meta == nullptr)
  1423. return output;
  1424. auto& resourceMetas = entry->meta->getResourceMetaData();
  1425. for(auto& resMeta : resourceMetas)
  1426. {
  1427. if (resMeta->getTypeID() == TID_Shader)
  1428. {
  1429. SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
  1430. for (auto& include : metaData->includes)
  1431. output.push_back(include);
  1432. }
  1433. }
  1434. return output;
  1435. }
  1436. void ProjectLibrary::addDependencies(const FileEntry* entry)
  1437. {
  1438. Vector<Path> dependencies = getImportDependencies(entry);
  1439. for (auto& dependency : dependencies)
  1440. mDependencies[dependency].push_back(entry->path);
  1441. }
  1442. void ProjectLibrary::removeDependencies(const FileEntry* entry)
  1443. {
  1444. Vector<Path> dependencies = getImportDependencies(entry);
  1445. for (auto& dependency : dependencies)
  1446. {
  1447. Vector<Path>& curDependencies = mDependencies[dependency];
  1448. auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
  1449. [&](const Path& x)
  1450. {
  1451. return x == entry->path;
  1452. });
  1453. curDependencies.erase(iterRemove, curDependencies.end());
  1454. }
  1455. }
  1456. void ProjectLibrary::reimportDependants(const Path& entryPath)
  1457. {
  1458. auto iterFind = mDependencies.find(entryPath);
  1459. if (iterFind == mDependencies.end())
  1460. return;
  1461. // Make a copy since we might modify this list during reimport
  1462. Vector<Path> dependencies = iterFind->second;
  1463. for (auto& dependency : dependencies)
  1464. {
  1465. LibraryEntry* entry = findEntry(dependency);
  1466. if (entry != nullptr && entry->type == LibraryEntryType::File)
  1467. {
  1468. FileEntry* resEntry = static_cast<FileEntry*>(entry);
  1469. SPtr<ImportOptions> importOptions;
  1470. if (resEntry->meta != nullptr)
  1471. importOptions = resEntry->meta->getImportOptions();
  1472. reimportResourceInternal(resEntry, importOptions, true);
  1473. }
  1474. }
  1475. }
  1476. BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
  1477. {
  1478. return ProjectLibrary::instance();
  1479. }
  1480. }