feat: updated flame mod resolution

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-20 19:59:58 +03:00
parent 593f452986
commit 60d0013bc4
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
5 changed files with 86 additions and 33 deletions

View File

@ -30,25 +30,19 @@
#include "LocalTexturePackParseTask.h" #include "LocalTexturePackParseTask.h"
#include "LocalWorldSaveParseTask.h" #include "LocalWorldSaveParseTask.h"
static const QMap<PackedResourceType, QString> s_packed_type_names = { { PackedResourceType::ResourcePack, QObject::tr("resource pack") },
static const QMap<PackedResourceType, QString> s_packed_type_names = { { PackedResourceType::TexturePack, QObject::tr("texture pack") },
{PackedResourceType::ResourcePack, QObject::tr("resource pack")}, { PackedResourceType::DataPack, QObject::tr("data pack") },
{PackedResourceType::TexturePack, QObject::tr("texture pack")}, { PackedResourceType::ShaderPack, QObject::tr("shader pack") },
{PackedResourceType::DataPack, QObject::tr("data pack")}, { PackedResourceType::WorldSave, QObject::tr("world save") },
{PackedResourceType::ShaderPack, QObject::tr("shader pack")}, { PackedResourceType::Mod, QObject::tr("mod") },
{PackedResourceType::WorldSave, QObject::tr("world save")}, { PackedResourceType::UNKNOWN, QObject::tr("unknown") } };
{PackedResourceType::Mod , QObject::tr("mod")},
{PackedResourceType::UNKNOWN, QObject::tr("unknown")}
};
namespace ResourceUtils { namespace ResourceUtils {
PackedResourceType identify(QFileInfo file){ PackedResourceType identify(QFileInfo file)
{
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
if (ModUtils::validate(file)) { if (ResourcePackUtils::validate(file)) {
// mods can contain resource and data packs so they must be tested first
qDebug() << file.fileName() << "is a mod";
return PackedResourceType::Mod;
} else if (ResourcePackUtils::validate(file)) {
qDebug() << file.fileName() << "is a resource pack"; qDebug() << file.fileName() << "is a resource pack";
return PackedResourceType::ResourcePack; return PackedResourceType::ResourcePack;
} else if (TexturePackUtils::validate(file)) { } else if (TexturePackUtils::validate(file)) {
@ -63,8 +57,12 @@ PackedResourceType identify(QFileInfo file){
} else if (ShaderPackUtils::validate(file)) { } else if (ShaderPackUtils::validate(file)) {
qDebug() << file.fileName() << "is a shader pack"; qDebug() << file.fileName() << "is a shader pack";
return PackedResourceType::ShaderPack; return PackedResourceType::ShaderPack;
} else if (ModUtils::validate(file)) {
// mods should be already filtered out by the API response
qDebug() << file.fileName() << "is a mod";
return PackedResourceType::Mod;
} else { } else {
qDebug() << "Can't Identify" << file.fileName() ; qDebug() << "Can't Identify" << file.fileName();
} }
} else { } else {
qDebug() << "Can't find" << file.absolutePath(); qDebug() << "Can't find" << file.absolutePath();
@ -72,8 +70,9 @@ PackedResourceType identify(QFileInfo file){
return PackedResourceType::UNKNOWN; return PackedResourceType::UNKNOWN;
} }
QString getPackedTypeName(PackedResourceType type) { QString getPackedTypeName(PackedResourceType type)
{
return s_packed_type_names.constFind(type).value(); return s_packed_type_names.constFind(type).value();
} }
} } // namespace ResourceUtils

View File

@ -1,6 +1,7 @@
#include "FileResolvingTask.h" #include "FileResolvingTask.h"
#include "Json.h" #include "Json.h"
#include "minecraft/mod/tasks/LocalResourceParse.h"
#include "net/Upload.h" #include "net/Upload.h"
#include "modplatform/modrinth/ModrinthPackIndex.h" #include "modplatform/modrinth/ModrinthPackIndex.h"
@ -29,6 +30,7 @@ void Flame::FileResolvingTask::executeTask()
setProgress(0, 3); setProgress(0, 3);
m_dljob.reset(new NetJob("Mod id resolver", m_network)); m_dljob.reset(new NetJob("Mod id resolver", m_network));
result.reset(new QByteArray()); result.reset(new QByteArray());
mods_result.reset(new QByteArray());
// build json data to send // build json data to send
QJsonObject object; QJsonObject object;
@ -37,9 +39,20 @@ void Flame::FileResolvingTask::executeTask()
l.push_back(s.fileId); l.push_back(s.fileId);
return l; return l;
})); }));
QJsonObject mods_object;
mods_object["modIds"] = QJsonArray::fromVariantList(
std::accumulate(m_toProcess.files.begin(), m_toProcess.files.end(), QVariantList(), [](QVariantList& l, const File& s) {
l.push_back(s.projectId);
return l;
}));
QByteArray data = Json::toText(object); QByteArray data = Json::toText(object);
QByteArray mods_data = Json::toText(mods_object);
auto dl = Net::Upload::makeByteArray(QUrl("https://api.curseforge.com/v1/mods/files"), result, data); auto dl = Net::Upload::makeByteArray(QUrl("https://api.curseforge.com/v1/mods/files"), result, data);
m_dljob->addNetAction(dl); m_dljob->addNetAction(dl);
auto dl2 = Net::Upload::makeByteArray(QUrl("https://api.curseforge.com/v1/mods"), mods_result, mods_data);
m_dljob->addNetAction(dl2);
auto step_progress = std::make_shared<TaskStepProgress>(); auto step_progress = std::make_shared<TaskStepProgress>();
connect(m_dljob.get(), &NetJob::finished, this, [this, step_progress]() { connect(m_dljob.get(), &NetJob::finished, this, [this, step_progress]() {
@ -66,6 +79,30 @@ void Flame::FileResolvingTask::executeTask()
m_dljob->start(); m_dljob->start();
} }
PackedResourceType getResourceType(int classId)
{
switch (classId) {
case 17: // Worlds
return PackedResourceType::WorldSave;
case 6: // Mods
return PackedResourceType::Mod;
case 12: // Resource Packs
// return PackedResourceType::ResourcePack; // not really a resourcepack
/* fallthrough */
case 4546: // Customization
// return PackedResourceType::ShaderPack; // not really a shaderPack
/* fallthrough */
case 4471: // Modpacks
/* fallthrough */
case 5: // Bukkit Plugins
/* fallthrough */
case 4559: // Addons
/* fallthrough */
default:
return PackedResourceType::UNKNOWN;
}
}
void Flame::FileResolvingTask::netJobFinished() void Flame::FileResolvingTask::netJobFinished()
{ {
setProgress(1, 3); setProgress(1, 3);
@ -75,10 +112,21 @@ void Flame::FileResolvingTask::netJobFinished()
QJsonDocument doc; QJsonDocument doc;
QJsonArray array; QJsonArray array;
QHash<int, PackedResourceType> resourceTypes;
try { try {
doc = Json::requireDocument(*result); doc = Json::requireDocument(*result);
array = Json::requireArray(doc.object()["data"]); array = Json::requireArray(doc.object()["data"]);
doc = Json::requireDocument(*mods_result);
auto obj = Json::requireObject(doc);
auto mods_array = Json::requireArray(obj, "data", "data");
for (auto element : mods_array) {
auto mod = Json::requireObject(element);
auto id = Json::requireInteger(mod, "id", "modId");
auto classId = Json::requireInteger(mod, "classId", "modClassId");
resourceTypes.insert(id, getResourceType(classId));
}
} catch (Json::JsonException& e) { } catch (Json::JsonException& e) {
qCritical() << "Non-JSON data returned from the CF API"; qCritical() << "Non-JSON data returned from the CF API";
qCritical() << e.cause(); qCritical() << e.cause();
@ -106,6 +154,10 @@ void Flame::FileResolvingTask::netJobFinished()
blockedProjects.insert(&out, output); blockedProjects.insert(&out, output);
} }
} }
out.resourceType = resourceTypes.value(out.projectId, PackedResourceType::UNKNOWN);
if (out.resourceType == PackedResourceType::WorldSave) {
out.targetFolder = "saves";
}
} }
auto step_progress = std::make_shared<TaskStepProgress>(); auto step_progress = std::make_shared<TaskStepProgress>();
connect(m_checkJob.get(), &NetJob::finished, this, [this, step_progress]() { connect(m_checkJob.get(), &NetJob::finished, this, [this, step_progress]() {

View File

@ -26,6 +26,7 @@ class FileResolvingTask : public Task {
shared_qobject_ptr<QNetworkAccessManager> m_network; shared_qobject_ptr<QNetworkAccessManager> m_network;
Flame::Manifest m_toProcess; Flame::Manifest m_toProcess;
std::shared_ptr<QByteArray> result; std::shared_ptr<QByteArray> result;
std::shared_ptr<QByteArray> mods_result;
NetJob::Ptr m_dljob; NetJob::Ptr m_dljob;
NetJob::Ptr m_checkJob; NetJob::Ptr m_checkJob;
NetJob::Ptr m_slugJob; NetJob::Ptr m_slugJob;

View File

@ -60,7 +60,6 @@
#include "minecraft/World.h" #include "minecraft/World.h"
#include "minecraft/mod/tasks/LocalResourceParse.h" #include "minecraft/mod/tasks/LocalResourceParse.h"
const static QMap<QString, QString> forgemap = { { "1.2.5", "3.4.9.171" }, const static QMap<QString, QString> forgemap = { { "1.2.5", "3.4.9.171" },
{ "1.4.2", "6.0.1.355" }, { "1.4.2", "6.0.1.355" },
{ "1.4.7", "6.6.2.534" }, { "1.4.7", "6.6.2.534" },
@ -413,7 +412,7 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
QList<BlockedMod> blocked_mods; QList<BlockedMod> blocked_mods;
auto anyBlocked = false; auto anyBlocked = false;
for (const auto& result : results.files.values()) { for (const auto& result : results.files.values()) {
if (result.fileName.endsWith(".zip")) { if (result.resourceType != PackedResourceType::Mod) {
m_ZIP_resources.append(std::make_pair(result.fileName, result.targetFolder)); m_ZIP_resources.append(std::make_pair(result.fileName, result.targetFolder));
} }
@ -502,7 +501,7 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
m_files_job.reset(); m_files_job.reset();
setError(reason); setError(reason);
}); });
connect(m_files_job.get(), &NetJob::progress, this, [this](qint64 current, qint64 total){ connect(m_files_job.get(), &NetJob::progress, this, [this](qint64 current, qint64 total) {
setDetails(tr("%1 out of %2 complete").arg(current).arg(total)); setDetails(tr("%1 out of %2 complete").arg(current).arg(total));
setProgress(current, total); setProgress(current, total);
}); });
@ -545,7 +544,6 @@ void FlameCreationTask::copyBlockedMods(QList<BlockedMod> const& blocked_mods)
setAbortable(true); setAbortable(true);
} }
void FlameCreationTask::validateZIPResouces() void FlameCreationTask::validateZIPResouces()
{ {
qDebug() << "Validating whether resources stored as .zip are in the right place"; qDebug() << "Validating whether resources stored as .zip are in the right place";
@ -569,7 +567,7 @@ void FlameCreationTask::validateZIPResouces()
return localPath; return localPath;
}; };
auto installWorld = [this](QString worldPath){ auto installWorld = [this](QString worldPath) {
qDebug() << "Installing World from" << worldPath; qDebug() << "Installing World from" << worldPath;
QFileInfo worldFileInfo(worldPath); QFileInfo worldFileInfo(worldPath);
World w(worldFileInfo); World w(worldFileInfo);
@ -586,29 +584,30 @@ void FlameCreationTask::validateZIPResouces()
QString worldPath; QString worldPath;
switch (type) { switch (type) {
case PackedResourceType::Mod : case PackedResourceType::Mod:
validatePath(fileName, targetFolder, "mods"); validatePath(fileName, targetFolder, "mods");
break; break;
case PackedResourceType::ResourcePack : case PackedResourceType::ResourcePack:
validatePath(fileName, targetFolder, "resourcepacks"); validatePath(fileName, targetFolder, "resourcepacks");
break; break;
case PackedResourceType::TexturePack : case PackedResourceType::TexturePack:
validatePath(fileName, targetFolder, "texturepacks"); validatePath(fileName, targetFolder, "texturepacks");
break; break;
case PackedResourceType::DataPack : case PackedResourceType::DataPack:
validatePath(fileName, targetFolder, "datapacks"); validatePath(fileName, targetFolder, "datapacks");
break; break;
case PackedResourceType::ShaderPack : case PackedResourceType::ShaderPack:
// in theroy flame API can't do this but who knows, that *may* change ? // 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 // better to handle it if it *does* occure in the future
validatePath(fileName, targetFolder, "shaderpacks"); validatePath(fileName, targetFolder, "shaderpacks");
break; break;
case PackedResourceType::WorldSave : case PackedResourceType::WorldSave:
worldPath = validatePath(fileName, targetFolder, "saves"); worldPath = validatePath(fileName, targetFolder, "saves");
installWorld(worldPath); installWorld(worldPath);
break; break;
case PackedResourceType::UNKNOWN : case PackedResourceType::UNKNOWN:
default : /* fallthrough */
default:
qDebug() << "Can't Identify" << fileName << "at" << localPath << ", leaving it where it is."; qDebug() << "Can't Identify" << fileName << "at" << localPath << ", leaving it where it is.";
break; break;
} }

View File

@ -40,6 +40,7 @@
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
#include <QVector> #include <QVector>
#include "minecraft/mod/tasks/LocalResourceParse.h"
namespace Flame namespace Flame
{ {
@ -71,6 +72,7 @@ struct File
Modpack, Modpack,
Mod Mod
} type = Type::Mod; } type = Type::Mod;
PackedResourceType resourceType;
}; };
struct Modloader struct Modloader