From 1e3dd6dc65728c77d21df7dae1c748b9413b1a8e Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Mon, 18 Aug 2025 00:02:11 +0200 Subject: [PATCH] Switch to upstream QuaZip --- .gitmodules | 4 +- launcher/CMakeLists.txt | 2 +- launcher/MMCZip.cpp | 113 ++++++++++++++++++++++++++- launcher/MMCZip.h | 15 +++- libraries/classparser/CMakeLists.txt | 2 +- libraries/quazip | 2 +- 6 files changed, 130 insertions(+), 8 deletions(-) diff --git a/.gitmodules b/.gitmodules index 04a561c2c9..c657e7e139 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,5 +4,5 @@ pushurl = git@github.com:MultiMC/libnbtplusplus.git [submodule "libraries/quazip"] path = libraries/quazip - url = https://github.com/MultiMC/quazip.git - pushurl = git@github.com:MultiMC/quazip.git + url = https://github.com/stachenov/quazip.git + pushurl = git@github.com:stachenov/quazip.git diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 6dc2bb0f01..cae52917df 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -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 diff --git a/launcher/MMCZip.cpp b/launcher/MMCZip.cpp index b25c61e759..5bcba51980 100644 --- a/launcher/MMCZip.cpp +++ b/launcher/MMCZip.cpp @@ -23,7 +23,7 @@ #include // ours -bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, const JlCompress::FilterFunction filter) +bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, const FilterFunction filter) { QuaZip modZip(from.filePath()); modZip.open(QuaZip::mdUnzip); @@ -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); @@ -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 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 &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; +} diff --git a/launcher/MMCZip.h b/launcher/MMCZip.h index 9c47fa11f2..d5a26953a3 100644 --- a/launcher/MMCZip.h +++ b/launcher/MMCZip.h @@ -26,12 +26,25 @@ namespace MMCZip { + using FilterFunction = std::function; + + /** + * 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 &added, QString prefix = QString(), + const FilterFunction filter = nullptr); /** * Merge two zip files, using a filter function */ bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, - const JlCompress::FilterFunction filter = nullptr); + const FilterFunction filter = nullptr); /** * take a source jar, add mods to it, resulting in target jar diff --git a/libraries/classparser/CMakeLists.txt b/libraries/classparser/CMakeLists.txt index c07e871c05..fc510e6865 100644 --- a/libraries/classparser/CMakeLists.txt +++ b/libraries/classparser/CMakeLists.txt @@ -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) diff --git a/libraries/quazip b/libraries/quazip index b1a72ac0bb..3fd3b299b8 160000 --- a/libraries/quazip +++ b/libraries/quazip @@ -1 +1 @@ -Subproject commit b1a72ac0bb5a732bf887a535ab75c6f9bedb6b6b +Subproject commit 3fd3b299b875fbd2beac4894b8a870d80022cad7