aboutsummaryrefslogtreecommitdiff
path: root/launcher/minecraft
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2021-08-20 01:34:32 +0200
committerPetr Mrázek <peterix@gmail.com>2021-08-20 01:34:32 +0200
commit1b68d51da634ddab39fe872fcc28a4f491c0c8a4 (patch)
tree9a22465d682018830635a8929eab3cc8482b1faa /launcher/minecraft
parent94fd9a3535ae9a55c228720858292ed2bb69ff98 (diff)
downloadPrismLauncher-1b68d51da634ddab39fe872fcc28a4f491c0c8a4.tar.gz
PrismLauncher-1b68d51da634ddab39fe872fcc28a4f491c0c8a4.tar.bz2
PrismLauncher-1b68d51da634ddab39fe872fcc28a4f491c0c8a4.zip
NOISSUE add setting capes, tweak missing profile message, fix cape IDs
Diffstat (limited to 'launcher/minecraft')
-rw-r--r--launcher/minecraft/auth/AccountData.cpp78
-rw-r--r--launcher/minecraft/auth/AccountData.h4
-rw-r--r--launcher/minecraft/auth/flows/AuthContext.cpp18
-rw-r--r--launcher/minecraft/services/CapeChange.cpp67
-rw-r--r--launcher/minecraft/services/CapeChange.h32
5 files changed, 155 insertions, 44 deletions
diff --git a/launcher/minecraft/auth/AccountData.cpp b/launcher/minecraft/auth/AccountData.cpp
index 77c73c1b..5c6de9df 100644
--- a/launcher/minecraft/auth/AccountData.cpp
+++ b/launcher/minecraft/auth/AccountData.cpp
@@ -78,8 +78,8 @@ void profileToJSONV3(QJsonObject &parent, MinecraftProfile p, const char * token
QJsonObject out;
out["id"] = QJsonValue(p.id);
out["name"] = QJsonValue(p.name);
- if(p.currentCape != -1) {
- out["cape"] = p.capes[p.currentCape].id;
+ if(!p.currentCape.isEmpty()) {
+ out["cape"] = p.currentCape;
}
{
@@ -155,41 +155,53 @@ MinecraftProfile profileFromJSONV3(const QJsonObject &parent, const char * token
}
}
- auto capesV = tokenObject.value("capes");
- if(!capesV.isArray()) {
- qWarning() << "capes is not an array!";
- return MinecraftProfile();
- }
- auto capesArray = capesV.toArray();
- for(auto capeV: capesArray) {
- if(!capeV.isObject()) {
- qWarning() << "cape is not an object!";
- return MinecraftProfile();
- }
- auto capeObj = capeV.toObject();
- auto idV = capeObj.value("id");
- auto urlV = capeObj.value("url");
- auto aliasV = capeObj.value("alias");
- if(!idV.isString() || !urlV.isString() || !aliasV.isString()) {
- qWarning() << "mandatory skin attributes are missing or of unexpected type";
+ {
+ auto capesV = tokenObject.value("capes");
+ if(!capesV.isArray()) {
+ qWarning() << "capes is not an array!";
return MinecraftProfile();
}
- Cape cape;
- cape.id = idV.toString();
- cape.url = urlV.toString();
- cape.alias = aliasV.toString();
-
- // data for cape is optional.
- auto dataV = capeObj.value("data");
- if(dataV.isString()) {
- // TODO: validate base64
- cape.data = QByteArray::fromBase64(dataV.toString().toLatin1());
+ auto capesArray = capesV.toArray();
+ for(auto capeV: capesArray) {
+ if(!capeV.isObject()) {
+ qWarning() << "cape is not an object!";
+ return MinecraftProfile();
+ }
+ auto capeObj = capeV.toObject();
+ auto idV = capeObj.value("id");
+ auto urlV = capeObj.value("url");
+ auto aliasV = capeObj.value("alias");
+ if(!idV.isString() || !urlV.isString() || !aliasV.isString()) {
+ qWarning() << "mandatory skin attributes are missing or of unexpected type";
+ return MinecraftProfile();
+ }
+ Cape cape;
+ cape.id = idV.toString();
+ cape.url = urlV.toString();
+ cape.alias = aliasV.toString();
+
+ // data for cape is optional.
+ auto dataV = capeObj.value("data");
+ if(dataV.isString()) {
+ // TODO: validate base64
+ cape.data = QByteArray::fromBase64(dataV.toString().toLatin1());
+ }
+ else if (!dataV.isUndefined()) {
+ qWarning() << "cape data is something unexpected";
+ return MinecraftProfile();
+ }
+ out.capes[cape.id] = cape;
}
- else if (!dataV.isUndefined()) {
- qWarning() << "cape data is something unexpected";
- return MinecraftProfile();
+ }
+ // current cape
+ {
+ auto capeV = tokenObject.value("cape");
+ if(capeV.isString()) {
+ auto currentCape = capeV.toString();
+ if(out.capes.contains(currentCape)) {
+ out.currentCape = currentCape;
+ }
}
- out.capes.push_back(cape);
}
out.validity = Katabasis::Validity::Assumed;
return out;
diff --git a/launcher/minecraft/auth/AccountData.h b/launcher/minecraft/auth/AccountData.h
index b2d09cb0..cf58fb76 100644
--- a/launcher/minecraft/auth/AccountData.h
+++ b/launcher/minecraft/auth/AccountData.h
@@ -25,8 +25,8 @@ struct MinecraftProfile {
QString id;
QString name;
Skin skin;
- int currentCape = -1;
- QVector<Cape> capes;
+ QString currentCape;
+ QMap<QString, Cape> capes;
Katabasis::Validity validity = Katabasis::Validity::None;
};
diff --git a/launcher/minecraft/auth/flows/AuthContext.cpp b/launcher/minecraft/auth/flows/AuthContext.cpp
index d6a72208..9754d1a9 100644
--- a/launcher/minecraft/auth/flows/AuthContext.cpp
+++ b/launcher/minecraft/auth/flows/AuthContext.cpp
@@ -576,7 +576,9 @@ void AuthContext::onXBoxProfileDone(
}
void AuthContext::checkResult() {
+ qDebug() << "AuthContext::checkResult called";
if(m_requestsDone != 2) {
+ qDebug() << "Number of ready results:" << m_requestsDone;
return;
}
if(m_mcAuthSucceeded && m_xboxProfileSucceeded) {
@@ -638,10 +640,9 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
break;
}
auto capesArray = obj.value("capes").toArray();
- int i = -1;
- int currentCape = -1;
+
+ QString currentCape;
for(auto cape: capesArray) {
- i++;
auto capeObj = cape.toObject();
Cape capeOut;
if(!getString(capeObj.value("id"), capeOut.id)) {
@@ -652,7 +653,7 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
continue;
}
if(state == "ACTIVE") {
- currentCape = i;
+ currentCape = capeOut.id;
}
if(!getString(capeObj.value("url"), capeOut.url)) {
continue;
@@ -661,8 +662,7 @@ bool parseMinecraftProfile(QByteArray & data, MinecraftProfile &output) {
continue;
}
- // we deal with only the active skin
- output.capes.push_back(capeOut);
+ output.capes[capeOut.id] = capeOut;
}
output.currentCape = currentCape;
output.validity = Katabasis::Validity::Certain;
@@ -692,18 +692,18 @@ void AuthContext::onMinecraftProfileDone(int, QNetworkReply::NetworkError error,
if (error == QNetworkReply::ContentNotFoundError) {
m_data->minecraftProfile = MinecraftProfile();
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;
}
if (error != QNetworkReply::NoError) {
finishActivity();
- changeState(STATE_FAILED_HARD, tr("Profile acquisition failed"));
+ changeState(STATE_FAILED_HARD, tr("Minecraft Java profile acquisition failed."));
return;
}
if(!parseMinecraftProfile(data, m_data->minecraftProfile)) {
m_data->minecraftProfile = MinecraftProfile();
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;
}
doGetSkin();
diff --git a/launcher/minecraft/services/CapeChange.cpp b/launcher/minecraft/services/CapeChange.cpp
new file mode 100644
index 00000000..c1d88d14
--- /dev/null
+++ b/launcher/minecraft/services/CapeChange.cpp
@@ -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();
+}
diff --git a/launcher/minecraft/services/CapeChange.h b/launcher/minecraft/services/CapeChange.h
new file mode 100644
index 00000000..1b6f2f72
--- /dev/null
+++ b/launcher/minecraft/services/CapeChange.h
@@ -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();
+};
+