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 /libmultimc/src | |
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
Diffstat (limited to 'libmultimc/src')
-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 |
6 files changed, 263 insertions, 31 deletions
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; } |