BsProjectLibrary.cpp 21 KB

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