2018-02-10 23:40:01 +00:00
|
|
|
/* Copyright 2013-2018 MultiMC Contributors
|
2017-05-31 08:17:45 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-04-12 21:50:52 +01:00
|
|
|
|
|
|
|
#include <quazip.h>
|
2015-09-13 03:21:26 +01:00
|
|
|
#include <quazipdir.h>
|
2017-05-29 00:32:28 +01:00
|
|
|
#include <quazipfile.h>
|
|
|
|
#include <JlCompress.h>
|
2015-04-12 21:50:52 +01:00
|
|
|
#include "MMCZip.h"
|
2015-10-05 00:47:27 +01:00
|
|
|
#include "FileSystem.h"
|
2015-04-12 21:50:52 +01:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
|
|
|
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const JlCompress::FilterFunction filter)
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
QuaZip modZip(from.filePath());
|
|
|
|
modZip.open(QuaZip::mdUnzip);
|
|
|
|
|
|
|
|
QuaZipFile fileInsideMod(&modZip);
|
|
|
|
QuaZipFile zipOutFile(into);
|
|
|
|
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
|
|
|
|
{
|
|
|
|
QString filename = modZip.getCurrentFileName();
|
2017-05-31 08:17:45 +01:00
|
|
|
if (filter && !filter(filename))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
qDebug() << "Skipping file " << filename << " from "
|
|
|
|
<< from.fileName() << " - filtered";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (contained.contains(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "Skipping already contained file " << filename << " from "
|
|
|
|
<< from.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
contained.insert(filename);
|
|
|
|
|
|
|
|
if (!fileInsideMod.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
|
|
|
|
|
|
|
if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to open " << filename << " in the jar";
|
|
|
|
fileInsideMod.close();
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-29 00:32:28 +01:00
|
|
|
if (!JlCompress::copyData(fileInsideMod, zipOutFile))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2015-04-12 21:50:52 +01:00
|
|
|
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod>& mods)
|
|
|
|
{
|
|
|
|
QuaZip zipOut(targetJarPath);
|
|
|
|
if (!zipOut.open(QuaZip::mdCreate))
|
|
|
|
{
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to open the minecraft.jar for modding";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Files already added to the jar.
|
|
|
|
// These files will be skipped.
|
|
|
|
QSet<QString> addedFiles;
|
|
|
|
|
|
|
|
// Modify the jar
|
|
|
|
QListIterator<Mod> i(mods);
|
|
|
|
i.toBack();
|
|
|
|
while (i.hasPrevious())
|
|
|
|
{
|
|
|
|
const Mod &mod = i.previous();
|
|
|
|
// do not merge disabled mods.
|
|
|
|
if (!mod.enabled())
|
|
|
|
continue;
|
|
|
|
if (mod.type() == Mod::MOD_ZIPFILE)
|
|
|
|
{
|
2017-05-31 08:17:45 +01:00
|
|
|
if (!mergeZipFiles(&zipOut, mod.filename(), addedFiles))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mod.type() == Mod::MOD_SINGLEFILE)
|
|
|
|
{
|
2017-05-31 08:17:45 +01:00
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2015-04-12 21:50:52 +01:00
|
|
|
auto filename = mod.filename();
|
2017-05-29 00:32:28 +01:00
|
|
|
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addedFiles.insert(filename.fileName());
|
|
|
|
}
|
|
|
|
else if (mod.type() == Mod::MOD_FOLDER)
|
|
|
|
{
|
2017-05-31 08:17:45 +01:00
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2015-04-12 21:50:52 +01:00
|
|
|
auto filename = mod.filename();
|
|
|
|
QString what_to_zip = filename.absoluteFilePath();
|
|
|
|
QDir dir(what_to_zip);
|
|
|
|
dir.cdUp();
|
|
|
|
QString parent_dir = dir.absolutePath();
|
2017-05-31 08:17:45 +01:00
|
|
|
if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qDebug() << "Adding folder " << filename.fileName() << " from "
|
|
|
|
<< filename.absoluteFilePath();
|
|
|
|
}
|
2017-04-23 01:31:13 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make sure we do not continue launching when something is missing or undefined...
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to add unknown mod type" << mod.filename().fileName() << "to the jar.";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-12 21:50:52 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key){return !key.contains("META-INF");}))
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to insert minecraft.jar contents.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompress the jar
|
|
|
|
zipOut.close();
|
|
|
|
if (zipOut.getZipError() != 0)
|
|
|
|
{
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to finalize minecraft.jar!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2017-09-04 07:17:25 +01:00
|
|
|
QString MMCZip::findFolderOfFileInZip(QuaZip * zip, const QString & what, const QString &root)
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
|
|
|
QuaZipDir rootDir(zip, root);
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Files))
|
|
|
|
{
|
|
|
|
if(fileName == what)
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
|
|
|
{
|
2017-09-04 07:17:25 +01:00
|
|
|
QString result = findFolderOfFileInZip(zip, what, root + fileName);
|
2015-09-13 03:21:26 +01:00
|
|
|
if(!result.isEmpty())
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2015-09-13 03:21:26 +01:00
|
|
|
bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root)
|
|
|
|
{
|
|
|
|
QuaZipDir rootDir(zip, root);
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Files))
|
|
|
|
{
|
|
|
|
if(fileName == what)
|
|
|
|
{
|
|
|
|
result.append(root);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
|
|
|
{
|
|
|
|
findFilesInZip(zip, what, result, root + fileName);
|
|
|
|
}
|
|
|
|
return !result.isEmpty();
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
|
|
|
|
// ours
|
2017-05-29 00:32:28 +01:00
|
|
|
QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
|
|
|
QDir directory(target);
|
|
|
|
QStringList extracted;
|
|
|
|
if (!zip->goToFirstFile())
|
|
|
|
{
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
QString name = zip->getCurrentFileName();
|
|
|
|
if(!name.startsWith(subdir))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name.remove(0, subdir.size());
|
|
|
|
QString absFilePath = directory.absoluteFilePath(name);
|
|
|
|
if(name.isEmpty())
|
|
|
|
{
|
|
|
|
absFilePath += "/";
|
|
|
|
}
|
2017-05-29 00:32:28 +01:00
|
|
|
if (!JlCompress::extractFile(zip, "", absFilePath))
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
2017-05-29 00:32:28 +01:00
|
|
|
JlCompress::removeFile(extracted);
|
2015-09-13 03:21:26 +01:00
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
extracted.append(absFilePath);
|
|
|
|
} while (zip->goToNextFile());
|
|
|
|
return extracted;
|
|
|
|
}
|
2017-05-25 00:24:27 +01:00
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2017-05-29 00:32:28 +01:00
|
|
|
QStringList MMCZip::extractDir(QString fileCompressed, QString dir)
|
2017-05-25 00:24:27 +01:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
if (!zip.open(QuaZip::mdUnzip))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2017-05-29 00:32:28 +01:00
|
|
|
return MMCZip::extractSubDir(&zip, "", dir);
|
2017-05-25 00:24:27 +01:00
|
|
|
}
|