BsProjectLibrary.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #include "BsProjectLibrary.h"
  2. #include "BsPath.h"
  3. #include "BsFileSystem.h"
  4. #include "BsException.h"
  5. #include "BsResources.h"
  6. #include "BsResourceManifest.h"
  7. #include "BsImporter.h"
  8. #include "BsProjectResourceMeta.h"
  9. #include "BsResources.h"
  10. #include "BsImporter.h"
  11. #include "BsImportOptions.h"
  12. #include "BsFileSerializer.h"
  13. #include "BsFolderMonitor.h"
  14. #include "BsDebug.h"
  15. #include "BsProjectLibraryEntries.h"
  16. #include "BsResource.h"
  17. #include "BsResourceImporter.h"
  18. using namespace std::placeholders;
  19. namespace BansheeEngine
  20. {
  21. const Path ProjectLibrary::RESOURCES_DIR = L"Resources\\";
  22. const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = L"Internal\\Resources\\";
  23. const WString ProjectLibrary::LIBRARY_ENTRIES_FILENAME = L"ProjectLibrary.asset";
  24. const WString ProjectLibrary::RESOURCE_MANIFEST_FILENAME = L"ResourceManifest.asset";
  25. ProjectLibrary::LibraryEntry::LibraryEntry()
  26. :parent(nullptr), type(LibraryEntryType::Directory)
  27. { }
  28. ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type)
  29. :path(path), parent(parent), type(type), elementName(name)
  30. { }
  31. ProjectLibrary::ResourceEntry::ResourceEntry()
  32. :lastUpdateTime(0)
  33. { }
  34. ProjectLibrary::ResourceEntry::ResourceEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  35. :LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  36. { }
  37. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  38. { }
  39. ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent)
  40. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  41. { }
  42. ProjectLibrary::ProjectLibrary(const Path& projectFolder)
  43. :mRootEntry(nullptr), mProjectFolder(projectFolder)
  44. {
  45. mResourcesFolder = mProjectFolder;
  46. mResourcesFolder.append(RESOURCES_DIR);
  47. mMonitor = bs_new<FolderMonitor>();
  48. FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  49. (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  50. mMonitor->startMonitor(mResourcesFolder, true, folderChanges);
  51. mMonitor->onAdded.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  52. mMonitor->onRemoved.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  53. mMonitor->onModified.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  54. load();
  55. if(mResourceManifest == nullptr)
  56. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  57. gResources().registerResourceManifest(mResourceManifest);
  58. checkForModifications(mResourcesFolder);
  59. }
  60. ProjectLibrary::~ProjectLibrary()
  61. {
  62. save();
  63. mMonitor->stopMonitorAll();
  64. bs_delete(mMonitor);
  65. if(mRootEntry != nullptr)
  66. deleteDirectoryInternal(mRootEntry);
  67. }
  68. void ProjectLibrary::update()
  69. {
  70. mMonitor->_update();
  71. }
  72. void ProjectLibrary::checkForModifications(const Path& fullPath)
  73. {
  74. if (!mResourcesFolder.includes(fullPath))
  75. return; // Folder not part of our resources path, so no modifications
  76. if(mRootEntry == nullptr)
  77. {
  78. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  79. }
  80. Path pathToSearch = fullPath;
  81. LibraryEntry* entry = findEntry(pathToSearch);
  82. if(entry == nullptr) // File could be new, try to find parent directory entry
  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. addResourceInternal(entryParent, pathToSearch);
  96. }
  97. else if(FileSystem::isDirectory(pathToSearch))
  98. {
  99. addDirectoryInternal(entryParent, pathToSearch);
  100. if(newHierarchyParent == nullptr)
  101. checkForModifications(pathToSearch);
  102. }
  103. if(newHierarchyParent != nullptr)
  104. checkForModifications(newHierarchyParent->path);
  105. }
  106. else if(entry->type == LibraryEntryType::File)
  107. {
  108. if(FileSystem::isFile(entry->path))
  109. {
  110. reimportResourceInternal(static_cast<ResourceEntry*>(entry));
  111. }
  112. else
  113. {
  114. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  115. }
  116. }
  117. else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
  118. {
  119. if(!FileSystem::isDirectory(entry->path))
  120. {
  121. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  122. }
  123. else
  124. {
  125. Stack<DirectoryEntry*> todo;
  126. todo.push(static_cast<DirectoryEntry*>(entry));
  127. Vector<Path> childFiles;
  128. Vector<Path> childDirectories;
  129. Vector<bool> existingEntries;
  130. Vector<LibraryEntry*> toDelete;
  131. while(!todo.empty())
  132. {
  133. DirectoryEntry* currentDir = todo.top();
  134. todo.pop();
  135. existingEntries.clear();
  136. existingEntries.resize(currentDir->mChildren.size());
  137. for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
  138. existingEntries[i] = false;
  139. childFiles.clear();
  140. childDirectories.clear();
  141. FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
  142. for(auto& filePath : childFiles)
  143. {
  144. if(isMeta(filePath))
  145. {
  146. Path sourceFilePath = filePath;
  147. sourceFilePath.setExtension(L"");
  148. if(!FileSystem::isFile(sourceFilePath))
  149. {
  150. LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
  151. FileSystem::remove(filePath);
  152. }
  153. }
  154. else
  155. {
  156. ResourceEntry* existingEntry = nullptr;
  157. UINT32 idx = 0;
  158. for(auto& child : currentDir->mChildren)
  159. {
  160. if(child->type == LibraryEntryType::File && child->path == filePath)
  161. {
  162. existingEntries[idx] = true;
  163. existingEntry = static_cast<ResourceEntry*>(child);
  164. break;
  165. }
  166. idx++;
  167. }
  168. if(existingEntry != nullptr)
  169. {
  170. reimportResourceInternal(existingEntry);
  171. }
  172. else
  173. {
  174. addResourceInternal(currentDir, filePath);
  175. }
  176. }
  177. }
  178. for(auto& dirPath : childDirectories)
  179. {
  180. DirectoryEntry* existingEntry = nullptr;
  181. UINT32 idx = 0;
  182. for(auto& child : currentDir->mChildren)
  183. {
  184. if(child->type == LibraryEntryType::Directory && child->path == dirPath)
  185. {
  186. existingEntries[idx] = true;
  187. existingEntry = static_cast<DirectoryEntry*>(child);
  188. break;
  189. }
  190. idx++;
  191. }
  192. if(existingEntry == nullptr)
  193. addDirectoryInternal(currentDir, dirPath);
  194. }
  195. {
  196. for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
  197. {
  198. if(existingEntries[i])
  199. continue;
  200. toDelete.push_back(currentDir->mChildren[i]);
  201. }
  202. for(auto& child : toDelete)
  203. {
  204. if(child->type == LibraryEntryType::Directory)
  205. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  206. else if(child->type == LibraryEntryType::File)
  207. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  208. }
  209. }
  210. for(auto& child : currentDir->mChildren)
  211. {
  212. if(child->type == LibraryEntryType::Directory)
  213. todo.push(static_cast<DirectoryEntry*>(child));
  214. }
  215. }
  216. }
  217. }
  218. }
  219. ProjectLibrary::ResourceEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath)
  220. {
  221. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  222. parent->mChildren.push_back(newResource);
  223. reimportResourceInternal(newResource);
  224. if(!onEntryAdded.empty())
  225. onEntryAdded(filePath);
  226. return newResource;
  227. }
  228. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  229. {
  230. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  231. parent->mChildren.push_back(newEntry);
  232. if(!onEntryAdded.empty())
  233. onEntryAdded(dirPath);
  234. return newEntry;
  235. }
  236. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  237. {
  238. if(resource->meta != nullptr)
  239. {
  240. Path path;
  241. if(mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  242. {
  243. if(FileSystem::isFile(path))
  244. FileSystem::remove(path);
  245. mResourceManifest->unregisterResource(resource->meta->getUUID());
  246. }
  247. }
  248. DirectoryEntry* parent = resource->parent;
  249. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  250. [&] (const LibraryEntry* entry) { return entry == resource; });
  251. parent->mChildren.erase(findIter);
  252. if(!onEntryRemoved.empty())
  253. onEntryRemoved(resource->path);
  254. bs_delete(resource);
  255. }
  256. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  257. {
  258. if(directory == mRootEntry)
  259. mRootEntry = nullptr;
  260. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  261. for(auto& child : childrenToDestroy)
  262. {
  263. if(child->type == LibraryEntryType::Directory)
  264. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  265. else
  266. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  267. }
  268. DirectoryEntry* parent = directory->parent;
  269. if(parent != nullptr)
  270. {
  271. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  272. [&] (const LibraryEntry* entry) { return entry == directory; });
  273. parent->mChildren.erase(findIter);
  274. }
  275. if(!onEntryRemoved.empty())
  276. onEntryRemoved(directory->path);
  277. bs_delete(directory);
  278. }
  279. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource)
  280. {
  281. WString ext = resource->path.getWExtension();
  282. Path metaPath = resource->path;
  283. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  284. ext = ext.substr(1, ext.size() - 1); // Remove the .
  285. if(!Importer::instance().supportsFileType(ext))
  286. return;
  287. if(resource->meta == nullptr)
  288. {
  289. FileSerializer fs;
  290. if(FileSystem::isFile(metaPath))
  291. {
  292. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  293. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  294. {
  295. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  296. resource->meta = resourceMeta;
  297. }
  298. }
  299. }
  300. if(!isUpToDate(resource))
  301. {
  302. ImportOptionsPtr importOptions = nullptr;
  303. if(resource->meta != nullptr)
  304. importOptions = resource->meta->getImportOptions();
  305. else
  306. importOptions = Importer::instance().createImportOptions(resource->path);
  307. HResource importedResource;
  308. if(resource->meta == nullptr)
  309. {
  310. importedResource = Importer::instance().import(resource->path, importOptions);
  311. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  312. UINT32 typeId = importedResource->getTypeId();
  313. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, importOptions);
  314. FileSerializer fs;
  315. fs.encode(resource->meta.get(), metaPath);
  316. }
  317. else
  318. {
  319. importedResource = HResource(resource->meta->getUUID());
  320. Importer::instance().reimport(importedResource, resource->path, importOptions);
  321. }
  322. Path internalResourcesPath = mProjectFolder;
  323. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  324. if(!FileSystem::isDirectory(internalResourcesPath))
  325. FileSystem::createDir(internalResourcesPath);
  326. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  327. gResources().save(importedResource, internalResourcesPath, true);
  328. gResources().unload(importedResource);
  329. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  330. resource->lastUpdateTime = std::time(nullptr);
  331. }
  332. }
  333. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  334. {
  335. if(resource->meta == nullptr)
  336. return false;
  337. Path path;
  338. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  339. return false;
  340. if(!FileSystem::isFile(path))
  341. return false;
  342. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  343. return lastModifiedTime <= resource->lastUpdateTime;
  344. }
  345. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& fullPath) const
  346. {
  347. if (!mRootEntry->path.includes(fullPath))
  348. return nullptr;
  349. Path relPath = fullPath.getRelative(mRootEntry->path);
  350. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  351. UINT32 idx = 0;
  352. LibraryEntry* current = mRootEntry;
  353. while (current != nullptr)
  354. {
  355. if (idx == numElems)
  356. return current;
  357. WString curElem;
  358. if (relPath.isFile() && idx == (numElems - 1))
  359. curElem = relPath.getWFilename();
  360. else
  361. curElem = relPath[idx];
  362. current = nullptr;
  363. if (current->type == LibraryEntryType::Directory)
  364. {
  365. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  366. for (auto& child : dirEntry->mChildren)
  367. {
  368. if (Path::comparePathElem(curElem, child->elementName))
  369. {
  370. idx++;
  371. current = child;
  372. break;
  373. }
  374. }
  375. }
  376. }
  377. return nullptr;
  378. }
  379. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  380. {
  381. if (mResourceManifest == nullptr)
  382. return nullptr;
  383. Path filePath;
  384. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  385. return nullptr;
  386. LibraryEntry* libEntry = findEntry(filePath);
  387. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  388. return nullptr;
  389. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  390. return resEntry->meta;
  391. }
  392. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  393. {
  394. if (resource == nullptr)
  395. return;
  396. if (!mResourcesFolder.includes(path))
  397. BS_EXCEPT(InvalidParametersException, "Provided path is not within the project library directory: " + path.toString());
  398. Path assetPath = path;
  399. assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
  400. LibraryEntry* existingEntry = findEntry(assetPath);
  401. if (existingEntry != nullptr)
  402. BS_EXCEPT(InvalidParametersException, "Existing resource already exists at the specified path: " + assetPath.toString());
  403. Resources::instance().save(resource, assetPath, false);
  404. checkForModifications(assetPath);
  405. }
  406. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath)
  407. {
  408. if(FileSystem::isFile(oldPath) || FileSystem::isDirectory(oldPath))
  409. FileSystem::move(oldPath, newPath);
  410. Path oldMetaPath = getMetaPath(oldPath);
  411. Path newMetaPath = getMetaPath(newPath);
  412. LibraryEntry* oldEntry = findEntry(oldPath);
  413. if(oldEntry != nullptr) // Moving from the Resources folder
  414. {
  415. // Moved outside of Resources, delete entry & meta file
  416. if (!mResourcesFolder.includes(newPath))
  417. {
  418. if(oldEntry->type == LibraryEntryType::File)
  419. {
  420. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  421. if(FileSystem::isFile(oldMetaPath))
  422. FileSystem::remove(oldMetaPath);
  423. }
  424. else if(oldEntry->type == LibraryEntryType::Directory)
  425. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  426. }
  427. else // Just moving internally
  428. {
  429. if(FileSystem::isFile(oldMetaPath))
  430. FileSystem::move(oldMetaPath, newMetaPath);
  431. DirectoryEntry* parent = oldEntry->parent;
  432. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  433. if(findIter != parent->mChildren.end())
  434. parent->mChildren.erase(findIter);
  435. Path parentPath = newPath.getParent();
  436. DirectoryEntry* newEntryParent = nullptr;
  437. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  438. if(newEntryParentLib != nullptr)
  439. {
  440. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  441. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  442. }
  443. DirectoryEntry* newHierarchyParent = nullptr;
  444. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  445. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  446. newEntryParent->mChildren.push_back(oldEntry);
  447. oldEntry->parent = newEntryParent;
  448. oldEntry->path = newPath;
  449. oldEntry->elementName = newPath.getWTail();
  450. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  451. {
  452. Stack<LibraryEntry*> todo;
  453. todo.push(oldEntry);
  454. while(!todo.empty())
  455. {
  456. LibraryEntry* curEntry = todo.top();
  457. todo.pop();
  458. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  459. for(auto& child : curDirEntry->mChildren)
  460. {
  461. child->path = child->parent->path;
  462. child->path.append(child->elementName);
  463. if(child->type == LibraryEntryType::Directory)
  464. todo.push(child);
  465. }
  466. }
  467. }
  468. if(!onEntryRemoved.empty())
  469. onEntryRemoved(oldPath);
  470. if(!onEntryAdded.empty())
  471. onEntryAdded(newPath);
  472. if(newHierarchyParent != nullptr)
  473. checkForModifications(newHierarchyParent->path);
  474. }
  475. }
  476. else // Moving from outside of the Resources folder (likely adding a new resource)
  477. {
  478. checkForModifications(newPath);
  479. }
  480. }
  481. void ProjectLibrary::deleteEntry(const Path& path)
  482. {
  483. if(FileSystem::exists(path))
  484. FileSystem::remove(path);
  485. LibraryEntry* entry = findEntry(path);
  486. if(entry != nullptr)
  487. {
  488. if(entry->type == LibraryEntryType::File)
  489. {
  490. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  491. Path metaPath = getMetaPath(path);
  492. if(FileSystem::isFile(metaPath))
  493. FileSystem::remove(metaPath);
  494. }
  495. else if(entry->type == LibraryEntryType::Directory)
  496. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  497. }
  498. }
  499. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  500. {
  501. Path parentPath = fullPath;
  502. DirectoryEntry* newEntryParent = nullptr;
  503. Stack<Path> parentPaths;
  504. do
  505. {
  506. Path newParentPath = parentPath.getParent();
  507. if(newParentPath == parentPath)
  508. break;
  509. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  510. if(newEntryParentLib != nullptr)
  511. {
  512. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  513. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  514. break;
  515. }
  516. parentPaths.push(newParentPath);
  517. parentPath = newParentPath;
  518. } while (true);
  519. assert(newEntryParent != nullptr); // Must exist
  520. if(newHierarchyRoot != nullptr)
  521. *newHierarchyRoot = newEntryParent;
  522. while(!parentPaths.empty())
  523. {
  524. Path curPath = parentPaths.top();
  525. parentPaths.pop();
  526. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  527. }
  528. if(newHierarchyLeaf != nullptr)
  529. *newHierarchyLeaf = newEntryParent;
  530. }
  531. Path ProjectLibrary::getMetaPath(const Path& path) const
  532. {
  533. Path metaPath = path;
  534. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  535. return metaPath;
  536. }
  537. bool ProjectLibrary::isMeta(const Path& fullPath) const
  538. {
  539. return fullPath.getWExtension() == L".meta";
  540. }
  541. void ProjectLibrary::onMonitorFileModified(const Path& path)
  542. {
  543. if(!isMeta(path))
  544. checkForModifications(path);
  545. else
  546. {
  547. Path resourcePath = path;
  548. resourcePath.setExtension(L"");
  549. checkForModifications(resourcePath);
  550. }
  551. }
  552. void ProjectLibrary::save()
  553. {
  554. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  555. Path libraryEntriesPath = mProjectFolder;
  556. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  557. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  558. FileSerializer fs;
  559. fs.encode(libEntries.get(), libraryEntriesPath);
  560. Path resourceManifestPath = mProjectFolder;
  561. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  562. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  563. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  564. }
  565. void ProjectLibrary::load()
  566. {
  567. if(mRootEntry != nullptr)
  568. {
  569. deleteDirectoryInternal(mRootEntry);
  570. mRootEntry = nullptr;
  571. }
  572. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  573. Path libraryEntriesPath = mProjectFolder;
  574. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  575. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  576. if(FileSystem::exists(libraryEntriesPath))
  577. {
  578. FileSerializer fs;
  579. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode(libraryEntriesPath));
  580. *mRootEntry = libEntries->getRootEntry();
  581. for(auto& child : mRootEntry->mChildren)
  582. child->parent = mRootEntry;
  583. mRootEntry->parent = nullptr;
  584. }
  585. // Load all meta files
  586. Stack<DirectoryEntry*> todo;
  587. todo.push(mRootEntry);
  588. while(!todo.empty())
  589. {
  590. DirectoryEntry* curDir = todo.top();
  591. todo.pop();
  592. for(auto& child : curDir->mChildren)
  593. {
  594. if(child->type == LibraryEntryType::File)
  595. {
  596. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  597. if(resEntry->meta == nullptr)
  598. {
  599. Path metaPath = resEntry->path;
  600. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  601. FileSerializer fs;
  602. if(FileSystem::isFile(metaPath))
  603. {
  604. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  605. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  606. {
  607. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  608. resEntry->meta = resourceMeta;
  609. }
  610. }
  611. }
  612. }
  613. else if(child->type == LibraryEntryType::Directory)
  614. {
  615. todo.push(static_cast<DirectoryEntry*>(child));
  616. }
  617. }
  618. }
  619. // Load resource manifest
  620. Path resourceManifestPath = mProjectFolder;
  621. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  622. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  623. if(FileSystem::exists(resourceManifestPath))
  624. {
  625. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  626. }
  627. }
  628. }