mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-02-10 13:13:09 +00:00
Core: harden library.json loading for migration
This commit is contained in:
parent
3ec2badcd7
commit
4d434e0f3a
1 changed files with 38 additions and 9 deletions
45
src/app.cpp
45
src/app.cpp
|
|
@ -17,6 +17,7 @@
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
#include <QJsonParseError>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QtConcurrent>
|
#include <QtConcurrent>
|
||||||
|
|
||||||
|
|
@ -958,20 +959,48 @@ void App::loadLibrary()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
const QByteArray data = file.readAll();
|
||||||
if (!doc.isArray()) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray gamesArray = doc.array();
|
QJsonArray gamesArray;
|
||||||
for (const QJsonValue &value : gamesArray) {
|
if (doc.isArray()) {
|
||||||
if (value.isObject()) {
|
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);
|
Game *game = Game::fromJson(value.toObject(), this);
|
||||||
if (game) {
|
if (!game) {
|
||||||
|
qWarning() << "Skipping invalid game entry at index" << i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
m_gameModel->addGame(game);
|
m_gameModel->addGame(game);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Game *App::createGame(const QString &name, const QString &launchCommand)
|
Game *App::createGame(const QString &name, const QString &launchCommand)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue