BsProjectLibrary.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "BsProjectLibrary.h"
  2. #include "BsFileSystem.h"
  3. #include "BsException.h"
  4. #include "BsResources.h"
  5. #include "BsResourceManifest.h"
  6. #include "BsImporter.h"
  7. #include "BsProjectResourceMeta.h"
  8. #include "BsResources.h"
  9. #include "BsImporter.h"
  10. #include "BsImportOptions.h"
  11. #include "BsFileSerializer.h"
  12. #include "BsFolderMonitor.h"
  13. #include "BsDebug.h"
  14. #include "BsProjectLibraryEntries.h"
  15. #include "BsResource.h"
  16. #include "BsResourceImporter.h"
  17. #include <regex>
  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. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern)
  352. {
  353. return search(pattern, {});
  354. }
  355. Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const WString& pattern, const Vector<UINT32>& typeIds)
  356. {
  357. Vector<LibraryEntry*> foundEntries;
  358. std::wregex escape(L"[.^$|()\\[\\]{}*+?\\\\]");
  359. WString replace(L"\\\\&");
  360. WString escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
  361. std::wregex wildcard(L"\\\\\\*");
  362. WString wildcardReplace(L".*");
  363. WString searchPattern = std::regex_replace(escapedPattern, wildcard, L".*");
  364. std::wregex searchRegex(searchPattern);
  365. Stack<DirectoryEntry*> todo;
  366. todo.push(mRootEntry);
  367. while (!todo.empty())
  368. {
  369. DirectoryEntry* dirEntry = todo.top();
  370. todo.pop();
  371. for (auto& child : dirEntry->mChildren)
  372. {
  373. if (std::regex_match(child->elementName, searchRegex))
  374. {
  375. if (typeIds.size() == 0)
  376. foundEntries.push_back(child);
  377. else
  378. {
  379. if (child->type == LibraryEntryType::File)
  380. {
  381. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  382. for (auto& typeId : typeIds)
  383. {
  384. if (childResEntry->meta->getTypeID() == typeId)
  385. {
  386. foundEntries.push_back(child);
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. if (child->type == LibraryEntryType::Directory)
  394. {
  395. DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child);
  396. todo.push(childDirEntry);
  397. }
  398. }
  399. }
  400. std::sort(foundEntries.begin(), foundEntries.end(),
  401. [&](const LibraryEntry* a, const LibraryEntry* b)
  402. {
  403. return a->elementName.compare(b->elementName);
  404. });
  405. return foundEntries;
  406. }
  407. ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& fullPath) const
  408. {
  409. if (!mRootEntry->path.includes(fullPath))
  410. return nullptr;
  411. Path relPath = fullPath.getRelative(mRootEntry->path);
  412. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  413. UINT32 idx = 0;
  414. LibraryEntry* current = mRootEntry;
  415. while (current != nullptr)
  416. {
  417. if (idx == numElems)
  418. return current;
  419. WString curElem;
  420. if (relPath.isFile() && idx == (numElems - 1))
  421. curElem = relPath.getWFilename();
  422. else
  423. curElem = relPath[idx];
  424. if (current->type == LibraryEntryType::Directory)
  425. {
  426. DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
  427. for (auto& child : dirEntry->mChildren)
  428. {
  429. current = nullptr;
  430. if (Path::comparePathElem(curElem, child->elementName))
  431. {
  432. idx++;
  433. current = child;
  434. break;
  435. }
  436. }
  437. }
  438. else
  439. break;
  440. }
  441. return nullptr;
  442. }
  443. ProjectResourceMetaPtr ProjectLibrary::findResourceMeta(const String& uuid) const
  444. {
  445. if (mResourceManifest == nullptr)
  446. return nullptr;
  447. Path filePath;
  448. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  449. return nullptr;
  450. LibraryEntry* libEntry = findEntry(filePath);
  451. if (libEntry == nullptr || libEntry->type != LibraryEntryType::File)
  452. return nullptr;
  453. ResourceEntry* resEntry = static_cast<ResourceEntry*>(libEntry);
  454. return resEntry->meta;
  455. }
  456. Path ProjectLibrary::uuidToPath(const String& uuid) const
  457. {
  458. if (mResourceManifest == nullptr)
  459. return Path();
  460. Path filePath;
  461. if (!mResourceManifest->uuidToFilePath(uuid, filePath))
  462. return Path();
  463. return filePath;
  464. }
  465. void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
  466. {
  467. if (resource == nullptr)
  468. return;
  469. Path assetPath = mResourcesFolder;
  470. assetPath.append(path);
  471. assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
  472. LibraryEntry* existingEntry = findEntry(assetPath);
  473. if (existingEntry != nullptr)
  474. BS_EXCEPT(InvalidParametersException, "Resource already exists at the specified path: " + assetPath.toString());
  475. Resources::instance().save(resource, assetPath, false);
  476. checkForModifications(assetPath);
  477. }
  478. void ProjectLibrary::saveEntry(const HResource& resource)
  479. {
  480. if (resource == nullptr)
  481. return;
  482. Path filePath;
  483. if (!mResourceManifest->uuidToFilePath(resource.getUUID(), filePath))
  484. return;
  485. Resources::instance().save(resource, filePath, false);
  486. }
  487. void ProjectLibrary::createFolderEntry(const Path& path)
  488. {
  489. if (FileSystem::isDirectory(path))
  490. return; // Already exists
  491. FileSystem::createDir(path);
  492. if (!mResourcesFolder.includes(path))
  493. return;
  494. Path parentPath = path.getParent();
  495. DirectoryEntry* newEntryParent = nullptr;
  496. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  497. if (newEntryParentLib != nullptr)
  498. {
  499. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  500. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  501. }
  502. DirectoryEntry* newHierarchyParent = nullptr;
  503. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  504. createInternalParentHierarchy(path, &newHierarchyParent, &newEntryParent);
  505. addDirectoryInternal(newEntryParent, path);
  506. }
  507. void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  508. {
  509. if(FileSystem::isFile(oldPath) || FileSystem::isDirectory(oldPath))
  510. FileSystem::move(oldPath, newPath, overwrite);
  511. Path oldMetaPath = getMetaPath(oldPath);
  512. Path newMetaPath = getMetaPath(newPath);
  513. LibraryEntry* oldEntry = findEntry(oldPath);
  514. if(oldEntry != nullptr) // Moving from the Resources folder
  515. {
  516. // Moved outside of Resources, delete entry & meta file
  517. if (!mResourcesFolder.includes(newPath))
  518. {
  519. if(oldEntry->type == LibraryEntryType::File)
  520. {
  521. deleteResourceInternal(static_cast<ResourceEntry*>(oldEntry));
  522. if(FileSystem::isFile(oldMetaPath))
  523. FileSystem::remove(oldMetaPath);
  524. }
  525. else if(oldEntry->type == LibraryEntryType::Directory)
  526. deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
  527. }
  528. else // Just moving internally
  529. {
  530. if(FileSystem::isFile(oldMetaPath))
  531. FileSystem::move(oldMetaPath, newMetaPath);
  532. DirectoryEntry* parent = oldEntry->parent;
  533. auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
  534. if(findIter != parent->mChildren.end())
  535. parent->mChildren.erase(findIter);
  536. Path parentPath = newPath.getParent();
  537. DirectoryEntry* newEntryParent = nullptr;
  538. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  539. if(newEntryParentLib != nullptr)
  540. {
  541. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  542. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  543. }
  544. DirectoryEntry* newHierarchyParent = nullptr;
  545. if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  546. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  547. newEntryParent->mChildren.push_back(oldEntry);
  548. oldEntry->parent = newEntryParent;
  549. oldEntry->path = newPath;
  550. oldEntry->elementName = newPath.getWTail();
  551. if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
  552. {
  553. Stack<LibraryEntry*> todo;
  554. todo.push(oldEntry);
  555. while(!todo.empty())
  556. {
  557. LibraryEntry* curEntry = todo.top();
  558. todo.pop();
  559. DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
  560. for(auto& child : curDirEntry->mChildren)
  561. {
  562. child->path = child->parent->path;
  563. child->path.append(child->elementName);
  564. if(child->type == LibraryEntryType::Directory)
  565. todo.push(child);
  566. }
  567. }
  568. }
  569. if(!onEntryRemoved.empty())
  570. onEntryRemoved(oldPath);
  571. if(!onEntryAdded.empty())
  572. onEntryAdded(newPath);
  573. if(newHierarchyParent != nullptr)
  574. checkForModifications(newHierarchyParent->path);
  575. }
  576. }
  577. else // Moving from outside of the Resources folder (likely adding a new resource)
  578. {
  579. checkForModifications(newPath);
  580. }
  581. }
  582. void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
  583. {
  584. if (!FileSystem::exists(oldPath))
  585. return;
  586. FileSystem::copy(oldPath, newPath, overwrite);
  587. // Copying a file/folder outside of the Resources folder, no special handling needed
  588. if (!mResourcesFolder.includes(newPath))
  589. return;
  590. Path parentPath = newPath.getParent();
  591. DirectoryEntry* newEntryParent = nullptr;
  592. LibraryEntry* newEntryParentLib = findEntry(parentPath);
  593. if (newEntryParentLib != nullptr)
  594. {
  595. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  596. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  597. }
  598. DirectoryEntry* newHierarchyParent = nullptr;
  599. if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
  600. createInternalParentHierarchy(newPath, &newHierarchyParent, &newEntryParent);
  601. // If the source is outside of Resources folder, just plain import the copy
  602. LibraryEntry* oldEntry = findEntry(oldPath);
  603. if (oldEntry == nullptr)
  604. {
  605. checkForModifications(newHierarchyParent->path);
  606. return;
  607. }
  608. // Both source and destination are within Resources folder, need to preserve import options on the copies
  609. LibraryEntry* newEntry = nullptr;
  610. if (FileSystem::isFile(newPath))
  611. {
  612. assert(oldEntry->type == LibraryEntryType::File);
  613. ResourceEntry* oldResEntry = static_cast<ResourceEntry*>(oldEntry);
  614. newEntry = addResourceInternal(newEntryParent, newPath, oldResEntry->meta->getImportOptions(), true);
  615. }
  616. else
  617. {
  618. assert(oldEntry->type == LibraryEntryType::File);
  619. DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
  620. DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newPath);
  621. newEntry = newDirEntry;
  622. Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
  623. todo.push(std::make_tuple(oldDirEntry, newDirEntry));
  624. while (!todo.empty())
  625. {
  626. auto current = todo.top();
  627. todo.pop();
  628. DirectoryEntry* sourceDir = std::get<0>(current);
  629. DirectoryEntry* destDir = std::get<1>(current);
  630. for (auto& child : sourceDir->mChildren)
  631. {
  632. Path childDestPath = destDir->path;
  633. childDestPath.append(child->path.getWTail());
  634. if (child->type == LibraryEntryType::File)
  635. {
  636. ResourceEntry* childResEntry = static_cast<ResourceEntry*>(child);
  637. addResourceInternal(destDir, childDestPath, childResEntry->meta->getImportOptions(), true);
  638. }
  639. else // Directory
  640. {
  641. DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
  642. DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
  643. todo.push(std::make_tuple(childSourceDirEntry, childSourceDirEntry));
  644. }
  645. }
  646. }
  647. }
  648. }
  649. void ProjectLibrary::deleteEntry(const Path& path)
  650. {
  651. if(FileSystem::exists(path))
  652. FileSystem::remove(path);
  653. LibraryEntry* entry = findEntry(path);
  654. if(entry != nullptr)
  655. {
  656. if(entry->type == LibraryEntryType::File)
  657. {
  658. deleteResourceInternal(static_cast<ResourceEntry*>(entry));
  659. Path metaPath = getMetaPath(path);
  660. if(FileSystem::isFile(metaPath))
  661. FileSystem::remove(metaPath);
  662. }
  663. else if(entry->type == LibraryEntryType::Directory)
  664. deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
  665. }
  666. }
  667. void ProjectLibrary::reimport(const Path& path, const ImportOptionsPtr& importOptions, bool forceReimport)
  668. {
  669. LibraryEntry* entry = findEntry(path);
  670. if (entry != nullptr)
  671. {
  672. if (entry->type == LibraryEntryType::File)
  673. {
  674. ResourceEntry* resEntry = static_cast<ResourceEntry*>(entry);
  675. reimportResourceInternal(resEntry, importOptions, forceReimport);
  676. }
  677. }
  678. }
  679. void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf)
  680. {
  681. Path parentPath = fullPath;
  682. DirectoryEntry* newEntryParent = nullptr;
  683. Stack<Path> parentPaths;
  684. do
  685. {
  686. Path newParentPath = parentPath.getParent();
  687. if(newParentPath == parentPath)
  688. break;
  689. LibraryEntry* newEntryParentLib = findEntry(newParentPath);
  690. if(newEntryParentLib != nullptr)
  691. {
  692. assert(newEntryParentLib->type == LibraryEntryType::Directory);
  693. newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
  694. break;
  695. }
  696. parentPaths.push(newParentPath);
  697. parentPath = newParentPath;
  698. } while (true);
  699. assert(newEntryParent != nullptr); // Must exist
  700. if(newHierarchyRoot != nullptr)
  701. *newHierarchyRoot = newEntryParent;
  702. while(!parentPaths.empty())
  703. {
  704. Path curPath = parentPaths.top();
  705. parentPaths.pop();
  706. newEntryParent = addDirectoryInternal(newEntryParent, curPath);
  707. }
  708. if(newHierarchyLeaf != nullptr)
  709. *newHierarchyLeaf = newEntryParent;
  710. }
  711. Path ProjectLibrary::getMetaPath(const Path& path) const
  712. {
  713. Path metaPath = path;
  714. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  715. return metaPath;
  716. }
  717. bool ProjectLibrary::isMeta(const Path& fullPath) const
  718. {
  719. return fullPath.getWExtension() == L".meta";
  720. }
  721. void ProjectLibrary::onMonitorFileModified(const Path& path)
  722. {
  723. if(!isMeta(path))
  724. checkForModifications(path);
  725. else
  726. {
  727. Path resourcePath = path;
  728. resourcePath.setExtension(L"");
  729. checkForModifications(resourcePath);
  730. }
  731. }
  732. void ProjectLibrary::save()
  733. {
  734. std::shared_ptr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(*mRootEntry);
  735. Path libraryEntriesPath = mProjectFolder;
  736. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  737. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  738. FileEncoder fs(libraryEntriesPath);
  739. fs.encode(libEntries.get());
  740. Path resourceManifestPath = mProjectFolder;
  741. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  742. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  743. ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
  744. }
  745. void ProjectLibrary::load()
  746. {
  747. if(mRootEntry != nullptr)
  748. {
  749. deleteDirectoryInternal(mRootEntry);
  750. mRootEntry = nullptr;
  751. }
  752. mRootEntry = bs_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getWTail(), nullptr);
  753. Path libraryEntriesPath = mProjectFolder;
  754. libraryEntriesPath.append(INTERNAL_RESOURCES_DIR);
  755. libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
  756. if(FileSystem::exists(libraryEntriesPath))
  757. {
  758. FileDecoder fs(libraryEntriesPath);
  759. std::shared_ptr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
  760. *mRootEntry = libEntries->getRootEntry();
  761. for(auto& child : mRootEntry->mChildren)
  762. child->parent = mRootEntry;
  763. mRootEntry->parent = nullptr;
  764. }
  765. // Load all meta files
  766. Stack<DirectoryEntry*> todo;
  767. todo.push(mRootEntry);
  768. while(!todo.empty())
  769. {
  770. DirectoryEntry* curDir = todo.top();
  771. todo.pop();
  772. for(auto& child : curDir->mChildren)
  773. {
  774. if(child->type == LibraryEntryType::File)
  775. {
  776. ResourceEntry* resEntry = static_cast<ResourceEntry*>(child);
  777. if(resEntry->meta == nullptr)
  778. {
  779. Path metaPath = resEntry->path;
  780. metaPath.setFilename(metaPath.getWFilename() + L".meta");
  781. if(FileSystem::isFile(metaPath))
  782. {
  783. FileDecoder fs(metaPath);
  784. std::shared_ptr<IReflectable> loadedMeta = fs.decode();
  785. if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectResourceMeta::getRTTIStatic()))
  786. {
  787. ProjectResourceMetaPtr resourceMeta = std::static_pointer_cast<ProjectResourceMeta>(loadedMeta);
  788. resEntry->meta = resourceMeta;
  789. }
  790. }
  791. }
  792. }
  793. else if(child->type == LibraryEntryType::Directory)
  794. {
  795. todo.push(static_cast<DirectoryEntry*>(child));
  796. }
  797. }
  798. }
  799. // Load resource manifest
  800. Path resourceManifestPath = mProjectFolder;
  801. resourceManifestPath.append(INTERNAL_RESOURCES_DIR);
  802. resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
  803. if(FileSystem::exists(resourceManifestPath))
  804. {
  805. mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
  806. }
  807. }
  808. }