Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgassetimportmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
10
11#include <QtCore/QFile>
12#include <QtCore/QDebug>
13#include <QtCore/QHash>
14#include <QtCore/QMap>
15#include <QtCore/QList>
16#include <QtCore/QString>
17
18#include <QtQml/QQmlFile>
19
21
22QSSGAssetImportManager::QSSGAssetImportManager(QObject *parent) : QObject(parent)
23{
24 // load assetimporters
25 const QStringList keys = QSSGAssetImporterFactory::keys();
26 for (const auto &key : keys) {
27 auto importer = QSSGAssetImporterFactory::create(key, QStringList());
28 if (importer) {
29 m_assetImporters.append(importer);
30 // Add to extension map; when several importers claim the same
31 // extension the one with the highest priority() wins, and equal
32 // priorities keep the first importer registered. Keys are
33 // normalized to lower case, because every lookup is.
34 const auto extensions = importer->inputExtensions();
35 for (const auto &extension : extensions) {
36 const QString extensionKey = extension.toLower();
37 const auto existing = m_extensionsMap.constFind(extensionKey);
38 if (existing == m_extensionsMap.cend() || (*existing)->priority() < importer->priority())
39 m_extensionsMap.insert(extensionKey, importer);
40 }
41 } else {
42 qWarning() << "Failed to load asset import plugin with key: " << key;
43 }
44 }
45}
46
47QSSGAssetImportManager::~QSSGAssetImportManager()
48{
49 for (auto *importer : std::as_const(m_assetImporters))
50 delete importer;
51}
52
53// Compatibility with old API
54QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
55 const QDir &outputPath,
56 QString *error)
57{
58 return importFile(filename, outputPath, QJsonObject(), error);
59}
60
61QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
62 const QDir &outputPath,
63 const QJsonObject &options,
64 QString *error)
65{
66 QFileInfo fileInfo(filename);
67
68 // Is this a real file?
69 if (!fileInfo.exists()) {
70 if (error)
71 *error = QStringLiteral("file does not exist");
72 return ImportState::IoError;
73 }
74
75 // Do we have a importer to load the file?
76 const auto extension = fileInfo.suffix().toLower();
77 auto importer = m_extensionsMap.value(extension, nullptr);
78 if (!importer) {
79 if (error)
80 *error = QStringLiteral("unsupported file extension %1").arg(extension);
81 return ImportState::Unsupported;
82 }
83
84 QStringList generatedFiles;
85 auto errorString = importer->import(fileInfo.absoluteFilePath(), outputPath, options, &generatedFiles);
86
87 if (!errorString.isEmpty()) {
88 if (error) {
89 *error = QStringLiteral("%1").arg(errorString);
90 }
91
92 return ImportState::IoError;
93 }
94
95 // debug output
96 for (const auto &file : std::as_const(generatedFiles))
97 qDebug() << "generated file: " << file;
98
99 return ImportState::Success;
100}
101
102QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url,
103 QSSGSceneDesc::Scene &scene,
104 QString *error)
105{
106 return importFile(url, scene, {}, error);
107}
108
109QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url,
110 QSSGSceneDesc::Scene &scene,
111 const QJsonObject &options,
112 QString *error)
113{
114 // Anything that is not a local file or a resource has to have its
115 // extension taken from the path rather than from the serialized URL, or a
116 // query string or fragment ends up being read as the suffix instead.
117 const QString filePath = QQmlFile::isLocalFile(url) ? QQmlFile::urlToLocalFileOrQrc(url) : url.path();
118
119 const auto extension = QFileInfo(filePath).suffix().toLower();
120 auto importer = m_extensionsMap.value(extension, nullptr);
121 if (!importer) {
122 if (error)
123 *error = QStringLiteral("unsupported file extension %1").arg(extension);
124 return ImportState::Unsupported;
125 }
126
127 const auto ret = importer->import(url, options, scene);
128 if (!ret.isEmpty()) {
129 if (error)
130 *error = ret;
131 return ImportState::IoError;
132 }
133
134 return ImportState::Success;
135}
136
137QJsonObject QSSGAssetImportManager::getOptionsForFile(const QString &filename)
138{
139 QFileInfo fileInfo(filename);
140
141 QJsonObject options;
142
143 // Is this a real file?
144 if (fileInfo.exists()) {
145 // Do we have a importer to load the file?
146 const auto extension = fileInfo.suffix().toLower();
147 auto importer = m_extensionsMap.value(extension, nullptr);
148 if (importer)
149 options = importer->importOptions();
150 }
151
152 return options;
153}
154
155QSSGAssetImportManager::PluginOptionMaps QSSGAssetImportManager::getAllOptions() const
156{
157 PluginOptionMaps options;
158 for (const auto importer : m_assetImporters)
159 options.insert(importer->inputExtensions().join(QChar::fromLatin1(':')), importer->importOptions());
160 return options;
161}
162
163QHash<QString, QStringList> QSSGAssetImportManager::getSupportedExtensions() const
164{
165 QHash<QString, QStringList> extensionMap;
166 for (const auto importer : std::as_const(m_assetImporters))
167 extensionMap.insert(importer->typeDescription(), importer->inputExtensions());
168 return extensionMap;
169}
170
171QList<QSSGAssetImporterPluginInfo> QSSGAssetImportManager::getImporterPluginInfos() const
172{
173 QList<QSSGAssetImporterPluginInfo> output;
174
175 for (const QSSGAssetImporter *importer : m_assetImporters) {
176 QSSGAssetImporterPluginInfo plugin;
177 plugin.name = importer->name();
178 plugin.inputExtensions = importer->inputExtensions();
179 plugin.outputExtension = importer->outputExtension();
180 plugin.type = importer->type();
181 plugin.importOptions = importer->importOptions();
182 plugin.typeDescription = importer->typeDescription();
183 output.push_back(plugin);
184 }
185
186 return output;
187}
188
189QT_END_NAMESPACE
Combined button and popup list for selecting options.