NOISSUE add setting capes, tweak missing profile message, fix cape IDs

This commit is contained in:
Petr Mrázek 2021-08-20 01:34:32 +02:00
parent 94fd9a3535
commit 1b68d51da6
11 changed files with 317 additions and 130 deletions

View File

@ -329,6 +329,8 @@ set(MINECRAFT_SOURCES
minecraft/AssetsUtils.cpp minecraft/AssetsUtils.cpp
# Minecraft services # Minecraft services
minecraft/services/CapeChange.cpp
minecraft/services/CapeChange.h
minecraft/services/SkinUpload.cpp minecraft/services/SkinUpload.cpp
minecraft/services/SkinUpload.h minecraft/services/SkinUpload.h
minecraft/services/SkinDelete.cpp minecraft/services/SkinDelete.cpp

View File

@ -73,7 +73,17 @@ void LoginDialog::on_passTextBox_textEdited(const QString &newText)
void LoginDialog::onTaskFailed(const QString &reason) void LoginDialog::onTaskFailed(const QString &reason)
{ {
// Set message // Set message
ui->label->setText("<span style='color:red'>" + reason + "</span>"); auto lines = reason.split('\n');
QString processed;
for(auto line: lines) {
if(line.size()) {
processed += "<font color='red'>" + line + "</font>\n";
}
else {
processed += '\n';
}
}
ui->label->setText(processed);
// Re-enable user-interaction // Re-enable user-interaction
setUserInputsEnabled(true); setUserInputsEnabled(true);

View File

@ -60,7 +60,17 @@ void MSALoginDialog::setUserInputsEnabled(bool enable)
void MSALoginDialog::onTaskFailed(const QString &reason) void MSALoginDialog::onTaskFailed(const QString &reason)
{ {
// Set message // Set message
ui->label->setText("<span style='color:red'>" + reason + "</span>"); auto lines = reason.split('\n');
QString processed;
for(auto line: lines) {
if(line.size()) {
processed += "<font color='red'>" + line + "</font>\n";
}
else {
processed += '\n';
}
}
ui->label->setText(processed);
// Re-enable user-interaction // Re-enable user-interaction
setUserInputsEnabled(true); setUserInputsEnabled(true);

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>421</width> <width>491</width>
<height>114</height> <height>143</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -23,10 +23,12 @@
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string notr="true">Message label placeholder.</string> <string notr="true">Message label placeholder.
aaaaa</string>
</property> </property>
<property name="textFormat"> <property name="textFormat">
<enum>Qt::RichText</enum> <enum>Qt::MarkdownText</enum>
</property> </property>
<property name="textInteractionFlags"> <property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> <set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>

View File

@ -1,11 +1,16 @@
#include <QFileInfo> #include <QFileInfo>
#include <QFileDialog> #include <QFileDialog>
#include <QPainter>
#include <FileSystem.h> #include <FileSystem.h>
#include <minecraft/services/SkinUpload.h> #include <minecraft/services/SkinUpload.h>
#include <tasks/SequentialTask.h>
#include "SkinUploadDialog.h" #include "SkinUploadDialog.h"
#include "ui_SkinUploadDialog.h" #include "ui_SkinUploadDialog.h"
#include "ProgressDialog.h" #include "ProgressDialog.h"
#include "CustomMessageBox.h" #include "CustomMessageBox.h"
#include <minecraft/services/CapeChange.h>
void SkinUploadDialog::on_buttonBox_rejected() void SkinUploadDialog::on_buttonBox_rejected()
{ {
@ -85,8 +90,13 @@ void SkinUploadDialog::on_buttonBox_accepted()
{ {
model = SkinUpload::ALEX; model = SkinUpload::ALEX;
} }
SkinUploadPtr upload = std::make_shared<SkinUpload>(this, session, FS::read(fileName), model); SequentialTask skinUpload;
if (prog.execWithTask((Task*)upload.get()) != QDialog::Accepted) skinUpload.addTask(std::make_shared<SkinUpload>(this, session, FS::read(fileName), model));
auto selectedCape = ui->capeCombo->currentData().toString();
if(selectedCape != session->m_accountPtr->accountData()->minecraftProfile.currentCape) {
skinUpload.addTask(std::make_shared<CapeChange>(this, session, selectedCape));
}
if (prog.execWithTask(&skinUpload) != QDialog::Accepted)
{ {
CustomMessageBox::selectable(this, tr("Skin Upload"), tr("Failed to upload skin!"), QMessageBox::Warning)->exec(); CustomMessageBox::selectable(this, tr("Skin Upload"), tr("Failed to upload skin!"), QMessageBox::Warning)->exec();
close(); close();
@ -111,4 +121,34 @@ SkinUploadDialog::SkinUploadDialog(MinecraftAccountPtr acct, QWidget *parent)
:QDialog(parent), m_acct(acct), ui(new Ui::SkinUploadDialog) :QDialog(parent), m_acct(acct), ui(new Ui::SkinUploadDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
// FIXME: add a model for this, download/refresh the capes on demand
auto &data = *acct->accountData();
int index = 0;
ui->capeCombo->addItem(tr("No Cape"), QVariant());
auto currentCape = data.minecraftProfile.currentCape;
if(currentCape.isEmpty()) {
ui->capeCombo->setCurrentIndex(index);
}
for(auto & cape: data.minecraftProfile.capes) {
index++;
if(cape.data.size()) {
QPixmap capeImage;
if(capeImage.loadFromData(cape.data, "PNG")) {
QPixmap preview = QPixmap(10, 16);
QPainter painter(&preview);
painter.drawPixmap(0, 0, capeImage.copy(1, 1, 10, 16));
ui->capeCombo->addItem(capeImage, cape.alias, cape.id);
if(currentCape == cape.id) {
ui->capeCombo->setCurrentIndex(index);
}
continue;
}
}
ui->capeCombo->addItem(cape.alias, cape.id);
if(currentCape == cape.id) {
ui->capeCombo->setCurrentIndex(index);
}
}
} }

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>413</width> <width>394</width>
<height>300</height> <height>360</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -71,6 +71,18 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item>
<widget class="QGroupBox" name="capeBox">
<property name="title">
<string>Cape</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QComboBox" name="capeCombo"/>
</item>
</layout>
</widget>
</item>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons"> <property name="standardButtons">

View File

@ -78,8 +78,8 @@ void profileToJSONV3(QJsonObject &parent, MinecraftProfile p, const char * token
QJsonObject out; QJsonObject out;
out["id"] = QJsonValue(p.id); out["id"] = QJsonValue(p.id);
out["name"] = QJsonValue(p.name); out["name"] = QJsonValue(p.name);
if(p.currentCape != -1) { if(!p.currentCape.isEmpty()) {
out["cape"] = p.capes[p.currentCape].id; out["cape"] = p.currentCape;
} }
{ {
@ -155,6 +155,7 @@ MinecraftProfile profileFromJSONV3(const QJsonObject &parent, const char * token
} }
} }
{
auto capesV = tokenObject.value("capes"); auto capesV = tokenObject.value("capes");
if(!capesV.isArray()) { if(!capesV.isArray()) {
qWarning() << "capes is not an array!"; qWarning() << "capes is not an array!";
@ -189,7 +190,18 @@ MinecraftProfile profileFromJSONV3(const QJsonObject &parent, const char * token
qWarning() << "cape data is something unexpected"; qWarning() << "cape data is something unexpected";
return MinecraftProfile(); return MinecraftProfile();
} }
out.capes.push_back(cape); out.capes[cape.id] = cape;
}
}
// current cape
{
auto capeV = tokenObject.value("cape");
if(capeV.isString()) {
auto currentCape = capeV.toString();
if(out.capes.contains(currentCape)) {
out.currentCape = currentCape;
}
}
} }
out.validity = Katabasis::Validity::Assumed; out.validity = Katabasis::Validity::Assumed;
return out; return out;

View File

@ -25,8 +25,8 @@ struct MinecraftProfile {
QString id; QString id;
QString name; QString name;
Skin skin; Skin skin;
int currentCape = -1; QString currentCape;
QVector<Cape> capes; QMap<QString, Cape> capes;
Katabasis::Validity validity = Katabasis::Validity::None; Katabasis::Validity validity = Katabasis::Validity::None;
}; };

View File

@ -576,7 +576,9 @@ void AuthContext::onXBoxProfileDone(
} }
void AuthContext::checkResult() { void AuthContext::checkResult() {
qDebug() << "AuthContext::checkResult called";
if(m_requestsDone != 2) { if(m_requestsDone != 2) {
qDebug() << "Number of ready results:" << m_requestsDone;
return; return;
} }
if(m_mcAuthSucceeded && m_xboxProfileSucceeded) { if(m_mcAuthSucceeded && m_xboxProfileSucceeded) {
@ -638,10 +640,9 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
break; break;
} }
auto capesArray = obj.value("capes").toArray(); auto capesArray = obj.value("capes").toArray();
int i = -1;
int currentCape = -1; QString currentCape;
for(auto cape: capesArray) { for(auto cape: capesArray) {
i++;
auto capeObj = cape.toObject(); auto capeObj = cape.toObject();
Cape capeOut; Cape capeOut;
if(!getString(capeObj.value("id"), capeOut.id)) { if(!getString(capeObj.value("id"), capeOut.id)) {
@ -652,7 +653,7 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
continue; continue;
} }
if(state == "ACTIVE") { if(state == "ACTIVE") {
currentCape = i; currentCape = capeOut.id;
} }
if(!getString(capeObj.value("url"), capeOut.url)) { if(!getString(capeObj.value("url"), capeOut.url)) {
continue; continue;
@ -661,8 +662,7 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
continue; continue;
} }
// we deal with only the active skin output.capes[capeOut.id] = capeOut;
output.capes.push_back(capeOut);
} }
output.currentCape = currentCape; output.currentCape = currentCape;
output.validity = Katabasis::Validity::Certain; output.validity = Katabasis::Validity::Certain;
@ -692,18 +692,18 @@ void AuthContext::onMinecraftProfileDone(int, QNetworkReply::NetworkError error,
if (error == QNetworkReply::ContentNotFoundError) { if (error == QNetworkReply::ContentNotFoundError) {
m_data->minecraftProfile = MinecraftProfile(); m_data->minecraftProfile = MinecraftProfile();
finishActivity(); finishActivity();
changeState(STATE_FAILED_HARD, tr("Account is missing a profile")); changeState(STATE_FAILED_HARD, tr("Account is missing a Minecraft Java profile.\n\nWhile the Microsoft account is valid, it does not own the game.\n\nYou might own Bedrock on this account, but that does not give you access to Java currently."));
return; return;
} }
if (error != QNetworkReply::NoError) { if (error != QNetworkReply::NoError) {
finishActivity(); finishActivity();
changeState(STATE_FAILED_HARD, tr("Profile acquisition failed")); changeState(STATE_FAILED_HARD, tr("Minecraft Java profile acquisition failed."));
return; return;
} }
if(!parseMinecraftProfile(data, m_data->minecraftProfile)) { if(!parseMinecraftProfile(data, m_data->minecraftProfile)) {
m_data->minecraftProfile = MinecraftProfile(); m_data->minecraftProfile = MinecraftProfile();
finishActivity(); finishActivity();
changeState(STATE_FAILED_HARD, tr("Profile response could not be parsed")); changeState(STATE_FAILED_HARD, tr("Minecraft Java profile response could not be parsed"));
return; return;
} }
doGetSkin(); doGetSkin();

View File

@ -0,0 +1,67 @@
#include "CapeChange.h"
#include <QNetworkRequest>
#include <QHttpMultiPart>
#include <Env.h>
CapeChange::CapeChange(QObject *parent, AuthSessionPtr session, QString cape)
: Task(parent), m_capeId(cape), m_session(session)
{
}
void CapeChange::setCape(QString& cape) {
QNetworkRequest request(QUrl("https://api.minecraftservices.com/minecraft/profile/capes/active"));
auto requestString = QString("{\"capeId\":\"%1\"}").arg(m_capeId);
request.setRawHeader("Authorization", QString("Bearer %1").arg(m_session->access_token).toLocal8Bit());
QNetworkReply *rep = ENV.qnam().put(request, requestString.toUtf8());
setStatus(tr("Equipping cape"));
m_reply = std::shared_ptr<QNetworkReply>(rep);
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
void CapeChange::clearCape() {
QNetworkRequest request(QUrl("https://api.minecraftservices.com/minecraft/profile/capes/active"));
auto requestString = QString("{\"capeId\":\"%1\"}").arg(m_capeId);
request.setRawHeader("Authorization", QString("Bearer %1").arg(m_session->access_token).toLocal8Bit());
QNetworkReply *rep = ENV.qnam().deleteResource(request);
setStatus(tr("Removing cape"));
m_reply = std::shared_ptr<QNetworkReply>(rep);
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
void CapeChange::executeTask()
{
if(m_capeId.isEmpty()) {
clearCape();
}
else {
setCape(m_capeId);
}
}
void CapeChange::downloadError(QNetworkReply::NetworkError error)
{
// error happened during download.
qCritical() << "Network error: " << error;
emitFailed(m_reply->errorString());
}
void CapeChange::downloadFinished()
{
// if the download failed
if (m_reply->error() != QNetworkReply::NetworkError::NoError)
{
emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
m_reply.reset();
return;
}
emitSucceeded();
}

View File

@ -0,0 +1,32 @@
#pragma once
#include <QFile>
#include <QtNetwork/QtNetwork>
#include <memory>
#include <minecraft/auth/AuthSession.h>
#include "tasks/Task.h"
class CapeChange : public Task
{
Q_OBJECT
public:
CapeChange(QObject *parent, AuthSessionPtr session, QString capeId);
virtual ~CapeChange() {}
private:
void setCape(QString & cape);
void clearCape();
private:
QString m_capeId;
AuthSessionPtr m_session;
std::shared_ptr<QNetworkReply> m_reply;
protected:
virtual void executeTask();
public slots:
void downloadError(QNetworkReply::NetworkError);
void downloadFinished();
};