Various task related improvements.

* Errors are reported back to task users via Failure signals.
* Lwjgl doesn't download on each legacy instance start anymore.
* Tasks were unified when it comes to success/failure.
* Task dialogs don't get spawned after short tasks finish anymore.
This commit is contained in:
Petr Mrázek
2013-08-09 00:26:35 +02:00
parent c8925e0f66
commit bf5f5091ef
16 changed files with 122 additions and 148 deletions

View File

@ -5,11 +5,6 @@ BaseUpdate::BaseUpdate ( BaseInstance* inst, QObject* parent ) : Task ( parent )
m_inst = inst;
}
void BaseUpdate::error ( const QString& msg )
{
emit gameUpdateError(msg);
}
void BaseUpdate::updateDownloadProgress(qint64 current, qint64 total)
{
// The progress on the current file is current / total

View File

@ -38,20 +38,8 @@ public:
virtual void executeTask() = 0;
signals:
/*!
* \brief Signal emitted when the game update is complete.
*/
void gameUpdateComplete();
/*!
* \brief Signal emitted if an error occurrs during the update.
* \param errorMsg An error message to be displayed to the user.
*/
void gameUpdateError(const QString &errorMsg);
protected slots:
virtual void error(const QString &msg);
//virtual void error(const QString &msg);
void updateDownloadProgress(qint64 current, qint64 total);
protected:

View File

@ -18,21 +18,33 @@ void LegacyUpdate::executeTask()
void LegacyUpdate::lwjglStart()
{
LegacyInstance * inst = (LegacyInstance *) m_inst;
lwjglVersion = inst->lwjglVersion();
lwjglTargetPath = PathCombine("lwjgl", lwjglVersion );
lwjglNativesPath = PathCombine( lwjglTargetPath, "natives/");
// if the 'done' file exists, we don't have to download this again
QFileInfo doneFile(PathCombine(lwjglTargetPath, "done"));
if(doneFile.exists())
{
emitSucceeded();
return;
}
auto &list = LWJGLVersionList::get();
if(!list.isLoaded())
{
error("Too soon! Let the LWJGL list load :)");
emitEnded();
emitFailed("Too soon! Let the LWJGL list load :)");
return;
}
QString lwjglVer = inst->lwjglVersion();
auto version = list.getVersion(lwjglVer);
auto version = list.getVersion(lwjglVersion);
if(!version)
{
error("Game update failed: the selected LWJGL version is invalid.");
emitEnded();
emitFailed("Game update failed: the selected LWJGL version is invalid.");
return;
}
lwjglVersion = version->name();
QString url = version->url();
QUrl realUrl(url);
QString hostname = realUrl.host();
@ -56,8 +68,9 @@ void LegacyUpdate::lwjglFinished(QNetworkReply* reply)
}
if(reply->error() != QNetworkReply::NoError)
{
error("Failed to download: " + reply->errorString() + "\nSometimes you have to wait a bit if you download many LWJGL versions in a row. YMMV");
emitEnded();
emitFailed( "Failed to download: "+
reply->errorString()+
"\nSometimes you have to wait a bit if you download many LWJGL versions in a row. YMMV");
return;
}
auto &worker = NetWorker::spawn();
@ -95,22 +108,19 @@ void LegacyUpdate::lwjglFinished(QNetworkReply* reply)
void LegacyUpdate::extractLwjgl()
{
// make sure the directories are there
QString lwjgl_base = PathCombine("lwjgl", lwjglVersion );
QString nativesPath = PathCombine( lwjgl_base, "natives/");
bool success = ensurePathExists(nativesPath);
bool success = ensurePathExists(lwjglNativesPath);
if(!success)
{
error("Failed to extract the lwjgl libs - error when creating required folders.");
emitEnded();
emitFailed("Failed to extract the lwjgl libs - error when creating required folders.");
return;
}
QuaZip zip("lwjgl.zip");
if(!zip.open(QuaZip::mdUnzip))
{
error("Failed to extract the lwjgl libs - not a valid archive.");
emitEnded();
emitFailed("Failed to extract the lwjgl libs - not a valid archive.");
return;
}
@ -122,8 +132,7 @@ void LegacyUpdate::extractLwjgl()
if(!file.open(QIODevice::ReadOnly))
{
zip.close();
error("Failed to extract the lwjgl libs - error while reading archive.");
emitEnded();
emitFailed("Failed to extract the lwjgl libs - error while reading archive.");
return;
}
QuaZipFileInfo info;
@ -139,7 +148,7 @@ void LegacyUpdate::extractLwjgl()
{
if (name.endsWith(jarNames[i]))
{
destFileName = PathCombine(lwjgl_base, jarNames[i]);
destFileName = PathCombine(lwjglTargetPath, jarNames[i]);
}
}
// Not found? look for the natives
@ -160,7 +169,7 @@ void LegacyUpdate::extractLwjgl()
name = name.mid(lastSlash+1);
else if(lastBackSlash != -1)
name = name.mid(lastBackSlash+1);
destFileName = PathCombine(nativesPath, name);
destFileName = PathCombine(lwjglNativesPath, name);
}
}
// Now if destFileName is still empty, go to the next file.
@ -176,13 +185,15 @@ void LegacyUpdate::extractLwjgl()
}
zip.close();
m_reply.clear();
emit gameUpdateComplete();
emitEnded();
QFile doneFile(PathCombine(lwjglTargetPath, "done"));
doneFile.open(QIODevice::WriteOnly);
doneFile.write("done.");
doneFile.close();
emitSucceeded();
}
void LegacyUpdate::lwjglFailed()
{
error("Bad stuff happened while trying to get the lwjgl libs...");
emitEnded();
emitFailed("Bad stuff happened while trying to get the lwjgl libs...");
}

