BsProjectLibrary.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 WString ProjectLibrary::RESOURCES_DIR = L"Resources";
  20. const WString 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 WString& 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 WString& 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 WString& path, const WString& name, DirectoryEntry* parent)
  38. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  39. { }
  40. ProjectLibrary::ProjectLibrary(const WString& projectFolder)
  41. :mRootEntry(nullptr), mProjectFolder(projectFolder)
  42. {
  43. mResourcesFolder = Path::combine(mProjectFolder, RESOURCES_DIR);
  44. mMonitor = cm_new<FolderMonitor>();
  45. FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  46. (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  47. mMonitor->startMonitor(mResourcesFolder, true, folderChanges);
  48. mMonitor->onAdded.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  49. mMonitor->onRemoved.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  50. mMonitor->onModified.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  51. load();
  52. if(mResourceManifest == nullptr)
  53. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  54. gResources().registerResourceManifest(mResourceManifest);
  55. checkForModifications(mResourcesFolder);
  56. }
  57. ProjectLibrary::~ProjectLibrary()
  58. {
  59. save();
  60. mMonitor->stopMonitorAll();
  61. cm_delete(mMonitor);
  62. if(mRootEntry != nullptr)
  63. deleteDirectoryInternal(mRootEntry);
  64. }
  65. void ProjectLibrary::update()
  66. {
  67. mMonitor->update();
  68. }
  69. void ProjectLibrary::checkForModifications(const WString& fullPath)
  70. {
  71. if(!Path::includes(fullPath, mResourcesFolder))
  72. return; // Folder not part of our resources path, so no modifications
  73. if(mRootEntry == nullptr)
  74. {
  75. WString resPath = mResourcesFolder;
  76. mRootEntry = cm_new<DirectoryEntry>(resPath, Path::getFilename(resPath), nullptr);
  77. }
  78. WString pathToSearch = fullPath;
  79. LibraryEntry* entry = findEntry(pathToSearch);
  80. if(entry == nullptr) // File could be new, try to find parent directory entry
  81. {
  82. WString parentDirPath = Path::parentPath(pathToSearch);
  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<WString>::type childFiles;
  126. Vector<WString>::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. WString sourceFilePath = filePath;
  145. Path::replaceExtension(sourceFilePath, 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 && Path::equals(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 && Path::equals(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 WString& filePath)
  218. {
  219. ResourceEntry* newResource = cm_new<ResourceEntry>(filePath, Path::getFilename(filePath), 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 WString& dirPath)
  227. {
  228. DirectoryEntry* newEntry = cm_new<DirectoryEntry>(dirPath, Path::getFilename(dirPath), 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. WString 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 = Path::getExtension(resource->path);
  280. WString metaPath = resource->path + L".meta";
  281. ext = ext.substr(1, ext.size() - 1); // Remove the .
  282. if(!Importer::instance().supportsFileType(ext))
  283. return;
  284. if(resource->meta == nullptr)
  285. {
  286. FileSerializer fs;
  287. if(FileSystem::isFile(metaPath))
  288. {
  289. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  290. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ResourceMeta::getRTTIStatic()))
  291. {
  292. ResourceMetaPtr resourceMeta = std::static_pointer_cast<ResourceMeta>(loadedMeta);
  293. resource->meta = resourceMeta;
  294. }
  295. }
  296. }
  297. if(!isUpToDate(resource))
  298. {
  299. ImportOptionsPtr importOptions = nullptr;
  300. if(resource->meta != nullptr)
  301. importOptions = resource->meta->getImportOptions();
  302. else
  303. importOptions = Importer::instance().createImportOptions(resource->path);
  304. HResource importedResource;
  305. if(resource->meta == nullptr)
  306. {
  307. importedResource = Importer::instance().import(resource->path, importOptions);
  308. resource->meta = ResourceMeta::create(importedResource.getUUID(), importOptions);
  309. FileSerializer fs;
  310. fs.encode(resource->meta.get(), metaPath);
  311. }
  312. else
  313. {
  314. importedResource = HResource(resource->meta->getUUID());
  315. Importer::instance().reimport(importedResource, resource->path, importOptions);
  316. }
  317. WString internalResourcesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  318. if(!FileSystem::isDirectory(internalResourcesPath))
  319. FileSystem::createDir(internalResourcesPath);
  320. internalResourcesPath = Path::combine(internalResourcesPath, toWString(importedResource.getUUID()) + L".asset");
  321. gResources().save(importedResource, internalResourcesPath, true);
  322. gResources().unload(importedResource);
  323. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  324. resource->lastUpdateTime = std::time(nullptr);
  325. }
  326. }
  327. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  328. {
  329. if(resource->meta == nullptr)
  330. return false;
  331. WString path;
  332. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  333. return false;
  334. if(!FileSystem::isFile(path))
  335. return false;
  336. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  337. return lastModifiedTime <= resource->lastUpdateTime;
  338. }
  339. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const WString& fullPath) const
  340. {
  341. Vector<WString>::type pathElems = Path::split(fullPath);
  342. Vector<WString>::type rootElems = Path::split(mRootEntry->path);
  343. auto pathIter = pathElems.begin();
  344. auto rootIter = rootElems.begin();
  345. while(pathIter != pathElems.end() && rootIter != rootElems.end() && Path::comparePathElements(*pathIter, *rootIter))
  346. {
  347. ++pathIter;
  348. ++rootIter;
  349. }
  350. if(pathIter == pathElems.begin()) // Not a single entry matches Resources path
  351. return nullptr;
  352. --pathIter;
  353. Stack<LibraryEntry*>::type todo;
  354. todo.push(mRootEntry);
  355. while(!todo.empty())
  356. {
  357. LibraryEntry* current = todo.top();
  358. todo.pop();
  359. if(Path::comparePathElements(*pathIter, current->elementName))
  360. {
  361. ++pathIter;
  362. if(pathIter == pathElems.end())
  363. return current;
  364. while(!todo.empty())
  365. todo.pop();
  366. if(current->type == LibraryEntryType::Directory)
  367. {
  368. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  369. for(auto& child : dirEntry->mChildren)
  370. todo.push(child);
  371. }
  372. }
  373. }
  374. return nullptr;
  375. }
  376. void ProjectLibrary::moveEntry(const WString& oldPath, const WString& newPath)
  377. {
  378. if(FileSystem::isFile(oldPath) || FileSystem::isDirectory(oldPath))
  379. FileSystem::move(oldPath, newPath);
  380. WString oldMetaPath = getMetaPath(oldPath);
  381. WString newMetaPath = getMetaPath(newPath);
  382. LibraryEntry* oldEntry = findEntry(oldPath);
  383. if(oldEntry != nullptr) // Moving from the Resources folder
  384. {
  385. // Moved outside of Resources, delete entry & meta file
  386. if(!Path::includes(newPath, mResourcesFolder))
  387. {
  388. if(oldEntry->type == LibraryEntryType::File)
  389. {
  390. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  391. if(FileSystem::isFile(oldMetaPath))
  392. FileSystem::remove(oldMetaPath);
  393. }
  394. else if(oldEntry->type == LibraryEntryType::Directory)
  395. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  396. }
  397. else // Just moving internally
  398. {
  399. if(FileSystem::isFile(oldMetaPath))
  400. FileSystem::move(oldMetaPath, newMetaPath);
  401. DirectoryEntry* parent = oldEntry->parent;
  402. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  403. if(findIter != parent->mChildren.end())
  404. parent->mChildren.erase(findIter);
  405. WString parentPath = Path::parentPath(newPath);
  406. DirectoryEntry* newEntryParent = nullptr;
  407. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  408. if(newEntryParentLib != nullptr)
  409. {
  410. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  411. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  412. }
  413. DirectoryEntry* newHierarchyParent = nullptr;
  414. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  415. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  416. newEntryParent->mChildren.push_back(oldEntry);
  417. oldEntry->parent = newEntryParent;
  418. oldEntry->path = newPath;
  419. oldEntry->elementName = Path::getFilename(newPath);
  420. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  421. {
  422. Stack<LibraryEntry*>::type todo;
  423. todo.push(oldEntry);
  424. while(!todo.empty())
  425. {
  426. LibraryEntry* curEntry = todo.top();
  427. todo.pop();
  428. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  429. for(auto& child : curDirEntry->mChildren)
  430. {
  431. child->path = Path::combine(child->parent->path, 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 WString& 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. WString 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 WString& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  469. {
  470. WString parentPath = fullPath;
  471. DirectoryEntry* newEntryParent = nullptr;
  472. Stack<WString>::type parentPaths;
  473. do
  474. {
  475. WString newParentPath = Path::parentPath(parentPath);
  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. WString curPath = parentPaths.top();
  494. parentPaths.pop();
  495. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  496. }
  497. if(newHierarchyLeaf != nullptr)
  498. *newHierarchyLeaf = newEntryParent;
  499. }
  500. WString ProjectLibrary::getMetaPath(const WString& path) const
  501. {
  502. WString metaPath = path + L".meta";
  503. return metaPath;
  504. }
  505. bool ProjectLibrary::isMeta(const WString& fullPath) const
  506. {
  507. return Path::getExtension(fullPath) == L".meta";
  508. }
  509. void ProjectLibrary::onMonitorFileModified(const WString& path)
  510. {
  511. if(!isMeta(path))
  512. checkForModifications(path);
  513. else
  514. {
  515. WString resourcePath = path;
  516. Path::replaceExtension(resourcePath, L"");
  517. checkForModifications(resourcePath);
  518. }
  519. }
  520. void ProjectLibrary::save()
  521. {
  522. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  523. WString libraryEntriesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  524. libraryEntriesPath = Path::combine(libraryEntriesPath, LIBRARY_ENTRIES_FILENAME);
  525. FileSerializer fs;
  526. fs.encode(libEntries.get(), libraryEntriesPath);
  527. WString resourceManifestPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  528. resourceManifestPath = Path::combine(resourceManifestPath, RESOURCE_MANIFEST_FILENAME);
  529. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  530. }
  531. void ProjectLibrary::load()
  532. {
  533. if(mRootEntry != nullptr)
  534. {
  535. deleteDirectoryInternal(mRootEntry);
  536. mRootEntry = nullptr;
  537. }
  538. WString resPath = mResourcesFolder;
  539. mRootEntry = cm_new<DirectoryEntry>(resPath, Path::getFilename(resPath), nullptr);
  540. WString libraryEntriesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  541. libraryEntriesPath = Path::combine(libraryEntriesPath, LIBRARY_ENTRIES_FILENAME);
  542. if(FileSystem::exists(libraryEntriesPath))
  543. {
  544. FileSerializer fs;
  545. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode(libraryEntriesPath));
  546. *mRootEntry = libEntries->getRootEntry();
  547. for(auto& child : mRootEntry->mChildren)
  548. child->parent = mRootEntry;
  549. mRootEntry->parent = nullptr;
  550. }
  551. // Load all meta files
  552. Stack<DirectoryEntry*>::type todo;
  553. todo.push(mRootEntry);
  554. while(!todo.empty())
  555. {
  556. DirectoryEntry* curDir = todo.top();
  557. todo.pop();
  558. for(auto& child : curDir->mChildren)
  559. {
  560. if(child->type == LibraryEntryType::File)
  561. {
  562. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  563. if(resEntry->meta == nullptr)
  564. {
  565. WString metaPath = resEntry->path + L".meta";
  566. FileSerializer fs;
  567. if(FileSystem::isFile(metaPath))
  568. {
  569. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  570. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ResourceMeta::getRTTIStatic()))
  571. {
  572. ResourceMetaPtr resourceMeta = std::static_pointer_cast<ResourceMeta>(loadedMeta);
  573. resEntry->meta = resourceMeta;
  574. }
  575. }
  576. }
  577. }
  578. else if(child->type == LibraryEntryType::Directory)
  579. {
  580. todo.push(static_cast<DirectoryEntry*>(child));
  581. }
  582. }
  583. }
  584. // Load resource manifest
  585. WString resourceManifestPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  586. resourceManifestPath = Path::combine(resourceManifestPath, RESOURCE_MANIFEST_FILENAME);
  587. if(FileSystem::exists(resourceManifestPath))
  588. {
  589. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  590. }
  591. }
  592. }