BsProjectLibrary.cpp 48 KB

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