View File

@ -47,6 +47,9 @@ private:
// MinecraftVersion *targetVersion;
QString lwjglURL;
QString lwjglVersion;
QString lwjglTargetPath;
QString lwjglNativesPath;
};

View File

@ -43,7 +43,7 @@ void OneSixUpdate::executeTask()
if(targetVersion == nullptr)
{
// don't do anything if it was invalid
emit gameUpdateComplete();
emitSucceeded();
return;
}
@ -104,8 +104,7 @@ void OneSixUpdate::versionFileFinished()
void OneSixUpdate::versionFileFailed()
{
error("Failed to download the version description. Try again.");
emitEnded();
emitFailed("Failed to download the version description. Try again.");
}
void OneSixUpdate::jarlibStart()
@ -114,8 +113,7 @@ void OneSixUpdate::jarlibStart()
bool successful = inst->reloadFullVersion();
if(!successful)
{
error("Failed to load the version description file (version.json). It might be corrupted, missing or simply too new.");
emitEnded();
emitFailed("Failed to load the version description file (version.json). It might be corrupted, missing or simply too new.");
return;
}
@ -149,13 +147,11 @@ void OneSixUpdate::jarlibStart()
void OneSixUpdate::jarlibFinished()
{
emit gameUpdateComplete();
emitEnded();
emitSucceeded();
}
void OneSixUpdate::jarlibFailed()
{
error("Failed to download the binary garbage. Try again. Maybe. IF YOU DARE");
emitEnded();
emitFailed("Failed to download the binary garbage. Try again. Maybe. IF YOU DARE");
}

View File

@ -119,8 +119,7 @@ void LWJGLVersionList::netRequestComplete()
int errorLine;
if (!doc.setContent(reply->readAll(), false, &xmlErrorMsg, &errorLine))
{
failed("Failed to load LWJGL list. XML error: " + xmlErrorMsg +
" at line " + QString::number(errorLine));
failed("Failed to load LWJGL list. XML error: " + xmlErrorMsg + " at line " + QString::number(errorLine));
setLoading(false);
return;
}

View File

@ -179,9 +179,8 @@ void MCVListLoadTask::list_downloaded()
{
if(vlistReply->error() != QNetworkReply::QNetworkReply::NoError)
{
qDebug() << "Failed to load Minecraft main version list" << vlistReply->errorString();
vlistReply->deleteLater();
emitEnded();
emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
return;
}
@ -191,15 +190,13 @@ void MCVListLoadTask::list_downloaded()
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "Error parsing version list JSON:" << jsonError.errorString();
emitEnded();
emitFailed("Error parsing version list JSON:" + jsonError.errorString());
return;
}
if(!jsonDoc.isObject())
{
qDebug() << "Error parsing version list JSON: " << "jsonDoc is not an object";
emitEnded();
emitFailed("Error parsing version list JSON: jsonDoc is not an object");
return;
}
@ -208,8 +205,7 @@ void MCVListLoadTask::list_downloaded()
// Get the ID of the latest release and the latest snapshot.
if(!root.value("latest").isObject())
{
qDebug() << "Error parsing version list JSON: " << "version list is missing 'latest' object";
emitEnded();
emitFailed("Error parsing version list JSON: version list is missing 'latest' object");
return;
}
@ -219,22 +215,19 @@ void MCVListLoadTask::list_downloaded()
QString latestSnapshotID = latest.value("snapshot").toString("");
if(latestReleaseID.isEmpty())
{
qDebug() << "Error parsing version list JSON: " << "latest release field is missing";
emitEnded();
emitFailed("Error parsing version list JSON: latest release field is missing");
return;
}
if(latestSnapshotID.isEmpty())
{
qDebug() << "Error parsing version list JSON: " << "latest snapshot field is missing";
emitEnded();
emitFailed("Error parsing version list JSON: latest snapshot field is missing");
return;
}
// Now, get the array of versions.
if(!root.value("versions").isArray())
{
qDebug() << "Error parsing version list JSON: " << "version list object is missing 'versions' array";
emitEnded();
emitFailed("Error parsing version list JSON: version list object is missing 'versions' array");
return;
}
QJsonArray versions = root.value("versions").toArray();
@ -308,7 +301,7 @@ void MCVListLoadTask::list_downloaded()
#ifdef PRINT_VERSIONS
m_list->printToStdOut();
#endif
emitEnded();
emitSucceeded();
return;
}

