refactor(InstanceCreationTask): return created instance to caller

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2026-02-02 16:03:34 +05:00
parent 9e86c44f7c
commit aa9896031d
No known key found for this signature in database
GPG key ID: B77C34313AEE1FFF
7 changed files with 56 additions and 52 deletions

View file

@ -2,6 +2,7 @@
#include "BaseVersion.h"
#include "InstanceTask.h"
#include "minecraft/MinecraftInstance.h"
class InstanceCreationTask : public InstanceTask {
Q_OBJECT
@ -27,9 +28,9 @@ class InstanceCreationTask : public InstanceTask {
/**
* Creates a new instance.
*
* Returns whether the instance creation was successful (true) or not (false).
* Returns the instance if it was created or nullptr otherwise.
*/
virtual bool createInstance() { return false; };
virtual std::unique_ptr<MinecraftInstance> createInstance() { return nullptr; }
QString getError() const { return m_error_message; }

View file

@ -15,22 +15,22 @@ VanillaCreationTask::VanillaCreationTask(BaseVersion::Ptr version, QString loade
, m_loader_version(std::move(loader_version))
{}
bool VanillaCreationTask::createInstance()
std::unique_ptr<MinecraftInstance> VanillaCreationTask::createInstance()
{
setStatus(tr("Creating instance from version %1").arg(m_version->name()));
MinecraftInstance inst(m_globalSettings, std::make_unique<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg")),
auto inst = std::make_unique<MinecraftInstance>(m_globalSettings, std::make_unique<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg")),
m_stagingPath);
SettingsObject::Lock lock(inst.settings());
SettingsObject::Lock lock(inst->settings());
auto components = inst.getPackProfile();
auto components = inst->getPackProfile();
components->buildingFromScratch();
components->setComponentVersion("net.minecraft", m_version->descriptor(), true);
if (m_using_loader)
components->setComponentVersion(m_loader, m_loader_version->descriptor());
inst.setName(name());
inst.setIconKey(m_instIcon);
inst->setName(name());
inst->setIconKey(m_instIcon);
return true;
return inst;
}

View file

@ -10,7 +10,7 @@ class VanillaCreationTask final : public InstanceCreationTask {
VanillaCreationTask(BaseVersion::Ptr version) : InstanceCreationTask(), m_version(std::move(version)) {}
VanillaCreationTask(BaseVersion::Ptr version, QString loader, BaseVersion::Ptr loader_version);
bool createInstance() override;
std::unique_ptr<MinecraftInstance> createInstance() override;
private:
// Version to update to / create of the instance.

View file

@ -316,7 +316,7 @@ QString FlameCreationTask::getVersionForLoader(QString uid, QString loaderType,
return loaderVersion;
}
bool FlameCreationTask::createInstance()
std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
{
QEventLoop loop;
@ -334,7 +334,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 +346,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 +387,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 +397,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 +433,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 +448,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 +456,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 +485,10 @@ bool FlameCreationTask::createInstance()
setAbortable(false);
auto inst = m_instance.value();
inst->copyManagedPack(instance);
inst->copyManagedPack(*instance);
}
return did_succeed;
return instance;
}
void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
@ -545,7 +545,7 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
BlockedModsDialog message_dialog(m_parent, tr("Blocked mods found"),
tr("The following files are not available for download in third party launchers.<br/>"
"You will need to manually download them and add them to the instance."),
"You will need to manually download them and add them to the instance->"),
blocked_mods);
message_dialog.setModal(true);

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

@ -169,7 +169,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 +177,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 +194,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 +207,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 +229,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 +257,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 +268,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 +312,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 +343,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);