Added some warnings

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-24 01:05:49 +03:00
parent 31aaa6d1da
commit cf94adb363
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
10 changed files with 158 additions and 37 deletions

View File

@ -166,6 +166,13 @@ auto Mod::homeurl() const -> QString
return details().homeurl;
}
auto Mod::metaurl() const -> QString
{
if (metadata() == nullptr)
return homeurl();
return ModPlatform::getMetaURL(metadata()->provider, metadata()->slug);
}
auto Mod::description() const -> QString
{
return details().description;

View File

@ -70,6 +70,7 @@ public:
auto provider() const -> std::optional<QString>;
auto licenses() const -> const QList<ModLicense>&;
auto issueTracker() const -> QString;
auto metaurl() const -> QString;
/** Get the intneral path to the mod's icon file*/
QString iconPath() const { return m_local_details.icon_file; };

View File

@ -70,11 +70,18 @@ auto ProviderCapabilities::hash(ResourceProvider p, QIODevice* device, QString t
}
QCryptographicHash hash(algo);
if(!hash.addData(device))
if (!hash.addData(device))
qCritical() << "Failed to read JAR to create hash!";
Q_ASSERT(hash.result().length() == hash.hashLength(algo));
return { hash.result().toHex() };
}
QString getMetaURL(ResourceProvider provider, QString slug)
{
return ((provider == ModPlatform::ResourceProvider::FLAME) ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
slug.remove(".pw.toml");
}
} // namespace ModPlatform

View File

@ -118,6 +118,7 @@ struct IndexedPack {
return std::any_of(versions.constBegin(), versions.constEnd(), [](auto const& v) { return v.is_currently_selected; });
}
};
QString getMetaURL(ResourceProvider provider, QString slug);
} // namespace ModPlatform

View File

