| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741 |
- //********************************** 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>
- 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 ProjectLibrary::RESOURCES_DIR = "Resources/";
- const Path ProjectLibrary::INTERNAL_RESOURCES_DIR = PROJECT_INTERNAL_DIR + GAME_RESOURCES_FOLDER_NAME;
- const char* ProjectLibrary::LIBRARY_ENTRIES_FILENAME = "ProjectLibrary.asset";
- const char* ProjectLibrary::RESOURCE_MANIFEST_FILENAME = "ResourceManifest.asset";
- ProjectLibrary::LibraryEntry::LibraryEntry()
- :type(LibraryEntryType::Directory), parent(nullptr)
- { }
- ProjectLibrary::LibraryEntry::LibraryEntry(const Path& path, const String& name, DirectoryEntry* parent, LibraryEntryType type)
- :type(type), path(path), elementName(name), parent(parent)
- { }
- ProjectLibrary::FileEntry::FileEntry()
- : lastUpdateTime(0)
- { }
- ProjectLibrary::FileEntry::FileEntry(const Path& path, const String& name, DirectoryEntry* parent)
- : LibraryEntry(path, name, parent, LibraryEntryType::File), lastUpdateTime(0)
- { }
- ProjectLibrary::DirectoryEntry::DirectoryEntry()
- { }
- 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_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_new<DirectoryEntry>(mResourcesFolder, mResourcesFolder.getTail(), nullptr);
- }
- Path pathToSearch = fullPath;
- 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);
- 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);
- if(reimportResourceInternal(resEntry))
- resourcesToImport++;
- }
- else
- deleteResourceInternal(static_cast<FileEntry*>(entry));
- }
- else if(entry->type == LibraryEntryType::Directory) // Check folder and all subfolders for modifications
- {
- if(!FileSystem::isDirectory(entry->path))
- {
- deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
- }
- else
- {
- Stack<DirectoryEntry*> todo;
- todo.push(static_cast<DirectoryEntry*>(entry));
- Vector<Path> childFiles;
- Vector<Path> childDirectories;
- Vector<bool> existingEntries;
- Vector<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);
- 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);
- 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_cast<DirectoryEntry*>(child));
- else if(child->type == LibraryEntryType::File)
- deleteResourceInternal(static_cast<FileEntry*>(child));
- }
- toDelete.clear();
- }
- for(auto& child : currentDir->mChildren)
- {
- if(child->type == LibraryEntryType::Directory)
- todo.push(static_cast<DirectoryEntry*>(child));
- }
- }
- }
- }
- return resourcesToImport;
- }
- ProjectLibrary::FileEntry* ProjectLibrary::addResourceInternal(DirectoryEntry* parent, const Path& filePath,
- const SPtr<ImportOptions>& importOptions, bool forceReimport)
- {
- FileEntry* newResource = bs_new<FileEntry>(filePath, filePath.getTail(), parent);
- parent->mChildren.push_back(newResource);
- reimportResourceInternal(newResource, importOptions, forceReimport);
- onEntryAdded(newResource->path);
- return newResource;
- }
- ProjectLibrary::DirectoryEntry* ProjectLibrary::addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath)
- {
- DirectoryEntry* newEntry = bs_new<DirectoryEntry>(dirPath, dirPath.getTail(), parent);
- parent->mChildren.push_back(newEntry);
- onEntryAdded(newEntry->path);
- return newEntry;
- }
- void ProjectLibrary::deleteResourceInternal(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 LibraryEntry* entry) { return entry == resource; });
- parent->mChildren.erase(findIter);
- Path originalPath = resource->path;
- onEntryRemoved(originalPath);
- mQueuedImports.erase(resource);
- removeDependencies(resource);
- bs_delete(resource);
- reimportDependants(originalPath);
- }
- void ProjectLibrary::deleteDirectoryInternal(DirectoryEntry* directory)
- {
- if(directory == mRootEntry)
- mRootEntry = nullptr;
- Vector<LibraryEntry*> childrenToDestroy = directory->mChildren;
- for(auto& child : childrenToDestroy)
- {
- if(child->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_cast<DirectoryEntry*>(child));
- else
- deleteResourceInternal(static_cast<FileEntry*>(child));
- }
- DirectoryEntry* parent = directory->parent;
- if(parent != nullptr)
- {
- auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
- [&] (const LibraryEntry* entry) { return entry == directory; });
- parent->mChildren.erase(findIter);
- }
- onEntryRemoved(directory->path);
- bs_delete(directory);
- }
- bool ProjectLibrary::reimportResourceInternal(FileEntry* fileEntry, const SPtr<ImportOptions>& importOptions,
- bool forceReimport, bool pruneResourceMetas)
- {
- 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;
- QueuedImport queuedImport;
- queuedImport.filePath = fileEntry->path;
- queuedImport.importOptions = curImportOptions;
- queuedImport.pruneMetas = pruneResourceMetas;
- if(!isNativeResource)
- {
- queuedImport.importOp = gImporter().importAllAsync(fileEntry->path, curImportOptions, false);
- }
- 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);
- }
- // 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.
- HResource resource = gResources().load(fileEntry->path, ResourceLoadFlag::KeepSourceData);
- queuedImport.resources.push_back({ "primary", resource.getInternalPtr() });
- }
- mQueuedImports[fileEntry] = queuedImport;
- fileEntry->lastUpdateTime = std::time(nullptr);
- // TODO - Copy native resource directly
- if (!newResources.empty())
- {
- Path internalResourcesPath = mProjectFolder;
- internalResourcesPath.append(INTERNAL_RESOURCES_DIR);
- if (!FileSystem::isDirectory(internalResourcesPath))
- FileSystem::createDir(internalResourcesPath);
- for (auto& entry : newResources)
- {
- String uuidStr = entry.getUUID().toString();
- internalResourcesPath.setFilename(uuidStr + ".asset");
- gResources().save(entry, internalResourcesPath, true);
- const UUID& uuid = entry.getUUID();
- // TODO - Not thread safe
- mResourceManifest->registerResource(uuid, internalResourcesPath);
- }
- }
- return true;
- }
- return false;
- }
- void ProjectLibrary::_finishQueuedImports(bool wait)
- {
- for(auto iter = mQueuedImports.begin(); iter != mQueuedImports.end();)
- {
- QueuedImport& queuedImport = iter->second;
- if(queuedImport.importOp != nullptr)
- {
- if(!queuedImport.importOp.hasCompleted())
- {
- if(wait)
- queuedImport.importOp.blockUntilComplete();
- else
- {
- ++iter;
- continue;
- }
- }
- Vector<SubResourceRaw> subresources = queuedImport.importOp.getReturnValue<Vector<SubResourceRaw>>();
- for(auto& entry : subresources)
- queuedImport.resources.push_back({entry.name, entry.value});
- }
- FileEntry* fileEntry = iter->first;
- Path metaPath = fileEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- Vector<HResource> newResources;
- if(fileEntry->meta == nullptr)
- {
- // Generate new resource handles and build the meta-file
- fileEntry->meta = ProjectFileMeta::create(queuedImport.importOptions);
- for(auto& entry : queuedImport.resources)
- {
- HResource handle = gResources()._createResourceHandle(entry.resource);
- newResources.push_back(handle);
- SPtr<ResourceMetaData> subMeta = handle->getMetaData();
- const UINT32 typeId = handle->getTypeId();
- const UUID& UUID = handle.getUUID();
- Path::stripInvalid(entry.name);
- const ProjectResourceIcons icons = generatePreviewIcons(*handle);
- const SPtr<ProjectResourceMeta> resMeta =
- ProjectResourceMeta::create(entry.name, UUID, typeId, icons, subMeta);
- fileEntry->meta->add(resMeta);
- }
- if(!queuedImport.resources.empty())
- {
- HResource primary = newResources[0];
- mUUIDToPath[primary.getUUID()] = fileEntry->path;
- for (UINT32 i = 1; i < (UINT32)queuedImport.resources.size(); i++)
- {
- const QueuedImportResource& entry = queuedImport.resources[i];
- const UUID& UUID = newResources[i].getUUID();
- mUUIDToPath[UUID] = fileEntry->path + entry.name;
- }
- }
- FileEncoder fs(metaPath);
- fs.encode(fileEntry->meta.get());
- }
- else
- {
- removeDependencies(fileEntry);
- Vector<SPtr<ProjectResourceMeta>> existingResourceMetas = fileEntry->meta->getAllResourceMetaData();
- fileEntry->meta->clearResourceMetaData();
- for (auto& entry : queuedImport.resources)
- {
- Path::stripInvalid(entry.name);
- const ProjectResourceIcons icons = generatePreviewIcons(*entry.resource);
- bool foundMeta = false;
- for (auto iterMeta = existingResourceMetas.begin(); iterMeta != existingResourceMetas.end(); ++iterMeta)
- {
- const SPtr<ProjectResourceMeta>& metaEntry = *iterMeta;
- if (entry.name == metaEntry->getUniqueName())
- {
- HResource importedResource = gResources()._getResourceHandle(metaEntry->getUUID());
- gResources().update(importedResource, entry.resource);
- newResources.push_back(importedResource);
- metaEntry->setPreviewIcons(icons);
- fileEntry->meta->add(metaEntry);
- iterMeta = existingResourceMetas.erase(iterMeta);
- foundMeta = true;
- break;
- }
- }
- if (!foundMeta)
- {
- HResource importedResource = gResources()._createResourceHandle(entry.resource);
- newResources.push_back(importedResource);
- SPtr<ResourceMetaData> subMeta = entry.resource->getMetaData();
- const UINT32 typeId = entry.resource->getTypeId();
- const UUID& UUID = importedResource.getUUID();
- SPtr<ProjectResourceMeta> resMeta = ProjectResourceMeta::create(entry.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(!queuedImport.pruneMetas)
- {
- for (auto& metaEntry : existingResourceMetas)
- fileEntry->meta->addInactive(metaEntry);
- }
- // Update UUID to path mapping
- 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>& meta = resourceMetas[i];
- mUUIDToPath[meta->getUUID()] = fileEntry->path + meta->getUniqueName();
- }
- }
- }
- fileEntry->meta->mImportOptions = queuedImport.importOptions;
- FileEncoder fs(metaPath);
- fs.encode(fileEntry->meta.get());
- }
- addDependencies(fileEntry);
- onEntryImported(fileEntry->path);
- reimportDependants(fileEntry->path);
- iter = mQueuedImports.erase(iter);
- }
- }
- bool ProjectLibrary::isUpToDate(FileEntry* resource) const
- {
- if(resource->meta == nullptr)
- {
- // Allow no meta if import in progress
- const auto iterFind = mQueuedImports.find(resource);
- if(iterFind == mQueuedImports.end())
- return false;
- }
- 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;
- }
- }
- const std::time_t lastModifiedTime = FileSystem::getLastModifiedTime(resource->path);
- return lastModifiedTime <= resource->lastUpdateTime;
- }
- Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const String& pattern)
- {
- return search(pattern, {});
- }
- Vector<ProjectLibrary::LibraryEntry*> ProjectLibrary::search(const String& pattern, const Vector<UINT32>& typeIds)
- {
- Vector<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);
- while (!todo.empty())
- {
- DirectoryEntry* dirEntry = todo.top();
- todo.pop();
- for (auto& child : dirEntry->mChildren)
- {
- if (std::regex_match(child->elementName, searchRegex))
- {
- if (typeIds.size() == 0)
- foundEntries.push_back(child);
- else
- {
- if (child->type == LibraryEntryType::File)
- {
- FileEntry* childFileEntry = static_cast<FileEntry*>(child);
- 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);
- todo.push(childDirEntry);
- }
- }
- }
- std::sort(foundEntries.begin(), foundEntries.end(),
- [&](const LibraryEntry* a, const LibraryEntry* b)
- {
- return a->elementName.compare(b->elementName) < 0;
- });
- return foundEntries;
- }
- ProjectLibrary::LibraryEntry* ProjectLibrary::findEntry(const Path& path) const
- {
- Path fullPath = path;
- if (fullPath.isAbsolute())
- {
- if (!mResourcesFolder.includes(fullPath))
- return nullptr;
- }
- else
- fullPath.makeAbsolute(mResourcesFolder);
- Path relPath = fullPath.getRelative(mRootEntry->path);
- UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
- UINT32 idx = 0;
- LibraryEntry* current = mRootEntry;
- while (current != nullptr)
- {
- if (idx == numElems)
- return current;
- String curElem;
- if (relPath.isFile() && idx == (numElems - 1))
- curElem = relPath.getFilename();
- else
- curElem = relPath[idx];
- if (current->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(current);
- current = nullptr;
- for (auto& child : dirEntry->mChildren)
- {
- 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);
- if (entry != nullptr && entry->type == LibraryEntryType::File)
- return true;
- return false;
- }
- 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);
- 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);
- if (fileEntry->meta == nullptr)
- return nullptr;
- return fileEntry->meta->getResourceMetaData()[0];
- }
- }
- }
- return nullptr;
- }
- }
- // Not a subresource path, load directly
- {
- LibraryEntry* entry = findEntry(path);
- 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);
- checkForModifications(absPath);
- }
- 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);
- checkForModifications(filePath);
- }
- 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;
- LibraryEntry* newEntryParentLib = findEntry(parentPath);
- if (newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
- }
- 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);
- 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_cast<FileEntry*>(oldEntry));
- else if(oldEntry->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_cast<DirectoryEntry*>(oldEntry));
- }
- else // Just moving internally
- {
- onEntryRemoved(oldEntry->path);
- FileEntry* fileEntry = nullptr;
- if (oldEntry->type == LibraryEntryType::File)
- {
- fileEntry = static_cast<FileEntry*>(oldEntry);
- removeDependencies(fileEntry);
- // Update uuid <-> path mapping
- if(fileEntry->meta != nullptr)
- {
- auto& resourceMetas = fileEntry->meta->getResourceMetaData();
- if (resourceMetas.size() > 0)
- {
- 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;
- LibraryEntry* newEntryParentLib = findEntry(parentPath);
- if(newEntryParentLib != nullptr)
- {
- assert(newEntryParentLib->type == LibraryEntryType::Directory);
- newEntryParent = static_cast<DirectoryEntry*>(newEntryParentLib);
- }
- 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();
- if(oldEntry->type == LibraryEntryType::Directory) // Update child paths
- {
- Stack<LibraryEntry*> todo;
- todo.push(oldEntry);
- 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);
- }
- }
- }
- 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);
- 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);
- 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);
- 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);
- SPtr<ImportOptions> importOptions;
- if (childResEntry->meta != nullptr)
- importOptions = childResEntry->meta->getImportOptions();
- addResourceInternal(destDir, childDestPath, importOptions, true);
- }
- else // Directory
- {
- DirectoryEntry* childSourceDirEntry = static_cast<DirectoryEntry*>(child);
- DirectoryEntry* childDestDirEntry = addDirectoryInternal(destDir, childDestPath);
- 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);
- LibraryEntry* entry = findEntry(fullPath);
- if(entry != nullptr)
- {
- if(entry->type == LibraryEntryType::File)
- deleteResourceInternal(static_cast<FileEntry*>(entry));
- else if(entry->type == LibraryEntryType::Directory)
- deleteDirectoryInternal(static_cast<DirectoryEntry*>(entry));
- }
- }
- void ProjectLibrary::reimport(const Path& path, const SPtr<ImportOptions>& importOptions, bool forceReimport)
- {
- LibraryEntry* entry = findEntry(path);
- if (entry != nullptr)
- {
- if (entry->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_cast<FileEntry*>(entry);
- reimportResourceInternal(resEntry, importOptions, forceReimport);
- }
- }
- }
- void ProjectLibrary::setIncludeInBuild(const Path& path, bool include)
- {
- LibraryEntry* entry = findEntry(path);
- if (entry == nullptr || entry->type == LibraryEntryType::Directory)
- return;
- FileEntry* resEntry = static_cast<FileEntry*>(entry);
- if (resEntry->meta == nullptr)
- return;
- resEntry->meta->setIncludeInBuild(include);
- Path metaPath = resEntry->path;
- metaPath.setFilename(metaPath.getFilename() + ".meta");
- FileEncoder fs(metaPath);
- fs.encode(resEntry->meta.get());
- }
- void ProjectLibrary::setUserData(const Path& path, const SPtr<IReflectable>& userData)
- {
- LibraryEntry* entry = findEntry(path);
- if (entry == nullptr || entry->type == LibraryEntryType::Directory)
- return;
- FileEntry* fileEntry = static_cast<FileEntry*>(entry);
- 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<ProjectLibrary::FileEntry*> ProjectLibrary::getResourcesForBuild() const
- {
- Vector<FileEntry*> output;
- Stack<DirectoryEntry*> todo;
- todo.push(mRootEntry);
- 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);
- if (resEntry->meta != nullptr && resEntry->meta->getIncludeInBuild())
- output.push_back(resEntry);
- }
- else if (child->type == LibraryEntryType::Directory)
- {
- todo.push(static_cast<DirectoryEntry*>(child));
- }
- }
- }
- 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);
- 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);
- }
- 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_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, root);
- }
- };
- Path root = getResourcesFolder();
- makeRelative(mRootEntry, 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, root);
- }
- };
- Path root = getResourcesFolder();
- makeAbsolute(mRootEntry, 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_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();
- for(auto& child : mRootEntry->mChildren)
- child->parent = mRootEntry;
- 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);
- Vector<LibraryEntry*> deletedEntries;
- while(!todo.empty())
- {
- DirectoryEntry* curDir = todo.top();
- todo.pop();
- for(auto& child : curDir->mChildren)
- {
- if(child->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_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.size() > 0)
- {
- 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);
- }
- else
- deletedEntries.push_back(resEntry);
- }
- else if(child->type == LibraryEntryType::Directory)
- {
- if (FileSystem::isDirectory(child->path))
- todo.push(static_cast<DirectoryEntry*>(child));
- else
- deletedEntries.push_back(child);
- }
- }
- }
- // Remove entries that no longer have corresponding files
- for (auto& deletedEntry : deletedEntries)
- {
- if (deletedEntry->type == LibraryEntryType::File)
- {
- FileEntry* resEntry = static_cast<FileEntry*>(deletedEntry);
- deleteResourceInternal(resEntry);
- }
- else
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(deletedEntry);
- deleteDirectoryInternal(dirEntry);
- }
- }
- // 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*)> deleteRecursive =
- [&](LibraryEntry* entry)
- {
- if (entry->type == LibraryEntryType::Directory)
- {
- DirectoryEntry* dirEntry = static_cast<DirectoryEntry*>(entry);
- for (auto& child : dirEntry->mChildren)
- deleteRecursive(child);
- }
- bs_delete(entry);
- };
- assert(mQueuedImports.empty());
- deleteRecursive(mRootEntry);
- 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);
- 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();
- }
- }
|