Added smart file selection

This might fail in a few special cases
This commit is contained in:
timoreo 2022-01-15 10:25:24 +01:00
parent f6de472da2
commit 621e0ba4a8
No known key found for this signature in database
GPG Key ID: 121A72C3512BA288
4 changed files with 43 additions and 10 deletions

View File

@ -8,7 +8,7 @@ ModDownloadTask::ModDownloadTask(const QUrl sourceUrl,const QString filename, co
void ModDownloadTask::executeTask() {
setStatus(tr("Downloading mod:\n%1").arg(m_sourceUrl.toString()));
m_filesNetJob.reset(new NetJob(tr("Modpack download"), APPLICATION->network()));
m_filesNetJob.reset(new NetJob(tr("Mod download"), APPLICATION->network()));
m_filesNetJob->addNetAction(Net::Download::makeFile(m_sourceUrl, mods->dir().absoluteFilePath(filename)));
connect(m_filesNetJob.get(), &NetJob::succeeded, this, &ModDownloadTask::downloadSucceeded);
connect(m_filesNetJob.get(), &NetJob::progress, this, &ModDownloadTask::downloadProgressChanged);

View File

@ -3,6 +3,10 @@
#include "Json.h"
#include "net/NetJob.h"
#include "BaseInstance.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
void Modrinth::loadIndexedPack(Modrinth::IndexedPack & pack, QJsonObject & obj)
{
@ -20,9 +24,12 @@ void Modrinth::loadIndexedPack(Modrinth::IndexedPack & pack, QJsonObject & obj)
pack.authors.append(packAuthor); //TODO delete this ? only one author ever exists
}
void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray & arr, const shared_qobject_ptr<QNetworkAccessManager>& network)
void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray & arr, const shared_qobject_ptr<QNetworkAccessManager>& network, BaseInstance * inst)
{
QVector<Modrinth::IndexedVersion> unsortedVersions;
bool hasFabric = !((MinecraftInstance *)inst)->getPackProfile()->getComponentVersion("net.fabricmc.fabric-loader").isEmpty();
QString mcVersion = ((MinecraftInstance *)inst)->getPackProfile()->getComponentVersion("net.minecraft");
for(auto versionIter: arr) {
auto obj = versionIter.toObject();
Modrinth::IndexedVersion file;
@ -33,7 +40,6 @@ void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray
if (versionArray.empty()) {
continue;
}
// pick the latest version supported
for(auto mcVer : versionArray){
file.mcVersion.append(mcVer.toString());
}
@ -42,8 +48,35 @@ void Modrinth::loadIndexedPackVersions(Modrinth::IndexedPack & pack, QJsonArray
file.loaders.append(loader.toString());
}
file.version = Json::requireString(obj, "name");
//TODO show all the files ?
auto parent = Json::requireArray(obj, "files")[0].toObject();
auto files = Json::requireArray(obj, "files");
int i = 0;
while (files.count() > 1 && i < files.count()){
//try to resolve the correct file
auto parent = files[i].toObject();
auto fileName = Json::requireString(parent, "filename");
//avoid grabbing "dev" files
if(fileName.contains("javadocs",Qt::CaseInsensitive) || fileName.contains("sources",Qt::CaseInsensitive)){
i++;
continue;
}
//grab the correct mod loader
if(fileName.contains("forge",Qt::CaseInsensitive) || fileName.contains("fabric",Qt::CaseInsensitive) ){
if(hasFabric){
if(fileName.contains("forge",Qt::CaseInsensitive)){
i++;
continue;
}
}else{
if(fileName.contains("fabric",Qt::CaseInsensitive)){
i++;
continue;
}
}
}
break;
}
auto parent = files[i].toObject();
file.downloadUrl = Json::requireString(parent, "url");
file.fileName = Json::requireString(parent, "filename");

View File

@ -7,6 +7,7 @@
#include <QNetworkAccessManager>
#include <QObjectPtr.h>
#include "net/NetJob.h"
#include "BaseInstance.h"
namespace Modrinth {
@ -41,8 +42,7 @@ struct IndexedPack
};
void loadIndexedPack(IndexedPack & m, QJsonObject & obj);
void loadIndexedPackVersions(IndexedPack & m, QJsonArray & arr, const shared_qobject_ptr<QNetworkAccessManager>& network);
void versionJobFinished();
void loadIndexedPackVersions(IndexedPack &pack, QJsonArray &arr, const shared_qobject_ptr<QNetworkAccessManager> &network, BaseInstance *inst);
}
Q_DECLARE_METATYPE(Modrinth::IndexedPack)

View File

@ -128,7 +128,7 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
QJsonArray arr = doc.array();
try
{
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network());
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
}
catch(const JSONValidationError &e)
{
@ -145,7 +145,7 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
}
if(ui->versionSelectionBox->count() == 0){
ui->versionSelectionBox->addItem("No Valid Version found !", QVariant(""));
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(""));
}
suggestCurrent();
@ -158,7 +158,7 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
}
if(ui->versionSelectionBox->count() == 0){
ui->versionSelectionBox->addItem("No Valid Version found !", QVariant(""));
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(""));
}
suggestCurrent();
}