Skip to content

Commit

Permalink
UltimMC: Actually make profile for local accounts, fixes #334
Browse files Browse the repository at this point in the history
  • Loading branch information
Neptune650 committed Mar 8, 2024
1 parent 17a06d8 commit 0b4b9a1
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 57 deletions.
6 changes: 5 additions & 1 deletion launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ set(MINECRAFT_SOURCES
minecraft/auth/Yggdrasil.h

minecraft/auth/providers/BaseAuthProvider.h
minecraft/auth/providers/DummyAuthProvider.h
minecraft/auth/providers/LocalAuthProvider.h
minecraft/auth/providers/ElybyAuthProvider.h
minecraft/auth/providers/MojangAuthProvider.h
minecraft/auth/providers/MicrosoftAuthProvider.h
Expand All @@ -231,6 +231,8 @@ set(MINECRAFT_SOURCES
minecraft/auth/flows/Mojang.h
minecraft/auth/flows/MSA.cpp
minecraft/auth/flows/MSA.h
minecraft/auth/flows/Local.cpp
minecraft/auth/flows/Local.h

minecraft/auth/steps/EntitlementsStep.cpp
minecraft/auth/steps/EntitlementsStep.h
Expand All @@ -246,6 +248,8 @@ set(MINECRAFT_SOURCES
minecraft/auth/steps/MinecraftProfileStep.h
minecraft/auth/steps/MSAStep.cpp
minecraft/auth/steps/MSAStep.h
minecraft/auth/steps/LocalStep.cpp
minecraft/auth/steps/LocalStep.h
minecraft/auth/steps/XboxAuthorizationStep.cpp
minecraft/auth/steps/XboxAuthorizationStep.h
minecraft/auth/steps/XboxProfileStep.cpp
Expand Down
78 changes: 77 additions & 1 deletion launcher/LaunchController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,89 @@ void LaunchController::login() {
m_session->wants_online = m_online;
m_accountToUse->fillSession(m_session);

if (m_accountToUse->typeString() == "local" || m_accountToUse->typeString() == "elyby") {
launchInstance();
return;
}

switch(m_accountToUse->accountState()) {
case AccountState::Offline: {
m_session->wants_online = false;
// NOTE: fallthrough is intentional
}
case AccountState::Online: {
launchInstance();
if(!m_session->wants_online) {
QString usedname;
if(m_offlineName.isEmpty()) {
// we ask the user for a player name
bool ok = false;
QString lastOfflinePlayerName = APPLICATION->settings()->get("LastOfflinePlayerName").toString();
usedname = lastOfflinePlayerName.isEmpty() ? m_session->player_name : lastOfflinePlayerName;
QString name = QInputDialog::getText(
m_parentWidget,
tr("Player name"),
tr("Choose your offline mode player name."),
QLineEdit::Normal,
usedname,
&ok
);
if (!ok)
{
tryagain = false;
break;
}
if (name.length())
{
usedname = name;
APPLICATION->settings()->set("LastOfflinePlayerName", usedname);
}
}
else {
usedname = m_offlineName;
}

m_session->MakeOffline(usedname);
// offline flavored game from here :3
}
if(m_accountToUse->ownsMinecraft()) {
if(!m_accountToUse->hasProfile()) {
// Now handle setting up a profile name here...
ProfileSetupDialog dialog(m_accountToUse, m_parentWidget);
if (dialog.exec() == QDialog::Accepted)
{
tryagain = true;
continue;
}
else
{
emitFailed(tr("Received undetermined session status during login."));
return;
}
}
// we own Minecraft, there is a profile, it's all ready to go!
launchInstance();
return;
}
else {
// play demo ?
QMessageBox box(m_parentWidget);
box.setWindowTitle(tr("Play demo?"));
box.setText(tr("This account does not own Minecraft.\nYou need to purchase the game first to play it.\n\nDo you want to play the demo?"));
box.setIcon(QMessageBox::Warning);
auto demoButton = box.addButton(tr("Play Demo"), QMessageBox::ButtonRole::YesRole);
auto cancelButton = box.addButton(tr("Cancel"), QMessageBox::ButtonRole::NoRole);
box.setDefaultButton(cancelButton);

box.exec();
if(box.clickedButton() == demoButton) {
// play demo here
m_session->MakeDemo();
launchInstance();
}
else {
emitFailed(tr("Launch cancelled - account does not own Minecraft."));
}
}
return;
}
case AccountState::Errored:
Expand Down
21 changes: 9 additions & 12 deletions launcher/minecraft/MinecraftInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,6 @@ QStringList MinecraftInstance::javaArguments() const
{
QStringList args;

if (m_injector) {
args.append(m_injector->javaArg);
}

// custom args go first. we want to override them if we have our own here.
args.append(extraArguments());

Expand Down Expand Up @@ -370,6 +366,11 @@ QStringList MinecraftInstance::javaArguments() const

args << "-Duser.language=en";

if (m_injector) {
args << m_injector->javaArg;
args << "-Dauthlibinjector.noShowServerName";
}

return args;
}

Expand Down Expand Up @@ -916,15 +917,11 @@ shared_qobject_ptr<LaunchTask> MinecraftInstance::createLaunchTask(AuthSessionPt
process->appendStep(step);
}

// if we aren't in offline mode,.
/*if(session->status != AuthSession::PlayableOffline)
{
process->appendStep(new ClaimAccount(pptr, session));
}*/

// do update only if we're in online mode
if (session->wants_online)
if(session->status != AuthSession::PlayableOffline)
{
if(!session->demo) {
process->appendStep(new ClaimAccount(pptr, session));
}
process->appendStep(new Update(pptr, Net::Mode::Online));
}
else
Expand Down
28 changes: 22 additions & 6 deletions launcher/minecraft/auth/AccountData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,18 @@ bool AccountData::resumeStateFromV3(QJsonObject data) {
if(typeS == "MSA") {
type = AccountType::MSA;
provider = AuthProviders::lookup("MSA");
} else {
} else if (typeS == "Mojang"){
type = AccountType::Mojang;
provider = AuthProviders::lookup(typeS);
provider = AuthProviders::lookup("mojang");
} else if (typeS == "Local") {
type = AccountType::Local;
provider = AuthProviders::lookup("local");
} else if (typeS == "Elyby") {
type = AccountType::Elyby;
provider = AuthProviders::lookup("elyby");
} else {
qWarning() << "Failed to parse account data: type is not recognized.";
return false;
}

if(type == AccountType::Mojang) {
Expand Down Expand Up @@ -349,7 +358,7 @@ bool AccountData::resumeStateFromV3(QJsonObject data) {
QJsonObject AccountData::saveState() const {
QJsonObject output;
if(type == AccountType::Mojang) {
output["type"] = provider->id();
output["type"] = "Mojang";
if(legacy) {
output["legacy"] = true;
}
Expand All @@ -366,6 +375,10 @@ QJsonObject AccountData::saveState() const {
tokenToJSONV3(output, userToken, "utoken");
tokenToJSONV3(output, xboxApiToken, "xrp-main");
tokenToJSONV3(output, mojangservicesToken, "xrp-mc");
} else if (type == AccountType::Local) {
output["type"] = "Local";
} else if (type == AccountType::Elyby) {
output["type"] = "Elyby";
}

tokenToJSONV3(output, yggdrasilToken, "ygg");
Expand Down Expand Up @@ -419,8 +432,7 @@ QString AccountData::profileId() const {

QString AccountData::profileName() const {
if(minecraftProfile.name.size() == 0) {
// Fix for too long of a name
return QObject::tr("%1").arg(accountDisplayString());
return QObject::tr("No profile (%1)").arg(accountDisplayString());
}
else {
return minecraftProfile.name;
Expand All @@ -429,7 +441,8 @@ QString AccountData::profileName() const {

QString AccountData::accountDisplayString() const {
switch(type) {
case AccountType::Mojang: {
case AccountType::Mojang:
case AccountType::Elyby: {
return userName();
}
case AccountType::MSA: {
Expand All @@ -438,6 +451,9 @@ QString AccountData::accountDisplayString() const {
}
return "Xbox profile missing";
}
case AccountType::Local: {
return "<Local>";
}
default: {
return "Invalid Account";
}
Expand Down
4 changes: 3 additions & 1 deletion launcher/minecraft/auth/AccountData.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ struct MinecraftProfile {

enum class AccountType {
MSA,
Mojang
Mojang,
Local,
Elyby
};

enum class AccountState {
Expand Down
4 changes: 2 additions & 2 deletions launcher/minecraft/auth/AuthProviders.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "AuthProviders.h"
#include "providers/ElybyAuthProvider.h"
#include "providers/DummyAuthProvider.h"
#include "providers/LocalAuthProvider.h"
#include "providers/MojangAuthProvider.h"
#include "providers/MicrosoftAuthProvider.h"
#include "../../AuthServer.h"
Expand All @@ -19,7 +19,7 @@ namespace AuthProviders
void load(std::shared_ptr<AuthServer> authserver)
{
REGISTER_AUTH_PROVIDER(ElybyAuthProvider);
REGISTER_AUTH_PROVIDER(DummyAuthProvider);
REGISTER_AUTH_PROVIDER(LocalAuthProvider);
REGISTER_AUTH_PROVIDER(MojangAuthProvider);
REGISTER_AUTH_PROVIDER(MicrosoftAuthProvider);
}
Expand Down
72 changes: 71 additions & 1 deletion launcher/minecraft/auth/MinecraftAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
#include "AuthProviders.h"
#include "flows/MSA.h"
#include "flows/Mojang.h"
#include "flows/Local.h"

MinecraftAccount::MinecraftAccount(QObject* parent) : QObject(parent) {
data.internalId = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
}


MinecraftAccountPtr MinecraftAccount::loadFromJsonV2(const QJsonObject& json) {
MinecraftAccountPtr account(new MinecraftAccount());
if(account->data.resumeStateFromV2(json)) {
Expand All @@ -58,7 +60,62 @@ MinecraftAccountPtr MinecraftAccount::createFromUsername(const QString &username
account->data.type = AccountType::Mojang;
account->data.yggdrasilToken.extra["userName"] = username;
account->data.yggdrasilToken.extra["clientToken"] = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
return account;
}

// Taken from Prism Launcher, just for compatibility with their UUIDs
static QUuid uuidFromUsername(QString username)
{
auto input = QString("OfflinePlayer:%1").arg(username).toUtf8();

// basically a reimplementation of Java's UUID#nameUUIDFromBytes
QByteArray digest = QCryptographicHash::hash(input, QCryptographicHash::Md5);

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto bOr = [](QByteArray& array, int index, char value) { array[index] = array.at(index) | value; };
auto bAnd = [](QByteArray& array, int index, char value) { array[index] = array.at(index) & value; };
#else
auto bOr = [](QByteArray& array, qsizetype index, char value) { array[index] |= value; };
auto bAnd = [](QByteArray& array, qsizetype index, char value) { array[index] &= value; };
#endif
bAnd(digest, 6, (char)0x0f); // clear version
bOr(digest, 6, (char)0x30); // set to version 3
bAnd(digest, 8, (char)0x3f); // clear variant
bOr(digest, 8, (char)0x80); // set to IETF variant

return QUuid::fromRfc4122(digest);
}

MinecraftAccountPtr MinecraftAccount::createLocal(const QString &username)
{
MinecraftAccountPtr account = new MinecraftAccount();
account->data.type = AccountType::Local;
account->data.yggdrasilToken.validity = Katabasis::Validity::Certain;
account->data.yggdrasilToken.issueInstant = QDateTime::currentDateTimeUtc();
account->data.yggdrasilToken.extra["userName"] = username;
account->data.yggdrasilToken.extra["clientToken"] = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
account->data.minecraftProfile.id = uuidFromUsername(username).toString().remove(QRegExp("[{}-]"));
account->data.minecraftProfile.id = account->data.internalId;
account->data.minecraftProfile.name = username;
account->data.minecraftProfile.validity = Katabasis::Validity::Certain;
account->data.minecraftEntitlement.ownsMinecraft = true;
account->data.minecraftEntitlement.canPlayMinecraft = true;
return account;
}

MinecraftAccountPtr MinecraftAccount::createElyby(const QString &username)
{
MinecraftAccountPtr account = new MinecraftAccount();
account->data.type = AccountType::Elyby;
account->data.yggdrasilToken.extra["userName"] = username;
account->data.yggdrasilToken.extra["clientToken"] = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
account->data.minecraftProfile.id = account->data.internalId;
account->data.minecraftProfile.name = username;
account->data.minecraftProfile.validity = Katabasis::Validity::Certain;
account->data.validity_ = Katabasis::Validity::Certain;
account->data.minecraftEntitlement.ownsMinecraft = true;
account->data.minecraftEntitlement.canPlayMinecraft = true;
account->data.minecraftEntitlement.validity = Katabasis::Validity::Certain;
return account;
}

Expand Down Expand Up @@ -113,6 +170,16 @@ shared_qobject_ptr<AccountTask> MinecraftAccount::loginMSA() {
return m_currentTask;
}

shared_qobject_ptr<AccountTask> MinecraftAccount::loginLocal() {
Q_ASSERT(m_currentTask.get() == nullptr);

m_currentTask.reset(new LocalLogin(&data));
connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
emit activityChanged(true);
return m_currentTask;
}

shared_qobject_ptr<AccountTask> MinecraftAccount::refresh() {
if(m_currentTask) {
return m_currentTask;
Expand All @@ -121,9 +188,12 @@ shared_qobject_ptr<AccountTask> MinecraftAccount::refresh() {
if(data.type == AccountType::MSA) {
m_currentTask.reset(new MSASilent(&data));
}
else {
else if (data.type == AccountType::Mojang) {
m_currentTask.reset(new MojangRefresh(&data));
}
else if (data.type == AccountType::Local) {
m_currentTask.reset(new LocalRefresh(&data));
}

connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
Expand Down
Loading

0 comments on commit 0b4b9a1

Please sign in to comment.