-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelfileio.cpp
More file actions
48 lines (34 loc) · 1 KB
/
levelfileio.cpp
File metadata and controls
48 lines (34 loc) · 1 KB
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
#include "levelfileio.h"
#include "levelhandler.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lvlfio, "app.levelfileio")
LevelFileIO::LevelFileIO(QObject *parent)
: QObject { parent }
{
}
LevelData LevelFileIO::loadLevel(const QString &path)
{
qCInfo(lvlfio) << "reading level data from path" << path;
QFile levelFile(path);
if (!levelFile.open(QIODevice::ReadOnly)) {
qCCritical(lvlfio) << "unable to read file";
return {};
}
QJsonDocument document = QJsonDocument::fromJson(levelFile.readAll());
return LevelData::fromJson(document.object());
}
bool LevelFileIO::storeLevel(const QString &path, const LevelData &data)
{
qCInfo(lvlfio) << "storing level data at path" << path;
QFile levelFile(path);
if (!levelFile.open(QIODevice::WriteOnly)) {
qCCritical(lvlfio) << "unable to write file";
return false;
}
QJsonDocument document(data.toJson());
levelFile.write(document.toJson());
return true;
}