Added curseforge search

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-23 19:35:41 +03:00
parent 90df092174
commit 0a56631831
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
3 changed files with 133 additions and 15 deletions

View File

@ -24,12 +24,15 @@
#include <QFileInfo> #include <QFileInfo>
#include <QMessageBox> #include <QMessageBox>
#include <QtConcurrentRun> #include <QtConcurrentRun>
#include <memory>
#include "Json.h" #include "Json.h"
#include "MMCZip.h" #include "MMCZip.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
#include "minecraft/mod/ModDetails.h"
#include "minecraft/mod/ModFolderModel.h" #include "minecraft/mod/ModFolderModel.h"
#include "modplatform/ModIndex.h" #include "modplatform/ModIndex.h"
#include "modplatform/helpers/ExportModsToStringTask.h" #include "modplatform/helpers/ExportModsToStringTask.h"
#include "modplatform/helpers/HashUtils.h"
const QString FlamePackExportTask::TEMPLATE = "<li><a href={url}>{name}({authors})</a></li>"; const QString FlamePackExportTask::TEMPLATE = "<li><a href={url}>{name}({authors})</a></li>";
@ -88,13 +91,118 @@ void FlamePackExportTask::collectFiles()
return; return;
} }
pendingHashes.clear();
resolvedFiles.clear(); resolvedFiles.clear();
if (mcInstance) {
mcInstance->loaderModList()->update(); mcInstance->loaderModList()->update();
connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this]() { connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this]() {
mods = mcInstance->loaderModList()->allMods(); mods = mcInstance->loaderModList()->allMods();
collectHashes();
});
} else
collectHashes();
}
void FlamePackExportTask::collectHashes()
{
ConcurrentTask::Ptr hashing_task(new ConcurrentTask(this, "MakeHashesTask", 10));
for (auto* mod : mods) {
if (!mod || !mod->valid() || mod->type() == ResourceType::FOLDER)
continue;
if (mod->metadata() && mod->metadata()->provider == ModPlatform::ResourceProvider::FLAME) {
resolvedFiles.insert(mod->fileinfo().absoluteFilePath(),
{ mod->metadata()->project_id.toInt(), mod->metadata()->file_id.toInt(), mod->enabled() });
continue;
}
auto hash_task = Hashing::createFlameHasher(mod->fileinfo().absoluteFilePath());
connect(hash_task.get(), &Task::succeeded, [this, hash_task, mod] { pendingHashes.insert(hash_task->getResult(), mod); });
connect(hash_task.get(), &Task::failed, this, &FlamePackExportTask::emitFailed);
connect(hash_task.get(), &Task::finished, [hash_task] { hash_task->deleteLater(); });
hashing_task->addTask(hash_task);
}
connect(hashing_task.get(), &Task::succeeded, this, &FlamePackExportTask::makeApiRequest);
connect(hashing_task.get(), &Task::failed, this, &FlamePackExportTask::emitFailed);
connect(hashing_task.get(), &Task::finished, [hashing_task] { hashing_task->deleteLater(); });
hashing_task->start();
}
void FlamePackExportTask::makeApiRequest()
{
setAbortable(true);
if (pendingHashes.isEmpty()) {
buildZip();
return;
}
auto response = std::make_shared<QByteArray>();
QList<uint> fingerprints;
for (auto& murmur : pendingHashes.keys()) {
fingerprints.push_back(murmur.toUInt());
}
auto task = api.matchFingerprints(fingerprints, response.get());
connect(task.get(), &Task::succeeded, this, [this, response] {
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth::CurrentVersions at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
failed(parse_error.errorString());
return;
}
try {
auto doc_obj = Json::requireObject(doc);
auto data_obj = Json::requireObject(doc_obj, "data");
auto data_arr = Json::requireArray(data_obj, "exactMatches");
if (data_arr.isEmpty()) {
qWarning() << "No matches found for fingerprint search!";
return;
}
for (auto match : data_arr) {
auto match_obj = Json::ensureObject(match, {});
auto file_obj = Json::ensureObject(match_obj, "file", {});
if (match_obj.isEmpty() || file_obj.isEmpty()) {
qWarning() << "Fingerprint match is empty!";
return;
}
auto fingerprint = QString::number(Json::ensureVariant(file_obj, "fileFingerprint").toUInt());
auto mod = pendingHashes.find(fingerprint);
if (mod == pendingHashes.end()) {
qWarning() << "Invalid fingerprint from the API response.";
continue;
}
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg((*mod)->name()));
resolvedFiles.insert(
mod.value()->fileinfo().absoluteFilePath(),
{ Json::requireInteger(file_obj, "modId"), Json::requireInteger(file_obj, "modId"), mod.value()->enabled() });
}
} catch (Json::JsonException& e) {
qDebug() << e.cause();
qDebug() << doc;
}
pendingHashes.clear();
buildZip(); buildZip();
}); });
connect(task.get(), &NetJob::finished, [task]() { task->deleteLater(); });
connect(task.get(), &NetJob::failed, this, &FlamePackExportTask::emitFailed);
task->start();
} }
void FlamePackExportTask::buildZip() void FlamePackExportTask::buildZip()
@ -177,6 +285,7 @@ QByteArray FlamePackExportTask::generateIndex()
obj["name"] = name; obj["name"] = name;
obj["version"] = version; obj["version"] = version;
obj["author"] = author; obj["author"] = author;
if (projectID.toInt() != 0)
obj["projectID"] = projectID.toInt(); obj["projectID"] = projectID.toInt();
obj["overrides"] = "overrides"; obj["overrides"] = "overrides";
if (mcInstance) { if (mcInstance) {
@ -205,16 +314,11 @@ QByteArray FlamePackExportTask::generateIndex()
} }
QJsonArray files; QJsonArray files;
for (auto mod : mods) { for (auto mod : resolvedFiles) {
auto meta = mod->metadata();
if (meta == nullptr || meta->provider != ModPlatform::ResourceProvider::FLAME)
continue;
resolvedFiles[gameRoot.relativeFilePath(mod->fileinfo().absoluteFilePath())] = true;
QJsonObject file; QJsonObject file;
file["projectID"] = meta->project_id.toInt(); file["projectID"] = mod.addonId;
file["fileID"] = meta->file_id.toInt(); file["fileID"] = mod.version;
file["required"] = true; file["required"] = mod.enabled;
files << file; files << file;
} }
obj["files"] = files; obj["files"] = files;