View File

@ -71,44 +71,41 @@ void LoginTask::processNetReply(QNetworkReply *reply)
QString username = strings[2];
QString sessionID = strings[3];
LoginResponse response{username, sessionID, latestVersion};
emit loginComplete(response);
result = {username, sessionID, latestVersion};
emitSucceeded();
}
else
{
emit loginFailed("Failed to parse Minecraft version string.");
emitFailed("Failed to parse Minecraft version string.");
}
}
else
{
if (responseStr.toLower() == "bad login")
emit loginFailed("Invalid username or password.");
emitFailed("Invalid username or password.");
else if (responseStr.toLower() == "old version")
emit loginFailed("Launcher outdated, please update.");
emitFailed("Launcher outdated, please update.");
else
emit loginFailed("Login failed: " + responseStr);
emitFailed("Login failed: " + responseStr);
}
}
else if (responseCode == 503)
{
emit loginFailed("The login servers are currently unavailable. "
"Check http://help.mojang.com/ for more info.");
emitFailed("The login servers are currently unavailable. Check http://help.mojang.com/ for more info.");
}
else
{
emit loginFailed(QString("Login failed: Unknown HTTP error %1 occurred.").
arg(QString::number(responseCode)));
emitFailed(QString("Login failed: Unknown HTTP error %1 occurred.").arg(QString::number(responseCode)));
}
break;
}
case QNetworkReply::OperationCanceledError:
emit loginFailed("Login canceled.");
emitFailed("Login canceled.");
break;
default:
emit loginFailed("Login failed: " + reply->errorString());
emitFailed("Login failed: " + reply->errorString());
break;
}
emitEnded();
}

View File

@ -40,17 +40,18 @@ class LIBMULTIMC_EXPORT LoginTask : public Task
Q_OBJECT
public:
explicit LoginTask(const UserInfo& uInfo, QObject *parent = 0);
LoginResponse getResult()
{
return result;
};
public slots:
protected slots:
void processNetReply(QNetworkReply* reply);
signals:
void loginComplete(LoginResponse loginResponse);
void loginFailed(const QString& errorMsg);
protected:
void executeTask();
LoginResponse result;
QNetworkReply* netReply;
UserInfo uInfo;
};

View File

@ -37,11 +37,6 @@ int Task::getProgress() const
return progress;
}
void Task::calcProgress(int parts, int whole)
{
setProgress((int)((((float)parts) / ((float)whole))*100)); // Not sure if C++ or LISP...
}
void Task::setProgress(int progress)
{
this->progress = progress;
@ -58,16 +53,21 @@ void Task::emitStarted()
{
running = true;
emit started();
emit started(this);
}
void Task::emitEnded()
void Task::emitFailed(QString reason)
{
running = false;
emit ended();
emit ended(this);
emit failed(reason);
}
void Task::emitSucceeded()
{
running = false;
emit succeeded();
}
bool Task::isRunning() const
{
return running;

View File

@ -27,33 +27,21 @@ class LIBMULTIMC_EXPORT Task : public QObject
public:
explicit Task(QObject *parent = 0);
// Starts the task.
void startTask();
QString getStatus() const;
int getProgress() const;
bool isRunning() const;
/*!
* \brief Calculates and sets the task's progress based on the number of parts completed out of the total number to complete.
* This is essentially just shorthand for setProgress((parts / whole) * 100);
* \param parts The parts out of the whole completed. This parameter should
* be less than whole. If it is greater than whole, progress is set to 100.
* \param whole The total number of things that need to be completed.
*/
void calcProgress(int parts, int whole);
public slots:
void startTask();
protected slots:
void setStatus(const QString& status);
void setProgress(int progress);
signals:
void started(Task* task);
void ended(Task* task);
void started();
void ended();
void failed(QString reason);
void succeeded();
void statusChanged(Task* task, const QString& status);
void progressChanged(Task* task, int progress);
@ -65,7 +53,8 @@ protected:
virtual void executeTask() = 0;
virtual void emitStarted();
virtual void emitEnded();
virtual void emitFailed(QString reason);
virtual void emitSucceeded();
virtual void emitStatusChange(const QString &status);
virtual void emitProgressChange(int progress);