| 457 | } |
| 458 | |
| 459 | bool AccountList::loadList() |
| 460 | { |
| 461 | if (m_listFilePath.isEmpty()) |
| 462 | { |
| 463 | qCritical() << "Can't load Mojang account list. No file path given and no default set."; |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | QFile file(m_listFilePath); |
| 468 | |
| 469 | // Try to open the file and fail if we can't. |
| 470 | // TODO: We should probably report this error to the user. |
| 471 | if (!file.open(QIODevice::ReadOnly)) |
| 472 | { |
| 473 | qCritical() << QString("Failed to read the account list file (%1).").arg(m_listFilePath).toUtf8(); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | // Read the file and close it. |
| 478 | QByteArray jsonData = file.readAll(); |
| 479 | file.close(); |
| 480 | |
| 481 | QJsonParseError parseError; |
| 482 | QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError); |
| 483 | |
| 484 | // Fail if the JSON is invalid. |
| 485 | if (parseError.error != QJsonParseError::NoError) |
| 486 | { |
| 487 | qCritical() << QString("Failed to parse account list file: %1 at offset %2") |
| 488 | .arg(parseError.errorString(), QString::number(parseError.offset)) |
| 489 | .toUtf8(); |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | // Make sure the root is an object. |
| 494 | if (!jsonDoc.isObject()) |
| 495 | { |
| 496 | qCritical() << "Invalid account list JSON: Root should be an array."; |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | QJsonObject root = jsonDoc.object(); |
| 501 | |
| 502 | // Make sure the format version matches. |
| 503 | auto listVersion = root.value("formatVersion").toVariant().toInt(); |
| 504 | switch(listVersion) { |
| 505 | case AccountListVersion::MojangOnly: { |
| 506 | return loadV2(root); |
| 507 | } |
| 508 | break; |
| 509 | case AccountListVersion::MojangMSA: { |
| 510 | return loadV3(root); |
| 511 | } |
| 512 | break; |
| 513 | default: { |
| 514 | QString newName = "accounts-old.json"; |
| 515 | qWarning() << "Unknown format version when loading account list. Existing one will be renamed to" << newName; |
| 516 | // Attempt to rename the old version. |