BsProjectLibrary.cpp 44 KB

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