Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
pushurl = [email protected]:MultiMC/libnbtplusplus.git
[submodule "libraries/quazip"]
path = libraries/quazip
url = https://github.com/MultiMC/quazip.git
pushurl = [email protected]:MultiMC/quazip.git
url = https://github.com/stachenov/quazip.git
pushurl = [email protected]:stachenov/quazip.git
2 changes: 1 addition & 1 deletion launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,8 @@ endif()
add_library(Launcher_logic STATIC ${LOGIC_SOURCES} ${LAUNCHER_SOURCES} ${LAUNCHER_UI} ${LAUNCHER_RESOURCES})
target_link_libraries(Launcher_logic
systeminfo
Launcher_quazip
Launcher_classparser
QuaZip::QuaZip
${NBT_NAME}
${ZLIB_LIBRARIES}
optional-bare
Expand Down
113 changes: 111 additions & 2 deletions launcher/MMCZip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <QDebug>

// ours
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const JlCompress::FilterFunction filter)
bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained, const FilterFunction filter)
{
QuaZip modZip(from.filePath());
modZip.open(QuaZip::mdUnzip);
Expand Down Expand Up @@ -128,7 +128,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
QDir dir(what_to_zip);
dir.cdUp();
QString parent_dir = dir.absolutePath();
if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles))
if (!compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles))
{
zipOut.close();
QFile::remove(targetJarPath);
Expand Down Expand Up @@ -310,3 +310,112 @@ bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
}
return MMCZip::extractRelFile(&zip, file, target);
}

bool MMCZip::compressDir(QString fileCompressed, QString dir, QString prefix, const FilterFunction filter)
{
QuaZip zip(fileCompressed);
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
if(!zip.open(QuaZip::mdCreate))
{
QFile::remove(fileCompressed);
return false;
}

QSet<QString> added;
if (!compressSubDir(&zip, dir, dir, added, prefix, filter))
{
QFile::remove(fileCompressed);
return false;
}
zip.close();
if(zip.getZipError()!=0)
{
QFile::remove(fileCompressed);
return false;
}
return true;
}

bool MMCZip::compressSubDir(QuaZip *zip, QString dir, QString origDir, QSet<QString> &added, QString prefix,
const FilterFunction filter)
{
if (!zip) return false;
if (zip->getMode()!=QuaZip::mdCreate && zip->getMode()!=QuaZip::mdAppend && zip->getMode()!=QuaZip::mdAdd)
{
return false;
}

QDir directory(dir);
if (!directory.exists())
{
return false;
}

QDir origDirectory(origDir);
if (dir != origDir)
{
QString internalDirName = origDirectory.relativeFilePath(dir);
if(!filter || !filter(internalDirName))
{
QuaZipFile dirZipFile(zip);
QString dirPrefix;
if(prefix.isEmpty())
{
dirPrefix = origDirectory.relativeFilePath(dir) + "/";
}
else
{
dirPrefix = prefix + '/' + origDirectory.relativeFilePath(dir) + "/";
}
if (!dirZipFile.open(QIODevice::WriteOnly, QuaZipNewInfo(dirPrefix, dir), 0, 0, 0))
{
return false;
}
dirZipFile.close();
}
}

QFileInfoList files = directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
for (auto file: files)
{
if(!file.isDir())
{
continue;
}
if(!compressSubDir(zip,file.absoluteFilePath(),origDir, added, prefix, filter))
{
return false;
}
}

files = directory.entryInfoList(QDir::Files | QDir::Hidden);
for (auto file: files)
{
if(!file.isFile())
{
continue;
}

if(file.absoluteFilePath()==zip->getZipName())
{
continue;
}

QString filename = origDirectory.relativeFilePath(file.absoluteFilePath());
if(filter && filter(filename))
{
continue;
}
if(prefix.size())
{
filename = prefix + '/' + filename;
}
added.insert(filename);
if (!JlCompress::compressFile(zip,file.absoluteFilePath(),filename))
{
return false;
}
}

return true;
}
15 changes: 14 additions & 1 deletion launcher/MMCZip.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,25 @@

namespace MMCZip
{
using FilterFunction = std::function<bool(const QString &)>;

/**
* Compress a directory, using a filter function
*/
bool compressDir(QString fileCompressed, QString dir, QString prefix = QString(),
const FilterFunction filter = nullptr);

/**
* Compress a subdirectory, using a filter function
*/
bool compressSubDir(QuaZip *zip, QString dir, QString origDir, QSet<QString> &added, QString prefix = QString(),
const FilterFunction filter = nullptr);

/**
* Merge two zip files, using a filter function
*/
bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
const JlCompress::FilterFunction filter = nullptr);
const FilterFunction filter = nullptr);

/**
* take a source jar, add mods to it, resulting in target jar
Expand Down
2 changes: 1 addition & 1 deletion libraries/classparser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ add_definitions(-DCLASSPARSER_LIBRARY)

add_library(Launcher_classparser STATIC ${CLASSPARSER_SOURCES} ${CLASSPARSER_HEADERS})
target_include_directories(Launcher_classparser PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(Launcher_classparser Launcher_quazip Qt5::Core)
target_link_libraries(Launcher_classparser QuaZip::QuaZip Qt5::Core)
2 changes: 1 addition & 1 deletion libraries/quazip