2
0

BsProjectLibrary.cpp 22 KB

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