Nuke and pave the old login system

Also, account list now saves profile lists.
This commit is contained in:
Andrew
2013-11-20 18:31:15 -06:00
parent 03652b01d2
commit abf8408911
11 changed files with 150 additions and 120 deletions

View File

@ -22,7 +22,7 @@
#include "inifile.h"
#include "lists/BaseVersionList.h"
#include "net/LoginTask.h"
#include "logic/auth/MojangAccount.h"
class QDialog;
class BaseUpdate;
@ -153,8 +153,8 @@ public:
/// returns a valid update task if update is needed, NULL otherwise
virtual BaseUpdate *doUpdate() = 0;
/// returns a valid minecraft process, ready for launch
virtual MinecraftProcess *prepareForLaunch(LoginResponse response) = 0;
/// returns a valid minecraft process, ready for launch with the given account.
virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account) = 0;
/// do any necessary cleanups after the instance finishes. also runs before
/// 'prepareForLaunch'

View File

@ -38,6 +38,8 @@ void InstanceLauncher::onTerminated()
void InstanceLauncher::onLoginComplete()
{
// TODO: Fix this.
/*
LoginTask *task = (LoginTask *)QObject::sender();
auto result = task->getResult();
auto instance = MMC->instances()->getInstanceById(instId);
@ -55,6 +57,7 @@ void InstanceLauncher::onLoginComplete()
SLOT(write(QString, MessageLevel::Enum)));
proc->launch();
*/
}
void InstanceLauncher::doLogin(const QString &errorMsg)

View File

@ -50,7 +50,7 @@ BaseUpdate *LegacyInstance::doUpdate()
return new LegacyUpdate(this, this);
}
MinecraftProcess *LegacyInstance::prepareForLaunch(LoginResponse response)
MinecraftProcess *LegacyInstance::prepareForLaunch(MojangAccountPtr account)
{
MinecraftProcess *proc = new MinecraftProcess(this);
@ -103,8 +103,8 @@ MinecraftProcess *LegacyInstance::prepareForLaunch(LoginResponse response)
#endif
args << "-jar" << LAUNCHER_FILE;
args << response.player_name;
args << response.session_id;
args << account->currentProfile()->name();
args << account->accessToken();
args << windowTitle;
args << windowSize;
args << lwjgl;

View File

@ -80,7 +80,7 @@ public:
virtual void setShouldUpdate(bool val);
virtual BaseUpdate *doUpdate();
virtual MinecraftProcess *prepareForLaunch(LoginResponse response);
virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account);
virtual void cleanupAfterRun();
virtual QDialog *createModEditDialog(QWidget *parent);
@ -93,4 +93,4 @@ public:
protected
slots:
virtual void jarModsChanged();
};
};

View File

