BsProjectLibrary.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 CamelotFramework;
  17. using namespace BansheeEngine;
  18. using namespace std::placeholders;
  19. namespace BansheeEditor
  20. {
  21. const WString ProjectLibrary::RESOURCES_DIR = L"Resources";
  22. const WString 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 CM::WString& path, const CM::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 CM::WString& path, const CM::WString& name, DirectoryEntry* parent)
  35. :LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
  36. { }
  37. ProjectLibrary::DirectoryEntry::DirectoryEntry()
  38. { }
  39. ProjectLibrary::DirectoryEntry::DirectoryEntry(const CM::WString& path, const CM::WString& name, DirectoryEntry* parent)
  40. :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
  41. { }
  42. ProjectLibrary::ProjectLibrary(const WString& projectFolder)
  43. :mRootEntry(nullptr), mProjectFolder(projectFolder)
  44. {
  45. mResourcesFolder = Path::combine(mProjectFolder, RESOURCES_DIR);
  46. mMonitor = cm_new<FolderMonitor>();
  47. FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  48. (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  49. mMonitor->startMonitor(mResourcesFolder, true, folderChanges);
  50. mMonitor->onAdded.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  51. mMonitor->onRemoved.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  52. mMonitor->onModified.connect(std::bind(&ProjectLibrary::onMonitorFileModified, this, _1));
  53. load();
  54. if(mResourceManifest == nullptr)
  55. mResourceManifest = ResourceManifest::create("ProjectLibrary");
  56. gResources().registerResourceManifest(mResourceManifest);
  57. checkForModifications(mResourcesFolder);
  58. }
  59. ProjectLibrary::~ProjectLibrary()
  60. {
  61. save();
  62. mMonitor->stopMonitorAll();
  63. cm_delete(mMonitor);
  64. if(mRootEntry != nullptr)
  65. deleteDirectoryInternal(mRootEntry);
  66. }
  67. void ProjectLibrary::update()
  68. {
  69. mMonitor->update();
  70. }
  71. void ProjectLibrary::checkForModifications(const CM::WString& fullPath)
  72. {
  73. if(!Path::includes(fullPath, mResourcesFolder))
  74. return; // Folder not part of our resources path, so no modifications
  75. if(mRootEntry == nullptr)
  76. {
  77. WString resPath = mResourcesFolder;
  78. mRootEntry = cm_new<DirectoryEntry>(resPath, Path::getFilename(resPath), nullptr);
  79. }
  80. WString pathToSearch = fullPath;
  81. LibraryEntry* entry = findEntry(pathToSearch);
  82. if(entry == nullptr) // File could be new, try to find parent directory entry
  83. {
  84. WString parentDirPath = Path::parentPath(pathToSearch);
  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*>::type todo;
  126. todo.push(static_cast<DirectoryEntry*>(entry));
  127. Vector<WString>::type childFiles;
  128. Vector<WString>::type childDirectories;
  129. Vector<bool>::type existingEntries;
  130. Vector<LibraryEntry*>::type 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. WString sourceFilePath = filePath;
  147. Path::replaceExtension(sourceFilePath, 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 && Path::equals(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 && Path::equals(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 CM::WString& filePath)
  220. {
  221. ResourceEntry* newResource = cm_new<ResourceEntry>(filePath, Path::getFilename(filePath), 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 CM::WString& dirPath)
  229. {
  230. DirectoryEntry* newEntry = cm_new<DirectoryEntry>(dirPath, Path::getFilename(dirPath), 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. WString 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. cm_delete(resource);
  255. }
  256. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  257. {
  258. if(directory == mRootEntry)
  259. mRootEntry = nullptr;
  260. CM::Vector<LibraryEntry*>::type 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. cm_delete(directory);
  278. }
  279. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource)
  280. {
  281. WString ext = Path::getExtension(resource->path);
  282. WString metaPath = resource->path + L".meta";
  283. ext = ext.substr(1, ext.size() - 1); // Remove the .
  284. if(!Importer::instance().supportsFileType(ext))
  285. return;
  286. if(resource->meta == nullptr)
  287. {
  288. FileSerializer fs;
  289. if(FileSystem::isFile(metaPath))
  290. {
  291. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  292. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ResourceMeta::getRTTIStatic()))
  293. {
  294. ResourceMetaPtr resourceMeta = std::static_pointer_cast<ResourceMeta>(loadedMeta);
  295. resource->meta = resourceMeta;
  296. }
  297. }
  298. }
  299. if(!isUpToDate(resource))
  300. {
  301. ImportOptionsPtr importOptions = nullptr;
  302. if(resource->meta != nullptr)
  303. importOptions = resource->meta->getImportOptions();
  304. else
  305. importOptions = Importer::instance().createImportOptions(resource->path);
  306. HResource importedResource;
  307. if(resource->meta == nullptr)
  308. {
  309. importedResource = Importer::instance().import(resource->path, importOptions);
  310. resource->meta = ResourceMeta::create(importedResource.getUUID(), importOptions);
  311. FileSerializer fs;
  312. fs.encode(resource->meta.get(), metaPath);
  313. }
  314. else
  315. {
  316. importedResource = HResource(resource->meta->getUUID());
  317. Importer::instance().reimport(importedResource, resource->path, importOptions);
  318. }
  319. WString internalResourcesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  320. if(!FileSystem::isDirectory(internalResourcesPath))
  321. FileSystem::createDir(internalResourcesPath);
  322. internalResourcesPath = Path::combine(internalResourcesPath, 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. WString 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 CM::WString& fullPath) const
  342. {
  343. Vector<WString>::type pathElems = Path::split(fullPath);
  344. Vector<WString>::type rootElems = Path::split(mRootEntry->path);
  345. auto pathIter = pathElems.begin();
  346. auto rootIter = rootElems.begin();
  347. while(pathIter != pathElems.end() && rootIter != rootElems.end() && Path::comparePathElements(*pathIter, *rootIter))
  348. {
  349. ++pathIter;
  350. ++rootIter;
  351. }
  352. if(pathIter == pathElems.begin()) // Not a single entry matches Resources path
  353. return nullptr;
  354. --pathIter;
  355. Stack<LibraryEntry*>::type todo;
  356. todo.push(mRootEntry);
  357. while(!todo.empty())
  358. {
  359. LibraryEntry* current = todo.top();
  360. todo.pop();
  361. if(Path::comparePathElements(*pathIter, current->elementName))
  362. {
  363. ++pathIter;
  364. if(pathIter == pathElems.end())
  365. return current;
  366. while(!todo.empty())
  367. todo.pop();
  368. if(current->type == LibraryEntryType::Directory)
  369. {
  370. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  371. for(auto& child : dirEntry->mChildren)
  372. todo.push(child);
  373. }
  374. }
  375. }
  376. return nullptr;
  377. }
  378. void ProjectLibrary::moveEntry(const CM::WString& oldPath, const CM::WString& newPath)
  379. {
  380. if(FileSystem::isFile(oldPath) || FileSystem::isDirectory(oldPath))
  381. FileSystem::move(oldPath, newPath);
  382. WString oldMetaPath = getMetaPath(oldPath);
  383. WString newMetaPath = getMetaPath(newPath);
  384. LibraryEntry* oldEntry = findEntry(oldPath);
  385. if(oldEntry != nullptr) // Moving from the Resources folder
  386. {
  387. // Moved outside of Resources, delete entry & meta file
  388. if(!Path::includes(newPath, mResourcesFolder))
  389. {
  390. if(oldEntry->type == LibraryEntryType::File)
  391. {
  392. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  393. if(FileSystem::isFile(oldMetaPath))
  394. FileSystem::remove(oldMetaPath);
  395. }
  396. else if(oldEntry->type == LibraryEntryType::Directory)
  397. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  398. }
  399. else // Just moving internally
  400. {
  401. if(FileSystem::isFile(oldMetaPath))
  402. FileSystem::move(oldMetaPath, newMetaPath);
  403. DirectoryEntry* parent = oldEntry->parent;
  404. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  405. if(findIter != parent->mChildren.end())
  406. parent->mChildren.erase(findIter);
  407. WString parentPath = Path::parentPath(newPath);
  408. DirectoryEntry* newEntryParent = nullptr;
  409. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  410. if(newEntryParentLib != nullptr)
  411. {
  412. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  413. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  414. }
  415. DirectoryEntry* newHierarchyParent = nullptr;
  416. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  417. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  418. newEntryParent->mChildren.push_back(oldEntry);
  419. oldEntry->parent = newEntryParent;
  420. oldEntry->path = newPath;
  421. oldEntry->elementName = Path::getFilename(newPath);
  422. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  423. {
  424. Stack<LibraryEntry*>::type todo;
  425. todo.push(oldEntry);
  426. while(!todo.empty())
  427. {
  428. LibraryEntry* curEntry = todo.top();
  429. todo.pop();
  430. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  431. for(auto& child : curDirEntry->mChildren)
  432. {
  433. child->path = Path::combine(child->parent->path, child->elementName);
  434. if(child->type == LibraryEntryType::Directory)
  435. todo.push(child);
  436. }
  437. }
  438. }
  439. if(!onEntryRemoved.empty())
  440. onEntryRemoved(oldPath);
  441. if(!onEntryAdded.empty())
  442. onEntryAdded(newPath);
  443. if(newHierarchyParent != nullptr)
  444. checkForModifications(newHierarchyParent->path);
  445. }
  446. }
  447. else // Moving from outside of the Resources folder (likely adding a new resource)
  448. {
  449. checkForModifications(newPath);
  450. }
  451. }
  452. void ProjectLibrary::deleteEntry(const CM::WString& path)
  453. {
  454. if(FileSystem::exists(path))
  455. FileSystem::remove(path);
  456. LibraryEntry* entry = findEntry(path);
  457. if(entry != nullptr)
  458. {
  459. if(entry->type == LibraryEntryType::File)
  460. {
  461. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  462. WString metaPath = getMetaPath(path);
  463. if(FileSystem::isFile(metaPath))
  464. FileSystem::remove(metaPath);
  465. }
  466. else if(entry->type == LibraryEntryType::Directory)
  467. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  468. }
  469. }
  470. void ProjectLibrary::createInternalParentHierarchy(const CM::WString& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  471. {
  472. WString parentPath = fullPath;
  473. DirectoryEntry* newEntryParent = nullptr;
  474. Stack<WString>::type parentPaths;
  475. do
  476. {
  477. WString newParentPath = Path::parentPath(parentPath);
  478. if(newParentPath == parentPath)
  479. break;
  480. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  481. if(newEntryParentLib != nullptr)
  482. {
  483. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  484. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  485. break;
  486. }
  487. parentPaths.push(newParentPath);
  488. parentPath = newParentPath;
  489. } while (true);
  490. assert(newEntryParent != nullptr); // Must exist
  491. if(newHierarchyRoot != nullptr)
  492. *newHierarchyRoot = newEntryParent;
  493. while(!parentPaths.empty())
  494. {
  495. WString curPath = parentPaths.top();
  496. parentPaths.pop();
  497. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  498. }
  499. if(newHierarchyLeaf != nullptr)
  500. *newHierarchyLeaf = newEntryParent;
  501. }
  502. WString ProjectLibrary::getMetaPath(const CM::WString& path) const
  503. {
  504. WString metaPath = path + L".meta";
  505. return metaPath;
  506. }
  507. bool ProjectLibrary::isMeta(const WString& fullPath) const
  508. {
  509. return Path::getExtension(fullPath) == L".meta";
  510. }
  511. void ProjectLibrary::onMonitorFileModified(const WString& path)
  512. {
  513. if(!isMeta(path))
  514. checkForModifications(path);
  515. else
  516. {
  517. WString resourcePath = path;
  518. Path::replaceExtension(resourcePath, L"");
  519. checkForModifications(resourcePath);
  520. }
  521. }
  522. void ProjectLibrary::save()
  523. {
  524. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  525. WString libraryEntriesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  526. libraryEntriesPath = Path::combine(libraryEntriesPath, LIBRARY_ENTRIES_FILENAME);
  527. FileSerializer fs;
  528. fs.encode(libEntries.get(), libraryEntriesPath);
  529. WString resourceManifestPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  530. resourceManifestPath = Path::combine(resourceManifestPath, RESOURCE_MANIFEST_FILENAME);
  531. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  532. }
  533. void ProjectLibrary::load()
  534. {
  535. if(mRootEntry != nullptr)
  536. {
  537. deleteDirectoryInternal(mRootEntry);
  538. mRootEntry = nullptr;
  539. }
  540. WString resPath = mResourcesFolder;
  541. mRootEntry = cm_new<DirectoryEntry>(resPath, Path::getFilename(resPath), nullptr);
  542. WString libraryEntriesPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  543. libraryEntriesPath = Path::combine(libraryEntriesPath, LIBRARY_ENTRIES_FILENAME);
  544. if(FileSystem::exists(libraryEntriesPath))
  545. {
  546. FileSerializer fs;
  547. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode(libraryEntriesPath));
  548. *mRootEntry = libEntries->getRootEntry();
  549. for(auto& child : mRootEntry->mChildren)
  550. child->parent = mRootEntry;
  551. mRootEntry->parent = nullptr;
  552. }
  553. // Load all meta files
  554. Stack<DirectoryEntry*>::type todo;
  555. todo.push(mRootEntry);
  556. while(!todo.empty())
  557. {
  558. DirectoryEntry* curDir = todo.top();
  559. todo.pop();
  560. for(auto& child : curDir->mChildren)
  561. {
  562. if(child->type == LibraryEntryType::File)
  563. {
  564. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  565. if(resEntry->meta == nullptr)
  566. {
  567. WString metaPath = resEntry->path + L".meta";
  568. FileSerializer fs;
  569. if(FileSystem::isFile(metaPath))
  570. {
  571. std::shared_ptr<IReflectable> loadedMeta = fs.decode(metaPath);
  572. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ResourceMeta::getRTTIStatic()))
  573. {
  574. ResourceMetaPtr resourceMeta = std::static_pointer_cast<ResourceMeta>(loadedMeta);
  575. resEntry->meta = resourceMeta;
  576. }
  577. }
  578. }
  579. }
  580. else if(child->type == LibraryEntryType::Directory)
  581. {
  582. todo.push(static_cast<DirectoryEntry*>(child));
  583. }
  584. }
  585. }
  586. // Load resource manifest
  587. WString resourceManifestPath = Path::combine(mProjectFolder, INTERNAL_RESOURCES_DIR);
  588. resourceManifestPath = Path::combine(resourceManifestPath, RESOURCE_MANIFEST_FILENAME);
  589. if(FileSystem::exists(resourceManifestPath))
  590. {
  591. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  592. }
  593. }
  594. }