format
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
b9568279dc
commit
251055302e
@ -33,56 +33,48 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "MMCZip.h"
|
||||||
#include <quazip/quazip.h>
|
#include <quazip/quazip.h>
|
||||||
#include <quazip/quazipdir.h>
|
#include <quazip/quazipdir.h>
|
||||||
#include <quazip/quazipfile.h>
|
#include <quazip/quazipfile.h>
|
||||||
#include "MMCZip.h"
|
|
||||||
#include "FileSystem.h"
|
#include "FileSystem.h"
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
// ours
|
// ours
|
||||||
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const FilterFunction filter)
|
bool MMCZip::mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& contained, const FilterFunction filter)
|
||||||
{
|
{
|
||||||
QuaZip modZip(from.filePath());
|
QuaZip modZip(from.filePath());
|
||||||
modZip.open(QuaZip::mdUnzip);
|
modZip.open(QuaZip::mdUnzip);
|
||||||
|
|
||||||
QuaZipFile fileInsideMod(&modZip);
|
QuaZipFile fileInsideMod(&modZip);
|
||||||
QuaZipFile zipOutFile(into);
|
QuaZipFile zipOutFile(into);
|
||||||
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
|
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile()) {
|
||||||
{
|
|
||||||
QString filename = modZip.getCurrentFileName();
|
QString filename = modZip.getCurrentFileName();
|
||||||
if (filter && !filter(filename))
|
if (filter && !filter(filename)) {
|
||||||
{
|
qDebug() << "Skipping file " << filename << " from " << from.fileName() << " - filtered";
|
||||||
qDebug() << "Skipping file " << filename << " from "
|
|
||||||
<< from.fileName() << " - filtered";
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (contained.contains(filename))
|
if (contained.contains(filename)) {
|
||||||
{
|
qDebug() << "Skipping already contained file " << filename << " from " << from.fileName();
|
||||||
qDebug() << "Skipping already contained file " << filename << " from "
|
|
||||||
<< from.fileName();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
contained.insert(filename);
|
contained.insert(filename);
|
||||||
|
|
||||||
if (!fileInsideMod.open(QIODevice::ReadOnly))
|
if (!fileInsideMod.open(QIODevice::ReadOnly)) {
|
||||||
{
|
|
||||||
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
||||||
|
|
||||||
if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
|
if (!zipOutFile.open(QIODevice::WriteOnly, info_out)) {
|
||||||
{
|
|
||||||
qCritical() << "Failed to open " << filename << " in the jar";
|
qCritical() << "Failed to open " << filename << " in the jar";
|
||||||
fileInsideMod.close();
|
fileInsideMod.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!JlCompress::copyData(fileInsideMod, zipOutFile))
|
if (!JlCompress::copyData(fileInsideMod, zipOutFile)) {
|
||||||
{
|
|
||||||
zipOutFile.close();
|
zipOutFile.close();
|
||||||
fileInsideMod.close();
|
fileInsideMod.close();
|
||||||
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
||||||
@ -94,10 +86,11 @@ bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &containe
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MMCZip::compressDirFiles(QuaZip *zip, QString dir, QFileInfoList files, bool followSymlinks)
|
bool MMCZip::compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, bool followSymlinks)
|
||||||
{
|
{
|
||||||
QDir directory(dir);
|
QDir directory(dir);
|
||||||
if (!directory.exists()) return false;
|
if (!directory.exists())
|
||||||
|
return false;
|
||||||
|
|
||||||
for (auto e : files) {
|
for (auto e : files) {
|
||||||
auto filePath = directory.relativeFilePath(e.absoluteFilePath());
|
auto filePath = directory.relativeFilePath(e.absoluteFilePath());
|
||||||
@ -109,7 +102,8 @@ bool MMCZip::compressDirFiles(QuaZip *zip, QString dir, QFileInfoList files, boo
|
|||||||
srcPath = e.canonicalFilePath();
|
srcPath = e.canonicalFilePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( !JlCompress::compressFile(zip, srcPath, filePath)) return false;
|
if (!JlCompress::compressFile(zip, srcPath, filePath))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -119,7 +113,7 @@ bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList
|
|||||||
{
|
{
|
||||||
QuaZip zip(fileCompressed);
|
QuaZip zip(fileCompressed);
|
||||||
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
|
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
|
||||||
if(!zip.open(QuaZip::mdCreate)) {
|
if (!zip.open(QuaZip::mdCreate)) {
|
||||||
QFile::remove(fileCompressed);
|
QFile::remove(fileCompressed);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -127,7 +121,7 @@ bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList
|
|||||||
auto result = compressDirFiles(&zip, dir, files, followSymlinks);
|
auto result = compressDirFiles(&zip, dir, files, followSymlinks);
|
||||||
|
|
||||||
zip.close();
|
zip.close();
|
||||||
if(zip.getZipError()!=0) {
|
if (zip.getZipError() != 0) {
|
||||||
QFile::remove(fileCompressed);
|
QFile::remove(fileCompressed);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -139,8 +133,7 @@ bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList
|
|||||||
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
|
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
|
||||||
{
|
{
|
||||||
QuaZip zipOut(targetJarPath);
|
QuaZip zipOut(targetJarPath);
|
||||||
if (!zipOut.open(QuaZip::mdCreate))
|
if (!zipOut.open(QuaZip::mdCreate)) {
|
||||||
{
|
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to open the minecraft.jar for modding";
|
qCritical() << "Failed to open the minecraft.jar for modding";
|
||||||
return false;
|
return false;
|
||||||
@ -151,37 +144,29 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
|||||||
|
|
||||||
// Modify the jar
|
// Modify the jar
|
||||||
// This needs to be done in reverse-order to ensure we respect the loading order of components
|
// This needs to be done in reverse-order to ensure we respect the loading order of components
|
||||||
for (auto i = mods.crbegin(); i != mods.crend(); i++)
|
for (auto i = mods.crbegin(); i != mods.crend(); i++) {
|
||||||
{
|
|
||||||
const auto* mod = *i;
|
const auto* mod = *i;
|
||||||
// do not merge disabled mods.
|
// do not merge disabled mods.
|
||||||
if (!mod->enabled())
|
if (!mod->enabled())
|
||||||
continue;
|
continue;
|
||||||
if (mod->type() == ResourceType::ZIPFILE)
|
if (mod->type() == ResourceType::ZIPFILE) {
|
||||||
{
|
if (!mergeZipFiles(&zipOut, mod->fileinfo(), addedFiles)) {
|
||||||
if (!mergeZipFiles(&zipOut, mod->fileinfo(), addedFiles))
|
|
||||||
{
|
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
} else if (mod->type() == ResourceType::SINGLEFILE) {
|
||||||
else if (mod->type() == ResourceType::SINGLEFILE)
|
|
||||||
{
|
|
||||||
// FIXME: buggy - does not work with addedFiles
|
// FIXME: buggy - does not work with addedFiles
|
||||||
auto filename = mod->fileinfo();
|
auto filename = mod->fileinfo();
|
||||||
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
|
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName())) {
|
||||||
{
|
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
addedFiles.insert(filename.fileName());
|
addedFiles.insert(filename.fileName());
|
||||||
}
|
} else if (mod->type() == ResourceType::FOLDER) {
|
||||||
else if (mod->type() == ResourceType::FOLDER)
|
|
||||||
{
|
|
||||||
// untested, but seems to be unused / not possible to reach
|
// untested, but seems to be unused / not possible to reach
|
||||||
// FIXME: buggy - does not work with addedFiles
|
// FIXME: buggy - does not work with addedFiles
|
||||||
auto filename = mod->fileinfo();
|
auto filename = mod->fileinfo();
|
||||||
@ -197,18 +182,14 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
|||||||
files.removeAll(e);
|
files.removeAll(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MMCZip::compressDirFiles(&zipOut, parent_dir, files))
|
if (!MMCZip::compressDirFiles(&zipOut, parent_dir, files)) {
|
||||||
{
|
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
qDebug() << "Adding folder " << filename.fileName() << " from "
|
qDebug() << "Adding folder " << filename.fileName() << " from " << filename.absoluteFilePath();
|
||||||
<< filename.absoluteFilePath();
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Make sure we do not continue launching when something is missing or undefined...
|
// Make sure we do not continue launching when something is missing or undefined...
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
@ -217,8 +198,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key){return !key.contains("META-INF");}))
|
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key) { return !key.contains("META-INF"); })) {
|
||||||
{
|
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to insert minecraft.jar contents.";
|
qCritical() << "Failed to insert minecraft.jar contents.";
|
||||||
@ -227,8 +207,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
|||||||
|
|
||||||
// Recompress the jar
|
// Recompress the jar
|
||||||
zipOut.close();
|
zipOut.close();
|
||||||
if (zipOut.getZipError() != 0)
|
if (zipOut.getZipError() != 0) {
|
||||||
{
|
|
||||||
QFile::remove(targetJarPath);
|
QFile::remove(targetJarPath);
|
||||||
qCritical() << "Failed to finalize minecraft.jar!";
|
qCritical() << "Failed to finalize minecraft.jar!";
|
||||||
return false;
|
return false;
|
||||||
@ -261,27 +240,23 @@ QString MMCZip::findFolderOfFileInZip(QuaZip* zip, const QString& what, const QS
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ours
|
// ours
|
||||||
bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root)
|
bool MMCZip::findFilesInZip(QuaZip* zip, const QString& what, QStringList& result, const QString& root)
|
||||||
{
|
{
|
||||||
QuaZipDir rootDir(zip, root);
|
QuaZipDir rootDir(zip, root);
|
||||||
for(auto fileName: rootDir.entryList(QDir::Files))
|
for (auto fileName : rootDir.entryList(QDir::Files)) {
|
||||||
{
|
if (fileName == what) {
|
||||||
if(fileName == what)
|
|
||||||
{
|
|
||||||
result.append(root);
|
result.append(root);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(auto fileName: rootDir.entryList(QDir::Dirs))
|
for (auto fileName : rootDir.entryList(QDir::Dirs)) {
|
||||||
{
|
|
||||||
findFilesInZip(zip, what, result, root + fileName);
|
findFilesInZip(zip, what, result, root + fileName);
|
||||||
}
|
}
|
||||||
return !result.isEmpty();
|
return !result.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ours
|
// ours
|
||||||
std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
std::optional<QStringList> MMCZip::extractSubDir(QuaZip* zip, const QString& subdir, const QString& target)
|
||||||
{
|
{
|
||||||
auto target_top_dir = QUrl::fromLocalFile(target);
|
auto target_top_dir = QUrl::fromLocalFile(target);
|
||||||
|
|
||||||
@ -289,16 +264,13 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & su
|
|||||||
|
|
||||||
qDebug() << "Extracting subdir" << subdir << "from" << zip->getZipName() << "to" << target;
|
qDebug() << "Extracting subdir" << subdir << "from" << zip->getZipName() << "to" << target;
|
||||||
auto numEntries = zip->getEntriesCount();
|
auto numEntries = zip->getEntriesCount();
|
||||||
if(numEntries < 0) {
|
if (numEntries < 0) {
|
||||||
qWarning() << "Failed to enumerate files in archive";
|
qWarning() << "Failed to enumerate files in archive";
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
} else if (numEntries == 0) {
|
||||||
else if(numEntries == 0) {
|
|
||||||
qDebug() << "Extracting empty archives seems odd...";
|
qDebug() << "Extracting empty archives seems odd...";
|
||||||
return extracted;
|
return extracted;
|
||||||
}
|
} else if (!zip->goToFirstFile()) {
|
||||||
else if (!zip->goToFirstFile())
|
|
||||||
{
|
|
||||||
qWarning() << "Failed to seek to first file in zip";
|
qWarning() << "Failed to seek to first file in zip";
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
@ -334,7 +306,8 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & su
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!target_top_dir.isParentOf(QUrl::fromLocalFile(target_file_path))) {
|
if (!target_top_dir.isParentOf(QUrl::fromLocalFile(target_file_path))) {
|
||||||
qWarning() << "Extracting" << relative_file_name << "was cancelled, because it was effectively outside of the target path" << target;
|
qWarning() << "Extracting" << relative_file_name << "was cancelled, because it was effectively outside of the target path"
|
||||||
|
<< target;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,7 +318,8 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & su
|
|||||||
}
|
}
|
||||||
|
|
||||||
extracted.append(target_file_path);
|
extracted.append(target_file_path);
|
||||||
QFile::setPermissions(target_file_path, QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser);
|
QFile::setPermissions(target_file_path,
|
||||||
|
QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser);
|
||||||
|
|
||||||
qDebug() << "Extracted file" << relative_file_name << "to" << target_file_path;
|
qDebug() << "Extracted file" << relative_file_name << "to" << target_file_path;
|
||||||
} while (zip->goToNextFile());
|
} while (zip->goToNextFile());
|
||||||
@ -354,7 +328,7 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & su
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ours
|
// ours
|
||||||
bool MMCZip::extractRelFile(QuaZip *zip, const QString &file, const QString &target)
|
bool MMCZip::extractRelFile(QuaZip* zip, const QString& file, const QString& target)
|
||||||
{
|
{
|
||||||
return JlCompress::extractFile(zip, file, target);
|
return JlCompress::extractFile(zip, file, target);
|
||||||
}
|
}
|
||||||
@ -363,14 +337,14 @@ bool MMCZip::extractRelFile(QuaZip *zip, const QString &file, const QString &tar
|
|||||||
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
||||||
{
|
{
|
||||||
QuaZip zip(fileCompressed);
|
QuaZip zip(fileCompressed);
|
||||||
if (!zip.open(QuaZip::mdUnzip))
|
if (!zip.open(QuaZip::mdUnzip)) {
|
||||||
{
|
|
||||||
// check if this is a minimum size empty zip file...
|
// check if this is a minimum size empty zip file...
|
||||||
QFileInfo fileInfo(fileCompressed);
|
QFileInfo fileInfo(fileCompressed);
|
||||||
if(fileInfo.size() == 22) {
|
if (fileInfo.size() == 22) {
|
||||||
return QStringList();
|
return QStringList();
|
||||||
}
|
}
|
||||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
||||||
|
;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return MMCZip::extractSubDir(&zip, "", dir);
|
return MMCZip::extractSubDir(&zip, "", dir);
|
||||||
@ -380,14 +354,14 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString di
|
|||||||
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
||||||
{
|
{
|
||||||
QuaZip zip(fileCompressed);
|
QuaZip zip(fileCompressed);
|
||||||
if (!zip.open(QuaZip::mdUnzip))
|
if (!zip.open(QuaZip::mdUnzip)) {
|
||||||
{
|
|
||||||
// check if this is a minimum size empty zip file...
|
// check if this is a minimum size empty zip file...
|
||||||
QFileInfo fileInfo(fileCompressed);
|
QFileInfo fileInfo(fileCompressed);
|
||||||
if(fileInfo.size() == 22) {
|
if (fileInfo.size() == 22) {
|
||||||
return QStringList();
|
return QStringList();
|
||||||
}
|
}
|
||||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
||||||
|
;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return MMCZip::extractSubDir(&zip, subdir, dir);
|
return MMCZip::extractSubDir(&zip, subdir, dir);
|
||||||
@ -397,11 +371,10 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString su
|
|||||||
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
||||||
{
|
{
|
||||||
QuaZip zip(fileCompressed);
|
QuaZip zip(fileCompressed);
|
||||||
if (!zip.open(QuaZip::mdUnzip))
|
if (!zip.open(QuaZip::mdUnzip)) {
|
||||||
{
|
|
||||||
// check if this is a minimum size empty zip file...
|
// check if this is a minimum size empty zip file...
|
||||||
QFileInfo fileInfo(fileCompressed);
|
QFileInfo fileInfo(fileCompressed);
|
||||||
if(fileInfo.size() == 22) {
|
if (fileInfo.size() == 22) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
||||||
@ -410,10 +383,14 @@ bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
|||||||
return MMCZip::extractRelFile(&zip, file, target);
|
return MMCZip::extractRelFile(&zip, file, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MMCZip::collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList *files,
|
bool MMCZip::collectFileListRecursively(const QString& rootDir,
|
||||||
MMCZip::FilterFunction excludeFilter) {
|
const QString& subDir,
|
||||||
|
QFileInfoList* files,
|
||||||
|
MMCZip::FilterFunction excludeFilter)
|
||||||
|
{
|
||||||
QDir rootDirectory(rootDir);
|
QDir rootDirectory(rootDir);
|
||||||
if (!rootDirectory.exists()) return false;
|
if (!rootDirectory.exists())
|
||||||
|
return false;
|
||||||
|
|
||||||
QDir directory;
|
QDir directory;
|
||||||
if (subDir == nullptr)
|
if (subDir == nullptr)
|
||||||
@ -421,18 +398,19 @@ bool MMCZip::collectFileListRecursively(const QString& rootDir, const QString& s
|
|||||||
else
|
else
|
||||||
directory = QDir(subDir);
|
directory = QDir(subDir);
|
||||||
|
|
||||||
if (!directory.exists()) return false; // shouldn't ever happen
|
if (!directory.exists())
|
||||||
|
return false; // shouldn't ever happen
|
||||||
|
|
||||||
// recurse directories
|
// recurse directories
|
||||||
QFileInfoList entries = directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
|
QFileInfoList entries = directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
|
||||||
for (const auto& e: entries) {
|
for (const auto& e : entries) {
|
||||||
if (!collectFileListRecursively(rootDir, e.filePath(), files, excludeFilter))
|
if (!collectFileListRecursively(rootDir, e.filePath(), files, excludeFilter))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect files
|
// collect files
|
||||||
entries = directory.entryInfoList(QDir::Files);
|
entries = directory.entryInfoList(QDir::Files);
|
||||||
for (const auto& e: entries) {
|
for (const auto& e : entries) {
|
||||||
QString relativeFilePath = rootDirectory.relativeFilePath(e.absoluteFilePath());
|
QString relativeFilePath = rootDirectory.relativeFilePath(e.absoluteFilePath());
|
||||||
if (excludeFilter && excludeFilter(relativeFilePath)) {
|
if (excludeFilter && excludeFilter(relativeFilePath)) {
|
||||||
qDebug() << "Skipping file " << relativeFilePath;
|
qDebug() << "Skipping file " << relativeFilePath;
|
||||||
|
@ -35,26 +35,24 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QString>
|
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include "minecraft/mod/Mod.h"
|
#include <QString>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include "minecraft/mod/Mod.h"
|
||||||
|
|
||||||
#include <quazip/JlCompress.h>
|
#include <quazip/JlCompress.h>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
namespace MMCZip
|
namespace MMCZip {
|
||||||
{
|
using FilterFunction = std::function<bool(const QString&)>;
|
||||||
using FilterFunction = std::function<bool(const QString &)>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge two zip files, using a filter function
|
* Merge two zip files, using a filter function
|
||||||
*/
|
*/
|
||||||
bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
|
bool mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& contained, const FilterFunction filter = nullptr);
|
||||||
const FilterFunction filter = nullptr);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compress directory, by providing a list of files to compress
|
* Compress directory, by providing a list of files to compress
|
||||||
* \param zip target archive
|
* \param zip target archive
|
||||||
* \param dir directory that will be compressed (to compress with relative paths)
|
* \param dir directory that will be compressed (to compress with relative paths)
|
||||||
@ -62,9 +60,9 @@ namespace MMCZip
|
|||||||
* \param followSymlinks should follow symlinks when compressing file data
|
* \param followSymlinks should follow symlinks when compressing file data
|
||||||
* \return true for success or false for failure
|
* \return true for success or false for failure
|
||||||
*/
|
*/
|
||||||
bool compressDirFiles(QuaZip *zip, QString dir, QFileInfoList files, bool followSymlinks = false);
|
bool compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, bool followSymlinks = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compress directory, by providing a list of files to compress
|
* Compress directory, by providing a list of files to compress
|
||||||
* \param fileCompressed target archive file
|
* \param fileCompressed target archive file
|
||||||
* \param dir directory that will be compressed (to compress with relative paths)
|
* \param dir directory that will be compressed (to compress with relative paths)
|
||||||
@ -72,47 +70,47 @@ namespace MMCZip
|
|||||||
* \param followSymlinks should follow symlinks when compressing file data
|
* \param followSymlinks should follow symlinks when compressing file data
|
||||||
* \return true for success or false for failure
|
* \return true for success or false for failure
|
||||||
*/
|
*/
|
||||||
bool compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files, bool followSymlinks = false);
|
bool compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files, bool followSymlinks = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* take a source jar, add mods to it, resulting in target jar
|
* take a source jar, add mods to it, resulting in target jar
|
||||||
*/
|
*/
|
||||||
bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods);
|
bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a single file in archive by file name (not path)
|
* Find a single file in archive by file name (not path)
|
||||||
*
|
*
|
||||||
* \param ignore_paths paths to skip when recursing the search
|
* \param ignore_paths paths to skip when recursing the search
|
||||||
*
|
*
|
||||||
* \return the path prefix where the file is
|
* \return the path prefix where the file is
|
||||||
*/
|
*/
|
||||||
QString findFolderOfFileInZip(QuaZip * zip, const QString & what, const QStringList& ignore_paths = {}, const QString &root = QString(""));
|
QString findFolderOfFileInZip(QuaZip* zip, const QString& what, const QStringList& ignore_paths = {}, const QString& root = QString(""));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a multiple files of the same name in archive by file name
|
* Find a multiple files of the same name in archive by file name
|
||||||
* If a file is found in a path, no deeper paths are searched
|
* If a file is found in a path, no deeper paths are searched
|
||||||
*
|
*
|
||||||
* \return true if anything was found
|
* \return true if anything was found
|
||||||
*/
|
*/
|
||||||
bool findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root = QString());
|
bool findFilesInZip(QuaZip* zip, const QString& what, QStringList& result, const QString& root = QString());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a subdirectory from an archive
|
* Extract a subdirectory from an archive
|
||||||
*/
|
*/
|
||||||
std::optional<QStringList> extractSubDir(QuaZip *zip, const QString & subdir, const QString &target);
|
std::optional<QStringList> extractSubDir(QuaZip* zip, const QString& subdir, const QString& target);
|
||||||
|
|
||||||
bool extractRelFile(QuaZip *zip, const QString & file, const QString &target);
|
bool extractRelFile(QuaZip* zip, const QString& file, const QString& target);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a whole archive.
|
* Extract a whole archive.
|
||||||
*
|
*
|
||||||
* \param fileCompressed The name of the archive.
|
* \param fileCompressed The name of the archive.
|
||||||
* \param dir The directory to extract to, the current directory if left empty.
|
* \param dir The directory to extract to, the current directory if left empty.
|
||||||
* \return The list of the full paths of the files extracted, empty on failure.
|
* \return The list of the full paths of the files extracted, empty on failure.
|
||||||
*/
|
*/
|
||||||
std::optional<QStringList> extractDir(QString fileCompressed, QString dir);
|
std::optional<QStringList> extractDir(QString fileCompressed, QString dir);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a subdirectory from an archive
|
* Extract a subdirectory from an archive
|
||||||
*
|
*
|
||||||
* \param fileCompressed The name of the archive.
|
* \param fileCompressed The name of the archive.
|
||||||
@ -120,9 +118,9 @@ namespace MMCZip
|
|||||||
* \param dir The directory to extract to, the current directory if left empty.
|
* \param dir The directory to extract to, the current directory if left empty.
|
||||||
* \return The list of the full paths of the files extracted, empty on failure.
|
* \return The list of the full paths of the files extracted, empty on failure.
|
||||||
*/
|
*/
|
||||||
std::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir);
|
std::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a single file from an archive into a directory
|
* Extract a single file from an archive into a directory
|
||||||
*
|
*
|
||||||
* \param fileCompressed The name of the archive.
|
* \param fileCompressed The name of the archive.
|
||||||
@ -130,9 +128,9 @@ namespace MMCZip
|
|||||||
* \param dir The directory to extract to, the current directory if left empty.
|
* \param dir The directory to extract to, the current directory if left empty.
|
||||||
* \return true for success or false for failure
|
* \return true for success or false for failure
|
||||||
*/
|
*/
|
||||||
bool extractFile(QString fileCompressed, QString file, QString dir);
|
bool extractFile(QString fileCompressed, QString file, QString dir);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate a QFileInfoList with a directory tree recursively, while allowing to excludeFilter what shouldn't be included.
|
* Populate a QFileInfoList with a directory tree recursively, while allowing to excludeFilter what shouldn't be included.
|
||||||
* \param rootDir directory to start off
|
* \param rootDir directory to start off
|
||||||
* \param subDir subdirectory, should be nullptr for first invocation
|
* \param subDir subdirectory, should be nullptr for first invocation
|
||||||
@ -140,5 +138,5 @@ namespace MMCZip
|
|||||||
* \param excludeFilter function to excludeFilter which files shouldn't be included (returning true means to excude)
|
* \param excludeFilter function to excludeFilter which files shouldn't be included (returning true means to excude)
|
||||||
* \return true for success or false for failure
|
* \return true for success or false for failure
|
||||||
*/
|
*/
|
||||||
bool collectFileListRecursively(const QString &rootDir, const QString &subDir, QFileInfoList *files, FilterFunction excludeFilter);
|
bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter);
|
||||||
}
|
} // namespace MMCZip
|
||||||
|
@ -72,7 +72,7 @@ ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget* parent
|
|||||||
ui->treeView->setRootIndex(proxyModel->mapFromSource(model->index(root)));
|
ui->treeView->setRootIndex(proxyModel->mapFromSource(model->index(root)));
|
||||||
ui->treeView->sortByColumn(0, Qt::AscendingOrder);
|
ui->treeView->sortByColumn(0, Qt::AscendingOrder);
|
||||||
|
|
||||||
connect(proxyModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(rowsInserted(QModelIndex,int,int)));
|
connect(proxyModel, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
|
||||||
|
|
||||||
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
||||||
model->setRootPath(root);
|
model->setRootPath(root);
|
||||||
@ -92,32 +92,26 @@ void SaveIcon(InstancePtr m_instance)
|
|||||||
auto iconKey = m_instance->iconKey();
|
auto iconKey = m_instance->iconKey();
|
||||||
auto iconList = APPLICATION->icons();
|
auto iconList = APPLICATION->icons();
|
||||||
auto mmcIcon = iconList->icon(iconKey);
|
auto mmcIcon = iconList->icon(iconKey);
|
||||||
if(!mmcIcon || mmcIcon->isBuiltIn()) {
|
if (!mmcIcon || mmcIcon->isBuiltIn()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto path = mmcIcon->getFilePath();
|
auto path = mmcIcon->getFilePath();
|
||||||
if(!path.isNull()) {
|
if (!path.isNull()) {
|
||||||
QFileInfo inInfo (path);
|
QFileInfo inInfo(path);
|
||||||
FS::copy(path, FS::PathCombine(m_instance->instanceRoot(), inInfo.fileName())) ();
|
FS::copy(path, FS::PathCombine(m_instance->instanceRoot(), inInfo.fileName()))();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto & image = mmcIcon->m_images[mmcIcon->type()];
|
auto& image = mmcIcon->m_images[mmcIcon->type()];
|
||||||
auto & icon = image.icon;
|
auto& icon = image.icon;
|
||||||
auto sizes = icon.availableSizes();
|
auto sizes = icon.availableSizes();
|
||||||
if(sizes.size() == 0)
|
if (sizes.size() == 0) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto areaOf = [](QSize size)
|
auto areaOf = [](QSize size) { return size.width() * size.height(); };
|
||||||
{
|
|
||||||
return size.width() * size.height();
|
|
||||||
};
|
|
||||||
QSize largest = sizes[0];
|
QSize largest = sizes[0];
|
||||||
// find variant with largest area
|
// find variant with largest area
|
||||||
for(auto size: sizes)
|
for (auto size : sizes) {
|
||||||
{
|
if (areaOf(largest) < areaOf(size)) {
|
||||||
if(areaOf(largest) < areaOf(size))
|
|
||||||
{
|
|
||||||
largest = size;
|
largest = size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,11 +123,9 @@ bool ExportInstanceDialog::doExport()
|
|||||||
{
|
{
|
||||||
auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
|
auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
|
||||||
|
|
||||||
const QString output = QFileDialog::getSaveFileName(
|
const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(m_instance->name()),
|
||||||
this, tr("Export %1").arg(m_instance->name()),
|
|
||||||
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr);
|
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr);
|
||||||
if (output.isEmpty())
|
if (output.isEmpty()) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,8 +138,7 @@ bool ExportInstanceDialog::doExport()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MMCZip::compressDirFiles(output, m_instance->instanceRoot(), files, true))
|
if (!MMCZip::compressDirFiles(output, m_instance->instanceRoot(), files, true)) {
|
||||||
{
|
|
||||||
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -157,15 +148,11 @@ bool ExportInstanceDialog::doExport()
|
|||||||
void ExportInstanceDialog::done(int result)
|
void ExportInstanceDialog::done(int result)
|
||||||
{
|
{
|
||||||
savePackIgnore();
|
savePackIgnore();
|
||||||
if (result == QDialog::Accepted)
|
if (result == QDialog::Accepted) {
|
||||||
{
|
if (doExport()) {
|
||||||
if (doExport())
|
|
||||||
{
|
|
||||||
QDialog::done(QDialog::Accepted);
|
QDialog::done(QDialog::Accepted);
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,15 +161,12 @@ void ExportInstanceDialog::done(int result)
|
|||||||
|
|
||||||
void ExportInstanceDialog::rowsInserted(QModelIndex parent, int top, int bottom)
|
void ExportInstanceDialog::rowsInserted(QModelIndex parent, int top, int bottom)
|
||||||
{
|
{
|
||||||
//WARNING: possible off-by-one?
|
// WARNING: possible off-by-one?
|
||||||
for(int i = top; i < bottom; i++)
|
for (int i = top; i < bottom; i++) {
|
||||||
{
|
|
||||||
auto node = proxyModel->index(i, 0, parent);
|
auto node = proxyModel->index(i, 0, parent);
|
||||||
if(proxyModel->shouldExpand(node))
|
if (proxyModel->shouldExpand(node)) {
|
||||||
{
|
|
||||||
auto expNode = node.parent();
|
auto expNode = node.parent();
|
||||||
if(!expNode.isValid())
|
if (!expNode.isValid()) {
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ui->treeView->expand(node);
|
ui->treeView->expand(node);
|
||||||
@ -199,8 +183,7 @@ void ExportInstanceDialog::loadPackIgnore()
|
|||||||
{
|
{
|
||||||
auto filename = ignoreFileName();
|
auto filename = ignoreFileName();
|
||||||
QFile ignoreFile(filename);
|
QFile ignoreFile(filename);
|
||||||
if(!ignoreFile.open(QIODevice::ReadOnly))
|
if (!ignoreFile.open(QIODevice::ReadOnly)) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto data = ignoreFile.readAll();
|
auto data = ignoreFile.readAll();
|
||||||
@ -216,12 +199,9 @@ void ExportInstanceDialog::savePackIgnore()
|
|||||||
{
|
{
|
||||||
auto data = proxyModel->blockedPaths().toStringList().join('\n').toUtf8();
|
auto data = proxyModel->blockedPaths().toStringList().join('\n').toUtf8();
|
||||||
auto filename = ignoreFileName();
|
auto filename = ignoreFileName();
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
FS::write(filename, data);
|
FS::write(filename, data);
|
||||||
}
|
} catch (const Exception& e) {
|
||||||
catch (const Exception &e)
|
|
||||||
{
|
|
||||||
qWarning() << e.cause();
|
qWarning() << e.cause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,39 +38,37 @@
|
|||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "FileIgnoreProxy.h"
|
|
||||||
#include "FastFileIconProvider.h"
|
#include "FastFileIconProvider.h"
|
||||||
|
#include "FileIgnoreProxy.h"
|
||||||
|
|
||||||
class BaseInstance;
|
class BaseInstance;
|
||||||
typedef std::shared_ptr<BaseInstance> InstancePtr;
|
typedef std::shared_ptr<BaseInstance> InstancePtr;
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui {
|
||||||
{
|
|
||||||
class ExportInstanceDialog;
|
class ExportInstanceDialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExportInstanceDialog : public QDialog
|
class ExportInstanceDialog : public QDialog {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ExportInstanceDialog(InstancePtr instance, QWidget *parent = 0);
|
explicit ExportInstanceDialog(InstancePtr instance, QWidget* parent = 0);
|
||||||
~ExportInstanceDialog();
|
~ExportInstanceDialog();
|
||||||
|
|
||||||
virtual void done(int result);
|
virtual void done(int result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool doExport();
|
bool doExport();
|
||||||
void loadPackIgnore();
|
void loadPackIgnore();
|
||||||
void savePackIgnore();
|
void savePackIgnore();
|
||||||
QString ignoreFileName();
|
QString ignoreFileName();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::ExportInstanceDialog *ui;
|
Ui::ExportInstanceDialog* ui;
|
||||||
InstancePtr m_instance;
|
InstancePtr m_instance;
|
||||||
FileIgnoreProxy * proxyModel;
|
FileIgnoreProxy* proxyModel;
|
||||||
FastFileIconProvider icons;
|
FastFileIconProvider icons;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void rowsInserted(QModelIndex parent, int top, int bottom);
|
void rowsInserted(QModelIndex parent, int top, int bottom);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user