@ -65,7 +65,7 @@ QString replaceTokensIn(QString text, QMap<QString, QString> with)
return result;
}
QStringList OneSixInstance::processMinecraftArgs(LoginResponse response)
QStringList OneSixInstance::processMinecraftArgs(MojangAccountPtr account)
{
I_D(OneSixInstance);
auto version = d->version;
@ -73,11 +73,11 @@ QStringList OneSixInstance::processMinecraftArgs(LoginResponse response)
QMap<QString, QString> token_mapping;
// yggdrasil!
token_mapping["auth_username"] = response.username;
token_mapping["auth_session"] = response.session_id;
token_mapping["auth_access_token"] = response.access_token;
token_mapping["auth_player_name"] = response.player_name;
token_mapping["auth_uuid"] = response.player_id;
token_mapping["auth_username"] = account->username();
//token_mapping["auth_session"] = response.session_id;
token_mapping["auth_access_token"] = account->accessToken();
token_mapping["auth_player_name"] = account->currentProfile()->name();
token_mapping["auth_uuid"] = account->currentProfile()->id();
// this is for offline?:
/*
@ -102,7 +102,7 @@ QStringList OneSixInstance::processMinecraftArgs(LoginResponse response)
return parts;
}
MinecraftProcess *OneSixInstance::prepareForLaunch(LoginResponse response)
MinecraftProcess *OneSixInstance::prepareForLaunch(MojangAccountPtr account)
{
I_D(OneSixInstance);
cleanupAfterRun();
@ -169,7 +169,7 @@ MinecraftProcess *OneSixInstance::prepareForLaunch(LoginResponse response)
args << classPath;
}
args << version->mainClass;
args.append(processMinecraftArgs(response));
args.append(processMinecraftArgs(account));
// Set the width and height for 1.6 instances
bool maximize = settings().get("LaunchMaximized").toBool();

View File

@ -40,7 +40,7 @@ public:
virtual QString instanceConfigFolder() const;
virtual BaseUpdate *doUpdate();
virtual MinecraftProcess *prepareForLaunch(LoginResponse response);
virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account);
virtual void cleanupAfterRun();
virtual QString intendedVersionId() const;
@ -72,5 +72,5 @@ public:
virtual QString getStatusbarDescription();
private:
QStringList processMinecraftArgs(LoginResponse response);
};
QStringList processMinecraftArgs(MojangAccountPtr account);
};

View File

@ -18,6 +18,8 @@
#include "MojangAccount.h"
#include <QUuid>
#include <QJsonObject>
#include <QJsonArray>
#include <logger/QsLog.h>
@ -89,7 +91,12 @@ const QList<AccountProfile> MojangAccount::profiles() const
const AccountProfile* MojangAccount::currentProfile() const
{
if (m_currentProfile < 0)
return nullptr;
{
if (m_profiles.length() > 0)
return &m_profiles.at(0);
else
return nullptr;
}
else
return &m_profiles.at(m_currentProfile);
}
@ -128,9 +135,36 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object)
QString clientToken = object.value("clientToken").toString("");
QString accessToken = object.value("accessToken").toString("");
// TODO: Load profiles?
QJsonArray profileArray = object.value("profiles").toArray();
if (profileArray.size() < 1)
{
QLOG_ERROR() << "Can't load Mojang account with username \"" << username << "\". No profiles found.";
return nullptr;
}
return MojangAccountPtr(new MojangAccount(username, clientToken, accessToken));
ProfileList profiles;
for (QJsonValue profileVal : profileArray)
{
QJsonObject profileObject = profileVal.toObject();
QString id = profileObject.value("id").toString("");
QString name = profileObject.value("name").toString("");
if (id.isEmpty() || name.isEmpty())
{
QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name.";
continue;
}
profiles.append(AccountProfile(id, name));
}
MojangAccountPtr account(new MojangAccount(username, clientToken, accessToken));
account->loadProfiles(profiles);
// Get the currently selected profile.
QString currentProfile = object.value("activeProfile").toString("");
if (!currentProfile.isEmpty())
account->setProfile(currentProfile);
return account;
}
QJsonObject MojangAccount::saveToJson()
@ -139,7 +173,20 @@ QJsonObject MojangAccount::saveToJson()
json.insert("username", username());
json.insert("clientToken", clientToken());
json.insert("accessToken", accessToken());
// TODO: Save profiles?
QJsonArray profileArray;
for (AccountProfile profile : m_profiles)
{
QJsonObject profileObj;
profileObj.insert("id", profile.id());
profileObj.insert("name", profile.name());
profileArray.append(profileObj);
}
json.insert("profiles", profileArray);
if (currentProfile() != nullptr)
json.insert("activeProfile", currentProfile()->id());
return json;
}

View File

@ -123,7 +123,7 @@ public:
/**
* Returns a pointer to the currently selected profile.
* If no profile is selected, returns nullptr.
* If no profile is selected, returns the first profile in the profile list or nullptr if there are none.
*/
const AccountProfile* currentProfile() const;

View File

@ -185,7 +185,7 @@ bool MojangAccountList::loadList(const QString& filePath)
// TODO: We should probably report this error to the user.
if (!file.open(QIODevice::ReadOnly))
{
QLOG_ERROR() << "Failed to read the account list file (" << path << ").";
QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8();
return false;
}
@ -217,9 +217,9 @@ bool MojangAccountList::loadList(const QString& filePath)
// Make sure the format version matches.
if (root.value("formatVersion").toVariant().toInt() != ACCOUNT_LIST_FORMAT_VERSION)
{
QString newName = "accountlist-old.json";
QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to \""
<< newName << "\".";
QString newName = "accounts-old.json";
QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to"
<< newName;
// Attempt to rename the old version.
file.rename(newName);
@ -257,7 +257,7 @@ bool MojangAccountList::saveList(const QString& filePath)
return false;
}
QLOG_INFO() << "Writing account list to \"" << path << "\"...";
QLOG_INFO() << "Writing account list to" << path;
QLOG_DEBUG() << "Building JSON data structure.";
// Build the JSON document to write to the list file.
@ -289,7 +289,7 @@ bool MojangAccountList::saveList(const QString& filePath)
// TODO: We should probably report this error to the user.
if (!file.open(QIODevice::WriteOnly))
{
QLOG_ERROR() << "Failed to read the account list file (" << path << ").";
QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8();
return false;
}
@ -297,7 +297,7 @@ bool MojangAccountList::saveList(const QString& filePath)
file.write(doc.toJson());
file.close();
QLOG_INFO() << "Saved account list to \"" << path << "\".";
QLOG_INFO() << "Saved account list to" << path;
return true;
}