mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-02-09 21:13:08 +00:00
Core: harden library.json loading for migration
This commit is contained in:
parent
3ec2badcd7
commit
ed9367fd71
1 changed files with 38 additions and 9 deletions
45
src/app.cpp
45
src/app.cpp
|
|
@ -17,6 +17,7 @@
|
|||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QStandardPaths>
|
||||
#include <QtConcurrent>
|
||||
|
||||
|
|
@ -958,20 +959,48 @@ void App::loadLibrary()
|
|||
return;
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
||||
if (!doc.isArray()) {
|
||||
const QByteArray data = file.readAll();
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qWarning() << "Failed to parse library.json:" << parseError.errorString() << "at offset" << parseError.offset;
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonArray gamesArray = doc.array();
|
||||
for (const QJsonValue &value : gamesArray) {
|
||||
if (value.isObject()) {
|
||||
QJsonArray gamesArray;
|
||||
if (doc.isArray()) {
|
||||
gamesArray = doc.array();
|
||||
} else if (doc.isObject()) {
|
||||
const QJsonObject rootObj = doc.object();
|
||||
const QJsonValue gamesValue = rootObj.value(QStringLiteral("games"));
|
||||
if (gamesValue.isArray()) {
|
||||
gamesArray = gamesValue.toArray();
|
||||
} else {
|
||||
qWarning() << "Invalid library.json format: expected a JSON array (or an object with a 'games' array)";
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
qWarning() << "Invalid library.json format: expected a JSON array (or an object with a 'games' array)";
|
||||
return;
|
||||
}
|
||||
|
||||
m_gameModel->clear();
|
||||
|
||||
for (int i = 0; i < gamesArray.size(); ++i) {
|
||||
const QJsonValue value = gamesArray.at(i);
|
||||
if (!value.isObject()) {
|
||||
qWarning() << "Skipping invalid library entry (not an object) at index" << i;
|
||||
continue;
|
||||
}
|
||||
|
||||
Game *game = Game::fromJson(value.toObject(), this);
|
||||
if (game) {
|
||||
if (!game) {
|
||||
qWarning() << "Skipping invalid game entry at index" << i;
|
||||
continue;
|
||||
}
|
||||
|
||||
m_gameModel->addGame(game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Game *App::createGame(const QString &name, const QString &launchCommand)
|
||||
|
|
|
|||
Loading…
Reference in a new issue