summaryrefslogtreecommitdiffstats
path: root/editorlib/src/editorscenesaver.cpp
blob: 2e128f6852db769c1b65ec25af2513a42913a970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D Editor of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "editorscenesaver.h"
#include "editorutils.h"

#include <Qt3DCore/qentity.h>
#include <private/qsceneexportfactory_p.h>
#include <private/qsceneexporter_p.h>
#include <private/qsceneimportfactory_p.h>
#include <private/qsceneimporter_p.h>

#include <QtCore/qfile.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qtemporarydir.h>
#include <QtCore/qtemporaryfile.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qdatetime.h>

namespace {

const QString autoSavePostfix = QStringLiteral("_autosave");
const QString saveSuffix = QStringLiteral(".qt3dscene");
const QString exportNameTemplate = QStringLiteral("%1_scene_res");
const QString editorDataFile = QStringLiteral("qt3dscene_editor_data.json");
const qint32 saveFileVersion = 1;
const QByteArray saveFileId = QByteArrayLiteral("Qt3DSceneEditor_SaveFile");
const QString activeCameraKey = QStringLiteral("activeCamera");
const QString rootEntityNameKey = QStringLiteral("rootEntityName");

} // namespace

EditorSceneSaver::EditorSceneSaver(QObject *parent)
    : QObject(parent)
    , m_loadDir(nullptr)
{
}

EditorSceneSaver::~EditorSceneSaver()
{
    delete m_loadDir;
}

bool EditorSceneSaver::saveScene(Qt3DCore::QEntity *sceneEntity,
                                 const QString &activeSceneCamera,
                                 const QString &saveFileName,
                                 bool autosave)
{
    // Save consists of a single JSON file with .qt3dscene extension and the exported QGLTF scene
    // in a separate subfolder in the same folder as the main save file.

    // The save is first created in a temp directory to ensure saving over existing save doesn't
    // partially overwrite the old save in case of error.
    QString finalFullSaveFilePathName = saveFileName;
    if (!finalFullSaveFilePathName.endsWith(saveSuffix))
        finalFullSaveFilePathName.append(saveSuffix);
    QFileInfo saveFileInfo(finalFullSaveFilePathName);
    QString finalSavePath = saveFileInfo.path();
    QString finalFileName = saveFileInfo.fileName();
    QString saveExportName = exportNameTemplate.arg(saveFileInfo.completeBaseName());
    QDir finalSaveDir = saveFileInfo.absoluteDir();
    if (autosave) {
        finalFullSaveFilePathName.append(autoSavePostfix);
        finalFileName.append(autoSavePostfix);
        saveExportName.append(autoSavePostfix);
    }
    QString finalScenePath = finalSavePath + QStringLiteral("/") + saveExportName;

    // Save scene to a temp folder using GLTF export plugin
    QTemporaryDir exportDir(finalScenePath + QStringLiteral("_temp_save_XXXXXX"));

    const QStringList keys = Qt3DRender::QSceneExportFactory::keys();
    for (const QString &key : keys) {
        Qt3DRender::QSceneExporter *exporter =
                Qt3DRender::QSceneExportFactory::create(key, QStringList());
        if (exporter != nullptr && key == QStringLiteral("gltfexport")) {
            QVariantHash options;
            if (!exporter->exportScene(sceneEntity, exportDir.path(), saveExportName, options)) {
                qWarning() << "Failed to export GLTF when saving the scene";
                return false;
            }
            break;
        }
    }

    // Create new save file in the temp folder
    QJsonDocument jsonDoc;
    QJsonObject editorData;
    editorData[activeCameraKey] = activeSceneCamera;
    editorData[rootEntityNameKey] = sceneEntity->objectName();
    jsonDoc.setObject(editorData);
    const QString tempScenePath = exportDir.path() + QStringLiteral("/") + saveExportName;
    const QString saveFilePath = exportDir.path() + QStringLiteral("/") + finalFileName;
    QFile saveFile(saveFilePath);
    if (saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QByteArray json = jsonDoc.toJson();
        saveFile.write(json);
        saveFile.close();
    } else {
        qWarning() << "Failed to create editor data file when saving the scene";
        return false;
    }

    // Create a unique backup suffix from current time
    QDateTime currentTime = QDateTime::currentDateTime();
    QString uniqueSuffix = currentTime.toString(QStringLiteral("yyyyMMddHHmmsszzz"));
    QString backupExportName = saveExportName + uniqueSuffix;
    QString backupSaveFileName = finalFullSaveFilePathName + uniqueSuffix;

    // Rename the old save file and exported resources and savefile
    if (finalSaveDir.exists(saveExportName)) {
        if (!finalSaveDir.rename(saveExportName, backupExportName)) {
            qWarning() << "Failed to rename the old resource dir:" << saveExportName;
            return false;
        }
    }
    QFile oldSaveFile(finalFullSaveFilePathName);
    if (oldSaveFile.exists()) {
        if (!oldSaveFile.rename(backupSaveFileName)) {
            qWarning() << "Failed to rename the old save file:" << finalFullSaveFilePathName;
            finalSaveDir.rename(backupExportName, saveExportName);
            return false;
        }
    }

    // Rename the temporary files as finals
    if (!QFile::rename(tempScenePath, finalScenePath)) {
        qWarning() << "Failed to rename the temp scene:" << tempScenePath << "->" << finalScenePath;
        return false;
    }
    if (!saveFile.rename(finalFullSaveFilePathName)) {
        qWarning() << "Failed to rename the temp save file:" << saveFilePath <<
                      "->" << finalFullSaveFilePathName;
        return false;
    }

    // If everything went well, remove the renamed originals.
    QFile::remove(backupSaveFileName);
    if (finalSaveDir.cd(backupExportName))
        finalSaveDir.removeRecursively();

    return true;
}

