Merge pull request #1402 from Trial97/flame_import

feat:made flame instace creation use metadata for recommended version
This commit is contained in:
TheKodeToad 2023-07-22 11:14:12 +01:00 committed by GitHub
commit 6ffcfcd7e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 42 deletions

View File

@ -57,15 +57,11 @@
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include "meta/Index.h"
#include "meta/VersionList.h"
#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" },
{ "1.4.2", "6.0.1.355" },
{ "1.4.7", "6.6.2.534" },
{ "1.5.2", "7.8.1.737" } };
static const FlameAPI api; static const FlameAPI api;
bool FlameCreationTask::abort() bool FlameCreationTask::abort()
@ -259,6 +255,56 @@ bool FlameCreationTask::updateInstance()
return false; return false;
} }
QString FlameCreationTask::getVersionForLoader(QString uid, QString loaderType, QString loaderVersion, QString mcVersion)
{
if (loaderVersion == "recommended") {
auto vlist = APPLICATION->metadataIndex()->get(uid);
if (!vlist) {
setError(tr("Failed to get local metadata index for %1").arg(uid));
return {};
}
if (!vlist->isLoaded()) {
QEventLoop loadVersionLoop;
auto task = vlist->getLoadTask();
connect(task.get(), &Task::finished, &loadVersionLoop, &QEventLoop::quit);
if (!task->isRunning())
task->start();
loadVersionLoop.exec();
}
for (auto version : vlist->versions()) {
// first recommended build we find, we use.
if (!version->isRecommended())
continue;
auto reqs = version->requiredSet();
// filter by minecraft version, if the loader depends on a certain version.
// not all mod loaders depend on a given Minecraft version, so we won't do this
// filtering for those loaders.
if (loaderType == "forge") {
auto iter = std::find_if(reqs.begin(), reqs.end(), [mcVersion](const Meta::Require& req) {
return req.uid == "net.minecraft" && req.equalsVersion == mcVersion;
});
if (iter == reqs.end())
continue;
}
return version->descriptor();
}
setError(tr("Failed to find version for %1 loader").arg(loaderType));
return {};
}
if (loaderVersion.isEmpty()) {
emitFailed(tr("No loader version set for modpack!"));
return {};
}
return loaderVersion;
}
bool FlameCreationTask::createInstance() bool FlameCreationTask::createInstance()
{ {
QEventLoop loop; QEventLoop loop;
@ -297,22 +343,29 @@ bool FlameCreationTask::createInstance()
} }
} }
QString forgeVersion; QString loaderType;
QString fabricVersion; QString loaderUid;
// TODO: is Quilt relevant here? QString loaderVersion;
for (auto& loader : m_pack.minecraft.modLoaders) { for (auto& loader : m_pack.minecraft.modLoaders) {
auto id = loader.id; auto id = loader.id;
if (id.startsWith("forge-")) { if (id.startsWith("forge-")) {
id.remove("forge-"); id.remove("forge-");
forgeVersion = id; loaderType = "forge";
continue; loaderUid = "net.minecraftforge";
} } else if (loaderType == "fabric") {
if (id.startsWith("fabric-")) {
id.remove("fabric-"); id.remove("fabric-");
fabricVersion = id; loaderType = "fabric";
loaderUid = "net.fabricmc.fabric-loader";
} else if (loaderType == "quilt") {
id.remove("quilt-");
loaderType = "quilt";
loaderUid = "org.quiltmc.quilt-loader";
} else {
logWarning(tr("Unknown mod loader in manifest: %1").arg(id));
continue; continue;
} }
logWarning(tr("Unknown mod loader in manifest: %1").arg(id)); loaderVersion = id;
} }
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg"); QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
@ -329,19 +382,12 @@ bool FlameCreationTask::createInstance()
auto components = instance.getPackProfile(); auto components = instance.getPackProfile();
components->buildingFromScratch(); components->buildingFromScratch();
components->setComponentVersion("net.minecraft", mcVersion, true); components->setComponentVersion("net.minecraft", mcVersion, true);
if (!forgeVersion.isEmpty()) { if (!loaderType.isEmpty()) {
// FIXME: dirty, nasty, hack. Proper solution requires dependency resolution and knowledge of the metadata. auto version = getVersionForLoader(loaderUid, loaderType, loaderVersion, mcVersion);
if (forgeVersion == "recommended") { if (version.isEmpty())
if (forgemap.contains(mcVersion)) { return false;
forgeVersion = forgemap[mcVersion]; components->setComponentVersion(loaderUid, version);
} else {
logWarning(tr("Could not map recommended Forge version for Minecraft %1").arg(mcVersion));
} }
}
components->setComponentVersion("net.minecraftforge", forgeVersion);
}
if (!fabricVersion.isEmpty())
components->setComponentVersion("net.fabricmc.fabric-loader", fabricVersion);
if (m_instIcon != "default") { if (m_instIcon != "default") {
instance.setIconKey(m_instIcon); instance.setIconKey(m_instIcon);
@ -502,7 +548,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 +591,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 +614,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 +631,29 @@ 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 : 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

@ -57,10 +57,7 @@ class FlameCreationTask final : public InstanceCreationTask {
QString id, QString id,
QString version_id, QString version_id,
QString original_instance_id = {}) QString original_instance_id = {})
: InstanceCreationTask() : InstanceCreationTask(), m_parent(parent), m_managed_id(std::move(id)), m_managed_version_id(std::move(version_id))
, m_parent(parent)
, m_managed_id(std::move(id))
, m_managed_version_id(std::move(version_id))
{ {
setStagingPath(staging_path); setStagingPath(staging_path);
setParentSettings(global_settings); setParentSettings(global_settings);
@ -78,6 +75,7 @@ class FlameCreationTask final : public InstanceCreationTask {
void setupDownloadJob(QEventLoop&); void setupDownloadJob(QEventLoop&);
void copyBlockedMods(QList<BlockedMod> const& blocked_mods); void copyBlockedMods(QList<BlockedMod> const& blocked_mods);
void validateZIPResouces(); void validateZIPResouces();
QString getVersionForLoader(QString uid, QString loaderType, QString version, QString mcVersion);
private: private:
QWidget* m_parent = nullptr; QWidget* m_parent = nullptr;