Download game files during instance creation (#4944)

This commit is contained in:
Alexandru Ionut Tripon 2026-02-17 20:23:06 +00:00 committed by GitHub
commit 71abf3beec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 136 additions and 74 deletions

View file

@ -75,7 +75,6 @@ bool FlameCreationTask::abort()
if (!canAbort())
return false;
m_abort = true;
if (m_processUpdateFileInfoJob)
m_processUpdateFileInfoJob->abort();
if (m_filesJob)
@ -83,7 +82,7 @@ bool FlameCreationTask::abort()
if (m_modIdResolver)
m_modIdResolver->abort();
return Task::abort();
return InstanceCreationTask::abort();
}
bool FlameCreationTask::updateInstance()
@ -316,7 +315,7 @@ QString FlameCreationTask::getVersionForLoader(QString uid, QString loaderType,
return loaderVersion;
}
bool FlameCreationTask::createInstance()
std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
{
QEventLoop loop;
@ -334,7 +333,7 @@ bool FlameCreationTask::createInstance()
} catch (const JSONValidationError& e) {
setError(tr("Could not understand pack manifest:\n") + e.cause());
return false;
return nullptr;
}
if (!m_pack.overrides.isEmpty()) {
@ -346,7 +345,7 @@ bool FlameCreationTask::createInstance()
QString mcPath = FS::PathCombine(m_stagingPath, "minecraft");
if (!FS::move(overridePath, mcPath)) {
setError(tr("Could not rename the overrides folder:\n") + m_pack.overrides);
return false;
return nullptr;
}
} else {
logWarning(
@ -387,7 +386,7 @@ bool FlameCreationTask::createInstance()
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
auto instanceSettings = std::make_unique<INISettingsObject>(configPath);
MinecraftInstance instance(m_globalSettings, std::move(instanceSettings), m_stagingPath);
auto instance = std::make_unique<MinecraftInstance>(m_globalSettings, std::move(instanceSettings), m_stagingPath);
auto mcVersion = m_pack.minecraft.version;
// Hack to correct some 'special sauce'...
@ -397,25 +396,25 @@ bool FlameCreationTask::createInstance()
logWarning(tr("Mysterious trailing dots removed from Minecraft version while importing pack."));
}
auto components = instance.getPackProfile();
auto components = instance->getPackProfile();
components->buildingFromScratch();
components->setComponentVersion("net.minecraft", mcVersion, true);
if (!loaderType.isEmpty()) {
auto version = getVersionForLoader(loaderUid, loaderType, loaderVersion, mcVersion);
if (version.isEmpty())
return false;
return nullptr;
components->setComponentVersion(loaderUid, version);
}
if (m_instIcon != "default") {
instance.setIconKey(m_instIcon);
instance->setIconKey(m_instIcon);
} else {
if (m_pack.name.contains("Direwolf20")) {
instance.setIconKey("steve");
instance->setIconKey("steve");
} else if (m_pack.name.contains("FTB") || m_pack.name.contains("Feed The Beast")) {
instance.setIconKey("ftb_logo");
instance->setIconKey("ftb_logo");
} else {
instance.setIconKey("flame");
instance->setIconKey("flame");
}
}
@ -433,8 +432,8 @@ bool FlameCreationTask::createInstance()
recommendedRAM = max;
}
instance.settings()->set("OverrideMemory", true);
instance.settings()->set("MaxMemAlloc", recommendedRAM);
instance->settings()->set("OverrideMemory", true);
instance->settings()->set("MaxMemAlloc", recommendedRAM);
}
QString jarmodsPath = FS::PathCombine(m_stagingPath, "minecraft", "jarmods");
@ -448,7 +447,7 @@ bool FlameCreationTask::createInstance()
qDebug() << info.fileName();
jarMods.push_back(info.absoluteFilePath());
}
auto profile = instance.getPackProfile();
auto profile = instance->getPackProfile();
profile->installJarMods(jarMods);
// nuke the original files
FS::deletePath(jarmodsPath);
@ -456,11 +455,11 @@ bool FlameCreationTask::createInstance()
// Don't add managed info to packs without an ID (most likely imported from ZIP)
if (!m_managedId.isEmpty())
instance.setManagedPack("flame", m_managedId, m_pack.name, m_managedVersionId, m_pack.version);
instance->setManagedPack("flame", m_managedId, m_pack.name, m_managedVersionId, m_pack.version);
else
instance.setManagedPack("flame", "", name(), "", "");
instance->setManagedPack("flame", "", name(), "", "");
instance.setName(name());
instance->setName(name());
m_modIdResolver.reset(new Flame::FileResolvingTask(m_pack));
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::succeeded, this, [this, &loop] { idResolverSucceeded(loop); });
@ -485,10 +484,13 @@ bool FlameCreationTask::createInstance()
setAbortable(false);
auto inst = m_instance.value();
inst->copyManagedPack(instance);
inst->copyManagedPack(*instance);
}
return did_succeed;
if (did_succeed) {
return instance;
}
return nullptr;
}
void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)

View file

