| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Poco/MD5Engine.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Math/Random.h>
- #include <Atomic/Resource/ResourceEvents.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include "../Import/ImportConfig.h"
- #include "../ToolEvents.h"
- #include "../ToolSystem.h"
- #include "../Project/Project.h"
- #include "../Project/ProjectEvents.h"
- #include "AssetEvents.h"
- #include "AssetDatabase.h"
- namespace ToolCore
- {
- AssetDatabase::AssetDatabase(Context* context) : Object(context),
- assetScanDepth_(0),
- assetScanImport_(false),
- cacheEnabled_(true)
- {
- SubscribeToEvent(E_LOADFAILED, ATOMIC_HANDLER(AssetDatabase, HandleResourceLoadFailed));
- SubscribeToEvent(E_PROJECTLOADED, ATOMIC_HANDLER(AssetDatabase, HandleProjectLoaded));
- SubscribeToEvent(E_PROJECTUNLOADED, ATOMIC_HANDLER(AssetDatabase, HandleProjectUnloaded));
- }
- AssetDatabase::~AssetDatabase()
- {
- }
- String AssetDatabase::GetCachePath()
- {
- if (project_.Null())
- return String::EMPTY;
- return project_->GetProjectPath() + "Cache/";
- }
- String AssetDatabase::GenerateAssetGUID()
- {
- Time* time = GetSubsystem<Time>();
- while (true)
- {
- Poco::MD5Engine md5;
- PODVector<unsigned> data;
- for (unsigned i = 0; i < 16; i++)
- {
- data.Push(time->GetTimeSinceEpoch() + Rand());
- }
- md5.update(&data[0], data.Size() * sizeof(unsigned));
- String guid = Poco::MD5Engine::digestToHex(md5.digest()).c_str();
- if (!usedGUID_.Contains(guid))
- {
- RegisterGUID(guid);
- return guid;
- }
- }
- assert(0);
- return "";
- }
- void AssetDatabase::RegisterGUID(const String& guid)
- {
- if (usedGUID_.Contains(guid))
- {
- assert(0);
- }
- usedGUID_.Push(guid);
- }
- void AssetDatabase::ReadImportConfig()
- {
- ImportConfig::Clear();
- ToolSystem* tsystem = GetSubsystem<ToolSystem>();
- Project* project = tsystem->GetProject();
- String projectPath = project->GetProjectPath();
- String filename = projectPath + "Settings/Import.json";
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if (!fileSystem->FileExists(filename))
- return;
- ImportConfig::LoadFromFile(context_, filename);
- }
- void AssetDatabase::Import(const String& path)
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- // nothing for now
- if (fs->DirExists(path))
- return;
- }
- Asset* AssetDatabase::GetAssetByCachePath(const String& cachePath)
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- // This is the GUID
- String cacheFilename = GetFileName(cachePath).ToLower();
- while (itr != assets_.End())
- {
- if ((*itr)->GetGUID().ToLower() == cacheFilename)
- return *itr;
- itr++;
- }
- return 0;
- }
- Asset* AssetDatabase::GetAssetByGUID(const String& guid)
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- if (guid == (*itr)->GetGUID())
- return *itr;
- itr++;
- }
- return 0;
- }
- Asset* AssetDatabase::GetAssetByPath(const String& path)
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- if (path == (*itr)->GetPath())
- return *itr;
- itr++;
- }
- return 0;
- }
- void AssetDatabase::PruneOrphanedDotAssetFiles()
- {
- if (project_.Null())
- {
- ATOMIC_LOGDEBUG("AssetDatabase::PruneOrphanedDotAssetFiles - called without project loaded");
- return;
- }
- FileSystem* fs = GetSubsystem<FileSystem>();
- const String& resourcePath = project_->GetResourcePath();
- Vector<String> allResults;
- fs->ScanDir(allResults, resourcePath, "*.asset", SCAN_FILES, true);
- for (unsigned i = 0; i < allResults.Size(); i++)
- {
- String dotAssetFilename = resourcePath + allResults[i];
- String assetFilename = ReplaceExtension(dotAssetFilename, "");
- // remove orphaned asset files
- if (!fs->FileExists(assetFilename) && !fs->DirExists(assetFilename))
- {
- ATOMIC_LOGINFOF("Removing orphaned asset file: %s", dotAssetFilename.CString());
- fs->Delete(dotAssetFilename);
- }
- }
- }
- String AssetDatabase::GetDotAssetFilename(const String& path)
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- String assetFilename = path + ".asset";
- if (fs->DirExists(path)) {
- assetFilename = RemoveTrailingSlash(path) + ".asset";
- }
- return assetFilename;
- }
- void AssetDatabase::AddAsset(SharedPtr<Asset>& asset, bool newAsset)
- {
- assert(asset->GetGUID().Length());
- assert(!GetAssetByGUID(asset->GetGUID()));
- assets_.Push(asset);
- // set to the current timestamp
- asset->UpdateFileTimestamp();
- VariantMap eventData;
- if (newAsset)
- {
- eventData[AssetNew::P_GUID] = asset->GetGUID();
- SendEvent(E_ASSETNEW, eventData);
- }
- eventData[ResourceAdded::P_GUID] = asset->GetGUID();
- SendEvent(E_RESOURCEADDED, eventData);
- }
- void AssetDatabase::DeleteAsset(Asset* asset)
- {
- SharedPtr<Asset> assetPtr(asset);
- List<SharedPtr<Asset>>::Iterator itr = assets_.Find(assetPtr);
- if (itr == assets_.End())
- return;
- assets_.Erase(itr);
- const String& resourcePath = asset->GetPath();
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (fs->DirExists(resourcePath))
- {
- fs->RemoveDir(resourcePath, true);
- }
- else if (fs->FileExists(resourcePath))
- {
- fs->Delete(resourcePath);
- }
- String dotAsset = resourcePath + ".asset";
- if (fs->FileExists(dotAsset))
- {
- fs->Delete(dotAsset);
- }
- VariantMap eventData;
- eventData[ResourceRemoved::P_GUID] = asset->GetGUID();
- SendEvent(E_RESOURCEREMOVED, eventData);
- }
- bool AssetDatabase::ImportDirtyAssets()
- {
- PODVector<Asset*> assets;
- GetDirtyAssets(assets);
- for (unsigned i = 0; i < assets.Size(); i++)
- {
- assetScanImport_ = true;
- assets[i]->Import();
- assets[i]->Save();
- assets[i]->dirty_ = false;
- assets[i]->UpdateFileTimestamp();
- }
- return assets.Size() != 0;
- }
- void AssetDatabase::PreloadAssets()
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- (*itr)->Preload();
- itr++;
- }
- }
- void AssetDatabase::UpdateAssetCacheMap()
- {
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if (project_.Null())
- return;
- bool gen = assetScanImport_;
- assetScanImport_ = false;
- String cachepath = project_->GetProjectPath() + "Cache/__atomic_ResourceCacheMap.json";
- if (!gen && !fileSystem->FileExists(cachepath))
- gen = true;
- if (!gen)
- return;
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- HashMap<String, String> assetMap;
- JSONValue jAssetMap;
- while (itr != assets_.End())
- {
- assetMap.Clear();
- (*itr)->GetAssetCacheMap(assetMap);
- HashMap<String, String>::ConstIterator amitr = assetMap.Begin();
- while (amitr != assetMap.End())
- {
- jAssetMap.Set(amitr->first_, amitr->second_);
- amitr++;
- }
- itr++;
- }
- SharedPtr<File> file(new File(context_, cachepath, FILE_WRITE));
- if (!file->IsOpen())
- {
- ATOMIC_LOGERRORF("Unable to update ResourceCacheMap: %s", cachepath.CString());
- return;
- }
- SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
- jsonFile->GetRoot().Set("assetMap", jAssetMap);
- jsonFile->Save(*file);
- }
- void AssetDatabase::Scan()
- {
- if (!assetScanDepth_)
- {
- assert(!assetScanImport_);
- SendEvent(E_ASSETSCANBEGIN);
- }
- assetScanDepth_++;
- PruneOrphanedDotAssetFiles();
- FileSystem* fs = GetSubsystem<FileSystem>();
- const String& resourcePath = project_->GetResourcePath();
- Vector<String> allResults;
- fs->ScanDir(allResults, resourcePath, "", SCAN_FILES | SCAN_DIRS, true);
- Vector<String> filteredResults;
- filteredResults.Push(RemoveTrailingSlash(resourcePath));
- for (unsigned i = 0; i < allResults.Size(); i++)
- {
- allResults[i] = resourcePath + allResults[i];
- const String& path = allResults[i];
- if (path.StartsWith(".") || path.EndsWith("."))
- continue;
- String ext = GetExtension(path);
- if (ext == ".asset")
- continue;
- filteredResults.Push(path);
- }
- for (unsigned i = 0; i < filteredResults.Size(); i++)
- {
- const String& path = filteredResults[i];
- String dotAssetFilename = GetDotAssetFilename(path);
- if (!fs->FileExists(dotAssetFilename))
- {
- // new asset
- SharedPtr<Asset> asset(new Asset(context_));
- if (asset->SetPath(path))
- {
- AddAsset(asset, true);
- }
- }
- else
- {
- SharedPtr<File> file(new File(context_, dotAssetFilename));
- SharedPtr<JSONFile> json(new JSONFile(context_));
- json->Load(*file);
- file->Close();
- JSONValue& root = json->GetRoot();
- assert(root.Get("version").GetInt() == ASSET_VERSION);
- String guid = root.Get("guid").GetString();
- if (!GetAssetByGUID(guid))
- {
- SharedPtr<Asset> asset(new Asset(context_));
- asset->SetPath(path);
- AddAsset(asset);
- }
- }
- }
- PreloadAssets();
- if (ImportDirtyAssets())
- Scan();
- assetScanDepth_--;
- if (!assetScanDepth_)
- {
- UpdateAssetCacheMap();
- SendEvent(E_ASSETSCANEND);
- }
- }
- void AssetDatabase::GetFolderAssets(String folder, PODVector<Asset*>& assets) const
- {
- if (project_.Null())
- return;
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- if (!folder.Length())
- {
- folder = project_->GetResourcePath();
- }
- folder = AddTrailingSlash(folder);
- while (itr != assets_.End())
- {
- String path = GetPath((*itr)->GetPath());
- if (path == folder)
- assets.Push(*itr);
- itr++;
- }
- }
- void AssetDatabase::GetAssetsByImporterType(StringHash type, const String &resourceType, PODVector<Asset*>& assets) const
- {
- assets.Clear();
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- Asset* asset = *itr;
- if (asset->GetImporterType() == type)
- assets.Push(asset);
- itr++;
- }
- }
- void AssetDatabase::GetDirtyAssets(PODVector<Asset*>& assets)
- {
- assets.Clear();
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- if ((*itr)->IsDirty())
- assets.Push(*itr);
- itr++;
- }
- }
- void AssetDatabase::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
- {
- project_ = GetSubsystem<ToolSystem>()->GetProject();
- ReadImportConfig();
- if (cacheEnabled_)
- {
- InitCache();
- }
- SubscribeToEvent(E_FILECHANGED, ATOMIC_HANDLER(AssetDatabase, HandleFileChanged));
- }
- void AssetDatabase::HandleProjectUnloaded(StringHash eventType, VariantMap& eventData)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- cache->RemoveResourceDir(GetCachePath());
- assets_.Clear();
- usedGUID_.Clear();
- assetImportErrorTimes_.Clear();
- project_ = 0;
- UnsubscribeFromEvent(E_FILECHANGED);
- }
- void AssetDatabase::HandleResourceLoadFailed(StringHash eventType, VariantMap& eventData)
- {
- if (project_.Null())
- return;
- String path = eventData[LoadFailed::P_RESOURCENAME].GetString();
- Asset* asset = GetAssetByPath(path);
- if (!asset)
- asset = GetAssetByPath(project_->GetResourcePath() + path);
- if (!asset)
- return;
- Time* time = GetSubsystem<Time>();
- unsigned ctime = time->GetSystemTime();
- // if less than 5 seconds since last report, stifle report
- if (assetImportErrorTimes_.Contains(asset->guid_))
- if (ctime - assetImportErrorTimes_[asset->guid_] < 5000)
- return;
- assetImportErrorTimes_[asset->guid_] = ctime;
- VariantMap evData;
- evData[AssetImportError::P_PATH] = asset->path_;
- evData[AssetImportError::P_GUID] = asset->guid_;
- evData[AssetImportError::P_ERROR] = ToString("Asset %s Failed to Load", asset->path_.CString());
- SendEvent(E_ASSETIMPORTERROR, evData);
- }
- void AssetDatabase::HandleFileChanged(StringHash eventType, VariantMap& eventData)
- {
- using namespace FileChanged;
- const String& fullPath = eventData[P_FILENAME].GetString();
- FileSystem* fs = GetSubsystem<FileSystem>();
- String pathName, fileName, ext;
- SplitPath(fullPath, pathName, fileName, ext);
- // ignore changes in the Cache resource dir
- if (fullPath == GetCachePath() || pathName.StartsWith(GetCachePath()))
- return;
- // don't care about directories and asset file changes
- if (fs->DirExists(fullPath) || ext == ".asset")
- return;
- Asset* asset = GetAssetByPath(fullPath);
- if (!asset && fs->FileExists(fullPath))
- {
- Scan();
- return;
- }
- if (asset)
- {
- if(!fs->Exists(fullPath))
- {
- DeleteAsset(asset);
- }
- else
- {
- if (asset->GetFileTimestamp() != fs->GetLastModifiedTime(asset->GetPath()))
- {
- asset->SetDirty(true);
- Scan();
- }
- }
- }
- }
- String AssetDatabase::GetResourceImporterName(const String& resourceTypeName)
- {
- // TODO: have resource type register themselves
- if (resourceTypeToImporterType_.Empty())
- {
- resourceTypeToImporterType_["Sound"] = "AudioImporter";
- resourceTypeToImporterType_["Model"] = "ModelImporter";
- resourceTypeToImporterType_["Material"] = "MaterialImporter";
- resourceTypeToImporterType_["Texture2D"] = "TextureImporter";
- resourceTypeToImporterType_["Sprite2D"] = "TextureImporter";
- resourceTypeToImporterType_["Image"] = "TextureImporter";
- resourceTypeToImporterType_["AnimatedSprite2D"] = "SpriterImporter";
- resourceTypeToImporterType_["JSComponentFile"] = "JavascriptImporter";
- resourceTypeToImporterType_["JSONFile"] = "JSONImporter";
- resourceTypeToImporterType_["ParticleEffect2D"] = "PEXImporter";
- resourceTypeToImporterType_["ParticleEffect"] = "ParticleEffectImporter";
- resourceTypeToImporterType_["Animation"] = "ModelImporter";
- resourceTypeToImporterType_["CSComponentAssembly"] = "NETAssemblyImporter";
- resourceTypeToImporterType_["TmxFile2D"] = "TMXImporter";
- }
- if (!resourceTypeToImporterType_.Contains(resourceTypeName))
- return String::EMPTY;
- return resourceTypeToImporterType_[resourceTypeName];
- }
- void AssetDatabase::ReimportAllAssets()
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- (*itr)->SetDirty(true);
- itr++;
- }
- Scan();
- }
- void AssetDatabase::ReimportAllAssetsInDirectory(const String& directoryPath)
- {
- List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
- while (itr != assets_.End())
- {
- if ((*itr)->GetPath().StartsWith(directoryPath))
- {
- (*itr)->SetDirty(true);
- }
- itr++;
- }
- Scan();
- }
- bool AssetDatabase::InitCache()
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (!fs->DirExists(GetCachePath()))
- fs->CreateDir(GetCachePath());
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- cache->AddResourceDir(GetCachePath());
- Scan();
- return true;
- }
- bool AssetDatabase::CleanCache()
- {
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- String cachePath = GetCachePath();
- if (fileSystem->DirExists(cachePath))
- {
- ATOMIC_LOGINFOF("Cleaning cache directory %s", cachePath.CString());
- fileSystem->RemoveDir(cachePath, true);
- if (fileSystem->DirExists(cachePath))
- {
- ATOMIC_LOGERRORF("Unable to remove cache directory %s", cachePath.CString());
- return false;
- }
- }
- fileSystem->CreateDir(cachePath);
- if (!fileSystem->DirExists(cachePath))
- {
- ATOMIC_LOGERRORF("Unable to create cache directory %s", cachePath.CString());
- return false;
- }
- return true;
- }
- bool AssetDatabase::GenerateCache(bool clean)
- {
- ATOMIC_LOGINFO("Generating cache... hold on");
- if (clean)
- {
- if (!CleanCache())
- return false;
- }
- else
- {
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- String cachePath = GetCachePath();
- if (!fileSystem->DirExists(cachePath))
- {
- fileSystem->CreateDir(cachePath);
- }
- }
- ReimportAllAssets();
- ATOMIC_LOGINFO("Cache generated");
- return true;
- }
- void AssetDatabase::SetCacheEnabled(bool cacheEnabled)
- {
- cacheEnabled_ = cacheEnabled;
- }
- }
|