diff options
author | Andrew <forkk@forkk.net> | 2013-05-08 12:56:43 -0500 |
---|---|---|
committer | Andrew <forkk@forkk.net> | 2013-05-08 12:56:43 -0500 |
commit | 5f781b3053c5ba8a25d354903acf2c31dc9a56c5 (patch) | |
tree | 94361d20568e55d63db7b18f3c7dded7d1e88e06 | |
parent | 2e62f6e8d8aded1036f96835ebebd4d656c0fcc2 (diff) | |
download | MultiMC-5f781b3053c5ba8a25d354903acf2c31dc9a56c5.tar MultiMC-5f781b3053c5ba8a25d354903acf2c31dc9a56c5.tar.gz MultiMC-5f781b3053c5ba8a25d354903acf2c31dc9a56c5.tar.lz MultiMC-5f781b3053c5ba8a25d354903acf2c31dc9a56c5.tar.xz MultiMC-5f781b3053c5ba8a25d354903acf2c31dc9a56c5.zip |
Implement basic game updater.
Resolves MMC-4: https://jira.forkk.net/browse/MMC-4
-rw-r--r-- | gui/mainwindow.cpp | 108 | ||||
-rw-r--r-- | gui/mainwindow.h | 31 | ||||
-rw-r--r-- | libmultimc/include/gameupdatetask.h | 144 | ||||
-rw-r--r-- | libmultimc/include/instance.h | 35 | ||||
-rw-r--r-- | libmultimc/include/logintask.h | 7 | ||||
-rw-r--r-- | libmultimc/include/minecraftprocess.h | 8 | ||||
-rw-r--r-- | libmultimc/include/minecraftversion.h | 10 | ||||
-rw-r--r-- | libmultimc/src/gameupdatetask.cpp | 227 | ||||
-rw-r--r-- | libmultimc/src/instance.cpp | 5 | ||||
-rw-r--r-- | libmultimc/src/logintask.cpp | 22 | ||||
-rw-r--r-- | libmultimc/src/minecraftprocess.cpp | 11 | ||||
-rw-r--r-- | libmultimc/src/minecraftversion.cpp | 12 | ||||
-rw-r--r-- | libmultimc/src/minecraftversionlist.cpp | 17 | ||||
-rw-r--r-- | libutil/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libutil/include/netutils.h | 36 | ||||
-rw-r--r-- | libutil/src/netutils.cpp | 16 | ||||
-rw-r--r-- | main.cpp | 4 |
17 files changed, 598 insertions, 97 deletions
diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 22055b51..47753b37 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -50,7 +50,9 @@ #include "version.h" #include "logintask.h" -#include <instance.h> +#include "gameupdatetask.h" + +#include "instance.h" #include "minecraftprocess.h" #include "instancemodel.h" @@ -68,6 +70,10 @@ MainWindow::MainWindow ( QWidget *parent ) : instList ( globalSettings->get ( "InstanceDir" ).toString() ) { ui->setupUi ( this ); + + // Set active instance to null. + m_activeInst = NULL; + // Create the widget view = new KCategorizedView ( ui->centralWidget ); drawer = new KCategoryDrawer ( view ); @@ -149,7 +155,7 @@ void MainWindow::instanceActivated ( QModelIndex index ) if(!index.isValid()) return; Instance * inst = (Instance *) index.data(InstanceModel::InstancePointerRole).value<void *>(); - doLogin(inst->id()); + doLogin(); } void MainWindow::on_actionAddInstance_triggered() @@ -313,55 +319,86 @@ void MainWindow::on_actionLaunchInstance_triggered() Instance* inst = selectedInstance(); if(inst) { - doLogin(inst->id()); + doLogin(); } } -void MainWindow::doLogin ( QString inst, const QString& errorMsg ) +void MainWindow::doLogin(const QString& errorMsg) { - LoginDialog* loginDlg = new LoginDialog ( this, errorMsg ); - if ( loginDlg->exec() ) + if (!selectedInstance()) + return; + + LoginDialog* loginDlg = new LoginDialog(this, errorMsg); + if (loginDlg->exec()) { - UserInfo uInfo ( loginDlg->getUsername(), loginDlg->getPassword() ); - - TaskDialog* tDialog = new TaskDialog ( this ); - LoginTask* loginTask = new LoginTask ( uInfo, inst, tDialog ); - connect ( loginTask, SIGNAL ( loginComplete ( QString, LoginResponse ) ), - SLOT ( onLoginComplete ( QString, LoginResponse ) ), Qt::QueuedConnection ); - connect ( loginTask, SIGNAL ( loginFailed ( QString, QString ) ), - SLOT ( onLoginFailed( QString, QString ) ), Qt::QueuedConnection ); - tDialog->exec ( loginTask ); + UserInfo uInfo(loginDlg->getUsername(), loginDlg->getPassword()); + + TaskDialog* tDialog = new TaskDialog(this); + LoginTask* loginTask = new LoginTask(uInfo, tDialog); + connect(loginTask, SIGNAL(loginComplete(LoginResponse)), + SLOT(onLoginComplete(LoginResponse)), Qt::QueuedConnection); + connect(loginTask, SIGNAL(loginFailed(QString)), + SLOT(doLogin(QString)), Qt::QueuedConnection); + m_activeInst = selectedInstance(); + tDialog->exec(loginTask); } } -void MainWindow::onLoginComplete ( QString inst, LoginResponse response ) +void MainWindow::onLoginComplete(LoginResponse response) { - // TODO: console - console = new ConsoleWindow(); - auto instance = instList.getInstanceById(inst); - if(instance) + Q_ASSERT_X(m_activeInst != NULL, "onLoginComplete", "no active instance is set"); + + if (!m_activeInst->shouldUpdateGame()) { - proc = new MinecraftProcess(instance, response.username(), response.sessionID()); - - console->show(); - //connect(proc, SIGNAL(ended()), SLOT(onTerminated())); - connect(proc, SIGNAL(log(QString,MessageLevel::Enum)), console, SLOT(write(QString,MessageLevel::Enum))); - proc->launch(); + launchInstance(m_activeInst, response); } else { - + TaskDialog *tDialog = new TaskDialog(this); + GameUpdateTask *updateTask = new GameUpdateTask(response, m_activeInst); + connect(updateTask, SIGNAL(gameUpdateComplete(LoginResponse)), + SLOT(onGameUpdateComplete(LoginResponse))); + connect(updateTask, SIGNAL(gameUpdateError(QString)), SLOT(onGameUpdateError(QString))); + tDialog->exec(updateTask); } - /* - QMessageBox::information ( this, "Login Successful", - QString ( "Logged in as %1 with session ID %2. Instance: %3" ). - arg ( response.username(), response.sessionID(), inst ) ); - */ } -void MainWindow::onLoginFailed ( QString inst, const QString& errorMsg ) +void MainWindow::onGameUpdateComplete(LoginResponse response) +{ + launchInstance(response); +} + +void MainWindow::onGameUpdateError(QString error) { - doLogin(inst, errorMsg); + QMessageBox::warning(this, "Error downloading instance", error); +} + + +void MainWindow::launchInstance(LoginResponse response) +{ + Q_ASSERT_X(m_activeInst != NULL, "onLoginComplete", "no active instance is set"); + launchInstance(m_activeInst, response); +} + +void MainWindow::launchInstance(QString instID, LoginResponse response) +{ + Instance *instance = instList.getInstanceById(instID).data(); + Q_ASSERT_X(instance != NULL, "launchInstance", "instance ID does not correspond to a valid instance"); + launchInstance(instance, response); +} + +void MainWindow::launchInstance(Instance *instance, LoginResponse response) +{ + Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); + + console = new ConsoleWindow(); + proc = new MinecraftProcess(instance, response.username(), response.sessionID()); + + console->show(); + //connect(proc, SIGNAL(ended()), SLOT(onTerminated())); + connect(proc, SIGNAL(log(QString, MessageLevel::Enum)), + console, SLOT(write(QString, MessageLevel::Enum))); + proc->launch(); } void MainWindow::taskStart(Task *task) @@ -415,8 +452,7 @@ void MainWindow::on_actionChangeInstMCVersion_triggered() if (view->selectionModel()->selectedIndexes().count() < 1) return; - QModelIndex index = view->selectionModel()->selectedIndexes().at(0); - Instance *inst = (Instance *)index.data(InstanceModel::InstancePointerRole).value<void *>(); + Instance *inst = selectedInstance(); VersionSelectDialog *vselect = new VersionSelectDialog(inst->versionList(), this); if (vselect->exec() && vselect->selectedVersion()) diff --git a/gui/mainwindow.h b/gui/mainwindow.h index bc35038e..192cdbc4 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -38,16 +38,21 @@ class MainWindow : public QMainWindow { Q_OBJECT + /*! + * The currently selected instance. + */ + Q_PROPERTY(Instance* selectedInstance READ selectedInstance STORED false) + public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void closeEvent(QCloseEvent *event); - // Browser Dialog - void openWebPage(QUrl url); - -private: + // Browser Dialog + void openWebPage(QUrl url); + + Instance *selectedInstance(); private slots: @@ -82,11 +87,14 @@ private slots: void on_actionChangeInstMCVersion_triggered(); - void doLogin( QString inst, const QString& errorMsg = "" ); + void doLogin(const QString& errorMsg = ""); - void onLoginComplete( QString inst, LoginResponse response ); - void onLoginFailed( QString inst, const QString& errorMsg ); + void onLoginComplete(LoginResponse response); + + + void onGameUpdateComplete(LoginResponse response); + void onGameUpdateError(QString error); void taskStart(Task *task); void taskEnd(Task *task); @@ -95,6 +103,10 @@ public slots: void instanceActivated ( QModelIndex ); void startTask(Task *task); + + void launchInstance(LoginResponse response); + void launchInstance(QString instID, LoginResponse response); + void launchInstance(Instance *inst, LoginResponse response); private: Ui::MainWindow *ui; @@ -106,6 +118,11 @@ private: MinecraftProcess *proc; ConsoleWindow *console; + // A pointer to the instance we are actively doing stuff with. + // This is set when the user launches an instance and is used to refer to that + // instance throughout the launching process. + Instance *m_activeInst; + Task *m_versionLoadTask; }; diff --git a/libmultimc/include/gameupdatetask.h b/libmultimc/include/gameupdatetask.h index eabfbd1f..47f6c007 100644 --- a/libmultimc/include/gameupdatetask.h +++ b/libmultimc/include/gameupdatetask.h @@ -18,19 +18,105 @@ #include <QObject> +#include <QList> + +#include <QNetworkAccessManager> +#include <QUrl> + +#include "task.h" #include "loginresponse.h" +#include "instance.h" #include "libmmc_config.h" +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) +public: + FileToDownload(const QUrl &url, const QString &path, QObject *parent = 0); + FileToDownload(const FileToDownload &other); + + 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; +}; + /*! - * \brief The game update task is the task that handles downloading instances. - * Each instance type has its own class inheriting from this base game update task. + * The game update task is the task that handles downloading instances' files. */ -class LIBMULTIMC_EXPORT GameUpdateTask : public QObject +class LIBMULTIMC_EXPORT GameUpdateTask : public Task { Q_OBJECT + + /*! + * The task's state. + * A certain state message will be shown depending on what this is set to. + */ + Q_PROPERTY(int state READ state WRITE setState) + + /*! + * The substatus message. + * This will be next to the the state message in the task's status. + */ + Q_PROPERTY(QString subStatus READ subStatus WRITE setSubStatus) public: - explicit GameUpdateTask(const LoginResponse &response, QObject *parent = 0); + explicit GameUpdateTask(const LoginResponse &response, Instance *inst, QObject *parent = 0); + + + ///////////////////////// + // EXECUTION FUNCTIONS // + ///////////////////////// + + virtual void executeTask(); + + virtual bool downloadFile(const FileToDownload &file); + + + ////////////////////// + // STATE AND STATUS // + ////////////////////// + + virtual int state() const; + virtual void setState(int state, bool resetSubStatus = true); + + virtual QString subStatus() const; + virtual void setSubStatus(const QString &msg); + + /*! + * Gets the message that will be displated for the given state. + */ + virtual QString getStateMessage(int state); + +public slots: + + /*! + * Updates the status message based on the state and substatus message. + */ + virtual void updateStatus(); + + + virtual void error(const QString &msg); + + +private slots: + virtual void updateDownloadProgress(qint64 current, qint64 total); signals: /*! @@ -40,13 +126,59 @@ signals: void gameUpdateComplete(const LoginResponse &response); /*! - * \brief Signal emitted if the game update fails. + * \brief Signal emitted if an error occurrs during the update. * \param errorMsg An error message to be displayed to the user. */ - void gameUpdateFailed(const QString &errorMsg); + void gameUpdateError(const QString &errorMsg); private: + /////////// + // STUFF // + /////////// + + Instance *m_inst; + LoginResponse m_response; + + QNetworkAccessManager *netMgr; + + + + //////////////////////// + // FILE DOWNLOAD LIST // + //////////////////////// + + // List of URLs that the game updater will need to download. + QList<FileToDownload> m_downloadList; + int m_currentDownload; + + + + //////////////////////////// + // STATE AND STATUS STUFF // + //////////////////////////// + + int m_updateState; + QString m_subStatusMsg; + + enum UpdateState + { + // Initializing + StateInit = 0, + + // Determining files to download + StateDetermineURLs, + + // Downloading files + StateDownloadFiles, + + // Installing files + StateInstall, + + // Finished + StateFinished + }; }; + #endif // GAMEUPDATETASK_H diff --git a/libmultimc/include/instance.h b/libmultimc/include/instance.h index e72a0be3..717f8816 100644 --- a/libmultimc/include/instance.h +++ b/libmultimc/include/instance.h @@ -75,6 +75,21 @@ class LIBMULTIMC_EXPORT Instance : public QObject */ Q_PROPERTY(bool shouldRebuild READ shouldRebuild WRITE setShouldRebuild) + /*! + * Whether or not Minecraft should be downloaded when the instance is launched. + * This returns true if shouldForceUpdate game is true or if the intended and + * current versions don't match. + */ + Q_PROPERTY(bool shouldUpdateGame READ shouldUpdateGame STORED false) + + /*! + * Whether or not the game will be forced to update on the next launch. + * If this is set to true, shouldUpdateGame will be true, regardless of whether or not + * the current and intended versions match. + * It should be noted that this is set to false automatically when game updates are run. + */ + Q_PROPERTY(bool shouldForceUpdateGame READ shouldForceUpdateGame WRITE setShouldForceUpdateGame) + /*! * The instance's current version. @@ -182,7 +197,7 @@ public: //////// INSTANCE INFO //////// //// General Info //// - virtual QString name() { return settings().get("name").toString(); } + virtual QString name() const { return settings().get("name").toString(); } virtual void setName(QString val) { settings().set("name", val); @@ -212,27 +227,33 @@ public: //// Version Stuff //// - virtual QString currentVersion() { return settings().get("JarVersion").toString(); } + virtual QString currentVersion() const { return settings().get("JarVersion").toString(); } virtual void setCurrentVersion(QString val) { settings().set("JarVersion", val); } - virtual QString lwjglVersion() { return settings().get("LwjglVersion").toString(); } + virtual QString lwjglVersion() const { return settings().get("LwjglVersion").toString(); } virtual void setLWJGLVersion(QString val) { settings().set("LwjglVersion", val); } - virtual QString intendedVersion() { return settings().get("IntendedJarVersion").toString(); } + virtual QString intendedVersion() const { return settings().get("IntendedJarVersion").toString(); } virtual void setIntendedVersion(QString val) { settings().set("IntendedJarVersion", val); } + virtual bool shouldUpdateGame() const + { return shouldForceUpdateGame() || intendedVersion() != currentVersion(); } + + virtual bool shouldForceUpdateGame() const { return settings().get("ShouldForceUpdate").toBool(); } + virtual void setShouldForceUpdateGame(bool val) { settings().set("ShouldForceUpdate", val); } + //// Timestamps //// - virtual qint64 lastLaunch() { return settings().get("lastLaunchTime").value<qint64>(); } + virtual qint64 lastLaunch() const { return settings().get("lastLaunchTime").value<qint64>(); } virtual void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch()) { settings().set("lastLaunchTime", val); emit propertiesChanged(this); } - virtual qint64 lastCurrentVersionUpdate() { return settings().get("lastVersionUpdate").value<qint64>(); } + virtual qint64 lastCurrentVersionUpdate() const { return settings().get("lastVersionUpdate").value<qint64>(); } virtual void setLastCurrentVersionUpdate(qint64 val) { settings().set("lastVersionUpdate", val); } @@ -274,7 +295,7 @@ public: * stored in the instance config file against the last modified time of Minecraft.jar. * \return True if updateCurrentVersion() should be called. */ - virtual bool shouldUpdateCurrentVersion(); + virtual bool shouldUpdateCurrentVersion() const; /*! * \brief Updates the current version. diff --git a/libmultimc/include/logintask.h b/libmultimc/include/logintask.h index e00609ec..15d715bd 100644 --- a/libmultimc/include/logintask.h +++ b/libmultimc/include/logintask.h @@ -30,20 +30,19 @@ class LIBMULTIMC_EXPORT LoginTask : public Task { Q_OBJECT public: - explicit LoginTask(const UserInfo& uInfo, QString inst, QObject *parent = 0); + explicit LoginTask(const UserInfo& uInfo, QObject *parent = 0); public slots: void processNetReply(QNetworkReply* reply); signals: - void loginComplete(QString inst, LoginResponse loginResponse); - void loginFailed(QString inst, const QString& errorMsg); + void loginComplete(LoginResponse loginResponse); + void loginFailed(const QString& errorMsg); protected: void executeTask(); QNetworkReply* netReply; - QString inst; UserInfo uInfo; }; diff --git a/libmultimc/include/minecraftprocess.h b/libmultimc/include/minecraftprocess.h index ac4d6be2..eb861236 100644 --- a/libmultimc/include/minecraftprocess.h +++ b/libmultimc/include/minecraftprocess.h @@ -54,7 +54,7 @@ public: * @param session the minecraft session id * @param console the instance console window */ - MinecraftProcess(InstancePtr inst, QString user, QString session); + MinecraftProcess(Instance *inst, QString user, QString session); /** * @brief launch minecraft @@ -66,7 +66,7 @@ public: * @param inst the instance * @param destination the destination path */ - static inline void extractIcon(InstancePtr inst, QString destination); + static inline void extractIcon(Instance *inst, QString destination); /** * @brief extract the MultiMC launcher.jar @@ -78,7 +78,7 @@ public: * @brief prepare the launch by extracting icon and launcher * @param inst the instance */ - static void prepare(InstancePtr inst); + static void prepare(Instance *inst); /** * @brief split a string into argv items like a shell would do @@ -101,7 +101,7 @@ signals: void log(QString text, MessageLevel::Enum level=MessageLevel::MultiMC); protected: - InstancePtr m_instance; + Instance *m_instance; QString m_user; QString m_session; QString m_err_leftover; diff --git a/libmultimc/include/minecraftversion.h b/libmultimc/include/minecraftversion.h index e30582ac..fd11b316 100644 --- a/libmultimc/include/minecraftversion.h +++ b/libmultimc/include/minecraftversion.h @@ -39,6 +39,11 @@ class LIBMULTIMC_EXPORT MinecraftVersion : public InstVersion */ Q_PROPERTY(QString etag READ etag) + /*! + * True if this is a version from the new Minecraft launcher's version list. + */ + Q_PROPERTY(bool isForNewLauncher READ isForNewLauncher WRITE setIsForNewLauncher) + public: explicit MinecraftVersion(QString descriptor, QString name, @@ -75,6 +80,9 @@ public: virtual QString typeName() const; virtual qint64 timestamp() const; + virtual bool isForNewLauncher() const; + virtual void setIsForNewLauncher(bool val); + virtual VersionType versionType() const; virtual void setVersionType(VersionType typeName); @@ -90,6 +98,8 @@ private: QString m_dlUrl; QString m_etag; VersionType m_type; + + bool m_isNewLauncherVersion; }; #endif // MINECRAFTVERSION_H diff --git a/libmultimc/src/gameupdatetask.cpp b/libmultimc/src/gameupdatetask.cpp index c152147e..34c8f670 100644 --- a/libmultimc/src/gameupdatetask.cpp +++ b/libmultimc/src/gameupdatetask.cpp @@ -15,8 +15,231 @@ #include "gameupdatetask.h" -GameUpdateTask::GameUpdateTask(const LoginResponse &response, QObject *parent) : - QObject(parent), m_response(response) +#include <QtNetwork> + +#include <QFile> +#include <QFileInfo> +#include <QTextStream> +#include <QDataStream> + +#include <QDebug> + +#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(). + findVersion(m_inst->intendedVersion()); + Q_ASSERT_X(targetVersion != NULL, "game update", "instance's intended version is not an actual version"); + + // Make directories + QDir binDir(m_inst->binDir()); + if (!binDir.exists() && !binDir.mkpath(".")) + { + error("Failed to create bin folder."); + 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"; + + if (targetVersion->isForNewLauncher()) + jarFilename = targetVersion->descriptor(); + + QUrl mcJarURL = targetVersion->downloadURL() + jarFilename + ".jar"; + qDebug() << mcJarURL.toString(); + m_downloadList.append(FileToDownload(mcJarURL, PathCombine(m_inst->minecraftDir(), "bin/minecraft.jar"))); + + + + //////////////////// + // 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); +} + +bool GameUpdateTask::downloadFile(const FileToDownload &file) +{ + 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) + { + QFile outFile = file.path(); + 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; +} + +int GameUpdateTask::state() const +{ + return m_updateState; +} + +void GameUpdateTask::setState(int state, bool resetSubStatus) +{ + m_updateState = state; + if (resetSubStatus) + setSubStatus(""); + else // We only need to update if we're not resetting substatus becasue setSubStatus updates status for us. + updateStatus(); +} + +QString GameUpdateTask::subStatus() const +{ + return m_subStatusMsg; +} + +void GameUpdateTask::setSubStatus(const QString &msg) +{ + m_subStatusMsg = msg; + updateStatus(); +} + +QString GameUpdateTask::getStateMessage(int state) +{ + switch (state) + { + case StateInit: + return "Initializing"; + + case StateDetermineURLs: + return "Determining files to download"; + + case StateDownloadFiles: + return "Downloading files"; + + case StateInstall: + return "Installing"; + + case StateFinished: + return "Finished"; + + default: + return "Downloading instance files"; + } +} + +void GameUpdateTask::updateStatus() +{ + QString newStatus; + + newStatus = getStateMessage(state()); + if (!subStatus().isEmpty()) + newStatus += ": " + subStatus(); + else + newStatus += "..."; + + setStatus(newStatus); +} + + +void GameUpdateTask::error(const QString &msg) +{ + emit gameUpdateError(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)); +} + + + +FileToDownload::FileToDownload(const QUrl &url, const QString &path, QObject *parent) : + QObject(parent), m_dlURL(url), m_dlPath(path) +{ + +} + +FileToDownload::FileToDownload(const FileToDownload &other) : + QObject(other.parent()), m_dlURL(other.m_dlURL), m_dlPath(other.m_dlPath) { } diff --git a/libmultimc/src/instance.cpp b/libmultimc/src/instance.cpp index f4a6a3c9..08cd6605 100644 --- a/libmultimc/src/instance.cpp +++ b/libmultimc/src/instance.cpp @@ -34,8 +34,9 @@ Instance::Instance(const QString &rootDir, QObject *parent) : settings().registerSetting(new Setting("iconKey", "default")); settings().registerSetting(new Setting("notes", "")); settings().registerSetting(new Setting("NeedsRebuild", true)); + settings().registerSetting(new Setting("ShouldForceUpdate", false)); settings().registerSetting(new Setting("JarVersion", "Unknown")); - settings().registerSetting(new Setting("LwjglVersion", "Mojang")); + settings().registerSetting(new Setting("LwjglVersion", "2.9.0")); settings().registerSetting(new Setting("IntendedJarVersion", "")); settings().registerSetting(new Setting("lastLaunchTime", 0)); @@ -157,7 +158,7 @@ InstVersionList *Instance::versionList() const return &MinecraftVersionList::getMainList(); } -bool Instance::shouldUpdateCurrentVersion() +bool Instance::shouldUpdateCurrentVersion() const { QFileInfo jar(mcJar()); return jar.lastModified().toUTC().toMSecsSinceEpoch() != lastCurrentVersionUpdate(); diff --git a/libmultimc/src/logintask.cpp b/libmultimc/src/logintask.cpp index e042a93f..4a55037c 100644 --- a/libmultimc/src/logintask.cpp +++ b/libmultimc/src/logintask.cpp @@ -24,8 +24,8 @@ #include <QUrl> #include <QUrlQuery> -LoginTask::LoginTask( const UserInfo& uInfo, QString inst, QObject* parent ) : - Task(parent), uInfo(uInfo), inst(inst) +LoginTask::LoginTask( const UserInfo& uInfo, QObject* parent ) : + Task(parent), uInfo(uInfo) { } @@ -78,42 +78,42 @@ void LoginTask::processNetReply(QNetworkReply *reply) QString sessionID = strings[3]; LoginResponse response(username, sessionID, latestVersion); - emit loginComplete(inst, response); + emit loginComplete(response); } else { - emit loginFailed(inst, "Failed to parse Minecraft version string."); + emit loginFailed("Failed to parse Minecraft version string."); } } else { if (responseStr.toLower() == "bad login") - emit loginFailed(inst, "Invalid username or password."); + emit loginFailed("Invalid username or password."); else if (responseStr.toLower() == "old version") - emit loginFailed(inst, "Launcher outdated, please update."); + emit loginFailed("Launcher outdated, please update."); else - emit loginFailed(inst, "Login failed: " + responseStr); + emit loginFailed("Login failed: " + responseStr); } } else if (responseCode == 503) { - emit loginFailed(inst, "The login servers are currently unavailable. " + emit loginFailed("The login servers are currently unavailable. " "Check http://help.mojang.com/ for more info."); } else { - emit loginFailed(inst, QString("Login failed: Unknown HTTP error %1 occurred."). + emit loginFailed(QString("Login failed: Unknown HTTP error %1 occurred."). arg(QString::number(responseCode))); } break; } case QNetworkReply::OperationCanceledError: - emit loginFailed(inst, "Login canceled."); + emit loginFailed("Login canceled."); break; default: - emit loginFailed(inst, "Login failed: " + reply->errorString()); + emit loginFailed("Login failed: " + reply->errorString()); break; } diff --git a/libmultimc/src/minecraftprocess.cpp b/libmultimc/src/minecraftprocess.cpp index f1b63e3d..e8b9959e 100644 --- a/libmultimc/src/minecraftprocess.cpp +++ b/libmultimc/src/minecraftprocess.cpp @@ -73,7 +73,7 @@ QStringList MinecraftProcess::splitArgs(QString args) } // prepare tools -inline void MinecraftProcess::extractIcon(InstancePtr inst, QString destination) +inline void MinecraftProcess::extractIcon(Instance *inst, QString destination) { // QImage(":/icons/instances/" + inst->iconKey()).save(destination); } @@ -83,14 +83,14 @@ inline void MinecraftProcess::extractLauncher(QString destination) QFile(":/launcher/launcher.jar").copy(destination); } -void MinecraftProcess::prepare(InstancePtr inst) +void MinecraftProcess::prepare(Instance *inst) { extractLauncher(PathCombine(inst->minecraftDir(), LAUNCHER_FILE)); extractIcon(inst, PathCombine(inst->minecraftDir(), "icon.png")); } // constructor -MinecraftProcess::MinecraftProcess(InstancePtr inst, QString user, QString session) : +MinecraftProcess::MinecraftProcess(Instance *inst, QString user, QString session) : m_instance(inst), m_user(user), m_session(session) { connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(finish(int, QProcess::ExitStatus))); @@ -254,9 +254,8 @@ void MinecraftProcess::genArgs() #endif // lwjgl - QString lwjgl = m_instance->lwjglVersion(); - if (lwjgl != "Mojang") - lwjgl = QDir(globalSettings->get("LWJGLDir").toString() + "/" + lwjgl).absolutePath(); + QString lwjgl = QDir(globalSettings->get("LWJGLDir").toString() + "/" + + m_instance->lwjglVersion()).absolutePath(); // launcher arguments m_arguments << QString("-Xms%1m").arg(m_instance->settings().get("MinMemAlloc").toInt()); diff --git a/libmultimc/src/minecraftversion.cpp b/libmultimc/src/minecraftversion.cpp index 2e8b4a9b..7dee802f 100644 --- a/libmultimc/src/minecraftversion.cpp +++ b/libmultimc/src/minecraftversion.cpp @@ -24,6 +24,7 @@ MinecraftVersion::MinecraftVersion(QString descriptor, InstVersion(descriptor, name, timestamp, parent), m_dlUrl(dlUrl), m_etag(etag) { m_linkedVersion = NULL; + m_isNewLauncherVersion = false; } MinecraftVersion::MinecraftVersion(const MinecraftVersion *linkedVersion) : @@ -98,6 +99,16 @@ qint64 MinecraftVersion::timestamp() const return m_timestamp; } +bool MinecraftVersion::isForNewLauncher() const +{ + return m_isNewLauncherVersion; +} + +void MinecraftVersion::setIsForNewLauncher(bool val) +{ + m_isNewLauncherVersion = val; +} + MinecraftVersion::VersionType MinecraftVersion::versionType() const { return m_type; @@ -137,6 +148,7 @@ InstVersion *MinecraftVersion::copyVersion(InstVersionList *newParent) const MinecraftVersion *version = new MinecraftVersion( descriptor(), name(), timestamp(), downloadURL(), etag(), newParent); version->setVersionType(versionType()); + version->setIsForNewLauncher(isForNewLauncher()); return version; } } diff --git a/libmultimc/src/minecraftversionlist.cpp b/libmultimc/src/minecraftversionlist.cpp index bb8b0f7a..8b40069e 100644 --- a/libmultimc/src/minecraftversionlist.cpp +++ b/libmultimc/src/minecraftversionlist.cpp @@ -29,6 +29,8 @@ #include <QtNetwork> +#include "netutils.h" + #define MCVLIST_URLBASE "http://s3.amazonaws.com/Minecraft.Download/versions/" #define ASSETS_URLBASE "http://assets.minecraft.net/" #define MCN_URLBASE "http://sonicrules.org/mcnweb.py" @@ -101,6 +103,7 @@ InstVersion *MinecraftVersionList::getLatestStable() const return m_vlist.at(i); } } + return NULL; } MinecraftVersionList &MinecraftVersionList::getMainList() @@ -169,13 +172,6 @@ inline QDateTime timeFromMCVListTime(QString str) } -inline void waitForNetRequest(QNetworkReply *netReply) -{ - QEventLoop loop; - loop.connect(netReply, SIGNAL(finished()), SLOT(quit())); - loop.exec(); -} - MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist) { @@ -222,7 +218,7 @@ bool MCVListLoadTask::loadFromVList() { QNetworkReply *vlistReply = netMgr->get(QNetworkRequest(QUrl(QString(MCVLIST_URLBASE) + "versions.json"))); - waitForNetRequest(vlistReply); + NetUtils::waitForNetRequest(vlistReply); switch (vlistReply->error()) { @@ -307,6 +303,7 @@ bool MCVListLoadTask::loadFromVList() MinecraftVersion *mcVersion = new MinecraftVersion( versionID, versionID, versionTime.toMSecsSinceEpoch(), dlUrl, ""); + mcVersion->setIsForNewLauncher(true); mcVersion->setVersionType(versionType); tempList.append(mcVersion); } @@ -335,7 +332,7 @@ bool MCVListLoadTask::loadFromAssets() bool succeeded = false; QNetworkReply *assetsReply = netMgr->get(QNetworkRequest(QUrl(ASSETS_URLBASE))); - waitForNetRequest(assetsReply); + NetUtils::waitForNetRequest(assetsReply); switch (assetsReply->error()) { @@ -459,7 +456,7 @@ bool MCVListLoadTask::loadFromAssets() bool MCVListLoadTask::loadMCNostalgia() { QNetworkReply *mcnReply = netMgr->get(QNetworkRequest(QUrl(QString(MCN_URLBASE) + "?pversion=1&list=True"))); - waitForNetRequest(mcnReply); + NetUtils::waitForNetRequest(mcnReply); return true; } diff --git a/libutil/CMakeLists.txt b/libutil/CMakeLists.txt index 11b21426..caafc756 100644 --- a/libutil/CMakeLists.txt +++ b/libutil/CMakeLists.txt @@ -31,6 +31,7 @@ include/pathutils.h include/osutils.h include/userutils.h include/cmdutils.h +include/netutils.h ) SET(LIBUTIL_SOURCES @@ -38,6 +39,7 @@ src/pathutils.cpp src/osutils.cpp src/userutils.cpp src/cmdutils.cpp +src/netutils.cpp ) # Set the include dir path. diff --git a/libutil/include/netutils.h b/libutil/include/netutils.h new file mode 100644 index 00000000..0153693b --- /dev/null +++ b/libutil/include/netutils.h @@ -0,0 +1,36 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NETUTILS_H +#define NETUTILS_H + +#include <QObject> + +#include <QNetworkReply> +#include <QEventLoop> + +namespace NetUtils +{ + +inline void waitForNetRequest(QNetworkReply *netReply) +{ + QEventLoop loop; + loop.connect(netReply, SIGNAL(finished()), SLOT(quit())); + loop.exec(); +} + +} + +#endif // NETUTILS_H diff --git a/libutil/src/netutils.cpp b/libutil/src/netutils.cpp new file mode 100644 index 00000000..57eead9b --- /dev/null +++ b/libutil/src/netutils.cpp @@ -0,0 +1,16 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/netutils.h" @@ -66,7 +66,7 @@ private slots: { // TODO: console console = new ConsoleWindow(); - proc = new MinecraftProcess(instance, response.username(), response.sessionID()); + proc = new MinecraftProcess(instance.data(), response.username(), response.sessionID()); //if (instance->getShowConsole()) console->show(); connect(proc, SIGNAL(ended()), SLOT(onTerminated())); @@ -82,7 +82,7 @@ private slots: UserInfo uInfo(loginDlg->getUsername(), loginDlg->getPassword()); TaskDialog* tDialog = new TaskDialog(nullptr); - LoginTask* loginTask = new LoginTask(uInfo, instance.data()->id(), tDialog); + LoginTask* loginTask = new LoginTask(uInfo, tDialog); connect(loginTask, SIGNAL(loginComplete(QString, LoginResponse)), SLOT(onLoginComplete(QString, LoginResponse)), Qt::QueuedConnection); connect(loginTask, SIGNAL(loginFailed(QString, QString)), |