@ -24,13 +24,14 @@
#include <QFileInfo>
#include <QMessageBox>
#include <QtConcurrentRun>
#include <algorithm>
#include <memory>
#include "Json.h"
#include "MMCZip.h"
#include "minecraft/PackProfile.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/ModIndex.h"
#include "modplatform/helpers/ExportModsToStringTask.h"
#include "modplatform/flame/FlameModIndex.h"
#include "modplatform/helpers/HashUtils.h"
#include "tasks/Task.h"
@ -40,6 +41,7 @@ FlamePackExportTask::FlamePackExportTask(const QString& name,
const QString& version,
const QString& author,
const QVariant& projectID,
const bool generateModList,
InstancePtr instance,
const QString& output,
MMCZip::FilterFunction filter)
@ -52,6 +54,7 @@ FlamePackExportTask::FlamePackExportTask(const QString& name,
, gameRoot(instance->gameRoot())
, output(output)
, filter(filter)
, generateModList(generateModList)
{}
void FlamePackExportTask::executeTask()
@ -116,7 +119,8 @@ void FlamePackExportTask::collectHashes()
}
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() });
{ mod->metadata()->project_id.toInt(), mod->metadata()->file_id.toInt(), mod->enabled(),
mod->metadata()->name, mod->metadata()->slug, mod->authors().join(", ") });
setProgress(m_progress + 1, mods.count());
continue;
}
@ -195,10 +199,10 @@ void FlamePackExportTask::makeApiRequest()
}
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() });
if (Json::ensureBoolean(file_obj, "isAvailable", false))
resolvedFiles.insert(
mod.value()->fileinfo().absoluteFilePath(),
{ Json::requireInteger(file_obj, "modId"), Json::requireInteger(file_obj, "id"), mod.value()->enabled() });
}
} catch (Json::JsonException& e) {
@ -206,10 +210,91 @@ void FlamePackExportTask::makeApiRequest()
qDebug() << doc;
}
pendingHashes.clear();
});
connect(task.get(), &Task::finished, this, &FlamePackExportTask::getProjectsInfo);
connect(task.get(), &NetJob::failed, this, &FlamePackExportTask::emitFailed);
task->start();
}
void FlamePackExportTask::getProjectsInfo()
{
if (!generateModList) {
buildZip();
return;
}
setStatus(tr("Find project info from curseforge..."));
QList<QString> addonIds;
for (auto resolved : resolvedFiles) {
if (resolved.slug.isEmpty()) {
addonIds << QString::number(resolved.addonId);
}
}
auto response = std::make_shared<QByteArray>();
Task::Ptr proj_task;
if (addonIds.isEmpty()) {
buildZip();
return;
} else if (addonIds.size() == 1) {
proj_task = api.getProject(*addonIds.begin(), response);
} else {
proj_task = api.getProjects(addonIds, response);
}
connect(proj_task.get(), &Task::succeeded, this, [this, response, addonIds] {
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Modrinth projects task at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
try {
QJsonArray entries;
if (addonIds.size() == 1)
entries = { Json::requireObject(Json::requireObject(doc), "data") };
else
entries = Json::requireArray(Json::requireObject(doc), "data");
size_t progress = 0;
for (auto entry : entries) {
setProgress(progress++, entries.count());
auto entry_obj = Json::requireObject(entry);
try {
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg(Json::requireString(entry_obj, "name")));
ModPlatform::IndexedPack pack;
FlameMod::loadIndexedPack(pack, entry_obj);
for (auto key : resolvedFiles.keys()) {
auto val = resolvedFiles.value(key);
if (val.addonId == pack.addonId) {
val.name = pack.name;
val.slug = pack.slug;
QStringList authors;
for (auto author : pack.authors)
authors << author.name;
val.authors = authors.join(", ");
resolvedFiles[key] = val;
}
}
} catch (Json::JsonException& e) {
qDebug() << e.cause();
qDebug() << entries;
}
}
} catch (Json::JsonException& e) {
qDebug() << e.cause();
qDebug() << doc;
}
buildZip();
});
connect(task.get(), &NetJob::failed, this, &FlamePackExportTask::emitFailed);
task.reset(proj_task);
task->start();
}
@ -234,14 +319,23 @@ void FlamePackExportTask::buildZip()
}
indexFile.write(generateIndex());
QuaZipFile modlist(&zip);
if (!modlist.open(QIODevice::WriteOnly, QuaZipNewInfo("modlist.html"))) {
QFile::remove(output);
return BuildZipResult(tr("Could not create index"));
if (generateModList) {
QuaZipFile modlist(&zip);
if (!modlist.open(QIODevice::WriteOnly, QuaZipNewInfo("modlist.html"))) {
QFile::remove(output);
return BuildZipResult(tr("Could not create index"));
}
QString content = "";
for (auto mod : resolvedFiles) {
content += QString(TEMPLATE)
.replace("{name}", mod.name)
.replace("{url}", ModPlatform::getMetaURL(ModPlatform::ResourceProvider::FLAME, mod.slug))
.replace("{authors}", mod.authors) +
"\n";
}
content = "<ul>" + content + "</ul>";
modlist.write(content.toUtf8());
}
QString content = ExportToString::ExportModsToStringTask(mcInstance->loaderModList()->allMods(), TEMPLATE);
content = "<ul>" + content + "</ul>";
modlist.write(content.toUtf8());
size_t progress = 0;
for (const QFileInfo& file : files) {

View File

@ -32,6 +32,7 @@ class FlamePackExportTask : public Task {
const QString& version,
const QString& author,
const QVariant& projectID,
const bool generateModList,
InstancePtr instance,
const QString& output,
MMCZip::FilterFunction filter);
@ -51,12 +52,17 @@ class FlamePackExportTask : public Task {
const QDir gameRoot;
const QString output;
const MMCZip::FilterFunction filter;
const bool generateModList;
typedef std::optional<QString> BuildZipResult;
struct ResolvedFile {
int addonId;
int version;
bool enabled;
QString name;
QString slug;
QString authors;
};
FlameAPI api;
@ -71,6 +77,7 @@ class FlamePackExportTask : public Task {
void collectFiles();
void collectHashes();
void makeApiRequest();
void getProjectsInfo();
void buildZip();
void finish();

View File

@ -19,6 +19,7 @@
#include "modplatform/ModIndex.h"
namespace ExportToString {
QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData extraData)
{
switch (format) {
@ -28,12 +29,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
auto meta = mod->metadata();
auto modName = mod->name();
if (extraData & Url) {
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
if (!url.isEmpty())
modName = QString("<a href=\"%1\">%2</a>").arg(url, modName);
}
@ -57,12 +53,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
auto meta = mod->metadata();
auto modName = mod->name();
if (extraData & Url) {
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
if (!url.isEmpty())
modName = QString("[%1](%2)").arg(modName, url);
}
@ -93,12 +84,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, QString lineTemplate)
auto meta = mod->metadata();
auto modName = mod->name();
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString();

View File

@ -1420,8 +1420,17 @@ void MainWindow::on_actionExportInstanceMrPack_triggered()
void MainWindow::on_actionExportInstanceFlamePack_triggered()
{
if (m_selectedInstance) {
ExportMrPackDialog dlg(m_selectedInstance, this, ModPlatform::ResourceProvider::FLAME);
dlg.exec();
auto instance = dynamic_cast<MinecraftInstance*>(m_selectedInstance.get());
if (instance) {
if (instance->getPackProfile()->getComponent("org.quiltmc.quilt-loader")) {
QMessageBox msgBox;
msgBox.setText(tr("Quilt is not yet supported by curseforge."));
msgBox.exec();
return;
}
ExportMrPackDialog dlg(m_selectedInstance, this, ModPlatform::ResourceProvider::FLAME);
dlg.exec();
}
}
}

View File

@ -43,6 +43,7 @@ ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent, Mo
ui->summary->setText(instance->notes().split(QRegularExpression("\\r?\\n"))[0]);
ui->author->hide();
ui->authorLabel->hide();
ui->gnerateModlist->hide();
} else {
setWindowTitle("Export CurseForge Pack");
ui->version->setText("");
@ -117,7 +118,8 @@ void ExportMrPackDialog::done(int result)
task = new ModrinthPackExportTask(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
else
task = new FlamePackExportTask(ui->name->text(), ui->version->text(), ui->author->text(), ui->summary->text(), instance, output,
task = new FlamePackExportTask(ui->name->text(), ui->version->text(), ui->author->text(), ui->summary->text(),
ui->gnerateModlist->isChecked(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
connect(task, &Task::failed,

View File

@ -67,6 +67,13 @@
<item row="4" column="1">
<widget class="QLineEdit" name="author"/>
</item>
<item row="5" column="2">
<widget class="QCheckBox" name="gnerateModlist">
<property name="text">
<string>Generate modlist</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>