BsProjectLibrary.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. const ImportOptionsPtr& importOptions, bool forceReimport)
  221. {
  222. ResourceEntry* newResource = bs_new<ResourceEntry>(filePath, filePath.getWTail(), parent);
  223. parent->mChildren.push_back(newResource);
  224. reimportResourceInternal(newResource, importOptions, forceReimport);
  225. if(!onEntryAdded.empty())
  226. onEntryAdded(filePath);
  227. return newResource;
  228. }
  229. ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
  230. {
  231. DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getWTail(), parent);
  232. parent->mChildren.push_back(newEntry);
  233. if(!onEntryAdded.empty())
  234. onEntryAdded(dirPath);
  235. return newEntry;
  236. }
  237. void ProjectLibrary::deleteResourceInternal(ResourceEntry* resource)
  238. {
  239. if(resource->meta != nullptr)
  240. {
  241. Path path;
  242. if(mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  243. {
  244. if(FileSystem::isFile(path))
  245. FileSystem::remove(path);
  246. mResourceManifest->unregisterResource(resource->meta->getUUID());
  247. }
  248. }
  249. DirectoryEntry* parent = resource->parent;
  250. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  251. [&] (const LibraryEntry* entry) { return entry == resource; });
  252. parent->mChildren.erase(findIter);
  253. if(!onEntryRemoved.empty())
  254. onEntryRemoved(resource->path);
  255. bs_delete(resource);
  256. }
  257. void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
  258. {
  259. if(directory == mRootEntry)
  260. mRootEntry = nullptr;
  261. Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
  262. for(auto& child : childrenToDestroy)
  263. {
  264. if(child->type == LibraryEntryType::Directory)
  265. deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
  266. else
  267. deleteResourceInternal(static_cast<ResourceEntry*>(child));
  268. }
  269. DirectoryEntry* parent = directory->parent;
  270. if(parent != nullptr)
  271. {
  272. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  273. [&] (const LibraryEntry* entry) { return entry == directory; });
  274. parent->mChildren.erase(findIter);
  275. }
  276. if(!onEntryRemoved.empty())
  277. onEntryRemoved(directory->path);
  278. bs_delete(directory);
  279. }
  280. void ProjectLibrary::reimportResourceInternal(ResourceEntry* resource, const ImportOptionsPtr& importOptions, bool forceReimport)
  281. {
  282. WString ext = resource->path.getWExtension();
  283. Path metaPath = resource->path;
  284. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  285. ext = ext.substr(1, ext.size() - 1); // Remove the .
  286. if(!Importer::instance().supportsFileType(ext))
  287. return;
  288. if(resource->meta == nullptr)
  289. {
  290. if(FileSystem::isFile(metaPath))
  291. {
  292. FileDecoder fs(metaPath);
  293. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  294. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  295. {
  296. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  297. resource->meta = resourceMeta;
  298. }
  299. }
  300. }
  301. if (!isUpToDate(resource) || forceReimport)
  302. {
  303. ImportOptionsPtr curImportOptions = nullptr;
  304. if (importOptions == nullptr)
  305. {
  306. if (resource->meta != nullptr)
  307. curImportOptions = resource->meta->getImportOptions();
  308. else
  309. curImportOptions = Importer::instance().createImportOptions(resource->path);
  310. }
  311. else
  312. curImportOptions = importOptions;
  313. HResource importedResource;
  314. if(resource->meta == nullptr)
  315. {
  316. importedResource = Importer::instance().import(resource->path, curImportOptions);
  317. ResourceMetaDataPtr subMeta = importedResource->getMetaData();
  318. UINT32 typeId = importedResource->getTypeId();
  319. resource->meta = ProjectResourceMeta::create(importedResource.getUUID(), typeId, subMeta, curImportOptions);
  320. FileEncoder fs(metaPath);
  321. fs.encode(resource->meta.get());
  322. }
  323. else
  324. {
  325. importedResource = HResource(resource->meta->getUUID());
  326. Importer::instance().reimport(importedResource, resource->path, curImportOptions);
  327. }
  328. Path internalResourcesPath = mProjectFolder;
  329. internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
  330. if(!FileSystem::isDirectory(internalResourcesPath))
  331. FileSystem::createDir(internalResourcesPath);
  332. internalResourcesPath.setFilename(toWString(importedResource.getUUID()) + L".asset");
  333. gResources().save(importedResource, internalResourcesPath, true);
  334. gResources().unload(importedResource);
  335. mResourceManifest->registerResource(importedResource.getUUID(), internalResourcesPath);
  336. resource->lastUpdateTime = std::time(nullptr);
  337. }
  338. }
  339. bool ProjectLibrary::isUpToDate(ResourceEntry* resource) const
  340. {
  341. if(resource->meta == nullptr)
  342. return false;
  343. Path path;
  344. if(!mResourceManifest->uuidToFilePath(resource->meta->getUUID(), path))
  345. return false;
  346. if(!FileSystem::isFile(path))
  347. return false;
  348. std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(path);
  349. return lastModifiedTime <= resource->lastUpdateTime;
  350. }
  351. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& fullPath) const
  352. {
  353. if (!mRootEntry->path.includes(fullPath))
  354. return nullptr;
  355. Path relPath = fullPath.getRelative(mRootEntry->path);
  356. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  357. UINT32 idx = 0;
  358. LibraryEntry* current = mRootEntry;
  359. while (current != nullptr)
  360. {
  361. if (idx == numElems)
  362. return current;
  363. WString curElem;
  364. if (relPath.isFile() && idx == (numElems - 1))
  365. curElem = relPath.getWFilename();
  366. else
  367. curElem = relPath[idx];
  368. if (current->type == LibraryEntryType::Directory)
  369. {
  370. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  371. for (auto& child : dirEntry->mChildren)
  372. {
  373. current = nullptr;
  374. if (Path::comparePathElem(curElem, child->elementName))
  375. {
  376. idx++;
  377. current = child;
  378. break;
  379. }
  380. }
  381. }
  382. else
  383. break;
  384. }
  385. return nullptr;
  386. }
  387. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  388. {
  389. if (mResourceManifest == nullptr)
  390. return nullptr;
  391. Path filePath;
  392. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  393. return nullptr;
  394. LibraryEntry* libEntry = findEntry(filePath);
  395. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  396. return nullptr;
  397. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  398. return resEntry->meta;
  399. }
  400. Path ProjectLibrary::uuidToPath(const String& uuid) const
  401. {
  402. if (mResourceManifest == nullptr)
  403. return Path();
  404. Path filePath;
  405. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  406. return Path();
  407. return filePath;
  408. }
  409. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  410. {
  411. if (resource == nullptr)
  412. return;
  413. Path assetPath = mResourcesFolder;
  414. assetPath.append(path);
  415. assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
  416. LibraryEntry* existingEntry = findEntry(assetPath);
  417. if (existingEntry != nullptr)
  418. BS_EXCEPT(InvalidParametersException, "Resource already exists at the specified path: " + assetPath.toString());
  419. Resources::instance().save(resource, assetPath, false);
  420. checkForModifications(assetPath);
  421. }
  422. void ProjectLibrary::saveEntry(const HResource& resource)
  423. {
  424. if (resource == nullptr)
  425. return;
  426. Path filePath;
  427. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  428. return;
  429. Resources::instance().save(resource, filePath, false);
  430. }
  431. void ProjectLibrary::createFolderEntry(const Path& path)
  432. {
  433. if (FileSystem::isDirectory(path))
  434. return; // Already exists
  435. FileSystem::createDir(path);
  436. if (!mResourcesFolder.includes(path))
  437. return;
  438. Path parentPath = path.getParent();
  439. DirectoryEntry* newEntryParent = nullptr;
  440. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  441. if (newEntryParentLib != nullptr)
  442. {
  443. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  444. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  445. }
  446. DirectoryEntry* newHierarchyParent = nullptr;
  447. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  448. createInternalParentHierarchy(path, &newHierarchyParent, &newEntryParent);
  449. addDirectoryInternal(newEntryParent, path);
  450. }
  451. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  452. {
  453. if(FileSystem::isFile(oldPath) || FileSystem::isDirectory(oldPath))
  454. FileSystem::move(oldPath, newPath, overwrite);
  455. Path oldMetaPath = getMetaPath(oldPath);
  456. Path newMetaPath = getMetaPath(newPath);
  457. LibraryEntry* oldEntry = findEntry(oldPath);
  458. if(oldEntry != nullptr) // Moving from the Resources folder
  459. {
  460. // Moved outside of Resources, delete entry & meta file
  461. if (!mResourcesFolder.includes(newPath))
  462. {
  463. if(oldEntry->type == LibraryEntryType::File)
  464. {
  465. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  466. if(FileSystem::isFile(oldMetaPath))
  467. FileSystem::remove(oldMetaPath);
  468. }
  469. else if(oldEntry->type == LibraryEntryType::Directory)
  470. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  471. }
  472. else // Just moving internally
  473. {
  474. if(FileSystem::isFile(oldMetaPath))
  475. FileSystem::move(oldMetaPath, newMetaPath);
  476. DirectoryEntry* parent = oldEntry->parent;
  477. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  478. if(findIter != parent->mChildren.end())
  479. parent->mChildren.erase(findIter);
  480. Path parentPath = newPath.getParent();
  481. DirectoryEntry* newEntryParent = nullptr;
  482. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  483. if(newEntryParentLib != nullptr)
  484. {
  485. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  486. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  487. }
  488. DirectoryEntry* newHierarchyParent = nullptr;
  489. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  490. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  491. newEntryParent->mChildren.push_back(oldEntry);
  492. oldEntry->parent = newEntryParent;
  493. oldEntry->path = newPath;
  494. oldEntry->elementName = newPath.getWTail();
  495. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  496. {
  497. Stack<LibraryEntry*> todo;
  498. todo.push(oldEntry);
  499. while(!todo.empty())
  500. {
  501. LibraryEntry* curEntry = todo.top();
  502. todo.pop();
  503. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  504. for(auto& child : curDirEntry->mChildren)
  505. {
  506. child->path = child->parent->path;
  507. child->path.append(child->elementName);
  508. if(child->type == LibraryEntryType::Directory)
  509. todo.push(child);
  510. }
  511. }
  512. }
  513. if(!onEntryRemoved.empty())
  514. onEntryRemoved(oldPath);
  515. if(!onEntryAdded.empty())
  516. onEntryAdded(newPath);
  517. if(newHierarchyParent != nullptr)
  518. checkForModifications(newHierarchyParent->path);
  519. }
  520. }
  521. else // Moving from outside of the Resources folder (likely adding a new resource)
  522. {
  523. checkForModifications(newPath);
  524. }
  525. }
  526. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  527. {
  528. if (!FileSystem::exists(oldPath))
  529. return;
  530. FileSystem::copy(oldPath, newPath, overwrite);
  531. // Copying a file/folder outside of the Resources folder, no special handling needed
  532. if (!mResourcesFolder.includes(newPath))
  533. return;
  534. Path parentPath = newPath.getParent();
  535. DirectoryEntry* newEntryParent = nullptr;
  536. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  537. if (newEntryParentLib != nullptr)
  538. {
  539. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  540. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  541. }
  542. DirectoryEntry* newHierarchyParent = nullptr;
  543. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  544. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  545. // If the source is outside of Resources folder, just plain import the copy
  546. LibraryEntry* oldEntry = findEntry(oldPath);
  547. if (oldEntry == nullptr)
  548. {
  549. checkForModifications(newHierarchyParent->path);
  550. return;
  551. }
  552. // Both source and destination are within Resources folder, need to preserve import options on the copies
  553. LibraryEntry* newEntry = nullptr;
  554. if (FileSystem::isFile(newPath))
  555. {
  556. assert(oldEntry->type == LibraryEntryType::File);
  557. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  558. newEntry = addResourceInternal(newEntryParent, newPath, oldResEntry->meta->getImportOptions(), true);
  559. }
  560. else
  561. {
  562. assert(oldEntry->type == LibraryEntryType::File);
  563. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  564. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newPath);
  565. newEntry = newDirEntry;
  566. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  567. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  568. while (!todo.empty())
  569. {
  570. auto current = todo.top();
  571. todo.pop();
  572. DirectoryEntry* sourceDir = std::get<0>(current);
  573. DirectoryEntry* destDir = std::get<1>(current);
  574. for (auto& child : sourceDir->mChildren)
  575. {
  576. Path childDestPath = destDir->path;
  577. childDestPath.append(child->path.getWTail());
  578. if (child->type == LibraryEntryType::File)
  579. {
  580. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  581. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  582. }
  583. else // Directory
  584. {
  585. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  586. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  587. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  588. }
  589. }
  590. }
  591. }
  592. }
  593. void ProjectLibrary::deleteEntry(const Path& path)
  594. {
  595. if(FileSystem::exists(path))
  596. FileSystem::remove(path);
  597. LibraryEntry* entry = findEntry(path);
  598. if(entry != nullptr)
  599. {
  600. if(entry->type == LibraryEntryType::File)
  601. {
  602. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  603. Path metaPath = getMetaPath(path);
  604. if(FileSystem::isFile(metaPath))
  605. FileSystem::remove(metaPath);
  606. }
  607. else if(entry->type == LibraryEntryType::Directory)
  608. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  609. }
  610. }
  611. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  612. {
  613. LibraryEntry* entry = findEntry(path);
  614. if (entry != nullptr)
  615. {
  616. if (entry->type == LibraryEntryType::File)
  617. {
  618. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  619. reimportResourceInternal(resEntry, importOptions, forceReimport);
  620. }
  621. }
  622. }
  623. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  624. {
  625. Path parentPath = fullPath;
  626. DirectoryEntry* newEntryParent = nullptr;
  627. Stack<Path> parentPaths;
  628. do
  629. {
  630. Path newParentPath = parentPath.getParent();
  631. if(newParentPath == parentPath)
  632. break;
  633. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  634. if(newEntryParentLib != nullptr)
  635. {
  636. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  637. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  638. break;
  639. }
  640. parentPaths.push(newParentPath);
  641. parentPath = newParentPath;
  642. } while (true);
  643. assert(newEntryParent != nullptr); // Must exist
  644. if(newHierarchyRoot != nullptr)
  645. *newHierarchyRoot = newEntryParent;
  646. while(!parentPaths.empty())
  647. {
  648. Path curPath = parentPaths.top();
  649. parentPaths.pop();
  650. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  651. }
  652. if(newHierarchyLeaf != nullptr)
  653. *newHierarchyLeaf = newEntryParent;
  654. }
  655. Path ProjectLibrary::getMetaPath(const Path& path) const
  656. {
  657. Path metaPath = path;
  658. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  659. return metaPath;
  660. }
  661. bool ProjectLibrary::isMeta(const Path& fullPath) const
  662. {
  663. return fullPath.getWExtension() == L".meta";
  664. }
  665. void ProjectLibrary::onMonitorFileModified(const Path& path)
  666. {
  667. if(!isMeta(path))
  668. checkForModifications(path);
  669. else
  670. {
  671. Path resourcePath = path;
  672. resourcePath.setExtension(L"");
  673. checkForModifications(resourcePath);
  674. }
  675. }
  676. void ProjectLibrary::save()
  677. {
  678. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  679. Path libraryEntriesPath = mProjectFolder;
  680. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  681. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  682. FileEncoder fs(libraryEntriesPath);
  683. fs.encode(libEntries.get());
  684. Path resourceManifestPath = mProjectFolder;
  685. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  686. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  687. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  688. }
  689. void ProjectLibrary::load()
  690. {
  691. if(mRootEntry != nullptr)
  692. {
  693. deleteDirectoryInternal(mRootEntry);
  694. mRootEntry = nullptr;
  695. }
  696. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  697. Path libraryEntriesPath = mProjectFolder;
  698. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  699. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  700. if(FileSystem::exists(libraryEntriesPath))
  701. {
  702. FileDecoder fs(libraryEntriesPath);
  703. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  704. *mRootEntry = libEntries->getRootEntry();
  705. for(auto& child : mRootEntry->mChildren)
  706. child->parent = mRootEntry;
  707. mRootEntry->parent = nullptr;
  708. }
  709. // Load all meta files
  710. Stack<DirectoryEntry*> todo;
  711. todo.push(mRootEntry);
  712. while(!todo.empty())
  713. {
  714. DirectoryEntry* curDir = todo.top();
  715. todo.pop();
  716. for(auto& child : curDir->mChildren)
  717. {
  718. if(child->type == LibraryEntryType::File)
  719. {
  720. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  721. if(resEntry->meta == nullptr)
  722. {
  723. Path metaPath = resEntry->path;
  724. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  725. if(FileSystem::isFile(metaPath))
  726. {
  727. FileDecoder fs(metaPath);
  728. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  729. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  730. {
  731. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  732. resEntry->meta = resourceMeta;
  733. }
  734. }
  735. }
  736. }
  737. else if(child->type == LibraryEntryType::Directory)
  738. {
  739. todo.push(static_cast<DirectoryEntry*>(child));
  740. }
  741. }
  742. }
  743. // Load resource manifest
  744. Path resourceManifestPath = mProjectFolder;
  745. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  746. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  747. if(FileSystem::exists(resourceManifestPath))
  748. {
  749. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  750. }
  751. }
  752. }