sneedmc/logic/minecraft/LegacyUpdate.cpp

396 lines
11 KiB
C++
Raw Normal View History

2015-02-02 22:25:30 +00:00
/* Copyright 2013-2015 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.
*/
2014-05-08 19:20:10 +00:00
#include <QStringList>
#include <pathutils.h>
#include <quazip.h>
#include <quazipfile.h>
#include <QDebug>
2014-05-08 19:20:10 +00:00
2015-02-09 00:51:14 +00:00
#include "Env.h"
#include "BaseInstance.h"
#include "net/URLConstants.h"
#include "MMCZip.h"
2015-02-09 00:51:14 +00:00
#include "LegacyUpdate.h"
2015-02-09 00:51:14 +00:00
#include "minecraft/LwjglVersionList.h"
#include "minecraft/MinecraftVersionList.h"
#include "minecraft/ModList.h"
#include "minecraft/LegacyInstance.h"
LegacyUpdate::LegacyUpdate(BaseInstance *inst, QObject *parent) : Task(parent), m_inst(inst)
2013-10-05 23:13:40 +00:00
{
}
void LegacyUpdate::executeTask()
{
fmllibsStart();
}
void LegacyUpdate::fmllibsStart()
{
// Get the mod list
LegacyInstance *inst = (LegacyInstance *)m_inst;
auto modList = inst->jarModList();
bool forge_present = false;
QString version = inst->intendedVersionId();
2014-05-04 22:10:59 +00:00
auto & fmlLibsMapping = g_VersionFilterData.fmlLibsMapping;
if (!fmlLibsMapping.contains(version))
{
lwjglStart();
return;
}
auto &libList = fmlLibsMapping[version];
// determine if we need some libs for FML or forge
setStatus(tr("Checking for FML libraries..."));
for (unsigned i = 0; i < modList->size(); i++)
{
auto &mod = modList->operator[](i);
// do not use disabled mods.
if (!mod.enabled())
continue;
if (mod.type() != Mod::MOD_ZIPFILE)
continue;
if (mod.mmc_id().contains("forge", Qt::CaseInsensitive))
{
forge_present = true;
break;
}
if (mod.mmc_id().contains("fml", Qt::CaseInsensitive))
{
forge_present = true;
break;
}
}
// we don't...
if (!forge_present)
{
lwjglStart();
return;
}
// now check the lib folder inside the instance for files.
for (auto &lib : libList)
{
QFileInfo libInfo(PathCombine(inst->libDir(), lib.filename));
if (libInfo.exists())
continue;
fmlLibsToProcess.append(lib);
}
// if everything is in place, there's nothing to do here...
if (fmlLibsToProcess.isEmpty())
{
lwjglStart();
return;
}
// download missing libs to our place
setStatus(tr("Dowloading FML libraries..."));
auto dljob = new NetJob("FML libraries");
auto metacache = ENV.metacache();
for (auto &lib : fmlLibsToProcess)
{
auto entry = metacache->resolveEntry("fmllibs", lib.filename);
QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
: URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename;
dljob->addNetAction(CacheDownload::make(QUrl(urlString), entry));
}
2015-04-26 11:47:14 +00:00
connect(dljob, &NetJob::succeeded, this, &LegacyUpdate::fmllibsFinished);
connect(dljob, &NetJob::failed, this, &LegacyUpdate::fmllibsFailed);
connect(dljob, &NetJob::progress, this, &LegacyUpdate::progress);
legacyDownloadJob.reset(dljob);
legacyDownloadJob->start();
}
void LegacyUpdate::fmllibsFinished()
{
legacyDownloadJob.reset();
if(!fmlLibsToProcess.isEmpty())
{
setStatus(tr("Copying FML libraries into the instance..."));
LegacyInstance *inst = (LegacyInstance *)m_inst;
auto metacache = ENV.metacache();
int index = 0;
for (auto &lib : fmlLibsToProcess)
{
progress(index, fmlLibsToProcess.size());
auto entry = metacache->resolveEntry("fmllibs", lib.filename);
auto path = PathCombine(inst->libDir(), lib.filename);
if(!ensureFilePathExists(path))
{
emitFailed(tr("Failed creating FML library folder inside the instance."));
return;
}
if (!QFile::copy(entry->getFullPath(), PathCombine(inst->libDir(), lib.filename)))
{
emitFailed(tr("Failed copying Forge/FML library: %1.").arg(lib.filename));
return;
}
index++;
}
progress(index, fmlLibsToProcess.size());
}
lwjglStart();
}
2015-04-26 11:47:14 +00:00
void LegacyUpdate::fmllibsFailed(QString reason)
{
2015-04-26 11:47:14 +00:00
emitFailed(tr("Game update failed: it was impossible to fetch the required FML libraries. Reason: %1").arg(reason));
return;
}
void LegacyUpdate::lwjglStart()
{
2013-10-05 23:13:40 +00:00
LegacyInstance *inst = (LegacyInstance *)m_inst;
lwjglVersion = inst->lwjglVersion();
lwjglTargetPath = PathCombine(inst->lwjglFolder(), lwjglVersion);
2013-10-05 23:13:40 +00:00
lwjglNativesPath = PathCombine(lwjglTargetPath, "natives");
// if the 'done' file exists, we don't have to download this again
QFileInfo doneFile(PathCombine(lwjglTargetPath, "done"));
2013-10-05 23:13:40 +00:00
if (doneFile.exists())
{
jarStart();
return;
}
2013-10-05 23:13:40 +00:00
auto list = std::dynamic_pointer_cast<LWJGLVersionList>(ENV.getVersionList("org.lwjgl.legacy"));
2013-10-05 23:13:40 +00:00
if (!list->isLoaded())
{
emitFailed("Too soon! Let the LWJGL list load :)");
return;
}
2013-10-05 23:13:40 +00:00
2013-12-23 15:46:01 +00:00
setStatus(tr("Downloading new LWJGL..."));
auto version = std::dynamic_pointer_cast<LWJGLVersion>(list->findVersion(lwjglVersion));
2013-10-05 23:13:40 +00:00
if (!version)
{
emitFailed("Game update failed: the selected LWJGL version is invalid.");
return;
}
2013-10-05 23:13:40 +00:00
QString url = version->url();
QUrl realUrl(url);
QString hostname = realUrl.host();
auto worker = ENV.qnam();
QNetworkRequest req(realUrl);
req.setRawHeader("Host", hostname.toLatin1());
req.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
2013-10-05 23:13:40 +00:00
QNetworkReply *rep = worker->get(req);
m_reply = std::shared_ptr<QNetworkReply>(rep);
2015-04-26 11:47:14 +00:00
connect(rep, &QNetworkReply::downloadProgress, this, &LegacyUpdate::progress);
connect(worker.get(), &QNetworkAccessManager::finished, this, &LegacyUpdate::lwjglFinished);
}
2013-10-05 23:13:40 +00:00
void LegacyUpdate::lwjglFinished(QNetworkReply *reply)
{
2013-10-05 23:13:40 +00:00
if (m_reply.get() != reply)
{
return;
}
2013-10-05 23:13:40 +00:00
if (reply->error() != QNetworkReply::NoError)
{
2013-10-05 23:13:40 +00:00
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 = ENV.qnam();
2013-10-05 23:13:40 +00:00
// Here i check if there is a cookie for me in the reply and extract it
QList<QNetworkCookie> cookies =
qvariant_cast<QList<QNetworkCookie>>(reply->header(QNetworkRequest::SetCookieHeader));
if (cookies.count() != 0)
{
2013-10-05 23:13:40 +00:00
// you must tell which cookie goes with which url
worker->cookieJar()->setCookiesFromUrl(cookies, QUrl("sourceforge.net"));
}
2013-10-05 23:13:40 +00:00
// here you can check for the 302 or whatever other header i need
QVariant newLoc = reply->header(QNetworkRequest::LocationHeader);
2013-10-05 23:13:40 +00:00
if (newLoc.isValid())
{
QString redirectedTo = reply->header(QNetworkRequest::LocationHeader).toString();
QUrl realUrl(redirectedTo);
QString hostname = realUrl.host();
QNetworkRequest req(redirectedTo);
req.setRawHeader("Host", hostname.toLatin1());
req.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
2013-10-05 23:13:40 +00:00
QNetworkReply *rep = worker->get(req);
2015-04-26 11:47:14 +00:00
connect(rep, &QNetworkReply::downloadProgress, this, &LegacyUpdate::progress);
2013-10-05 23:13:40 +00:00
m_reply = std::shared_ptr<QNetworkReply>(rep);
return;
}
QFile saveMe("lwjgl.zip");
saveMe.open(QIODevice::WriteOnly);
saveMe.write(m_reply->readAll());
saveMe.close();
2013-12-23 15:46:01 +00:00
setStatus(tr("Installing new LWJGL..."));
extractLwjgl();
jarStart();
}
void LegacyUpdate::extractLwjgl()
{
// make sure the directories are there
bool success = ensureFolderPathExists(lwjglNativesPath);
2013-10-05 23:13:40 +00:00
if (!success)
{
emitFailed("Failed to extract the lwjgl libs - error when creating required folders.");
return;
}
2013-10-05 23:13:40 +00:00
QuaZip zip("lwjgl.zip");
2013-10-05 23:13:40 +00:00
if (!zip.open(QuaZip::mdUnzip))
{
emitFailed("Failed to extract the lwjgl libs - not a valid archive.");
return;
}
2013-10-05 23:13:40 +00:00
// and now we are going to access files inside it
QuaZipFile file(&zip);
2013-10-05 23:13:40 +00:00
const QString jarNames[] = {"jinput.jar", "lwjgl_util.jar", "lwjgl.jar"};
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
{
2013-10-05 23:13:40 +00:00
if (!file.open(QIODevice::ReadOnly))
{
zip.close();
emitFailed("Failed to extract the lwjgl libs - error while reading archive.");
return;
}
QuaZipFileInfo info;
QString name = file.getActualFileName();
2013-10-05 23:13:40 +00:00
if (name.endsWith('/'))
{
file.close();
continue;
}
QString destFileName;
// Look for the jars
for (int i = 0; i < 3; i++)
{
if (name.endsWith(jarNames[i]))
{
destFileName = PathCombine(lwjglTargetPath, jarNames[i]);
}
}
// Not found? look for the natives
2013-10-05 23:13:40 +00:00
if (destFileName.isEmpty())
{
#ifdef Q_OS_WIN32
QString nativesDir = "windows";
#else
2013-10-05 23:13:40 +00:00
#ifdef Q_OS_MAC
QString nativesDir = "macosx";
2013-10-05 23:13:40 +00:00
#else
QString nativesDir = "linux";
2013-10-05 23:13:40 +00:00
#endif
#endif
if (name.contains(nativesDir))
{
int lastSlash = name.lastIndexOf('/');
int lastBackSlash = name.lastIndexOf('\\');
2013-10-05 23:13:40 +00:00
if (lastSlash != -1)
name = name.mid(lastSlash + 1);
else if (lastBackSlash != -1)
name = name.mid(lastBackSlash + 1);
destFileName = PathCombine(lwjglNativesPath, name);
}
}
// Now if destFileName is still empty, go to the next file.
if (!destFileName.isEmpty())
{
2013-12-23 15:46:01 +00:00
setStatus(tr("Installing new LWJGL - extracting ") + name + "...");
QFile output(destFileName);
output.open(QIODevice::WriteOnly);
output.write(file.readAll());
output.close();
}
file.close(); // do not forget to close!
}
zip.close();
2013-10-05 23:13:40 +00:00
m_reply.reset();
QFile doneFile(PathCombine(lwjglTargetPath, "done"));
doneFile.open(QIODevice::WriteOnly);
doneFile.write("done.");
doneFile.close();
}
2015-04-26 11:47:14 +00:00
void LegacyUpdate::lwjglFailed(QString reason)
{
2015-04-26 11:47:14 +00:00
emitFailed(tr("Bad stuff happened while trying to get the lwjgl libs: %1").arg(reason));
}
void LegacyUpdate::jarStart()
{
2013-10-05 23:13:40 +00:00
LegacyInstance *inst = (LegacyInstance *)m_inst;
if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
{
emitSucceeded();
return;
}
2013-10-05 23:13:40 +00:00
2013-12-23 15:46:01 +00:00
setStatus(tr("Checking for jar updates..."));
// Make directories
QDir binDir(inst->binDir());
if (!binDir.exists() && !binDir.mkpath("."))
{
emitFailed("Failed to create bin folder.");
return;
}
// Build a list of URLs that will need to be downloaded.
2013-12-23 15:46:01 +00:00
setStatus(tr("Downloading new minecraft.jar ..."));
QString version_id = inst->intendedVersionId();
QString localPath = version_id + "/" + version_id + ".jar";
QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + localPath;
2013-10-05 23:13:40 +00:00
auto dljob = new NetJob("Minecraft.jar for version " + version_id);
auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("versions", localPath);
dljob->addNetAction(CacheDownload::make(QUrl(urlstr), entry));
2013-09-08 18:16:38 +00:00
connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished()));
2015-04-26 11:47:14 +00:00
connect(dljob, SIGNAL(failed(QString)), SLOT(jarFailed(QString)));
2013-10-05 23:13:40 +00:00
connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
legacyDownloadJob.reset(dljob);
2013-09-01 22:25:40 +00:00
legacyDownloadJob->start();
}
void LegacyUpdate::jarFinished()
{
// process the jar
emitSucceeded();
}
2015-04-26 11:47:14 +00:00
void LegacyUpdate::jarFailed(QString reason)
{
// bad, bad
2015-04-26 11:47:14 +00:00
emitFailed(tr("Failed to download the minecraft jar: %1.").arg(reason));
}