| 421 | } |
| 422 | |
| 423 | bool AccountList::loadList() |
| 424 | { |
| 425 | if (m_listFilePath.isEmpty()) { |
| 426 | qCritical() << "Can't load Mojang account list. No file path given and no default set."; |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | QFile file(m_listFilePath); |
| 431 | |
| 432 | // Try to open the file and fail if we can't. |
| 433 | // TODO: We should probably report this error to the user. |
| 434 | if (!file.open(QIODevice::ReadOnly)) { |
| 435 | qCritical() << QString("Failed to read the account list file (%1).").arg(m_listFilePath).toUtf8(); |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | // Read the file and close it. |
| 440 | QByteArray jsonData = file.readAll(); |
| 441 | file.close(); |
| 442 | |
| 443 | QJsonParseError parseError; |
| 444 | QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError); |
| 445 | |
| 446 | // Fail if the JSON is invalid. |
| 447 | if (parseError.error != QJsonParseError::NoError) { |
| 448 | qCritical() << QString("Failed to parse account list file: %1 at offset %2") |
| 449 | .arg(parseError.errorString(), QString::number(parseError.offset)) |
| 450 | .toUtf8(); |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | // Make sure the root is an object. |
| 455 | if (!jsonDoc.isObject()) { |
| 456 | qCritical() << "Invalid account list JSON: Root should be an array."; |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | QJsonObject root = jsonDoc.object(); |
| 461 | |
| 462 | // Make sure the format version matches. |
| 463 | auto listVersion = root.value("formatVersion").toVariant().toInt(); |
| 464 | switch (listVersion) { |
| 465 | case AccountListVersion::MojangMSA: { |
| 466 | return loadV3(root); |
| 467 | } break; |
| 468 | default: { |
| 469 | QString newName = "accounts-old.json"; |
| 470 | qWarning() << "Unknown format version when loading account list. Existing one will be renamed to" << newName; |
| 471 | // Attempt to rename the old version. |
| 472 | file.rename(newName); |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | bool AccountList::loadV3(QJsonObject& root) |
| 479 | { |