GH-3012 add --offline and --name arguments

These allow launching offline with a specified name from the command line.
This commit is contained in:
Petr Mrázek 2022-06-09 23:46:28 +02:00
parent 8235752dec
commit c7256744c6
4 changed files with 99 additions and 36 deletions

View File

@ -247,6 +247,14 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
parser.addOption("profile"); parser.addOption("profile");
parser.addShortOpt("profile", 'a'); parser.addShortOpt("profile", 'a');
parser.addDocumentation("profile", "Use the account specified by its profile name (only valid in combination with --launch)"); parser.addDocumentation("profile", "Use the account specified by its profile name (only valid in combination with --launch)");
// --offline
parser.addSwitch("offline");
parser.addShortOpt("offline", 'o');
parser.addDocumentation("offline", "Launch offline (only valid in combination with --launch)");
// --name
parser.addOption("name");
parser.addShortOpt("name", 'n');
parser.addDocumentation("name", "When launching offline, use specified name (only makes sense in combination with --launch and --offline)");
// --alive // --alive
parser.addSwitch("alive"); parser.addSwitch("alive");
parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after the launcher starts"); parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after the launcher starts");
@ -290,6 +298,10 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
m_instanceIdToLaunch = args["launch"].toString(); m_instanceIdToLaunch = args["launch"].toString();
m_serverToJoin = args["server"].toString(); m_serverToJoin = args["server"].toString();
m_profileToUse = args["profile"].toString(); m_profileToUse = args["profile"].toString();
if(args["offline"].toBool()) {
m_offline = true;
m_offlineName = args["name"].toString();
}
m_liveCheck = args["alive"].toBool(); m_liveCheck = args["alive"].toBool();
m_zipToImport = args["import"].toUrl(); m_zipToImport = args["import"].toUrl();
@ -355,18 +367,44 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
return; return;
} }
if(m_instanceIdToLaunch.isEmpty() && !m_serverToJoin.isEmpty()) // all the things invalid when NOT trying to --launch
{ if(m_instanceIdToLaunch.isEmpty()) {
std::cerr << "--server can only be used in combination with --launch!" << std::endl; if(!m_serverToJoin.isEmpty())
m_status = Application::Failed; {
return; std::cerr << "--server can only be used in combination with --launch!" << std::endl;
} m_status = Application::Failed;
return;
}
if(m_instanceIdToLaunch.isEmpty() && !m_profileToUse.isEmpty()) if(!m_profileToUse.isEmpty())
{ {
std::cerr << "--account can only be used in combination with --launch!" << std::endl; std::cerr << "--account can only be used in combination with --launch!" << std::endl;
m_status = Application::Failed; m_status = Application::Failed;
return; return;
}
if(m_offline)
{
std::cerr << "--offline can only be used in combination with --launch!" << std::endl;
m_status = Application::Failed;
return;
}
if(!m_offlineName.isEmpty())
{
std::cerr << "--offlineName can only be used in combination with --launch and --offline!" << std::endl;
m_status = Application::Failed;
return;
}
}
else {
// all the things invalid when trying to --launch
// online, and offline name is set
if(!m_offline && !m_offlineName.isEmpty()) {
std::cerr << "--offlineName can only be used in combination with --launch and --offline!" << std::endl;
m_status = Application::Failed;
return;
}
} }
#if defined(Q_OS_MAC) #if defined(Q_OS_MAC)
@ -473,6 +511,10 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
{ {
launch.args["profile"] = m_profileToUse; launch.args["profile"] = m_profileToUse;
} }
if(m_offline) {
launch.args["offline_enabled"] = "true";
launch.args["offline_name"] = m_offlineName;
}
m_peerInstance->sendMessage(launch.serialize(), timeout); m_peerInstance->sendMessage(launch.serialize(), timeout);
} }
m_status = Application::Succeeded; m_status = Application::Succeeded;
@ -1030,6 +1072,7 @@ void Application::performMainStartupAction()
{ {
MinecraftServerTargetPtr serverToJoin = nullptr; MinecraftServerTargetPtr serverToJoin = nullptr;
MinecraftAccountPtr accountToUse = nullptr; MinecraftAccountPtr accountToUse = nullptr;
bool offline = m_offline;
qDebug() << "<> Instance" << m_instanceIdToLaunch << "launching"; qDebug() << "<> Instance" << m_instanceIdToLaunch << "launching";
if(!m_serverToJoin.isEmpty()) if(!m_serverToJoin.isEmpty())
@ -1048,7 +1091,7 @@ void Application::performMainStartupAction()
qDebug() << " Launching with account" << m_profileToUse; qDebug() << " Launching with account" << m_profileToUse;
} }
launch(inst, true, nullptr, serverToJoin, accountToUse); launch(inst, !offline, nullptr, serverToJoin, accountToUse, m_offlineName);
return; return;
} }
} }
@ -1121,6 +1164,8 @@ void Application::messageReceived(const QByteArray& message)
QString id = received.args["id"]; QString id = received.args["id"];
QString server = received.args["server"]; QString server = received.args["server"];
QString profile = received.args["profile"]; QString profile = received.args["profile"];
bool offline = received.args["offline_enabled"] == "true";
QString offlineName = received.args["offline_name"];
InstancePtr instance; InstancePtr instance;
if(!id.isEmpty()) { if(!id.isEmpty()) {
@ -1151,10 +1196,11 @@ void Application::messageReceived(const QByteArray& message)
launch( launch(
instance, instance,
true, !offline,
nullptr, nullptr,
serverObject, serverObject,
accountObject accountObject,
offlineName
); );
} }
else else
@ -1252,7 +1298,8 @@ bool Application::launch(
bool online, bool online,
BaseProfilerFactory *profiler, BaseProfilerFactory *profiler,
MinecraftServerTargetPtr serverToJoin, MinecraftServerTargetPtr serverToJoin,
MinecraftAccountPtr accountToUse MinecraftAccountPtr accountToUse,
const QString& offlineName
) { ) {
if(m_updateRunning) if(m_updateRunning)
{ {
@ -1276,6 +1323,7 @@ bool Application::launch(
controller->setProfiler(profiler); controller->setProfiler(profiler);
controller->setServerToJoin(serverToJoin); controller->setServerToJoin(serverToJoin);
controller->setAccountToUse(accountToUse); controller->setAccountToUse(accountToUse);
controller->setOfflineName(offlineName);
if(window) if(window)
{ {
controller->setParentWidget(window); controller->setParentWidget(window);

View File

@ -154,7 +154,8 @@ public slots:
bool online = true, bool online = true,
BaseProfilerFactory *profiler = nullptr, BaseProfilerFactory *profiler = nullptr,
MinecraftServerTargetPtr serverToJoin = nullptr, MinecraftServerTargetPtr serverToJoin = nullptr,
MinecraftAccountPtr accountToUse = nullptr MinecraftAccountPtr accountToUse = nullptr,
const QString &offlineName = QString()
); );
bool kill(InstancePtr instance); bool kill(InstancePtr instance);
@ -234,6 +235,8 @@ public:
QString m_instanceIdToLaunch; QString m_instanceIdToLaunch;
QString m_serverToJoin; QString m_serverToJoin;
QString m_profileToUse; QString m_profileToUse;
bool m_offline = false;
QString m_offlineName;
bool m_liveCheck = false; bool m_liveCheck = false;
QUrl m_zipToImport; QUrl m_zipToImport;
std::unique_ptr<QFile> logFile; std::unique_ptr<QFile> logFile;

View File

@ -126,28 +126,35 @@ void LaunchController::login() {
} }
case AccountState::Online: { case AccountState::Online: {
if(!m_session->wants_online) { if(!m_session->wants_online) {
// we ask the user for a player name QString usedname;
bool ok = false; if(m_offlineName.isEmpty()) {
QString lastOfflinePlayerName = APPLICATION->settings()->get("LastOfflinePlayerName").toString(); // we ask the user for a player name
QString usedname = lastOfflinePlayerName.isEmpty() ? m_session->player_name : lastOfflinePlayerName; bool ok = false;
QString name = QInputDialog::getText( QString lastOfflinePlayerName = APPLICATION->settings()->get("LastOfflinePlayerName").toString();
m_parentWidget, usedname = lastOfflinePlayerName.isEmpty() ? m_session->player_name : lastOfflinePlayerName;
tr("Player name"), QString name = QInputDialog::getText(
tr("Choose your offline mode player name."), m_parentWidget,
QLineEdit::Normal, tr("Player name"),
usedname, tr("Choose your offline mode player name."),
&ok QLineEdit::Normal,
); usedname,
if (!ok) &ok
{ );
tryagain = false; if (!ok)
break; {
tryagain = false;
break;
}
if (name.length())
{
usedname = name;
APPLICATION->settings()->set("LastOfflinePlayerName", usedname);
}
} }
if (name.length()) else {
{ usedname = m_offlineName;
usedname = name;
APPLICATION->settings()->set("LastOfflinePlayerName", usedname);
} }
m_session->MakeOffline(usedname); m_session->MakeOffline(usedname);
// offline flavored game from here :3 // offline flavored game from here :3
} }

View File

@ -28,6 +28,10 @@ public:
m_online = online; m_online = online;
} }
void setOfflineName(const QString &offlineName) {
m_offlineName = offlineName;
}
void setProfiler(BaseProfilerFactory *profiler) { void setProfiler(BaseProfilerFactory *profiler) {
m_profiler = profiler; m_profiler = profiler;
} }
@ -66,6 +70,7 @@ private slots:
private: private:
BaseProfilerFactory *m_profiler = nullptr; BaseProfilerFactory *m_profiler = nullptr;
bool m_online = true; bool m_online = true;
QString m_offlineName;
InstancePtr m_instance; InstancePtr m_instance;
QWidget * m_parentWidget = nullptr; QWidget * m_parentWidget = nullptr;
InstanceWindow *m_console = nullptr; InstanceWindow *m_console = nullptr;