| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "Library/BsProjectLibrary.h"
- #include "FileSystem/BsFileSystem.h"
- #include "Error/BsException.h"
- #include "Resources/BsResources.h"
- #include "Resources/BsResourceManifest.h"
- #include "Importer/BsImporter.h"
- #include "Library/BsProjectResourceMeta.h"
- #include "Resources/BsResources.h"
- #include "Importer/BsImporter.h"
- #include "Importer/BsImportOptions.h"
- #include "Serialization/BsFileSerializer.h"
- #include "Debug/BsDebug.h"
- #include "Library/BsProjectLibraryEntries.h"
- #include "Resources/BsResource.h"
- #include "BsEditorApplication.h"
- #include "Material/BsShader.h"
- #include "Image/BsTexture.h"
- #include "String/BsUnicode.h"
- #include "CoreThread/BsCoreThread.h"
- #include <regex>
- #include "Threading/BsTaskScheduler.h"
- using namespace std::placeholders;
- namespace bs
- {
- ProjectResourceIcons generatePreviewIcons(Resource& resource)
- {
- ProjectResourceIcons icons;
- const UINT32 typeId = resource.getTypeId();
- if(typeId == TID_Texture)
- {
- Texture& texture = static_cast<Texture&>(resource);
- const TextureProperties& props = texture.getProperties();
- const SPtr<PixelData> srcData = props.allocBuffer(0, 0);
- AsyncOp readOp = texture.readData(srcData);
- gCoreThread().submitAll(true);
- // 256
- const SPtr<PixelData> data256 = PixelData::create(256, 256, 1, props.getFormat());
- PixelUtil::scale(*srcData, *data256);
- // 192
- const SPtr<PixelData> data192 = PixelData::create(192, 192, 1, props.getFormat());
- PixelUtil::scale(*data256, *data192);
- // 128
- const SPtr<PixelData> data128 = PixelData::create(128, 128, 1, props.getFormat());
- PixelUtil::scale(*data192, *data128);
- // 96
- const SPtr<PixelData> data96 = PixelData::create(96, 96, 1, props.getFormat());
- PixelUtil::scale(*data128, *data96);
- // 64
- const SPtr<PixelData> data64 = PixelData::create(64, 64, 1, props.getFormat());
- PixelUtil::scale(*data96, *data64);
- // 48
- const SPtr<PixelData> data48 = PixelData::create(48, 48, 1, props.getFormat());
- PixelUtil::scale(*data64, *data48);
- // 32
- const SPtr<PixelData> data32 = PixelData::create(32, 32, 1, props.getFormat());
- PixelUtil::scale(*data48, *data32);
- // 16
- const SPtr<PixelData> data16 = PixelData::create(16, 16, 1, props.getFormat());
- PixelUtil::scale(*data32, *data16);
- icons.icon16 = Texture::create(data16);
- icons.icon32 = Texture::create(data32);
- icons.icon48 = Texture::create(data48);
- icons.icon64 = Texture::create(data64);
- icons.icon96 = Texture::create(data96);
- icons.icon128 = Texture::create(data128);
- icons.icon192 = Texture::create(data192);
- icons.icon256 = Texture::create(data256);
- }
- return icons;
- }
- const Path TEMP_DIR = "Temp/";
- const Path INTERNAL_TEMP_DIR = PROJECT_INTERNAL_DIR + TEMP_DIR;
- const Path ProjectLibrary::RESOURCES_DIR = "Resources/";
- const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + RESOURCES_DIR;
- const char* ProjectLibrary::LIBRARY_ENTRIES_FILENAME = "ProjectLibrary.asset";
- const char* ProjectLibrary::RESOURCE_MANIFEST_FILENAME = "ResourceManifest.asset";
- ProjectLibrary::LibraryEntry::LibraryEntry()
- :type(LibraryEntryType::Directory)
- { }
- ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const String& name, DirectoryEntry* parent,
- LibraryEntryType type)
- :type(type), path(path), elementName(name), elementNameHash(bs_hash(UTF8::toLower(name))), parent(parent)
- { }
- ProjectLibrary::FileEntry::FileEntry(const Path& path, const String& name, DirectoryEntry* parent)
- :LibraryEntry(path, name, parent, LibraryEntryType::File)
- { }
- ProjectLibrary::DirectoryEntry::DirectoryEntry(const Path& path, const String& name, DirectoryEntry* parent)
- :LibraryEntry(path, name, parent, LibraryEntryType::Directory)
- { }
- ProjectLibrary::ProjectLibrary()
- : mRootEntry(nullptr), mIsLoaded(false)
- {
- mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
- }
- ProjectLibrary::~ProjectLibrary()
- {
- _finishQueuedImports(true);
- clearEntries();
- }
- UINT32 ProjectLibrary::checkForModifications(const Path& fullPath)
- {
- UINT32 resourcesToImport = 0;
- if (!mResourcesFolder.includes(fullPath))
- return resourcesToImport; // Folder not part of our resources path, so no modifications
- if(mRootEntry == nullptr)
- mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
- Path pathToSearch = fullPath;
- USPtr<LibraryEntry> entry = findEntry(pathToSearch);
- if (entry == nullptr) // File could be new, try to find parent directory entry
- {
- if (FileSystem::exists(pathToSearch))
- {
- if (isMeta(pathToSearch))
- {
- Path sourceFilePath = pathToSearch;
- sourceFilePath.setExtension("");
- if (!FileSystem::isFile(sourceFilePath))
- {
- LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
- FileSystem::remove(pathToSearch);
- }
- }
- else
- {
- Path parentDirPath = pathToSearch.getParent();
- entry = findEntry(parentDirPath);
- // Cannot find parent directory. Create the needed hierarchy.
- DirectoryEntry* entryParent = nullptr;
- DirectoryEntry* newHierarchyParent = nullptr;
- if (entry == nullptr)
- createInternalParentHierarchy(pathToSearch, &newHierarchyParent, &entryParent);
- else
- entryParent = static_cast<DirectoryEntry*>(entry.get());
- if (FileSystem::isFile(pathToSearch))
- addResourceInternal(entryParent, pathToSearch);
- else if (FileSystem::isDirectory(pathToSearch))
- {
- addDirectoryInternal(entryParent, pathToSearch);
- resourcesToImport += checkForModifications(pathToSearch);
- }
- }
- }
- }
- else if(entry->type == LibraryEntryType::File)
- {
- if(FileSystem::isFile(entry->path))
- {
- FileEntry* resEntry = static_cast<FileEntry*>(entry.get());
- if(reimportResourceInternal(resEntry))
- resourcesToImport++;
- }
- else
- deleteResourceInternal(static_pointer_cast<FileEntry>(entry));
- }
- else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
- {
- if(!FileSystem::isDirectory(entry->path))
- {
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(entry));
- }
- else
- {
- Stack<DirectoryEntry*> todo;
- todo.push(static_cast<DirectoryEntry*>(entry.get()));
- Vector<Path> childFiles;
- Vector<Path> childDirectories;
- Vector<bool> existingEntries;
- Vector<USPtr<LibraryEntry>> toDelete;
- while(!todo.empty())
- {
- DirectoryEntry* currentDir = todo.top();
- todo.pop();
- existingEntries.clear();
- existingEntries.resize(currentDir->mChildren.size());
- for(UINT32 i = 0; i < (UINT32)currentDir->mChildren.size(); i++)
- existingEntries[i] = false;
- childFiles.clear();
- childDirectories.clear();
- FileSystem::getChildren(currentDir->path, childFiles, childDirectories);
-
- for(auto& filePath : childFiles)
- {
- if(isMeta(filePath))
- {
- Path sourceFilePath = filePath;
- sourceFilePath.setExtension("");
- if(!FileSystem::isFile(sourceFilePath))
- {
- LOGWRN("Found a .meta file without a corresponding resource. Deleting.");
- FileSystem::remove(filePath);
- }
- }
- else
- {
- FileEntry* existingEntry = nullptr;
- UINT32 idx = 0;
- for(auto& child : currentDir->mChildren)
- {
- if(child->type == LibraryEntryType::File && child->path == filePath)
- {
- existingEntries[idx] = true;
- existingEntry = static_cast<FileEntry*>(child.get());
- break;
- }
- idx++;
- }
- if(existingEntry != nullptr)
- {
- if(reimportResourceInternal(existingEntry))
- resourcesToImport++;
- }
- else
- {
- addResourceInternal(currentDir, filePath);
- resourcesToImport++;
- }
- }
- }
- for(auto& dirPath : childDirectories)
- {
- DirectoryEntry* existingEntry = nullptr;
- UINT32 idx = 0;
- for(auto& child : currentDir->mChildren)
- {
- if(child->type == LibraryEntryType::Directory && child->path == dirPath)
- {
- existingEntries[idx] = true;
- existingEntry = static_cast<DirectoryEntry*>(child.get());
- break;
- }
- idx++;
- }
- if(existingEntry == nullptr)
- addDirectoryInternal(currentDir, dirPath);
- }
- {
- for(UINT32 i = 0; i < (UINT32)existingEntries.size(); i++)
- {
- if(existingEntries[i])
- continue;
- toDelete.push_back(currentDir->mChildren[i]);
- }
- for(auto& child : toDelete)
- {
- if(child->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(child));
- else if(child->type == LibraryEntryType::File)
- deleteResourceInternal(static_pointer_cast<FileEntry>(child));
- }
- toDelete.clear();
- }
- for(auto& child : currentDir->mChildren)
- {
- if(child->type == LibraryEntryType::Directory)
- todo.push(static_cast<DirectoryEntry*>(child.get()));
- }
- }
- }
- }
- return resourcesToImport;
- }
- USPtr<ProjectLibrary::FileEntry> ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
- const SPtr<ImportOptions>& importOptions, bool forceReimport, bool synchronous)
- {
- USPtr<FileEntry> newResource = bs_ushared_ptr_new<FileEntry>(filePath, filePath.getTail(), parent);
- parent->mChildren.push_back(newResource);
- reimportResourceInternal(newResource.get(), importOptions, forceReimport, false, synchronous);
- onEntryAdded(newResource->path);
- return newResource;
- }
- USPtr<ProjectLibrary::DirectoryEntry> ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
- {
- USPtr<DirectoryEntry> newEntry = bs_ushared_ptr_new<DirectoryEntry>(dirPath, dirPath.getTail(), parent);
- parent->mChildren.push_back(newEntry);
- onEntryAdded(newEntry->path);
- return newEntry;
- }
- void ProjectLibrary::deleteResourceInternal(USPtr<FileEntry> resource)
- {
- if(resource->meta != nullptr)
- {
- auto& resourceMetas = resource->meta->getResourceMetaData();
- for(auto& entry : resourceMetas)
- {
- const UUID& uuid = entry->getUUID();
- Path path;
- if (mResourceManifest->uuidToFilePath(uuid, path))
- {
- if (FileSystem::isFile(path))
- FileSystem::remove(path);
- mResourceManifest->unregisterResource(uuid);
- }
- mUUIDToPath.erase(uuid);
- }
- }
- Path metaPath = getMetaPath(resource->path);
- if (FileSystem::isFile(metaPath))
- FileSystem::remove(metaPath);
- DirectoryEntry* parent = resource->parent;
- auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
- [&] (const USPtr<LibraryEntry>& entry) { return entry == resource; });
- parent->mChildren.erase(findIter);
- Path originalPath = resource->path;
- onEntryRemoved(originalPath);
- const auto iterQueuedImport = mQueuedImports.find(resource.get());
- if(iterQueuedImport != mQueuedImports.end())
- iterQueuedImport->second->canceled = true;
- removeDependencies(resource.get());
- *resource = FileEntry();
- reimportDependants(originalPath);
- }
- void ProjectLibrary::deleteDirectoryInternal(USPtr<DirectoryEntry> directory)
- {
- if(directory == mRootEntry)
- mRootEntry = nullptr;
- Vector<USPtr<LibraryEntry>> childrenToDestroy = directory->mChildren;
- for(auto& child : childrenToDestroy)
- {
- if(child->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(child));
- else
- deleteResourceInternal(static_pointer_cast<FileEntry>(child));
- }
- DirectoryEntry* parent = directory->parent;
- if(parent != nullptr)
- {
- auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
- [&] (const USPtr<LibraryEntry>& entry) { return entry == directory; });
- parent->mChildren.erase(findIter);
- }
- onEntryRemoved(directory->path);
- *directory = DirectoryEntry();
- }
- bool ProjectLibrary::reimportResourceInternal(FileEntry* fileEntry, const SPtr<ImportOptions>& importOptions,
- bool forceReimport, bool pruneResourceMetas, bool synchronous)
- {
- Path metaPath = fileEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- // If the file doesn't have meta-data, attempt to read it from a meta-file, if one exists. This can only happen
- // if library data is obsolete (e.g. when adding files from another copy of the project)
- if(fileEntry->meta == nullptr)
- {
- if(FileSystem::isFile(metaPath))
- {
- FileDecoder fs(metaPath);
- SPtr<IReflectable> loadedMeta = fs.decode();
- if(loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
- {
- const SPtr<ProjectFileMeta>& fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
- fileEntry->meta = fileMeta;
- auto& resourceMetas = fileEntry->meta->getResourceMetaData();
- if (!resourceMetas.empty())
- {
- mUUIDToPath[resourceMetas[0]->getUUID()] = fileEntry->path;
- for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
- {
- const SPtr<ProjectResourceMeta>& entry = resourceMetas[i];
- mUUIDToPath[entry->getUUID()] = fileEntry->path + entry->getUniqueName();
- }
- }
- }
- }
- }
- if (!isUpToDate(fileEntry) || forceReimport)
- {
- // Note: If resource is native we just copy it to the internal folder. We could avoid the copy and
- // load the resource directly from the Resources folder but that requires complicating library code.
- const bool isNativeResource = isNative(fileEntry->path);
- SPtr<ImportOptions> curImportOptions = nullptr;
- if (importOptions == nullptr && !isNativeResource)
- {
- if (fileEntry->meta != nullptr)
- curImportOptions = fileEntry->meta->getImportOptions();
- else
- curImportOptions = Importer::instance().createImportOptions(fileEntry->path);
- }
- else
- curImportOptions = importOptions;
- SPtr<QueuedImport> queuedImport = bs_shared_ptr_new<QueuedImport>();
- queuedImport->filePath = fileEntry->path;
- queuedImport->importOptions = curImportOptions;
- queuedImport->pruneMetas = pruneResourceMetas;
- queuedImport->native = isNativeResource;
- queuedImport->timestamp = std::time(nullptr);
- // If import is already queued for this file make the tasks dependant so they don't execute at the same time,
- // and so they execute in the proper order
- SPtr<Task> dependency;
- const auto iterFind = mQueuedImports.find(fileEntry);
- if (iterFind != mQueuedImports.end())
- {
- dependency = iterFind->second->importTask;
- // Need this reference just so the dependency is kept alive, otherwise it goes out of scope when we
- // remove or overwrite it from mQueuedImports map
- queuedImport->dependsOn = iterFind->second;
- // Note: We should cancel the task here so it doesn't run unnecessarily. But if the task is already
- // running it shouldn't be canceled as dependencies still need to wait on it (since cancelling a
- // running task doesn't actually stop it). Yet there is currently no good wait to check if task
- // is currently running.
- // Dependency being imported async but we want the current resource right away. Wait until dependency is
- // done otherwise when dependency finishes it will overwrite whatever we write now.
- if(synchronous && dependency)
- {
- if (finishQueuedImport(fileEntry, *iterFind->second, true))
- mQueuedImports.erase(iterFind);
- }
- }
-
- // Needs to be pass a weak pointer to worker methods since internally it holds a reference to the task itself,
- // and we can't have the task closure holding a reference back, otherwise it leaks
- std::weak_ptr<QueuedImport> queuedImportWeak = queuedImport;
- if(!isNativeResource)
- {
- // Find UUIDs for any existing sub-resources
- if (fileEntry->meta != nullptr)
- {
- const Vector<SPtr<ProjectResourceMeta>>& resourceMetas = fileEntry->meta->getAllResourceMetaData();
- for (auto& entry : resourceMetas)
- queuedImport->resources.emplace_back(entry->getUniqueName(), nullptr, entry->getUUID());
- }
- // Perform import, register the resources and their UUID in the QueuedImport structure and save the
- // resource on disk
- const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
- {
- SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
- Vector<SubResourceRaw> importedResources = gImporter()._importAll(queuedImport->filePath,
- queuedImport->importOptions);
- if (!importedResources.empty())
- {
- Path outputPath = projectFolder;
- outputPath.append(INTERNAL_TEMP_DIR);
- if (!FileSystem::isDirectory(outputPath))
- FileSystem::createDir(outputPath);
- for (auto& entry : importedResources)
- {
- String subresourceName = entry.name;
- Path::stripInvalid(subresourceName);
- UUID uuid;
- {
- // Any access to queuedImport->resources must be locked
- Lock lock(mutex);
- auto iterFind = std::find_if(queuedImport->resources.begin(), queuedImport->resources.end(),
- [&subresourceName](const QueuedImportResource& importResource)
- {
- return importResource.name == subresourceName;
- });
- if (iterFind != queuedImport->resources.end())
- iterFind->resource = entry.value;
- else
- {
- queuedImport->resources.push_back(QueuedImportResource(entry.name, entry.value,
- UUID::EMPTY));
- iterFind = queuedImport->resources.end() - 1;
- }
- if (iterFind->uuid.empty())
- iterFind->uuid = UUIDGenerator::generateRandom();
- uuid = iterFind->uuid;
- }
- const String uuidStr = uuid.toString();
- outputPath.setFilename(uuidStr + ".asset");
- gResources()._save(entry.value, outputPath, true);
- }
- }
- };
- if(!synchronous)
- {
- queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
- dependency);
- }
- else
- importAsync();
- }
- else
- {
- // If meta exists make sure it is registered in the manifest before load, otherwise it will get assigned a new UUID.
- // This can happen if library isn't properly saved before exiting the application.
- if (fileEntry->meta != nullptr)
- {
- auto& resourceMetas = fileEntry->meta->getResourceMetaData();
- mResourceManifest->registerResource(resourceMetas[0]->getUUID(), fileEntry->path);
- }
- const auto importAsync = [queuedImportWeak, &projectFolder = mProjectFolder, &mutex = mQueuedImportMutex]()
- {
- // Don't load dependencies because we don't need them, but also because they might not be in the
- // manifest which would screw up their UUIDs.
- SPtr<QueuedImport> queuedImport = queuedImportWeak.lock();
- HResource resource = gResources().load(queuedImport->filePath, ResourceLoadFlag::KeepSourceData);
- if (resource)
- {
- Path outputPath = projectFolder;
- outputPath.append(INTERNAL_TEMP_DIR);
- if (!FileSystem::isDirectory(outputPath))
- FileSystem::createDir(outputPath);
- {
- // Any access to queuedImport->resources must be locked
- Lock lock(mutex);
- queuedImport->resources.push_back(QueuedImportResource("primary", resource.getInternalPtr(),
- resource.getUUID()));
- }
- const String uuidStr = resource.getUUID().toString();
- outputPath.setFilename(uuidStr + ".asset");
- gResources()._save(resource.getInternalPtr(), outputPath, true);
- }
- };
- if(!synchronous)
- {
- queuedImport->importTask = Task::create("ProjectLibraryImport", importAsync, TaskPriority::Normal,
- dependency);
- }
- else
- importAsync();
- }
- if(!synchronous)
- {
- TaskScheduler::instance().addTask(queuedImport->importTask);
- mQueuedImports[fileEntry] = queuedImport;
- }
- if(synchronous)
- finishQueuedImport(fileEntry, *queuedImport, true);
- return true;
- }
- return false;
- }
- bool ProjectLibrary::finishQueuedImport(FileEntry* fileEntry, const QueuedImport& import, bool wait)
- {
- if (import.importTask != nullptr && !import.importTask->isComplete())
- {
- if (wait)
- import.importTask->wait();
- else
- return false;
- }
- // We wait on canceled task to finish and then just discard the results because any dependant tasks need to be
- // aware this tasks exists, so we can't just remove it straight away.
- if (import.canceled)
- return true;
- Path metaPath = fileEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- Vector<SPtr<ProjectResourceMeta>> existingMetas;
- if (fileEntry->meta == nullptr) // Build a brand new meta-file
- fileEntry->meta = ProjectFileMeta::create(import.importOptions);
- else // Existing meta-file, which needs to be updated
- {
- // Remove existing dependencies (they will be re-added later)
- removeDependencies(fileEntry);
- existingMetas = fileEntry->meta->getAllResourceMetaData();
- fileEntry->meta->clearResourceMetaData();
- fileEntry->meta->mImportOptions = import.importOptions;
- }
- fileEntry->lastUpdateTime = import.timestamp;
- Path internalResourcesPath = mProjectFolder;
- internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
- if (!FileSystem::isDirectory(internalResourcesPath))
- FileSystem::createDir(internalResourcesPath);
- Path tempResourcesPath = mProjectFolder;
- tempResourcesPath.append(INTERNAL_TEMP_DIR);
- // See which sub-resource metas need to be updated, removed or added based on the new resource set
- bool isFirst = true;
- for (const auto& entry : import.resources)
- {
- // Entries with no resources are sub-resources that used to exist in this file, but haven't been imported
- // this time
- if (!entry.resource)
- continue;
- // Copy the resource file from the temporary directory
- const String uuidStr = entry.uuid.toString();
- tempResourcesPath.setFilename(uuidStr + ".asset");
- internalResourcesPath.setFilename(uuidStr + ".asset");
- FileSystem::move(tempResourcesPath, internalResourcesPath);
- String name = entry.name;
- Path::stripInvalid(name);
- const ProjectResourceIcons icons = generatePreviewIcons(*entry.resource);
- bool foundMeta = false;
- for (auto iterMeta = existingMetas.begin(); iterMeta != existingMetas.end(); ++iterMeta)
- {
- const SPtr<ProjectResourceMeta>& metaEntry = *iterMeta;
- if (name == metaEntry->getUniqueName())
- {
- // Make sure the UUID we used for saving the resource matches the current one (should always
- // be true unless the meta-data somehow changes while the async import is happening)
- assert(entry.uuid == metaEntry->getUUID());
- HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
- gResources().update(importedResource, entry.resource);
- metaEntry->setPreviewIcons(icons);
- fileEntry->meta->add(metaEntry);
- iterMeta = existingMetas.erase(iterMeta);
- foundMeta = true;
- break;
- }
- }
- if (!foundMeta)
- {
- HResource importedResource;
- // Native resources are always expected to have a handle since Resources::load was called during
- // the 'import' step
- if (import.native)
- {
- importedResource = gResources()._getResourceHandle(entry.uuid);
- gResources().update(importedResource, entry.resource);
- }
- else
- importedResource = gResources()._createResourceHandle(entry.resource, entry.uuid);
- SPtr<ResourceMetaData> subMeta = entry.resource->getMetaData();
- const UINT32 typeId = entry.resource->getTypeId();
- const UUID& UUID = importedResource.getUUID();
- SPtr<ProjectResourceMeta> resMeta = ProjectResourceMeta::create(name, UUID, typeId,
- icons, subMeta);
- fileEntry->meta->add(resMeta);
- }
- // Keep resource metas that we are not currently using, in case they get restored so their references
- // don't get broken
- if (!import.pruneMetas)
- {
- for (auto& metaEntry : existingMetas)
- fileEntry->meta->addInactive(metaEntry);
- }
- // Update UUID to path mapping
- if (isFirst)
- mUUIDToPath[entry.uuid] = fileEntry->path;
- else
- mUUIDToPath[entry.uuid] = fileEntry->path + name;
- isFirst = false;
- // Register path in manifest
- mResourceManifest->registerResource(entry.uuid, internalResourcesPath);
- }
- // Save the meta file
- FileEncoder fs(metaPath);
- fs.encode(fileEntry->meta.get());
- // Register any dependencies this resource depends on
- addDependencies(fileEntry);
- // Notify the outside world import is doen
- onEntryImported(fileEntry->path);
- // Queue any resources dependant on this one for import
- reimportDependants(fileEntry->path);
- return true;
- }
- void ProjectLibrary::_finishQueuedImports(bool wait)
- {
- for(auto iter = mQueuedImports.begin(); iter != mQueuedImports.end();)
- {
- if(finishQueuedImport(iter->first, *iter->second, wait))
- iter = mQueuedImports.erase(iter);
- else
- ++iter;
- }
- }
- bool ProjectLibrary::isUpToDate(FileEntry* resource) const
- {
- SPtr<QueuedImport> queuedImport;
- if(resource->meta == nullptr)
- {
- // Allow no meta if import in progress
- const auto iterFind = mQueuedImports.find(resource);
- if(iterFind == mQueuedImports.end())
- return false;
- queuedImport = iterFind->second;
- }
- else
- {
- auto& resourceMetas = resource->meta->getResourceMetaData();
- for (auto& resMeta : resourceMetas)
- {
- Path internalPath;
- if (!mResourceManifest->uuidToFilePath(resMeta->getUUID(), internalPath))
- return false;
- if (!FileSystem::isFile(internalPath))
- return false;
- }
- }
- // Note: We're keeping separate update times for queued imports. This allows the import to be cancelled (either by
- // user or by app crashing), without updating the actual update time. This way the systems knows to try to reimport
- // the resource on the next check. At the same time we don't want our checkForModifications function to keep
- // trying to reimport a resource if it's already been queued for import.
- const std::time_t lastUpdateTime = queuedImport ? queuedImport->timestamp : resource->lastUpdateTime;
- const std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
- return lastModifiedTime <= lastUpdateTime;
- }
- Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern)
- {
- return search(pattern, {});
- }
- Vector<USPtr<ProjectLibrary::LibraryEntry>> ProjectLibrary::search(const String& pattern, const Vector<UINT32>& typeIds)
- {
- Vector<USPtr<LibraryEntry>> foundEntries;
- std::regex escape("[.^$|()\\[\\]{}*+?\\\\]");
- String replace("\\\\&");
- String escapedPattern = std::regex_replace(pattern, escape, replace, std::regex_constants::match_default | std::regex_constants::format_sed);
- // For some reason MSVC stdlib implementation requires a different pattern than stdlib one
- #if BS_PLATFORM == BS_PLATFORM_WIN32
- std::regex wildcard("\\\\\\*");
- #else
- std::regex wildcard("\\\\\\\\\\*");
- #endif
- String wildcardReplace(".*");
- String searchPattern = std::regex_replace(escapedPattern, wildcard, ".*");
- std::regex searchRegex(searchPattern, std::regex_constants::ECMAScript | std::regex_constants::icase);
- Stack<DirectoryEntry*> todo;
- todo.push(mRootEntry.get());
- while (!todo.empty())
- {
- DirectoryEntry* dirEntry = todo.top();
- todo.pop();
- for (auto& child : dirEntry->mChildren)
- {
- if (std::regex_match(child->elementName, searchRegex))
- {
- if (typeIds.empty())
- foundEntries.push_back(child);
- else
- {
- if (child->type == LibraryEntryType::File)
- {
- FileEntry* childFileEntry = static_cast<FileEntry*>(child.get());
- if (childFileEntry->meta != nullptr)
- {
- auto& resourceMetas = childFileEntry->meta->getResourceMetaData();
- for (auto& typeId : typeIds)
- {
- bool found = false;
- for (auto& resMeta : resourceMetas)
- {
- if (resMeta->getTypeID() == typeId)
- {
- foundEntries.push_back(child);
- found = true;
- break;
- }
- }
- if (found)
- break;
- }
- }
- }
- }
- }
- if (child->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* childDirEntry = static_cast<DirectoryEntry*>(child.get());
- todo.push(childDirEntry);
- }
- }
- }
- std::sort(foundEntries.begin(), foundEntries.end(),
- [&](const USPtr<LibraryEntry>& a, const USPtr<LibraryEntry>& b)
- {
- return a->elementName.compare(b->elementName) < 0;
- });
- return foundEntries;
- }
- USPtr<ProjectLibrary::LibraryEntry> ProjectLibrary::findEntry(const Path& path) const
- {
- Path relPath;
- const Path* searchPath;
- if (path.isAbsolute())
- {
- if (!mResourcesFolder.includes(path))
- return nullptr;
- relPath = path.getRelative(mRootEntry->path);
- searchPath = &relPath;
- }
- else
- searchPath = &path;
- BS_ASSERT(mRootEntry->path == mResourcesFolder);
- UINT32 numElems = searchPath->getNumDirectories() + (searchPath->isFile() ? 1 : 0);
- UINT32 idx = 0;
- USPtr<LibraryEntry> rootLibEntry = mRootEntry;
- USPtr<LibraryEntry>* current = &rootLibEntry;
- while (current != nullptr)
- {
- if (idx == numElems)
- return *current;
- const String& curElem =
- (searchPath->isFile() && idx == (numElems - 1)) ? searchPath->getFilename() : (*searchPath)[idx];
- if ((*current)->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current->get());
- size_t curElemHash = bs_hash(UTF8::toLower(curElem));
- current = nullptr;
- for (auto& child : dirEntry->mChildren)
- {
- if(curElemHash != child->elementNameHash)
- continue;
- if (Path::comparePathElem(curElem, child->elementName))
- {
- idx++;
- current = &child;
- break;
- }
- }
- }
- else // Found file
- {
- // If this is next to last element, next entry is assumed to be a sub-resource name, which we ignore
- if (idx == (numElems - 1))
- return *current;
- else
- break; // Not a valid path
- }
- }
- return nullptr;
- }
- bool ProjectLibrary::isSubresource(const Path& path) const
- {
- UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
- if (numElems <= 1)
- return false;
- Path filePath = path;
- filePath.makeParent();
- LibraryEntry* entry = findEntry(filePath).get();
- return entry != nullptr && entry->type == LibraryEntryType::File;
- }
- SPtr<ProjectResourceMeta> ProjectLibrary::findResourceMeta(const Path& path) const
- {
- UINT32 numElems = path.getNumDirectories() + (path.isFile() ? 1 : 0);
- // Check if it is a subresource path
- if(numElems > 1)
- {
- Path filePath = path;
- filePath.makeParent();
- LibraryEntry* entry = findEntry(filePath).get();
- if (entry == nullptr)
- return nullptr;
- // Entry is a subresource
- if (entry->type == LibraryEntryType::File)
- {
- FileEntry* fileEntry = static_cast<FileEntry*>(entry);
- if (fileEntry->meta == nullptr)
- return nullptr;
- auto& resourceMetas = fileEntry->meta->getResourceMetaData();
- for(auto& resMeta : resourceMetas)
- {
- if (resMeta->getUniqueName() == path.getTail())
- return resMeta;
- }
- // Found the file but no subresource or meta information
- return nullptr;
- }
- else // Entry not a subresource
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
- for (auto& child : dirEntry->mChildren)
- {
- if (Path::comparePathElem(path.getTail(), child->elementName))
- {
- if (child->type == LibraryEntryType::File)
- {
- FileEntry* fileEntry = static_cast<FileEntry*>(child.get());
- if (fileEntry->meta == nullptr)
- return nullptr;
- return fileEntry->meta->getResourceMetaData()[0];
- }
- }
- }
- return nullptr;
- }
- }
- // Not a subresource path, load directly
- {
- LibraryEntry* entry = findEntry(path).get();
- if (entry == nullptr || entry->type == LibraryEntryType::Directory)
- return nullptr;
- FileEntry* fileEntry = static_cast<FileEntry*>(entry);
- if (fileEntry->meta == nullptr)
- return nullptr;
- return fileEntry->meta->getResourceMetaData()[0];
- }
- }
- Path ProjectLibrary::uuidToPath(const UUID& uuid) const
- {
- auto iterFind = mUUIDToPath.find(uuid);
- if (iterFind != mUUIDToPath.end())
- return iterFind->second;
- return Path::BLANK;
- }
- void ProjectLibrary::createEntry(const HResource& resource, const Path& path)
- {
- if (resource == nullptr)
- return;
- Path assetPath = path;
- if (path.isAbsolute())
- {
- if (!getResourcesFolder().includes(path))
- return;
- assetPath = path.getRelative(getResourcesFolder());
- }
- deleteEntry(assetPath);
- resource->setName(path.getFilename(false));
- Path absPath = assetPath.getAbsolute(getResourcesFolder());
- Resources::instance().save(resource, absPath, false);
- Path parentDirPath = absPath.getParent();
- USPtr<LibraryEntry> parentEntry = findEntry(parentDirPath);
- // Register parent hierarchy if not found
- DirectoryEntry* entryParent = nullptr;
- if (parentEntry == nullptr)
- createInternalParentHierarchy(absPath, nullptr, &entryParent);
- else
- entryParent = static_cast<DirectoryEntry*>(parentEntry.get());
- addResourceInternal(entryParent, absPath, nullptr, true, true);
- }
- void ProjectLibrary::saveEntry(const HResource& resource)
- {
- if (resource == nullptr)
- return;
- Path filePath = uuidToPath(resource.getUUID());
- if(filePath.isEmpty())
- {
- LOGWRN("Trying to save a resource that hasn't been registered with the project library. Call ProjectLibrary::create first.");
- return;
- }
- filePath.makeAbsolute(getResourcesFolder());
- Resources::instance().save(resource, filePath, true);
- LibraryEntry* fileEntry = findEntry(filePath).get();
- if(fileEntry)
- reimportResourceInternal(static_cast<FileEntry*>(fileEntry), nullptr, true, false, true);
- }
- void ProjectLibrary::createFolderEntry(const Path& path)
- {
- Path fullPath = path;
- if (fullPath.isAbsolute())
- {
- if (!mResourcesFolder.includes(fullPath))
- return;
- }
- else
- fullPath.makeAbsolute(mResourcesFolder);
- if (FileSystem::isDirectory(fullPath))
- return; // Already exists
- FileSystem::createDir(fullPath);
- Path parentPath = fullPath.getParent();
- DirectoryEntry* newEntryParent = nullptr;
- USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
- if (newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
- }
- DirectoryEntry* newHierarchyParent = nullptr;
- if (newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
- createInternalParentHierarchy(fullPath, &newHierarchyParent, &newEntryParent);
- addDirectoryInternal(newEntryParent, fullPath);
- }
- void ProjectLibrary::moveEntry(const Path& oldPath, const Path& newPath, bool overwrite)
- {
- Path oldFullPath = oldPath;
- if (!oldFullPath.isAbsolute())
- oldFullPath.makeAbsolute(mResourcesFolder);
- Path newFullPath = newPath;
- if (!newFullPath.isAbsolute())
- newFullPath.makeAbsolute(mResourcesFolder);
- Path parentPath = newFullPath.getParent();
- if (!FileSystem::isDirectory(parentPath))
- {
- LOGWRN("Move operation failed. Destination directory \"" + parentPath.toString() + "\" doesn't exist.");
- return;
- }
- if(FileSystem::isFile(oldFullPath) || FileSystem::isDirectory(oldFullPath))
- FileSystem::move(oldFullPath, newFullPath, overwrite);
- Path oldMetaPath = getMetaPath(oldFullPath);
- Path newMetaPath = getMetaPath(newFullPath);
- USPtr<LibraryEntry> oldEntry = findEntry(oldFullPath);
- if(oldEntry != nullptr) // Moving from the Resources folder
- {
- // Moved outside of Resources, delete entry & meta file
- if (!mResourcesFolder.includes(newFullPath))
- {
- if(oldEntry->type == LibraryEntryType::File)
- deleteResourceInternal(static_pointer_cast<FileEntry>(oldEntry));
- else if(oldEntry->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(oldEntry));
- }
- else // Just moving internally
- {
- onEntryRemoved(oldEntry->path);
- USPtr<FileEntry> fileEntry = nullptr;
- if (oldEntry->type == LibraryEntryType::File)
- {
- fileEntry = static_pointer_cast<FileEntry>(oldEntry);
- removeDependencies(fileEntry.get());
- // Update uuid <-> path mapping
- if(fileEntry->meta != nullptr)
- {
- auto& resourceMetas = fileEntry->meta->getResourceMetaData();
- if (!resourceMetas.empty())
- {
- mUUIDToPath[resourceMetas[0]->getUUID()] = newFullPath;
- for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
- {
- SPtr<ProjectResourceMeta> resMeta = resourceMetas[i];
- const UUID& UUID = resMeta->getUUID();
- mUUIDToPath[UUID] = newFullPath + resMeta->getUniqueName();
- }
- }
- }
- }
- if(FileSystem::isFile(oldMetaPath))
- FileSystem::move(oldMetaPath, newMetaPath);
- DirectoryEntry* parent = oldEntry->parent;
- auto findIter = std::find(parent->mChildren.begin(), parent->mChildren.end(), oldEntry);
- if(findIter != parent->mChildren.end())
- parent->mChildren.erase(findIter);
- Path parentPath = newFullPath.getParent();
- DirectoryEntry* newEntryParent = nullptr;
- USPtr<LibraryEntry> newEntryParentLib = findEntry(parentPath);
- if(newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib.get());
- }
- DirectoryEntry* newHierarchyParent = nullptr;
- if(newEntryParent == nullptr) // New path parent doesn't exist, so we need to create the hierarchy
- createInternalParentHierarchy(newFullPath, &newHierarchyParent, &newEntryParent);
- newEntryParent->mChildren.push_back(oldEntry);
- oldEntry->parent = newEntryParent;
- oldEntry->path = newFullPath;
- oldEntry->elementName = newFullPath.getTail();
- oldEntry->elementNameHash = bs_hash(UTF8::toLower(oldEntry->elementName));
- if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
- {
- Stack<LibraryEntry*> todo;
- todo.push(oldEntry.get());
- while(!todo.empty())
- {
- LibraryEntry* curEntry = todo.top();
- todo.pop();
- DirectoryEntry* curDirEntry = static_cast<DirectoryEntry*>(curEntry);
- for(auto& child : curDirEntry->mChildren)
- {
- child->path = child->parent->path;
- child->path.append(child->elementName);
- if(child->type == LibraryEntryType::Directory)
- todo.push(child.get());
- }
- }
- }
- onEntryAdded(oldEntry->path);
- if (fileEntry != nullptr)
- {
- reimportDependants(oldFullPath);
- reimportDependants(newFullPath);
- }
- }
- }
- else // Moving from outside of the Resources folder (likely adding a new resource)
- {
- checkForModifications(newFullPath);
- }
- }
- void ProjectLibrary::copyEntry(const Path& oldPath, const Path& newPath, bool overwrite)
- {
- Path oldFullPath = oldPath;
- if (!oldFullPath.isAbsolute())
- oldFullPath.makeAbsolute(mResourcesFolder);
- Path newFullPath = newPath;
- if (!newFullPath.isAbsolute())
- newFullPath.makeAbsolute(mResourcesFolder);
- if (!FileSystem::exists(oldFullPath))
- return;
- FileSystem::copy(oldFullPath, newFullPath, overwrite);
- // Copying a file/folder outside of the Resources folder, no special handling needed
- if (!mResourcesFolder.includes(newFullPath))
- return;
- Path parentPath = newFullPath.getParent();
- DirectoryEntry* newEntryParent = nullptr;
- LibraryEntry* newEntryParentLib = findEntry(parentPath).get();
- if (newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
- }
- // If the source is outside of Resources folder, just plain import the copy
- LibraryEntry* oldEntry = findEntry(oldFullPath).get();
- if (oldEntry == nullptr)
- {
- checkForModifications(newFullPath);
- return;
- }
- // Both source and destination are within Resources folder, need to preserve import options on the copies
- if (FileSystem::isFile(newFullPath))
- {
- assert(oldEntry->type == LibraryEntryType::File);
- FileEntry* oldResEntry = static_cast<FileEntry*>(oldEntry);
- SPtr<ImportOptions> importOptions;
- if (oldResEntry->meta != nullptr)
- importOptions = oldResEntry->meta->getImportOptions();
- addResourceInternal(newEntryParent, newFullPath, importOptions, true);
- }
- else
- {
- assert(oldEntry->type == LibraryEntryType::File);
- DirectoryEntry* oldDirEntry = static_cast<DirectoryEntry*>(oldEntry);
- DirectoryEntry* newDirEntry = addDirectoryInternal(newEntryParent, newFullPath).get();
- Stack<std::tuple<DirectoryEntry*, DirectoryEntry*>> todo;
- todo.push(std::make_tuple(oldDirEntry, newDirEntry));
- while (!todo.empty())
- {
- auto current = todo.top();
- todo.pop();
- DirectoryEntry* sourceDir = std::get<0>(current);
- DirectoryEntry* destDir = std::get<1>(current);
- for (auto& child : sourceDir->mChildren)
- {
- Path childDestPath = destDir->path;
- childDestPath.append(child->path.getTail());
- if (child->type == LibraryEntryType::File)
- {
- FileEntry* childResEntry = static_cast<FileEntry*>(child.get());
- SPtr<ImportOptions> importOptions;
- if (childResEntry->meta != nullptr)
- importOptions = childResEntry->meta->getImportOptions();
- addResourceInternal(destDir, childDestPath, importOptions, true);
- }
- else // Directory
- {
- DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child.get());
- DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath).get();
- todo.push(std::make_tuple(childSourceDirEntry, childDestDirEntry));
- }
- }
- }
- }
- }
- void ProjectLibrary::deleteEntry(const Path& path)
- {
- Path fullPath = path;
- if (!fullPath.isAbsolute())
- fullPath.makeAbsolute(mResourcesFolder);
- if(FileSystem::exists(fullPath))
- FileSystem::remove(fullPath);
- USPtr<LibraryEntry> entry = findEntry(fullPath);
- if(entry != nullptr)
- {
- if(entry->type == LibraryEntryType::File)
- deleteResourceInternal(static_pointer_cast<FileEntry>(entry));
- else if(entry->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(entry));
- }
- }
- void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport,
- bool synchronous)
- {
- LibraryEntry* entry = findEntry(path).get();
- if (entry != nullptr)
- {
- if (entry->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_cast<FileEntry*>(entry);
- reimportResourceInternal(resEntry, importOptions, forceReimport, synchronous);
- }
- }
- }
- float ProjectLibrary::getImportProgress(const Path& path) const
- {
- LibraryEntry* entry = findEntry(path).get();
- if (entry == nullptr)
- return 0.0f;
- if(entry->type == LibraryEntryType::Directory)
- return 1.0f;
- // Note: Only supporting binary progress reporting for now
- const auto iterFind = mQueuedImports.find(static_cast<FileEntry*>(entry));
- return iterFind != mQueuedImports.end() ? 0.0f : 1.0f;
- }
- void ProjectLibrary::cancelImport()
- {
- for(auto& entry : mQueuedImports)
- entry.second->canceled = true;
- }
- void ProjectLibrary::waitForQueuedImport(FileEntry* fileEntry)
- {
- const auto iterFind = mQueuedImports.find(fileEntry);
- if (iterFind != mQueuedImports.end())
- {
- if (finishQueuedImport(fileEntry, *iterFind->second, true))
- mQueuedImports.erase(iterFind);
- }
- }
- void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
- {
- LibraryEntry* entry = findEntry(path).get();
- if (entry == nullptr || entry->type == LibraryEntryType::Directory)
- return;
- auto fileEntry = static_cast<FileEntry*>(entry);
- // Any queued imports will overwrite the meta file, so make sure they finish first
- waitForQueuedImport(fileEntry);
- if (fileEntry->meta == nullptr)
- return;
- fileEntry->meta->setIncludeInBuild(include);
- Path metaPath = fileEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- FileEncoder fs(metaPath);
- fs.encode(fileEntry->meta.get());
- }
- void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
- {
- LibraryEntry* entry = findEntry(path).get();
- if (entry == nullptr || entry->type == LibraryEntryType::Directory)
- return;
- auto fileEntry = static_cast<FileEntry*>(entry);
- // Any queued imports will overwrite the meta file, so make sure they finish first
- waitForQueuedImport(fileEntry);
- SPtr<ProjectResourceMeta> resMeta = findResourceMeta(path);
-
- if (resMeta == nullptr)
- return;
- resMeta->mUserData = userData;
- Path metaPath = fileEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- FileEncoder fs(metaPath);
- fs.encode(fileEntry->meta.get());
- }
- Vector<USPtr<ProjectLibrary::FileEntry>> ProjectLibrary::getResourcesForBuild() const
- {
- Vector<USPtr<FileEntry>> output;
- Stack<DirectoryEntry*> todo;
- todo.push(mRootEntry.get());
- while (!todo.empty())
- {
- DirectoryEntry* directory = todo.top();
- todo.pop();
- for (auto& child : directory->mChildren)
- {
- if (child->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_cast<FileEntry*>(child.get());
- if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
- output.push_back(static_pointer_cast<FileEntry>(child));
- }
- else if (child->type == LibraryEntryType::Directory)
- {
- todo.push(static_cast<DirectoryEntry*>(child.get()));
- }
- }
- }
- return output;
- }
- HResource ProjectLibrary::load(const Path& path)
- {
- SPtr<ProjectResourceMeta> meta = findResourceMeta(path);
- if (meta == nullptr)
- return HResource();
- ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
- const UUID& resUUID = meta->getUUID();
- return gResources().loadFromUUID(resUUID, false, loadFlags);
- }
- void ProjectLibrary::createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot,
- DirectoryEntry** newHierarchyLeaf)
- {
- Path parentPath = fullPath;
- DirectoryEntry* newEntryParent = nullptr;
- Stack<Path> parentPaths;
- do
- {
- Path newParentPath = parentPath.getParent();
- if(newParentPath == parentPath)
- break;
- LibraryEntry* newEntryParentLib = findEntry(newParentPath).get();
- if(newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
- break;
- }
- parentPaths.push(newParentPath);
- parentPath = newParentPath;
- } while (true);
- assert(newEntryParent != nullptr); // Must exist
- if(newHierarchyRoot != nullptr)
- *newHierarchyRoot = newEntryParent;
- while(!parentPaths.empty())
- {
- Path curPath = parentPaths.top();
- parentPaths.pop();
- newEntryParent = addDirectoryInternal(newEntryParent, curPath).get();
- }
- if(newHierarchyLeaf != nullptr)
- *newHierarchyLeaf = newEntryParent;
- }
- Path ProjectLibrary::getMetaPath(const Path& path) const
- {
- Path metaPath = path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- return metaPath;
- }
- bool ProjectLibrary::isMeta(const Path& fullPath) const
- {
- return fullPath.getExtension() == ".meta";
- }
- bool ProjectLibrary::isNative(const Path& path) const
- {
- String extension = path.getExtension();
- return extension == ".asset" || extension == ".prefab";
- }
- void ProjectLibrary::unloadLibrary()
- {
- if (!mIsLoaded)
- return;
- _finishQueuedImports(true);
- mProjectFolder = Path::BLANK;
- mResourcesFolder = Path::BLANK;
- clearEntries();
- mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
- mDependencies.clear();
- gResources().unregisterResourceManifest(mResourceManifest);
- mResourceManifest = nullptr;
- mIsLoaded = false;
- }
- void ProjectLibrary::makeEntriesRelative()
- {
- // Make all paths relative before saving
- std::function<void(LibraryEntry*, const Path&)> makeRelative =
- [&](LibraryEntry* entry, const Path& root)
- {
- entry->path.makeRelative(root);
- if (entry->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
- for (auto& child : dirEntry->mChildren)
- makeRelative(child.get(), root);
- }
- };
- Path root = getResourcesFolder();
- makeRelative(mRootEntry.get(), root);
- }
- void ProjectLibrary::makeEntriesAbsolute()
- {
- std::function<void(LibraryEntry*, const Path&)> makeAbsolute =
- [&](LibraryEntry* entry, const Path& root)
- {
- entry->path.makeAbsolute(root);
- if (entry->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
- for (auto& child : dirEntry->mChildren)
- makeAbsolute(child.get(), root);
- }
- };
- Path root = getResourcesFolder();
- makeAbsolute(mRootEntry.get(), root);
- }
- void ProjectLibrary::saveLibrary()
- {
- if (!mIsLoaded)
- return;
- // Make all paths relative before saving
- makeEntriesRelative();
- SPtr<ProjectLibraryEntries> libEntries = ProjectLibraryEntries::create(mRootEntry);
- Path libraryEntriesPath = mProjectFolder;
- libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
- libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
- FileEncoder fs(libraryEntriesPath);
- fs.encode(libEntries.get());
- // Restore absolute entry paths
- makeEntriesAbsolute();
- Path resourceManifestPath = mProjectFolder;
- resourceManifestPath.append(PROJECT_INTERNAL_DIR);
- resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
- ResourceManifest::save(mResourceManifest, resourceManifestPath, mProjectFolder);
- }
- void ProjectLibrary::loadLibrary()
- {
- unloadLibrary();
- mProjectFolder = gEditorApplication().getProjectPath();
- mResourcesFolder = mProjectFolder;
- mResourcesFolder.append(RESOURCES_DIR);
- mRootEntry = bs_ushared_ptr_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
- Path libraryEntriesPath = mProjectFolder;
- libraryEntriesPath.append(PROJECT_INTERNAL_DIR);
- libraryEntriesPath.append(LIBRARY_ENTRIES_FILENAME);
- if(FileSystem::exists(libraryEntriesPath))
- {
- FileDecoder fs(libraryEntriesPath);
- SPtr<ProjectLibraryEntries> libEntries = std::static_pointer_cast<ProjectLibraryEntries>(fs.decode());
- mRootEntry = libEntries->getRootEntry();
- mRootEntry->parent = nullptr;
- }
- // Entries are stored relative to project folder, but we want their absolute paths now
- makeEntriesAbsolute();
- // Load resource manifest
- Path resourceManifestPath = mProjectFolder;
- resourceManifestPath.append(PROJECT_INTERNAL_DIR);
- resourceManifestPath.append(RESOURCE_MANIFEST_FILENAME);
- if (FileSystem::exists(resourceManifestPath))
- mResourceManifest = ResourceManifest::load(resourceManifestPath, mProjectFolder);
- else
- mResourceManifest = ResourceManifest::create("ProjectLibrary");
- gResources().registerResourceManifest(mResourceManifest);
- // Load all meta files
- Stack<DirectoryEntry*> todo;
- todo.push(mRootEntry.get());
- Vector<USPtr<LibraryEntry>> deletedEntries;
- while(!todo.empty())
- {
- DirectoryEntry* curDir = todo.top();
- todo.pop();
- for(auto& child : curDir->mChildren)
- {
- if(child->type == LibraryEntryType::File)
- {
- USPtr<FileEntry> resEntry = static_pointer_cast<FileEntry>(child);
-
- if (FileSystem::isFile(resEntry->path))
- {
- if (resEntry->meta == nullptr)
- {
- Path metaPath = resEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- if (FileSystem::isFile(metaPath))
- {
- FileDecoder fs(metaPath);
- SPtr<IReflectable> loadedMeta = fs.decode();
- if (loadedMeta != nullptr && loadedMeta->isDerivedFrom(ProjectFileMeta::getRTTIStatic()))
- {
- SPtr<ProjectFileMeta> fileMeta = std::static_pointer_cast<ProjectFileMeta>(loadedMeta);
- resEntry->meta = fileMeta;
- }
- }
- }
- if (resEntry->meta != nullptr)
- {
- auto& resourceMetas = resEntry->meta->getResourceMetaData();
- if (!resourceMetas.empty())
- {
- mUUIDToPath[resourceMetas[0]->getUUID()] = resEntry->path;
- for (UINT32 i = 1; i < (UINT32)resourceMetas.size(); i++)
- {
- SPtr<ProjectResourceMeta> entry = resourceMetas[i];
- mUUIDToPath[entry->getUUID()] = resEntry->path + entry->getUniqueName();
- }
- }
- }
- addDependencies(resEntry.get());
- }
- else
- deletedEntries.push_back(resEntry);
- }
- else if(child->type == LibraryEntryType::Directory)
- {
- if (FileSystem::isDirectory(child->path))
- todo.push(static_cast<DirectoryEntry*>(child.get()));
- else
- deletedEntries.push_back(child);
- }
- }
- }
- // Remove entries that no longer have corresponding files
- for (auto& deletedEntry : deletedEntries)
- {
- if (deletedEntry->type == LibraryEntryType::File)
- deleteResourceInternal(static_pointer_cast<FileEntry>(deletedEntry));
- else
- deleteDirectoryInternal(static_pointer_cast<DirectoryEntry>(deletedEntry));
- }
- // Clean up internal library folder from obsolete files
- Path internalResourcesFolder = mProjectFolder;
- internalResourcesFolder.append(INTERNAL_RESOURCES_DIR);
- if (FileSystem::exists(internalResourcesFolder))
- {
- Vector<Path> toDelete;
- auto processFile = [&](const Path& file)
- {
- UUID uuid = UUID(file.getFilename(false));
- if (mUUIDToPath.find(uuid) == mUUIDToPath.end())
- {
- mResourceManifest->unregisterResource(uuid);
- toDelete.push_back(file);
- }
- return true;
- };
- FileSystem::iterate(internalResourcesFolder, processFile);
- for (auto& entry : toDelete)
- FileSystem::remove(entry);
- }
- mIsLoaded = true;
- }
- void ProjectLibrary::clearEntries()
- {
- if (mRootEntry == nullptr)
- return;
- std::function<void(LibraryEntry*)> invalidateRecursive =
- [&](LibraryEntry* entry)
- {
- if (entry->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
- for (auto& child : dirEntry->mChildren)
- invalidateRecursive(child.get());
- *dirEntry = DirectoryEntry();
- }
- else
- {
- FileEntry* fileEntry = static_cast<FileEntry*>(entry);
- *fileEntry = FileEntry();
- }
- };
- assert(mQueuedImports.empty());
- invalidateRecursive(mRootEntry.get());
- mRootEntry = nullptr;
- }
- Vector<Path> ProjectLibrary::getImportDependencies(const FileEntry* entry)
- {
- Vector<Path> output;
- if (entry->meta == nullptr)
- return output;
- auto& resourceMetas = entry->meta->getResourceMetaData();
- for(auto& resMeta : resourceMetas)
- {
- if (resMeta->getTypeID() == TID_Shader)
- {
- SPtr<ShaderMetaData> metaData = std::static_pointer_cast<ShaderMetaData>(resMeta->getResourceMetaData());
- for (auto& include : metaData->includes)
- output.push_back(include);
- }
- }
- return output;
- }
- void ProjectLibrary::addDependencies(const FileEntry* entry)
- {
- Vector<Path> dependencies = getImportDependencies(entry);
- for (auto& dependency : dependencies)
- mDependencies[dependency].push_back(entry->path);
- }
- void ProjectLibrary::removeDependencies(const FileEntry* entry)
- {
- Vector<Path> dependencies = getImportDependencies(entry);
- for (auto& dependency : dependencies)
- {
- Vector<Path>& curDependencies = mDependencies[dependency];
- auto iterRemove = std::remove_if(curDependencies.begin(), curDependencies.end(),
- [&](const Path& x)
- {
- return x == entry->path;
- });
- curDependencies.erase(iterRemove, curDependencies.end());
- }
- }
- void ProjectLibrary::reimportDependants(const Path& entryPath)
- {
- auto iterFind = mDependencies.find(entryPath);
- if (iterFind == mDependencies.end())
- return;
- // Make a copy since we might modify this list during reimport
- Vector<Path> dependencies = iterFind->second;
- for (auto& dependency : dependencies)
- {
- LibraryEntry* entry = findEntry(dependency).get();
- if (entry != nullptr && entry->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_cast<FileEntry*>(entry);
- SPtr<ImportOptions> importOptions;
- if (resEntry->meta != nullptr)
- importOptions = resEntry->meta->getImportOptions();
- reimportResourceInternal(resEntry, importOptions, true);
- }
- }
- }
- BS_ED_EXPORT ProjectLibrary& gProjectLibrary()
- {
- return ProjectLibrary::instance();
- }
- }
|