| 274 | */ |
| 275 | |
| 276 | bool parseMinecraftProfileMojang(QByteArray & data, MinecraftProfile &output) { |
| 277 | qDebug() << "Parsing Minecraft profile..."; |
| 278 | #ifndef NDEBUG |
| 279 | qDebug() << data; |
| 280 | #endif |
| 281 | |
| 282 | QJsonParseError jsonError; |
| 283 | QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); |
| 284 | if(jsonError.error) { |
| 285 | qWarning() << "Failed to parse response as JSON: " << jsonError.errorString(); |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | auto obj = Json::requireObject(doc, "mojang minecraft profile"); |
| 290 | if(!getString(obj.value("id"), output.id)) { |
| 291 | qWarning() << "Minecraft profile id is not a string"; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | if(!getString(obj.value("name"), output.name)) { |
| 296 | qWarning() << "Minecraft profile name is not a string"; |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | auto propsArray = obj.value("properties").toArray(); |
| 301 | QByteArray texturePayload; |
| 302 | for( auto p : propsArray) { |
| 303 | auto pObj = p.toObject(); |
| 304 | auto name = pObj.value("name"); |
| 305 | if (!name.isString() || name.toString() != "textures") { |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | auto value = pObj.value("value"); |
| 310 | if (value.isString()) { |
| 311 | #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
| 312 | texturePayload = QByteArray::fromBase64(value.toString().toUtf8(), QByteArray::AbortOnBase64DecodingErrors); |
| 313 | #else |
| 314 | texturePayload = QByteArray::fromBase64(value.toString().toUtf8()); |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | if (!texturePayload.isEmpty()) { |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (texturePayload.isNull()) { |
| 324 | qWarning() << "No texture payload data"; |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | doc = QJsonDocument::fromJson(texturePayload, &jsonError); |
| 329 | if(jsonError.error) { |
| 330 | qWarning() << "Failed to parse response as JSON: " << jsonError.errorString(); |
| 331 | return false; |
| 332 | } |
| 333 |
no test coverage detected