2013-11-04 01:53:05 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2013-08-06 23:38:18 +00:00
|
|
|
#include "LegacyUpdate.h"
|
|
|
|
#include "lists/LwjglVersionList.h"
|
2013-08-11 22:39:19 +00:00
|
|
|
#include "lists/MinecraftVersionList.h"
|
2013-08-06 23:38:18 +00:00
|
|
|
#include "BaseInstance.h"
|
|
|
|
#include "LegacyInstance.h"
|
2013-09-07 02:00:58 +00:00
|
|
|
#include "MultiMC.h"
|
2013-08-24 01:09:46 +00:00
|
|
|
#include "ModList.h"
|
2013-08-06 23:38:18 +00:00
|
|
|
#include <pathutils.h>
|
|
|
|
#include <quazip.h>
|
|
|
|
#include <quazipfile.h>
|
2013-08-24 01:09:46 +00:00
|
|
|
#include <JlCompress.h>
|
2013-11-04 01:53:05 +00:00
|
|
|
#include "logger/QsLog.h"
|
2013-12-13 14:58:11 +00:00
|
|
|
#include "logic/net/URLConstants.h"
|
2013-08-06 23:38:18 +00:00
|
|
|
|
2013-12-08 21:06:04 +00:00
|
|
|
LegacyUpdate::LegacyUpdate(BaseInstance *inst, bool only_prepare, QObject *parent)
|
|
|
|
: Task(parent), m_inst(inst), m_only_prepare(only_prepare)
|
2013-10-05 23:13:40 +00:00
|
|
|
{
|
|
|
|
}
|
2013-08-06 23:38:18 +00:00
|
|
|
|
|
|
|
void LegacyUpdate::executeTask()
|
|
|
|
{
|
2013-12-08 21:06:04 +00:00
|
|
|
if(m_only_prepare)
|
|
|
|
{
|
|
|
|
// FIXME: think this through some more.
|
|
|
|
LegacyInstance *inst = (LegacyInstance *)m_inst;
|
|
|
|
if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
|
|
|
|
{
|
|
|
|
ModTheJar();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lwjglStart();
|
|
|
|
}
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LegacyUpdate::lwjglStart()
|
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
LegacyInstance *inst = (LegacyInstance *)m_inst;
|
|
|
|
|
|
|
|
lwjglVersion = inst->lwjglVersion();
|
2013-11-11 02:47:14 +00:00
|
|
|
lwjglTargetPath = PathCombine(MMC->settings()->get("LWJGLDir").toString(), lwjglVersion);
|
2013-10-05 23:13:40 +00:00
|
|
|
lwjglNativesPath = PathCombine(lwjglTargetPath, "natives");
|
2013-08-08 22:26:35 +00:00
|
|
|
|
|
|
|
// 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())
|
2013-08-08 22:26:35 +00:00
|
|
|
{
|
2013-08-11 22:39:19 +00:00
|
|
|
jarStart();
|
2013-08-08 22:26:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-09-15 22:54:39 +00:00
|
|
|
auto list = MMC->lwjgllist();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!list->isLoaded())
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Too soon! Let the LWJGL list load :)");
|
2013-08-06 23:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Downloading new LWJGL..."));
|
2013-09-15 22:54:39 +00:00
|
|
|
auto version = list->getVersion(lwjglVersion);
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!version)
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Game update failed: the selected LWJGL version is invalid.");
|
|
|
|
return;
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-06 23:38:18 +00:00
|
|
|
QString url = version->url();
|
2013-08-06 23:49:19 +00:00
|
|
|
QUrl realUrl(url);
|
|
|
|
QString hostname = realUrl.host();
|
2013-09-07 02:00:58 +00:00
|
|
|
auto worker = MMC->qnam();
|
2013-08-06 23:49:19 +00:00
|
|
|
QNetworkRequest req(realUrl);
|
|
|
|
req.setRawHeader("Host", hostname.toLatin1());
|
2013-10-09 23:47:48 +00:00
|
|
|
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);
|
|
|
|
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
|
|
|
|
connect(worker.get(), SIGNAL(finished(QNetworkReply *)),
|
|
|
|
SLOT(lwjglFinished(QNetworkReply *)));
|
|
|
|
// connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
// SLOT(downloadError(QNetworkReply::NetworkError)));
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
void LegacyUpdate::lwjglFinished(QNetworkReply *reply)
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
if (m_reply.get() != reply)
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
if (reply->error() != QNetworkReply::NoError)
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
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");
|
2013-08-06 23:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-22 02:21:36 +00:00
|
|
|
auto worker = MMC->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-08-06 23:38:18 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
// you must tell which cookie goes with which url
|
2013-09-07 02:00:58 +00:00
|
|
|
worker->cookieJar()->setCookiesFromUrl(cookies, QUrl("sourceforge.net"));
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
// here you can check for the 302 or whatever other header i need
|
2013-08-06 23:38:18 +00:00
|
|
|
QVariant newLoc = reply->header(QNetworkRequest::LocationHeader);
|
2013-10-05 23:13:40 +00:00
|
|
|
if (newLoc.isValid())
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
QString redirectedTo = reply->header(QNetworkRequest::LocationHeader).toString();
|
2013-08-06 23:49:19 +00:00
|
|
|
QUrl realUrl(redirectedTo);
|
|
|
|
QString hostname = realUrl.host();
|
2013-08-06 23:38:18 +00:00
|
|
|
QNetworkRequest req(redirectedTo);
|
2013-08-06 23:49:19 +00:00
|
|
|
req.setRawHeader("Host", hostname.toLatin1());
|
2013-10-09 23:47:48 +00:00
|
|
|
req.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
|
2013-10-05 23:13:40 +00:00
|
|
|
QNetworkReply *rep = worker->get(req);
|
|
|
|
connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
|
|
|
|
SIGNAL(progress(qint64, qint64)));
|
|
|
|
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
2013-08-06 23:38:18 +00:00
|
|
|
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..."));
|
2013-08-06 23:38:18 +00:00
|
|
|
extractLwjgl();
|
2013-08-11 22:39:19 +00:00
|
|
|
jarStart();
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
void LegacyUpdate::extractLwjgl()
|
|
|
|
{
|
|
|
|
// make sure the directories are there
|
2013-08-08 22:26:35 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
bool success = ensureFolderPathExists(lwjglNativesPath);
|
2013-10-05 23:13:40 +00:00
|
|
|
|
|
|
|
if (!success)
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Failed to extract the lwjgl libs - error when creating required folders.");
|
2013-08-06 23:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-06 23:38:18 +00:00
|
|
|
QuaZip zip("lwjgl.zip");
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Failed to extract the lwjgl libs - not a valid archive.");
|
2013-08-06 23:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-06 23:38:18 +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-08-06 23:38:18 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
zip.close();
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Failed to extract the lwjgl libs - error while reading archive.");
|
2013-08-06 23:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QuaZipFileInfo info;
|
|
|
|
QString name = file.getActualFileName();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (name.endsWith('/'))
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
file.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QString destFileName;
|
|
|
|
// Look for the jars
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if (name.endsWith(jarNames[i]))
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
destFileName = PathCombine(lwjglTargetPath, jarNames[i]);
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not found? look for the natives
|
2013-10-05 23:13:40 +00:00
|
|
|
if (destFileName.isEmpty())
|
2013-08-06 23:38:18 +00:00
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
QString nativesDir = "windows";
|
|
|
|
#else
|
2013-10-05 23:13:40 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2013-08-26 00:53:29 +00:00
|
|
|
QString nativesDir = "macosx";
|
2013-10-05 23:13:40 +00:00
|
|
|
#else
|
2013-08-06 23:38:18 +00:00
|
|
|
QString nativesDir = "linux";
|
2013-10-05 23:13:40 +00:00
|
|
|
#endif
|
2013-08-06 23:38:18 +00:00
|
|
|
#endif
|
|
|
|
if (name.contains(nativesDir))
|
|
|
|
{
|
|
|
|
int lastSlash = name.lastIndexOf('/');
|
2013-09-15 22:54:39 +00:00
|
|
|
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);
|
2013-08-08 22:26:35 +00:00
|
|
|
destFileName = PathCombine(lwjglNativesPath, name);
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 + "...");
|
2013-08-06 23:38:18 +00:00
|
|
|
QFile output(destFileName);
|
|
|
|
output.open(QIODevice::WriteOnly);
|
|
|
|
output.write(file.readAll()); // FIXME: wste of memory!?
|
|
|
|
output.close();
|
|
|
|
}
|
|
|
|
file.close(); // do not forget to close!
|
|
|
|
}
|
|
|
|
zip.close();
|
2013-10-05 23:13:40 +00:00
|
|
|
m_reply.reset();
|
2013-08-08 22:26:35 +00:00
|
|
|
QFile doneFile(PathCombine(lwjglTargetPath, "done"));
|
|
|
|
doneFile.open(QIODevice::WriteOnly);
|
|
|
|
doneFile.write("done.");
|
|
|
|
doneFile.close();
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LegacyUpdate::lwjglFailed()
|
|
|
|
{
|
2013-08-08 22:26:35 +00:00
|
|
|
emitFailed("Bad stuff happened while trying to get the lwjgl libs...");
|
2013-08-06 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 22:39:19 +00:00
|
|
|
void LegacyUpdate::jarStart()
|
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
LegacyInstance *inst = (LegacyInstance *)m_inst;
|
|
|
|
if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
|
2013-08-11 22:39:19 +00:00
|
|
|
{
|
2013-08-24 01:09:46 +00:00
|
|
|
ModTheJar();
|
2013-08-11 22:39:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Checking for jar updates..."));
|
2013-08-11 22:39:19 +00:00
|
|
|
// 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 ..."));
|
2013-08-11 22:39:19 +00:00
|
|
|
|
2013-12-10 19:33:24 +00:00
|
|
|
QString version_id = inst->intendedVersionId();
|
|
|
|
QString localPath = version_id + "/" + version_id + ".jar";
|
2013-12-13 14:58:11 +00:00
|
|
|
QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + localPath;
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-12-10 19:33:24 +00:00
|
|
|
auto dljob = new NetJob("Minecraft.jar for version " + version_id);
|
|
|
|
|
|
|
|
|
|
|
|
auto metacache = MMC->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()));
|
|
|
|
connect(dljob, SIGNAL(failed()), SLOT(jarFailed()));
|
2013-10-05 23:13:40 +00:00
|
|
|
connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
|
2013-12-10 19:33:24 +00:00
|
|
|
legacyDownloadJob.reset(dljob);
|
2013-09-01 22:25:40 +00:00
|
|
|
legacyDownloadJob->start();
|
2013-08-11 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LegacyUpdate::jarFinished()
|
|
|
|
{
|
|
|
|
// process the jar
|
2013-08-24 01:09:46 +00:00
|
|
|
ModTheJar();
|
2013-08-11 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LegacyUpdate::jarFailed()
|
|
|
|
{
|
|
|
|
// bad, bad
|
|
|
|
emitFailed("Failed to download the minecraft jar. Try again later.");
|
|
|
|
}
|
2013-08-14 06:13:41 +00:00
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
bool LegacyUpdate::MergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
|
|
|
|
MetainfAction metainf)
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Installing mods: Adding ") + from.fileName() + " ...");
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
QuaZip modZip(from.filePath());
|
|
|
|
modZip.open(QuaZip::mdUnzip);
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
QuaZipFile fileInsideMod(&modZip);
|
2013-10-05 23:13:40 +00:00
|
|
|
QuaZipFile zipOutFile(into);
|
|
|
|
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
QString filename = modZip.getCurrentFileName();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (filename.contains("META-INF") && metainf == LegacyUpdate::IgnoreMetainf)
|
2013-08-28 19:52:19 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_INFO() << "Skipping META-INF " << filename << " from " << from.fileName();
|
2013-08-24 01:09:46 +00:00
|
|
|
continue;
|
2013-08-28 19:52:19 +00:00
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
if (contained.contains(filename))
|
2013-08-28 19:52:19 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_INFO() << "Skipping already contained file " << filename << " from "
|
|
|
|
<< from.fileName();
|
2013-08-24 01:09:46 +00:00
|
|
|
continue;
|
2013-08-28 19:52:19 +00:00
|
|
|
}
|
2013-08-24 01:09:46 +00:00
|
|
|
contained.insert(filename);
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_INFO() << "Adding file " << filename << " from " << from.fileName();
|
|
|
|
|
|
|
|
if (!fileInsideMod.open(QIODevice::ReadOnly))
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_ERROR() << "Failed to open " << filename << " from " << from.fileName();
|
2013-08-24 01:09:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-24 23:32:42 +00:00
|
|
|
/*
|
|
|
|
QuaZipFileInfo old_info;
|
|
|
|
fileInsideMod.getFileInfo(&old_info);
|
|
|
|
*/
|
|
|
|
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
|
|
|
/*
|
|
|
|
info_out.externalAttr = old_info.externalAttr;
|
|
|
|
*/
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_ERROR() << "Failed to open " << filename << " in the jar";
|
2013-08-24 01:09:46 +00:00
|
|
|
fileInsideMod.close();
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!JlCompress::copyData(fileInsideMod, zipOutFile))
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_ERROR() << "Failed to copy data of " << filename << " into the jar";
|
2013-08-24 01:09:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-14 06:13:41 +00:00
|
|
|
void LegacyUpdate::ModTheJar()
|
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
LegacyInstance *inst = (LegacyInstance *)m_inst;
|
|
|
|
|
|
|
|
if (!inst->shouldRebuild())
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-14 06:13:41 +00:00
|
|
|
// Get the mod list
|
2013-08-24 01:09:46 +00:00
|
|
|
auto modList = inst->jarModList();
|
2013-10-05 23:13:40 +00:00
|
|
|
|
|
|
|
QFileInfo runnableJar(inst->runnableJar());
|
|
|
|
QFileInfo baseJar(inst->baseJar());
|
2013-08-24 01:09:46 +00:00
|
|
|
bool base_is_custom = inst->shouldUseCustomBaseJar();
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-14 06:13:41 +00:00
|
|
|
// Nothing to do if there are no jar mods to install, no backup and just the mc jar
|
2013-10-05 23:13:40 +00:00
|
|
|
if (base_is_custom)
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 01:09:46 +00:00
|
|
|
// yes, this can happen if the instance only has the runnable jar and not the base jar
|
|
|
|
// it *could* be assumed that such an instance is vanilla, but that wouldn't be safe
|
|
|
|
// because that's not something mmc4 guarantees
|
2013-10-05 23:13:40 +00:00
|
|
|
if (runnableJar.isFile() && !baseJar.exists() && modList->empty())
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
inst->setShouldRebuild(false);
|
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Installing mods: Backing up minecraft.jar ..."));
|
2013-08-24 01:09:46 +00:00
|
|
|
if (!baseJar.exists() && !QFile::copy(runnableJar.filePath(), baseJar.filePath()))
|
|
|
|
{
|
2013-11-24 23:46:52 +00:00
|
|
|
emitFailed("It seems both the active and base jar are gone. A fresh base jar will "
|
|
|
|
"be used on next run.");
|
2013-11-11 03:45:31 +00:00
|
|
|
inst->setShouldRebuild(true);
|
|
|
|
inst->setShouldUpdate(true);
|
|
|
|
inst->setShouldUseCustomBaseJar(false);
|
2013-08-24 01:09:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-08-14 06:13:41 +00:00
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
if (!baseJar.exists())
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 01:09:46 +00:00
|
|
|
emitFailed("The base jar " + baseJar.filePath() + " does not exist");
|
2013-08-14 06:13:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
if (runnableJar.exists() && !QFile::remove(runnableJar.filePath()))
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
|
|
|
emitFailed("Failed to delete old minecraft.jar");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
|
|
|
// TaskStep(); // STEP 1
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Installing mods: Opening minecraft.jar ..."));
|
2013-08-14 06:13:41 +00:00
|
|
|
|
2013-08-24 01:09:46 +00:00
|
|
|
QuaZip zipOut(runnableJar.filePath());
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!zipOut.open(QuaZip::mdCreate))
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to open the minecraft.jar for modding");
|
|
|
|
return;
|
|
|
|
}
|
2013-08-14 06:13:41 +00:00
|
|
|
// Files already added to the jar.
|
|
|
|
// These files will be skipped.
|
|
|
|
QSet<QString> addedFiles;
|
|
|
|
|
|
|
|
// Modify the jar
|
2013-12-23 15:46:01 +00:00
|
|
|
setStatus(tr("Installing mods: Adding mod files..."));
|
2013-08-24 01:09:46 +00:00
|
|
|
for (int i = modList->size() - 1; i >= 0; i--)
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 01:09:46 +00:00
|
|
|
auto &mod = modList->operator[](i);
|
|
|
|
if (mod.type() == Mod::MOD_ZIPFILE)
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!MergeZipFiles(&zipOut, mod.filename(), addedFiles, LegacyUpdate::KeepMetainf))
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 01:09:46 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to add " + mod.filename().fileName() + " to the jar.");
|
|
|
|
return;
|
2013-08-14 06:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-24 01:09:46 +00:00
|
|
|
else if (mod.type() == Mod::MOD_SINGLEFILE)
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 23:32:42 +00:00
|
|
|
auto filename = mod.filename();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(),
|
|
|
|
filename.fileName()))
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 23:32:42 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to add " + filename.fileName() + " to the jar");
|
|
|
|
return;
|
2013-08-14 06:13:41 +00:00
|
|
|
}
|
2013-08-24 23:32:42 +00:00
|
|
|
addedFiles.insert(filename.fileName());
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_INFO() << "Adding file " << filename.fileName() << " from "
|
|
|
|
<< filename.absoluteFilePath();
|
2013-08-14 06:13:41 +00:00
|
|
|
}
|
2013-08-24 01:09:46 +00:00
|
|
|
else if (mod.type() == Mod::MOD_FOLDER)
|
2013-08-14 06:13:41 +00:00
|
|
|
{
|
2013-08-24 23:32:42 +00:00
|
|
|
auto filename = mod.filename();
|
|
|
|
QString what_to_zip = filename.absoluteFilePath();
|
|
|
|
QDir dir(what_to_zip);
|
|
|
|
dir.cdUp();
|
|
|
|
QString parent_dir = dir.absolutePath();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, true, addedFiles))
|
2013-08-24 23:32:42 +00:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to add " + filename.fileName() + " to the jar");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
QLOG_INFO() << "Adding folder " << filename.fileName() << " from "
|
|
|
|
<< filename.absoluteFilePath();
|
2013-08-14 06:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
|
|
|
if (!MergeZipFiles(&zipOut, baseJar, addedFiles, LegacyUpdate::IgnoreMetainf))
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to insert minecraft.jar contents.");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2013-08-14 06:13:41 +00:00
|
|
|
// Recompress the jar
|
2013-08-24 01:09:46 +00:00
|
|
|
zipOut.close();
|
2013-10-05 23:13:40 +00:00
|
|
|
if (zipOut.getZipError() != 0)
|
2013-08-24 01:09:46 +00:00
|
|
|
{
|
|
|
|
QFile::remove(runnableJar.filePath());
|
|
|
|
emitFailed("Failed to finalize minecraft.jar!");
|
|
|
|
return;
|
2013-10-05 23:13:40 +00:00
|
|
|
}
|
2013-08-24 01:09:46 +00:00
|
|
|
inst->setShouldRebuild(false);
|
2013-10-05 23:13:40 +00:00
|
|
|
// inst->UpdateVersion(true);
|
2013-08-24 01:09:46 +00:00
|
|
|
emitSucceeded();
|
|
|
|
return;
|
2013-12-10 19:33:24 +00:00
|
|
|
}
|