BsProjectLibrary.cpp 21 KB

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