finish moving all code to libarchive

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2025-07-11 21:54:33 +03:00
parent e0fad4a784
commit ace9ad29a6
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 67 additions and 109 deletions

View File

@ -73,6 +73,7 @@ runs:
cmark:p cmark:p
tomlplusplus:p tomlplusplus:p
quazip-qt6:p quazip-qt6:p
libarchive:p
- name: List pacman packages (MinGW) - name: List pacman packages (MinGW)
if: ${{ inputs.msystem != '' }} if: ${{ inputs.msystem != '' }}

View File

@ -1,8 +1,7 @@
#include "LocalModParseTask.h" #include "LocalModParseTask.h"
#include <qdcss.h> #include <qdcss.h>
#include <quazip/quazip.h> #include <qstringview.h>
#include <quazip/quazipfile.h>
#include <toml++/toml.h> #include <toml++/toml.h>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
@ -13,6 +12,7 @@
#include "FileSystem.h" #include "FileSystem.h"
#include "Json.h" #include "Json.h"
#include "archive/ArchiveReader.h"
#include "minecraft/mod/ModDetails.h" #include "minecraft/mod/ModDetails.h"
#include "settings/INIFile.h" #include "settings/INIFile.h"
@ -470,31 +470,35 @@ bool processZIP(Mod& mod, [[maybe_unused]] ProcessingLevel level)
{ {
ModDetails details; ModDetails details;
QuaZip zip(mod.fileinfo().filePath()); MMCZip::ArchiveReader zip(mod.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip)) if (!zip.collectFiles())
return false; return false;
QuaZipFile file(&zip); auto isForge = zip.exists("META-INF/mods.toml");
if (isForge || zip.exists("META-INF/neoforge.mods.toml")) {
if (zip.setCurrentFile("META-INF/mods.toml") || zip.setCurrentFile("META-INF/neoforge.mods.toml")) { {
if (!file.open(QIODevice::ReadOnly)) { std::unique_ptr<MMCZip::ArchiveReader::File> file;
zip.close(); if (isForge) {
file = zip.goToFile("META-INF/mods.toml");
} else {
file = zip.goToFile("META-INF/neoforge.mods.toml");
}
if (!file) {
return false; return false;
} }
details = ReadMCModTOML(file.readAll()); details = ReadMCModTOML(file->readAll());
file.close(); }
// to replace ${file.jarVersion} with the actual version, as needed // to replace ${file.jarVersion} with the actual version, as needed
if (details.version == "${file.jarVersion}") { if (details.version == "${file.jarVersion}") {
if (zip.setCurrentFile("META-INF/MANIFEST.MF")) { if (zip.exists("META-INF/MANIFEST.MF")) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile("META-INF/MANIFEST.MF");
zip.close(); if (!file) {
return false; return false;
} }
// quick and dirty line-by-line parser // quick and dirty line-by-line parser
auto manifestLines = QString(file.readAll()).split(s_newlineRegex); auto manifestLines = QString(file->readAll()).split(s_newlineRegex);
QString manifestVersion = ""; QString manifestVersion = "";
for (auto& line : manifestLines) { for (auto& line : manifestLines) {
if (line.startsWith("Implementation-Version: ", Qt::CaseInsensitive)) { if (line.startsWith("Implementation-Version: ", Qt::CaseInsensitive)) {
@ -510,69 +514,58 @@ bool processZIP(Mod& mod, [[maybe_unused]] ProcessingLevel level)
} }
details.version = manifestVersion; details.version = manifestVersion;
file.close();
} }
} }
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} else if (zip.setCurrentFile("mcmod.info")) { } else if (zip.exists("mcmod.info")) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile("mcmod.info");
zip.close(); if (!file) {
return false; return false;
} }
details = ReadMCModInfo(file.readAll()); details = ReadMCModInfo(file->readAll());
file.close();
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} else if (zip.setCurrentFile("quilt.mod.json")) { } else if (zip.exists("quilt.mod.json")) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile("quilt.mod.json");
zip.close(); if (!file) {
return false; return false;
} }
details = ReadQuiltModInfo(file.readAll()); details = ReadQuiltModInfo(file->readAll());
file.close();
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} else if (zip.setCurrentFile("fabric.mod.json")) { } else if (zip.exists("fabric.mod.json")) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile("fabric.mod.json");
zip.close(); if (!file) {
return false; return false;
} }
details = ReadFabricModInfo(file.readAll()); details = ReadFabricModInfo(file->readAll());
file.close();
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} else if (zip.setCurrentFile("forgeversion.properties")) { } else if (zip.exists("forgeversion.properties")) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile("forgeversion.properties");
zip.close(); if (!file) {
return false; return false;
} }
details = ReadForgeInfo(file.readAll()); details = ReadForgeInfo(file->readAll());
file.close();
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} else if (zip.setCurrentFile("META-INF/nil/mappings.json")) { } else if (zip.exists("META-INF/nil/mappings.json")) {
// nilloader uses the filename of the metadata file for the modid, so we can't know the exact filename // nilloader uses the filename of the metadata file for the modid, so we can't know the exact filename
// thankfully, there is a good file to use as a canary so we don't look for nil meta all the time // thankfully, there is a good file to use as a canary so we don't look for nil meta all the time
QString foundNilMeta; QString foundNilMeta;
for (auto& fname : zip.getFileNameList()) { for (auto& fname : zip.getFiles()) {
// nilmods can shade nilloader to be able to run as a standalone agent - which includes nilloader's own meta file // nilmods can shade nilloader to be able to run as a standalone agent - which includes nilloader's own meta file
if (fname.endsWith(".nilmod.css") && fname != "nilloader.nilmod.css") { if (fname.endsWith(".nilmod.css") && fname != "nilloader.nilmod.css") {
foundNilMeta = fname; foundNilMeta = fname;
@ -580,22 +573,18 @@ bool processZIP(Mod& mod, [[maybe_unused]] ProcessingLevel level)
} }
} }
if (zip.setCurrentFile(foundNilMeta)) { if (zip.exists(foundNilMeta)) {
if (!file.open(QIODevice::ReadOnly)) { auto file = zip.goToFile(foundNilMeta);
zip.close(); if (!file) {
return false; return false;
} }
details = ReadNilModInfo(file->readAll(), foundNilMeta);
details = ReadNilModInfo(file.readAll(), foundNilMeta);
file.close();
zip.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} }
} }
zip.close();
return false; // no valid mod found in archive return false; // no valid mod found in archive
} }
@ -624,25 +613,14 @@ bool processLitemod(Mod& mod, [[maybe_unused]] ProcessingLevel level)
{ {
ModDetails details; ModDetails details;
QuaZip zip(mod.fileinfo().filePath()); MMCZip::ArchiveReader zip(mod.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip))
return false;
QuaZipFile file(&zip); if (auto file = zip.goToFile("litemod.json"); file) {
details = ReadLiteModInfo(file->readAll());
if (zip.setCurrentFile("litemod.json")) {
if (!file.open(QIODevice::ReadOnly)) {
zip.close();
return false;
}
details = ReadLiteModInfo(file.readAll());
file.close();
mod.setDetails(details); mod.setDetails(details);
return true; return true;
} }
zip.close();
return false; // no valid litemod.json found in archive return false; // no valid litemod.json found in archive
} }
@ -700,24 +678,13 @@ bool loadIconFile(const Mod& mod, QPixmap* pixmap)
return png_invalid("file '" + icon_info.filePath() + "' does not exists or is not a file"); return png_invalid("file '" + icon_info.filePath() + "' does not exists or is not a file");
} }
case ResourceType::ZIPFILE: { case ResourceType::ZIPFILE: {
QuaZip zip(mod.fileinfo().filePath()); MMCZip::ArchiveReader zip(mod.fileinfo().filePath());
if (!zip.open(QuaZip::mdUnzip)) auto file = zip.goToFile(mod.iconPath());
return png_invalid("failed to open '" + mod.fileinfo().filePath() + "' as a zip archive"); if (file) {
auto data = file->readAll();
QuaZipFile file(&zip);
if (zip.setCurrentFile(mod.iconPath())) {
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open file in zip.";
zip.close();
return png_invalid("Failed to open '" + mod.iconPath() + "' in zip archive");
}
auto data = file.readAll();
bool icon_result = ModUtils::processIconPNG(mod, std::move(data), pixmap); bool icon_result = ModUtils::processIconPNG(mod, std::move(data), pixmap);
file.close();
if (!icon_result) { if (!icon_result) {
return png_invalid("invalid png image"); // icon png invalid return png_invalid("invalid png image"); // icon png invalid
} }

View File

@ -133,8 +133,7 @@ static std::tuple<bool, QString, bool> contains_level_dat(QString fileName)
// Check that there's nothing between worldName/ and level.dat // Check that there's nothing between worldName/ and level.dat
if (remaining == "level.dat") { if (remaining == "level.dat") {
QString worldDir = (saves ? "saves/" : "") + worldName; return std::make_tuple(true, worldName, saves);
return std::make_tuple(true, worldDir, saves);
} }
} }

View File

@ -19,12 +19,10 @@
#include <Json.h> #include <Json.h>
#include <minecraft/MinecraftInstance.h> #include <minecraft/MinecraftInstance.h>
#include <minecraft/PackProfile.h> #include <minecraft/PackProfile.h>
#include <quazip/quazip.h>
#include <quazip/quazipdir.h>
#include <quazip/quazipfile.h>
#include <settings/INISettingsObject.h> #include <settings/INISettingsObject.h>
#include <memory> #include <memory>
#include "archive/ArchiveReader.h"
void Technic::TechnicPackProcessor::run(SettingsObjectPtr globalSettings, void Technic::TechnicPackProcessor::run(SettingsObjectPtr globalSettings,
const QString& instName, const QString& instName,
@ -53,35 +51,30 @@ void Technic::TechnicPackProcessor::run(SettingsObjectPtr globalSettings,
QString versionJson = FS::PathCombine(minecraftPath, "bin", "version.json"); QString versionJson = FS::PathCombine(minecraftPath, "bin", "version.json");
QString fmlMinecraftVersion; QString fmlMinecraftVersion;
if (QFile::exists(modpackJar)) { if (QFile::exists(modpackJar)) {
QuaZip zipFile(modpackJar); MMCZip::ArchiveReader zipFile(modpackJar);
if (!zipFile.open(QuaZip::mdUnzip)) { if (!zipFile.collectFiles()) {
emit failed(tr("Unable to open \"bin/modpack.jar\" file!")); emit failed(tr("Unable to open \"bin/modpack.jar\" file!"));
return; return;
} }
QuaZipDir zipFileRoot(&zipFile, "/"); if (zipFile.exists("/version.json")) {
if (zipFileRoot.exists("/version.json")) { if (zipFile.exists("/fmlversion.properties")) {
if (zipFileRoot.exists("/fmlversion.properties")) { auto file = zipFile.goToFile("fmlversion.properties");
zipFile.setCurrentFile("fmlversion.properties"); if (!file) {
QuaZipFile file(&zipFile);
if (!file.open(QIODevice::ReadOnly)) {
emit failed(tr("Unable to open \"fmlversion.properties\"!")); emit failed(tr("Unable to open \"fmlversion.properties\"!"));
return; return;
} }
QByteArray fmlVersionData = file.readAll(); QByteArray fmlVersionData = file->readAll();
file.close();
INIFile iniFile; INIFile iniFile;
iniFile.loadFile(fmlVersionData); iniFile.loadFile(fmlVersionData);
// If not present, this evaluates to a null string // If not present, this evaluates to a null string
fmlMinecraftVersion = iniFile["fmlbuild.mcversion"].toString(); fmlMinecraftVersion = iniFile["fmlbuild.mcversion"].toString();
} }
zipFile.setCurrentFile("version.json", QuaZip::csSensitive); auto file = zipFile.goToFile("version.json");
QuaZipFile file(&zipFile); if (!file) {
if (!file.open(QIODevice::ReadOnly)) {
emit failed(tr("Unable to open \"version.json\"!")); emit failed(tr("Unable to open \"version.json\"!"));
return; return;
} }
data = file.readAll(); data = file->readAll();
file.close();
} else { } else {
if (minecraftVersion.isEmpty()) { if (minecraftVersion.isEmpty()) {
emit failed(tr("Could not find \"version.json\" inside \"bin/modpack.jar\", but Minecraft version is unknown")); emit failed(tr("Could not find \"version.json\" inside \"bin/modpack.jar\", but Minecraft version is unknown"));
@ -93,16 +86,14 @@ void Technic::TechnicPackProcessor::run(SettingsObjectPtr globalSettings,
// Forge for 1.4.7 and for 1.5.2 require extra libraries. // Forge for 1.4.7 and for 1.5.2 require extra libraries.
// Figure out the forge version and add it as a component // Figure out the forge version and add it as a component
// (the code still comes from the jar mod installed above) // (the code still comes from the jar mod installed above)
if (zipFileRoot.exists("/forgeversion.properties")) { if (zipFile.exists("/forgeversion.properties")) {
zipFile.setCurrentFile("forgeversion.properties", QuaZip::csSensitive); auto file = zipFile.goToFile("forgeversion.properties");
QuaZipFile file(&zipFile); if (!file) {
if (!file.open(QIODevice::ReadOnly)) {
// Really shouldn't happen, but error handling shall not be forgotten // Really shouldn't happen, but error handling shall not be forgotten
emit failed(tr("Unable to open \"forgeversion.properties\"")); emit failed(tr("Unable to open \"forgeversion.properties\""));
return; return;
} }
QByteArray forgeVersionData = file.readAll(); auto forgeVersionData = file->readAll();
file.close();
INIFile iniFile; INIFile iniFile;
iniFile.loadFile(forgeVersionData); iniFile.loadFile(forgeVersionData);
QString major, minor, revision, build; QString major, minor, revision, build;