Merge pull request #576 from Ryex/identify-zip-packs

fix https://github.com/PrismLauncher/PrismLauncher/issues/349
This commit is contained in:
Sefa Eyeoglu
2023-01-07 17:33:34 +01:00
committed by GitHub
48 changed files with 1858 additions and 162 deletions

View File

@ -53,6 +53,13 @@
#include "ui/dialogs/BlockedModsDialog.h"
#include "ui/dialogs/CustomMessageBox.h"
#include <QDebug>
#include <QFileInfo>
#include "minecraft/World.h"
#include "minecraft/mod/tasks/LocalResourceParse.h"
const static QMap<QString, QString> forgemap = { { "1.2.5", "3.4.9.171" },
{ "1.4.2", "6.0.1.355" },
{ "1.4.7", "6.6.2.534" },
@ -401,6 +408,10 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
QList<BlockedMod> blocked_mods;
auto anyBlocked = false;
for (const auto& result : results.files.values()) {
if (result.fileName.endsWith(".zip")) {
m_ZIP_resources.append(std::make_pair(result.fileName, result.targetFolder));
}
if (!result.resolved || result.url.isEmpty()) {
BlockedMod blocked_mod;
blocked_mod.name = result.fileName;
@ -439,38 +450,6 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
}
}
/// @brief copy the matched blocked mods to the instance staging area
/// @param blocked_mods list of the blocked mods and their matched paths
void FlameCreationTask::copyBlockedMods(QList<BlockedMod> const& blocked_mods)
{
setStatus(tr("Copying Blocked Mods..."));
setAbortable(false);
int i = 0;
int total = blocked_mods.length();
setProgress(i, total);
for (auto const& mod : blocked_mods) {
if (!mod.matched) {
qDebug() << mod.name << "was not matched to a local file, skipping copy";
continue;
}
auto dest_path = FS::PathCombine(m_stagingPath, "minecraft", mod.targetFolder, mod.name);
setStatus(tr("Copying Blocked Mods (%1 out of %2 are done)").arg(QString::number(i), QString::number(total)));
qDebug() << "Will try to copy" << mod.localPath << "to" << dest_path;
if (!FS::copy(mod.localPath, dest_path)()) {
qDebug() << "Copy of" << mod.localPath << "to" << dest_path << "Failed";
}
i++;
setProgress(i, total);
}
setAbortable(true);
}
void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
{
m_files_job = new NetJob(tr("Mod download"), APPLICATION->network());
@ -509,7 +488,10 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
}
m_mod_id_resolver.reset();
connect(m_files_job.get(), &NetJob::succeeded, this, [&]() { m_files_job.reset(); });
connect(m_files_job.get(), &NetJob::succeeded, this, [&]() {
m_files_job.reset();
validateZIPResouces();
});
connect(m_files_job.get(), &NetJob::failed, [&](QString reason) {
m_files_job.reset();
setError(reason);
@ -520,3 +502,103 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
setStatus(tr("Downloading mods..."));
m_files_job->start();
}
/// @brief copy the matched blocked mods to the instance staging area
/// @param blocked_mods list of the blocked mods and their matched paths
void FlameCreationTask::copyBlockedMods(QList<BlockedMod> const& blocked_mods)
{
setStatus(tr("Copying Blocked Mods..."));
setAbortable(false);
int i = 0;
int total = blocked_mods.length();
setProgress(i, total);
for (auto const& mod : blocked_mods) {
if (!mod.matched) {
qDebug() << mod.name << "was not matched to a local file, skipping copy";
continue;
}
auto destPath = FS::PathCombine(m_stagingPath, "minecraft", mod.targetFolder, mod.name);
setStatus(tr("Copying Blocked Mods (%1 out of %2 are done)").arg(QString::number(i), QString::number(total)));
qDebug() << "Will try to copy" << mod.localPath << "to" << destPath;
if (!FS::copy(mod.localPath, destPath)()) {
qDebug() << "Copy of" << mod.localPath << "to" << destPath << "Failed";
}
i++;
setProgress(i, total);
}
setAbortable(true);
}
void FlameCreationTask::validateZIPResouces()
{
qDebug() << "Validating whether resources stored as .zip are in the right place";
for (auto [fileName, targetFolder] : m_ZIP_resources) {
qDebug() << "Checking" << fileName << "...";
auto localPath = FS::PathCombine(m_stagingPath, "minecraft", targetFolder, fileName);
/// @brief check the target and move the the file
/// @return path where file can now be found
auto validatePath = [&localPath, this](QString fileName, QString targetFolder, QString realTarget) {
if (targetFolder != realTarget) {
qDebug() << "Target folder of" << fileName << "is incorrect, it belongs in" << realTarget;
auto destPath = FS::PathCombine(m_stagingPath, "minecraft", realTarget, fileName);
qDebug() << "Moving" << localPath << "to" << destPath;
if (FS::move(localPath, destPath)) {
return destPath;
}
}
return localPath;
};
auto installWorld = [this](QString worldPath){
qDebug() << "Installing World from" << worldPath;
QFileInfo worldFileInfo(worldPath);
World w(worldFileInfo);
if (!w.isValid()) {
qDebug() << "World at" << worldPath << "is not valid, skipping install.";
} else {
w.install(FS::PathCombine(m_stagingPath, "minecraft", "saves"));
}
};
QFileInfo localFileInfo(localPath);
auto type = ResourceUtils::identify(localFileInfo);
QString worldPath;
switch (type) {
case PackedResourceType::ResourcePack :
validatePath(fileName, targetFolder, "resourcepacks");
break;
case PackedResourceType::TexturePack :
validatePath(fileName, targetFolder, "texturepacks");
break;
case PackedResourceType::DataPack :
validatePath(fileName, targetFolder, "datapacks");
break;
case PackedResourceType::Mod :
validatePath(fileName, targetFolder, "mods");
break;
case PackedResourceType::ShaderPack :
// in theroy flame API can't do this but who knows, that *may* change ?
// better to handle it if it *does* occure in the future
validatePath(fileName, targetFolder, "shaderpacks");
break;
case PackedResourceType::WorldSave :
worldPath = validatePath(fileName, targetFolder, "saves");
installWorld(worldPath);
break;
case PackedResourceType::UNKNOWN :
default :
qDebug() << "Can't Identify" << fileName << "at" << localPath << ", leaving it where it is.";
break;
}
}
}

View File

@ -77,6 +77,7 @@ class FlameCreationTask final : public InstanceCreationTask {
void idResolverSucceeded(QEventLoop&);
void setupDownloadJob(QEventLoop&);
void copyBlockedMods(QList<BlockedMod> const& blocked_mods);
void validateZIPResouces();
private:
QWidget* m_parent = nullptr;
@ -90,5 +91,7 @@ class FlameCreationTask final : public InstanceCreationTask {
QString m_managed_id, m_managed_version_id;
QList<std::pair<QString, QString>> m_ZIP_resources;
std::optional<InstancePtr> m_instance;
};