1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Quick 3D.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include "qssgassetimportmanager_p.h"
31 
32 #include "qssgassetimporter_p.h"
33 #include "qssgassetimporterfactory_p.h"
34 
35 #include <QtCore/QFile>
36 #include <QtCore/QDebug>
37 
38 QT_BEGIN_NAMESPACE
39 
QSSGAssetImportManager(QObject * parent)40 QSSGAssetImportManager::QSSGAssetImportManager(QObject *parent) : QObject(parent)
41 {
42     // load assetimporters
43     const QStringList keys = QSSGAssetImporterFactory::keys();
44     for (const auto &key : keys) {
45         auto importer = QSSGAssetImporterFactory::create(key, QStringList());
46         if (importer) {
47             m_assetImporters.append(importer);
48             // Add to extension map
49             for (const auto &extension : importer->inputExtensions()) {
50                 m_extensionsMap.insert(extension, importer);
51             }
52         } else {
53             qWarning() << "Failed to load asset import plugin with key: " << key;
54         }
55     }
56 }
57 
~QSSGAssetImportManager()58 QSSGAssetImportManager::~QSSGAssetImportManager()
59 {
60     for (auto importer : m_assetImporters) {
61         delete importer;
62     }
63 }
64 
65 // Compatibility with old API
importFile(const QString & filename,const QDir & outputPath,QString * error)66 QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
67                                                                        const QDir &outputPath,
68                                                                        QString *error)
69 {
70     return importFile(filename, outputPath, QVariantMap(), error);
71 }
72 
importFile(const QString & filename,const QDir & outputPath,const QVariantMap & options,QString * error)73 QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
74                                                                        const QDir &outputPath,
75                                                                        const QVariantMap &options,
76                                                                        QString *error)
77 {
78     QFileInfo fileInfo(filename);
79 
80     // Is this a real file?
81     if (!fileInfo.exists()) {
82         if (error)
83             *error = QStringLiteral("file does not exist");
84         return ImportState::IoError;
85     }
86 
87     // Do we have a importer to load the file?
88     const auto extension = fileInfo.suffix().toLower();
89     auto importer = m_extensionsMap.value(extension, nullptr);
90     if (!importer) {
91         if (error)
92             *error = QStringLiteral("unsupported file extension %1").arg(extension);
93         return ImportState::Unsupported;
94     }
95 
96     QStringList generatedFiles;
97     auto errorString = importer->import(fileInfo.absoluteFilePath(), outputPath, options, &generatedFiles);
98 
99     if (!errorString.isEmpty()) {
100         if (error) {
101             *error = QStringLiteral("%1").arg(errorString);
102         }
103 
104         return ImportState::IoError;
105     }
106 
107     // debug output
108     for (const auto &file : generatedFiles)
109         qDebug() << "generated file: " << file;
110 
111     return ImportState::Success;
112 }
113 
getOptionsForFile(const QString & filename)114 QVariantMap QSSGAssetImportManager::getOptionsForFile(const QString &filename)
115 {
116     QFileInfo fileInfo(filename);
117 
118     QVariantMap options;
119 
120     // Is this a real file?
121     if (fileInfo.exists()) {
122         // Do we have a importer to load the file?
123         const auto extension = fileInfo.suffix().toLower();
124         auto importer = m_extensionsMap.value(extension, nullptr);
125         if (importer)
126             options = importer->importOptions();
127     }
128 
129     return options;
130 }
131 
getAllOptions() const132 QHash<QString, QVariantMap> QSSGAssetImportManager::getAllOptions() const
133 {
134     QHash<QString, QVariantMap> options;
135     for (const auto importer : m_assetImporters)
136         options.insert(importer->inputExtensions().join(':'), importer->importOptions());
137     return options;
138 }
139 
getSupportedExtensions() const140 QHash<QString, QStringList> QSSGAssetImportManager::getSupportedExtensions() const
141 {
142     QHash<QString, QStringList> extensionMap;
143     for (const auto importer : qAsConst(m_assetImporters))
144         extensionMap.insert(importer->typeDescription(), importer->inputExtensions());
145     return extensionMap;
146 }
147 
148 QT_END_NAMESPACE
149