View File

@ -23,6 +23,7 @@
#include "BaseInstance.h" #include "BaseInstance.h"
#include "MMCZip.h" #include "MMCZip.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "modplatform/flame/FlameAPI.h"
#include "tasks/Task.h" #include "tasks/Task.h"
class FlamePackExportTask : public Task { class FlamePackExportTask : public Task {
@ -52,15 +53,25 @@ class FlamePackExportTask : public Task {
const MMCZip::FilterFunction filter; const MMCZip::FilterFunction filter;
typedef std::optional<QString> BuildZipResult; typedef std::optional<QString> BuildZipResult;
struct ResolvedFile {
int addonId;
int version;
bool enabled;
};
FlameAPI api;
QFileInfoList files; QFileInfoList files;
QMap<QString, bool> resolvedFiles; QMap<QString, Mod*> pendingHashes;
QMap<QString, ResolvedFile> resolvedFiles;
Task::Ptr task; Task::Ptr task;
QFuture<BuildZipResult> buildZipFuture; QFuture<BuildZipResult> buildZipFuture;
QFutureWatcher<BuildZipResult> buildZipWatcher; QFutureWatcher<BuildZipResult> buildZipWatcher;
QList<Mod*> mods; QList<Mod*> mods;
void collectFiles(); void collectFiles();
void collectHashes();
void makeApiRequest();
void buildZip(); void buildZip();
void finish(); void finish();

View File

@ -44,6 +44,8 @@ ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent, Mo
ui->author->hide(); ui->author->hide();
ui->authorLabel->hide(); ui->authorLabel->hide();
} else { } else {
setWindowTitle("Export CurseForge Pack");
ui->version->setText("");
ui->summaryLabel->setText("ProjectID"); ui->summaryLabel->setText("ProjectID");
} }
@ -137,6 +139,7 @@ void ExportMrPackDialog::done(int result)
void ExportMrPackDialog::validate() void ExportMrPackDialog::validate()
{ {
const bool invalid = ui->name->text().isEmpty() || ui->version->text().isEmpty(); const bool invalid =
ui->name->text().isEmpty() || ((m_provider == ModPlatform::ResourceProvider::MODRINTH) && ui->version->text().isEmpty());
ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(invalid); ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(invalid);
} }