1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qssgassetimportmanager_p.h" |
5 | |
6 | #include "qssgassetimporter_p.h" |
7 | #include "qssgassetimporterfactory_p.h" |
8 | |
9 | #include <QtCore/QFile> |
10 | #include <QtCore/QDebug> |
11 | #include <QtCore/QHash> |
12 | #include <QtCore/QMap> |
13 | #include <QtCore/QList> |
14 | #include <QtCore/QString> |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | QSSGAssetImportManager::QSSGAssetImportManager(QObject *parent) : QObject(parent) |
19 | { |
20 | // load assetimporters |
21 | const QStringList keys = QSSGAssetImporterFactory::keys(); |
22 | for (const auto &key : keys) { |
23 | auto importer = QSSGAssetImporterFactory::create(name: key, args: QStringList()); |
24 | if (importer) { |
25 | m_assetImporters.append(t: importer); |
26 | // Add to extension map |
27 | for (const auto &extension : importer->inputExtensions()) { |
28 | m_extensionsMap.insert(key: extension, value: importer); |
29 | } |
30 | } else { |
31 | qWarning() << "Failed to load asset import plugin with key: " << key; |
32 | } |
33 | } |
34 | } |
35 | |
36 | QSSGAssetImportManager::~QSSGAssetImportManager() |
37 | { |
38 | for (auto importer : m_assetImporters) { |
39 | delete importer; |
40 | } |
41 | } |
42 | |
43 | // Compatibility with old API |
44 | QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename, |
45 | const QDir &outputPath, |
46 | QString *error) |
47 | { |
48 | return importFile(filename, outputPath, options: QJsonObject(), error); |
49 | } |
50 | |
51 | QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename, |
52 | const QDir &outputPath, |
53 | const QJsonObject &options, |
54 | QString *error) |
55 | { |
56 | QFileInfo fileInfo(filename); |
57 | |
58 | // Is this a real file? |
59 | if (!fileInfo.exists()) { |
60 | if (error) |
61 | *error = QStringLiteral("file does not exist" ); |
62 | return ImportState::IoError; |
63 | } |
64 | |
65 | // Do we have a importer to load the file? |
66 | const auto extension = fileInfo.suffix().toLower(); |
67 | auto importer = m_extensionsMap.value(key: extension, defaultValue: nullptr); |
68 | if (!importer) { |
69 | if (error) |
70 | *error = QStringLiteral("unsupported file extension %1" ).arg(a: extension); |
71 | return ImportState::Unsupported; |
72 | } |
73 | |
74 | QStringList generatedFiles; |
75 | auto errorString = importer->import(sourceFile: fileInfo.absoluteFilePath(), savePath: outputPath, options, generatedFiles: &generatedFiles); |
76 | |
77 | if (!errorString.isEmpty()) { |
78 | if (error) { |
79 | *error = QStringLiteral("%1" ).arg(a: errorString); |
80 | } |
81 | |
82 | return ImportState::IoError; |
83 | } |
84 | |
85 | // debug output |
86 | for (const auto &file : generatedFiles) |
87 | qDebug() << "generated file: " << file; |
88 | |
89 | return ImportState::Success; |
90 | } |
91 | |
92 | QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url, |
93 | QSSGSceneDesc::Scene &scene, |
94 | QString *error) |
95 | { |
96 | return importFile(url, scene, options: {}, error); |
97 | } |
98 | |
99 | QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url, |
100 | QSSGSceneDesc::Scene &scene, |
101 | const QJsonObject &options, |
102 | QString *error) |
103 | { |
104 | auto importState = ImportState::Unsupported; |
105 | auto it = m_assetImporters.cbegin(); |
106 | const auto end = m_assetImporters.cend(); |
107 | for (; it != end; ++it) { |
108 | if ((*it)->name() == QLatin1String("assimp" )) |
109 | break; |
110 | } |
111 | |
112 | if (it != end) { |
113 | const auto &importer = *it; |
114 | const auto ret = importer->import(url, options, scene); |
115 | if (!ret.isEmpty()) { |
116 | if (error) |
117 | *error = ret; |
118 | importState = ImportState::IoError; |
119 | } else { |
120 | importState = ImportState::Success; |
121 | } |
122 | } |
123 | |
124 | return importState; |
125 | } |
126 | |
127 | QJsonObject QSSGAssetImportManager::getOptionsForFile(const QString &filename) |
128 | { |
129 | QFileInfo fileInfo(filename); |
130 | |
131 | QJsonObject options; |
132 | |
133 | // Is this a real file? |
134 | if (fileInfo.exists()) { |
135 | // Do we have a importer to load the file? |
136 | const auto extension = fileInfo.suffix().toLower(); |
137 | auto importer = m_extensionsMap.value(key: extension, defaultValue: nullptr); |
138 | if (importer) |
139 | options = importer->importOptions(); |
140 | } |
141 | |
142 | return options; |
143 | } |
144 | |
145 | QSSGAssetImportManager::PluginOptionMaps QSSGAssetImportManager::getAllOptions() const |
146 | { |
147 | PluginOptionMaps options; |
148 | for (const auto importer : m_assetImporters) |
149 | options.insert(key: importer->inputExtensions().join(sep: QChar::fromLatin1(c: ':')), value: importer->importOptions()); |
150 | return options; |
151 | } |
152 | |
153 | QHash<QString, QStringList> QSSGAssetImportManager::getSupportedExtensions() const |
154 | { |
155 | QHash<QString, QStringList> extensionMap; |
156 | for (const auto importer : std::as_const(t: m_assetImporters)) |
157 | extensionMap.insert(key: importer->typeDescription(), value: importer->inputExtensions()); |
158 | return extensionMap; |
159 | } |
160 | |
161 | QList<QSSGAssetImporterPluginInfo> QSSGAssetImportManager::getImporterPluginInfos() const |
162 | { |
163 | QList<QSSGAssetImporterPluginInfo> output; |
164 | |
165 | for (const QSSGAssetImporter *importer : m_assetImporters) { |
166 | QSSGAssetImporterPluginInfo plugin; |
167 | plugin.name = importer->name(); |
168 | plugin.inputExtensions = importer->inputExtensions(); |
169 | plugin.outputExtension = importer->outputExtension(); |
170 | plugin.type = importer->type(); |
171 | plugin.importOptions = importer->importOptions(); |
172 | plugin.typeDescription = importer->typeDescription(); |
173 | output.push_back(t: plugin); |
174 | } |
175 | |
176 | return output; |
177 | } |
178 | |
179 | QT_END_NAMESPACE |
180 | |