Fix asset type retrieval in AssetCatalogModel::GetAssetType (#4995)

* Fix asset type retrieval in AssetCatalogModel::GetAssetType

Previous logic would visit the next entry in m_extensionToAssetType map, if the previous entry had multiple types
 was only exiting the inner loop.

The main change is that now the first found matching asset type is returned.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Apply reviewer's suggestions + reduce allocations.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>
This commit is contained in:
Artur K
2021-11-01 15:51:15 +01:00
committed by GitHub
parent db01d8ddda
commit 2dff26ddb5
2 changed files with 32 additions and 34 deletions
@@ -197,48 +197,46 @@ AssetCatalogModel::~AssetCatalogModel()
AzFramework::AssetCatalogEventBus::Handler::BusDisconnect(); AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
} }
AZ::Data::AssetType AssetCatalogModel::GetAssetType(QString filename) const AZ::Data::AssetType AssetCatalogModel::GetAssetType(const QString &filename) const
{ {
AZ::Data::AssetType returnType = AZ::Uuid::CreateNull();
// Compare file extensions with the map created from the asset database. // Compare file extensions with the map created from the asset database.
int dotIndex = filename.lastIndexOf('.'); int dotIndex = filename.lastIndexOf('.');
if (dotIndex >= 0) if (dotIndex < 0)
{ {
QString extension = filename.mid(dotIndex); return AZ::Uuid::CreateNull();
for (auto pair : m_extensionToAssetType) }
{
QString qExtensions = pair.first.c_str();
if (qExtensions.indexOf(extension) >= 0)
{
if (pair.second.size() > 1)
{
// There are multiple types with this extension. Check each handler to see if they can handle this data type.
AZStd::string azFilename = filename.toStdString().c_str();
EBUS_EVENT(AzFramework::ApplicationRequests::Bus, MakePathAssetRootRelative, azFilename);
AZ::Data::AssetId assetId;
EBUS_EVENT_RESULT(assetId, AZ::Data::AssetCatalogRequestBus, GetAssetIdByPath, azFilename.c_str(), AZ::Data::s_invalidAssetType, false);
for (AZ::Uuid type : pair.second) QStringRef extension = filename.midRef(dotIndex);
{ for (const auto& pair : m_extensionToAssetType)
const AZ::Data::AssetHandler* handler = AZ::Data::AssetManager::Instance().GetHandler(type); {
if (handler && handler->CanHandleAsset(assetId)) QString qExtensions = pair.first.c_str();
{ if (qExtensions.indexOf(extension) < 0 || pair.second.empty())
returnType = type; {
break; continue;
} }
} if (pair.second.size() == 1)
} {
else return pair.second[0];
{ }
returnType = pair.second[0];
break; // There are multiple types with this extension. Search for a handler that can handle this data type.
} AZStd::string azFilename = filename.toStdString().c_str();
EBUS_EVENT(AzFramework::ApplicationRequests::Bus, MakePathAssetRootRelative, azFilename);
AZ::Data::AssetId assetId;
EBUS_EVENT_RESULT(assetId, AZ::Data::AssetCatalogRequestBus, GetAssetIdByPath, azFilename.c_str(), AZ::Data::s_invalidAssetType, false);
for (const AZ::Uuid& type : pair.second)
{
const AZ::Data::AssetHandler* handler = AZ::Data::AssetManager::Instance().GetHandler(type);
if (handler && handler->CanHandleAsset(assetId))
{
return type;
} }
} }
} }
return returnType; return AZ::Uuid::CreateNull();
} }
QStandardItem* AssetCatalogModel::GetPath(QString& path, bool createIfNeeded, QStandardItem* parent) QStandardItem* AssetCatalogModel::GetPath(QString& path, bool createIfNeeded, QStandardItem* parent)
@@ -419,7 +417,7 @@ AssetCatalogEntry* AssetCatalogModel::AddAsset(QString assetPath, AZ::Data::Asse
// icons' memory being reclaimed and crashing the Editor. // icons' memory being reclaimed and crashing the Editor.
QSize size = fileIcon.actualSize(QSize(16, 16)); QSize size = fileIcon.actualSize(QSize(16, 16));
QIcon deepCopy = fileIcon.pixmap(size).copy(0, 0, size.width(), size.height()); QIcon deepCopy = fileIcon.pixmap(size).copy(0, 0, size.width(), size.height());
if (!fileIcon.isNull()) if (!fileIcon.isNull())
{ {
m_assetTypeToIcon[assetType] = deepCopy; m_assetTypeToIcon[assetType] = deepCopy;
@@ -110,7 +110,7 @@ protected:
void SetFilterRegExp(const AZStd::string& filterType, const QRegExp& regExp); void SetFilterRegExp(const AZStd::string& filterType, const QRegExp& regExp);
void ClearFilterRegExp(const AZStd::string& filterType = AZStd::string()); void ClearFilterRegExp(const AZStd::string& filterType = AZStd::string());
AZ::Data::AssetType GetAssetType(QString filename) const; AZ::Data::AssetType GetAssetType(const QString &filename) const;
QStandardItem* GetPath(QString& path, bool createIfNeeded, QStandardItem* parent = nullptr); QStandardItem* GetPath(QString& path, bool createIfNeeded, QStandardItem* parent = nullptr);
void ApplyFilter(QStandardItem* parent); void ApplyFilter(QStandardItem* parent);