BsProjectLibrary.cpp 44 KB

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