Sfoglia il codice sorgente

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 <[email protected]>

* Apply reviewer's suggestions + reduce allocations.

Signed-off-by: nemerle <[email protected]>
Artur K 3 anni fa
parent
commit
2dff26ddb5

+ 30 - 32
Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.cpp

@@ -197,48 +197,46 @@ AssetCatalogModel::~AssetCatalogModel()
     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.
     int dotIndex = filename.lastIndexOf('.');
-    if (dotIndex >= 0)
+    if (dotIndex < 0)
     {
-        QString extension = filename.mid(dotIndex);
-        for (auto pair : m_extensionToAssetType)
+        return AZ::Uuid::CreateNull();
+    }
+
+    QStringRef extension = filename.midRef(dotIndex);
+    for (const auto& pair : m_extensionToAssetType)
+    {
+        QString qExtensions = pair.first.c_str();
+        if (qExtensions.indexOf(extension) < 0 || pair.second.empty())
         {
-            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);
+            continue;
+        }
+        if (pair.second.size() == 1)
+        {
+            return pair.second[0];
+        }
 
-                    for (AZ::Uuid type : pair.second)
-                    {
-                        const AZ::Data::AssetHandler* handler = AZ::Data::AssetManager::Instance().GetHandler(type);
-                        if (handler && handler->CanHandleAsset(assetId))
-                        {
-                            returnType = type;
-                            break;
-                        }
-                    }
-                }
-                else
-                {
-                    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)
@@ -419,7 +417,7 @@ AssetCatalogEntry* AssetCatalogModel::AddAsset(QString assetPath, AZ::Data::Asse
             //  icons' memory being reclaimed and crashing the Editor.
             QSize size = fileIcon.actualSize(QSize(16, 16));
             QIcon deepCopy = fileIcon.pixmap(size).copy(0, 0, size.width(), size.height());
-            
+
             if (!fileIcon.isNull())
             {
                 m_assetTypeToIcon[assetType] = deepCopy;

+ 1 - 1
Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/AssetCatalogModel.h

@@ -110,7 +110,7 @@ protected:
     void SetFilterRegExp(const AZStd::string& filterType, const QRegExp& regExp);
     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);
 
     void ApplyFilter(QStandardItem* parent);