aboutsummaryrefslogtreecommitdiff
path: root/launcher
diff options
context:
space:
mode:
authorflow <flowlnlnln@gmail.com>2022-07-07 20:31:24 -0300
committerflow <flowlnlnln@gmail.com>2022-09-20 18:32:36 -0300
commit941d75824af8d8e3deb1dfc0597c9493911f0cf0 (patch)
tree12578af364c0c44bf7a6addf9d855d75d22c6449 /launcher
parentec9ddc4f225c89ea3ccbf24c9a3be36848aa3172 (diff)
downloadPrismLauncher-941d75824af8d8e3deb1dfc0597c9493911f0cf0.tar.gz
PrismLauncher-941d75824af8d8e3deb1dfc0597c9493911f0cf0.tar.bz2
PrismLauncher-941d75824af8d8e3deb1dfc0597c9493911f0cf0.zip
refactor: add instance creation abstraction and move vanilla
This is so that 1. Code is more cleanly separated, and 2. Allows to more easily add instance updating :) Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher')
-rw-r--r--launcher/CMakeLists.txt2
-rw-r--r--launcher/InstanceCreationTask.cpp42
-rw-r--r--launcher/InstanceCreationTask.h47
-rw-r--r--launcher/minecraft/VanillaInstanceCreationTask.cpp32
-rw-r--r--launcher/minecraft/VanillaInstanceCreationTask.h20
-rw-r--r--launcher/ui/pages/modplatform/VanillaPage.cpp10
6 files changed, 99 insertions, 54 deletions
diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt
index 848d2e51..c51cd6bd 100644
--- a/launcher/CMakeLists.txt
+++ b/launcher/CMakeLists.txt
@@ -297,6 +297,8 @@ set(MINECRAFT_SOURCES
minecraft/Library.cpp
minecraft/Library.h
minecraft/MojangDownloadInfo.h
+ minecraft/VanillaInstanceCreationTask.cpp
+ minecraft/VanillaInstanceCreationTask.h
minecraft/VersionFile.cpp
minecraft/VersionFile.h
minecraft/VersionFilterData.h
diff --git a/launcher/InstanceCreationTask.cpp b/launcher/InstanceCreationTask.cpp
index e01bf306..c8c91997 100644
--- a/launcher/InstanceCreationTask.cpp
+++ b/launcher/InstanceCreationTask.cpp
@@ -1,40 +1,18 @@
#include "InstanceCreationTask.h"
-#include "settings/INISettingsObject.h"
-#include "FileSystem.h"
-//FIXME: remove this
-#include "minecraft/MinecraftInstance.h"
-#include "minecraft/PackProfile.h"
+#include <QDebug>
-InstanceCreationTask::InstanceCreationTask(BaseVersionPtr version)
-{
- m_version = version;
- m_usingLoader = false;
-}
-
-InstanceCreationTask::InstanceCreationTask(BaseVersionPtr version, QString loader, BaseVersionPtr loaderVersion)
-{
- m_version = version;
- m_usingLoader = true;
- m_loader = loader;
- m_loaderVersion = loaderVersion;
-}
+InstanceCreationTask::InstanceCreationTask() {}
void InstanceCreationTask::executeTask()
{
- setStatus(tr("Creating instance from version %1").arg(m_version->name()));
- {
- auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg"));
- instanceSettings->suspendSave();
- MinecraftInstance inst(m_globalSettings, instanceSettings, m_stagingPath);
- auto components = inst.getPackProfile();
- components->buildingFromScratch();
- components->setComponentVersion("net.minecraft", m_version->descriptor(), true);
- if(m_usingLoader)
- components->setComponentVersion(m_loader, m_loaderVersion->descriptor());
- inst.setName(m_instName);
- inst.setIconKey(m_instIcon);
- instanceSettings->resumeSave();
+ if (updateInstance() || createInstance()) {
+ emitSucceeded();
+ return;
}
- emitSucceeded();
+
+ qWarning() << "Instance creation failed!";
+ if (!m_error_message.isEmpty())
+ qWarning() << "Reason: " << m_error_message;
+ emitFailed(tr("Error while creating new instance."));
}
diff --git a/launcher/InstanceCreationTask.h b/launcher/InstanceCreationTask.h
index 23367c3f..af854713 100644
--- a/launcher/InstanceCreationTask.h
+++ b/launcher/InstanceCreationTask.h
@@ -1,26 +1,39 @@
#pragma once
-#include "tasks/Task.h"
-#include "net/NetJob.h"
-#include <QUrl>
-#include "settings/SettingsObject.h"
#include "BaseVersion.h"
#include "InstanceTask.h"
-class InstanceCreationTask : public InstanceTask
-{
+class InstanceCreationTask : public InstanceTask {
Q_OBJECT
-public:
- explicit InstanceCreationTask(BaseVersionPtr version);
- explicit InstanceCreationTask(BaseVersionPtr version, QString loader, BaseVersionPtr loaderVersion);
+ public:
+ InstanceCreationTask();
+ virtual ~InstanceCreationTask() = default;
-protected:
- //! Entry point for tasks.
- virtual void executeTask() override;
+ protected:
+ void executeTask() final override;
-private: /* data */
- BaseVersionPtr m_version;
- bool m_usingLoader;
- QString m_loader;
- BaseVersionPtr m_loaderVersion;
+ /**
+ * Tries to update an already existing instance.
+ *
+ * This can be implemented by subclasses to provide a way of updating an already existing
+ * instance, according to that implementation's concept of 'identity' (i.e. instances that
+ * are updates / downgrades of one another).
+ *
+ * If this returns true, createInstance() will not run, so you should do all update steps in here.
+ * Otherwise, createInstance() is run as normal.
+ */
+ virtual bool updateInstance() { return false; };
+
+ /**
+ * Creates a new instance.
+ *
+ * Returns whether the instance creation was successful (true) or not (false).
+ */
+ virtual bool createInstance() { return false; };
+
+ protected:
+ void setError(QString message) { m_error_message = message; };
+
+ private:
+ QString m_error_message;
};
diff --git a/launcher/minecraft/VanillaInstanceCreationTask.cpp b/launcher/minecraft/VanillaInstanceCreationTask.cpp
new file mode 100644
index 00000000..2d1593b6
--- /dev/null
+++ b/launcher/minecraft/VanillaInstanceCreationTask.cpp
@@ -0,0 +1,32 @@
+#include "VanillaInstanceCreationTask.h"
+
+#include "FileSystem.h"
+#include "settings/INISettingsObject.h"
+#include "minecraft/MinecraftInstance.h"
+#include "minecraft/PackProfile.h"
+
+VanillaCreationTask::VanillaCreationTask(BaseVersionPtr version, QString loader, BaseVersionPtr loader_version)
+ : InstanceCreationTask(), m_version(version), m_using_loader(true), m_loader(loader), m_loader_version(loader_version)
+{}
+
+bool VanillaCreationTask::createInstance()
+{
+ setStatus(tr("Creating instance from version %1").arg(m_version->name()));
+
+ auto instance_settings = std::make_shared<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg"));
+ instance_settings->suspendSave();
+ {
+ MinecraftInstance inst(m_globalSettings, instance_settings, m_stagingPath);
+ 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(m_instName);
+ inst.setIconKey(m_instIcon);
+ }
+ instance_settings->resumeSave();
+
+ return true;
+}
diff --git a/launcher/minecraft/VanillaInstanceCreationTask.h b/launcher/minecraft/VanillaInstanceCreationTask.h
new file mode 100644
index 00000000..540ecb70
--- /dev/null
+++ b/launcher/minecraft/VanillaInstanceCreationTask.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include "InstanceCreationTask.h"
+
+class VanillaCreationTask final : public InstanceCreationTask {
+ Q_OBJECT
+ public:
+ VanillaCreationTask(BaseVersionPtr version) : InstanceCreationTask(), m_version(version) {}
+ VanillaCreationTask(BaseVersionPtr version, QString loader, BaseVersionPtr loader_version);
+
+ bool createInstance() override;
+
+ private:
+ // Version to update to / create of the instance.
+ BaseVersionPtr m_version;
+
+ bool m_using_loader = false;
+ QString m_loader;
+ BaseVersionPtr m_loader_version;
+};
diff --git a/launcher/ui/pages/modplatform/VanillaPage.cpp b/launcher/ui/pages/modplatform/VanillaPage.cpp
index a026947f..99190f31 100644
--- a/launcher/ui/pages/modplatform/VanillaPage.cpp
+++ b/launcher/ui/pages/modplatform/VanillaPage.cpp
@@ -39,12 +39,12 @@
#include <QTabBar>
#include "Application.h"
+#include "Filter.h"
+#include "Version.h"
#include "meta/Index.h"
#include "meta/VersionList.h"
+#include "minecraft/VanillaInstanceCreationTask.h"
#include "ui/dialogs/NewInstanceDialog.h"
-#include "Filter.h"
-#include "InstanceCreationTask.h"
-#include "Version.h"
VanillaPage::VanillaPage(NewInstanceDialog *dialog, QWidget *parent)
: QWidget(parent), dialog(dialog), ui(new Ui::VanillaPage)
@@ -217,11 +217,11 @@ void VanillaPage::suggestCurrent()
// There isn't a selected version if the version list is empty
if(ui->loaderVersionList->selectedVersion() == nullptr)
- dialog->setSuggestedPack(m_selectedVersion->descriptor(), new InstanceCreationTask(m_selectedVersion));
+ dialog->setSuggestedPack(m_selectedVersion->descriptor(), new VanillaCreationTask(m_selectedVersion));
else
{
dialog->setSuggestedPack(m_selectedVersion->descriptor(),
- new InstanceCreationTask(m_selectedVersion, m_selectedLoader,
+ new VanillaCreationTask(m_selectedVersion, m_selectedLoader,
m_selectedLoaderVersion));
}
dialog->setSuggestedIcon("default");