From 4d8f068f9cc576c7d1fb19551cb2429282a7c449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 12 Apr 2015 22:50:52 +0200 Subject: NOISSUE refactor and rearrange zip file utils --- logic/minecraft/JarUtils.cpp | 158 ------------------------------------- logic/minecraft/JarUtils.h | 18 ----- logic/minecraft/LegacyUpdate.cpp | 5 +- logic/minecraft/OneSixInstance.cpp | 1 + logic/minecraft/OneSixUpdate.cpp | 4 +- 5 files changed, 5 insertions(+), 181 deletions(-) delete mode 100644 logic/minecraft/JarUtils.cpp delete mode 100644 logic/minecraft/JarUtils.h (limited to 'logic/minecraft') diff --git a/logic/minecraft/JarUtils.cpp b/logic/minecraft/JarUtils.cpp deleted file mode 100644 index b8fb6f0d..00000000 --- a/logic/minecraft/JarUtils.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "minecraft/JarUtils.h" -#include -#include -#include -#include - -namespace JarUtils { - -bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, - std::function filter) -{ - 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(); - if (!filter(filename)) - { - 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; - } - if (!JlCompress::copyData(fileInsideMod, zipOutFile)) - { - zipOutFile.close(); - fileInsideMod.close(); - qCritical() << "Failed to copy data of " << filename << " into the jar"; - return false; - } - zipOutFile.close(); - fileInsideMod.close(); - } - return true; -} - -bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList& 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 addedFiles; - - // Modify the jar - QListIterator 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) - { - if (!mergeZipFiles(&zipOut, mod.filename(), addedFiles, noFilter)) - { - zipOut.close(); - QFile::remove(targetJarPath); - qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar."; - return false; - } - } - else if (mod.type() == Mod::MOD_SINGLEFILE) - { - auto filename = mod.filename(); - if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), - filename.fileName())) - { - 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) - { - auto filename = mod.filename(); - QString what_to_zip = filename.absoluteFilePath(); - QDir dir(what_to_zip); - dir.cdUp(); - QString parent_dir = dir.absolutePath(); - if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, true, addedFiles)) - { - 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(); - } - } - - if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, metaInfFilter)) - { - 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; -} - -bool noFilter(QString) -{ - return true; -} - -bool metaInfFilter(QString key) -{ - if(key.contains("META-INF")) - { - return false; - } - return true; -} - -} diff --git a/logic/minecraft/JarUtils.h b/logic/minecraft/JarUtils.h deleted file mode 100644 index 2e8bd2a7..00000000 --- a/logic/minecraft/JarUtils.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include -#include -#include -#include "Mod.h" -#include - -class QuaZip; -namespace JarUtils -{ - bool noFilter(QString); - bool metaInfFilter(QString key); - - bool mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, - std::function filter); - - bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList& mods); -} diff --git a/logic/minecraft/LegacyUpdate.cpp b/logic/minecraft/LegacyUpdate.cpp index e2fffbb3..6c0a4cdf 100644 --- a/logic/minecraft/LegacyUpdate.cpp +++ b/logic/minecraft/LegacyUpdate.cpp @@ -17,14 +17,13 @@ #include #include #include -#include #include #include "Env.h" #include "BaseInstance.h" #include "net/URLConstants.h" +#include "MMCZip.h" -#include "minecraft/JarUtils.h" #include "minecraft/LegacyUpdate.h" #include "minecraft/LwjglVersionList.h" #include "minecraft/MinecraftVersionList.h" @@ -455,7 +454,7 @@ void LegacyUpdate::ModTheJar() QString outputJarPath = runnableJar.filePath(); QString inputJarPath = baseJar.filePath(); - if(!JarUtils::createModdedJar(inputJarPath, outputJarPath, modList)) + if(!MMCZip::createModdedJar(inputJarPath, outputJarPath, modList)) { emitFailed(tr("Failed to create the custom Minecraft jar file.")); return; diff --git a/logic/minecraft/OneSixInstance.cpp b/logic/minecraft/OneSixInstance.cpp index 7a29c4b4..e9989f2e 100644 --- a/logic/minecraft/OneSixInstance.cpp +++ b/logic/minecraft/OneSixInstance.cpp @@ -25,6 +25,7 @@ #include "minecraft/VersionBuildError.h" #include "minecraft/MinecraftProcess.h" #include "minecraft/OneSixProfileStrategy.h" +#include "MMCZip.h" #include "minecraft/AssetsUtils.h" #include "icons/IconList.h" diff --git a/logic/minecraft/OneSixUpdate.cpp b/logic/minecraft/OneSixUpdate.cpp index 6cea2341..8a7a1f66 100644 --- a/logic/minecraft/OneSixUpdate.cpp +++ b/logic/minecraft/OneSixUpdate.cpp @@ -33,7 +33,7 @@ #include "forge/ForgeMirrors.h" #include "net/URLConstants.h" #include "minecraft/AssetsUtils.h" -#include "minecraft/JarUtils.h" +#include "MMCZip.h" OneSixUpdate::OneSixUpdate(OneSixInstance *inst, QObject *parent) : Task(parent), m_inst(inst) { @@ -320,7 +320,7 @@ void OneSixUpdate::jarlibFinished() auto metacache = ENV.metacache(); auto entry = metacache->resolveEntry("versions", localPath); QString fullJarPath = entry->getFullPath(); - if(!JarUtils::createModdedJar(sourceJarPath, finalJarPath, jarMods)) + if(!MMCZip::createModdedJar(sourceJarPath, finalJarPath, jarMods)) { emitFailed(tr("Failed to create the custom Minecraft jar file.")); return; -- cgit v1.2.3