Vector<AssetPtr> AssetRefListListener::Assets() const { Vector<AssetPtr> assets; for (uint i = 0; i < current_.Size(); ++i) { const AssetReference &ref = current_[i]; if (!ref.ref.Empty()) assets.Push(assetAPI_->FindAsset(ref.ref)); else assets.Push(AssetPtr()); } return assets; }
AssetPtr AssetManager::FindAsset(const boost::filesystem::path& file) { AssetMap::iterator it = m_assets.find(file.string()); if(it != m_assets.end()) { if(it->second->Ready()) { return it->second; } } return AssetPtr(); }
AssetPtr AssetManager::LoadAsset(const boost::filesystem::path& file) { AssetPtr pAsset = FindAsset(file); if(pAsset) { pAsset->IncRef(); return pAsset; } AssetLoader loader = FindLoader(file); if(loader) { return AssetPtr(); } pAsset = loader(file); m_assets[file.string()] = pAsset; return pAsset; }
AssetPtr AssetManager::LoadSound(const boost::filesystem::path& file, bool sound3d) { SoundPtr pSound; if(sound3d) { pSound = m_pSound->Create3DSound(file.string().c_str()); } else { pSound = m_pSound->CreateSound(file.string().c_str()); } if(pSound == nullptr) { return AssetPtr(); } AssetPtr pAsset = alloc_object<SoundAsset, SoundPtr>(pSound); return pAsset; }
AssetPtr IAsset::Clone(QString newAssetName) const { assert(assetAPI); if (!IsLoaded()) return AssetPtr(); AssetPtr existing = assetAPI->GetAsset(newAssetName); if (existing) { LogError("Cannot Clone() asset \"" + Name() + "\" to a new asset \"" + newAssetName + "\": An asset with that name already exists!"); return AssetPtr(); } std::vector<u8> data; bool success = SerializeTo(data); if (!success) { LogError("Cannot Clone() asset \"" + Name() + "\" to a new asset \"" + newAssetName + "\": Serializing the asset failed!"); return AssetPtr(); } if (data.size() == 0) { LogError("Cannot Clone() asset \"" + Name() + "\" to a new asset \"" + newAssetName + "\": Asset serialization succeeded with zero size!"); return AssetPtr(); } AssetPtr newAsset = assetAPI->CreateNewAsset(this->Type(), newAssetName); if (!newAsset) { LogError("Cannot Clone() asset \"" + Name() + "\" to a new asset \"" + newAssetName + "\": AssetAPI::CreateNewAsset failed!"); return AssetPtr(); } // Do not allow asynchronous loading due the caller of this // expects the asset to be usable when this function returns. success = newAsset->LoadFromFileInMemory(&data[0], data.size(), false); if (!success) { LogError("Cannot Clone() asset \"" + Name() + "\" to a new asset \"" + newAssetName + "\": Deserializing the new asset from bytes failed!"); assetAPI->ForgetAsset(newAsset, false); return AssetPtr(); } return newAsset; }
void AssetRefListener::HandleAssetRefChange(AssetAPI *assetApi, String assetRef, const String& assetType) { // Disconnect from any previous transfer we might be listening to if (!currentTransfer.Expired()) { IAssetTransfer* current = currentTransfer.Get(); current->Succeeded.Disconnect(this, &AssetRefListener::OnTransferSucceeded); current->Failed.Disconnect(this, &AssetRefListener::OnTransferFailed); currentTransfer.Reset(); } assert(assetApi); // Store AssetAPI ptr for later signal hooking. if (!myAssetAPI) myAssetAPI = assetApi; // If the ref is empty, don't go any further as it will just trigger the LogWarning below. assetRef = assetRef.Trimmed(); if (assetRef.Empty()) { asset = AssetPtr(); return; } currentWaitingRef = ""; // Resolve the protocol for generated:// assets. These assets are never meant to be // requested from AssetAPI, they cannot be fetched from anywhere. They can only be either // loaded or we must wait for something to load/create them. String protocolPart = ""; assetApi->ParseAssetRef(assetRef, &protocolPart); if (protocolPart.ToLower() == "generated") { AssetPtr loadedAsset = assetApi->FindAsset(assetRef); if (loadedAsset.Get() && loadedAsset->IsLoaded()) { // Asset is loaded, emit Loaded with 1 msec delay to preserve the logic // that HandleAssetRefChange won't emit anything itself as before. // Otherwise existing connection can break/be too late after calling this function. asset = loadedAsset; assetApi->GetFramework()->Frame()->DelayedExecute(0.0f).Connect(this, &AssetRefListener::EmitLoaded); return; } else { // Wait for it to be created. currentWaitingRef = assetRef; myAssetAPI->AssetCreated.Connect(this, &AssetRefListener::OnAssetCreated); } } else { // This is not a generated asset, request normally from asset api. AssetTransferPtr transfer = assetApi->RequestAsset(assetRef, assetType); if (!transfer) { LogWarning("AssetRefListener::HandleAssetRefChange: Asset request for asset \"" + assetRef + "\" failed."); return; } currentWaitingRef = assetRef; transfer->Succeeded.Connect(this, &AssetRefListener::OnTransferSucceeded); transfer->Failed.Connect(this, &AssetRefListener::OnTransferFailed); currentTransfer = transfer; } // Disconnect from the old asset's load signal if (asset) asset->Loaded.Disconnect(this, &AssetRefListener::OnAssetLoaded); asset = AssetPtr(); }
AssetPtr AssetRefListListener::Asset(uint index) const { if (index < current_.Size() && !current_[index].ref.Empty()) return assetAPI_->FindAsset(current_[index].ref); return AssetPtr(); }