@ -68,7 +68,7 @@ class FlameCreationTask final : public InstanceCreationTask {
bool abort() override;
bool updateInstance() override;
bool createInstance() override;
std::unique_ptr<MinecraftInstance> createInstance() override;
private slots:
void idResolverSucceeded(QEventLoop&);

View file

@ -32,10 +32,9 @@ bool ModrinthCreationTask::abort()
if (!canAbort())
return false;
m_abort = true;
if (m_task)
m_task->abort();
return Task::abort();
return InstanceCreationTask::abort();
}
bool ModrinthCreationTask::updateInstance()
@ -169,7 +168,7 @@ bool ModrinthCreationTask::updateInstance()
}
// https://docs.modrinth.com/docs/modpacks/format_definition/
bool ModrinthCreationTask::createInstance()
std::unique_ptr<MinecraftInstance> ModrinthCreationTask::createInstance()
{
QEventLoop loop;
@ -177,7 +176,7 @@ bool ModrinthCreationTask::createInstance()
QString index_path = FS::PathCombine(m_stagingPath, "modrinth.index.json");
if (m_files.empty() && !parseManifest(index_path, m_files, true, true))
return false;
return nullptr;
// Keep index file in case we need it some other time (like when changing versions)
QString new_index_place(FS::PathCombine(parent_folder, "modrinth.index.json"));
@ -194,7 +193,7 @@ bool ModrinthCreationTask::createInstance()
// Apply the overrides
if (!FS::move(override_path, mcPath)) {
setError(tr("Could not rename the overrides folder:\n") + "overrides");
return false;
return nullptr;
}
}
@ -207,15 +206,15 @@ bool ModrinthCreationTask::createInstance()
// Apply the overrides
if (!FS::overrideFolder(mcPath, client_override_path)) {
setError(tr("Could not rename the client overrides folder:\n") + "client overrides");
return false;
return nullptr;
}
}
QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg");
auto instanceSettings = std::make_unique<INISettingsObject>(configPath);
MinecraftInstance instance(m_globalSettings, std::move(instanceSettings), m_stagingPath);
auto instance = std::make_unique<MinecraftInstance>(m_globalSettings, std::move(instanceSettings), m_stagingPath);
auto components = instance.getPackProfile();
auto components = instance->getPackProfile();
components->buildingFromScratch();
components->setComponentVersion("net.minecraft", m_minecraft_version, true);
@ -229,19 +228,19 @@ bool ModrinthCreationTask::createInstance()
components->setComponentVersion("net.neoforged", m_neoForge_version);
if (m_instIcon != "default") {
instance.setIconKey(m_instIcon);
instance->setIconKey(m_instIcon);
} else if (!m_managed_id.isEmpty()) {
instance.setIconKey("modrinth");
instance->setIconKey("modrinth");
}
// Don't add managed info to packs without an ID (most likely imported from ZIP)
if (!m_managed_id.isEmpty())
instance.setManagedPack("modrinth", m_managed_id, m_managed_name, m_managed_version_id, version());
instance->setManagedPack("modrinth", m_managed_id, m_managed_name, m_managed_version_id, version());
else
instance.setManagedPack("modrinth", "", name(), "", "");
instance->setManagedPack("modrinth", "", name(), "", "");
instance.setName(name());
instance.saveNow();
instance->setName(name());
instance->saveNow();
auto downloadMods = makeShared<NetJob>(tr("Mod Download Modrinth"), APPLICATION->network());
@ -257,7 +256,7 @@ bool ModrinthCreationTask::createInstance()
// This means we somehow got out of the root folder, so abort here to prevent exploits
setError(tr("One of the files has a path that leads to an arbitrary location (%1). This is a security risk and isn't allowed.")
.arg(fileName));
return false;
return nullptr;
}
if (fileName.startsWith("mods/")) {
auto mod = new Mod(file_path);
@ -268,7 +267,7 @@ bool ModrinthCreationTask::createInstance()
}
if (file.downloads.empty()) {
setError(tr("The file '%1' is missing a download link. This is invalid in the pack format.").arg(fileName));
return false;
return nullptr;
}
qDebug() << "Will try to download" << file.downloads.front() << "to" << file_path;
auto dl = Net::ApiDownload::makeFile(file.downloads.dequeue(), file_path);
@ -312,11 +311,11 @@ bool ModrinthCreationTask::createInstance()
for (auto resource : resources) {
delete resource;
}
return ended_well;
return nullptr;
}
QEventLoop ensureMetaLoop;
QDir folder = FS::PathCombine(instance.modsRoot(), ".index");
QDir folder = FS::PathCombine(instance->modsRoot(), ".index");
auto ensureMetadataTask = makeShared<EnsureMetadataTask>(resources, folder, ModPlatform::ResourceProvider::MODRINTH);
connect(ensureMetadataTask.get(), &Task::succeeded, this, [&ended_well]() { ended_well = true; });
connect(ensureMetadataTask.get(), &Task::finished, &ensureMetaLoop, &QEventLoop::quit);
@ -343,15 +342,18 @@ bool ModrinthCreationTask::createInstance()
// Only change the name if it didn't use a custom name, so that the previous custom name
// is preserved, but if we're using the original one, we update the version string.
// NOTE: This needs to come before the copyManagedPack call!
if (inst->name().contains(inst->getManagedPackVersionName()) && inst->name() != instance.name()) {
if (askForChangingInstanceName(m_parent, inst->name(), instance.name()) == InstanceNameChange::ShouldChange)
inst->setName(instance.name());
if (inst->name().contains(inst->getManagedPackVersionName()) && inst->name() != instance->name()) {
if (askForChangingInstanceName(m_parent, inst->name(), instance->name()) == InstanceNameChange::ShouldChange)
inst->setName(instance->name());
}
inst->copyManagedPack(instance);
inst->copyManagedPack(*instance);
}
return ended_well;
if (ended_well) {
return instance;
}
return nullptr;
}
bool ModrinthCreationTask::parseManifest(const QString& index_path,

View file

@ -41,7 +41,7 @@ class ModrinthCreationTask final : public InstanceCreationTask {
bool abort() override;
bool updateInstance() override;
bool createInstance() override;
std::unique_ptr<MinecraftInstance> createInstance() override;
private:
bool parseManifest(const QString&, std::vector<File>&, bool set_internal_data = true, bool show_optional_dialog = true);