Qt3DCore::QEntity *EditorSceneSaver::loadScene(const QString &fileName, Qt3DCore::QEntity *camera)
{
    Qt3DCore::QEntity *loadedScene = nullptr;
    camera = nullptr;

    // Read the scene data
    QFile jsonFile(fileName);
    QString activeCameraId;
    QString sceneName;
    if (jsonFile.open(QIODevice::ReadOnly)) {
        QByteArray jsonData = jsonFile.readAll();
        jsonFile.close();
        QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
        QJsonObject editorData = jsonDocument.object();
        activeCameraId = editorData.value(activeCameraKey).toString();
        sceneName = editorData.value(rootEntityNameKey).toString();
    } else {
        qWarning() << "Failed to open scene save file:" << fileName;
        return nullptr;
    }

    // Import scene from temp folder
    const QStringList keys = Qt3DRender::QSceneImportFactory::keys();
    for (const QString &key : keys) {
        Qt3DRender::QSceneImporter *importer =
                Qt3DRender::QSceneImportFactory::create(key, QStringList());
        if (importer != nullptr && key == QStringLiteral("gltf")) {
            QFileInfo saveFileInfo(fileName);
            const QString exportName = exportNameTemplate.arg(saveFileInfo.completeBaseName());
            QString sceneSource = saveFileInfo.absolutePath();
            sceneSource += QStringLiteral("/");
            sceneSource += exportName;
            sceneSource += QStringLiteral("/");
            sceneSource += exportName;
            sceneSource += QStringLiteral(".qgltf");
            importer->setSource(QUrl::fromLocalFile(sceneSource));
            loadedScene = importer->scene();
            break;
        }
    }

    if (!loadedScene) {
        qWarning() << "Failed to load the saved scene";
        return nullptr;
    }

    loadedScene->setObjectName(sceneName);

    // Find the active camera
    camera = EditorUtils::findEntityByName(loadedScene, activeCameraId);
    if (!EditorUtils::entityCameraLens(camera))
        camera = nullptr;

    return loadedScene;
}

void EditorSceneSaver::deleteSavedScene(const QString &saveFileName, bool autosave)
{
    QString fullSaveFilePathName = saveFileName;
    if (!fullSaveFilePathName.endsWith(saveSuffix))
        fullSaveFilePathName.append(saveSuffix);
    QFileInfo saveFileInfo(fullSaveFilePathName);
    QString savePath = saveFileInfo.path();
    QString saveExportName = exportNameTemplate.arg(saveFileInfo.completeBaseName());
    if (autosave) {
        fullSaveFilePathName.append(autoSavePostfix);
        saveExportName.append(autoSavePostfix);
    }
    QDir sceneResourcesDir(savePath + QStringLiteral("/") + saveExportName);
    sceneResourcesDir.removeRecursively();
    QFile::remove(fullSaveFilePathName);
}