From ee5583251d92d47f96b03c3b447c115bab901c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 7 Jul 2013 23:51:26 +0200 Subject: [PATCH] Legacy versions downloaded from the new location are treated as legacy versions! --- libmultimc/include/gameupdatetask.h | 72 +++------- libmultimc/src/gameupdatetask.cpp | 200 ++++++++++++++-------------- libutil/include/dlqueue.h | 2 +- libutil/src/dlqueue.cpp | 12 +- 4 files changed, 124 insertions(+), 162 deletions(-) diff --git a/libmultimc/include/gameupdatetask.h b/libmultimc/include/gameupdatetask.h index c3f84356..f607db6b 100644 --- a/libmultimc/include/gameupdatetask.h +++ b/libmultimc/include/gameupdatetask.h @@ -22,47 +22,15 @@ #include #include +#include "dlqueue.h" #include "task.h" #include "loginresponse.h" #include "instance.h" #include "libmmc_config.h" -class FileToDownload; -typedef QSharedPointer FileToDownloadPtr; - -class FileToDownload : public QObject -{ - Q_OBJECT - - /*! - * The URL to download the file from. - */ - Q_PROPERTY(QUrl url READ url WRITE setURL) - - /*! - * The path to download to. - * This path is relative to the instance's root directory. - */ - Q_PROPERTY(QString path READ path WRITE setPath) - -private: - FileToDownload(const QUrl &url, const QString &path, QObject *parent = 0); -public: - static FileToDownloadPtr Create(const QUrl &url, const QString &path, QObject *parent = 0); - - virtual QUrl url() const { return m_dlURL; } - virtual void setURL(const QUrl &url) { m_dlURL = url; } - - virtual QString path() const { return m_dlPath; } - virtual void setPath(const QString &path) { m_dlPath = path; } - -private: - QUrl m_dlURL; - QString m_dlPath; -}; - +class MinecraftVersion; /*! * The game update task is the task that handles downloading instances' files. @@ -92,9 +60,6 @@ public: virtual void executeTask(); - virtual bool downloadFile(const FileToDownloadPtr file); - - ////////////////////// // STATE AND STATUS // ////////////////////// @@ -110,6 +75,10 @@ public: */ virtual QString getStateMessage(int state); +private: + void getLegacyJar(); + void determineNewVersion(); + public slots: /*! @@ -122,7 +91,12 @@ public slots: private slots: - virtual void updateDownloadProgress(qint64 current, qint64 total); + void updateDownloadProgress(qint64 current, qint64 total); + void legacyJarFinished(); + void legacyJarFailed(); + + void versionFileFinished(); + void versionFileFailed(); signals: /*! @@ -143,23 +117,8 @@ private: /////////// Instance *m_inst; - LoginResponse m_response; - QNetworkAccessManager *netMgr; - - - - //////////////////////// - // FILE DOWNLOAD LIST // - //////////////////////// - - // List of URLs that the game updater will need to download. - QList m_downloadList; - int m_currentDownload; - - - //////////////////////////// // STATE AND STATUS STUFF // //////////////////////////// @@ -184,6 +143,13 @@ private: // Finished StateFinished }; + JobListPtr legacyDownloadJob; + JobListPtr specificVersionDownloadJob; + JobListPtr jarlibDownloadJob; + JobListQueue download_queue; + + // target version, determined during this task + MinecraftVersion *targetVersion; }; diff --git a/libmultimc/src/gameupdatetask.cpp b/libmultimc/src/gameupdatetask.cpp index 49f9335f..fee2aa29 100644 --- a/libmultimc/src/gameupdatetask.cpp +++ b/libmultimc/src/gameupdatetask.cpp @@ -27,25 +27,20 @@ #include "minecraftversionlist.h" #include "pathutils.h" -#include "netutils.h" GameUpdateTask::GameUpdateTask(const LoginResponse &response, Instance *inst, QObject *parent) : Task(parent), m_response(response) { m_inst = inst; m_updateState = StateInit; - m_currentDownload = 0; } void GameUpdateTask::executeTask() { updateStatus(); - QNetworkAccessManager networkMgr; - netMgr = &networkMgr; - // Get a pointer to the version object that corresponds to the instance's version. - MinecraftVersion *targetVersion = (MinecraftVersion *)MinecraftVersionList::getMainList(). + targetVersion = (MinecraftVersion *)MinecraftVersionList::getMainList(). findVersion(m_inst->intendedVersion()); if(targetVersion == NULL) { @@ -55,6 +50,81 @@ void GameUpdateTask::executeTask() return; } + ///////////////////////// + // BUILD DOWNLOAD LIST // + ///////////////////////// + // Build a list of URLs that will need to be downloaded. + + setState(StateDetermineURLs); + + if (targetVersion->launcherVersion() == MinecraftVersion::Launcher16) + { + determineNewVersion(); + } + else + { + getLegacyJar(); + } + QEventLoop loop; + loop.exec(); +} + +void GameUpdateTask::determineNewVersion() +{ + QString urlstr("http://s3.amazonaws.com/Minecraft.Download/versions/"); + urlstr += targetVersion->descriptor() + "/" + targetVersion->descriptor() + ".json"; + auto dljob = DownloadJob::create(QUrl(urlstr)); + specificVersionDownloadJob.reset(new JobList()); + specificVersionDownloadJob->add(dljob); + connect(specificVersionDownloadJob.data(), SIGNAL(finished()), SLOT(versionFileFinished())); + connect(specificVersionDownloadJob.data(), SIGNAL(failed()), SLOT(versionFileFailed())); + connect(specificVersionDownloadJob.data(), SIGNAL(progress(qint64,qint64)), SLOT(updateDownloadProgress(qint64,qint64))); + download_queue.enqueue(specificVersionDownloadJob); +} + +void GameUpdateTask::versionFileFinished() +{ + JobPtr firstJob = specificVersionDownloadJob->getFirstJob(); + auto DlJob = firstJob.dynamicCast(); + QJsonParseError jsonError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(DlJob->m_data, &jsonError); + + if (jsonError.error != QJsonParseError::NoError) + { + error(QString( "Error reading version file :") + " " + jsonError.errorString()); + exit(0); + } + + Q_ASSERT_X(jsonDoc.isObject(), "loadFromVList", "jsonDoc is not an object"); + if(!jsonDoc.isObject()) + { + error("Error reading version file."); + exit(0); + } + QJsonObject root = jsonDoc.object(); + + QString args = root.value("processArguments").toString("legacy"); + if(args == "legacy") + { + getLegacyJar(); + return; + } + + + error("MC 1.6 isn't supported yet..."); + exit(0); +} + +void GameUpdateTask::versionFileFailed() +{ + error("Failed to download the version description. Try again."); + exit(0); +} + + +// this is legacy minecraft... +void GameUpdateTask::getLegacyJar() +{ // Make directories QDir binDir(m_inst->binDir()); if (!binDir.exists() && !binDir.mkpath(".")) @@ -63,99 +133,40 @@ void GameUpdateTask::executeTask() return; } - - - ///////////////////////// - // BUILD DOWNLOAD LIST // - ///////////////////////// - // Build a list of URLs that will need to be downloaded. - - setState(StateDetermineURLs); - - // Add the URL for minecraft.jar - // This will be either 'minecraft' or the version number, depending on where // we're downloading from. QString jarFilename = "minecraft"; - - // FIXME: this is NOT enough if (targetVersion->launcherVersion() == MinecraftVersion::Launcher16) + { jarFilename = targetVersion->descriptor(); + } QUrl mcJarURL = targetVersion->downloadURL() + jarFilename + ".jar"; qDebug() << mcJarURL.toString(); - m_downloadList.append(FileToDownload::Create(mcJarURL, PathCombine(m_inst->minecraftDir(), "bin/minecraft.jar"))); + auto dljob = DownloadJob::create(mcJarURL, PathCombine(m_inst->minecraftDir(), "bin/minecraft.jar")); + legacyDownloadJob.reset(new JobList()); + legacyDownloadJob->add(dljob); + connect(legacyDownloadJob.data(), SIGNAL(finished()), SLOT(legacyJarFinished())); + connect(legacyDownloadJob.data(), SIGNAL(failed()), SLOT(legacyJarFailed())); + connect(legacyDownloadJob.data(), SIGNAL(progress(qint64,qint64)), SLOT(updateDownloadProgress(qint64,qint64))); - - //////////////////// - // DOWNLOAD FILES // - //////////////////// - setState(StateDownloadFiles); - for (int i = 0; i < m_downloadList.length(); i++) - { - m_currentDownload = i; - if (!downloadFile(m_downloadList[i])) - return; - } - - - - /////////////////// - // INSTALL FILES // - /////////////////// - setState(StateInstall); - - // Nothing to do here yet - - - - ////////////// - // FINISHED // - ////////////// - setState(StateFinished); - emit gameUpdateComplete(m_response); + download_queue.enqueue(legacyDownloadJob); } -bool GameUpdateTask::downloadFile( const FileToDownloadPtr file ) + +void GameUpdateTask::legacyJarFinished() { - setSubStatus("Downloading " + file->url().toString()); - QNetworkReply *reply = netMgr->get(QNetworkRequest(file->url())); - - this->connect(reply, SIGNAL(downloadProgress(qint64,qint64)), - SLOT(updateDownloadProgress(qint64,qint64))); - - NetUtils::waitForNetRequest(reply); - - if (reply->error() == QNetworkReply::NoError) - { - QString filePath = file->path(); - QFile outFile(filePath); - if (outFile.exists() && !outFile.remove()) - { - error("Can't delete old file " + file->path() + ": " + outFile.errorString()); - return false; - } - - if (!outFile.open(QIODevice::WriteOnly)) - { - error("Can't write to " + file->path() + ": " + outFile.errorString()); - return false; - } - - outFile.write(reply->readAll()); - outFile.close(); - } - else - { - error("Can't download " + file->url().toString() + ": " + reply->errorString()); - return false; - } - - // TODO: Check file integrity after downloading. - - return true; + setState(StateFinished); + emit gameUpdateComplete(m_response); + exit(1); +} + +void GameUpdateTask::legacyJarFailed() +{ + emit gameUpdateError("failed to download the minecraft.jar"); + exit(0); } int GameUpdateTask::state() const @@ -229,22 +240,7 @@ void GameUpdateTask::error(const QString &msg) void GameUpdateTask::updateDownloadProgress(qint64 current, qint64 total) { // The progress on the current file is current / total - float currentDLProgress = (float) current / (float) total; // Cast ALL the values! - - // The overall progress is (current progress + files downloaded) / total files to download - float overallDLProgress = ((currentDLProgress + m_currentDownload) / (float) m_downloadList.length()); - - // Multiply by 100 to make it a percentage. - setProgress((int)(overallDLProgress * 100)); + float currentDLProgress = (float) current / (float) total; + setProgress((int)(currentDLProgress * 100)); // convert to percentage } -FileToDownloadPtr FileToDownload::Create(const QUrl &url, const QString &path, QObject *parent) -{ - return FileToDownloadPtr(new FileToDownload (url, path, parent)); -} - -FileToDownload::FileToDownload(const QUrl &url, const QString &path, QObject *parent) : - QObject(parent), m_dlURL(url), m_dlPath(path) -{ - -} diff --git a/libutil/include/dlqueue.h b/libutil/include/dlqueue.h index 9041e762..5fb9409c 100644 --- a/libutil/include/dlqueue.h +++ b/libutil/include/dlqueue.h @@ -38,7 +38,7 @@ public: /// save to file? bool m_save_to_file; /// if saving to file, use the one specified in this string - QString m_rel_target_path; + QString m_target_path; /// this is the output file, if any QFile m_output_file; /// if not saving to file, downloaded data is placed here diff --git a/libutil/src/dlqueue.cpp b/libutil/src/dlqueue.cpp index dfc51f36..7e4d47eb 100644 --- a/libutil/src/dlqueue.cpp +++ b/libutil/src/dlqueue.cpp @@ -1,20 +1,20 @@ #include "include/dlqueue.h" -DownloadJob::DownloadJob ( QUrl url, QString rel_target_path, QString expected_md5 ) +DownloadJob::DownloadJob ( QUrl url, QString target_path, QString expected_md5 ) :Job() { m_url = url; - m_rel_target_path = rel_target_path; + m_target_path = target_path; m_expected_md5 = expected_md5; m_check_md5 = m_expected_md5.size(); - m_save_to_file = m_rel_target_path.size(); + m_save_to_file = m_target_path.size(); m_status = Job_NotStarted; } -JobPtr DownloadJob::create ( QUrl url, QString rel_target_path, QString expected_md5 ) +JobPtr DownloadJob::create ( QUrl url, QString target_path, QString expected_md5 ) { - return JobPtr ( new DownloadJob ( url, rel_target_path, expected_md5 ) ); + return JobPtr ( new DownloadJob ( url, target_path, expected_md5 ) ); } void DownloadJob::start() @@ -22,7 +22,7 @@ void DownloadJob::start() m_manager.reset ( new QNetworkAccessManager() ); if ( m_save_to_file ) { - QString filename = m_rel_target_path; + QString filename = m_target_path; m_output_file.setFileName ( filename ); // if there already is a file and md5 checking is in effect if ( m_output_file.exists() && m_check_md5 )