summaryrefslogtreecommitdiffstats
path: root/logic
diff options
context:
space:
mode:
Diffstat (limited to 'logic')
-rw-r--r--logic/BaseInstance.cpp268
-rw-r--r--logic/BaseInstance.h200
-rw-r--r--logic/BaseInstance_p.h29
-rw-r--r--logic/BaseVersion.h55
-rw-r--r--logic/EnabledItemFilter.cpp43
-rw-r--r--logic/EnabledItemFilter.h32
-rw-r--r--logic/ForgeInstaller.cpp155
-rw-r--r--logic/ForgeInstaller.h36
-rw-r--r--logic/InstanceFactory.cpp196
-rw-r--r--logic/InstanceFactory.h105
-rw-r--r--logic/InstanceLauncher.cpp94
-rw-r--r--logic/InstanceLauncher.h44
-rw-r--r--logic/JavaChecker.cpp124
-rw-r--r--logic/JavaChecker.h42
-rw-r--r--logic/JavaCheckerJob.cpp47
-rw-r--r--logic/JavaCheckerJob.h100
-rw-r--r--logic/JavaUtils.cpp208
-rw-r--r--logic/JavaUtils.h43
-rw-r--r--logic/LegacyFTBInstance.cpp21
-rw-r--r--logic/LegacyFTBInstance.h14
-rw-r--r--logic/LegacyForge.cpp56
-rw-r--r--logic/LegacyForge.h25
-rw-r--r--logic/LegacyInstance.cpp282
-rw-r--r--logic/LegacyInstance.h94
-rw-r--r--logic/LegacyInstance_p.h30
-rw-r--r--logic/LegacyUpdate.cpp495
-rw-r--r--logic/LegacyUpdate.h75
-rw-r--r--logic/LiteLoaderInstaller.cpp102
-rw-r--r--logic/LiteLoaderInstaller.h39
-rw-r--r--logic/MinecraftProcess.cpp374
-rw-r--r--logic/MinecraftProcess.h142
-rw-r--r--logic/MinecraftVersion.h89
-rw-r--r--logic/Mod.cpp358
-rw-r--r--logic/Mod.h129
-rw-r--r--logic/ModList.cpp599
-rw-r--r--logic/ModList.h152
-rw-r--r--logic/NagUtils.cpp38
-rw-r--r--logic/NagUtils.h23
-rw-r--r--logic/NostalgiaInstance.cpp32
-rw-r--r--logic/NostalgiaInstance.h28
-rw-r--r--logic/OneSixFTBInstance.cpp125
-rw-r--r--logic/OneSixFTBInstance.h22
-rw-r--r--logic/OneSixInstance.cpp406
-rw-r--r--logic/OneSixInstance.h78
-rw-r--r--logic/OneSixInstance_p.h30
-rw-r--r--logic/OneSixLibrary.cpp268
-rw-r--r--logic/OneSixLibrary.h132
-rw-r--r--logic/OneSixRule.cpp89
-rw-r--r--logic/OneSixRule.h98
-rw-r--r--logic/OneSixUpdate.cpp326
-rw-r--r--logic/OneSixUpdate.h59
-rw-r--r--logic/OneSixVersion.cpp340
-rw-r--r--logic/OneSixVersion.h106
-rw-r--r--logic/OpSys.cpp42
-rw-r--r--logic/OpSys.h37
-rw-r--r--logic/SkinUtils.cpp47
-rw-r--r--logic/SkinUtils.h23
-rw-r--r--logic/assets/AssetsMigrateTask.cpp143
-rw-r--r--logic/assets/AssetsMigrateTask.h18
-rw-r--r--logic/assets/AssetsUtils.cpp154
-rw-r--r--logic/assets/AssetsUtils.h39
-rw-r--r--logic/auth/AuthSession.cpp30
-rw-r--r--logic/auth/AuthSession.h49
-rw-r--r--logic/auth/MojangAccount.cpp278
-rw-r--r--logic/auth/MojangAccount.h171
-rw-r--r--logic/auth/MojangAccountList.cpp426
-rw-r--r--logic/auth/MojangAccountList.h199
-rw-r--r--logic/auth/YggdrasilTask.cpp211
-rw-r--r--logic/auth/YggdrasilTask.h137
-rw-r--r--logic/auth/flows/AuthenticateTask.cpp203
-rw-r--r--logic/auth/flows/AuthenticateTask.h46
-rw-r--r--logic/auth/flows/RefreshTask.cpp152
-rw-r--r--logic/auth/flows/RefreshTask.h44
-rw-r--r--logic/auth/flows/ValidateTask.cpp64
-rw-r--r--logic/auth/flows/ValidateTask.h47
-rw-r--r--logic/icons/IconList.cpp368
-rw-r--r--logic/icons/IconList.h78
-rw-r--r--logic/icons/MMCIcon.cpp89
-rw-r--r--logic/icons/MMCIcon.h52
-rw-r--r--logic/lists/BaseVersionList.cpp121
-rw-r--r--logic/lists/BaseVersionList.h120
-rw-r--r--logic/lists/ForgeVersionList.cpp439
-rw-r--r--logic/lists/ForgeVersionList.h128
-rw-r--r--logic/lists/InstanceList.cpp608
-rw-r--r--logic/lists/InstanceList.h139
-rw-r--r--logic/lists/JavaVersionList.cpp242
-rw-r--r--logic/lists/JavaVersionList.h96
-rw-r--r--logic/lists/LwjglVersionList.cpp199
-rw-r--r--logic/lists/LwjglVersionList.h148
-rw-r--r--logic/lists/MinecraftVersionList.cpp286
-rw-r--r--logic/lists/MinecraftVersionList.h74
-rw-r--r--logic/net/ByteArrayDownload.cpp82
-rw-r--r--logic/net/ByteArrayDownload.h44
-rw-r--r--logic/net/CacheDownload.cpp169
-rw-r--r--logic/net/CacheDownload.h58
-rw-r--r--logic/net/ForgeMirror.h10
-rw-r--r--logic/net/ForgeMirrors.cpp118
-rw-r--r--logic/net/ForgeMirrors.h58
-rw-r--r--logic/net/ForgeXzDownload.cpp389
-rw-r--r--logic/net/ForgeXzDownload.h65
-rw-r--r--logic/net/HttpMetaCache.cpp253
-rw-r--r--logic/net/HttpMetaCache.h75
-rw-r--r--logic/net/MD5EtagDownload.cpp156
-rw-r--r--logic/net/MD5EtagDownload.h51
-rw-r--r--logic/net/NetAction.h89
-rw-r--r--logic/net/NetJob.cpp112
-rw-r--r--logic/net/NetJob.h124
-rw-r--r--logic/net/PasteUpload.cpp86
-rw-r--r--logic/net/PasteUpload.h26
-rw-r--r--logic/net/URLConstants.h36
-rw-r--r--logic/news/NewsChecker.cpp135
-rw-r--r--logic/news/NewsChecker.h105
-rw-r--r--logic/news/NewsEntry.cpp77
-rw-r--r--logic/news/NewsEntry.h65
-rw-r--r--logic/status/StatusChecker.cpp137
-rw-r--r--logic/status/StatusChecker.h57
-rw-r--r--logic/tasks/ProgressProvider.h42
-rw-r--r--logic/tasks/SequentialTask.cpp77
-rw-r--r--logic/tasks/SequentialTask.h32
-rw-r--r--logic/tasks/Task.cpp84
-rw-r--r--logic/tasks/Task.h66
-rw-r--r--logic/tasks/ThreadTask.cpp41
-rw-r--r--logic/tasks/ThreadTask.h25
-rw-r--r--logic/updater/DownloadUpdateTask.cpp543
-rw-r--r--logic/updater/DownloadUpdateTask.h217
-rw-r--r--logic/updater/NotificationChecker.cpp121
-rw-r--r--logic/updater/NotificationChecker.h54
-rw-r--r--logic/updater/UpdateChecker.cpp263
-rw-r--r--logic/updater/UpdateChecker.h111
129 files changed, 17097 insertions, 0 deletions
diff --git a/logic/BaseInstance.cpp b/logic/BaseInstance.cpp
new file mode 100644
index 00000000..222004a3
--- /dev/null
+++ b/logic/BaseInstance.cpp
@@ -0,0 +1,268 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "BaseInstance.h"
+#include "BaseInstance_p.h"
+
+#include <QFileInfo>
+#include <QDir>
+#include "MultiMC.h"
+
+#include "inisettingsobject.h"
+#include "setting.h"
+#include "overridesetting.h"
+
+#include "pathutils.h"
+#include <cmdutils.h>
+#include "lists/MinecraftVersionList.h"
+#include "logic/icons/IconList.h"
+
+BaseInstance::BaseInstance(BaseInstancePrivate *d_in, const QString &rootDir,
+ SettingsObject *settings_obj, QObject *parent)
+ : QObject(parent), inst_d(d_in)
+{
+ I_D(BaseInstance);
+ d->m_settings = settings_obj;
+ d->m_rootDir = rootDir;
+
+ settings().registerSetting("name", "Unnamed Instance");
+ settings().registerSetting("iconKey", "default");
+ connect(MMC->icons().get(), SIGNAL(iconUpdated(QString)), SLOT(iconUpdated(QString)));
+ settings().registerSetting("notes", "");
+ settings().registerSetting("lastLaunchTime", 0);
+
+ /*
+ * custom base jar has no default. it is determined in code... see the accessor methods for
+ *it
+ *
+ * for instances that DO NOT have the CustomBaseJar setting (legacy instances),
+ * [.]minecraft/bin/mcbackup.jar is the default base jar
+ */
+ settings().registerSetting("UseCustomBaseJar", true);
+ settings().registerSetting("CustomBaseJar", "");
+
+ auto globalSettings = MMC->settings();
+
+ // Java Settings
+ settings().registerSetting("OverrideJava", false);
+ settings().registerOverride(globalSettings->getSetting("JavaPath"));
+ settings().registerOverride(globalSettings->getSetting("JvmArgs"));
+
+ // Custom Commands
+ settings().registerSetting({"OverrideCommands","OverrideLaunchCmd"}, false);
+ settings().registerOverride(globalSettings->getSetting("PreLaunchCommand"));
+ settings().registerOverride(globalSettings->getSetting("PostExitCommand"));
+
+ // Window Size
+ settings().registerSetting("OverrideWindow", false);
+ settings().registerOverride(globalSettings->getSetting("LaunchMaximized"));
+ settings().registerOverride(globalSettings->getSetting("MinecraftWinWidth"));
+ settings().registerOverride(globalSettings->getSetting("MinecraftWinHeight"));
+
+ // Memory
+ settings().registerSetting("OverrideMemory", false);
+ settings().registerOverride(globalSettings->getSetting("MinMemAlloc"));
+ settings().registerOverride(globalSettings->getSetting("MaxMemAlloc"));
+ settings().registerOverride(globalSettings->getSetting("PermGen"));
+
+ // Console
+ settings().registerSetting("OverrideConsole", false);
+ settings().registerOverride(globalSettings->getSetting("ShowConsole"));
+ settings().registerOverride(globalSettings->getSetting("AutoCloseConsole"));
+ settings().registerOverride(globalSettings->getSetting("LogPrePostOutput"));
+}
+
+void BaseInstance::iconUpdated(QString key)
+{
+ if(iconKey() == key)
+ {
+ emit propertiesChanged(this);
+ }
+}
+
+void BaseInstance::nuke()
+{
+ QDir(instanceRoot()).removeRecursively();
+ emit nuked(this);
+}
+
+QString BaseInstance::id() const
+{
+ return QFileInfo(instanceRoot()).fileName();
+}
+
+QString BaseInstance::instanceType() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("InstanceType").toString();
+}
+
+QString BaseInstance::instanceRoot() const
+{
+ I_D(BaseInstance);
+ return d->m_rootDir;
+}
+
+QString BaseInstance::minecraftRoot() const
+{
+ QFileInfo mcDir(PathCombine(instanceRoot(), "minecraft"));
+ QFileInfo dotMCDir(PathCombine(instanceRoot(), ".minecraft"));
+
+ if (dotMCDir.exists() && !mcDir.exists())
+ return dotMCDir.filePath();
+ else
+ return mcDir.filePath();
+}
+
+InstanceList *BaseInstance::instList() const
+{
+ if (parent()->inherits("InstanceList"))
+ return (InstanceList *)parent();
+ else
+ return NULL;
+}
+
+std::shared_ptr<BaseVersionList> BaseInstance::versionList() const
+{
+ return MMC->minecraftlist();
+}
+
+SettingsObject &BaseInstance::settings() const
+{
+ I_D(BaseInstance);
+ return *d->m_settings;
+}
+
+QString BaseInstance::baseJar() const
+{
+ I_D(BaseInstance);
+ bool customJar = d->m_settings->get("UseCustomBaseJar").toBool();
+ if (customJar)
+ {
+ return customBaseJar();
+ }
+ else
+ return defaultBaseJar();
+}
+
+QString BaseInstance::customBaseJar() const
+{
+ I_D(BaseInstance);
+ QString value = d->m_settings->get("CustomBaseJar").toString();
+ if (value.isNull() || value.isEmpty())
+ {
+ return defaultCustomBaseJar();
+ }
+ return value;
+}
+
+void BaseInstance::setCustomBaseJar(QString val)
+{
+ I_D(BaseInstance);
+ if (val.isNull() || val.isEmpty() || val == defaultCustomBaseJar())
+ d->m_settings->reset("CustomBaseJar");
+ else
+ d->m_settings->set("CustomBaseJar", val);
+}
+
+void BaseInstance::setShouldUseCustomBaseJar(bool val)
+{
+ I_D(BaseInstance);
+ d->m_settings->set("UseCustomBaseJar", val);
+}
+
+bool BaseInstance::shouldUseCustomBaseJar() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("UseCustomBaseJar").toBool();
+}
+
+qint64 BaseInstance::lastLaunch() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("lastLaunchTime").value<qint64>();
+}
+void BaseInstance::setLastLaunch(qint64 val)
+{
+ I_D(BaseInstance);
+ d->m_settings->set("lastLaunchTime", val);
+ emit propertiesChanged(this);
+}
+
+void BaseInstance::setGroupInitial(QString val)
+{
+ I_D(BaseInstance);
+ d->m_group = val;
+ emit propertiesChanged(this);
+}
+
+void BaseInstance::setGroupPost(QString val)
+{
+ setGroupInitial(val);
+ emit groupChanged();
+}
+
+QString BaseInstance::group() const
+{
+ I_D(BaseInstance);
+ return d->m_group;
+}
+
+void BaseInstance::setNotes(QString val)
+{
+ I_D(BaseInstance);
+ d->m_settings->set("notes", val);
+}
+QString BaseInstance::notes() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("notes").toString();
+}
+
+void BaseInstance::setIconKey(QString val)
+{
+ I_D(BaseInstance);
+ d->m_settings->set("iconKey", val);
+ emit propertiesChanged(this);
+}
+QString BaseInstance::iconKey() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("iconKey").toString();
+}
+
+void BaseInstance::setName(QString val)
+{
+ I_D(BaseInstance);
+ d->m_settings->set("name", val);
+ emit propertiesChanged(this);
+}
+
+QString BaseInstance::name() const
+{
+ I_D(BaseInstance);
+ return d->m_settings->get("name").toString();
+}
+
+QString BaseInstance::windowTitle() const
+{
+ return "MultiMC: " + name();
+}
+
+QStringList BaseInstance::extraArguments() const
+{
+ return Util::Commandline::splitArgs(settings().get("JvmArgs").toString());
+}
diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h
new file mode 100644
index 00000000..cd49f99b
--- /dev/null
+++ b/logic/BaseInstance.h
@@ -0,0 +1,200 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QDateTime>
+
+#include <settingsobject.h>
+
+#include "inifile.h"
+#include "lists/BaseVersionList.h"
+#include "logic/auth/MojangAccount.h"
+
+class QDialog;
+class Task;
+class MinecraftProcess;
+class OneSixUpdate;
+class InstanceList;
+class BaseInstancePrivate;
+
+/*!
+ * \brief Base class for instances.
+ * This class implements many functions that are common between instances and
+ * provides a standard interface for all instances.
+ *
+ * To create a new instance type, create a new class inheriting from this class
+ * and implement the pure virtual functions.
+ */
+class BaseInstance : public QObject
+{
+ Q_OBJECT
+protected:
+ /// no-touchy!
+ BaseInstance(BaseInstancePrivate *d, const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+
+public:
+ /// virtual destructor to make sure the destruction is COMPLETE
+ virtual ~BaseInstance() {};
+
+ /// nuke thoroughly - deletes the instance contents, notifies the list/model which is
+ /// responsible of cleaning up the husk
+ void nuke();
+
+ /// The instance's ID. The ID SHALL be determined by MMC internally. The ID IS guaranteed to
+ /// be unique.
+ virtual QString id() const;
+
+ /// get the type of this instance
+ QString instanceType() const;
+
+ /// Path to the instance's root directory.
+ QString instanceRoot() const;
+
+ /// Path to the instance's minecraft directory.
+ QString minecraftRoot() const;
+
+ QString name() const;
+ void setName(QString val);
+
+ /// Value used for instance window titles
+ QString windowTitle() const;
+
+ QString iconKey() const;
+ void setIconKey(QString val);
+
+ QString notes() const;
+ void setNotes(QString val);
+
+ QString group() const;
+ void setGroupInitial(QString val);
+ void setGroupPost(QString val);
+
+ QStringList extraArguments() const;
+
+ virtual QString intendedVersionId() const = 0;
+ virtual bool setIntendedVersionId(QString version) = 0;
+
+ virtual bool versionIsCustom() = 0;
+
+ /*!
+ * The instance's current version.
+ * This value represents the instance's current version. If this value is
+ * different from the intendedVersion, the instance should be updated.
+ * \warning Don't change this value unless you know what you're doing.
+ */
+ virtual QString currentVersionId() const = 0;
+
+ /*!
+ * Whether or not Minecraft should be downloaded when the instance is launched.
+ */
+ virtual bool shouldUpdate() const = 0;
+ virtual void setShouldUpdate(bool val) = 0;
+
+ /// Get the curent base jar of this instance. By default, it's the
+ /// versions/$version/$version.jar
+ QString baseJar() const;
+
+ /// the default base jar of this instance
+ virtual QString defaultBaseJar() const = 0;
+ /// the default custom base jar of this instance
+ virtual QString defaultCustomBaseJar() const = 0;
+
+ /*!
+ * Whether or not custom base jar is used
+ */
+ bool shouldUseCustomBaseJar() const;
+ void setShouldUseCustomBaseJar(bool val);
+ /*!
+ * The value of the custom base jar
+ */
+ QString customBaseJar() const;
+ void setCustomBaseJar(QString val);
+
+ /**
+ * Gets the time that the instance was last launched.
+ * Stored in milliseconds since epoch.
+ */
+ qint64 lastLaunch() const;
+ /// Sets the last launched time to 'val' milliseconds since epoch
+ void setLastLaunch(qint64 val = QDateTime::currentMSecsSinceEpoch());
+
+ /*!
+ * \brief Gets the instance list that this instance is a part of.
+ * Returns NULL if this instance is not in a list
+ * (the parent is not an InstanceList).
+ * \return A pointer to the InstanceList containing this instance.
+ */
+ InstanceList *instList() const;
+
+ /*!
+ * \brief Gets a pointer to this instance's version list.
+ * \return A pointer to the available version list for this instance.
+ */
+ virtual std::shared_ptr<BaseVersionList> versionList() const;
+
+ /*!
+ * \brief Gets this instance's settings object.
+ * This settings object stores instance-specific settings.
+ * \return A pointer to this instance's settings object.
+ */
+ virtual SettingsObject &settings() const;
+
+ /// returns a valid update task
+ virtual std::shared_ptr<Task> doUpdate() = 0;
+
+ /// returns a valid minecraft process, ready for launch with the given account.
+ virtual MinecraftProcess *prepareForLaunch(AuthSessionPtr account) = 0;
+
+ /// do any necessary cleanups after the instance finishes. also runs before
+ /// 'prepareForLaunch'
+ virtual void cleanupAfterRun() = 0;
+
+ /// create a mod edit dialog for the instance
+ virtual QDialog *createModEditDialog(QWidget *parent) = 0;
+
+ /// is a particular action enabled with this instance selected?
+ virtual bool menuActionEnabled(QString action_name) const = 0;
+
+ virtual QString getStatusbarDescription() = 0;
+
+ /// FIXME: this really should be elsewhere...
+ virtual QString instanceConfigFolder() const = 0;
+
+signals:
+ /*!
+ * \brief Signal emitted when properties relevant to the instance view change
+ */
+ void propertiesChanged(BaseInstance *inst);
+ /*!
+ * \brief Signal emitted when groups are affected in any way
+ */
+ void groupChanged();
+ /*!
+ * \brief The instance just got nuked. Hurray!
+ */
+ void nuked(BaseInstance *inst);
+
+protected slots:
+ void iconUpdated(QString key);
+
+protected:
+ std::shared_ptr<BaseInstancePrivate> inst_d;
+};
+
+// pointer for lazy people
+typedef std::shared_ptr<BaseInstance> InstancePtr;
diff --git a/logic/BaseInstance_p.h b/logic/BaseInstance_p.h
new file mode 100644
index 00000000..06581a34
--- /dev/null
+++ b/logic/BaseInstance_p.h
@@ -0,0 +1,29 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <settingsobject.h>
+
+class BaseInstance;
+
+#define I_D(Class) Class##Private *const d = (Class##Private * const)inst_d.get()
+
+struct BaseInstancePrivate
+{
+ QString m_rootDir;
+ QString m_group;
+ SettingsObject *m_settings;
+}; \ No newline at end of file
diff --git a/logic/BaseVersion.h b/logic/BaseVersion.h
new file mode 100644
index 00000000..43f5942a
--- /dev/null
+++ b/logic/BaseVersion.h
@@ -0,0 +1,55 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <memory>
+
+/*!
+ * An abstract base class for versions.
+ */
+struct BaseVersion
+{
+ /*!
+ * A string used to identify this version in config files.
+ * This should be unique within the version list or shenanigans will occur.
+ */
+ virtual QString descriptor() = 0;
+
+ /*!
+ * The name of this version as it is displayed to the user.
+ * For example: "1.5.1"
+ */
+ virtual QString name() = 0;
+
+ /*!
+ * This should return a string that describes
+ * the kind of version this is (Stable, Beta, Snapshot, whatever)
+ */
+ virtual QString typeString() const = 0;
+
+ virtual bool operator<(BaseVersion &a)
+ {
+ return name() < a.name();
+ };
+ virtual bool operator>(BaseVersion &a)
+ {
+ return name() > a.name();
+ };
+};
+
+typedef std::shared_ptr<BaseVersion> BaseVersionPtr;
+
+Q_DECLARE_METATYPE(BaseVersionPtr) \ No newline at end of file
diff --git a/logic/EnabledItemFilter.cpp b/logic/EnabledItemFilter.cpp
new file mode 100644
index 00000000..c252a0ad
--- /dev/null
+++ b/logic/EnabledItemFilter.cpp
@@ -0,0 +1,43 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "EnabledItemFilter.h"
+
+EnabledItemFilter::EnabledItemFilter(QObject *parent) : QSortFilterProxyModel(parent)
+{
+}
+
+void EnabledItemFilter::setActive(bool active)
+{
+ m_active = active;
+ invalidateFilter();
+}
+
+bool EnabledItemFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
+{
+ if (!m_active)
+ return true;
+ QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
+ if (sourceModel()->flags(index) & Qt::ItemIsEnabled)
+ {
+ return true;
+ }
+ return false;
+}
+
+bool EnabledItemFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const
+{
+ return QSortFilterProxyModel::lessThan(left, right);
+}
diff --git a/logic/EnabledItemFilter.h b/logic/EnabledItemFilter.h
new file mode 100644
index 00000000..bf5e1e85
--- /dev/null
+++ b/logic/EnabledItemFilter.h
@@ -0,0 +1,32 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QSortFilterProxyModel>
+
+class EnabledItemFilter : public QSortFilterProxyModel
+{
+ Q_OBJECT
+public:
+ EnabledItemFilter(QObject *parent = 0);
+ void setActive(bool active);
+
+protected:
+ bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
+ bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
+
+private:
+ bool m_active = false;
+}; \ No newline at end of file
diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp
new file mode 100644
index 00000000..8d4c5b41
--- /dev/null
+++ b/logic/ForgeInstaller.cpp
@@ -0,0 +1,155 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ForgeInstaller.h"
+#include "OneSixVersion.h"
+#include "OneSixLibrary.h"
+#include "net/HttpMetaCache.h"
+#include <quazip.h>
+#include <quazipfile.h>
+#include <pathutils.h>
+#include <QStringList>
+#include "MultiMC.h"
+
+ForgeInstaller::ForgeInstaller(QString filename, QString universal_url)
+{
+ std::shared_ptr<OneSixVersion> newVersion;
+ m_universal_url = universal_url;
+
+ QuaZip zip(filename);
+ if (!zip.open(QuaZip::mdUnzip))
+ return;
+
+ QuaZipFile file(&zip);
+
+ // read the install profile
+ if (!zip.setCurrentFile("install_profile.json"))
+ return;
+
+ QJsonParseError jsonError;
+ if (!file.open(QIODevice::ReadOnly))
+ return;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &jsonError);
+ file.close();
+ if (jsonError.error != QJsonParseError::NoError)
+ return;
+
+ if (!jsonDoc.isObject())
+ return;
+
+ QJsonObject root = jsonDoc.object();
+
+ auto installVal = root.value("install");
+ auto versionInfoVal = root.value("versionInfo");
+ if (!installVal.isObject() || !versionInfoVal.isObject())
+ return;
+
+ // read the forge version info
+ {
+ newVersion = OneSixVersion::fromJson(versionInfoVal.toObject());
+ if (!newVersion)
+ return;
+ }
+
+ QJsonObject installObj = installVal.toObject();
+ QString libraryName = installObj.value("path").toString();
+ internalPath = installObj.value("filePath").toString();
+
+ // where do we put the library? decode the mojang path
+ OneSixLibrary lib(libraryName);
+ lib.finalize();
+
+ auto cacheentry = MMC->metacache()->resolveEntry("libraries", lib.storagePath());
+ finalPath = "libraries/" + lib.storagePath();
+ if (!ensureFilePathExists(finalPath))
+ return;
+
+ if (!zip.setCurrentFile(internalPath))
+ return;
+ if (!file.open(QIODevice::ReadOnly))
+ return;
+ {
+ QByteArray data = file.readAll();
+ // extract file
+ QSaveFile extraction(finalPath);
+ if (!extraction.open(QIODevice::WriteOnly))
+ return;
+ if (extraction.write(data) != data.size())
+ return;
+ if (!extraction.commit())
+ return;
+ QCryptographicHash md5sum(QCryptographicHash::Md5);
+ md5sum.addData(data);
+
+ cacheentry->stale = false;
+ cacheentry->md5sum = md5sum.result().toHex().constData();
+ MMC->metacache()->updateEntry(cacheentry);
+ }
+ file.close();
+
+ m_forge_version = newVersion;
+ realVersionId = m_forge_version->id = installObj.value("minecraft").toString();
+}
+
+bool ForgeInstaller::apply(std::shared_ptr<OneSixVersion> to)
+{
+ if (!m_forge_version)
+ return false;
+ to->externalUpdateStart();
+ int sliding_insert_window = 0;
+ {
+ // for each library in the version we are adding (except for the blacklisted)
+ QSet<QString> blacklist{"lwjgl", "lwjgl_util", "lwjgl-platform"};
+ for (auto lib : m_forge_version->libraries)
+ {
+ QString libName = lib->name();
+ // WARNING: This could actually break.
+ // if this is the actual forge lib, set an absolute url for the download
+ if (libName.contains("minecraftforge"))
+ {
+ lib->setAbsoluteUrl(m_universal_url);
+ }
+ else if (libName.contains("scala"))
+ {
+ lib->setHint("forge-pack-xz");
+ }
+ if (blacklist.contains(libName))
+ continue;
+
+ // find an entry that matches this one
+ bool found = false;
+ for (auto tolib : to->libraries)
+ {
+ if (tolib->name() != libName)
+ continue;
+ found = true;
+ // replace lib
+ tolib = lib;
+ break;
+ }
+ if (!found)
+ {
+ // add lib
+ to->libraries.insert(sliding_insert_window, lib);
+ sliding_insert_window++;
+ }
+ }
+ to->mainClass = m_forge_version->mainClass;
+ to->minecraftArguments = m_forge_version->minecraftArguments;
+ to->processArguments = m_forge_version->processArguments;
+ }
+ to->externalUpdateFinish();
+ return to->toOriginalFile();
+}
diff --git a/logic/ForgeInstaller.h b/logic/ForgeInstaller.h
new file mode 100644
index 00000000..0b9f9c77
--- /dev/null
+++ b/logic/ForgeInstaller.h
@@ -0,0 +1,36 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <memory>
+
+class OneSixVersion;
+
+class ForgeInstaller
+{
+public:
+ ForgeInstaller(QString filename, QString universal_url);
+
+ bool apply(std::shared_ptr<OneSixVersion> to);
+
+private:
+ // the version, read from the installer
+ std::shared_ptr<OneSixVersion> m_forge_version;
+ QString internalPath;
+ QString finalPath;
+ QString realVersionId;
+ QString m_universal_url;
+};
diff --git a/logic/InstanceFactory.cpp b/logic/InstanceFactory.cpp
new file mode 100644
index 00000000..1f1a5879
--- /dev/null
+++ b/logic/InstanceFactory.cpp
@@ -0,0 +1,196 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "InstanceFactory.h"
+
+#include <QDir>
+#include <QFileInfo>
+
+#include "BaseInstance.h"
+#include "LegacyInstance.h"
+#include "LegacyFTBInstance.h"
+#include "OneSixInstance.h"
+#include "OneSixFTBInstance.h"
+#include "NostalgiaInstance.h"
+#include "BaseVersion.h"
+#include "MinecraftVersion.h"
+
+#include "inifile.h"
+#include <inisettingsobject.h>
+#include <setting.h>
+
+#include "pathutils.h"
+#include "logger/QsLog.h"
+
+InstanceFactory InstanceFactory::loader;
+
+InstanceFactory::InstanceFactory() : QObject(NULL)
+{
+}
+
+InstanceFactory::InstLoadError InstanceFactory::loadInstance(BaseInstance *&inst,
+ const QString &instDir)
+{
+ auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg"));
+
+ m_settings->registerSetting("InstanceType", "Legacy");
+
+ QString inst_type = m_settings->get("InstanceType").toString();
+
+ // FIXME: replace with a map lookup, where instance classes register their types
+ if (inst_type == "Legacy")
+ {
+ inst = new LegacyInstance(instDir, m_settings, this);
+ }
+ else if (inst_type == "OneSix")
+ {
+ inst = new OneSixInstance(instDir, m_settings, this);
+ }
+ else if (inst_type == "Nostalgia")
+ {
+ inst = new NostalgiaInstance(instDir, m_settings, this);
+ }
+ else if (inst_type == "LegacyFTB")
+ {
+ inst = new LegacyFTBInstance(instDir, m_settings, this);
+ }
+ else if (inst_type == "OneSixFTB")
+ {
+ inst = new OneSixFTBInstance(instDir, m_settings, this);
+ }
+ else
+ {
+ return InstanceFactory::UnknownLoadError;
+ }
+ return NoLoadError;
+}
+
+InstanceFactory::InstCreateError InstanceFactory::createInstance(BaseInstance *&inst,
+ BaseVersionPtr version,
+ const QString &instDir,
+ const InstType type)
+{
+ QDir rootDir(instDir);
+
+ QLOG_DEBUG() << instDir.toUtf8();
+ if (!rootDir.exists() && !rootDir.mkpath("."))
+ {
+ return InstanceFactory::CantCreateDir;
+ }
+ auto mcVer = std::dynamic_pointer_cast<MinecraftVersion>(version);
+ if (!mcVer)
+ return InstanceFactory::NoSuchVersion;
+
+ auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg"));
+ m_settings->registerSetting("InstanceType", "Legacy");
+
+ if (type == NormalInst)
+ {
+ switch (mcVer->type)
+ {
+ case MinecraftVersion::Legacy:
+ m_settings->set("InstanceType", "Legacy");
+ inst = new LegacyInstance(instDir, m_settings, this);
+ inst->setIntendedVersionId(version->descriptor());
+ inst->setShouldUseCustomBaseJar(false);
+ break;
+ case MinecraftVersion::OneSix:
+ m_settings->set("InstanceType", "OneSix");
+ inst = new OneSixInstance(instDir, m_settings, this);
+ inst->setIntendedVersionId(version->descriptor());
+ inst->setShouldUseCustomBaseJar(false);
+ break;
+ case MinecraftVersion::Nostalgia:
+ m_settings->set("InstanceType", "Nostalgia");
+ inst = new NostalgiaInstance(instDir, m_settings, this);
+ inst->setIntendedVersionId(version->descriptor());
+ inst->setShouldUseCustomBaseJar(false);
+ break;
+ default:
+ {
+ delete m_settings;
+ return InstanceFactory::NoSuchVersion;
+ }
+ }
+ }
+ else if (type == FTBInstance)
+ {
+ switch (mcVer->type)
+ {
+ case MinecraftVersion::Legacy:
+ m_settings->set("InstanceType", "LegacyFTB");
+ inst = new LegacyFTBInstance(instDir, m_settings, this);
+ inst->setIntendedVersionId(version->descriptor());
+ inst->setShouldUseCustomBaseJar(false);
+ break;
+ case MinecraftVersion::OneSix:
+ m_settings->set("InstanceType", "OneSixFTB");
+ inst = new OneSixFTBInstance(instDir, m_settings, this);
+ inst->setIntendedVersionId(version->descriptor());
+ inst->setShouldUseCustomBaseJar(false);
+ break;
+ default:
+ {
+ delete m_settings;
+ return InstanceFactory::NoSuchVersion;
+ }
+ }
+ }
+ else
+ {
+ delete m_settings;
+ return InstanceFactory::NoSuchVersion;
+ }
+
+ // FIXME: really, how do you even know?
+ return InstanceFactory::NoCreateError;
+}
+
+InstanceFactory::InstCreateError InstanceFactory::copyInstance(BaseInstance *&newInstance,
+ BaseInstance *&oldInstance,
+ const QString &instDir)
+{
+ QDir rootDir(instDir);
+
+ QLOG_DEBUG() << instDir.toUtf8();
+ if (!copyPath(oldInstance->instanceRoot(), instDir))
+ {
+ rootDir.removeRecursively();
+ return InstanceFactory::CantCreateDir;
+ }
+ auto m_settings = new INISettingsObject(PathCombine(instDir, "instance.cfg"));
+ m_settings->registerSetting("InstanceType", "Legacy");
+ QString inst_type = m_settings->get("InstanceType").toString();
+
+ if(inst_type == "OneSixFTB")
+ m_settings->set("InstanceType", "OneSix");
+ if(inst_type == "LegacyFTB")
+ m_settings->set("InstanceType", "Legacy");
+
+ auto error = loadInstance(newInstance, instDir);
+
+ switch (error)
+ {
+ case NoLoadError:
+ return NoCreateError;
+ case NotAnInstance:
+ rootDir.removeRecursively();
+ return CantCreateDir;
+ default:
+ case UnknownLoadError:
+ rootDir.removeRecursively();
+ return UnknownCreateError;
+ }
+}
diff --git a/logic/InstanceFactory.h b/logic/InstanceFactory.h
new file mode 100644
index 00000000..5ff4c7ec
--- /dev/null
+++ b/logic/InstanceFactory.h
@@ -0,0 +1,105 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QMap>
+#include <QList>
+
+#include "BaseVersion.h"
+
+class BaseVersion;
+class BaseInstance;
+
+/*!
+ * The \bInstanceFactory\b is a singleton that manages loading and creating instances.
+ */
+class InstanceFactory : public QObject
+{
+ Q_OBJECT
+public:
+ /*!
+ * \brief Gets a reference to the instance loader.
+ */
+ static InstanceFactory &get()
+ {
+ return loader;
+ }
+
+ enum InstLoadError
+ {
+ NoLoadError = 0,
+ UnknownLoadError,
+ NotAnInstance
+ };
+
+ enum InstCreateError
+ {
+ NoCreateError = 0,
+ NoSuchVersion,
+ UnknownCreateError,
+ InstExists,
+ CantCreateDir
+ };
+
+ enum InstType
+ {
+ NormalInst,
+ FTBInstance
+ };
+
+ /*!
+ * \brief Creates a stub instance
+ *
+ * \param inst Pointer to store the created instance in.
+ * \param version Game version to use for the instance
+ * \param instDir The new instance's directory.
+ * \param type The type of instance to create
+ * \return An InstCreateError error code.
+ * - InstExists if the given instance directory is already an instance.
+ * - CantCreateDir if the given instance directory cannot be created.
+ */
+ InstCreateError createInstance(BaseInstance *&inst, BaseVersionPtr version,
+ const QString &instDir, const InstType type = NormalInst);
+
+ /*!
+ * \brief Creates a copy of an existing instance with a new name
+ *
+ * \param newInstance Pointer to store the created instance in.
+ * \param oldInstance The instance to copy
+ * \param instDir The new instance's directory.
+ * \return An InstCreateError error code.
+ * - InstExists if the given instance directory is already an instance.
+ * - CantCreateDir if the given instance directory cannot be created.
+ */
+ InstCreateError copyInstance(BaseInstance *&newInstance, BaseInstance *&oldInstance,
+ const QString &instDir);
+
+ /*!
+ * \brief Loads an instance from the given directory.
+ * Checks the instance's INI file to figure out what the instance's type is first.
+ * \param inst Pointer to store the loaded instance in.
+ * \param instDir The instance's directory.
+ * \return An InstLoadError error code.
+ * - NotAnInstance if the given instance directory isn't a valid instance.
+ */
+ InstLoadError loadInstance(BaseInstance *&inst, const QString &instDir);
+
+private:
+ InstanceFactory();
+
+ static InstanceFactory loader;
+};
diff --git a/logic/InstanceLauncher.cpp b/logic/InstanceLauncher.cpp
new file mode 100644
index 00000000..c0079d80
--- /dev/null
+++ b/logic/InstanceLauncher.cpp
@@ -0,0 +1,94 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+
+#include "InstanceLauncher.h"
+#include "MultiMC.h"
+
+#include "gui/ConsoleWindow.h"
+#include "gui/dialogs/ProgressDialog.h"
+
+#include "logic/MinecraftProcess.h"
+#include "logic/lists/InstanceList.h"
+
+InstanceLauncher::InstanceLauncher(QString instId) : QObject(), instId(instId)
+{
+}
+
+void InstanceLauncher::onTerminated()
+{
+ std::cout << "Minecraft exited" << std::endl;
+ MMC->quit();
+}
+
+void InstanceLauncher::onLoginComplete()
+{
+ // TODO: Fix this.
+ /*
+ LoginTask *task = (LoginTask *)QObject::sender();
+ auto result = task->getResult();
+ auto instance = MMC->instances()->getInstanceById(instId);
+ proc = instance->prepareForLaunch(result);
+ if (!proc)
+ {
+ // FIXME: report error
+ return;
+ }
+ console = new ConsoleWindow(proc);
+ connect(console, SIGNAL(isClosing()), this, SLOT(onTerminated()));
+
+ proc->setLogin(result.username, result.session_id);
+ proc->launch();
+ */
+}
+
+void InstanceLauncher::doLogin(const QString &errorMsg)
+{
+ // FIXME: Use new account system here...
+ /*
+ LoginDialog *loginDlg = new LoginDialog(nullptr, errorMsg);
+ loginDlg->exec();
+ if (loginDlg->result() == QDialog::Accepted)
+ {
+ PasswordLogin uInfo{loginDlg->getUsername(), loginDlg->getPassword()};
+
+ ProgressDialog *tDialog = new ProgressDialog(nullptr);
+ LoginTask *loginTask = new LoginTask(uInfo, tDialog);
+ connect(loginTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), Qt::QueuedConnection);
+ connect(loginTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)),
+ Qt::QueuedConnection);
+ tDialog->exec(loginTask);
+ }
+ */
+ // onLoginComplete(LoginResponse("Offline","Offline", 1));
+}
+
+int InstanceLauncher::launch()
+{
+ std::cout << "Launching Instance '" << qPrintable(instId) << "'" << std::endl;
+ auto instance = MMC->instances()->getInstanceById(instId);
+ if (!instance)
+ {
+ std::cout << "Could not find instance requested. note that you have to specify the ID, "
+ "not the NAME" << std::endl;
+ return 1;
+ }
+
+ std::cout << "Logging in..." << std::endl;
+ doLogin("");
+
+ return MMC->exec();
+}
diff --git a/logic/InstanceLauncher.h b/logic/InstanceLauncher.h
new file mode 100644
index 00000000..107c069f
--- /dev/null
+++ b/logic/InstanceLauncher.h
@@ -0,0 +1,44 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+
+class MinecraftProcess;
+class ConsoleWindow;
+
+// Commandline instance launcher
+class InstanceLauncher : public QObject
+{
+ Q_OBJECT
+
+private:
+ QString instId;
+ MinecraftProcess *proc;
+ ConsoleWindow *console;
+
+public:
+ InstanceLauncher(QString instId);
+
+private
+slots:
+ void onTerminated();
+ void onLoginComplete();
+ void doLogin(const QString &errorMsg);
+
+public:
+ int launch();
+};
diff --git a/logic/JavaChecker.cpp b/logic/JavaChecker.cpp
new file mode 100644
index 00000000..b87ee3d5
--- /dev/null
+++ b/logic/JavaChecker.cpp
@@ -0,0 +1,124 @@
+#include "JavaChecker.h"
+#include "MultiMC.h"
+#include <pathutils.h>
+#include <QFile>
+#include <QProcess>
+#include <QMap>
+#include <QTemporaryFile>
+
+JavaChecker::JavaChecker(QObject *parent) : QObject(parent)
+{
+}
+
+void JavaChecker::performCheck()
+{
+ QString checkerJar = PathCombine(MMC->bin(), "jars", "JavaCheck.jar");
+
+ QStringList args = {"-jar", checkerJar};
+
+ process.reset(new QProcess());
+ process->setArguments(args);
+ process->setProgram(path);
+ process->setProcessChannelMode(QProcess::SeparateChannels);
+ QLOG_DEBUG() << "Running java checker!";
+ QLOG_DEBUG() << "Java: " + path;
+ QLOG_DEBUG() << "Args: {" + args.join("|") + "}";
+
+ connect(process.get(), SIGNAL(finished(int, QProcess::ExitStatus)), this,
+ SLOT(finished(int, QProcess::ExitStatus)));
+ connect(process.get(), SIGNAL(error(QProcess::ProcessError)), this,
+ SLOT(error(QProcess::ProcessError)));
+ connect(&killTimer, SIGNAL(timeout()), SLOT(timeout()));
+ killTimer.setSingleShot(true);
+ killTimer.start(5000);
+ process->start();
+}
+
+void JavaChecker::finished(int exitcode, QProcess::ExitStatus status)
+{
+ killTimer.stop();
+ QProcessPtr _process;
+ _process.swap(process);
+
+ JavaCheckResult result;
+ {
+ result.path = path;
+ result.id = id;
+ }
+ QLOG_DEBUG() << "Java checker finished with status " << status << " exit code " << exitcode;
+
+ if (status == QProcess::CrashExit || exitcode == 1)
+ {
+ QLOG_DEBUG() << "Java checker failed!";
+ emit checkFinished(result);
+ return;
+ }
+
+ bool success = true;
+ QString p_stdout = _process->readAllStandardOutput();
+ QLOG_DEBUG() << p_stdout;
+
+ QMap<QString, QString> results;
+ QStringList lines = p_stdout.split("\n", QString::SkipEmptyParts);
+ for(QString line : lines)
+ {
+ line = line.trimmed();
+
+ auto parts = line.split('=', QString::SkipEmptyParts);
+ if(parts.size() != 2 || parts[0].isEmpty() || parts[1].isEmpty())
+ {
+ success = false;
+ }
+ else
+ {
+ results.insert(parts[0], parts[1]);
+ }
+ }
+
+ if(!results.contains("os.arch") || !results.contains("java.version") || !success)
+ {
+ QLOG_DEBUG() << "Java checker failed - couldn't extract required information.";
+ emit checkFinished(result);
+ return;
+ }
+
+ auto os_arch = results["os.arch"];
+ auto java_version = results["java.version"];
+ bool is_64 = os_arch == "x86_64" || os_arch == "amd64";
+
+
+ result.valid = true;
+ result.is_64bit = is_64;
+ result.mojangPlatform = is_64 ? "64" : "32";
+ result.realPlatform = os_arch;
+ result.javaVersion = java_version;
+ QLOG_DEBUG() << "Java checker succeeded.";
+ emit checkFinished(result);
+}
+
+void JavaChecker::error(QProcess::ProcessError err)
+{
+ if(err == QProcess::FailedToStart)
+ {
+ killTimer.stop();
+ QLOG_DEBUG() << "Java checker has failed to start.";
+ JavaCheckResult result;
+ {
+ result.path = path;
+ result.id = id;
+ }
+
+ emit checkFinished(result);
+ return;
+ }
+}
+
+void JavaChecker::timeout()
+{
+ // NO MERCY. NO ABUSE.
+ if(process)
+ {
+ QLOG_DEBUG() << "Java checker has been killed by timeout.";
+ process->kill();
+ }
+}
diff --git a/logic/JavaChecker.h b/logic/JavaChecker.h
new file mode 100644
index 00000000..e19895f7
--- /dev/null
+++ b/logic/JavaChecker.h
@@ -0,0 +1,42 @@
+#pragma once
+#include <QProcess>
+#include <QTimer>
+#include <memory>
+
+class JavaChecker;
+
+
+struct JavaCheckResult
+{
+ QString path;
+ QString mojangPlatform;
+ QString realPlatform;
+ QString javaVersion;
+ bool valid = false;
+ bool is_64bit = false;
+ int id;
+};
+
+typedef std::shared_ptr<QProcess> QProcessPtr;
+typedef std::shared_ptr<JavaChecker> JavaCheckerPtr;
+class JavaChecker : public QObject
+{
+ Q_OBJECT
+public:
+ explicit JavaChecker(QObject *parent = 0);
+ void performCheck();
+
+ QString path;
+ int id;
+
+signals:
+ void checkFinished(JavaCheckResult result);
+private:
+ QProcessPtr process;
+ QTimer killTimer;
+public
+slots:
+ void timeout();
+ void finished(int exitcode, QProcess::ExitStatus);
+ void error(QProcess::ProcessError);
+};
diff --git a/logic/JavaCheckerJob.cpp b/logic/JavaCheckerJob.cpp
new file mode 100644
index 00000000..b0aea758
--- /dev/null
+++ b/logic/JavaCheckerJob.cpp
@@ -0,0 +1,47 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JavaCheckerJob.h"
+#include "pathutils.h"
+#include "MultiMC.h"
+
+#include "logger/QsLog.h"
+
+void JavaCheckerJob::partFinished(JavaCheckResult result)
+{
+ num_finished++;
+ QLOG_INFO() << m_job_name.toLocal8Bit() << "progress:" << num_finished << "/"
+ << javacheckers.size();
+ emit progress(num_finished, javacheckers.size());
+
+ javaresults.replace(result.id, result);
+
+ if (num_finished == javacheckers.size())
+ {
+ emit finished(javaresults);
+ }
+}
+
+void JavaCheckerJob::start()
+{
+ QLOG_INFO() << m_job_name.toLocal8Bit() << " started.";
+ m_running = true;
+ for (auto iter : javacheckers)
+ {
+ javaresults.append(JavaCheckResult());
+ connect(iter.get(), SIGNAL(checkFinished(JavaCheckResult)), SLOT(partFinished(JavaCheckResult)));
+ iter->performCheck();
+ }
+}
diff --git a/logic/JavaCheckerJob.h b/logic/JavaCheckerJob.h
new file mode 100644
index 00000000..132a92d4
--- /dev/null
+++ b/logic/JavaCheckerJob.h
@@ -0,0 +1,100 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QtNetwork>
+#include <QLabel>
+#include "JavaChecker.h"
+#include "logic/tasks/ProgressProvider.h"
+
+class JavaCheckerJob;
+typedef std::shared_ptr<JavaCheckerJob> JavaCheckerJobPtr;
+
+class JavaCheckerJob : public ProgressProvider
+{
+ Q_OBJECT
+public:
+ explicit JavaCheckerJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {};
+
+ bool addJavaCheckerAction(JavaCheckerPtr base)
+ {
+ javacheckers.append(base);
+ total_progress++;
+ // if this is already running, the action needs to be started right away!
+ if (isRunning())
+ {
+ emit progress(current_progress, total_progress);
+ connect(base.get(), SIGNAL(checkFinished(JavaCheckResult)), SLOT(partFinished(JavaCheckResult)));
+
+ base->performCheck();
+ }
+ return true;
+ }
+
+ JavaCheckerPtr operator[](int index)
+ {
+ return javacheckers[index];
+ }
+ ;
+ JavaCheckerPtr first()
+ {
+ if (javacheckers.size())
+ return javacheckers[0];
+ return JavaCheckerPtr();
+ }
+ int size() const
+ {
+ return javacheckers.size();
+ }
+ virtual void getProgress(qint64 &current, qint64 &total)
+ {
+ current = current_progress;
+ total = total_progress;
+ }
+ ;
+ virtual QString getStatus() const
+ {
+ return m_job_name;
+ }
+ ;
+ virtual bool isRunning() const
+ {
+ return m_running;
+ }
+ ;
+
+signals:
+ void started();
+ void progress(int current, int total);
+ void finished(QList<JavaCheckResult>);
+public
+slots:
+ virtual void start();
+ // FIXME: implement
+ virtual void abort() {};
+private
+slots:
+ void partFinished(JavaCheckResult result);
+
+private:
+ QString m_job_name;
+ QList<JavaCheckerPtr> javacheckers;
+ QList<JavaCheckResult> javaresults;
+ qint64 current_progress = 0;
+ qint64 total_progress = 0;
+ int num_finished = 0;
+ bool m_running = false;
+};
diff --git a/logic/JavaUtils.cpp b/logic/JavaUtils.cpp
new file mode 100644
index 00000000..cf47df6f
--- /dev/null
+++ b/logic/JavaUtils.cpp
@@ -0,0 +1,208 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QStringList>
+#include <QString>
+#include <QDir>
+#include <QMessageBox>
+
+#include <setting.h>
+#include <pathutils.h>
+
+#include "MultiMC.h"
+
+#include "JavaUtils.h"
+#include "logger/QsLog.h"
+#include "gui/dialogs/VersionSelectDialog.h"
+#include "JavaCheckerJob.h"
+#include "lists/JavaVersionList.h"
+
+JavaUtils::JavaUtils()
+{
+}
+
+JavaVersionPtr JavaUtils::MakeJavaPtr(QString path, QString id, QString arch)
+{
+ JavaVersionPtr javaVersion(new JavaVersion());
+
+ javaVersion->id = id;
+ javaVersion->arch = arch;
+ javaVersion->path = path;
+
+ return javaVersion;
+}
+
+JavaVersionPtr JavaUtils::GetDefaultJava()
+{
+ JavaVersionPtr javaVersion(new JavaVersion());
+
+ javaVersion->id = "java";
+ javaVersion->arch = "unknown";
+ javaVersion->path = "java";
+
+ return javaVersion;
+}
+
+#if WINDOWS
+QList<JavaVersionPtr> JavaUtils::FindJavaFromRegistryKey(DWORD keyType, QString keyName)
+{
+ QList<JavaVersionPtr> javas;
+
+ QString archType = "unknown";
+ if (keyType == KEY_WOW64_64KEY)
+ archType = "64";
+ else if (keyType == KEY_WOW64_32KEY)
+ archType = "32";
+
+ HKEY jreKey;
+ if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName.toStdString().c_str(), 0,
+ KEY_READ | keyType | KEY_ENUMERATE_SUB_KEYS, &jreKey) == ERROR_SUCCESS)
+ {
+ // Read the current type version from the registry.
+ // This will be used to find any key that contains the JavaHome value.
+ char *value = new char[0];
+ DWORD valueSz = 0;
+ if (RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz) ==
+ ERROR_MORE_DATA)
+ {
+ value = new char[valueSz];
+ RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz);
+ }
+
+ QString recommended = value;
+
+ TCHAR subKeyName[255];
+ DWORD subKeyNameSize, numSubKeys, retCode;
+
+ // Get the number of subkeys
+ RegQueryInfoKey(jreKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL,
+ NULL, NULL);
+
+ // Iterate until RegEnumKeyEx fails
+ if (numSubKeys > 0)
+ {
+ for (int i = 0; i < numSubKeys; i++)
+ {
+ subKeyNameSize = 255;
+ retCode = RegEnumKeyEx(jreKey, i, subKeyName, &subKeyNameSize, NULL, NULL, NULL,
+ NULL);
+ if (retCode == ERROR_SUCCESS)
+ {
+ // Now open the registry key for the version that we just got.
+ QString newKeyName = keyName + "\\" + subKeyName;
+
+ HKEY newKey;
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, newKeyName.toStdString().c_str(), 0,
+ KEY_READ | KEY_WOW64_64KEY, &newKey) == ERROR_SUCCESS)
+ {
+ // Read the JavaHome value to find where Java is installed.
+ value = new char[0];
+ valueSz = 0;
+ if (RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
+ &valueSz) == ERROR_MORE_DATA)
+ {
+ value = new char[valueSz];
+ RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
+ &valueSz);
+
+ // Now, we construct the version object and add it to the list.
+ JavaVersionPtr javaVersion(new JavaVersion());
+
+ javaVersion->id = subKeyName;
+ javaVersion->arch = archType;
+ javaVersion->path =
+ QDir(PathCombine(value, "bin")).absoluteFilePath("java.exe");
+ javas.append(javaVersion);
+ }
+
+ RegCloseKey(newKey);
+ }
+ }
+ }
+ }
+
+ RegCloseKey(jreKey);
+ }
+
+ return javas;
+}
+
+QList<QString> JavaUtils::FindJavaPaths()
+{
+ QList<JavaVersionPtr> java_candidates;
+
+ QList<JavaVersionPtr> JRE64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
+ QList<JavaVersionPtr> JDK64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");
+ QList<JavaVersionPtr> JRE32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
+ QList<JavaVersionPtr> JDK32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");
+
+ java_candidates.append(JRE64s);
+ java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre7/bin/java.exe"));
+ java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre6/bin/java.exe"));
+ java_candidates.append(JDK64s);
+ java_candidates.append(JRE32s);
+ java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre7/bin/java.exe"));
+ java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre6/bin/java.exe"));
+ java_candidates.append(JDK32s);
+ java_candidates.append(MakeJavaPtr(this->GetDefaultJava()->path));
+
+ QList<QString> candidates;
+ for(JavaVersionPtr java_candidate : java_candidates)
+ {
+ if(!candidates.contains(java_candidate->path))
+ {
+ candidates.append(java_candidate->path);
+ }
+ }
+
+ return candidates;
+}
+
+#elif OSX
+QList<QString> JavaUtils::FindJavaPaths()
+{
+ QList<QString> javas;
+ javas.append(this->GetDefaultJava()->path);
+ javas.append("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java");
+ javas.append("/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java");
+
+ return javas;
+}
+
+#elif LINUX
+QList<QString> JavaUtils::FindJavaPaths()
+{
+ QLOG_INFO() << "Linux Java detection incomplete - defaulting to \"java\"";
+
+ QList<QString> javas;
+ javas.append(this->GetDefaultJava()->path);
+
+ return javas;
+}
+#else
+QList<QString> JavaUtils::FindJavaPaths()
+{
+ QLOG_INFO() << "Unknown operating system build - defaulting to \"java\"";
+
+ QList<QString> javas;
+ javas.append(this->GetDefaultJava()->path);
+
+ return javas;
+}
+#endif
diff --git a/logic/JavaUtils.h b/logic/JavaUtils.h
new file mode 100644
index 00000000..22a68ef3
--- /dev/null
+++ b/logic/JavaUtils.h
@@ -0,0 +1,43 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QStringList>
+#include <QWidget>
+
+#include <osutils.h>
+#include "JavaCheckerJob.h"
+#include "JavaChecker.h"
+#include "lists/JavaVersionList.h"
+
+#if WINDOWS
+#include <windows.h>
+#endif
+
+class JavaUtils : public QObject
+{
+ Q_OBJECT
+public:
+ JavaUtils();
+
+ JavaVersionPtr MakeJavaPtr(QString path, QString id = "unknown", QString arch = "unknown");
+ QList<QString> FindJavaPaths();
+ JavaVersionPtr GetDefaultJava();
+
+#if WINDOWS
+ QList<JavaVersionPtr> FindJavaFromRegistryKey(DWORD keyType, QString keyName);
+#endif
+};
diff --git a/logic/LegacyFTBInstance.cpp b/logic/LegacyFTBInstance.cpp
new file mode 100644
index 00000000..6c6bd10b
--- /dev/null
+++ b/logic/LegacyFTBInstance.cpp
@@ -0,0 +1,21 @@
+#include "LegacyFTBInstance.h"
+
+LegacyFTBInstance::LegacyFTBInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) :
+ LegacyInstance(rootDir, settings, parent)
+{
+}
+
+QString LegacyFTBInstance::getStatusbarDescription()
+{
+ return "Legacy FTB: " + intendedVersionId();
+}
+
+bool LegacyFTBInstance::menuActionEnabled(QString action_name) const
+{
+ return false;
+}
+
+QString LegacyFTBInstance::id() const
+{
+ return "FTB/" + BaseInstance::id();
+}
diff --git a/logic/LegacyFTBInstance.h b/logic/LegacyFTBInstance.h
new file mode 100644
index 00000000..70f60535
--- /dev/null
+++ b/logic/LegacyFTBInstance.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "LegacyInstance.h"
+
+class LegacyFTBInstance : public LegacyInstance
+{
+ Q_OBJECT
+public:
+ explicit LegacyFTBInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+ virtual QString getStatusbarDescription();
+ virtual bool menuActionEnabled(QString action_name) const;
+ virtual QString id() const;
+};
diff --git a/logic/LegacyForge.cpp b/logic/LegacyForge.cpp
new file mode 100644
index 00000000..94212ae4
--- /dev/null
+++ b/logic/LegacyForge.cpp
@@ -0,0 +1,56 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LegacyForge.h"
+
+MinecraftForge::MinecraftForge(const QString &file) : Mod(file)
+{
+}
+
+bool MinecraftForge::FixVersionIfNeeded(QString newVersion)
+{/*
+ wxString reportedVersion = GetModVersion();
+ if(reportedVersion == "..." || reportedVersion.empty())
+ {
+ std::auto_ptr<wxFFileInputStream> in(new wxFFileInputStream("forge.zip"));
+ wxTempFileOutputStream out("forge.zip");
+ wxTextOutputStream textout(out);
+ wxZipInputStream inzip(*in);
+ wxZipOutputStream outzip(out);
+ std::auto_ptr<wxZipEntry> entry;
+ // preserve metadata
+ outzip.CopyArchiveMetaData(inzip);
+ // copy all entries
+ while (entry.reset(inzip.GetNextEntry()), entry.get() != NULL)
+ if (!outzip.CopyEntry(entry.release(), inzip))
+ return false;
+ // release last entry
+ in.reset();
+ outzip.PutNextEntry("forgeversion.properties");
+
+ wxStringTokenizer tokenizer(newVersion,".");
+ wxString verFile;
+ verFile << wxString("forge.major.number=") << tokenizer.GetNextToken() << "\n";
+ verFile << wxString("forge.minor.number=") << tokenizer.GetNextToken() << "\n";
+ verFile << wxString("forge.revision.number=") << tokenizer.GetNextToken() << "\n";
+ verFile << wxString("forge.build.number=") << tokenizer.GetNextToken() << "\n";
+ auto buf = verFile.ToUTF8();
+ outzip.Write(buf.data(), buf.length());
+ // check if we succeeded
+ return inzip.Eof() && outzip.Close() && out.Commit();
+ }
+ */
+ return true;
+}
diff --git a/logic/LegacyForge.h b/logic/LegacyForge.h
new file mode 100644
index 00000000..f4165ffa
--- /dev/null
+++ b/logic/LegacyForge.h
@@ -0,0 +1,25 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "Mod.h"
+
+class MinecraftForge : public Mod
+{
+public:
+ MinecraftForge(const QString &file);
+ bool FixVersionIfNeeded(QString newVersion);
+};
diff --git a/logic/LegacyInstance.cpp b/logic/LegacyInstance.cpp
new file mode 100644
index 00000000..a9f0d112
--- /dev/null
+++ b/logic/LegacyInstance.cpp
@@ -0,0 +1,282 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QFileInfo>
+#include <QDir>
+#include <QImage>
+#include <setting.h>
+#include <pathutils.h>
+#include <cmdutils.h>
+
+#include "MultiMC.h"
+
+#include "LegacyInstance.h"
+#include "LegacyInstance_p.h"
+
+#include "logic/MinecraftProcess.h"
+#include "logic/LegacyUpdate.h"
+#include "logic/icons/IconList.h"
+
+#include "gui/dialogs/LegacyModEditDialog.h"
+
+LegacyInstance::LegacyInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent)
+ : BaseInstance(new LegacyInstancePrivate(), rootDir, settings, parent)
+{
+ settings->registerSetting("NeedsRebuild", true);
+ settings->registerSetting("ShouldUpdate", false);
+ settings->registerSetting("JarVersion", "Unknown");
+ settings->registerSetting("LwjglVersion", "2.9.0");
+ settings->registerSetting("IntendedJarVersion", "");
+}
+
+std::shared_ptr<Task> LegacyInstance::doUpdate()
+{
+ // make sure the jar mods list is initialized by asking for it.
+ auto list = jarModList();
+ // create an update task
+ return std::shared_ptr<Task>(new LegacyUpdate(this, this));
+}
+
+MinecraftProcess *LegacyInstance::prepareForLaunch(AuthSessionPtr account)
+{
+ MinecraftProcess *proc = new MinecraftProcess(this);
+
+ QIcon icon = MMC->icons()->getIcon(iconKey());
+ auto pixmap = icon.pixmap(128, 128);
+ pixmap.save(PathCombine(minecraftRoot(), "icon.png"), "PNG");
+
+ // create the launch script
+ QString launchScript;
+ {
+ // window size
+ QString windowParams;
+ if (settings().get("LaunchMaximized").toBool())
+ windowParams = "max";
+ else
+ windowParams = QString("%1x%2")
+ .arg(settings().get("MinecraftWinWidth").toInt())
+ .arg(settings().get("MinecraftWinHeight").toInt());
+
+ QString lwjgl = QDir(MMC->settings()->get("LWJGLDir").toString() + "/" + lwjglVersion())
+ .absolutePath();
+ launchScript += "userName " + account->player_name + "\n";
+ launchScript += "sessionId " + account->session + "\n";
+ launchScript += "windowTitle " + windowTitle() + "\n";
+ launchScript += "windowParams " + windowParams + "\n";
+ launchScript += "lwjgl " + lwjgl + "\n";
+ launchScript += "launch legacy\n";
+ }
+ proc->setLaunchScript(launchScript);
+
+ // set the process work path
+ proc->setWorkdir(minecraftRoot());
+
+ return proc;
+}
+
+void LegacyInstance::cleanupAfterRun()
+{
+ // FIXME: delete the launcher and icons and whatnot.
+}
+
+std::shared_ptr<ModList> LegacyInstance::coreModList()
+{
+ I_D(LegacyInstance);
+ if (!d->core_mod_list)
+ {
+ d->core_mod_list.reset(new ModList(coreModsDir()));
+ }
+ d->core_mod_list->update();
+ return d->core_mod_list;
+}
+
+std::shared_ptr<ModList> LegacyInstance::jarModList()
+{
+ I_D(LegacyInstance);
+ if (!d->jar_mod_list)
+ {
+ auto list = new ModList(jarModsDir(), modListFile());
+ connect(list, SIGNAL(changed()), SLOT(jarModsChanged()));
+ d->jar_mod_list.reset(list);
+ }
+ d->jar_mod_list->update();
+ return d->jar_mod_list;
+}
+
+void LegacyInstance::jarModsChanged()
+{
+ QLOG_INFO() << "Jar mods of instance " << name() << " have changed. Jar will be rebuilt.";
+ setShouldRebuild(true);
+}
+
+std::shared_ptr<ModList> LegacyInstance::loaderModList()
+{
+ I_D(LegacyInstance);
+ if (!d->loader_mod_list)
+ {
+ d->loader_mod_list.reset(new ModList(loaderModsDir()));
+ }
+ d->loader_mod_list->update();
+ return d->loader_mod_list;
+}
+
+std::shared_ptr<ModList> LegacyInstance::texturePackList()
+{
+ I_D(LegacyInstance);
+ if (!d->texture_pack_list)
+ {
+ d->texture_pack_list.reset(new ModList(texturePacksDir()));
+ }
+ d->texture_pack_list->update();
+ return d->texture_pack_list;
+}
+
+QDialog *LegacyInstance::createModEditDialog(QWidget *parent)
+{
+ return new LegacyModEditDialog(this, parent);
+}
+
+QString LegacyInstance::jarModsDir() const
+{
+ return PathCombine(instanceRoot(), "instMods");
+}
+
+QString LegacyInstance::binDir() const
+{
+ return PathCombine(minecraftRoot(), "bin");
+}
+
+QString LegacyInstance::savesDir() const
+{
+ return PathCombine(minecraftRoot(), "saves");
+}
+
+QString LegacyInstance::loaderModsDir() const
+{
+ return PathCombine(minecraftRoot(), "mods");
+}
+
+QString LegacyInstance::coreModsDir() const
+{
+ return PathCombine(minecraftRoot(), "coremods");
+}
+
+QString LegacyInstance::resourceDir() const
+{
+ return PathCombine(minecraftRoot(), "resources");
+}
+QString LegacyInstance::texturePacksDir() const
+{
+ return PathCombine(minecraftRoot(), "texturepacks");
+}
+
+QString LegacyInstance::runnableJar() const
+{
+ return PathCombine(binDir(), "minecraft.jar");
+}
+
+QString LegacyInstance::modListFile() const
+{
+ return PathCombine(instanceRoot(), "modlist");
+}
+
+QString LegacyInstance::instanceConfigFolder() const
+{
+ return PathCombine(minecraftRoot(), "config");
+}
+
+bool LegacyInstance::shouldRebuild() const
+{
+ I_D(LegacyInstance);
+ return d->m_settings->get("NeedsRebuild").toBool();
+}
+
+void LegacyInstance::setShouldRebuild(bool val)
+{
+ I_D(LegacyInstance);
+ d->m_settings->set("NeedsRebuild", val);
+}
+
+QString LegacyInstance::currentVersionId() const
+{
+ I_D(LegacyInstance);
+ return d->m_settings->get("JarVersion").toString();
+}
+
+QString LegacyInstance::lwjglVersion() const
+{
+ I_D(LegacyInstance);
+ return d->m_settings->get("LwjglVersion").toString();
+}
+
+void LegacyInstance::setLWJGLVersion(QString val)
+{
+ I_D(LegacyInstance);
+ d->m_settings->set("LwjglVersion", val);
+}
+
+QString LegacyInstance::intendedVersionId() const
+{
+ I_D(LegacyInstance);
+ return d->m_settings->get("IntendedJarVersion").toString();
+}
+
+bool LegacyInstance::setIntendedVersionId(QString version)
+{
+ settings().set("IntendedJarVersion", version);
+ setShouldUpdate(true);
+ return true;
+}
+
+bool LegacyInstance::shouldUpdate() const
+{
+ QVariant var = settings().get("ShouldUpdate");
+ if (!var.isValid() || var.toBool() == false)
+ {
+ return intendedVersionId() != currentVersionId();
+ }
+ return true;
+}
+
+void LegacyInstance::setShouldUpdate(bool val)
+{
+ settings().set("ShouldUpdate", val);
+}
+
+QString LegacyInstance::defaultBaseJar() const
+{
+ return "versions/" + intendedVersionId() + "/" + intendedVersionId() + ".jar";
+}
+
+QString LegacyInstance::defaultCustomBaseJar() const
+{
+ return PathCombine(binDir(), "mcbackup.jar");
+}
+
+bool LegacyInstance::menuActionEnabled(QString action_name) const
+{
+ if (action_name == "actionChangeInstMCVersion")
+ return false;
+ return true;
+}
+
+QString LegacyInstance::getStatusbarDescription()
+{
+ if (shouldUpdate())
+ return "Legacy : " + currentVersionId() + " -> " + intendedVersionId();
+ else
+ return "Legacy : " + currentVersionId();
+}
diff --git a/logic/LegacyInstance.h b/logic/LegacyInstance.h
new file mode 100644
index 00000000..636addeb
--- /dev/null
+++ b/logic/LegacyInstance.h
@@ -0,0 +1,94 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "BaseInstance.h"
+
+class ModList;
+class Task;
+
+class LegacyInstance : public BaseInstance
+{
+ Q_OBJECT
+public:
+
+ explicit LegacyInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+
+ /// Path to the instance's minecraft.jar
+ QString runnableJar() const;
+
+ //! Path to the instance's modlist file.
+ QString modListFile() const;
+
+ ////// Mod Lists //////
+ std::shared_ptr<ModList> jarModList();
+ std::shared_ptr<ModList> coreModList();
+ std::shared_ptr<ModList> loaderModList();
+ std::shared_ptr<ModList> texturePackList();
+
+ ////// Directories //////
+ QString savesDir() const;
+ QString texturePacksDir() const;
+ QString jarModsDir() const;
+ QString binDir() const;
+ QString loaderModsDir() const;
+ QString coreModsDir() const;
+ QString resourceDir() const;
+ virtual QString instanceConfigFolder() const override;
+
+ /*!
+ * Whether or not the instance's minecraft.jar needs to be rebuilt.
+ * If this is true, when the instance launches, its jar mods will be
+ * re-added to a fresh minecraft.jar file.
+ */
+ bool shouldRebuild() const;
+ void setShouldRebuild(bool val);
+
+ virtual QString currentVersionId() const override;
+
+ //! The version of LWJGL that this instance uses.
+ QString lwjglVersion() const;
+ /// st the version of LWJGL libs this instance will use
+ void setLWJGLVersion(QString val);
+
+ virtual QString intendedVersionId() const override;
+ virtual bool setIntendedVersionId(QString version) override;
+ // the `version' of Legacy instances is defined by the launcher code.
+ // in contrast with OneSix, where `version' is described in a json file
+ virtual bool versionIsCustom() override
+ {
+ return false;
+ }
+
+ virtual bool shouldUpdate() const override;
+ virtual void setShouldUpdate(bool val) override;
+ virtual std::shared_ptr<Task> doUpdate() override;
+
+ virtual MinecraftProcess *prepareForLaunch(AuthSessionPtr account) override;
+ virtual void cleanupAfterRun() override;
+ virtual QDialog *createModEditDialog(QWidget *parent) override;
+
+ virtual QString defaultBaseJar() const override;
+ virtual QString defaultCustomBaseJar() const override;
+
+ bool menuActionEnabled(QString action_name) const;
+ virtual QString getStatusbarDescription() override;
+
+protected
+slots:
+ virtual void jarModsChanged();
+};
diff --git a/logic/LegacyInstance_p.h b/logic/LegacyInstance_p.h
new file mode 100644
index 00000000..ed97ccd3
--- /dev/null
+++ b/logic/LegacyInstance_p.h
@@ -0,0 +1,30 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <settingsobject.h>
+#include <memory>
+
+#include "BaseInstance_p.h"
+#include "ModList.h"
+
+struct LegacyInstancePrivate : public BaseInstancePrivate
+{
+ std::shared_ptr<ModList> jar_mod_list;
+ std::shared_ptr<ModList> core_mod_list;
+ std::shared_ptr<ModList> loader_mod_list;
+ std::shared_ptr<ModList> texture_pack_list;
+};
diff --git a/logic/LegacyUpdate.cpp b/logic/LegacyUpdate.cpp
new file mode 100644
index 00000000..5d82a76b
--- /dev/null
+++ b/logic/LegacyUpdate.cpp
@@ -0,0 +1,495 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LegacyUpdate.h"
+#include "lists/LwjglVersionList.h"
+#include "lists/MinecraftVersionList.h"
+#include "BaseInstance.h"
+#include "LegacyInstance.h"
+#include "MultiMC.h"
+#include "ModList.h"
+#include <pathutils.h>
+#include <quazip.h>
+#include <quazipfile.h>
+#include <JlCompress.h>
+#include "logger/QsLog.h"
+#include "logic/net/URLConstants.h"
+
+LegacyUpdate::LegacyUpdate(BaseInstance *inst, QObject *parent) : Task(parent), m_inst(inst)
+{
+}
+
+void LegacyUpdate::executeTask()
+{
+ /*
+ if(m_only_prepare)
+ {
+ // FIXME: think this through some more.
+ LegacyInstance *inst = (LegacyInstance *)m_inst;
+ if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
+ {
+ ModTheJar();
+ }
+ else
+ {
+ emitSucceeded();
+ }
+ }
+ else
+ {
+ */
+ lwjglStart();
+ //}
+}
+
+void LegacyUpdate::lwjglStart()
+{
+ LegacyInstance *inst = (LegacyInstance *)m_inst;
+
+ lwjglVersion = inst->lwjglVersion();
+ lwjglTargetPath = PathCombine(MMC->settings()->get("LWJGLDir").toString(), lwjglVersion);
+ lwjglNativesPath = PathCombine(lwjglTargetPath, "natives");
+
+ // if the 'done' file exists, we don't have to download this again
+ QFileInfo doneFile(PathCombine(lwjglTargetPath, "done"));
+ if (doneFile.exists())
+ {
+ jarStart();
+ return;
+ }
+
+ auto list = MMC->lwjgllist();
+ if (!list->isLoaded())
+ {
+ emitFailed("Too soon! Let the LWJGL list load :)");
+ return;
+ }
+
+ setStatus(tr("Downloading new LWJGL..."));
+ auto version = list->getVersion(lwjglVersion);
+ if (!version)
+ {
+ emitFailed("Game update failed: the selected LWJGL version is invalid.");
+ return;
+ }
+
+ QString url = version->url();
+ QUrl realUrl(url);
+ QString hostname = realUrl.host();
+ auto worker = MMC->qnam();
+ QNetworkRequest req(realUrl);
+ req.setRawHeader("Host", hostname.toLatin1());
+ req.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
+ QNetworkReply *rep = worker->get(req);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
+ connect(worker.get(), SIGNAL(finished(QNetworkReply *)),
+ SLOT(lwjglFinished(QNetworkReply *)));
+ // connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ // SLOT(downloadError(QNetworkReply::NetworkError)));
+}
+
+void LegacyUpdate::lwjglFinished(QNetworkReply *reply)
+{
+ if (m_reply.get() != reply)
+ {
+ return;
+ }
+ if (reply->error() != QNetworkReply::NoError)
+ {
+ emitFailed("Failed to download: " + reply->errorString() +
+ "\nSometimes you have to wait a bit if you download many LWJGL versions in "
+ "a row. YMMV");
+ return;
+ }
+ auto worker = MMC->qnam();
+ // Here i check if there is a cookie for me in the reply and extract it
+ QList<QNetworkCookie> cookies =
+ qvariant_cast<QList<QNetworkCookie>>(reply->header(QNetworkRequest::SetCookieHeader));
+ if (cookies.count() != 0)
+ {
+ // you must tell which cookie goes with which url
+ worker->cookieJar()->setCookiesFromUrl(cookies, QUrl("sourceforge.net"));
+ }
+
+ // here you can check for the 302 or whatever other header i need
+ QVariant newLoc = reply->header(QNetworkRequest::LocationHeader);
+ if (newLoc.isValid())
+ {
+ QString redirectedTo = reply->header(QNetworkRequest::LocationHeader).toString();
+ QUrl realUrl(redirectedTo);
+ QString hostname = realUrl.host();
+ QNetworkRequest req(redirectedTo);
+ req.setRawHeader("Host", hostname.toLatin1());
+ req.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
+ QNetworkReply *rep = worker->get(req);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SIGNAL(progress(qint64, qint64)));
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ return;
+ }
+ QFile saveMe("lwjgl.zip");
+ saveMe.open(QIODevice::WriteOnly);
+ saveMe.write(m_reply->readAll());
+ saveMe.close();
+ setStatus(tr("Installing new LWJGL..."));
+ extractLwjgl();
+ jarStart();
+}
+void LegacyUpdate::extractLwjgl()
+{
+ // make sure the directories are there
+
+ bool success = ensureFolderPathExists(lwjglNativesPath);
+
+ if (!success)
+ {
+ emitFailed("Failed to extract the lwjgl libs - error when creating required folders.");
+ return;
+ }
+
+ QuaZip zip("lwjgl.zip");
+ if (!zip.open(QuaZip::mdUnzip))
+ {
+ emitFailed("Failed to extract the lwjgl libs - not a valid archive.");
+ return;
+ }
+
+ // and now we are going to access files inside it
+ QuaZipFile file(&zip);
+ const QString jarNames[] = {"jinput.jar", "lwjgl_util.jar", "lwjgl.jar"};
+ for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
+ {
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ zip.close();
+ emitFailed("Failed to extract the lwjgl libs - error while reading archive.");
+ return;
+ }
+ QuaZipFileInfo info;
+ QString name = file.getActualFileName();
+ if (name.endsWith('/'))
+ {
+ file.close();
+ continue;
+ }
+ QString destFileName;
+ // Look for the jars
+ for (int i = 0; i < 3; i++)
+ {
+ if (name.endsWith(jarNames[i]))
+ {
+ destFileName = PathCombine(lwjglTargetPath, jarNames[i]);
+ }
+ }
+ // Not found? look for the natives
+ if (destFileName.isEmpty())
+ {
+#ifdef Q_OS_WIN32
+ QString nativesDir = "windows";
+#else
+#ifdef Q_OS_MAC
+ QString nativesDir = "macosx";
+#else
+ QString nativesDir = "linux";
+#endif
+#endif
+ if (name.contains(nativesDir))
+ {
+ int lastSlash = name.lastIndexOf('/');
+ int lastBackSlash = name.lastIndexOf('\\');
+ if (lastSlash != -1)
+ name = name.mid(lastSlash + 1);
+ else if (lastBackSlash != -1)
+ name = name.mid(lastBackSlash + 1);
+ destFileName = PathCombine(lwjglNativesPath, name);
+ }
+ }
+ // Now if destFileName is still empty, go to the next file.
+ if (!destFileName.isEmpty())
+ {
+ setStatus(tr("Installing new LWJGL - extracting ") + name + "...");
+ QFile output(destFileName);
+ output.open(QIODevice::WriteOnly);
+ output.write(file.readAll()); // FIXME: wste of memory!?
+ output.close();
+ }
+ file.close(); // do not forget to close!
+ }
+ zip.close();
+ m_reply.reset();
+ QFile doneFile(PathCombine(lwjglTargetPath, "done"));
+ doneFile.open(QIODevice::WriteOnly);
+ doneFile.write("done.");
+ doneFile.close();
+}
+
+void LegacyUpdate::lwjglFailed()
+{
+ emitFailed("Bad stuff happened while trying to get the lwjgl libs...");
+}
+
+void LegacyUpdate::jarStart()
+{
+ LegacyInstance *inst = (LegacyInstance *)m_inst;
+ if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar())
+ {
+ ModTheJar();
+ return;
+ }
+
+ setStatus(tr("Checking for jar updates..."));
+ // Make directories
+ QDir binDir(inst->binDir());
+ if (!binDir.exists() && !binDir.mkpath("."))
+ {
+ emitFailed("Failed to create bin folder.");
+ return;
+ }
+
+ // Build a list of URLs that will need to be downloaded.
+ setStatus(tr("Downloading new minecraft.jar ..."));
+
+ QString version_id = inst->intendedVersionId();
+ QString localPath = version_id + "/" + version_id + ".jar";
+ QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + localPath;
+
+ auto dljob = new NetJob("Minecraft.jar for version " + version_id);
+
+ auto metacache = MMC->metacache();
+ auto entry = metacache->resolveEntry("versions", localPath);
+ dljob->addNetAction(CacheDownload::make(QUrl(urlstr), entry));
+ connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished()));
+ connect(dljob, SIGNAL(failed()), SLOT(jarFailed()));
+ connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
+ legacyDownloadJob.reset(dljob);
+ legacyDownloadJob->start();
+}
+
+void LegacyUpdate::jarFinished()
+{
+ // process the jar
+ ModTheJar();
+}
+
+void LegacyUpdate::jarFailed()
+{
+ // bad, bad
+ emitFailed("Failed to download the minecraft jar. Try again later.");
+}
+
+bool LegacyUpdate::MergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
+ MetainfAction metainf)
+{
+ setStatus(tr("Installing mods: Adding ") + from.fileName() + " ...");
+
+ QuaZip modZip(from.filePath());
+ modZip.open(QuaZip::mdUnzip);
+
+ QuaZipFile fileInsideMod(&modZip);
+ QuaZipFile zipOutFile(into);
+ for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile())
+ {
+ QString filename = modZip.getCurrentFileName();
+ if (filename.contains("META-INF") && metainf == LegacyUpdate::IgnoreMetainf)
+ {
+ QLOG_INFO() << "Skipping META-INF " << filename << " from " << from.fileName();
+ continue;
+ }
+ if (contained.contains(filename))
+ {
+ QLOG_INFO() << "Skipping already contained file " << filename << " from "
+ << from.fileName();
+ continue;
+ }
+ contained.insert(filename);
+ QLOG_INFO() << "Adding file " << filename << " from " << from.fileName();
+
+ if (!fileInsideMod.open(QIODevice::ReadOnly))
+ {
+ QLOG_ERROR() << "Failed to open " << filename << " from " << from.fileName();
+ return false;
+ }
+ /*
+ QuaZipFileInfo old_info;
+ fileInsideMod.getFileInfo(&old_info);
+ */
+ QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
+ /*
+ info_out.externalAttr = old_info.externalAttr;
+ */
+ if (!zipOutFile.open(QIODevice::WriteOnly, info_out))
+ {
+ QLOG_ERROR() << "Failed to open " << filename << " in the jar";
+ fileInsideMod.close();
+ return false;
+ }
+ if (!JlCompress::copyData(fileInsideMod, zipOutFile))
+ {
+ zipOutFile.close();
+ fileInsideMod.close();
+ QLOG_ERROR() << "Failed to copy data of " << filename << " into the jar";
+ return false;
+ }
+ zipOutFile.close();
+ fileInsideMod.close();
+ }
+ return true;
+}
+
+void LegacyUpdate::ModTheJar()
+{
+ LegacyInstance *inst = (LegacyInstance *)m_inst;
+
+ if (!inst->shouldRebuild())
+ {
+ emitSucceeded();
+ return;
+ }
+
+ // Get the mod list
+ auto modList = inst->jarModList();
+
+ QFileInfo runnableJar(inst->runnableJar());
+ QFileInfo baseJar(inst->baseJar());
+ bool base_is_custom = inst->shouldUseCustomBaseJar();
+
+ // Nothing to do if there are no jar mods to install, no backup and just the mc jar
+ if (base_is_custom)
+ {
+ // yes, this can happen if the instance only has the runnable jar and not the base jar
+ // it *could* be assumed that such an instance is vanilla, but that wouldn't be safe
+ // because that's not something mmc4 guarantees
+ if (runnableJar.isFile() && !baseJar.exists() && modList->empty())
+ {
+ inst->setShouldRebuild(false);
+ emitSucceeded();
+ return;
+ }
+
+ setStatus(tr("Installing mods: Backing up minecraft.jar ..."));
+ if (!baseJar.exists() && !QFile::copy(runnableJar.filePath(), baseJar.filePath()))
+ {
+ emitFailed("It seems both the active and base jar are gone. A fresh base jar will "
+ "be used on next run.");
+ inst->setShouldRebuild(true);
+ inst->setShouldUpdate(true);
+ inst->setShouldUseCustomBaseJar(false);
+ return;
+ }
+ }
+
+ if (!baseJar.exists())
+ {
+ emitFailed("The base jar " + baseJar.filePath() + " does not exist");
+ return;
+ }
+
+ if (runnableJar.exists() && !QFile::remove(runnableJar.filePath()))
+ {
+ emitFailed("Failed to delete old minecraft.jar");
+ return;
+ }
+
+ // TaskStep(); // STEP 1
+ setStatus(tr("Installing mods: Opening minecraft.jar ..."));
+
+ QuaZip zipOut(runnableJar.filePath());
+ if (!zipOut.open(QuaZip::mdCreate))
+ {
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to open the minecraft.jar for modding");
+ return;
+ }
+ // Files already added to the jar.
+ // These files will be skipped.
+ QSet<QString> addedFiles;
+
+ // Modify the jar
+ setStatus(tr("Installing mods: Adding mod files..."));
+ for (int i = modList->size() - 1; i >= 0; i--)
+ {
+ auto &mod = modList->operator[](i);
+
+ // do not merge disabled mods.
+ if (!mod.enabled())
+ continue;
+
+ if (mod.type() == Mod::MOD_ZIPFILE)
+ {
+ if (!MergeZipFiles(&zipOut, mod.filename(), addedFiles, LegacyUpdate::KeepMetainf))
+ {
+ zipOut.close();
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to add " + mod.filename().fileName() + " to the jar.");
+ return;
+ }
+ }
+ else if (mod.type() == Mod::MOD_SINGLEFILE)
+ {
+ auto filename = mod.filename();
+ if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(),
+ filename.fileName()))
+ {
+ zipOut.close();
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to add " + filename.fileName() + " to the jar");
+ return;
+ }
+ addedFiles.insert(filename.fileName());
+ QLOG_INFO() << "Adding file " << filename.fileName() << " from "
+ << filename.absoluteFilePath();
+ }
+ else if (mod.type() == Mod::MOD_FOLDER)
+ {
+ auto filename = mod.filename();
+ QString what_to_zip = filename.absoluteFilePath();
+ QDir dir(what_to_zip);
+ dir.cdUp();
+ QString parent_dir = dir.absolutePath();
+ if (!JlCompress::compressSubDir(&zipOut, what_to_zip, parent_dir, true, addedFiles))
+ {
+ zipOut.close();
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to add " + filename.fileName() + " to the jar");
+ return;
+ }
+ QLOG_INFO() << "Adding folder " << filename.fileName() << " from "
+ << filename.absoluteFilePath();
+ }
+ }
+
+ if (!MergeZipFiles(&zipOut, baseJar, addedFiles, LegacyUpdate::IgnoreMetainf))
+ {
+ zipOut.close();
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to insert minecraft.jar contents.");
+ return;
+ }
+
+ // Recompress the jar
+ zipOut.close();
+ if (zipOut.getZipError() != 0)
+ {
+ QFile::remove(runnableJar.filePath());
+ emitFailed("Failed to finalize minecraft.jar!");
+ return;
+ }
+ inst->setShouldRebuild(false);
+ // inst->UpdateVersion(true);
+ emitSucceeded();
+ return;
+}
diff --git a/logic/LegacyUpdate.h b/logic/LegacyUpdate.h
new file mode 100644
index 00000000..613eb1f9
--- /dev/null
+++ b/logic/LegacyUpdate.h
@@ -0,0 +1,75 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QList>
+#include <QUrl>
+
+#include "logic/net/NetJob.h"
+#include "logic/tasks/Task.h"
+
+class MinecraftVersion;
+class BaseInstance;
+class QuaZip;
+class Mod;
+
+class LegacyUpdate : public Task
+{
+ Q_OBJECT
+public:
+ explicit LegacyUpdate(BaseInstance *inst, QObject *parent = 0);
+ virtual void executeTask();
+
+private
+slots:
+ void lwjglStart();
+ void lwjglFinished(QNetworkReply *);
+ void lwjglFailed();
+
+ void jarStart();
+ void jarFinished();
+ void jarFailed();
+
+ void extractLwjgl();
+
+ void ModTheJar();
+
+private:
+ enum MetainfAction
+ {
+ KeepMetainf, // the META-INF folder will be added from the merged jar
+ IgnoreMetainf // the META-INF from the merged jar will be ignored
+ };
+ bool MergeZipFiles(QuaZip *into, QFileInfo from, QSet<QString> &contained,
+ MetainfAction metainf);
+
+private:
+
+ std::shared_ptr<QNetworkReply> m_reply;
+
+ // target version, determined during this task
+ // MinecraftVersion *targetVersion;
+ QString lwjglURL;
+ QString lwjglVersion;
+
+ QString lwjglTargetPath;
+ QString lwjglNativesPath;
+
+private:
+ NetJobPtr legacyDownloadJob;
+ BaseInstance *m_inst = nullptr;
+};
diff --git a/logic/LiteLoaderInstaller.cpp b/logic/LiteLoaderInstaller.cpp
new file mode 100644
index 00000000..07fffff3
--- /dev/null
+++ b/logic/LiteLoaderInstaller.cpp
@@ -0,0 +1,102 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LiteLoaderInstaller.h"
+
+#include "OneSixVersion.h"
+#include "OneSixLibrary.h"
+
+QMap<QString, QString> LiteLoaderInstaller::m_launcherWrapperVersionMapping;
+
+LiteLoaderInstaller::LiteLoaderInstaller(const QString &mcVersion) : m_mcVersion(mcVersion)
+{
+ if (m_launcherWrapperVersionMapping.isEmpty())
+ {
+ m_launcherWrapperVersionMapping["1.6.2"] = "1.3";
+ m_launcherWrapperVersionMapping["1.6.4"] = "1.8";
+ //m_launcherWrapperVersionMapping["1.7.2"] = "1.8";
+ //m_launcherWrapperVersionMapping["1.7.4"] = "1.8";
+ }
+}
+
+bool LiteLoaderInstaller::canApply() const
+{
+ return m_launcherWrapperVersionMapping.contains(m_mcVersion);
+}
+
+bool LiteLoaderInstaller::apply(std::shared_ptr<OneSixVersion> to)
+{
+ to->externalUpdateStart();
+
+ applyLaunchwrapper(to);
+ applyLiteLoader(to);
+
+ to->mainClass = "net.minecraft.launchwrapper.Launch";
+ if (!to->minecraftArguments.contains(
+ " --tweakClass com.mumfrey.liteloader.launch.LiteLoaderTweaker"))
+ {
+ to->minecraftArguments.append(
+ " --tweakClass com.mumfrey.liteloader.launch.LiteLoaderTweaker");
+ }
+
+ to->externalUpdateFinish();
+ return to->toOriginalFile();
+}
+
+void LiteLoaderInstaller::applyLaunchwrapper(std::shared_ptr<OneSixVersion> to)
+{
+ const QString intendedVersion = m_launcherWrapperVersionMapping[m_mcVersion];
+
+ QMutableListIterator<std::shared_ptr<OneSixLibrary>> it(to->libraries);
+ while (it.hasNext())
+ {
+ it.next();
+ if (it.value()->rawName().startsWith("net.minecraft:launchwrapper:"))
+ {
+ if (it.value()->version() >= intendedVersion)
+ {
+ return;
+ }
+ else
+ {
+ it.remove();
+ }
+ }
+ }
+
+ std::shared_ptr<OneSixLibrary> lib(new OneSixLibrary(
+ "net.minecraft:launchwrapper:" + m_launcherWrapperVersionMapping[m_mcVersion]));
+ lib->finalize();
+ to->libraries.prepend(lib);
+}
+
+void LiteLoaderInstaller::applyLiteLoader(std::shared_ptr<OneSixVersion> to)
+{
+ QMutableListIterator<std::shared_ptr<OneSixLibrary>> it(to->libraries);
+ while (it.hasNext())
+ {
+ it.next();
+ if (it.value()->rawName().startsWith("com.mumfrey:liteloader:"))
+ {
+ it.remove();
+ }
+ }
+
+ std::shared_ptr<OneSixLibrary> lib(
+ new OneSixLibrary("com.mumfrey:liteloader:" + m_mcVersion));
+ lib->setBaseUrl("http://dl.liteloader.com/versions/");
+ lib->finalize();
+ to->libraries.prepend(lib);
+}
diff --git a/logic/LiteLoaderInstaller.h b/logic/LiteLoaderInstaller.h
new file mode 100644
index 00000000..44b306d6
--- /dev/null
+++ b/logic/LiteLoaderInstaller.h
@@ -0,0 +1,39 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <QMap>
+#include <memory>
+
+class OneSixVersion;
+
+class LiteLoaderInstaller
+{
+public:
+ LiteLoaderInstaller(const QString &mcVersion);
+
+ bool canApply() const;
+
+ bool apply(std::shared_ptr<OneSixVersion> to);
+
+private:
+ QString m_mcVersion;
+
+ void applyLaunchwrapper(std::shared_ptr<OneSixVersion> to);
+ void applyLiteLoader(std::shared_ptr<OneSixVersion> to);
+
+ static QMap<QString, QString> m_launcherWrapperVersionMapping;
+};
diff --git a/logic/MinecraftProcess.cpp b/logic/MinecraftProcess.cpp
new file mode 100644
index 00000000..9c0a7074
--- /dev/null
+++ b/logic/MinecraftProcess.cpp
@@ -0,0 +1,374 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "MultiMC.h"
+
+#include "MinecraftProcess.h"
+
+#include <QDataStream>
+#include <QFile>
+#include <QDir>
+#include <QProcessEnvironment>
+#include <QRegularExpression>
+
+#include "BaseInstance.h"
+
+#include "osutils.h"
+#include "pathutils.h"
+#include "cmdutils.h"
+
+#define IBUS "@im=ibus"
+
+// constructor
+MinecraftProcess::MinecraftProcess(BaseInstance *inst) : m_instance(inst)
+{
+ connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
+ SLOT(finish(int, QProcess::ExitStatus)));
+
+ // prepare the process environment
+ QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
+
+#ifdef LINUX
+ // Strip IBus
+ // IBus is a Linux IME framework. For some reason, it breaks MC?
+ if (env.value("XMODIFIERS").contains(IBUS))
+ env.insert("XMODIFIERS", env.value("XMODIFIERS").replace(IBUS, ""));
+#endif
+
+ // export some infos
+ env.insert("INST_NAME", inst->name());
+ env.insert("INST_ID", inst->id());
+ env.insert("INST_DIR", QDir(inst->instanceRoot()).absolutePath());
+
+ this->setProcessEnvironment(env);
+ m_prepostlaunchprocess.setProcessEnvironment(env);
+
+ // std channels
+ connect(this, SIGNAL(readyReadStandardError()), SLOT(on_stdErr()));
+ connect(this, SIGNAL(readyReadStandardOutput()), SLOT(on_stdOut()));
+
+ // Log prepost launch command output (can be disabled.)
+ if (m_instance->settings().get("LogPrePostOutput").toBool())
+ {
+ connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardError,
+ this, &MinecraftProcess::on_prepost_stdErr);
+ connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardOutput,
+ this, &MinecraftProcess::on_prepost_stdOut);
+ }
+}
+
+void MinecraftProcess::setWorkdir(QString path)
+{
+ QDir mcDir(path);
+ this->setWorkingDirectory(mcDir.absolutePath());
+ m_prepostlaunchprocess.setWorkingDirectory(mcDir.absolutePath());
+}
+
+QString MinecraftProcess::censorPrivateInfo(QString in)
+{
+ if(!m_session)
+ return in;
+
+ if(m_session->session != "-")
+ in.replace(m_session->session, "<SESSION ID>");
+ in.replace(m_session->access_token, "<ACCESS TOKEN>");
+ in.replace(m_session->client_token, "<CLIENT TOKEN>");
+ in.replace(m_session->uuid, "<PROFILE ID>");
+ in.replace(m_session->player_name, "<PROFILE NAME>");
+
+ auto i = m_session->u.properties.begin();
+ while (i != m_session->u.properties.end())
+ {
+ in.replace(i.value(), "<" + i.key().toUpper() + ">");
+ ++i;
+ }
+
+ return in;
+}
+
+// console window
+MessageLevel::Enum MinecraftProcess::guessLevel(const QString &line, MessageLevel::Enum level)
+{
+ if (line.contains("[INFO]") || line.contains("[CONFIG]") || line.contains("[FINE]") ||
+ line.contains("[FINER]") || line.contains("[FINEST]"))
+ level = MessageLevel::Message;
+ if (line.contains("[SEVERE]") || line.contains("[STDERR]"))
+ level = MessageLevel::Error;
+ if (line.contains("[WARNING]"))
+ level = MessageLevel::Warning;
+ if (line.contains("Exception in thread") || line.contains(" at "))
+ level = MessageLevel::Fatal;
+ if (line.contains("[DEBUG]"))
+ level = MessageLevel::Debug;
+ return level;
+}
+
+MessageLevel::Enum MinecraftProcess::getLevel(const QString &levelName)
+{
+ if (levelName == "MultiMC")
+ return MessageLevel::MultiMC;
+ else if (levelName == "Debug")
+ return MessageLevel::Debug;
+ else if (levelName == "Info")
+ return MessageLevel::Info;
+ else if (levelName == "Message")
+ return MessageLevel::Message;
+ else if (levelName == "Warning")
+ return MessageLevel::Warning;
+ else if (levelName == "Error")
+ return MessageLevel::Error;
+ else if (levelName == "Fatal")
+ return MessageLevel::Fatal;
+ // Skip PrePost, it's not exposed to !![]!
+ else
+ return MessageLevel::Message;
+}
+
+void MinecraftProcess::logOutput(const QStringList &lines,
+ MessageLevel::Enum defaultLevel,
+ bool guessLevel, bool censor)
+{
+ for (int i = 0; i < lines.size(); ++i)
+ logOutput(lines[i], defaultLevel, guessLevel, censor);
+}
+
+void MinecraftProcess::logOutput(QString line,
+ MessageLevel::Enum defaultLevel,
+ bool guessLevel, bool censor)
+{
+ MessageLevel::Enum level = defaultLevel;
+
+ // Level prefix
+ int endmark = line.indexOf("]!");
+ if (line.startsWith("!![") && endmark != -1)
+ {
+ level = getLevel(line.left(endmark).mid(3));
+ line = line.mid(endmark + 2);
+ }
+ // Guess level
+ else if (guessLevel)
+ level = this->guessLevel(line, defaultLevel);
+
+ if (censor)
+ line = censorPrivateInfo(line);
+
+ emit log(line, level);
+}
+
+void MinecraftProcess::on_stdErr()
+{
+ QByteArray data = readAllStandardError();
+ QString str = m_err_leftover + QString::fromLocal8Bit(data);
+
+ QStringList lines = str.split("\n");
+ m_err_leftover = lines.takeLast();
+
+ logOutput(lines, MessageLevel::Error);
+}
+
+void MinecraftProcess::on_stdOut()
+{
+ QByteArray data = readAllStandardOutput();
+ QString str = m_out_leftover + QString::fromLocal8Bit(data);
+
+ QStringList lines = str.split("\n");
+ m_out_leftover = lines.takeLast();
+
+ logOutput(lines);
+}
+
+void MinecraftProcess::on_prepost_stdErr()
+{
+ QByteArray data = m_prepostlaunchprocess.readAllStandardError();
+ QString str = m_err_leftover + QString::fromLocal8Bit(data);
+
+ QStringList lines = str.split("\n");
+ m_err_leftover = lines.takeLast();
+
+ logOutput(lines, MessageLevel::PrePost, false, false);
+}
+
+void MinecraftProcess::on_prepost_stdOut()
+{
+ QByteArray data = m_prepostlaunchprocess.readAllStandardOutput();
+ QString str = m_out_leftover + QString::fromLocal8Bit(data);
+
+ QStringList lines = str.split("\n");
+ m_out_leftover = lines.takeLast();
+
+ logOutput(lines, MessageLevel::PrePost, false, false);
+}
+
+// exit handler
+void MinecraftProcess::finish(int code, ExitStatus status)
+{
+ // Flush console window
+ if (!m_err_leftover.isEmpty())
+ {
+ logOutput(m_err_leftover, MessageLevel::Error);
+ m_err_leftover.clear();
+ }
+ if (!m_out_leftover.isEmpty())
+ {
+ logOutput(m_out_leftover);
+ m_out_leftover.clear();
+ }
+
+ if (!killed)
+ {
+ if (status == NormalExit)
+ {
+ //: Message displayed on instance exit
+ emit log(tr("Minecraft exited with exitcode %1.").arg(code));
+ }
+ else
+ {
+ //: Message displayed on instance crashed
+ emit log(tr("Minecraft crashed with exitcode %1.").arg(code));
+ }
+ }
+ else
+ {
+ //: Message displayed after the instance exits due to kill request
+ emit log(tr("Minecraft was killed by user."), MessageLevel::Error);
+ }
+
+ m_prepostlaunchprocess.processEnvironment().insert("INST_EXITCODE", QString(code));
+
+ // run post-exit
+ QString postlaunch_cmd = m_instance->settings().get("PostExitCommand").toString();
+ if (!postlaunch_cmd.isEmpty())
+ {
+ emit log(tr("Running Post-Launch command: %1").arg(postlaunch_cmd));
+ m_prepostlaunchprocess.start(postlaunch_cmd);
+ m_prepostlaunchprocess.waitForFinished();
+ // Flush console window
+ if (!m_err_leftover.isEmpty())
+ {
+ logOutput(m_err_leftover, MessageLevel::PrePost);
+ m_err_leftover.clear();
+ }
+ if (!m_out_leftover.isEmpty())
+ {
+ logOutput(m_out_leftover, MessageLevel::PrePost);
+ m_out_leftover.clear();
+ }
+ if (m_prepostlaunchprocess.exitStatus() != NormalExit)
+ {
+ emit log(tr("Post-Launch command failed with code %1.\n\n").arg(m_prepostlaunchprocess.exitCode()),
+ MessageLevel::Error);
+ emit postlaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(),
+ m_prepostlaunchprocess.exitStatus());
+ }
+ else
+ emit log(tr("Post-Launch command ran successfully.\n\n"));
+ }
+ m_instance->cleanupAfterRun();
+ emit ended(m_instance, code, status);
+}
+
+void MinecraftProcess::killMinecraft()
+{
+ killed = true;
+ kill();
+}
+
+void MinecraftProcess::launch()
+{
+ emit log("MultiMC version: " + MMC->version().toString() + "\n\n");
+ emit log("Minecraft folder is:\n" + workingDirectory() + "\n\n");
+
+ QString prelaunch_cmd = m_instance->settings().get("PreLaunchCommand").toString();
+ if (!prelaunch_cmd.isEmpty())
+ {
+ // Launch
+ emit log(tr("Running Pre-Launch command: %1").arg(prelaunch_cmd));
+ m_prepostlaunchprocess.start(prelaunch_cmd);
+ // Wait
+ m_prepostlaunchprocess.waitForFinished();
+ // Flush console window
+ if (!m_err_leftover.isEmpty())
+ {
+ logOutput(m_err_leftover, MessageLevel::PrePost);
+ m_err_leftover.clear();
+ }
+ if (!m_out_leftover.isEmpty())
+ {
+ logOutput(m_out_leftover, MessageLevel::PrePost);
+ m_out_leftover.clear();
+ }
+ // Process return values
+ if (m_prepostlaunchprocess.exitStatus() != NormalExit)
+ {
+ emit log(tr("Pre-Launch command failed with code %1.\n\n").arg(m_prepostlaunchprocess.exitCode()),
+ MessageLevel::Fatal);
+ m_instance->cleanupAfterRun();
+ emit prelaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(),
+ m_prepostlaunchprocess.exitStatus());
+ return;
+ }
+ else
+ emit log(tr("Pre-Launch command ran successfully.\n\n"));
+ }
+
+ m_instance->setLastLaunch();
+ auto &settings = m_instance->settings();
+
+ //////////// java arguments ////////////
+ QStringList args;
+ {
+ // custom args go first. we want to override them if we have our own here.
+ args.append(m_instance->extraArguments());
+
+ // OSX dock icon and name
+ #ifdef OSX
+ args << "-Xdock:icon=icon.png";
+ args << QString("-Xdock:name=\"%1\"").arg(m_instance->windowTitle());
+ #endif
+
+ // HACK: Stupid hack for Intel drivers. See: https://mojang.atlassian.net/browse/MCL-767
+ #ifdef Q_OS_WIN32
+ args << QString("-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_"
+ "minecraft.exe.heapdump");
+ #endif
+
+ args << QString("-Xms%1m").arg(settings.get("MinMemAlloc").toInt());
+ args << QString("-Xmx%1m").arg(settings.get("MaxMemAlloc").toInt());
+ args << QString("-XX:PermSize=%1m").arg(settings.get("PermGen").toInt());
+ if(!m_nativeFolder.isEmpty())
+ args << QString("-Djava.library.path=%1").arg(m_nativeFolder);
+ args << "-jar" << PathCombine(MMC->bin(), "jars", "NewLaunch.jar");
+ }
+
+ QString JavaPath = m_instance->settings().get("JavaPath").toString();
+ emit log("Java path is:\n" + JavaPath + "\n\n");
+ QString allArgs = args.join(", ");
+ emit log("Java Arguments:\n[" + censorPrivateInfo(allArgs) + "]\n\n");
+
+ // instantiate the launcher part
+ start(JavaPath, args);
+ if (!waitForStarted())
+ {
+ //: Error message displayed if instace can't start
+ emit log(tr("Could not launch minecraft!"), MessageLevel::Error);
+ m_instance->cleanupAfterRun();
+ emit launch_failed(m_instance);
+ return;
+ }
+ // send the launch script to the launcher part
+ QByteArray bytes = launchScript.toUtf8();
+ writeData(bytes.constData(), bytes.length());
+}
diff --git a/logic/MinecraftProcess.h b/logic/MinecraftProcess.h
new file mode 100644
index 00000000..26214026
--- /dev/null
+++ b/logic/MinecraftProcess.h
@@ -0,0 +1,142 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QProcess>
+#include <QString>
+#include "BaseInstance.h"
+
+/**
+ * @brief the MessageLevel Enum
+ * defines what level a message is
+ */
+namespace MessageLevel
+{
+enum Enum
+{
+ MultiMC, /**< MultiMC Messages */
+ Debug, /**< Debug Messages */
+ Info, /**< Info Messages */
+ Message, /**< Standard Messages */
+ Warning, /**< Warnings */
+ Error, /**< Errors */
+ Fatal, /**< Fatal Errors */
+ PrePost, /**< Pre/Post Launch command output */
+};
+}
+
+/**
+ * @file data/minecraftprocess.h
+ * @brief The MinecraftProcess class
+ */
+class MinecraftProcess : public QProcess
+{
+ Q_OBJECT
+public:
+ /**
+ * @brief MinecraftProcess constructor
+ * @param inst the Instance pointer to launch
+ */
+ MinecraftProcess(BaseInstance *inst);
+
+ /**
+ * @brief launch minecraft
+ */
+ void launch();
+
+ BaseInstance *instance()
+ {
+ return m_instance;
+ }
+
+ void setWorkdir(QString path);
+
+ void setLaunchScript(QString script)
+ {
+ launchScript = script;
+ }
+
+ void setNativeFolder(QString natives)
+ {
+ m_nativeFolder = natives;
+ }
+
+ void killMinecraft();
+
+ inline void setLogin(AuthSessionPtr session)
+ {
+ m_session = session;
+ }
+
+signals:
+ /**
+ * @brief emitted when Minecraft immediately fails to run
+ */
+ void launch_failed(BaseInstance *);
+
+ /**
+ * @brief emitted when the PreLaunchCommand fails
+ */
+ void prelaunch_failed(BaseInstance *, int code, QProcess::ExitStatus status);
+
+ /**
+ * @brief emitted when the PostLaunchCommand fails
+ */
+ void postlaunch_failed(BaseInstance *, int code, QProcess::ExitStatus status);
+
+ /**
+ * @brief emitted when mc has finished and the PostLaunchCommand was run
+ */
+ void ended(BaseInstance *, int code, QProcess::ExitStatus status);
+
+ /**
+ * @brief emitted when we want to log something
+ * @param text the text to log
+ * @param level the level to log at
+ */
+ void log(QString text, MessageLevel::Enum level = MessageLevel::MultiMC);
+
+protected:
+ BaseInstance *m_instance = nullptr;
+ QString m_err_leftover;
+ QString m_out_leftover;
+ QProcess m_prepostlaunchprocess;
+ bool killed = false;
+ AuthSessionPtr m_session;
+ QString launchScript;
+ QString m_nativeFolder;
+
+protected
+slots:
+ void finish(int, QProcess::ExitStatus status);
+ void on_stdErr();
+ void on_stdOut();
+ void on_prepost_stdOut();
+ void on_prepost_stdErr();
+ void logOutput(const QStringList &lines,
+ MessageLevel::Enum defaultLevel = MessageLevel::Message,
+ bool guessLevel = true, bool censor = true);
+ void logOutput(QString line,
+ MessageLevel::Enum defaultLevel = MessageLevel::Message,
+ bool guessLevel = true, bool censor = true);
+
+private:
+ QString censorPrivateInfo(QString in);
+ MessageLevel::Enum guessLevel(const QString &message, MessageLevel::Enum defaultLevel);
+ MessageLevel::Enum getLevel(const QString &levelName);
+};
diff --git a/logic/MinecraftVersion.h b/logic/MinecraftVersion.h
new file mode 100644
index 00000000..504381a8
--- /dev/null
+++ b/logic/MinecraftVersion.h
@@ -0,0 +1,89 @@
+/* Copyright 2013 Andrew Okin
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "BaseVersion.h"
+#include <QStringList>
+
+struct MinecraftVersion : public BaseVersion
+{
+ /*!
+ * Gets the version's timestamp.
+ * This is primarily used for sorting versions in a list.
+ */
+ qint64 timestamp;
+
+ /// The URL that this version will be downloaded from. maybe.
+ QString download_url;
+
+ /// This version's type. Used internally to identify what kind of version this is.
+ enum VersionType
+ {
+ OneSix,
+ Legacy,
+ Nostalgia
+ } type;
+
+ /// is this the latest version?
+ bool is_latest = false;
+
+ /// is this a snapshot?
+ bool is_snapshot = false;
+
+ QString m_name;
+
+ QString m_descriptor;
+
+ virtual QString descriptor()
+ {
+ return m_descriptor;
+ }
+
+ virtual QString name()
+ {
+ return m_name;
+ }
+
+ virtual QString typeString() const
+ {
+ QStringList pre_final;
+ if (is_latest == true)
+ {
+ pre_final.append("Latest");
+ }
+ switch (type)
+ {
+ case OneSix:
+ pre_final.append("OneSix");
+ break;
+ case Legacy:
+ pre_final.append("Legacy");
+ break;
+ case Nostalgia:
+ pre_final.append("Nostalgia");
+ break;
+
+ default:
+ pre_final.append(QString("Type(%1)").arg(type));
+ break;
+ }
+ if (is_snapshot == true)
+ {
+ pre_final.append("Snapshot");
+ }
+ return pre_final.join(' ');
+ }
+};
diff --git a/logic/Mod.cpp b/logic/Mod.cpp
new file mode 100644
index 00000000..6732446d
--- /dev/null
+++ b/logic/Mod.cpp
@@ -0,0 +1,358 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QDir>
+#include <QString>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonValue>
+#include <quazip.h>
+#include <quazipfile.h>
+
+#include "Mod.h"
+#include <pathutils.h>
+#include <inifile.h>
+#include "logger/QsLog.h"
+
+Mod::Mod(const QFileInfo &file)
+{
+ repath(file);
+}
+
+void Mod::repath(const QFileInfo &file)
+{
+ m_file = file;
+ QString name_base = file.fileName();
+
+ m_type = Mod::MOD_UNKNOWN;
+
+ if (m_file.isDir())
+ {
+ m_type = MOD_FOLDER;
+ m_name = name_base;
+ m_mmc_id = name_base;
+ }
+ else if (m_file.isFile())
+ {
+ if (name_base.endsWith(".disabled"))
+ {
+ m_enabled = false;
+ name_base.chop(9);
+ }
+ else
+ {
+ m_enabled = true;
+ }
+ m_mmc_id = name_base;
+ if (name_base.endsWith(".zip") || name_base.endsWith(".jar"))
+ {
+ m_type = MOD_ZIPFILE;
+ name_base.chop(4);
+ }
+ else if (name_base.endsWith(".litemod"))
+ {
+ m_type = MOD_LITEMOD;
+ name_base.chop(8);
+ }
+ else
+ {
+ m_type = MOD_SINGLEFILE;
+ }
+ m_name = name_base;
+ }
+
+ if (m_type == MOD_ZIPFILE)
+ {
+ QuaZip zip(m_file.filePath());
+ if (!zip.open(QuaZip::mdUnzip))
+ return;
+
+ QuaZipFile file(&zip);
+
+ if (zip.setCurrentFile("mcmod.info"))
+ {
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ zip.close();
+ return;
+ }
+
+ ReadMCModInfo(file.readAll());
+ file.close();
+ zip.close();
+ return;
+ }
+ else if (zip.setCurrentFile("forgeversion.properties"))
+ {
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ zip.close();
+ return;
+ }
+
+ ReadForgeInfo(file.readAll());
+ file.close();
+ zip.close();
+ return;
+ }
+
+ zip.close();
+ }
+ else if (m_type == MOD_FOLDER)
+ {
+ QFileInfo mcmod_info(PathCombine(m_file.filePath(), "mcmod.info"));
+ if (mcmod_info.isFile())
+ {
+ QFile mcmod(mcmod_info.filePath());
+ if (!mcmod.open(QIODevice::ReadOnly))
+ return;
+ auto data = mcmod.readAll();
+ if (data.isEmpty() || data.isNull())
+ return;
+ ReadMCModInfo(data);
+ }
+ }
+ else if (m_type == MOD_LITEMOD)
+ {
+ QuaZip zip(m_file.filePath());
+ if (!zip.open(QuaZip::mdUnzip))
+ return;
+
+ QuaZipFile file(&zip);
+
+ if (zip.setCurrentFile("litemod.json"))
+ {
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ zip.close();
+ return;
+ }
+
+ ReadLiteModInfo(file.readAll());
+ file.close();
+ }
+ zip.close();
+ }
+}
+
+// NEW format
+// https://github.com/MinecraftForge/FML/wiki/FML-mod-information-file/6f62b37cea040daf350dc253eae6326dd9c822c3
+
+// OLD format:
+// https://github.com/MinecraftForge/FML/wiki/FML-mod-information-file/5bf6a2d05145ec79387acc0d45c958642fb049fc
+void Mod::ReadMCModInfo(QByteArray contents)
+{
+ auto getInfoFromArray = [&](QJsonArray arr)->void
+ {
+ if (!arr.at(0).isObject())
+ return;
+ auto firstObj = arr.at(0).toObject();
+ m_mod_id = firstObj.value("modid").toString();
+ m_name = firstObj.value("name").toString();
+ m_version = firstObj.value("version").toString();
+ m_homeurl = firstObj.value("url").toString();
+ m_description = firstObj.value("description").toString();
+ QJsonArray authors = firstObj.value("authors").toArray();
+ if (authors.size() == 0)
+ m_authors = "";
+ else if (authors.size() >= 1)
+ {
+ m_authors = authors.at(0).toString();
+ for (int i = 1; i < authors.size(); i++)
+ {
+ m_authors += ", " + authors.at(i).toString();
+ }
+ }
+ m_credits = firstObj.value("credits").toString();
+ return;
+ };
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError);
+ // this is the very old format that had just the array
+ if (jsonDoc.isArray())
+ {
+ getInfoFromArray(jsonDoc.array());
+ }
+ else if (jsonDoc.isObject())
+ {
+ auto val = jsonDoc.object().value("modinfoversion");
+ int version = val.toDouble();
+ if (version != 2)
+ {
+ QLOG_ERROR() << "BAD stuff happened to mod json:";
+ QLOG_ERROR() << contents;
+ return;
+ }
+ auto arrVal = jsonDoc.object().value("modlist");
+ if (arrVal.isArray())
+ {
+ getInfoFromArray(arrVal.toArray());
+ }
+ }
+}
+
+void Mod::ReadForgeInfo(QByteArray contents)
+{
+ // Read the data
+ m_name = "Minecraft Forge";
+ m_mod_id = "Forge";
+ m_homeurl = "http://www.minecraftforge.net/forum/";
+ INIFile ini;
+ if (!ini.loadFile(contents))
+ return;
+
+ QString major = ini.get("forge.major.number", "0").toString();
+ QString minor = ini.get("forge.minor.number", "0").toString();
+ QString revision = ini.get("forge.revision.number", "0").toString();
+ QString build = ini.get("forge.build.number", "0").toString();
+
+ m_version = major + "." + minor + "." + revision + "." + build;
+}
+
+void Mod::ReadLiteModInfo(QByteArray contents)
+{
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError);
+ auto object = jsonDoc.object();
+ if(object.contains("name"))
+ {
+ m_mod_id = m_name = object.value("name").toString();
+ }
+ if(object.contains("version"))
+ {
+ m_version=object.value("version").toString("");
+ }
+ else
+ {
+ m_version=object.value("revision").toString("");
+ }
+ m_mcversion = object.value("mcversion").toString();
+ m_authors = object.value("author").toString();
+ m_description = object.value("description").toString();
+ m_homeurl = object.value("url").toString();
+}
+
+bool Mod::replace(Mod &with)
+{
+ if (!destroy())
+ return false;
+ bool success = false;
+ auto t = with.type();
+
+ if (t == MOD_ZIPFILE || t == MOD_SINGLEFILE)
+ {
+ QLOG_DEBUG() << "Copy: " << with.m_file.filePath() << " to " << m_file.filePath();
+ success = QFile::copy(with.m_file.filePath(), m_file.filePath());
+ }
+ if (t == MOD_FOLDER)
+ {
+ success = copyPath(with.m_file.filePath(), m_file.path());
+ }
+ if (success)
+ {
+ m_name = with.m_name;
+ m_mmc_id = with.m_mmc_id;
+ m_mod_id = with.m_mod_id;
+ m_version = with.m_version;
+ m_mcversion = with.m_mcversion;
+ m_description = with.m_description;
+ m_authors = with.m_authors;
+ m_credits = with.m_credits;
+ m_homeurl = with.m_homeurl;
+ m_type = with.m_type;
+ m_file.refresh();
+ }
+ return success;
+}
+
+bool Mod::destroy()
+{
+ if (m_type == MOD_FOLDER)
+ {
+ QDir d(m_file.filePath());
+ if (d.removeRecursively())
+ {
+ m_type = MOD_UNKNOWN;
+ return true;
+ }
+ return false;
+ }
+ else if (m_type == MOD_SINGLEFILE || m_type == MOD_ZIPFILE)
+ {
+ QFile f(m_file.filePath());
+ if (f.remove())
+ {
+ m_type = MOD_UNKNOWN;
+ return true;
+ }
+ return false;
+ }
+ return true;
+}
+
+QString Mod::version() const
+{
+ switch (type())
+ {
+ case MOD_ZIPFILE:
+ case MOD_LITEMOD:
+ return m_version;
+ case MOD_FOLDER:
+ return "Folder";
+ case MOD_SINGLEFILE:
+ return "File";
+ default:
+ return "VOID";
+ }
+}
+
+bool Mod::enable(bool value)
+{
+ if (m_type == Mod::MOD_UNKNOWN || m_type == Mod::MOD_FOLDER)
+ return false;
+
+ if (m_enabled == value)
+ return false;
+
+ QString path = m_file.absoluteFilePath();
+ if (value)
+ {
+ QFile foo(path);
+ if (!path.endsWith(".disabled"))
+ return false;
+ path.chop(9);
+ if (!foo.rename(path))
+ return false;
+ }
+ else
+ {
+ QFile foo(path);
+ path += ".disabled";
+ if (!foo.rename(path))
+ return false;
+ }
+ m_file = QFileInfo(path);
+ m_enabled = value;
+ return true;
+}
+bool Mod::operator==(const Mod &other) const
+{
+ return mmc_id() == other.mmc_id();
+}
+bool Mod::strongCompare(const Mod &other) const
+{
+ return mmc_id() == other.mmc_id() && version() == other.version() && type() == other.type();
+}
diff --git a/logic/Mod.h b/logic/Mod.h
new file mode 100644
index 00000000..2eb2b97a
--- /dev/null
+++ b/logic/Mod.h
@@ -0,0 +1,129 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QFileInfo>
+
+class Mod
+{
+public:
+ enum ModType
+ {
+ MOD_UNKNOWN, //!< Indicates an unspecified mod type.
+ MOD_ZIPFILE, //!< The mod is a zip file containing the mod's class files.
+ MOD_SINGLEFILE, //!< The mod is a single file (not a zip file).
+ MOD_FOLDER, //!< The mod is in a folder on the filesystem.
+ MOD_LITEMOD, //!< The mod is a litemod
+ };
+
+ Mod(const QFileInfo &file);
+
+ QFileInfo filename() const
+ {
+ return m_file;
+ }
+ QString mmc_id() const
+ {
+ return m_mmc_id;
+ }
+ QString mod_id() const
+ {
+ return m_mod_id;
+ }
+ ModType type() const
+ {
+ return m_type;
+ }
+ QString mcversion() const
+ {
+ return m_mcversion;
+ }
+ ;
+ bool valid()
+ {
+ return m_type != MOD_UNKNOWN;
+ }
+ QString name() const
+ {
+ return m_name;
+ }
+
+ QString version() const;
+
+ QString homeurl() const
+ {
+ return m_homeurl;
+ }
+
+ QString description() const
+ {
+ return m_description;
+ }
+
+ QString authors() const
+ {
+ return m_authors;
+ }
+
+ QString credits() const
+ {
+ return m_credits;
+ }
+
+ bool enabled() const
+ {
+ return m_enabled;
+ }
+
+ bool enable(bool value);
+
+ // delete all the files of this mod
+ bool destroy();
+ // replace this mod with a copy of the other
+ bool replace(Mod &with);
+ // change the mod's filesystem path (used by mod lists for *MAGIC* purposes)
+ void repath(const QFileInfo &file);
+
+ // WEAK compare operator - used for replacing mods
+ bool operator==(const Mod &other) const;
+ bool strongCompare(const Mod &other) const;
+
+private:
+ void ReadMCModInfo(QByteArray contents);
+ void ReadForgeInfo(QByteArray contents);
+ void ReadLiteModInfo(QByteArray contents);
+
+protected:
+
+ // FIXME: what do do with those? HMM...
+ /*
+ void ReadModInfoData(QString info);
+ void ReadForgeInfoData(QString infoFileData);
+ */
+
+ QFileInfo m_file;
+ QString m_mmc_id;
+ QString m_mod_id;
+ bool m_enabled = true;
+ QString m_name;
+ QString m_version;
+ QString m_mcversion;
+ QString m_homeurl;
+ QString m_description;
+ QString m_authors;
+ QString m_credits;
+
+ ModType m_type;
+};
diff --git a/logic/ModList.cpp b/logic/ModList.cpp
new file mode 100644
index 00000000..499623bf
--- /dev/null
+++ b/logic/ModList.cpp
@@ -0,0 +1,599 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ModList.h"
+#include "LegacyInstance.h"
+#include <pathutils.h>
+#include <QMimeData>
+#include <QUrl>
+#include <QUuid>
+#include <QString>
+#include <QFileSystemWatcher>
+#include "logger/QsLog.h"
+
+ModList::ModList(const QString &dir, const QString &list_file)
+ : QAbstractListModel(), m_dir(dir), m_list_file(list_file)
+{
+ m_dir.setFilter(QDir::Readable | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs |
+ QDir::NoSymLinks);
+ m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
+ m_list_id = QUuid::createUuid().toString();
+ m_watcher = new QFileSystemWatcher(this);
+ is_watching = false;
+ connect(m_watcher, SIGNAL(directoryChanged(QString)), this,
+ SLOT(directoryChanged(QString)));
+}
+
+void ModList::startWatching()
+{
+ is_watching = m_watcher->addPath(m_dir.absolutePath());
+ if (is_watching)
+ {
+ QLOG_INFO() << "Started watching " << m_dir.absolutePath();
+ }
+ else
+ {
+ QLOG_INFO() << "Failed to start watching " << m_dir.absolutePath();
+ }
+}
+
+void ModList::stopWatching()
+{
+ is_watching = !m_watcher->removePath(m_dir.absolutePath());
+ if (!is_watching)
+ {
+ QLOG_INFO() << "Stopped watching " << m_dir.absolutePath();
+ }
+ else
+ {
+ QLOG_INFO() << "Failed to stop watching " << m_dir.absolutePath();
+ }
+}
+
+bool ModList::update()
+{
+ if (!isValid())
+ return false;
+
+ QList<Mod> orderedMods;
+ QList<Mod> newMods;
+ m_dir.refresh();
+ auto folderContents = m_dir.entryInfoList();
+ bool orderOrStateChanged = false;
+
+ // first, process the ordered items (if any)
+ OrderList listOrder = readListFile();
+ for (auto item : listOrder)
+ {
+ QFileInfo infoEnabled(m_dir.filePath(item.id));
+ QFileInfo infoDisabled(m_dir.filePath(item.id + ".disabled"));
+ int idxEnabled = folderContents.indexOf(infoEnabled);
+ int idxDisabled = folderContents.indexOf(infoDisabled);
+ bool isEnabled;
+ // if both enabled and disabled versions are present, it's a special case...
+ if (idxEnabled >= 0 && idxDisabled >= 0)
+ {
+ // we only process the one we actually have in the order file.
+ // and exactly as we have it.
+ // THIS IS A CORNER CASE
+ isEnabled = item.enabled;
+ }
+ else
+ {
+ // only one is present.
+ // we pick the one that we found.
+ // we assume the mod was enabled/disabled by external means
+ isEnabled = idxEnabled >= 0;
+ }
+ int idx = isEnabled ? idxEnabled : idxDisabled;
+ QFileInfo & info = isEnabled ? infoEnabled : infoDisabled;
+ // if the file from the index file exists
+ if (idx != -1)
+ {
+ // remove from the actual folder contents list
+ folderContents.takeAt(idx);
+ // append the new mod
+ orderedMods.append(Mod(info));
+ if (isEnabled != item.enabled)
+ orderOrStateChanged = true;
+ }
+ else
+ {
+ orderOrStateChanged = true;
+ }
+ }
+ // if there are any untracked files...
+ if (folderContents.size())
+ {
+ // the order surely changed!
+ for (auto entry : folderContents)
+ {
+ newMods.append(Mod(entry));
+ }
+ std::sort(newMods.begin(), newMods.end(), [](const Mod & left, const Mod & right)
+ { return left.name().localeAwareCompare(right.name()) <= 0; });
+ orderedMods.append(newMods);
+ orderOrStateChanged = true;
+ }
+ // otherwise, if we were already tracking some mods
+ else if (mods.size())
+ {
+ // if the number doesn't match, order changed.
+ if (mods.size() != orderedMods.size())
+ orderOrStateChanged = true;
+ // if it does match, compare the mods themselves
+ else
+ for (int i = 0; i < mods.size(); i++)
+ {
+ if (!mods[i].strongCompare(orderedMods[i]))
+ {
+ orderOrStateChanged = true;
+ break;
+ }
+ }
+ }
+ beginResetModel();
+ mods.swap(orderedMods);
+ endResetModel();
+ if (orderOrStateChanged && !m_list_file.isEmpty())
+ {
+ QLOG_INFO() << "Mod list " << m_list_file << " changed!";
+ saveListFile();
+ emit changed();
+ }
+ return true;
+}
+
+void ModList::directoryChanged(QString path)
+{
+ update();
+}
+
+ModList::OrderList ModList::readListFile()
+{
+ OrderList itemList;
+ if (m_list_file.isNull() || m_list_file.isEmpty())
+ return itemList;
+
+ QFile textFile(m_list_file);
+ if (!textFile.open(QIODevice::ReadOnly | QIODevice::Text))
+ return OrderList();
+
+ QTextStream textStream;
+ textStream.setAutoDetectUnicode(true);
+ textStream.setDevice(&textFile);
+ while (true)
+ {
+ QString line = textStream.readLine();
+ if (line.isNull() || line.isEmpty())
+ break;
+ else
+ {
+ OrderItem it;
+ it.enabled = !line.endsWith(".disabled");
+ if (!it.enabled)
+ {
+ line.chop(9);
+ }
+ it.id = line;
+ itemList.append(it);
+ }
+ }
+ textFile.close();
+ return itemList;
+}
+
+bool ModList::saveListFile()
+{
+ if (m_list_file.isNull() || m_list_file.isEmpty())
+ return false;
+ QFile textFile(m_list_file);
+ if (!textFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
+ return false;
+ QTextStream textStream;
+ textStream.setGenerateByteOrderMark(true);
+ textStream.setCodec("UTF-8");
+ textStream.setDevice(&textFile);
+ for (auto mod : mods)
+ {
+ textStream << mod.mmc_id();
+ if (!mod.enabled())
+ textStream << ".disabled";
+ textStream << endl;
+ }
+ textFile.close();
+ return false;
+}
+
+bool ModList::isValid()
+{
+ return m_dir.exists() && m_dir.isReadable();
+}
+
+bool ModList::installMod(const QFileInfo &filename, int index)
+{
+ if (!filename.exists() || !filename.isReadable() || index < 0)
+ {
+ return false;
+ }
+ Mod m(filename);
+ if (!m.valid())
+ return false;
+
+ // if it's already there, replace the original mod (in place)
+ int idx = mods.indexOf(m);
+ if (idx != -1)
+ {
+ int idx2 = mods.indexOf(m,idx+1);
+ if(idx2 != -1)
+ return false;
+ if (mods[idx].replace(m))
+ {
+
+ auto left = this->index(index);
+ auto right = this->index(index, columnCount(QModelIndex()) - 1);
+ emit dataChanged(left, right);
+ saveListFile();
+ emit changed();
+ return true;
+ }
+ return false;
+ }
+
+ auto type = m.type();
+ if (type == Mod::MOD_UNKNOWN)
+ return false;
+ if (type == Mod::MOD_SINGLEFILE || type == Mod::MOD_ZIPFILE || type == Mod::MOD_LITEMOD)
+ {
+ QString newpath = PathCombine(m_dir.path(), filename.fileName());
+ if (!QFile::copy(filename.filePath(), newpath))
+ return false;
+ m.repath(newpath);
+ beginInsertRows(QModelIndex(), index, index);
+ mods.insert(index, m);
+ endInsertRows();
+ saveListFile();
+ emit changed();
+ return true;
+ }
+ else if (type == Mod::MOD_FOLDER)
+ {
+
+ QString from = filename.filePath();
+ QString to = PathCombine(m_dir.path(), filename.fileName());
+ if (!copyPath(from, to))
+ return false;
+ m.repath(to);
+ beginInsertRows(QModelIndex(), index, index);
+ mods.insert(index, m);
+ endInsertRows();
+ saveListFile();
+ emit changed();
+ return true;
+ }
+ return false;
+}
+
+bool ModList::deleteMod(int index)
+{
+ if (index >= mods.size() || index < 0)
+ return false;
+ Mod &m = mods[index];
+ if (m.destroy())
+ {
+ beginRemoveRows(QModelIndex(), index, index);
+ mods.removeAt(index);
+ endRemoveRows();
+ saveListFile();
+ emit changed();
+ return true;
+ }
+ return false;
+}
+
+bool ModList::deleteMods(int first, int last)
+{
+ for (int i = first; i <= last; i++)
+ {
+ Mod &m = mods[i];
+ m.destroy();
+ }
+ beginRemoveRows(QModelIndex(), first, last);
+ mods.erase(mods.begin() + first, mods.begin() + last + 1);
+ endRemoveRows();
+ saveListFile();
+ emit changed();
+ return true;
+}
+
+bool ModList::moveModTo(int from, int to)
+{
+ if (from < 0 || from >= mods.size())
+ return false;
+ if (to >= rowCount())
+ to = rowCount() - 1;
+ if (to == -1)
+ to = rowCount() - 1;
+ if (from == to)
+ return false;
+ int togap = to > from ? to + 1 : to;
+ beginMoveRows(QModelIndex(), from, from, QModelIndex(), togap);
+ mods.move(from, to);
+ endMoveRows();
+ saveListFile();
+ emit changed();
+ return true;
+}
+
+bool ModList::moveModUp(int from)
+{
+ if (from > 0)
+ return moveModTo(from, from - 1);
+ return false;
+}
+
+bool ModList::moveModsUp(int first, int last)
+{
+ if (first == 0)
+ return false;
+
+ beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
+ mods.move(first - 1, last);
+ endMoveRows();
+ saveListFile();
+ emit changed();
+ return true;
+}
+
+bool ModList::moveModDown(int from)
+{
+ if (from < 0)
+ return false;
+ if (from < mods.size() - 1)
+ return moveModTo(from, from + 1);
+ return false;
+}
+
+bool ModList::moveModsDown(int first, int last)
+{
+ if (last == mods.size() - 1)
+ return false;
+
+ beginMoveRows(QModelIndex(), first, last, QModelIndex(), last + 2);
+ mods.move(last + 1, first);
+ endMoveRows();
+ saveListFile();
+ emit changed();
+ return true;
+}
+
+int ModList::columnCount(const QModelIndex &parent) const
+{
+ return 3;
+}
+
+QVariant ModList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ int row = index.row();
+ int column = index.column();
+
+ if (row < 0 || row >= mods.size())
+ return QVariant();
+
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (index.column())
+ {
+ case NameColumn:
+ return mods[row].name();
+ case VersionColumn:
+ return mods[row].version();
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ return mods[row].mmc_id();
+
+ case Qt::CheckStateRole:
+ switch (index.column())
+ {
+ case ActiveColumn:
+ return mods[row].enabled() ? Qt::Checked: Qt::Unchecked;
+ default:
+ return QVariant();
+ }
+ default:
+ return QVariant();
+ }
+}
+
+bool ModList::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ if (index.row() < 0 || index.row() >= rowCount(index) || !index.isValid())
+ {
+ return false;
+ }
+
+ if (role == Qt::CheckStateRole)
+ {
+ auto &mod = mods[index.row()];
+ if (mod.enable(!mod.enabled()))
+ {
+ emit dataChanged(index, index);
+ return true;
+ }
+ }
+ return false;
+}
+
+QVariant ModList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (section)
+ {
+ case ActiveColumn:
+ return QString();
+ case NameColumn:
+ return QString("Name");
+ case VersionColumn:
+ return QString("Version");
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ switch (section)
+ {
+ case ActiveColumn:
+ return "Is the mod enabled?";
+ case NameColumn:
+ return "The name of the mod.";
+ case VersionColumn:
+ return "The version of the mod.";
+ default:
+ return QVariant();
+ }
+ default:
+ return QVariant();
+ }
+ return QVariant();
+}
+
+Qt::ItemFlags ModList::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
+ if (index.isValid())
+ return Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled |
+ defaultFlags;
+ else
+ return Qt::ItemIsDropEnabled | defaultFlags;
+}
+
+QStringList ModList::mimeTypes() const
+{
+ QStringList types;
+ types << "text/uri-list";
+ types << "text/plain";
+ return types;
+}
+
+Qt::DropActions ModList::supportedDropActions() const
+{
+ // copy from outside, move from within and other mod lists
+ return Qt::CopyAction | Qt::MoveAction;
+}
+
+Qt::DropActions ModList::supportedDragActions() const
+{
+ // move to other mod lists or VOID
+ return Qt::MoveAction;
+}
+
+QMimeData *ModList::mimeData(const QModelIndexList &indexes) const
+{
+ QMimeData *data = new QMimeData();
+
+ if (indexes.size() == 0)
+ return data;
+
+ auto idx = indexes[0];
+ int row = idx.row();
+ if (row < 0 || row >= mods.size())
+ return data;
+
+ QStringList params;
+ params << m_list_id << QString::number(row);
+ data->setText(params.join('|'));
+ return data;
+}
+bool ModList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
+ const QModelIndex &parent)
+{
+ if (action == Qt::IgnoreAction)
+ return true;
+ // check if the action is supported
+ if (!data || !(action & supportedDropActions()))
+ return false;
+ if (parent.isValid())
+ {
+ row = parent.row();
+ column = parent.column();
+ }
+
+ if (row > rowCount())
+ row = rowCount();
+ if (row == -1)
+ row = rowCount();
+ if (column == -1)
+ column = 0;
+ QLOG_INFO() << "Drop row: " << row << " column: " << column;
+
+ // files dropped from outside?
+ if (data->hasUrls())
+ {
+ bool was_watching = is_watching;
+ if (was_watching)
+ stopWatching();
+ auto urls = data->urls();
+ for (auto url : urls)
+ {
+ // only local files may be dropped...
+ if (!url.isLocalFile())
+ continue;
+ QString filename = url.toLocalFile();
+ installMod(filename, row);
+ QLOG_INFO() << "installing: " << filename;
+ // if there is no ordering, re-sort the list
+ if (m_list_file.isEmpty())
+ {
+ beginResetModel();
+ std::sort(mods.begin(), mods.end(), [](const Mod & left, const Mod & right)
+ { return left.name().localeAwareCompare(right.name()) <= 0; });
+ endResetModel();
+ }
+ }
+ if (was_watching)
+ startWatching();
+ return true;
+ }
+ else if (data->hasText())
+ {
+ QString sourcestr = data->text();
+ auto list = sourcestr.split('|');
+ if (list.size() != 2)
+ return false;
+ QString remoteId = list[0];
+ int remoteIndex = list[1].toInt();
+ QLOG_INFO() << "move: " << sourcestr;
+ // no moving of things between two lists
+ if (remoteId != m_list_id)
+ return false;
+ // no point moving to the same place...
+ if (row == remoteIndex)
+ return false;
+ // otherwise, move the mod :D
+ moveModTo(remoteIndex, row);
+ return true;
+ }
+ return false;
+}
diff --git a/logic/ModList.h b/logic/ModList.h
new file mode 100644
index 00000000..0d6507fb
--- /dev/null
+++ b/logic/ModList.h
@@ -0,0 +1,152 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QList>
+#include <QString>
+#include <QDir>
+#include <QAbstractListModel>
+
+#include "logic/Mod.h"
+
+class LegacyInstance;
+class BaseInstance;
+class QFileSystemWatcher;
+
+/**
+ * A legacy mod list.
+ * Backed by a folder.
+ */
+class ModList : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ enum Columns
+ {
+ ActiveColumn = 0,
+ NameColumn,
+ VersionColumn
+ };
+ ModList(const QString &dir, const QString &list_file = QString());
+
+ virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+ virtual bool setData(const QModelIndex &index, const QVariant &value,
+ int role = Qt::EditRole);
+
+ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const
+ {
+ return size();
+ }
+ ;
+ virtual QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+
+ size_t size() const
+ {
+ return mods.size();
+ }
+ ;
+ bool empty() const
+ {
+ return size() == 0;
+ }
+ Mod &operator[](size_t index)
+ {
+ return mods[index];
+ }
+
+ /// Reloads the mod list and returns true if the list changed.
+ virtual bool update();
+
+ /**
+ * Adds the given mod to the list at the given index - if the list supports custom ordering
+ */
+ virtual bool installMod(const QFileInfo &filename, int index = 0);
+
+ /// Deletes the mod at the given index.
+ virtual bool deleteMod(int index);
+
+ /// Deletes all the selected mods
+ virtual bool deleteMods(int first, int last);
+
+ /**
+ * move the mod at index to the position N
+ * 0 is the beginning of the list, length() is the end of the list.
+ */
+ virtual bool moveModTo(int from, int to);
+
+ /**
+ * move the mod at index one position upwards
+ */
+ virtual bool moveModUp(int from);
+ virtual bool moveModsUp(int first, int last);
+
+ /**
+ * move the mod at index one position downwards
+ */
+ virtual bool moveModDown(int from);
+ virtual bool moveModsDown(int first, int last);
+
+ /// flags, mostly to support drag&drop
+ virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+ /// get data for drag action
+ virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
+ /// get the supported mime types
+ virtual QStringList mimeTypes() const;
+ /// process data from drop action
+ virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
+ const QModelIndex &parent);
+ /// what drag actions do we support?
+ virtual Qt::DropActions supportedDragActions() const;
+
+ /// what drop actions do we support?
+ virtual Qt::DropActions supportedDropActions() const;
+
+ void startWatching();
+ void stopWatching();
+
+ virtual bool isValid();
+
+ QDir dir()
+ {
+ return m_dir;
+ }
+
+private:
+ struct OrderItem
+ {
+ QString id;
+ bool enabled = false;
+ };
+ typedef QList<OrderItem> OrderList;
+ OrderList readListFile();
+ bool saveListFile();
+private
+slots:
+ void directoryChanged(QString path);
+
+signals:
+ void changed();
+
+protected:
+ QFileSystemWatcher *m_watcher;
+ bool is_watching;
+ QDir m_dir;
+ QString m_list_file;
+ QString m_list_id;
+ QList<Mod> mods;
+};
diff --git a/logic/NagUtils.cpp b/logic/NagUtils.cpp
new file mode 100644
index 00000000..c963a98a
--- /dev/null
+++ b/logic/NagUtils.cpp
@@ -0,0 +1,38 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logic/NagUtils.h"
+#include "gui/dialogs/CustomMessageBox.h"
+
+namespace NagUtils
+{
+void checkJVMArgs(QString jvmargs, QWidget *parent)
+{
+ if (jvmargs.contains("-XX:PermSize=") || jvmargs.contains(QRegExp("-Xm[sx]")))
+ {
+ CustomMessageBox::selectable(
+ parent, QObject::tr("JVM arguments warning"),
+ QObject::tr("You tried to manually set a JVM memory option (using "
+ " \"-XX:PermSize\", \"-Xmx\" or \"-Xms\") - there"
+ " are dedicated boxes for these in the settings (Java"
+ " tab, in the Memory group at the top).\n"
+ "Your manual settings will be overridden by the"
+ " dedicated options.\n"
+ "This message will be displayed until you remove them"
+ " from the JVM arguments."),
+ QMessageBox::Warning)->exec();
+ }
+}
+}
diff --git a/logic/NagUtils.h b/logic/NagUtils.h
new file mode 100644
index 00000000..9564a2b1
--- /dev/null
+++ b/logic/NagUtils.h
@@ -0,0 +1,23 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QWidget>
+
+namespace NagUtils
+{
+void checkJVMArgs(QString args, QWidget *parent);
+}
diff --git a/logic/NostalgiaInstance.cpp b/logic/NostalgiaInstance.cpp
new file mode 100644
index 00000000..2e23ee71
--- /dev/null
+++ b/logic/NostalgiaInstance.cpp
@@ -0,0 +1,32 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NostalgiaInstance.h"
+
+NostalgiaInstance::NostalgiaInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent)
+ : OneSixInstance(rootDir, settings, parent)
+{
+}
+
+QString NostalgiaInstance::getStatusbarDescription()
+{
+ return "Nostalgia : " + intendedVersionId();
+}
+
+bool NostalgiaInstance::menuActionEnabled(QString action_name) const
+{
+ return false;
+}
diff --git a/logic/NostalgiaInstance.h b/logic/NostalgiaInstance.h
new file mode 100644
index 00000000..a26f7f0a
--- /dev/null
+++ b/logic/NostalgiaInstance.h
@@ -0,0 +1,28 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "OneSixInstance.h"
+
+class NostalgiaInstance : public OneSixInstance
+{
+ Q_OBJECT
+public:
+ explicit NostalgiaInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+ virtual QString getStatusbarDescription();
+ virtual bool menuActionEnabled(QString action_name) const;
+};
diff --git a/logic/OneSixFTBInstance.cpp b/logic/OneSixFTBInstance.cpp
new file mode 100644
index 00000000..f8e695b9
--- /dev/null
+++ b/logic/OneSixFTBInstance.cpp
@@ -0,0 +1,125 @@
+#include "OneSixFTBInstance.h"
+
+#include "OneSixVersion.h"
+#include "OneSixLibrary.h"
+#include "tasks/SequentialTask.h"
+#include "ForgeInstaller.h"
+#include "lists/ForgeVersionList.h"
+#include "MultiMC.h"
+
+class OneSixFTBInstanceForge : public Task
+{
+ Q_OBJECT
+public:
+ explicit OneSixFTBInstanceForge(const QString &version, OneSixFTBInstance *inst, QObject *parent = 0) :
+ Task(parent), instance(inst), version("Forge " + version)
+ {
+ }
+
+ void executeTask()
+ {
+ for (int i = 0; i < MMC->forgelist()->count(); ++i)
+ {
+ if (MMC->forgelist()->at(i)->name() == version)
+ {
+ forgeVersion = std::dynamic_pointer_cast<ForgeVersion>(MMC->forgelist()->at(i));
+ break;
+ }
+ }
+ if (!forgeVersion)
+ {
+ emitFailed(QString("Couldn't find forge version ") + version );
+ return;
+ }
+ entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename);
+ if (entry->stale)
+ {
+ setStatus(tr("Downloading Forge..."));
+ fjob = new NetJob("Forge download");
+ fjob->addNetAction(CacheDownload::make(forgeVersion->installer_url, entry));
+ connect(fjob, &NetJob::failed, [this](){emitFailed(m_failReason);});
+ connect(fjob, &NetJob::succeeded, this, &OneSixFTBInstanceForge::installForge);
+ connect(fjob, &NetJob::progress, [this](qint64 c, qint64 total){ setProgress(100 * c / total); });
+ fjob->start();
+ }
+ else
+ {
+ installForge();
+ }
+ }
+
+private
+slots:
+ void installForge()
+ {
+ setStatus(tr("Installing Forge..."));
+ QString forgePath = entry->getFullPath();
+ ForgeInstaller forge(forgePath, forgeVersion->universal_url);
+ if (!instance->reloadFullVersion())
+ {
+ emitFailed(tr("Couldn't load the version config"));
+ return;
+ }
+ instance->revertCustomVersion();
+ instance->customizeVersion();
+ auto version = instance->getFullVersion();
+ if (!forge.apply(version))
+ {
+ emitFailed(tr("Couldn't install Forge"));
+ return;
+ }
+ emitSucceeded();
+ }
+
+private:
+ OneSixFTBInstance *instance;
+ QString version;
+ ForgeVersionPtr forgeVersion;
+ MetaEntryPtr entry;
+ NetJob *fjob;
+};
+
+OneSixFTBInstance::OneSixFTBInstance(const QString &rootDir, SettingsObject *settings, QObject *parent) :
+ OneSixInstance(rootDir, settings, parent)
+{
+ QFile f(QDir(minecraftRoot()).absoluteFilePath("pack.json"));
+ if (f.open(QFile::ReadOnly))
+ {
+ QString data = QString::fromUtf8(f.readAll());
+ QRegularExpressionMatch match = QRegularExpression("net.minecraftforge:minecraftforge:[\\.\\d]*").match(data);
+ m_forge.reset(new OneSixLibrary(match.captured()));
+ m_forge->finalize();
+ }
+}
+
+QString OneSixFTBInstance::id() const
+{
+ return "FTB/" + BaseInstance::id();
+}
+
+QString OneSixFTBInstance::getStatusbarDescription()
+{
+ return "OneSix FTB: " + intendedVersionId();
+}
+bool OneSixFTBInstance::menuActionEnabled(QString action_name) const
+{
+ return false;
+}
+
+std::shared_ptr<Task> OneSixFTBInstance::doUpdate()
+{
+ std::shared_ptr<SequentialTask> task;
+ task.reset(new SequentialTask(this));
+ if (!MMC->forgelist()->isLoaded())
+ {
+ task->addTask(std::shared_ptr<Task>(MMC->forgelist()->getLoadTask()));
+ }
+ task->addTask(OneSixInstance::doUpdate());
+ task->addTask(std::shared_ptr<Task>(new OneSixFTBInstanceForge(m_forge->version(), this, this)));
+ //FIXME: yes. this may appear dumb. but the previous step can change the list, so we do it all again.
+ //TODO: Add a graph task. Construct graphs of tasks so we may capture the logic properly.
+ task->addTask(OneSixInstance::doUpdate());
+ return task;
+}
+
+#include "OneSixFTBInstance.moc"
diff --git a/logic/OneSixFTBInstance.h b/logic/OneSixFTBInstance.h
new file mode 100644
index 00000000..bc543aeb
--- /dev/null
+++ b/logic/OneSixFTBInstance.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "OneSixInstance.h"
+
+class OneSixLibrary;
+
+class OneSixFTBInstance : public OneSixInstance
+{
+ Q_OBJECT
+public:
+ explicit OneSixFTBInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+ virtual QString getStatusbarDescription();
+ virtual bool menuActionEnabled(QString action_name) const;
+
+ virtual std::shared_ptr<Task> doUpdate() override;
+
+ virtual QString id() const;
+
+private:
+ std::shared_ptr<OneSixLibrary> m_forge;
+};
diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp
new file mode 100644
index 00000000..67649f77
--- /dev/null
+++ b/logic/OneSixInstance.cpp
@@ -0,0 +1,406 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "OneSixInstance.h"
+#include "OneSixInstance_p.h"
+#include "OneSixUpdate.h"
+#include "MinecraftProcess.h"
+#include "OneSixVersion.h"
+#include "JavaChecker.h"
+#include "logic/icons/IconList.h"
+
+#include <setting.h>
+#include <pathutils.h>
+#include <cmdutils.h>
+#include <JlCompress.h>
+#include "gui/dialogs/OneSixModEditDialog.h"
+#include "logger/QsLog.h"
+#include "logic/assets/AssetsUtils.h"
+#include <QIcon>
+
+OneSixInstance::OneSixInstance(const QString &rootDir, SettingsObject *setting_obj,
+ QObject *parent)
+ : BaseInstance(new OneSixInstancePrivate(), rootDir, setting_obj, parent)
+{
+ I_D(OneSixInstance);
+ d->m_settings->registerSetting("IntendedVersion", "");
+ d->m_settings->registerSetting("ShouldUpdate", false);
+ reloadFullVersion();
+}
+
+std::shared_ptr<Task> OneSixInstance::doUpdate()
+{
+ return std::shared_ptr<Task>(new OneSixUpdate(this));
+}
+
+QString replaceTokensIn(QString text, QMap<QString, QString> with)
+{
+ QString result;
+ QRegExp token_regexp("\\$\\{(.+)\\}");
+ token_regexp.setMinimal(true);
+ QStringList list;
+ int tail = 0;
+ int head = 0;
+ while ((head = token_regexp.indexIn(text, head)) != -1)
+ {
+ result.append(text.mid(tail, head - tail));
+ QString key = token_regexp.cap(1);
+ auto iter = with.find(key);
+ if (iter != with.end())
+ {
+ result.append(*iter);
+ }
+ head += token_regexp.matchedLength();
+ tail = head;
+ }
+ result.append(text.mid(tail));
+ return result;
+}
+
+QDir OneSixInstance::reconstructAssets(std::shared_ptr<OneSixVersion> version)
+{
+ QDir assetsDir = QDir("assets/");
+ QDir indexDir = QDir(PathCombine(assetsDir.path(), "indexes"));
+ QDir objectDir = QDir(PathCombine(assetsDir.path(), "objects"));
+ QDir virtualDir = QDir(PathCombine(assetsDir.path(), "virtual"));
+
+ QString indexPath = PathCombine(indexDir.path(), version->assets + ".json");
+ QFile indexFile(indexPath);
+ QDir virtualRoot(PathCombine(virtualDir.path(), version->assets));
+
+ if (!indexFile.exists())
+ {
+ QLOG_ERROR() << "No assets index file" << indexPath << "; can't reconstruct assets";
+ return virtualRoot;
+ }
+
+ QLOG_DEBUG() << "reconstructAssets" << assetsDir.path() << indexDir.path()
+ << objectDir.path() << virtualDir.path() << virtualRoot.path();
+
+ AssetsIndex index;
+ bool loadAssetsIndex = AssetsUtils::loadAssetsIndexJson(indexPath, &index);
+
+ if (loadAssetsIndex && index.isVirtual)
+ {
+ QLOG_INFO() << "Reconstructing virtual assets folder at" << virtualRoot.path();
+
+ for (QString map : index.objects.keys())
+ {
+ AssetObject asset_object = index.objects.value(map);
+ QString target_path = PathCombine(virtualRoot.path(), map);
+ QFile target(target_path);
+
+ QString tlk = asset_object.hash.left(2);
+
+ QString original_path =
+ PathCombine(PathCombine(objectDir.path(), tlk), asset_object.hash);
+ QFile original(original_path);
+ if(!original.exists())
+ continue;
+ if (!target.exists())
+ {
+ QFileInfo info(target_path);
+ QDir target_dir = info.dir();
+ // QLOG_DEBUG() << target_dir;
+ if (!target_dir.exists())
+ QDir("").mkpath(target_dir.path());
+
+ bool couldCopy = original.copy(target_path);
+ QLOG_DEBUG() << " Copying" << original_path << "to" << target_path
+ << QString::number(couldCopy); // << original.errorString();
+ }
+ }
+
+ // TODO: Write last used time to virtualRoot/.lastused
+ }
+
+ return virtualRoot;
+}
+
+QStringList OneSixInstance::processMinecraftArgs(AuthSessionPtr session)
+{
+ I_D(OneSixInstance);
+ auto version = d->version;
+ QString args_pattern = version->minecraftArguments;
+
+ QMap<QString, QString> token_mapping;
+ // yggdrasil!
+ token_mapping["auth_username"] = session->username;
+ token_mapping["auth_session"] = session->session;
+ token_mapping["auth_access_token"] = session->access_token;
+ token_mapping["auth_player_name"] = session->player_name;
+ token_mapping["auth_uuid"] = session->uuid;
+
+ // these do nothing and are stupid.
+ token_mapping["profile_name"] = name();
+ token_mapping["version_name"] = version->id;
+
+ QString absRootDir = QDir(minecraftRoot()).absolutePath();
+ token_mapping["game_directory"] = absRootDir;
+ QString absAssetsDir = QDir("assets/").absolutePath();
+ token_mapping["game_assets"] = reconstructAssets(d->version).absolutePath();
+
+ token_mapping["user_properties"] = session->serializeUserProperties();
+ token_mapping["user_type"] = session->user_type;
+ // 1.7.3+ assets tokens
+ token_mapping["assets_root"] = absAssetsDir;
+ token_mapping["assets_index_name"] = version->assets;
+
+ QStringList parts = args_pattern.split(' ', QString::SkipEmptyParts);
+ for (int i = 0; i < parts.length(); i++)
+ {
+ parts[i] = replaceTokensIn(parts[i], token_mapping);
+ }
+ return parts;
+}
+
+MinecraftProcess *OneSixInstance::prepareForLaunch(AuthSessionPtr session)
+{
+ I_D(OneSixInstance);
+
+ QIcon icon = MMC->icons()->getIcon(iconKey());
+ auto pixmap = icon.pixmap(128, 128);
+ pixmap.save(PathCombine(minecraftRoot(), "icon.png"), "PNG");
+
+ auto version = d->version;
+ if (!version)
+ return nullptr;
+ QString launchScript;
+ {
+ auto libs = version->getActiveNormalLibs();
+ for (auto lib : libs)
+ {
+ QFileInfo fi(QString("libraries/") + lib->storagePath());
+ launchScript += "cp " + fi.absoluteFilePath() + "\n";
+ }
+ QString targetstr = "versions/" + version->id + "/" + version->id + ".jar";
+ QFileInfo fi(targetstr);
+ launchScript += "cp " + fi.absoluteFilePath() + "\n";
+ }
+ launchScript += "mainClass " + version->mainClass + "\n";
+
+ for (auto param : processMinecraftArgs(session))
+ {
+ launchScript += "param " + param + "\n";
+ }
+
+ // Set the width and height for 1.6 instances
+ bool maximize = settings().get("LaunchMaximized").toBool();
+ if (maximize)
+ {
+ // this is probably a BAD idea
+ // launchScript += "param --fullscreen\n";
+ }
+ else
+ {
+ launchScript +=
+ "param --width\nparam " + settings().get("MinecraftWinWidth").toString() + "\n";
+ launchScript +=
+ "param --height\nparam " + settings().get("MinecraftWinHeight").toString() + "\n";
+ }
+ QDir natives_dir(PathCombine(instanceRoot(), "natives/"));
+ launchScript += "windowTitle " + windowTitle() + "\n";
+ for(auto native: version->getActiveNativeLibs())
+ {
+ QFileInfo finfo(PathCombine("libraries", native->storagePath()));
+ launchScript += "ext " + finfo.absoluteFilePath() + "\n";
+ }
+ launchScript += "natives " + natives_dir.absolutePath() + "\n";
+ launchScript += "launch onesix\n";
+
+ // create the process and set its parameters
+ MinecraftProcess *proc = new MinecraftProcess(this);
+ proc->setWorkdir(minecraftRoot());
+ proc->setLaunchScript(launchScript);
+ // proc->setNativeFolder(natives_dir.absolutePath());
+ return proc;
+}
+
+void OneSixInstance::cleanupAfterRun()
+{
+ QString target_dir = PathCombine(instanceRoot(), "natives/");
+ QDir dir(target_dir);
+ dir.removeRecursively();
+}
+
+std::shared_ptr<ModList> OneSixInstance::loaderModList()
+{
+ I_D(OneSixInstance);
+ if (!d->loader_mod_list)
+ {
+ d->loader_mod_list.reset(new ModList(loaderModsDir()));
+ }
+ d->loader_mod_list->update();
+ return d->loader_mod_list;
+}
+
+std::shared_ptr<ModList> OneSixInstance::resourcePackList()
+{
+ I_D(OneSixInstance);
+ if (!d->resource_pack_list)
+ {
+ d->resource_pack_list.reset(new ModList(resourcePacksDir()));
+ }
+ d->resource_pack_list->update();
+ return d->resource_pack_list;
+}
+
+QDialog *OneSixInstance::createModEditDialog(QWidget *parent)
+{
+ return new OneSixModEditDialog(this, parent);
+}
+
+bool OneSixInstance::setIntendedVersionId(QString version)
+{
+ settings().set("IntendedVersion", version);
+ setShouldUpdate(true);
+ auto pathCustom = PathCombine(instanceRoot(), "custom.json");
+ auto pathOrig = PathCombine(instanceRoot(), "version.json");
+ QFile::remove(pathCustom);
+ QFile::remove(pathOrig);
+ reloadFullVersion();
+ return true;
+}
+
+QString OneSixInstance::intendedVersionId() const
+{
+ return settings().get("IntendedVersion").toString();
+}
+
+void OneSixInstance::setShouldUpdate(bool val)
+{
+ settings().set("ShouldUpdate", val);
+}
+
+bool OneSixInstance::shouldUpdate() const
+{
+ QVariant var = settings().get("ShouldUpdate");
+ if (!var.isValid() || var.toBool() == false)
+ {
+ return intendedVersionId() != currentVersionId();
+ }
+ return true;
+}
+
+bool OneSixInstance::versionIsCustom()
+{
+ QString verpath_custom = PathCombine(instanceRoot(), "custom.json");
+ QFile versionfile(verpath_custom);
+ return versionfile.exists();
+}
+
+QString OneSixInstance::currentVersionId() const
+{
+ return intendedVersionId();
+}
+
+bool OneSixInstance::customizeVersion()
+{
+ if (!versionIsCustom())
+ {
+ auto pathCustom = PathCombine(instanceRoot(), "custom.json");
+ auto pathOrig = PathCombine(instanceRoot(), "version.json");
+ QFile::copy(pathOrig, pathCustom);
+ return reloadFullVersion();
+ }
+ else
+ return true;
+}
+
+bool OneSixInstance::revertCustomVersion()
+{
+ if (versionIsCustom())
+ {
+ auto path = PathCombine(instanceRoot(), "custom.json");
+ QFile::remove(path);
+ return reloadFullVersion();
+ }
+ else
+ return true;
+}
+
+bool OneSixInstance::reloadFullVersion()
+{
+ I_D(OneSixInstance);
+
+ QString verpath = PathCombine(instanceRoot(), "version.json");
+ {
+ QString verpath_custom = PathCombine(instanceRoot(), "custom.json");
+ QFile versionfile(verpath_custom);
+ if (versionfile.exists())
+ verpath = verpath_custom;
+ }
+
+ auto version = OneSixVersion::fromFile(verpath);
+ if (version)
+ {
+ d->version = version;
+ return true;
+ }
+ else
+ {
+ d->version.reset();
+ return false;
+ }
+}
+
+std::shared_ptr<OneSixVersion> OneSixInstance::getFullVersion()
+{
+ I_D(OneSixInstance);
+ return d->version;
+}
+
+QString OneSixInstance::defaultBaseJar() const
+{
+ return "versions/" + intendedVersionId() + "/" + intendedVersionId() + ".jar";
+}
+
+QString OneSixInstance::defaultCustomBaseJar() const
+{
+ return PathCombine(instanceRoot(), "custom.jar");
+}
+
+bool OneSixInstance::menuActionEnabled(QString action_name) const
+{
+ if (action_name == "actionChangeInstLWJGLVersion")
+ return false;
+ return true;
+}
+
+QString OneSixInstance::getStatusbarDescription()
+{
+ QString descr = "One Six : " + intendedVersionId();
+ if (versionIsCustom())
+ {
+ descr + " (custom)";
+ }
+ return descr;
+}
+
+QString OneSixInstance::loaderModsDir() const
+{
+ return PathCombine(minecraftRoot(), "mods");
+}
+
+QString OneSixInstance::resourcePacksDir() const
+{
+ return PathCombine(minecraftRoot(), "resourcepacks");
+}
+
+QString OneSixInstance::instanceConfigFolder() const
+{
+ return PathCombine(minecraftRoot(), "config");
+}
diff --git a/logic/OneSixInstance.h b/logic/OneSixInstance.h
new file mode 100644
index 00000000..c159723b
--- /dev/null
+++ b/logic/OneSixInstance.h
@@ -0,0 +1,78 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QStringList>
+#include <QDir>
+
+#include "BaseInstance.h"
+
+class OneSixVersion;
+class Task;
+class ModList;
+
+class OneSixInstance : public BaseInstance
+{
+ Q_OBJECT
+public:
+ explicit OneSixInstance(const QString &rootDir, SettingsObject *settings,
+ QObject *parent = 0);
+
+ ////// Mod Lists //////
+ std::shared_ptr<ModList> loaderModList();
+ std::shared_ptr<ModList> resourcePackList();
+
+ ////// Directories //////
+ QString resourcePacksDir() const;
+ QString loaderModsDir() const;
+ virtual QString instanceConfigFolder() const override;
+
+ virtual std::shared_ptr<Task> doUpdate() override;
+ virtual MinecraftProcess *prepareForLaunch(AuthSessionPtr session) override;
+
+ virtual void cleanupAfterRun() override;
+
+ virtual QString intendedVersionId() const override;
+ virtual bool setIntendedVersionId(QString version) override;
+
+ virtual QString currentVersionId() const override;
+
+ virtual bool shouldUpdate() const override;
+ virtual void setShouldUpdate(bool val) override;
+
+ virtual QDialog *createModEditDialog(QWidget *parent) override;
+
+ /// reload the full version json file. return true on success!
+ bool reloadFullVersion();
+ /// get the current full version info
+ std::shared_ptr<OneSixVersion> getFullVersion();
+ /// revert the current custom version back to base
+ bool revertCustomVersion();
+ /// customize the current base version
+ bool customizeVersion();
+ /// is the current version original, or custom?
+ virtual bool versionIsCustom() override;
+
+ virtual QString defaultBaseJar() const override;
+ virtual QString defaultCustomBaseJar() const override;
+
+ virtual bool menuActionEnabled(QString action_name) const override;
+ virtual QString getStatusbarDescription() override;
+
+private:
+ QStringList processMinecraftArgs(AuthSessionPtr account);
+ QDir reconstructAssets(std::shared_ptr<OneSixVersion> version);
+};
diff --git a/logic/OneSixInstance_p.h b/logic/OneSixInstance_p.h
new file mode 100644
index 00000000..6b7ea431
--- /dev/null
+++ b/logic/OneSixInstance_p.h
@@ -0,0 +1,30 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <memory>
+
+#include "logic/BaseInstance_p.h"
+#include "logic/OneSixVersion.h"
+#include "logic/OneSixLibrary.h"
+#include "logic/ModList.h"
+
+struct OneSixInstancePrivate : public BaseInstancePrivate
+{
+ std::shared_ptr<OneSixVersion> version;
+ std::shared_ptr<ModList> loader_mod_list;
+ std::shared_ptr<ModList> resource_pack_list;
+}; \ No newline at end of file
diff --git a/logic/OneSixLibrary.cpp b/logic/OneSixLibrary.cpp
new file mode 100644
index 00000000..7b80d5e7
--- /dev/null
+++ b/logic/OneSixLibrary.cpp
@@ -0,0 +1,268 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QJsonArray>
+
+#include "OneSixLibrary.h"
+#include "OneSixRule.h"
+#include "OpSys.h"
+#include "logic/net/URLConstants.h"
+#include <pathutils.h>
+#include <JlCompress.h>
+#include "logger/QsLog.h"
+
+void OneSixLibrary::finalize()
+{
+ QStringList parts = m_name.split(':');
+ QString relative = parts[0];
+ relative.replace('.', '/');
+ relative += '/' + parts[1] + '/' + parts[2] + '/' + parts[1] + '-' + parts[2];
+
+ if (!m_is_native)
+ relative += ".jar";
+ else
+ {
+ if (m_native_suffixes.contains(currentSystem))
+ {
+ relative += "-" + m_native_suffixes[currentSystem] + ".jar";
+ }
+ else
+ {
+ // really, bad.
+ relative += ".jar";
+ }
+ }
+
+ m_decentname = parts[1];
+ m_decentversion = parts[2];
+ m_storage_path = relative;
+ m_download_url = m_base_url + relative;
+
+ if (m_rules.empty())
+ {
+ m_is_active = true;
+ }
+ else
+ {
+ RuleAction result = Disallow;
+ for (auto rule : m_rules)
+ {
+ RuleAction temp = rule->apply(this);
+ if (temp != Defer)
+ result = temp;
+ }
+ m_is_active = (result == Allow);
+ }
+ if (m_is_native)
+ {
+ m_is_active = m_is_active && m_native_suffixes.contains(currentSystem);
+ m_decenttype = "Native";
+ }
+ else
+ {
+ m_decenttype = "Java";
+ }
+}
+
+void OneSixLibrary::setName(QString name)
+{
+ m_name = name;
+}
+void OneSixLibrary::setBaseUrl(QString base_url)
+{
+ m_base_url = base_url;
+}
+void OneSixLibrary::setIsNative()
+{
+ m_is_native = true;
+}
+void OneSixLibrary::addNative(OpSys os, QString suffix)
+{
+ m_is_native = true;
+ m_native_suffixes[os] = suffix;
+}
+void OneSixLibrary::setRules(QList<std::shared_ptr<Rule>> rules)
+{
+ m_rules = rules;
+}
+bool OneSixLibrary::isActive()
+{
+ return m_is_active;
+}
+bool OneSixLibrary::isNative()
+{
+ return m_is_native;
+}
+QString OneSixLibrary::downloadUrl()
+{
+ if (m_absolute_url.size())
+ return m_absolute_url;
+ return m_download_url;
+}
+QString OneSixLibrary::storagePath()
+{
+ return m_storage_path;
+}
+
+void OneSixLibrary::setAbsoluteUrl(QString absolute_url)
+{
+ m_absolute_url = absolute_url;
+}
+
+QString OneSixLibrary::absoluteUrl()
+{
+ return m_absolute_url;
+}
+
+void OneSixLibrary::setHint(QString hint)
+{
+ m_hint = hint;
+}
+
+QString OneSixLibrary::hint()
+{
+ return m_hint;
+}
+
+bool OneSixLibrary::filesExist()
+{
+ QString storage = storagePath();
+ if (storage.contains("${arch}"))
+ {
+ QString cooked_storage = storage;
+ cooked_storage.replace("${arch}", "32");
+ QFileInfo info32(PathCombine("libraries", cooked_storage));
+ if (!info32.exists())
+ {
+ return false;
+ }
+ cooked_storage = storage;
+ cooked_storage.replace("${arch}", "64");
+ QFileInfo info64(PathCombine("libraries", cooked_storage));
+ if (!info64.exists())
+ {
+ return false;
+ }
+ }
+ else
+ {
+ QFileInfo info(PathCombine("libraries", storage));
+ if (!info.exists())
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool OneSixLibrary::extractTo(QString target_dir)
+{
+ QString storage = storagePath();
+ if (storage.contains("${arch}"))
+ {
+ QString cooked_storage = storage;
+ cooked_storage.replace("${arch}", "32");
+ QString origin = PathCombine("libraries", cooked_storage);
+ QString target_dir_cooked = PathCombine(target_dir, "32");
+ if(!ensureFolderPathExists(target_dir_cooked))
+ {
+ QLOG_ERROR() << "Couldn't create folder " + target_dir_cooked;
+ return false;
+ }
+ if (JlCompress::extractWithExceptions(origin, target_dir_cooked, extract_excludes)
+ .isEmpty())
+ {
+ QLOG_ERROR() << "Couldn't extract " + origin;
+ return false;
+ }
+ cooked_storage = storage;
+ cooked_storage.replace("${arch}", "64");
+ origin = PathCombine("libraries", cooked_storage);
+ target_dir_cooked = PathCombine(target_dir, "64");
+ if(!ensureFolderPathExists(target_dir_cooked))
+ {
+ QLOG_ERROR() << "Couldn't create folder " + target_dir_cooked;
+ return false;
+ }
+ if (JlCompress::extractWithExceptions(origin, target_dir_cooked, extract_excludes)
+ .isEmpty())
+ {
+ QLOG_ERROR() << "Couldn't extract " + origin;
+ return false;
+ }
+ }
+ else
+ {
+ if(!ensureFolderPathExists(target_dir))
+ {
+ QLOG_ERROR() << "Couldn't create folder " + target_dir;
+ return false;
+ }
+ QString path = PathCombine("libraries", storage);
+ if (JlCompress::extractWithExceptions(path, target_dir, extract_excludes).isEmpty())
+ {
+ QLOG_ERROR() << "Couldn't extract " + path;
+ return false;
+ }
+ }
+ return true;
+}
+
+QJsonObject OneSixLibrary::toJson()
+{
+ QJsonObject libRoot;
+ libRoot.insert("name", m_name);
+ if (m_absolute_url.size())
+ libRoot.insert("MMC-absoluteUrl", m_absolute_url);
+ if (m_hint.size())
+ libRoot.insert("MMC-hint", m_hint);
+ if (m_base_url != "http://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
+ m_base_url != "https://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
+ m_base_url != "https://" + URLConstants::LIBRARY_BASE)
+ libRoot.insert("url", m_base_url);
+ if (isNative() && m_native_suffixes.size())
+ {
+ QJsonObject nativeList;
+ auto iter = m_native_suffixes.begin();
+ while (iter != m_native_suffixes.end())
+ {
+ nativeList.insert(OpSys_toString(iter.key()), iter.value());
+ iter++;
+ }
+ libRoot.insert("natives", nativeList);
+ }
+ if (isNative() && extract_excludes.size())
+ {
+ QJsonArray excludes;
+ QJsonObject extract;
+ for (auto exclude : extract_excludes)
+ {
+ excludes.append(exclude);
+ }
+ extract.insert("exclude", excludes);
+ libRoot.insert("extract", extract);
+ }
+ if (m_rules.size())
+ {
+ QJsonArray allRules;
+ for (auto &rule : m_rules)
+ {
+ QJsonObject ruleObj = rule->toJson();
+ allRules.append(ruleObj);
+ }
+ libRoot.insert("rules", allRules);
+ }
+ return libRoot;
+}
diff --git a/logic/OneSixLibrary.h b/logic/OneSixLibrary.h
new file mode 100644
index 00000000..227cdbef
--- /dev/null
+++ b/logic/OneSixLibrary.h
@@ -0,0 +1,132 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QString>
+#include <QStringList>
+#include <QMap>
+#include <QJsonObject>
+#include <memory>
+
+#include "logic/net/URLConstants.h"
+#include "OpSys.h"
+
+class Rule;
+
+class OneSixLibrary
+{
+private:
+ // basic values used internally (so far)
+ QString m_name;
+ QString m_base_url = "https://" + URLConstants::LIBRARY_BASE;
+ QList<std::shared_ptr<Rule>> m_rules;
+
+ // custom values
+ /// absolute URL. takes precedence over m_download_path, if defined
+ QString m_absolute_url;
+ /// download hint - how to actually get the library
+ QString m_hint;
+
+ // derived values used for real things
+ /// a decent name fit for display
+ QString m_decentname;
+ /// a decent version fit for display
+ QString m_decentversion;
+ /// a decent type fit for display
+ QString m_decenttype;
+ /// where to store the lib locally
+ QString m_storage_path;
+ /// where to download the lib from
+ QString m_download_url;
+ /// is this lib actually active on the current OS?
+ bool m_is_active = false;
+ /// is the library a native?
+ bool m_is_native = false;
+ /// native suffixes per OS
+ QMap<OpSys, QString> m_native_suffixes;
+
+public:
+ QStringList extract_excludes;
+
+public:
+ /// Constructor
+ OneSixLibrary(QString name)
+ {
+ m_name = name;
+ }
+
+ /// Returns the raw name field
+ QString rawName() const
+ {
+ return m_name;
+ }
+
+ QJsonObject toJson();
+
+ /**
+ * finalize the library, processing the input values into derived values and state
+ *
+ * This SHALL be called after all the values are parsed or after any further change.
+ */
+ void finalize();
+
+ /// Set the library composite name
+ void setName(QString name);
+ /// get a decent-looking name
+ QString name()
+ {
+ return m_decentname;
+ }
+ /// get a decent-looking version
+ QString version()
+ {
+ return m_decentversion;
+ }
+ /// what kind of library is it? (for display)
+ QString type()
+ {
+ return m_decenttype;
+ }
+ /// Set the url base for downloads
+ void setBaseUrl(QString base_url);
+
+ /// Call this to mark the library as 'native' (it's a zip archive with DLLs)
+ void setIsNative();
+ /// Attach a name suffix to the specified OS native
+ void addNative(OpSys os, QString suffix);
+ /// Set the load rules
+ void setRules(QList<std::shared_ptr<Rule>> rules);
+
+ /// Returns true if the library should be loaded (or extracted, in case of natives)
+ bool isActive();
+ /// Returns true if the library is native
+ bool isNative();
+ /// Get the URL to download the library from
+ QString downloadUrl();
+ /// Get the relative path where the library should be saved
+ QString storagePath();
+
+ /// set an absolute URL for the library. This is an MMC extension.
+ void setAbsoluteUrl(QString absolute_url);
+ QString absoluteUrl();
+
+ /// set a hint about how to treat the library. This is an MMC extension.
+ void setHint(QString hint);
+ QString hint();
+
+ bool extractTo(QString target_dir);
+ bool filesExist();
+};
diff --git a/logic/OneSixRule.cpp b/logic/OneSixRule.cpp
new file mode 100644
index 00000000..392b1dd1
--- /dev/null
+++ b/logic/OneSixRule.cpp
@@ -0,0 +1,89 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QJsonObject>
+#include <QJsonArray>
+
+#include "OneSixRule.h"
+
+QList<std::shared_ptr<Rule>> rulesFromJsonV4(QJsonObject &objectWithRules)
+{
+ QList<std::shared_ptr<Rule>> rules;
+ auto rulesVal = objectWithRules.value("rules");
+ if (!rulesVal.isArray())
+ return rules;
+
+ QJsonArray ruleList = rulesVal.toArray();
+ for (auto ruleVal : ruleList)
+ {
+ std::shared_ptr<Rule> rule;
+ if (!ruleVal.isObject())
+ continue;
+ auto ruleObj = ruleVal.toObject();
+ auto actionVal = ruleObj.value("action");
+ if (!actionVal.isString())
+ continue;
+ auto action = RuleAction_fromString(actionVal.toString());
+ if (action == Defer)
+ continue;
+
+ auto osVal = ruleObj.value("os");
+ if (!osVal.isObject())
+ {
+ // add a new implicit action rule
+ rules.append(ImplicitRule::create(action));
+ continue;
+ }
+
+ auto osObj = osVal.toObject();
+ auto osNameVal = osObj.value("name");
+ if (!osNameVal.isString())
+ continue;
+ OpSys requiredOs = OpSys_fromString(osNameVal.toString());
+ QString versionRegex = osObj.value("version").toString();
+ // add a new OS rule
+ rules.append(OsRule::create(action, requiredOs, versionRegex));
+ }
+ return rules;
+}
+
+QJsonObject ImplicitRule::toJson()
+{
+ QJsonObject ruleObj;
+ ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
+ return ruleObj;
+}
+
+QJsonObject OsRule::toJson()
+{
+ QJsonObject ruleObj;
+ ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
+ QJsonObject osObj;
+ {
+ osObj.insert("name", OpSys_toString(m_system));
+ osObj.insert("version", m_version_regexp);
+ }
+ ruleObj.insert("os", osObj);
+ return ruleObj;
+}
+
+RuleAction RuleAction_fromString(QString name)
+{
+ if (name == "allow")
+ return Allow;
+ if (name == "disallow")
+ return Disallow;
+ return Defer;
+} \ No newline at end of file
diff --git a/logic/OneSixRule.h b/logic/OneSixRule.h
new file mode 100644
index 00000000..5a13cbd9
--- /dev/null
+++ b/logic/OneSixRule.h
@@ -0,0 +1,98 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QString>
+
+#include "logic/OneSixLibrary.h"
+
+enum RuleAction
+{
+ Allow,
+ Disallow,
+ Defer
+};
+
+RuleAction RuleAction_fromString(QString);
+QList<std::shared_ptr<Rule>> rulesFromJsonV4(QJsonObject &objectWithRules);
+
+class Rule
+{
+protected:
+ RuleAction m_result;
+ virtual bool applies(OneSixLibrary *parent) = 0;
+
+public:
+ Rule(RuleAction result) : m_result(result)
+ {
+ }
+ virtual ~Rule() {};
+ virtual QJsonObject toJson() = 0;
+ RuleAction apply(OneSixLibrary *parent)
+ {
+ if (applies(parent))
+ return m_result;
+ else
+ return Defer;
+ }
+ ;
+};
+
+class OsRule : public Rule
+{
+private:
+ // the OS
+ OpSys m_system;
+ // the OS version regexp
+ QString m_version_regexp;
+
+protected:
+ virtual bool applies(OneSixLibrary *)
+ {
+ return (m_system == currentSystem);
+ }
+ OsRule(RuleAction result, OpSys system, QString version_regexp)
+ : Rule(result), m_system(system), m_version_regexp(version_regexp)
+ {
+ }
+
+public:
+ virtual QJsonObject toJson();
+ static std::shared_ptr<OsRule> create(RuleAction result, OpSys system,
+ QString version_regexp)
+ {
+ return std::shared_ptr<OsRule>(new OsRule(result, system, version_regexp));
+ }
+};
+
+class ImplicitRule : public Rule
+{
+protected:
+ virtual bool applies(OneSixLibrary *)
+ {
+ return true;
+ }
+ ImplicitRule(RuleAction result) : Rule(result)
+ {
+ }
+
+public:
+ virtual QJsonObject toJson();
+ static std::shared_ptr<ImplicitRule> create(RuleAction result)
+ {
+ return std::shared_ptr<ImplicitRule>(new ImplicitRule(result));
+ }
+};
diff --git a/logic/OneSixUpdate.cpp b/logic/OneSixUpdate.cpp
new file mode 100644
index 00000000..7685952c
--- /dev/null
+++ b/logic/OneSixUpdate.cpp
@@ -0,0 +1,326 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "OneSixUpdate.h"
+
+#include <QtNetwork>
+
+#include <QFile>
+#include <QFileInfo>
+#include <QTextStream>
+#include <QDataStream>
+
+#include "BaseInstance.h"
+#include "lists/MinecraftVersionList.h"
+#include "OneSixVersion.h"
+#include "OneSixLibrary.h"
+#include "OneSixInstance.h"
+#include "net/ForgeMirrors.h"
+#include "net/URLConstants.h"
+#include "assets/AssetsUtils.h"
+
+#include "pathutils.h"
+#include <JlCompress.h>
+
+OneSixUpdate::OneSixUpdate(BaseInstance *inst, QObject *parent)
+ : Task(parent), m_inst(inst)
+{
+}
+
+void OneSixUpdate::executeTask()
+{
+ QString intendedVersion = m_inst->intendedVersionId();
+
+ // Make directories
+ QDir mcDir(m_inst->minecraftRoot());
+ if (!mcDir.exists() && !mcDir.mkpath("."))
+ {
+ emitFailed("Failed to create bin folder.");
+ return;
+ }
+
+ if (m_inst->shouldUpdate())
+ {
+ // Get a pointer to the version object that corresponds to the instance's version.
+ targetVersion = std::dynamic_pointer_cast<MinecraftVersion>(
+ MMC->minecraftlist()->findVersion(intendedVersion));
+ if (targetVersion == nullptr)
+ {
+ // don't do anything if it was invalid
+ emitFailed("The specified Minecraft version is invalid. Choose a different one.");
+ return;
+ }
+ versionFileStart();
+ }
+ else
+ {
+ jarlibStart();
+ }
+}
+
+void OneSixUpdate::versionFileStart()
+{
+ QLOG_INFO() << m_inst->name() << ": getting version file.";
+ setStatus(tr("Getting the version files from Mojang..."));
+
+ QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS +
+ targetVersion->descriptor() + "/" + targetVersion->descriptor() + ".json";
+ auto job = new NetJob("Version index");
+ job->addNetAction(ByteArrayDownload::make(QUrl(urlstr)));
+ specificVersionDownloadJob.reset(job);
+ connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(versionFileFinished()));
+ connect(specificVersionDownloadJob.get(), SIGNAL(failed()), SLOT(versionFileFailed()));
+ connect(specificVersionDownloadJob.get(), SIGNAL(progress(qint64, qint64)),
+ SIGNAL(progress(qint64, qint64)));
+ specificVersionDownloadJob->start();
+}
+
+void OneSixUpdate::versionFileFinished()
+{
+ NetActionPtr DlJob = specificVersionDownloadJob->first();
+ OneSixInstance *inst = (OneSixInstance *)m_inst;
+
+ QString version_id = targetVersion->descriptor();
+ QString inst_dir = m_inst->instanceRoot();
+ // save the version file in $instanceId/version.json
+ {
+ QString version1 = PathCombine(inst_dir, "/version.json");
+ ensureFilePathExists(version1);
+ // FIXME: detect errors here, download to a temp file, swap
+ QSaveFile vfile1(version1);
+ if (!vfile1.open(QIODevice::Truncate | QIODevice::WriteOnly))
+ {
+ emitFailed("Can't open " + version1 + " for writing.");
+ return;
+ }
+ auto data = std::dynamic_pointer_cast<ByteArrayDownload>(DlJob)->m_data;
+ qint64 actual = 0;
+ if ((actual = vfile1.write(data)) != data.size())
+ {
+ emitFailed("Failed to write into " + version1 + ". Written " + actual + " out of " +
+ data.size() + '.');
+ return;
+ }
+ if (!vfile1.commit())
+ {
+ emitFailed("Can't commit changes to " + version1);
+ return;
+ }
+ }
+
+ // the version is downloaded safely. update is 'done' at this point
+ m_inst->setShouldUpdate(false);
+
+ // delete any custom version inside the instance (it's no longer relevant, we did an update)
+ QString custom = PathCombine(inst_dir, "/custom.json");
+ QFile finfo(custom);
+ if (finfo.exists())
+ {
+ finfo.remove();
+ }
+ inst->reloadFullVersion();
+
+ jarlibStart();
+}
+
+void OneSixUpdate::versionFileFailed()
+{
+ emitFailed("Failed to download the version description. Try again.");
+}
+
+void OneSixUpdate::assetIndexStart()
+{
+ setStatus(tr("Updating assets index..."));
+ OneSixInstance *inst = (OneSixInstance *)m_inst;
+ std::shared_ptr<OneSixVersion> version = inst->getFullVersion();
+ QString assetName = version->assets;
+ QUrl indexUrl = "http://" + URLConstants::AWS_DOWNLOAD_INDEXES + assetName + ".json";
+ QString localPath = assetName + ".json";
+ auto job = new NetJob("Asset index for " + inst->name());
+
+ auto metacache = MMC->metacache();
+ auto entry = metacache->resolveEntry("asset_indexes", localPath);
+ job->addNetAction(CacheDownload::make(indexUrl, entry));
+ jarlibDownloadJob.reset(job);
+
+ connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetIndexFinished()));
+ connect(jarlibDownloadJob.get(), SIGNAL(failed()), SLOT(assetIndexFailed()));
+ connect(jarlibDownloadJob.get(), SIGNAL(progress(qint64, qint64)),
+ SIGNAL(progress(qint64, qint64)));
+
+ jarlibDownloadJob->start();
+}
+
+void OneSixUpdate::assetIndexFinished()
+{
+ AssetsIndex index;
+
+ OneSixInstance *inst = (OneSixInstance *)m_inst;
+ std::shared_ptr<OneSixVersion> version = inst->getFullVersion();
+ QString assetName = version->assets;
+
+ QString asset_fname = "assets/indexes/" + assetName + ".json";
+ if (!AssetsUtils::loadAssetsIndexJson(asset_fname, &index))
+ {
+ emitFailed("Failed to read the assets index!");
+ }
+
+ QList<Md5EtagDownloadPtr> dls;
+ for (auto object : index.objects.values())
+ {
+ QString objectName = object.hash.left(2) + "/" + object.hash;
+ QFileInfo objectFile("assets/objects/" + objectName);
+ if ((!objectFile.isFile()) || (objectFile.size() != object.size))
+ {
+ auto objectDL = MD5EtagDownload::make(
+ QUrl("http://" + URLConstants::RESOURCE_BASE + objectName),
+ objectFile.filePath());
+ objectDL->m_total_progress = object.size;
+ dls.append(objectDL);
+ }
+ }
+ if (dls.size())
+ {
+ setStatus(tr("Getting the assets files from Mojang..."));
+ auto job = new NetJob("Assets for " + inst->name());
+ for (auto dl : dls)
+ job->addNetAction(dl);
+ jarlibDownloadJob.reset(job);
+ connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetsFinished()));
+ connect(jarlibDownloadJob.get(), SIGNAL(failed()), SLOT(assetsFailed()));
+ connect(jarlibDownloadJob.get(), SIGNAL(progress(qint64, qint64)),
+ SIGNAL(progress(qint64, qint64)));
+ jarlibDownloadJob->start();
+ return;
+ }
+ assetsFinished();
+}
+
+void OneSixUpdate::assetIndexFailed()
+{
+ emitFailed("Failed to download the assets index!");
+}
+
+void OneSixUpdate::assetsFinished()
+{
+ emitSucceeded();
+}
+
+void OneSixUpdate::assetsFailed()
+{
+ emitFailed("Failed to download assets!");
+}
+
+void OneSixUpdate::jarlibStart()
+{
+ setStatus(tr("Getting the library files from Mojang..."));
+ QLOG_INFO() << m_inst->name() << ": downloading libraries";
+ OneSixInstance *inst = (OneSixInstance *)m_inst;
+ bool successful = inst->reloadFullVersion();
+ if (!successful)
+ {
+ emitFailed("Failed to load the version description file. It might be "
+ "corrupted, missing or simply too new.");
+ return;
+ }
+
+ // Build a list of URLs that will need to be downloaded.
+ std::shared_ptr<OneSixVersion> version = inst->getFullVersion();
+ // minecraft.jar for this version
+ {
+ QString version_id = version->id;
+ QString localPath = version_id + "/" + version_id + ".jar";
+ QString urlstr = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + localPath;
+
+ auto job = new NetJob("Libraries for instance " + inst->name());
+
+ auto metacache = MMC->metacache();
+ auto entry = metacache->resolveEntry("versions", localPath);
+ job->addNetAction(CacheDownload::make(QUrl(urlstr), entry));
+
+ jarlibDownloadJob.reset(job);
+ }
+
+ auto libs = version->getActiveNativeLibs();
+ libs.append(version->getActiveNormalLibs());
+
+ auto metacache = MMC->metacache();
+ QList<ForgeXzDownloadPtr> ForgeLibs;
+ for (auto lib : libs)
+ {
+ if (lib->hint() == "local")
+ continue;
+
+ QString raw_storage = lib->storagePath();
+ QString raw_dl = lib->downloadUrl();
+
+ auto f = [&](QString storage, QString dl)
+ {
+ auto entry = metacache->resolveEntry("libraries", storage);
+ if (entry->stale)
+ {
+ if (lib->hint() == "forge-pack-xz")
+ {
+ ForgeLibs.append(ForgeXzDownload::make(storage, entry));
+ }
+ else
+ {
+ jarlibDownloadJob->addNetAction(CacheDownload::make(dl, entry));
+ }
+ }
+ };
+ if (raw_storage.contains("${arch}"))
+ {
+ QString cooked_storage = raw_storage;
+ QString cooked_dl = raw_dl;
+ f(cooked_storage.replace("${arch}", "32"), cooked_dl.replace("${arch}", "32"));
+ cooked_storage = raw_storage;
+ cooked_dl = raw_dl;
+ f(cooked_storage.replace("${arch}", "64"), cooked_dl.replace("${arch}", "64"));
+ }
+ else
+ {
+ f(raw_storage, raw_dl);
+ }
+ }
+ // TODO: think about how to propagate this from the original json file... or IF AT ALL
+ QString forgeMirrorList = "http://files.minecraftforge.net/mirror-brand.list";
+ if (!ForgeLibs.empty())
+ {
+ jarlibDownloadJob->addNetAction(
+ ForgeMirrors::make(ForgeLibs, jarlibDownloadJob, forgeMirrorList));
+ }
+
+ connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(jarlibFinished()));
+ connect(jarlibDownloadJob.get(), SIGNAL(failed()), SLOT(jarlibFailed()));
+ connect(jarlibDownloadJob.get(), SIGNAL(progress(qint64, qint64)),
+ SIGNAL(progress(qint64, qint64)));
+
+ jarlibDownloadJob->start();
+}
+
+void OneSixUpdate::jarlibFinished()
+{
+ assetIndexStart();
+}
+
+void OneSixUpdate::jarlibFailed()
+{
+ QStringList failed = jarlibDownloadJob->getFailedFiles();
+ QString failed_all = failed.join("\n");
+ emitFailed("Failed to download the following files:\n" + failed_all +
+ "\n\nPlease try again.");
+}
diff --git a/logic/OneSixUpdate.h b/logic/OneSixUpdate.h
new file mode 100644
index 00000000..3c18211e
--- /dev/null
+++ b/logic/OneSixUpdate.h
@@ -0,0 +1,59 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QList>
+#include <QUrl>
+
+#include "logic/net/NetJob.h"
+#include "logic/tasks/Task.h"
+
+class MinecraftVersion;
+class BaseInstance;
+
+class OneSixUpdate : public Task
+{
+ Q_OBJECT
+public:
+ explicit OneSixUpdate(BaseInstance *inst, QObject *parent = 0);
+ virtual void executeTask();
+
+private
+slots:
+ void versionFileStart();
+ void versionFileFinished();
+ void versionFileFailed();
+
+ void jarlibStart();
+ void jarlibFinished();
+ void jarlibFailed();
+
+ void assetIndexStart();
+ void assetIndexFinished();
+ void assetIndexFailed();
+
+ void assetsFinished();
+ void assetsFailed();
+
+private:
+ NetJobPtr specificVersionDownloadJob;
+ NetJobPtr jarlibDownloadJob;
+
+ // target version, determined during this task
+ std::shared_ptr<MinecraftVersion> targetVersion;
+ BaseInstance *m_inst = nullptr;
+};
diff --git a/logic/OneSixVersion.cpp b/logic/OneSixVersion.cpp
new file mode 100644
index 00000000..8ae685f0
--- /dev/null
+++ b/logic/OneSixVersion.cpp
@@ -0,0 +1,340 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logic/OneSixVersion.h"
+#include "logic/OneSixLibrary.h"
+#include "logic/OneSixRule.h"
+
+#include "logger/QsLog.h"
+
+std::shared_ptr<OneSixVersion> fromJsonV4(QJsonObject root,
+ std::shared_ptr<OneSixVersion> fullVersion)
+{
+ fullVersion->id = root.value("id").toString();
+
+ fullVersion->mainClass = root.value("mainClass").toString();
+ auto procArgsValue = root.value("processArguments");
+ if (procArgsValue.isString())
+ {
+ fullVersion->processArguments = procArgsValue.toString();
+ QString toCompare = fullVersion->processArguments.toLower();
+ if (toCompare == "legacy")
+ {
+ fullVersion->minecraftArguments = " ${auth_player_name} ${auth_session}";
+ }
+ else if (toCompare == "username_session")
+ {
+ fullVersion->minecraftArguments =
+ "--username ${auth_player_name} --session ${auth_session}";
+ }
+ else if (toCompare == "username_session_version")
+ {
+ fullVersion->minecraftArguments = "--username ${auth_player_name} "
+ "--session ${auth_session} "
+ "--version ${profile_name}";
+ }
+ }
+
+ auto minecraftArgsValue = root.value("minecraftArguments");
+ if (minecraftArgsValue.isString())
+ {
+ fullVersion->minecraftArguments = minecraftArgsValue.toString();
+ }
+
+ auto minecraftTypeValue = root.value("type");
+ if (minecraftTypeValue.isString())
+ {
+ fullVersion->type = minecraftTypeValue.toString();
+ }
+
+ fullVersion->releaseTime = root.value("releaseTime").toString();
+ fullVersion->time = root.value("time").toString();
+
+ auto assetsID = root.value("assets");
+ if (assetsID.isString())
+ {
+ fullVersion->assets = assetsID.toString();
+ }
+ else
+ {
+ fullVersion->assets = "legacy";
+ }
+
+ QLOG_DEBUG() << "Assets version:" << fullVersion->assets;
+
+ // Iterate through the list, if it's a list.
+ auto librariesValue = root.value("libraries");
+ if (!librariesValue.isArray())
+ return fullVersion;
+
+ QJsonArray libList = root.value("libraries").toArray();
+ for (auto libVal : libList)
+ {
+ if (!libVal.isObject())
+ {
+ continue;
+ }
+
+ QJsonObject libObj = libVal.toObject();
+
+ // Library name
+ auto nameVal = libObj.value("name");
+ if (!nameVal.isString())
+ continue;
+ std::shared_ptr<OneSixLibrary> library(new OneSixLibrary(nameVal.toString()));
+
+ auto urlVal = libObj.value("url");
+ if (urlVal.isString())
+ {
+ library->setBaseUrl(urlVal.toString());
+ }
+ auto hintVal = libObj.value("MMC-hint");
+ if (hintVal.isString())
+ {
+ library->setHint(hintVal.toString());
+ }
+ auto urlAbsVal = libObj.value("MMC-absoluteUrl");
+ auto urlAbsuVal = libObj.value("MMC-absulute_url"); // compatibility
+ if (urlAbsVal.isString())
+ {
+ library->setAbsoluteUrl(urlAbsVal.toString());
+ }
+ else if (urlAbsuVal.isString())
+ {
+ library->setAbsoluteUrl(urlAbsuVal.toString());
+ }
+ // Extract excludes (if any)
+ auto extractVal = libObj.value("extract");
+ if (extractVal.isObject())
+ {
+ QStringList excludes;
+ auto extractObj = extractVal.toObject();
+ auto excludesVal = extractObj.value("exclude");
+ if (excludesVal.isArray())
+ {
+ auto excludesList = excludesVal.toArray();
+ for (auto excludeVal : excludesList)
+ {
+ if (excludeVal.isString())
+ excludes.append(excludeVal.toString());
+ }
+ library->extract_excludes = excludes;
+ }
+ }
+
+ auto nativesVal = libObj.value("natives");
+ if (nativesVal.isObject())
+ {
+ library->setIsNative();
+ auto nativesObj = nativesVal.toObject();
+ auto iter = nativesObj.begin();
+ while (iter != nativesObj.end())
+ {
+ auto osType = OpSys_fromString(iter.key());
+ if (osType == Os_Other)
+ continue;
+ if (!iter.value().isString())
+ continue;
+ library->addNative(osType, iter.value().toString());
+ iter++;
+ }
+ }
+ library->setRules(rulesFromJsonV4(libObj));
+ library->finalize();
+ fullVersion->libraries.append(library);
+ }
+ return fullVersion;
+}
+
+std::shared_ptr<OneSixVersion> OneSixVersion::fromJson(QJsonObject root)
+{
+ std::shared_ptr<OneSixVersion> readVersion(new OneSixVersion());
+ int launcher_ver = readVersion->minimumLauncherVersion =
+ root.value("minimumLauncherVersion").toDouble();
+
+ // ADD MORE HERE :D
+ if (launcher_ver > 0 && launcher_ver <= 13)
+ return fromJsonV4(root, readVersion);
+ else
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+}
+
+std::shared_ptr<OneSixVersion> OneSixVersion::fromFile(QString filepath)
+{
+ QFile file(filepath);
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+
+ auto data = file.readAll();
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ return std::shared_ptr<OneSixVersion>();
+ }
+ QJsonObject root = jsonDoc.object();
+ auto version = fromJson(root);
+ if (version)
+ version->original_file = filepath;
+ return version;
+}
+
+bool OneSixVersion::toOriginalFile()
+{
+ if (original_file.isEmpty())
+ return false;
+ QSaveFile file(original_file);
+ if (!file.open(QIODevice::WriteOnly))
+ {
+ return false;
+ }
+ // serialize base attributes (those we care about anyway)
+ QJsonObject root;
+ root.insert("minecraftArguments", minecraftArguments);
+ root.insert("mainClass", mainClass);
+ root.insert("minimumLauncherVersion", minimumLauncherVersion);
+ root.insert("time", time);
+ root.insert("id", id);
+ root.insert("type", type);
+ // screw processArguments
+ root.insert("releaseTime", releaseTime);
+ QJsonArray libarray;
+ for (const auto &lib : libraries)
+ {
+ libarray.append(lib->toJson());
+ }
+ if (libarray.count())
+ root.insert("libraries", libarray);
+ QJsonDocument doc(root);
+ file.write(doc.toJson());
+ return file.commit();
+}
+
+QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNormalLibs()
+{
+ QList<std::shared_ptr<OneSixLibrary>> output;
+ for (auto lib : libraries)
+ {
+ if (lib->isActive() && !lib->isNative())
+ {
+ output.append(lib);
+ }
+ }
+ return output;
+}
+
+QList<std::shared_ptr<OneSixLibrary>> OneSixVersion::getActiveNativeLibs()
+{
+ QList<std::shared_ptr<OneSixLibrary>> output;
+ for (auto lib : libraries)
+ {
+ if (lib->isActive() && lib->isNative())
+ {
+ output.append(lib);
+ }
+ }
+ return output;
+}
+
+void OneSixVersion::externalUpdateStart()
+{
+ beginResetModel();
+}
+
+void OneSixVersion::externalUpdateFinish()
+{
+ endResetModel();
+}
+
+QVariant OneSixVersion::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ int row = index.row();
+ int column = index.column();
+
+ if (row < 0 || row >= libraries.size())
+ return QVariant();
+
+ if (role == Qt::DisplayRole)
+ {
+ switch (column)
+ {
+ case 0:
+ return libraries[row]->name();
+ case 1:
+ return libraries[row]->type();
+ case 2:
+ return libraries[row]->version();
+ default:
+ return QVariant();
+ }
+ }
+ return QVariant();
+}
+
+Qt::ItemFlags OneSixVersion::flags(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return Qt::NoItemFlags;
+ int row = index.row();
+ if (libraries[row]->isActive())
+ {
+ return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren;
+ }
+ else
+ {
+ return Qt::ItemNeverHasChildren;
+ }
+ // return QAbstractListModel::flags(index);
+}
+
+QVariant OneSixVersion::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
+ return QVariant();
+ switch (section)
+ {
+ case 0:
+ return QString("Name");
+ case 1:
+ return QString("Type");
+ case 2:
+ return QString("Version");
+ default:
+ return QString();
+ }
+}
+
+int OneSixVersion::rowCount(const QModelIndex &parent) const
+{
+ return libraries.size();
+}
+
+int OneSixVersion::columnCount(const QModelIndex &parent) const
+{
+ return 3;
+}
diff --git a/logic/OneSixVersion.h b/logic/OneSixVersion.h
new file mode 100644
index 00000000..036f3d53
--- /dev/null
+++ b/logic/OneSixVersion.h
@@ -0,0 +1,106 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QtCore>
+#include <memory>
+
+class OneSixLibrary;
+
+class OneSixVersion : public QAbstractListModel
+{
+ // Things required to implement the Qt list model
+public:
+ virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+ virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+
+ // serialization/deserialization
+public:
+ bool toOriginalFile();
+ static std::shared_ptr<OneSixVersion> fromJson(QJsonObject root);
+ static std::shared_ptr<OneSixVersion> fromFile(QString filepath);
+
+public:
+ QList<std::shared_ptr<OneSixLibrary>> getActiveNormalLibs();
+ QList<std::shared_ptr<OneSixLibrary>> getActiveNativeLibs();
+ // called when something starts/stops messing with the object
+ // FIXME: these are ugly in every possible way.
+ void externalUpdateStart();
+ void externalUpdateFinish();
+
+ // data members
+public:
+ /// file this was read from. blank, if none
+ QString original_file;
+ /// the ID - determines which jar to use! ACTUALLY IMPORTANT!
+ QString id;
+ /// Last updated time - as a string
+ QString time;
+ /// Release time - as a string
+ QString releaseTime;
+ /// Release type - "release" or "snapshot"
+ QString type;
+ /// Assets type - "legacy" or a version ID
+ QString assets;
+ /**
+ * DEPRECATED: Old versions of the new vanilla launcher used this
+ * ex: "username_session_version"
+ */
+ QString processArguments;
+ /**
+ * arguments that should be used for launching minecraft
+ *
+ * ex: "--username ${auth_player_name} --session ${auth_session}
+ * --version ${version_name} --gameDir ${game_directory} --assetsDir ${game_assets}"
+ */
+ QString minecraftArguments;
+ /**
+ * the minimum launcher version required by this version ... current is 4 (at point of
+ * writing)
+ */
+ int minimumLauncherVersion = 0xDEADBEEF;
+ /**
+ * The main class to load first
+ */
+ QString mainClass;
+
+ /// the list of libs - both active and inactive, native and java
+ QList<std::shared_ptr<OneSixLibrary>> libraries;
+
+ /*
+ FIXME: add support for those rules here? Looks like a pile of quick hacks to me though.
+
+ "rules": [
+ {
+ "action": "allow"
+ },
+ {
+ "action": "disallow",
+ "os": {
+ "name": "osx",
+ "version": "^10\\.5\\.\\d$"
+ }
+ }
+ ],
+ "incompatibilityReason": "There is a bug in LWJGL which makes it incompatible with OSX
+ 10.5.8. Please go to New Profile and use 1.5.2 for now. Sorry!"
+ }
+ */
+ // QList<Rule> rules;
+};
diff --git a/logic/OpSys.cpp b/logic/OpSys.cpp
new file mode 100644
index 00000000..e001b7f3
--- /dev/null
+++ b/logic/OpSys.cpp
@@ -0,0 +1,42 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "OpSys.h"
+
+OpSys OpSys_fromString(QString name)
+{
+ if (name == "linux")
+ return Os_Linux;
+ if (name == "windows")
+ return Os_Windows;
+ if (name == "osx")
+ return Os_OSX;
+ return Os_Other;
+}
+
+QString OpSys_toString(OpSys name)
+{
+ switch (name)
+ {
+ case Os_Linux:
+ return "linux";
+ case Os_OSX:
+ return "osx";
+ case Os_Windows:
+ return "windows";
+ default:
+ return "other";
+ }
+} \ No newline at end of file
diff --git a/logic/OpSys.h b/logic/OpSys.h
new file mode 100644
index 00000000..363c87d7
--- /dev/null
+++ b/logic/OpSys.h
@@ -0,0 +1,37 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+enum OpSys
+{
+ Os_Windows,
+ Os_Linux,
+ Os_OSX,
+ Os_Other
+};
+
+OpSys OpSys_fromString(QString);
+QString OpSys_toString(OpSys);
+
+#ifdef Q_OS_WIN32
+#define currentSystem Os_Windows
+#else
+#ifdef Q_OS_MAC
+#define currentSystem Os_OSX
+#else
+#define currentSystem Os_Linux
+#endif
+#endif \ No newline at end of file
diff --git a/logic/SkinUtils.cpp b/logic/SkinUtils.cpp
new file mode 100644
index 00000000..c6c80006
--- /dev/null
+++ b/logic/SkinUtils.cpp
@@ -0,0 +1,47 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "logic/SkinUtils.h"
+#include "net/HttpMetaCache.h"
+
+#include <QFile>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+
+namespace SkinUtils
+{
+/*
+ * Given a username, return a pixmap of the cached skin (if it exists), QPixmap() otherwise
+ */
+QPixmap getFaceFromCache(QString username, int height, int width)
+{
+ QFile fskin(MMC->metacache()
+ ->resolveEntry("skins", username + ".png")
+ ->getFullPath());
+
+ if (fskin.exists())
+ {
+ QPixmap skin(fskin.fileName());
+ if(!skin.isNull())
+ {
+ return skin.copy(8, 8, 8, 8).scaled(height, width, Qt::KeepAspectRatio);
+ }
+ }
+
+ return QPixmap();
+}
+}
diff --git a/logic/SkinUtils.h b/logic/SkinUtils.h
new file mode 100644
index 00000000..64353b72
--- /dev/null
+++ b/logic/SkinUtils.h
@@ -0,0 +1,23 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QPixmap>
+
+namespace SkinUtils
+{
+QPixmap getFaceFromCache(QString username, int height = 64, int width = 64);
+}
diff --git a/logic/assets/AssetsMigrateTask.cpp b/logic/assets/AssetsMigrateTask.cpp
new file mode 100644
index 00000000..7c1f5204
--- /dev/null
+++ b/logic/assets/AssetsMigrateTask.cpp
@@ -0,0 +1,143 @@
+#include "AssetsMigrateTask.h"
+#include "MultiMC.h"
+#include "logger/QsLog.h"
+#include <QJsonObject>
+#include <QJsonDocument>
+#include <QDirIterator>
+#include <QCryptographicHash>
+#include "gui/dialogs/CustomMessageBox.h"
+#include <QDesktopServices>
+
+AssetsMigrateTask::AssetsMigrateTask(int expected, QObject *parent)
+ : Task(parent)
+{
+ this->m_expected = expected;
+}
+
+void AssetsMigrateTask::executeTask()
+{
+ this->setStatus(tr("Migrating legacy assets..."));
+ this->setProgress(0);
+
+ QDir assets_dir("assets");
+ if (!assets_dir.exists())
+ {
+ emitFailed("Assets directory didn't exist");
+ return;
+ }
+ assets_dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
+ int base_length = assets_dir.path().length();
+
+ QList<QString> blacklist = {"indexes", "objects", "virtual"};
+
+ if (!assets_dir.exists("objects"))
+ assets_dir.mkdir("objects");
+ QDir objects_dir("assets/objects");
+
+ QDirIterator iterator(assets_dir, QDirIterator::Subdirectories);
+ int successes = 0;
+ int failures = 0;
+ while (iterator.hasNext())
+ {
+ QString currentDir = iterator.next();
+ currentDir = currentDir.remove(0, base_length + 1);
+
+ bool ignore = false;
+ for (QString blacklisted : blacklist)
+ {
+ if (currentDir.startsWith(blacklisted))
+ ignore = true;
+ }
+
+ if (!iterator.fileInfo().isDir() && !ignore)
+ {
+ QString filename = iterator.filePath();
+
+ QFile input(filename);
+ input.open(QIODevice::ReadOnly | QIODevice::WriteOnly);
+ QString sha1sum =
+ QCryptographicHash::hash(input.readAll(), QCryptographicHash::Sha1)
+ .toHex()
+ .constData();
+
+ QString object_name = filename.remove(0, base_length + 1);
+ QLOG_DEBUG() << "Processing" << object_name << ":" << sha1sum << input.size();
+
+ QString object_tlk = sha1sum.left(2);
+ QString object_tlk_dir = objects_dir.path() + "/" + object_tlk;
+
+ QDir tlk_dir(object_tlk_dir);
+ if (!tlk_dir.exists())
+ objects_dir.mkdir(object_tlk);
+
+ QString new_filename = tlk_dir.path() + "/" + sha1sum;
+ QFile new_object(new_filename);
+ if (!new_object.exists())
+ {
+ bool rename_success = input.rename(new_filename);
+ QLOG_DEBUG() << " Doesn't exist, copying to" << new_filename << ":"
+ << QString::number(rename_success);
+ if (rename_success)
+ successes++;
+ else
+ failures++;
+ }
+ else
+ {
+ input.remove();
+ QLOG_DEBUG() << " Already exists, deleting original and not copying.";
+ }
+
+ this->setProgress(100 * ((successes + failures) / (float) this->m_expected));
+ }
+ }
+
+ if (successes + failures == 0)
+ {
+ this->setProgress(100);
+ QLOG_DEBUG() << "No legacy assets needed importing.";
+ }
+ else
+ {
+ QLOG_DEBUG() << "Finished copying legacy assets:" << successes << "successes and"
+ << failures << "failures.";
+
+ this->setStatus("Cleaning up legacy assets...");
+ this->setProgress(100);
+
+ QDirIterator cleanup_iterator(assets_dir);
+
+ while (cleanup_iterator.hasNext())
+ {
+ QString currentDir = cleanup_iterator.next();
+ currentDir = currentDir.remove(0, base_length + 1);
+
+ bool ignore = false;
+ for (QString blacklisted : blacklist)
+ {
+ if (currentDir.startsWith(blacklisted))
+ ignore = true;
+ }
+
+ if (cleanup_iterator.fileInfo().isDir() && !ignore)
+ {
+ QString path = cleanup_iterator.filePath();
+ QDir folder(path);
+
+ QLOG_DEBUG() << "Cleaning up legacy assets folder:" << path;
+
+ folder.removeRecursively();
+ }
+ }
+ }
+
+ if(failures > 0)
+ {
+ emitFailed(QString("Failed to migrate %1 legacy assets").arg(failures));
+ }
+ else
+ {
+ emitSucceeded();
+ }
+}
+
diff --git a/logic/assets/AssetsMigrateTask.h b/logic/assets/AssetsMigrateTask.h
new file mode 100644
index 00000000..d8d58c97
--- /dev/null
+++ b/logic/assets/AssetsMigrateTask.h
@@ -0,0 +1,18 @@
+#pragma once
+#include "logic/tasks/Task.h"
+#include <QMessageBox>
+#include <QNetworkReply>
+#include <memory>
+
+class AssetsMigrateTask : public Task
+{
+ Q_OBJECT
+public:
+ explicit AssetsMigrateTask(int expected, QObject* parent=0);
+
+protected:
+ virtual void executeTask();
+
+private:
+ int m_expected;
+};
diff --git a/logic/assets/AssetsUtils.cpp b/logic/assets/AssetsUtils.cpp
new file mode 100644
index 00000000..bca7773d
--- /dev/null
+++ b/logic/assets/AssetsUtils.cpp
@@ -0,0 +1,154 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QDir>
+#include <QDirIterator>
+#include <QCryptographicHash>
+#include <QJsonParseError>
+#include <QJsonDocument>
+#include <QJsonObject>
+
+#include "AssetsUtils.h"
+#include "MultiMC.h"
+
+namespace AssetsUtils
+{
+int findLegacyAssets()
+{
+ QDir assets_dir("assets");
+ if (!assets_dir.exists())
+ return 0;
+ assets_dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
+ int base_length = assets_dir.path().length();
+
+ QList<QString> blacklist = {"indexes", "objects", "virtual"};
+
+ QDirIterator iterator(assets_dir, QDirIterator::Subdirectories);
+ int found = 0;
+ while (iterator.hasNext())
+ {
+ QString currentDir = iterator.next();
+ currentDir = currentDir.remove(0, base_length + 1);
+
+ bool ignore = false;
+ for (QString blacklisted : blacklist)
+ {
+ if (currentDir.startsWith(blacklisted))
+ ignore = true;
+ }
+
+ if (!iterator.fileInfo().isDir() && !ignore)
+ {
+ found++;
+ }
+ }
+
+ return found;
+}
+
+/*
+ * Returns true on success, with index populated
+ * index is undefined otherwise
+ */
+bool loadAssetsIndexJson(QString path, AssetsIndex *index)
+{
+ /*
+ {
+ "objects": {
+ "icons/icon_16x16.png": {
+ "hash": "bdf48ef6b5d0d23bbb02e17d04865216179f510a",
+ "size": 3665
+ },
+ ...
+ }
+ }
+ }
+ */
+
+ QFile file(path);
+
+ // Try to open the file and fail if we can't.
+ // TODO: We should probably report this error to the user.
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ QLOG_ERROR() << "Failed to read assets index file" << path;
+ return false;
+ }
+
+ // Read the file and close it.
+ QByteArray jsonData = file.readAll();
+ file.close();
+
+ QJsonParseError parseError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError);
+
+ // Fail if the JSON is invalid.
+ if (parseError.error != QJsonParseError::NoError)
+ {
+ QLOG_ERROR() << "Failed to parse assets index file:" << parseError.errorString()
+ << "at offset " << QString::number(parseError.offset);
+ return false;
+ }
+
+ // Make sure the root is an object.
+ if (!jsonDoc.isObject())
+ {
+ QLOG_ERROR() << "Invalid assets index JSON: Root should be an array.";
+ return false;
+ }
+
+ QJsonObject root = jsonDoc.object();
+
+ QJsonValue isVirtual = root.value("virtual");
+ if (!isVirtual.isUndefined())
+ {
+ index->isVirtual = isVirtual.toBool(false);
+ }
+
+ QJsonValue objects = root.value("objects");
+ QVariantMap map = objects.toVariant().toMap();
+
+ for (QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter)
+ {
+ // QLOG_DEBUG() << iter.key();
+
+ QVariant variant = iter.value();
+ QVariantMap nested_objects = variant.toMap();
+
+ AssetObject object;
+
+ for (QVariantMap::const_iterator nested_iter = nested_objects.begin();
+ nested_iter != nested_objects.end(); ++nested_iter)
+ {
+ // QLOG_DEBUG() << nested_iter.key() << nested_iter.value().toString();
+ QString key = nested_iter.key();
+ QVariant value = nested_iter.value();
+
+ if (key == "hash")
+ {
+ object.hash = value.toString();
+ }
+ else if (key == "size")
+ {
+ object.size = value.toDouble();
+ }
+ }
+
+ index->objects.insert(iter.key(), object);
+ }
+
+ return true;
+}
+}
diff --git a/logic/assets/AssetsUtils.h b/logic/assets/AssetsUtils.h
new file mode 100644
index 00000000..aaacc2db
--- /dev/null
+++ b/logic/assets/AssetsUtils.h
@@ -0,0 +1,39 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QString>
+#include <QMap>
+
+class AssetObject;
+
+struct AssetObject
+{
+ QString hash;
+ qint64 size;
+};
+
+struct AssetsIndex
+{
+ QMap<QString, AssetObject> objects;
+ bool isVirtual = false;
+};
+
+namespace AssetsUtils
+{
+bool loadAssetsIndexJson(QString file, AssetsIndex* index);
+int findLegacyAssets();
+}
diff --git a/logic/auth/AuthSession.cpp b/logic/auth/AuthSession.cpp
new file mode 100644
index 00000000..8758bfbd
--- /dev/null
+++ b/logic/auth/AuthSession.cpp
@@ -0,0 +1,30 @@
+#include "AuthSession.h"
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QStringList>
+
+QString AuthSession::serializeUserProperties()
+{
+ QJsonObject userAttrs;
+ for (auto key : u.properties.keys())
+ {
+ auto array = QJsonArray::fromStringList(u.properties.values(key));
+ userAttrs.insert(key, array);
+ }
+ QJsonDocument value(userAttrs);
+ return value.toJson(QJsonDocument::Compact);
+
+}
+
+bool AuthSession::MakeOffline(QString offline_playername)
+{
+ if (status != PlayableOffline && status != PlayableOnline)
+ {
+ return false;
+ }
+ session = "-";
+ player_name = offline_playername;
+ status = PlayableOffline;
+ return true;
+}
diff --git a/logic/auth/AuthSession.h b/logic/auth/AuthSession.h
new file mode 100644
index 00000000..2ac170fa
--- /dev/null
+++ b/logic/auth/AuthSession.h
@@ -0,0 +1,49 @@
+#pragma once
+
+#include <QString>
+#include <QMultiMap>
+#include <memory>
+
+struct User
+{
+ QString id;
+ QMultiMap<QString, QString> properties;
+};
+
+struct AuthSession
+{
+ bool MakeOffline(QString offline_playername);
+
+ QString serializeUserProperties();
+
+ enum Status
+ {
+ Undetermined,
+ RequiresPassword,
+ PlayableOffline,
+ PlayableOnline
+ } status = Undetermined;
+
+ User u;
+
+ // client token
+ QString client_token;
+ // account user name
+ QString username;
+ // combined session ID
+ QString session;
+ // volatile auth token
+ QString access_token;
+ // profile name
+ QString player_name;
+ // profile ID
+ QString uuid;
+ // 'legacy' or 'mojang', depending on account type
+ QString user_type;
+ // Did the auth server reply?
+ bool auth_server_online = false;
+ // Did the user request online mode?
+ bool wants_online = true;
+};
+
+typedef std::shared_ptr<AuthSession> AuthSessionPtr;
diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp
new file mode 100644
index 00000000..6c937ef1
--- /dev/null
+++ b/logic/auth/MojangAccount.cpp
@@ -0,0 +1,278 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MojangAccount.h"
+#include "flows/RefreshTask.h"
+#include "flows/AuthenticateTask.h"
+
+#include <QUuid>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QRegExp>
+#include <QStringList>
+#include <QJsonDocument>
+
+#include <logger/QsLog.h>
+
+MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject &object)
+{
+ // The JSON object must at least have a username for it to be valid.
+ if (!object.value("username").isString())
+ {
+ QLOG_ERROR() << "Can't load Mojang account info from JSON object. Username field is "
+ "missing or of the wrong type.";
+ return nullptr;
+ }
+
+ QString username = object.value("username").toString("");
+ QString clientToken = object.value("clientToken").toString("");
+ QString accessToken = object.value("accessToken").toString("");
+
+ QJsonArray profileArray = object.value("profiles").toArray();
+ if (profileArray.size() < 1)
+ {
+ QLOG_ERROR() << "Can't load Mojang account with username \"" << username
+ << "\". No profiles found.";
+ return nullptr;
+ }
+
+ QList<AccountProfile> profiles;
+ for (QJsonValue profileVal : profileArray)
+ {
+ QJsonObject profileObject = profileVal.toObject();
+ QString id = profileObject.value("id").toString("");
+ QString name = profileObject.value("name").toString("");
+ bool legacy = profileObject.value("legacy").toBool(false);
+ if (id.isEmpty() || name.isEmpty())
+ {
+ QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name.";
+ continue;
+ }
+ profiles.append({id, name, legacy});
+ }
+
+ MojangAccountPtr account(new MojangAccount());
+ if (object.value("user").isObject())
+ {
+ User u;
+ QJsonObject userStructure = object.value("user").toObject();
+ u.id = userStructure.value("id").toString();
+ /*
+ QJsonObject propMap = userStructure.value("properties").toObject();
+ for(auto key: propMap.keys())
+ {
+ auto values = propMap.operator[](key).toArray();
+ for(auto value: values)
+ u.properties.insert(key, value.toString());
+ }
+ */
+ account->m_user = u;
+ }
+ account->m_username = username;
+ account->m_clientToken = clientToken;
+ account->m_accessToken = accessToken;
+ account->m_profiles = profiles;
+
+ // Get the currently selected profile.
+ QString currentProfile = object.value("activeProfile").toString("");
+ if (!currentProfile.isEmpty())
+ account->setCurrentProfile(currentProfile);
+
+ return account;
+}
+
+MojangAccountPtr MojangAccount::createFromUsername(const QString &username)
+{
+ MojangAccountPtr account(new MojangAccount());
+ account->m_clientToken = QUuid::createUuid().toString().remove(QRegExp("[{}-]"));
+ account->m_username = username;
+ return account;
+}
+
+QJsonObject MojangAccount::saveToJson() const
+{
+ QJsonObject json;
+ json.insert("username", m_username);
+ json.insert("clientToken", m_clientToken);
+ json.insert("accessToken", m_accessToken);
+
+ QJsonArray profileArray;
+ for (AccountProfile profile : m_profiles)
+ {
+ QJsonObject profileObj;
+ profileObj.insert("id", profile.id);
+ profileObj.insert("name", profile.name);
+ profileObj.insert("legacy", profile.legacy);
+ profileArray.append(profileObj);
+ }
+ json.insert("profiles", profileArray);
+
+ QJsonObject userStructure;
+ {
+ userStructure.insert("id", m_user.id);
+ /*
+ QJsonObject userAttrs;
+ for(auto key: m_user.properties.keys())
+ {
+ auto array = QJsonArray::fromStringList(m_user.properties.values(key));
+ userAttrs.insert(key, array);
+ }
+ userStructure.insert("properties", userAttrs);
+ */
+ }
+ json.insert("user", userStructure);
+
+ if (m_currentProfile != -1)
+ json.insert("activeProfile", currentProfile()->id);
+
+ return json;
+}
+
+bool MojangAccount::setCurrentProfile(const QString &profileId)
+{
+ for (int i = 0; i < m_profiles.length(); i++)
+ {
+ if (m_profiles[i].id == profileId)
+ {
+ m_currentProfile = i;
+ return true;
+ }
+ }
+ return false;
+}
+
+const AccountProfile *MojangAccount::currentProfile() const
+{
+ if (m_currentProfile == -1)
+ return nullptr;
+ return &m_profiles[m_currentProfile];
+}
+
+AccountStatus MojangAccount::accountStatus() const
+{
+ if (m_accessToken.isEmpty())
+ return NotVerified;
+ else
+ return Verified;
+}
+
+std::shared_ptr<YggdrasilTask> MojangAccount::login(AuthSessionPtr session,
+ QString password)
+{
+ Q_ASSERT(m_currentTask.get() == nullptr);
+
+ // take care of the true offline status
+ if (accountStatus() == NotVerified && password.isEmpty())
+ {
+ if (session)
+ {
+ session->status = AuthSession::RequiresPassword;
+ fillSession(session);
+ }
+ return nullptr;
+ }
+
+ if (password.isEmpty())
+ {
+ m_currentTask.reset(new RefreshTask(this));
+ }
+ else
+ {
+ m_currentTask.reset(new AuthenticateTask(this, password));
+ }
+ m_currentTask->assignSession(session);
+
+ connect(m_currentTask.get(), SIGNAL(succeeded()), SLOT(authSucceeded()));
+ connect(m_currentTask.get(), SIGNAL(failed(QString)), SLOT(authFailed(QString)));
+ return m_currentTask;
+}
+
+void MojangAccount::authSucceeded()
+{
+ auto session = m_currentTask->getAssignedSession();
+ if (session)
+ {
+ session->status =
+ session->wants_online ? AuthSession::PlayableOnline : AuthSession::PlayableOffline;
+ fillSession(session);
+ session->auth_server_online = true;
+ }
+ m_currentTask.reset();
+ emit changed();
+}
+
+void MojangAccount::authFailed(QString reason)
+{
+ auto session = m_currentTask->getAssignedSession();
+ // This is emitted when the yggdrasil tasks time out or are cancelled.
+ // -> we treat the error as no-op
+ if (reason == "Yggdrasil task cancelled.")
+ {
+ if (session)
+ {
+ session->status = accountStatus() == Verified ? AuthSession::PlayableOffline
+ : AuthSession::RequiresPassword;
+ session->auth_server_online = false;
+ fillSession(session);
+ }
+ }
+ else
+ {
+ m_accessToken = QString();
+ emit changed();
+ if (session)
+ {
+ session->status = AuthSession::RequiresPassword;
+ session->auth_server_online = true;
+ fillSession(session);
+ }
+ }
+ m_currentTask.reset();
+}
+
+void MojangAccount::fillSession(AuthSessionPtr session)
+{
+ // the user name. you have to have an user name
+ session->username = m_username;
+ // volatile auth token
+ session->access_token = m_accessToken;
+ // the semi-permanent client token
+ session->client_token = m_clientToken;
+ if (currentProfile())
+ {
+ // profile name
+ session->player_name = currentProfile()->name;
+ // profile ID
+ session->uuid = currentProfile()->id;
+ // 'legacy' or 'mojang', depending on account type
+ session->user_type = currentProfile()->legacy ? "legacy" : "mojang";
+ if (!session->access_token.isEmpty())
+ {
+ session->session = "token:" + m_accessToken + ":" + m_profiles[m_currentProfile].id;
+ }
+ else
+ {
+ session->session = "-";
+ }
+ }
+ else
+ {
+ session->player_name = "Player";
+ session->session = "-";
+ }
+ session->u = user();
+}
diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h
new file mode 100644
index 00000000..a0565e2c
--- /dev/null
+++ b/logic/auth/MojangAccount.h
@@ -0,0 +1,171 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QString>
+#include <QList>
+#include <QJsonObject>
+#include <QPair>
+#include <QMap>
+
+#include <memory>
+#include "AuthSession.h"
+
+class Task;
+class YggdrasilTask;
+class MojangAccount;
+
+typedef std::shared_ptr<MojangAccount> MojangAccountPtr;
+Q_DECLARE_METATYPE(MojangAccountPtr)
+
+/**
+ * A profile within someone's Mojang account.
+ *
+ * Currently, the profile system has not been implemented by Mojang yet,
+ * but we might as well add some things for it in MultiMC right now so
+ * we don't have to rip the code to pieces to add it later.
+ */
+struct AccountProfile
+{
+ QString id;
+ QString name;
+ bool legacy;
+};
+
+enum AccountStatus
+{
+ NotVerified,
+ Verified
+};
+
+/**
+ * Object that stores information about a certain Mojang account.
+ *
+ * Said information may include things such as that account's username, client token, and access
+ * token if the user chose to stay logged in.
+ */
+class MojangAccount : public QObject
+{
+ Q_OBJECT
+public: /* construction */
+ //! Do not copy accounts. ever.
+ explicit MojangAccount(const MojangAccount &other, QObject *parent) = delete;
+
+ //! Default constructor
+ explicit MojangAccount(QObject *parent = 0) : QObject(parent) {};
+
+ //! Creates an empty account for the specified user name.
+ static MojangAccountPtr createFromUsername(const QString &username);
+
+ //! Loads a MojangAccount from the given JSON object.
+ static MojangAccountPtr loadFromJson(const QJsonObject &json);
+
+ //! Saves a MojangAccount to a JSON object and returns it.
+ QJsonObject saveToJson() const;
+
+public: /* manipulation */
+ /**
+ * Sets the currently selected profile to the profile with the given ID string.
+ * If profileId is not in the list of available profiles, the function will simply return
+ * false.
+ */
+ bool setCurrentProfile(const QString &profileId);
+
+ /**
+ * Attempt to login. Empty password means we use the token.
+ * If the attempt fails because we already are performing some task, it returns false.
+ */
+ std::shared_ptr<YggdrasilTask> login(AuthSessionPtr session,
+ QString password = QString());
+
+public: /* queries */
+ const QString &username() const
+ {
+ return m_username;
+ }
+
+ const QString &clientToken() const
+ {
+ return m_clientToken;
+ }
+
+ const QString &accessToken() const
+ {
+ return m_accessToken;
+ }
+
+ const QList<AccountProfile> &profiles() const
+ {
+ return m_profiles;
+ }
+
+ const User &user()
+ {
+ return m_user;
+ }
+
+ //! Returns the currently selected profile (if none, returns nullptr)
+ const AccountProfile *currentProfile() const;
+
+ //! Returns whether the account is NotVerified, Verified or Online
+ AccountStatus accountStatus() const;
+
+signals:
+ /**
+ * This signal is emitted when the account changes
+ */
+ void changed();
+
+ // TODO: better signalling for the various possible state changes - especially errors
+
+protected: /* variables */
+ QString m_username;
+
+ // Used to identify the client - the user can have multiple clients for the same account
+ // Think: different launchers, all connecting to the same account/profile
+ QString m_clientToken;
+
+ // Blank if not logged in.
+ QString m_accessToken;
+
+ // Index of the selected profile within the list of available
+ // profiles. -1 if nothing is selected.
+ int m_currentProfile = -1;
+
+ // List of available profiles.
+ QList<AccountProfile> m_profiles;
+
+ // the user structure, whatever it is.
+ User m_user;
+
+ // current task we are executing here
+ std::shared_ptr<YggdrasilTask> m_currentTask;
+
+private
+slots:
+ void authSucceeded();
+ void authFailed(QString reason);
+
+private:
+ void fillSession(AuthSessionPtr session);
+
+public:
+ friend class YggdrasilTask;
+ friend class AuthenticateTask;
+ friend class ValidateTask;
+ friend class RefreshTask;
+};
diff --git a/logic/auth/MojangAccountList.cpp b/logic/auth/MojangAccountList.cpp
new file mode 100644
index 00000000..70bc0cf2
--- /dev/null
+++ b/logic/auth/MojangAccountList.cpp
@@ -0,0 +1,426 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logic/auth/MojangAccountList.h"
+
+#include <QIODevice>
+#include <QFile>
+#include <QTextStream>
+#include <QJsonDocument>
+#include <QJsonArray>
+#include <QJsonObject>
+#include <QJsonParseError>
+#include <QDir>
+
+#include "logger/QsLog.h"
+
+#include "logic/auth/MojangAccount.h"
+#include <pathutils.h>
+
+#define ACCOUNT_LIST_FORMAT_VERSION 2
+
+MojangAccountList::MojangAccountList(QObject *parent) : QAbstractListModel(parent)
+{
+}
+
+MojangAccountPtr MojangAccountList::findAccount(const QString &username) const
+{
+ for (int i = 0; i < count(); i++)
+ {
+ MojangAccountPtr account = at(i);
+ if (account->username() == username)
+ return account;
+ }
+ return nullptr;
+}
+
+const MojangAccountPtr MojangAccountList::at(int i) const
+{
+ return MojangAccountPtr(m_accounts.at(i));
+}
+
+void MojangAccountList::addAccount(const MojangAccountPtr account)
+{
+ beginResetModel();
+ connect(account.get(), SIGNAL(changed()), SLOT(accountChanged()));
+ m_accounts.append(account);
+ endResetModel();
+ onListChanged();
+}
+
+void MojangAccountList::removeAccount(const QString &username)
+{
+ beginResetModel();
+ for (auto account : m_accounts)
+ {
+ if (account->username() == username)
+ {
+ m_accounts.removeOne(account);
+ return;
+ }
+ }
+ endResetModel();
+ onListChanged();
+}
+
+void MojangAccountList::removeAccount(QModelIndex index)
+{
+ beginResetModel();
+ m_accounts.removeAt(index.row());
+ endResetModel();
+ onListChanged();
+}
+
+MojangAccountPtr MojangAccountList::activeAccount() const
+{
+ return m_activeAccount;
+}
+
+void MojangAccountList::setActiveAccount(const QString &username)
+{
+ beginResetModel();
+ if (username.isEmpty())
+ {
+ m_activeAccount = nullptr;
+ }
+ else
+ {
+ for (MojangAccountPtr account : m_accounts)
+ {
+ if (account->username() == username)
+ m_activeAccount = account;
+ }
+ }
+ endResetModel();
+ onActiveChanged();
+}
+
+void MojangAccountList::accountChanged()
+{
+ // the list changed. there is no doubt.
+ onListChanged();
+}
+
+void MojangAccountList::onListChanged()
+{
+ if (m_autosave)
+ // TODO: Alert the user if this fails.
+ saveList();
+
+ emit listChanged();
+}
+
+void MojangAccountList::onActiveChanged()
+{
+ if (m_autosave)
+ saveList();
+
+ emit activeAccountChanged();
+}
+
+int MojangAccountList::count() const
+{
+ return m_accounts.count();
+}
+
+QVariant MojangAccountList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ MojangAccountPtr account = at(index.row());
+
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (index.column())
+ {
+ case NameColumn:
+ return account->username();
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ return account->username();
+
+ case PointerRole:
+ return qVariantFromValue(account);
+
+ case Qt::CheckStateRole:
+ switch (index.column())
+ {
+ case ActiveColumn:
+ return account == m_activeAccount;
+ }
+
+ default:
+ return QVariant();
+ }
+}
+
+QVariant MojangAccountList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (section)
+ {
+ case ActiveColumn:
+ return "Active?";
+
+ case NameColumn:
+ return "Name";
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ switch (section)
+ {
+ case NameColumn:
+ return "The name of the version.";
+
+ default:
+ return QVariant();
+ }
+
+ default:
+ return QVariant();
+ }
+}
+
+int MojangAccountList::rowCount(const QModelIndex &parent) const
+{
+ // Return count
+ return count();
+}
+
+int MojangAccountList::columnCount(const QModelIndex &parent) const
+{
+ return 2;
+}
+
+Qt::ItemFlags MojangAccountList::flags(const QModelIndex &index) const
+{
+ if (index.row() < 0 || index.row() >= rowCount(index) || !index.isValid())
+ {
+ return Qt::NoItemFlags;
+ }
+
+ return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+bool MojangAccountList::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ if (index.row() < 0 || index.row() >= rowCount(index) || !index.isValid())
+ {
+ return false;
+ }
+
+ if(role == Qt::CheckStateRole)
+ {
+ if(value == Qt::Checked)
+ {
+ MojangAccountPtr account = this->at(index.row());
+ this->setActiveAccount(account->username());
+ }
+ }
+
+ emit dataChanged(index, index);
+ return true;
+}
+
+void MojangAccountList::updateListData(QList<MojangAccountPtr> versions)
+{
+ beginResetModel();
+ m_accounts = versions;
+ endResetModel();
+}
+
+bool MojangAccountList::loadList(const QString &filePath)
+{
+ QString path = filePath;
+ if (path.isEmpty())
+ path = m_listFilePath;
+ if (path.isEmpty())
+ {
+ QLOG_ERROR() << "Can't load Mojang account list. No file path given and no default set.";
+ return false;
+ }
+
+ QFile file(path);
+
+ // Try to open the file and fail if we can't.
+ // TODO: We should probably report this error to the user.
+ if (!file.open(QIODevice::ReadOnly))
+ {
+ QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8();
+ return false;
+ }
+
+ // Read the file and close it.
+ QByteArray jsonData = file.readAll();
+ file.close();
+
+ QJsonParseError parseError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError);
+
+ // Fail if the JSON is invalid.
+ if (parseError.error != QJsonParseError::NoError)
+ {
+ QLOG_ERROR() << QString("Failed to parse account list file: %1 at offset %2")
+ .arg(parseError.errorString(), QString::number(parseError.offset))
+ .toUtf8();
+ return false;
+ }
+
+ // Make sure the root is an object.
+ if (!jsonDoc.isObject())
+ {
+ QLOG_ERROR() << "Invalid account list JSON: Root should be an array.";
+ return false;
+ }
+
+ QJsonObject root = jsonDoc.object();
+
+ // Make sure the format version matches.
+ if (root.value("formatVersion").toVariant().toInt() != ACCOUNT_LIST_FORMAT_VERSION)
+ {
+ QString newName = "accounts-old.json";
+ QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to"
+ << newName;
+
+ // Attempt to rename the old version.
+ file.rename(newName);
+ return false;
+ }
+
+ // Now, load the accounts array.
+ beginResetModel();
+ QJsonArray accounts = root.value("accounts").toArray();
+ for (QJsonValue accountVal : accounts)
+ {
+ QJsonObject accountObj = accountVal.toObject();
+ MojangAccountPtr account = MojangAccount::loadFromJson(accountObj);
+ if (account.get() != nullptr)
+ {
+ connect(account.get(), SIGNAL(changed()), SLOT(accountChanged()));
+ m_accounts.append(account);
+ }
+ else
+ {
+ QLOG_WARN() << "Failed to load an account.";
+ }
+ }
+ // Load the active account.
+ m_activeAccount = findAccount(root.value("activeAccount").toString(""));
+ endResetModel();
+ return true;
+}
+
+bool MojangAccountList::saveList(const QString &filePath)
+{
+ QString path(filePath);
+ if (path.isEmpty())
+ path = m_listFilePath;
+ if (path.isEmpty())
+ {
+ QLOG_ERROR() << "Can't save Mojang account list. No file path given and no default set.";
+ return false;
+ }
+
+ // make sure the parent folder exists
+ if(!ensureFilePathExists(path))
+ return false;
+
+ // make sure the file wasn't overwritten with a folder before (fixes a bug)
+ QFileInfo finfo(path);
+ if(finfo.isDir())
+ {
+ QDir badDir(path);
+ badDir.removeRecursively();
+ }
+
+ QLOG_INFO() << "Writing account list to" << path;
+
+ QLOG_DEBUG() << "Building JSON data structure.";
+ // Build the JSON document to write to the list file.
+ QJsonObject root;
+
+ root.insert("formatVersion", ACCOUNT_LIST_FORMAT_VERSION);
+
+ // Build a list of accounts.
+ QLOG_DEBUG() << "Building account array.";
+ QJsonArray accounts;
+ for (MojangAccountPtr account : m_accounts)
+ {
+ QJsonObject accountObj = account->saveToJson();
+ accounts.append(accountObj);
+ }
+
+ // Insert the account list into the root object.
+ root.insert("accounts", accounts);
+
+ if(m_activeAccount)
+ {
+ // Save the active account.
+ root.insert("activeAccount", m_activeAccount->username());
+ }
+
+ // Create a JSON document object to convert our JSON to bytes.
+ QJsonDocument doc(root);
+
+ // Now that we're done building the JSON object, we can write it to the file.
+ QLOG_DEBUG() << "Writing account list to file.";
+ QFile file(path);
+
+ // Try to open the file and fail if we can't.
+ // TODO: We should probably report this error to the user.
+ if (!file.open(QIODevice::WriteOnly))
+ {
+ QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8();
+ return false;
+ }
+
+ // Write the JSON to the file.
+ file.write(doc.toJson());
+ file.close();
+
+ QLOG_INFO() << "Saved account list to" << path;
+
+ return true;
+}
+
+void MojangAccountList::setListFilePath(QString path, bool autosave)
+{
+ m_listFilePath = path;
+ m_autosave = autosave;
+}
+
+bool MojangAccountList::anyAccountIsValid()
+{
+ for(auto account:m_accounts)
+ {
+ if(account->accountStatus() != NotVerified)
+ return true;
+ }
+ return false;
+}
diff --git a/logic/auth/MojangAccountList.h b/logic/auth/MojangAccountList.h
new file mode 100644
index 00000000..6f4fbb17
--- /dev/null
+++ b/logic/auth/MojangAccountList.h
@@ -0,0 +1,199 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QVariant>
+#include <QAbstractListModel>
+#include <QSharedPointer>
+
+#include "logic/auth/MojangAccount.h"
+
+/*!
+ * \brief List of available Mojang accounts.
+ * This should be loaded in the background by MultiMC on startup.
+ *
+ * This class also inherits from QAbstractListModel. Methods from that
+ * class determine how this list shows up in a list view. Said methods
+ * all have a default implementation, but they can be overridden by subclasses to
+ * change the behavior of the list.
+ */
+class MojangAccountList : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ enum ModelRoles
+ {
+ PointerRole = 0x34B1CB48
+ };
+
+ enum VListColumns
+ {
+ // TODO: Add icon column.
+
+ // First column - Active?
+ ActiveColumn = 0,
+
+ // Second column - Name
+ NameColumn,
+ };
+
+ explicit MojangAccountList(QObject *parent = 0);
+
+ //! Gets the account at the given index.
+ virtual const MojangAccountPtr at(int i) const;
+
+ //! Returns the number of accounts in the list.
+ virtual int count() const;
+
+ //////// List Model Functions ////////
+ virtual QVariant data(const QModelIndex &index, int role) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+ virtual int rowCount(const QModelIndex &parent) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+ virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+ virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
+
+ /*!
+ * Adds a the given Mojang account to the account list.
+ */
+ virtual void addAccount(const MojangAccountPtr account);
+
+ /*!
+ * Removes the mojang account with the given username from the account list.
+ */
+ virtual void removeAccount(const QString &username);
+
+ /*!
+ * Removes the account at the given QModelIndex.
+ */
+ virtual void removeAccount(QModelIndex index);
+
+ /*!
+ * \brief Finds an account by its username.
+ * \param The username of the account to find.
+ * \return A const pointer to the account with the given username. NULL if
+ * one doesn't exist.
+ */
+ virtual MojangAccountPtr findAccount(const QString &username) const;
+
+ /*!
+ * Sets the default path to save the list file to.
+ * If autosave is true, this list will automatically save to the given path whenever it changes.
+ * THIS FUNCTION DOES NOT LOAD THE LIST. If you set autosave, be sure to call loadList() immediately
+ * after calling this function to ensure an autosaved change doesn't overwrite the list you intended
+ * to load.
+ */
+ virtual void setListFilePath(QString path, bool autosave = false);
+
+ /*!
+ * \brief Loads the account list from the given file path.
+ * If the given file is an empty string (default), will load from the default account list file.
+ * \return True if successful, otherwise false.
+ */
+ virtual bool loadList(const QString &file = "");
+
+ /*!
+ * \brief Saves the account list to the given file.
+ * If the given file is an empty string (default), will save from the default account list file.
+ * \return True if successful, otherwise false.
+ */
+ virtual bool saveList(const QString &file = "");
+
+ /*!
+ * \brief Gets a pointer to the account that the user has selected as their "active" account.
+ * Which account is active can be overridden on a per-instance basis, but this will return the one that
+ * is set as active globally.
+ * \return The currently active MojangAccount. If there isn't an active account, returns a null pointer.
+ */
+ virtual MojangAccountPtr activeAccount() const;
+
+ /*!
+ * Sets the given account as the current active account.
+ * If the username given is an empty string, sets the active account to nothing.
+ */
+ virtual void setActiveAccount(const QString &username);
+
+ /*!
+ * Returns true if any of the account is at least Validated
+ */
+ bool anyAccountIsValid();
+
+signals:
+ /*!
+ * Signal emitted to indicate that the account list has changed.
+ * This will also fire if the value of an element in the list changes (will be implemented
+ * later).
+ */
+ void listChanged();
+
+ /*!
+ * Signal emitted to indicate that the active account has changed.
+ */
+ void activeAccountChanged();
+
+public
+slots:
+ /**
+ * This is called when one of the accounts changes and the list needs to be updated
+ */
+ void accountChanged();
+
+protected:
+ /*!
+ * Called whenever the list changes.
+ * This emits the listChanged() signal and autosaves the list (if autosave is enabled).
+ */
+ void onListChanged();
+
+ /*!
+ * Called whenever the active account changes.
+ * Emits the activeAccountChanged() signal and autosaves the list if enabled.
+ */
+ void onActiveChanged();
+
+ QList<MojangAccountPtr> m_accounts;
+
+ /*!
+ * Account that is currently active.
+ */
+ MojangAccountPtr m_activeAccount;
+
+ //! Path to the account list file. Empty string if there isn't one.
+ QString m_listFilePath;
+
+ /*!
+ * If true, the account list will automatically save to the account list path when it changes.
+ * Ignored if m_listFilePath is blank.
+ */
+ bool m_autosave = false;
+
+protected
+slots:
+ /*!
+ * Updates this list with the given list of accounts.
+ * This is done by copying each account in the given list and inserting it
+ * into this one.
+ * We need to do this so that we can set the parents of the accounts are set to this
+ * account list. This can't be done in the load task, because the accounts the load
+ * task creates are on the load task's thread and Qt won't allow their parents
+ * to be set to something created on another thread.
+ * To get around that problem, we invoke this method on the GUI thread, which
+ * then copies the accounts and sets their parents correctly.
+ * \param accounts List of accounts whose parents should be set.
+ */
+ virtual void updateListData(QList<MojangAccountPtr> versions);
+};
diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp
new file mode 100644
index 00000000..277d7bfd
--- /dev/null
+++ b/logic/auth/YggdrasilTask.cpp
@@ -0,0 +1,211 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <logic/auth/YggdrasilTask.h>
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+#include <QJsonDocument>
+#include <QNetworkReply>
+#include <QByteArray>
+
+#include <MultiMC.h>
+#include <logic/auth/MojangAccount.h>
+#include <logic/net/URLConstants.h>
+
+YggdrasilTask::YggdrasilTask(MojangAccount *account, QObject *parent)
+ : Task(parent), m_account(account)
+{
+}
+
+void YggdrasilTask::executeTask()
+{
+ setStatus(getStateMessage(STATE_SENDING_REQUEST));
+
+ // Get the content of the request we're going to send to the server.
+ QJsonDocument doc(getRequestContent());
+
+ auto worker = MMC->qnam();
+ QUrl reqUrl("https://" + URLConstants::AUTH_BASE + getEndpoint());
+ QNetworkRequest netRequest(reqUrl);
+ netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
+
+ QByteArray requestData = doc.toJson();
+ m_netReply = worker->post(netRequest, requestData);
+ connect(m_netReply, &QNetworkReply::finished, this, &YggdrasilTask::processReply);
+ connect(m_netReply, &QNetworkReply::uploadProgress, this, &YggdrasilTask::refreshTimers);
+ connect(m_netReply, &QNetworkReply::downloadProgress, this, &YggdrasilTask::refreshTimers);
+ connect(m_netReply, &QNetworkReply::sslErrors, this, &YggdrasilTask::sslErrors);
+ timeout_keeper.setSingleShot(true);
+ timeout_keeper.start(timeout_max);
+ counter.setSingleShot(false);
+ counter.start(time_step);
+ progress(0, timeout_max);
+ connect(&timeout_keeper, &QTimer::timeout, this, &YggdrasilTask::abort);
+ connect(&counter, &QTimer::timeout, this, &YggdrasilTask::heartbeat);
+}
+
+void YggdrasilTask::refreshTimers(qint64, qint64)
+{
+ timeout_keeper.stop();
+ timeout_keeper.start(timeout_max);
+ progress(count = 0, timeout_max);
+}
+void YggdrasilTask::heartbeat()
+{
+ count += time_step;
+ progress(count, timeout_max);
+}
+
+void YggdrasilTask::abort()
+{
+ progress(timeout_max, timeout_max);
+ m_netReply->abort();
+}
+
+void YggdrasilTask::sslErrors(QList<QSslError> errors)
+{
+ int i = 1;
+ for (auto error : errors)
+ {
+ QLOG_ERROR() << "LOGIN SSL Error #" << i << " : " << error.errorString();
+ auto cert = error.certificate();
+ QLOG_ERROR() << "Certificate in question:\n" << cert.toText();
+ i++;
+ }
+}
+
+void YggdrasilTask::processReply()
+{
+ setStatus(getStateMessage(STATE_PROCESSING_RESPONSE));
+
+ if (m_netReply->error() == QNetworkReply::SslHandshakeFailedError)
+ {
+ emitFailed(
+ tr("<b>SSL Handshake failed.</b><br/>There might be a few causes for it:<br/>"
+ "<ul>"
+ "<li>You use Windows XP and need to <a "
+ "href=\"http://www.microsoft.com/en-us/download/details.aspx?id=38918\">update "
+ "your root certificates</a></li>"
+ "<li>Some device on your network is interfering with SSL traffic. In that case, "
+ "you have bigger worries than Minecraft not starting.</li>"
+ "<li>Possibly something else. Check the MultiMC log file for details</li>"
+ "</ul>"));
+ return;
+ }
+
+ // any network errors lead to offline mode right now
+ if (m_netReply->error() >= QNetworkReply::ConnectionRefusedError &&
+ m_netReply->error() <= QNetworkReply::UnknownNetworkError)
+ {
+ // WARNING/FIXME: the value here is used in MojangAccount to detect the cancel/timeout
+ emitFailed("Yggdrasil task cancelled.");
+ QLOG_ERROR() << "Yggdrasil task cancelled because of: " << m_netReply->error() << " : "
+ << m_netReply->errorString();
+ return;
+ }
+
+ // Try to parse the response regardless of the response code.
+ // Sometimes the auth server will give more information and an error code.
+ QJsonParseError jsonError;
+ QByteArray replyData = m_netReply->readAll();
+ QJsonDocument doc = QJsonDocument::fromJson(replyData, &jsonError);
+ // Check the response code.
+ int responseCode = m_netReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+
+ if (responseCode == 200)
+ {
+ // If the response code was 200, then there shouldn't be an error. Make sure
+ // anyways.
+ // Also, sometimes an empty reply indicates success. If there was no data received,
+ // pass an empty json object to the processResponse function.
+ if (jsonError.error == QJsonParseError::NoError || replyData.size() == 0)
+ {
+ if (processResponse(replyData.size() > 0 ? doc.object() : QJsonObject()))
+ {
+ emitSucceeded();
+ return;
+ }
+
+ // errors happened anyway?
+ emitFailed(m_error ? m_error->m_errorMessageVerbose
+ : tr("An unknown error occurred when processing the response "
+ "from the authentication server."));
+ }
+ else
+ {
+ emitFailed(tr("Failed to parse Yggdrasil JSON response: %1 at offset %2.")
+ .arg(jsonError.errorString())
+ .arg(jsonError.offset));
+ }
+ return;
+ }
+
+ // If the response code was not 200, then Yggdrasil may have given us information
+ // about the error.
+ // If we can parse the response, then get information from it. Otherwise just say
+ // there was an unknown error.
+ if (jsonError.error == QJsonParseError::NoError)
+ {
+ // We were able to parse the server's response. Woo!
+ // Call processError. If a subclass has overridden it then they'll handle their
+ // stuff there.
+ QLOG_DEBUG() << "The request failed, but the server gave us an error message. "
+ "Processing error.";
+ emitFailed(processError(doc.object()));
+ }
+ else
+ {
+ // The server didn't say anything regarding the error. Give the user an unknown
+ // error.
+ QLOG_DEBUG() << "The request failed and the server gave no error message. "
+ "Unknown error.";
+ emitFailed(tr("An unknown error occurred when trying to communicate with the "
+ "authentication server: %1").arg(m_netReply->errorString()));
+ }
+}
+
+QString YggdrasilTask::processError(QJsonObject responseData)
+{
+ QJsonValue errorVal = responseData.value("error");
+ QJsonValue errorMessageValue = responseData.value("errorMessage");
+ QJsonValue causeVal = responseData.value("cause");
+
+ if (errorVal.isString() && errorMessageValue.isString())
+ {
+ m_error = std::shared_ptr<Error>(new Error{
+ errorVal.toString(""), errorMessageValue.toString(""), causeVal.toString("")});
+ return m_error->m_errorMessageVerbose;
+ }
+ else
+ {
+ // Error is not in standard format. Don't set m_error and return unknown error.
+ return tr("An unknown Yggdrasil error occurred.");
+ }
+}
+
+QString YggdrasilTask::getStateMessage(const YggdrasilTask::State state) const
+{
+ switch (state)
+ {
+ case STATE_SENDING_REQUEST:
+ return tr("Sending request to auth servers...");
+ case STATE_PROCESSING_RESPONSE:
+ return tr("Processing response from servers...");
+ default:
+ return tr("Processing. Please wait...");
+ }
+}
diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h
new file mode 100644
index 00000000..4a87067a
--- /dev/null
+++ b/logic/auth/YggdrasilTask.h
@@ -0,0 +1,137 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <logic/tasks/Task.h>
+
+#include <QString>
+#include <QJsonObject>
+#include <QTimer>
+#include <qsslerror.h>
+
+#include "logic/auth/MojangAccount.h"
+
+class QNetworkReply;
+
+/**
+ * A Yggdrasil task is a task that performs an operation on a given mojang account.
+ */
+class YggdrasilTask : public Task
+{
+ Q_OBJECT
+public:
+ explicit YggdrasilTask(MojangAccount * account, QObject *parent = 0);
+
+ /**
+ * assign a session to this task. the session will be filled with required infomration
+ * upon completion
+ */
+ void assignSession(AuthSessionPtr session)
+ {
+ m_session = session;
+ }
+
+ /// get the assigned session for filling with information.
+ AuthSessionPtr getAssignedSession()
+ {
+ return m_session;
+ }
+
+ /**
+ * Class describing a Yggdrasil error response.
+ */
+ struct Error
+ {
+ QString m_errorMessageShort;
+ QString m_errorMessageVerbose;
+ QString m_cause;
+ };
+
+protected:
+ /**
+ * Enum for describing the state of the current task.
+ * Used by the getStateMessage function to determine what the status message should be.
+ */
+ enum State
+ {
+ STATE_SENDING_REQUEST,
+ STATE_PROCESSING_RESPONSE,
+ STATE_OTHER,
+ };
+
+ virtual void executeTask();
+
+ /**
+ * Gets the JSON object that will be sent to the authentication server.
+ * Should be overridden by subclasses.
+ */
+ virtual QJsonObject getRequestContent() const = 0;
+
+ /**
+ * Gets the endpoint to POST to.
+ * No leading slash.
+ */
+ virtual QString getEndpoint() const = 0;
+
+ /**
+ * Processes the response received from the server.
+ * If an error occurred, this should emit a failed signal and return false.
+ * If Yggdrasil gave an error response, it should call setError() first, and then return false.
+ * Otherwise, it should return true.
+ * Note: If the response from the server was blank, and the HTTP code was 200, this function is called with
+ * an empty QJsonObject.
+ */
+ virtual bool processResponse(QJsonObject responseData) = 0;
+
+ /**
+ * Processes an error response received from the server.
+ * The default implementation will read data from Yggdrasil's standard error response format and set it as this task's Error.
+ * \returns a QString error message that will be passed to emitFailed.
+ */
+ virtual QString processError(QJsonObject responseData);
+
+ /**
+ * Returns the state message for the given state.
+ * Used to set the status message for the task.
+ * Should be overridden by subclasses that want to change messages for a given state.
+ */
+ virtual QString getStateMessage(const State state) const;
+
+protected
+slots:
+ void processReply();
+ void refreshTimers(qint64, qint64);
+ void heartbeat();
+ void sslErrors(QList<QSslError>);
+
+public
+slots:
+ virtual void abort() override;
+
+protected:
+ // FIXME: segfault disaster waiting to happen
+ MojangAccount *m_account = nullptr;
+ QNetworkReply *m_netReply = nullptr;
+ std::shared_ptr<Error> m_error;
+ QTimer timeout_keeper;
+ QTimer counter;
+ int count = 0; // num msec since time reset
+
+ const int timeout_max = 10000;
+ const int time_step = 50;
+
+ AuthSessionPtr m_session;
+};
diff --git a/logic/auth/flows/AuthenticateTask.cpp b/logic/auth/flows/AuthenticateTask.cpp
new file mode 100644
index 00000000..6548c4e9
--- /dev/null
+++ b/logic/auth/flows/AuthenticateTask.cpp
@@ -0,0 +1,203 @@
+
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <logic/auth/flows/AuthenticateTask.h>
+
+#include <logic/auth/MojangAccount.h>
+
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QVariant>
+#include <QDebug>
+
+#include "logger/QsLog.h"
+
+AuthenticateTask::AuthenticateTask(MojangAccount * account, const QString &password,
+ QObject *parent)
+ : YggdrasilTask(account, parent), m_password(password)
+{
+}
+
+QJsonObject AuthenticateTask::getRequestContent() const
+{
+ /*
+ * {
+ * "agent": { // optional
+ * "name": "Minecraft", // So far this is the only encountered value
+ * "version": 1 // This number might be increased
+ * // by the vanilla client in the future
+ * },
+ * "username": "mojang account name", // Can be an email address or player name for
+ // unmigrated accounts
+ * "password": "mojang account password",
+ * "clientToken": "client identifier" // optional
+ * "requestUser": true/false // request the user structure
+ * }
+ */
+ QJsonObject req;
+
+ {
+ QJsonObject agent;
+ // C++ makes string literals void* for some stupid reason, so we have to tell it
+ // QString... Thanks Obama.
+ agent.insert("name", QString("Minecraft"));
+ agent.insert("version", 1);
+ req.insert("agent", agent);
+ }
+
+ req.insert("username", m_account->username());
+ req.insert("password", m_password);
+ req.insert("requestUser", true);
+
+ // If we already have a client token, give it to the server.
+ // Otherwise, let the server give us one.
+ if (!m_account->m_clientToken.isEmpty())
+ req.insert("clientToken", m_account->m_clientToken);
+
+ return req;
+}
+
+bool AuthenticateTask::processResponse(QJsonObject responseData)
+{
+ // Read the response data. We need to get the client token, access token, and the selected
+ // profile.
+ QLOG_DEBUG() << "Processing authentication response.";
+ // QLOG_DEBUG() << responseData;
+ // If we already have a client token, make sure the one the server gave us matches our
+ // existing one.
+ QLOG_DEBUG() << "Getting client token.";
+ QString clientToken = responseData.value("clientToken").toString("");
+ if (clientToken.isEmpty())
+ {
+ // Fail if the server gave us an empty client token
+ // TODO: Set an error properly to display to the user.
+ QLOG_ERROR() << "Server didn't send a client token.";
+ return false;
+ }
+ if (!m_account->m_clientToken.isEmpty() && clientToken != m_account->m_clientToken)
+ {
+ // The server changed our client token! Obey its wishes, but complain. That's what I do
+ // for my parents, so...
+ QLOG_WARN() << "Server changed our client token to '" << clientToken
+ << "'. This shouldn't happen, but it isn't really a big deal.";
+ }
+ // Set the client token.
+ m_account->m_clientToken = clientToken;
+
+ // Now, we set the access token.
+ QLOG_DEBUG() << "Getting access token.";
+ QString accessToken = responseData.value("accessToken").toString("");
+ if (accessToken.isEmpty())
+ {
+ // Fail if the server didn't give us an access token.
+ // TODO: Set an error properly to display to the user.
+ QLOG_ERROR() << "Server didn't send an access token.";
+ }
+ // Set the access token.
+ m_account->m_accessToken = accessToken;
+
+ // Now we load the list of available profiles.
+ // Mojang hasn't yet implemented the profile system,
+ // but we might as well support what's there so we
+ // don't have trouble implementing it later.
+ QLOG_DEBUG() << "Loading profile list.";
+ QJsonArray availableProfiles = responseData.value("availableProfiles").toArray();
+ QList<AccountProfile> loadedProfiles;
+ for (auto iter : availableProfiles)
+ {
+ QJsonObject profile = iter.toObject();
+ // Profiles are easy, we just need their ID and name.
+ QString id = profile.value("id").toString("");
+ QString name = profile.value("name").toString("");
+ bool legacy = profile.value("legacy").toBool(false);
+
+ if (id.isEmpty() || name.isEmpty())
+ {
+ // This should never happen, but we might as well
+ // warn about it if it does so we can debug it easily.
+ // You never know when Mojang might do something truly derpy.
+ QLOG_WARN() << "Found entry in available profiles list with missing ID or name "
+ "field. Ignoring it.";
+ }
+
+ // Now, add a new AccountProfile entry to the list.
+ loadedProfiles.append({id, name, legacy});
+ }
+ // Put the list of profiles we loaded into the MojangAccount object.
+ m_account->m_profiles = loadedProfiles;
+
+ // Finally, we set the current profile to the correct value. This is pretty simple.
+ // We do need to make sure that the current profile that the server gave us
+ // is actually in the available profiles list.
+ // If it isn't, we'll just fail horribly (*shouldn't* ever happen, but you never know).
+ QLOG_DEBUG() << "Setting current profile.";
+ QJsonObject currentProfile = responseData.value("selectedProfile").toObject();
+ QString currentProfileId = currentProfile.value("id").toString("");
+ if (currentProfileId.isEmpty())
+ {
+ // TODO: Set an error to display to the user.
+ QLOG_ERROR() << "Server didn't specify a currently selected profile.";
+ return false;
+ }
+ if (!m_account->setCurrentProfile(currentProfileId))
+ {
+ // TODO: Set an error to display to the user.
+ QLOG_ERROR() << "Server specified a selected profile that wasn't in the available "
+ "profiles list.";
+ return false;
+ }
+
+ // this is what the vanilla launcher passes to the userProperties launch param
+ if (responseData.contains("user"))
+ {
+ User u;
+ auto obj = responseData.value("user").toObject();
+ u.id = obj.value("id").toString();
+ auto propArray = obj.value("properties").toArray();
+ for (auto prop : propArray)
+ {
+ auto propTuple = prop.toObject();
+ auto name = propTuple.value("name").toString();
+ auto value = propTuple.value("value").toString();
+ u.properties.insert(name, value);
+ }
+ m_account->m_user = u;
+ }
+
+ // We've made it through the minefield of possible errors. Return true to indicate that
+ // we've succeeded.
+ QLOG_DEBUG() << "Finished reading authentication response.";
+ return true;
+}
+
+QString AuthenticateTask::getEndpoint() const
+{
+ return "authenticate";
+}
+
+QString AuthenticateTask::getStateMessage(const YggdrasilTask::State state) const
+{
+ switch (state)
+ {
+ case STATE_SENDING_REQUEST:
+ return tr("Authenticating: Sending request...");
+ case STATE_PROCESSING_RESPONSE:
+ return tr("Authenticating: Processing response...");
+ default:
+ return YggdrasilTask::getStateMessage(state);
+ }
+}
diff --git a/logic/auth/flows/AuthenticateTask.h b/logic/auth/flows/AuthenticateTask.h
new file mode 100644
index 00000000..b6564657
--- /dev/null
+++ b/logic/auth/flows/AuthenticateTask.h
@@ -0,0 +1,46 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <logic/auth/YggdrasilTask.h>
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+
+/**
+ * The authenticate task takes a MojangAccount with no access token and password and attempts to
+ * authenticate with Mojang's servers.
+ * If successful, it will set the MojangAccount's access token.
+ */
+class AuthenticateTask : public YggdrasilTask
+{
+ Q_OBJECT
+public:
+ AuthenticateTask(MojangAccount *account, const QString &password, QObject *parent = 0);
+
+protected:
+ virtual QJsonObject getRequestContent() const;
+
+ virtual QString getEndpoint() const;
+
+ virtual bool processResponse(QJsonObject responseData);
+
+ QString getStateMessage(const YggdrasilTask::State state) const;
+
+private:
+ QString m_password;
+};
diff --git a/logic/auth/flows/RefreshTask.cpp b/logic/auth/flows/RefreshTask.cpp
new file mode 100644
index 00000000..5a55ed91
--- /dev/null
+++ b/logic/auth/flows/RefreshTask.cpp
@@ -0,0 +1,152 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <logic/auth/flows/RefreshTask.h>
+
+#include <logic/auth/MojangAccount.h>
+
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QVariant>
+#include <QDebug>
+
+#include "logger/QsLog.h"
+
+RefreshTask::RefreshTask(MojangAccount *account) : YggdrasilTask(account)
+{
+}
+
+QJsonObject RefreshTask::getRequestContent() const
+{
+ /*
+ * {
+ * "clientToken": "client identifier"
+ * "accessToken": "current access token to be refreshed"
+ * "selectedProfile": // specifying this causes errors
+ * {
+ * "id": "profile ID"
+ * "name": "profile name"
+ * }
+ * "requestUser": true/false // request the user structure
+ * }
+ */
+ QJsonObject req;
+ req.insert("clientToken", m_account->m_clientToken);
+ req.insert("accessToken", m_account->m_accessToken);
+ /*
+ {
+ auto currentProfile = m_account->currentProfile();
+ QJsonObject profile;
+ profile.insert("id", currentProfile->id());
+ profile.insert("name", currentProfile->name());
+ req.insert("selectedProfile", profile);
+ }
+ */
+ req.insert("requestUser", true);
+
+ return req;
+}
+
+bool RefreshTask::processResponse(QJsonObject responseData)
+{
+ // Read the response data. We need to get the client token, access token, and the selected
+ // profile.
+ QLOG_DEBUG() << "Processing authentication response.";
+
+ // QLOG_DEBUG() << responseData;
+ // If we already have a client token, make sure the one the server gave us matches our
+ // existing one.
+ QString clientToken = responseData.value("clientToken").toString("");
+ if (clientToken.isEmpty())
+ {
+ // Fail if the server gave us an empty client token
+ // TODO: Set an error properly to display to the user.
+ QLOG_ERROR() << "Server didn't send a client token.";
+ return false;
+ }
+ if (!m_account->m_clientToken.isEmpty() && clientToken != m_account->m_clientToken)
+ {
+ // The server changed our client token! Obey its wishes, but complain. That's what I do
+ // for my parents, so...
+ QLOG_ERROR() << "Server changed our client token to '" << clientToken
+ << "'. This shouldn't happen, but it isn't really a big deal.";
+ return false;
+ }
+
+ // Now, we set the access token.
+ QLOG_DEBUG() << "Getting new access token.";
+ QString accessToken = responseData.value("accessToken").toString("");
+ if (accessToken.isEmpty())
+ {
+ // Fail if the server didn't give us an access token.
+ // TODO: Set an error properly to display to the user.
+ QLOG_ERROR() << "Server didn't send an access token.";
+ return false;
+ }
+
+ // we validate that the server responded right. (our current profile = returned current
+ // profile)
+ QJsonObject currentProfile = responseData.value("selectedProfile").toObject();
+ QString currentProfileId = currentProfile.value("id").toString("");
+ if (m_account->currentProfile()->id != currentProfileId)
+ {
+ // TODO: Set an error to display to the user.
+ QLOG_ERROR() << "Server didn't specify the same selected profile as ours.";
+ return false;
+ }
+
+ // this is what the vanilla launcher passes to the userProperties launch param
+ if (responseData.contains("user"))
+ {
+ User u;
+ auto obj = responseData.value("user").toObject();
+ u.id = obj.value("id").toString();
+ auto propArray = obj.value("properties").toArray();
+ for (auto prop : propArray)
+ {
+ auto propTuple = prop.toObject();
+ auto name = propTuple.value("name").toString();
+ auto value = propTuple.value("value").toString();
+ u.properties.insert(name, value);
+ }
+ m_account->m_user = u;
+ }
+
+ // We've made it through the minefield of possible errors. Return true to indicate that
+ // we've succeeded.
+ QLOG_DEBUG() << "Finished reading refresh response.";
+ // Reset the access token.
+ m_account->m_accessToken = accessToken;
+ return true;
+}
+
+QString RefreshTask::getEndpoint() const
+{
+ return "refresh";
+}
+
+QString RefreshTask::getStateMessage(const YggdrasilTask::State state) const
+{
+ switch (state)
+ {
+ case STATE_SENDING_REQUEST:
+ return tr("Refreshing login token...");
+ case STATE_PROCESSING_RESPONSE:
+ return tr("Refreshing login token: Processing response...");
+ default:
+ return YggdrasilTask::getStateMessage(state);
+ }
+}
diff --git a/logic/auth/flows/RefreshTask.h b/logic/auth/flows/RefreshTask.h
new file mode 100644
index 00000000..0dadc025
--- /dev/null
+++ b/logic/auth/flows/RefreshTask.h
@@ -0,0 +1,44 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <logic/auth/YggdrasilTask.h>
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+
+/**
+ * The authenticate task takes a MojangAccount with a possibly timed-out access token
+ * and attempts to authenticate with Mojang's servers.
+ * If successful, it will set the new access token. The token is considered validated.
+ */
+class RefreshTask : public YggdrasilTask
+{
+ Q_OBJECT
+public:
+ RefreshTask(MojangAccount * account);
+
+protected:
+ virtual QJsonObject getRequestContent() const;
+
+ virtual QString getEndpoint() const;
+
+ virtual bool processResponse(QJsonObject responseData);
+
+ QString getStateMessage(const YggdrasilTask::State state) const;
+};
+
diff --git a/logic/auth/flows/ValidateTask.cpp b/logic/auth/flows/ValidateTask.cpp
new file mode 100644
index 00000000..4f7323fd
--- /dev/null
+++ b/logic/auth/flows/ValidateTask.cpp
@@ -0,0 +1,64 @@
+
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <logic/auth/flows/ValidateTask.h>
+
+#include <logic/auth/MojangAccount.h>
+
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QVariant>
+#include <QDebug>
+
+#include "logger/QsLog.h"
+
+ValidateTask::ValidateTask(MojangAccount * account, QObject *parent)
+ : YggdrasilTask(account, parent)
+{
+}
+
+QJsonObject ValidateTask::getRequestContent() const
+{
+ QJsonObject req;
+ req.insert("accessToken", m_account->m_accessToken);
+ return req;
+}
+
+bool ValidateTask::processResponse(QJsonObject responseData)
+{
+ // Assume that if processError wasn't called, then the request was successful.
+ emitSucceeded();
+ return true;
+}
+
+QString ValidateTask::getEndpoint() const
+{
+ return "validate";
+}
+
+QString ValidateTask::getStateMessage(const YggdrasilTask::State state) const
+{
+ switch (state)
+ {
+ case STATE_SENDING_REQUEST:
+ return tr("Validating access token: Sending request...");
+ case STATE_PROCESSING_RESPONSE:
+ return tr("Validating access token: Processing response...");
+ default:
+ return YggdrasilTask::getStateMessage(state);
+ }
+}
diff --git a/logic/auth/flows/ValidateTask.h b/logic/auth/flows/ValidateTask.h
new file mode 100644
index 00000000..0e34f0c3
--- /dev/null
+++ b/logic/auth/flows/ValidateTask.h
@@ -0,0 +1,47 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * :FIXME: DEAD CODE, DEAD CODE, DEAD CODE! :FIXME:
+ */
+
+#pragma once
+
+#include <logic/auth/YggdrasilTask.h>
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+
+/**
+ * The validate task takes a MojangAccount and checks to make sure its access token is valid.
+ */
+class ValidateTask : public YggdrasilTask
+{
+ Q_OBJECT
+public:
+ ValidateTask(MojangAccount *account, QObject *parent = 0);
+
+protected:
+ virtual QJsonObject getRequestContent() const;
+
+ virtual QString getEndpoint() const;
+
+ virtual bool processResponse(QJsonObject responseData);
+
+ QString getStateMessage(const YggdrasilTask::State state) const;
+
+private:
+};
diff --git a/logic/icons/IconList.cpp b/logic/icons/IconList.cpp
new file mode 100644
index 00000000..d76e6fbb
--- /dev/null
+++ b/logic/icons/IconList.cpp
@@ -0,0 +1,368 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "IconList.h"
+#include <pathutils.h>
+#include <settingsobject.h>
+#include <QMap>
+#include <QEventLoop>
+#include <QMimeData>
+#include <QUrl>
+#include <QFileSystemWatcher>
+#include <MultiMC.h>
+#include <setting.h>
+
+#define MAX_SIZE 1024
+
+IconList::IconList(QObject *parent) : QAbstractListModel(parent)
+{
+ // add builtin icons
+ QDir instance_icons(":/icons/instances/");
+ auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
+ for (auto file_info : file_info_list)
+ {
+ QString key = file_info.baseName();
+ addIcon(key, key, file_info.absoluteFilePath(), MMCIcon::Builtin);
+ }
+
+ m_watcher.reset(new QFileSystemWatcher());
+ is_watching = false;
+ connect(m_watcher.get(), SIGNAL(directoryChanged(QString)),
+ SLOT(directoryChanged(QString)));
+ connect(m_watcher.get(), SIGNAL(fileChanged(QString)), SLOT(fileChanged(QString)));
+
+ auto setting = MMC->settings()->getSetting("IconsDir");
+ QString path = setting->get().toString();
+ connect(setting.get(), SIGNAL(settingChanged(const Setting &, QVariant)),
+ SLOT(settingChanged(const Setting &, QVariant)));
+ directoryChanged(path);
+}
+
+void IconList::directoryChanged(const QString &path)
+{
+ QDir new_dir (path);
+ if(m_dir.absolutePath() != new_dir.absolutePath())
+ {
+ m_dir.setPath(path);
+ m_dir.refresh();
+ if(is_watching)
+ stopWatching();
+ startWatching();
+ }
+ if(!m_dir.exists())
+ if(!ensureFolderPathExists(m_dir.absolutePath()))
+ return;
+ m_dir.refresh();
+ auto new_list = m_dir.entryList(QDir::Files, QDir::Name);
+ for (auto it = new_list.begin(); it != new_list.end(); it++)
+ {
+ QString &foo = (*it);
+ foo = m_dir.filePath(foo);
+ }
+ auto new_set = new_list.toSet();
+ QList<QString> current_list;
+ for (auto &it : icons)
+ {
+ if (!it.has(MMCIcon::FileBased))
+ continue;
+ current_list.push_back(it.m_images[MMCIcon::FileBased].filename);
+ }
+ QSet<QString> current_set = current_list.toSet();
+
+ QSet<QString> to_remove = current_set;
+ to_remove -= new_set;
+
+ QSet<QString> to_add = new_set;
+ to_add -= current_set;
+
+ for (auto remove : to_remove)
+ {
+ QLOG_INFO() << "Removing " << remove;
+ QFileInfo rmfile(remove);
+ QString key = rmfile.baseName();
+ int idx = getIconIndex(key);
+ if (idx == -1)
+ continue;
+ icons[idx].remove(MMCIcon::FileBased);
+ if (icons[idx].type() == MMCIcon::ToBeDeleted)
+ {
+ beginRemoveRows(QModelIndex(), idx, idx);
+ icons.remove(idx);
+ reindex();
+ endRemoveRows();
+ }
+ else
+ {
+ dataChanged(index(idx), index(idx));
+ }
+ m_watcher->removePath(remove);
+ emit iconUpdated(key);
+ }
+
+ for (auto add : to_add)
+ {
+ QLOG_INFO() << "Adding " << add;
+ QFileInfo addfile(add);
+ QString key = addfile.baseName();
+ if (addIcon(key, QString(), addfile.filePath(), MMCIcon::FileBased))
+ {
+ m_watcher->addPath(add);
+ emit iconUpdated(key);
+ }
+ }
+}
+
+void IconList::fileChanged(const QString &path)
+{
+ QLOG_INFO() << "Checking " << path;
+ QFileInfo checkfile(path);
+ if (!checkfile.exists())
+ return;
+ QString key = checkfile.baseName();
+ int idx = getIconIndex(key);
+ if (idx == -1)
+ return;
+ QIcon icon(path);
+ if (!icon.availableSizes().size())
+ return;
+
+ icons[idx].m_images[MMCIcon::FileBased].icon = icon;
+ dataChanged(index(idx), index(idx));
+ emit iconUpdated(key);
+}
+
+void IconList::settingChanged(const Setting &setting, QVariant value)
+{
+ if(setting.id() != "IconsDir")
+ return;
+
+ directoryChanged(value.toString());
+}
+
+void IconList::startWatching()
+{
+ auto abs_path = m_dir.absolutePath();
+ ensureFolderPathExists(abs_path);
+ is_watching = m_watcher->addPath(abs_path);
+ if (is_watching)
+ {
+ QLOG_INFO() << "Started watching " << abs_path;
+ }
+ else
+ {
+ QLOG_INFO() << "Failed to start watching " << abs_path;
+ }
+}
+
+void IconList::stopWatching()
+{
+ m_watcher->removePaths(m_watcher->files());
+ m_watcher->removePaths(m_watcher->directories());
+ is_watching = false;
+}
+
+QStringList IconList::mimeTypes() const
+{
+ QStringList types;
+ types << "text/uri-list";
+ return types;
+}
+Qt::DropActions IconList::supportedDropActions() const
+{
+ return Qt::CopyAction;
+}
+
+bool IconList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
+ const QModelIndex &parent)
+{
+ if (action == Qt::IgnoreAction)
+ return true;
+ // check if the action is supported
+ if (!data || !(action & supportedDropActions()))
+ return false;
+
+ // files dropped from outside?
+ if (data->hasUrls())
+ {
+ auto urls = data->urls();
+ QStringList iconFiles;
+ for (auto url : urls)
+ {
+ // only local files may be dropped...
+ if (!url.isLocalFile())
+ continue;
+ iconFiles += url.toLocalFile();
+ }
+ installIcons(iconFiles);
+ return true;
+ }
+ return false;
+}
+
+Qt::ItemFlags IconList::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
+ if (index.isValid())
+ return Qt::ItemIsDropEnabled | defaultFlags;
+ else
+ return Qt::ItemIsDropEnabled | defaultFlags;
+}
+
+QVariant IconList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ int row = index.row();
+
+ if (row < 0 || row >= icons.size())
+ return QVariant();
+
+ switch (role)
+ {
+ case Qt::DecorationRole:
+ return icons[row].icon();
+ case Qt::DisplayRole:
+ return icons[row].name();
+ case Qt::UserRole:
+ return icons[row].m_key;
+ default:
+ return QVariant();
+ }
+}
+
+int IconList::rowCount(const QModelIndex &parent) const
+{
+ return icons.size();
+}
+
+void IconList::installIcons(QStringList iconFiles)
+{
+ for (QString file : iconFiles)
+ {
+ QFileInfo fileinfo(file);
+ if (!fileinfo.isReadable() || !fileinfo.isFile())
+ continue;
+ QString target = PathCombine("icons", fileinfo.fileName());
+
+ QString suffix = fileinfo.suffix();
+ if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico")
+ continue;
+
+ if (!QFile::copy(file, target))
+ continue;
+ }
+}
+
+bool IconList::deleteIcon(QString key)
+{
+ int iconIdx = getIconIndex(key);
+ if (iconIdx == -1)
+ return false;
+ auto &iconEntry = icons[iconIdx];
+ if (iconEntry.has(MMCIcon::FileBased))
+ {
+ return QFile::remove(iconEntry.m_images[MMCIcon::FileBased].filename);
+ }
+ return false;
+}
+
+bool IconList::addIcon(QString key, QString name, QString path, MMCIcon::Type type)
+{
+ // replace the icon even? is the input valid?
+ QIcon icon(path);
+ if (!icon.availableSizes().size())
+ return false;
+ auto iter = name_index.find(key);
+ if (iter != name_index.end())
+ {
+ auto &oldOne = icons[*iter];
+ oldOne.replace(type, icon, path);
+ dataChanged(index(*iter), index(*iter));
+ return true;
+ }
+ else
+ {
+ // add a new icon
+ beginInsertRows(QModelIndex(), icons.size(), icons.size());
+ {
+ MMCIcon mmc_icon;
+ mmc_icon.m_name = name;
+ mmc_icon.m_key = key;
+ mmc_icon.replace(type, icon, path);
+ icons.push_back(mmc_icon);
+ name_index[key] = icons.size() - 1;
+ }
+ endInsertRows();
+ return true;
+ }
+}
+
+void IconList::reindex()
+{
+ name_index.clear();
+ int i = 0;
+ for (auto &iter : icons)
+ {
+ name_index[iter.m_key] = i;
+ i++;
+ }
+}
+
+QIcon IconList::getIcon(QString key)
+{
+ int icon_index = getIconIndex(key);
+
+ if (icon_index != -1)
+ return icons[icon_index].icon();
+
+ // Fallback for icons that don't exist.
+ icon_index = getIconIndex("infinity");
+
+ if (icon_index != -1)
+ return icons[icon_index].icon();
+ return QIcon();
+}
+
+QIcon IconList::getBigIcon(QString key)
+{
+ int icon_index = getIconIndex(key);
+
+ if (icon_index == -1)
+ key = "infinity";
+
+ // Fallback for icons that don't exist.
+ icon_index = getIconIndex(key);
+
+ if (icon_index == -1)
+ return QIcon();
+
+ QPixmap bigone = icons[icon_index].icon().pixmap(256,256).scaled(256,256);
+ return QIcon(bigone);
+}
+
+int IconList::getIconIndex(QString key)
+{
+ if (key == "default")
+ key = "infinity";
+
+ auto iter = name_index.find(key);
+ if (iter != name_index.end())
+ return *iter;
+
+ return -1;
+}
+
+//#include "IconList.moc"
diff --git a/logic/icons/IconList.h b/logic/icons/IconList.h
new file mode 100644
index 00000000..4ee3f782
--- /dev/null
+++ b/logic/icons/IconList.h
@@ -0,0 +1,78 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QMutex>
+#include <QAbstractListModel>
+#include <QFile>
+#include <QDir>
+#include <QtGui/QIcon>
+#include <memory>
+#include "MMCIcon.h"
+#include "setting.h"
+
+class QFileSystemWatcher;
+
+class IconList : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ explicit IconList(QObject *parent = 0);
+ virtual ~IconList() {};
+
+ QIcon getIcon(QString key);
+ QIcon getBigIcon(QString key);
+ int getIconIndex(QString key);
+
+ virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
+
+ bool addIcon(QString key, QString name, QString path, MMCIcon::Type type);
+ bool deleteIcon(QString key);
+
+ virtual QStringList mimeTypes() const;
+ virtual Qt::DropActions supportedDropActions() const;
+ virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
+ const QModelIndex &parent);
+ virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+
+ void installIcons(QStringList iconFiles);
+
+ void startWatching();
+ void stopWatching();
+
+signals:
+ void iconUpdated(QString key);
+
+private:
+ // hide copy constructor
+ IconList(const IconList &) = delete;
+ // hide assign op
+ IconList &operator=(const IconList &) = delete;
+ void reindex();
+
+protected
+slots:
+ void directoryChanged(const QString &path);
+ void fileChanged(const QString &path);
+ void settingChanged(const Setting & setting, QVariant value);
+private:
+ std::shared_ptr<QFileSystemWatcher> m_watcher;
+ bool is_watching;
+ QMap<QString, int> name_index;
+ QVector<MMCIcon> icons;
+ QDir m_dir;
+};
diff --git a/logic/icons/MMCIcon.cpp b/logic/icons/MMCIcon.cpp
new file mode 100644
index 00000000..d721513d
--- /dev/null
+++ b/logic/icons/MMCIcon.cpp
@@ -0,0 +1,89 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MMCIcon.h"
+#include <QFileInfo>
+
+MMCIcon::Type operator--(MMCIcon::Type &t, int)
+{
+ MMCIcon::Type temp = t;
+ switch (t)
+ {
+ case MMCIcon::Type::Builtin:
+ t = MMCIcon::Type::ToBeDeleted;
+ break;
+ case MMCIcon::Type::Transient:
+ t = MMCIcon::Type::Builtin;
+ break;
+ case MMCIcon::Type::FileBased:
+ t = MMCIcon::Type::Transient;
+ break;
+ default:
+ {
+ }
+ }
+ return temp;
+}
+
+MMCIcon::Type MMCIcon::type() const
+{
+ return m_current_type;
+}
+
+QString MMCIcon::name() const
+{
+ if (m_name.size())
+ return m_name;
+ return m_key;
+}
+
+bool MMCIcon::has(MMCIcon::Type _type) const
+{
+ return m_images[_type].present();
+}
+
+QIcon MMCIcon::icon() const
+{
+ if (m_current_type == Type::ToBeDeleted)
+ return QIcon();
+ return m_images[m_current_type].icon;
+}
+
+void MMCIcon::remove(Type rm_type)
+{
+ m_images[rm_type].filename = QString();
+ m_images[rm_type].icon = QIcon();
+ for (auto iter = rm_type; iter != Type::ToBeDeleted; iter--)
+ {
+ if (m_images[iter].present())
+ {
+ m_current_type = iter;
+ return;
+ }
+ }
+ m_current_type = Type::ToBeDeleted;
+}
+
+void MMCIcon::replace(MMCIcon::Type new_type, QIcon icon, QString path)
+{
+ QFileInfo foo(path);
+ if (new_type > m_current_type || m_current_type == MMCIcon::ToBeDeleted)
+ {
+ m_current_type = new_type;
+ }
+ m_images[new_type].icon = icon;
+ m_images[new_type].changed = foo.lastModified();
+ m_images[new_type].filename = path;
+}
diff --git a/logic/icons/MMCIcon.h b/logic/icons/MMCIcon.h
new file mode 100644
index 00000000..5e4b3bb6
--- /dev/null
+++ b/logic/icons/MMCIcon.h
@@ -0,0 +1,52 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <QDateTime>
+#include <QIcon>
+struct MMCImage
+{
+ QIcon icon;
+ QString filename;
+ QDateTime changed;
+ bool present() const
+ {
+ return !icon.isNull();
+ }
+};
+
+struct MMCIcon
+{
+ enum Type : unsigned
+ {
+ Builtin,
+ Transient,
+ FileBased,
+ ICONS_TOTAL,
+ ToBeDeleted
+ };
+ QString m_key;
+ QString m_name;
+ MMCImage m_images[ICONS_TOTAL];
+ Type m_current_type = ToBeDeleted;
+
+ Type type() const;
+ QString name() const;
+ bool has(Type _type) const;
+ QIcon icon() const;
+ void remove(Type rm_type);
+ void replace(Type new_type, QIcon icon, QString path = QString());
+};
diff --git a/logic/lists/BaseVersionList.cpp b/logic/lists/BaseVersionList.cpp
new file mode 100644
index 00000000..6e2c5282
--- /dev/null
+++ b/logic/lists/BaseVersionList.cpp
@@ -0,0 +1,121 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "logic/lists/BaseVersionList.h"
+#include "logic/BaseVersion.h"
+
+BaseVersionList::BaseVersionList(QObject *parent) : QAbstractListModel(parent)
+{
+}
+
+BaseVersionPtr BaseVersionList::findVersion(const QString &descriptor)
+{
+ for (int i = 0; i < count(); i++)
+ {
+ if (at(i)->descriptor() == descriptor)
+ return at(i);
+ }
+ return BaseVersionPtr();
+}
+
+BaseVersionPtr BaseVersionList::getLatestStable() const
+{
+ if (count() <= 0)
+ return BaseVersionPtr();
+ else
+ return at(0);
+}
+
+QVariant BaseVersionList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ BaseVersionPtr version = at(index.row());
+
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (index.column())
+ {
+ case NameColumn:
+ return version->name();
+
+ case TypeColumn:
+ return version->typeString();
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ return version->descriptor();
+
+ case VersionPointerRole:
+ return qVariantFromValue(version);
+
+ default:
+ return QVariant();
+ }
+}
+
+QVariant BaseVersionList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (section)
+ {
+ case NameColumn:
+ return "Name";
+
+ case TypeColumn:
+ return "Type";
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ switch (section)
+ {
+ case NameColumn:
+ return "The name of the version.";
+
+ case TypeColumn:
+ return "The version's type.";
+
+ default:
+ return QVariant();
+ }
+
+ default:
+ return QVariant();
+ }
+}
+
+int BaseVersionList::rowCount(const QModelIndex &parent) const
+{
+ // Return count
+ return count();
+}
+
+int BaseVersionList::columnCount(const QModelIndex &parent) const
+{
+ return 2;
+}
diff --git a/logic/lists/BaseVersionList.h b/logic/lists/BaseVersionList.h
new file mode 100644
index 00000000..21b44e8d
--- /dev/null
+++ b/logic/lists/BaseVersionList.h
@@ -0,0 +1,120 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QVariant>
+#include <QAbstractListModel>
+
+#include "logic/BaseVersion.h"
+
+class Task;
+
+/*!
+ * \brief Class that each instance type's version list derives from.
+ * Version lists are the lists that keep track of the available game versions
+ * for that instance. This list will not be loaded on startup. It will be loaded
+ * when the list's load function is called. Before using the version list, you
+ * should check to see if it has been loaded yet and if not, load the list.
+ *
+ * Note that this class also inherits from QAbstractListModel. Methods from that
+ * class determine how this version list shows up in a list view. Said methods
+ * all have a default implementation, but they can be overridden by plugins to
+ * change the behavior of the list.
+ */
+class BaseVersionList : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ enum ModelRoles
+ {
+ VersionPointerRole = 0x34B1CB48
+ };
+
+ enum VListColumns
+ {
+ // First column - Name
+ NameColumn = 0,
+
+ // Second column - Type
+ TypeColumn,
+
+ // Third column - Timestamp
+ TimeColumn
+ };
+
+ explicit BaseVersionList(QObject *parent = 0);
+
+ /*!
+ * \brief Gets a task that will reload the version list.
+ * Simply execute the task to load the list.
+ * The task returned by this function should reset the model when it's done.
+ * \return A pointer to a task that reloads the version list.
+ */
+ virtual Task *getLoadTask() = 0;
+
+ //! Checks whether or not the list is loaded. If this returns false, the list should be
+ //loaded.
+ virtual bool isLoaded() = 0;
+
+ //! Gets the version at the given index.
+ virtual const BaseVersionPtr at(int i) const = 0;
+
+ //! Returns the number of versions in the list.
+ virtual int count() const = 0;
+
+ //////// List Model Functions ////////
+ virtual QVariant data(const QModelIndex &index, int role) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+ virtual int rowCount(const QModelIndex &parent) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+
+ /*!
+ * \brief Finds a version by its descriptor.
+ * \param The descriptor of the version to find.
+ * \return A const pointer to the version with the given descriptor. NULL if
+ * one doesn't exist.
+ */
+ virtual BaseVersionPtr findVersion(const QString &descriptor);
+
+ /*!
+ * \brief Gets the latest stable version of this instance type.
+ * This is the version that will be selected by default.
+ * By default, this is simply the first version in the list.
+ */
+ virtual BaseVersionPtr getLatestStable() const;
+
+ /*!
+ * Sorts the version list.
+ */
+ virtual void sort() = 0;
+
+protected
+slots:
+ /*!
+ * Updates this list with the given list of versions.
+ * This is done by copying each version in the given list and inserting it
+ * into this one.
+ * We need to do this so that we can set the parents of the versions are set to this
+ * version list. This can't be done in the load task, because the versions the load
+ * task creates are on the load task's thread and Qt won't allow their parents
+ * to be set to something created on another thread.
+ * To get around that problem, we invoke this method on the GUI thread, which
+ * then copies the versions and sets their parents correctly.
+ * \param versions List of versions whose parents should be set.
+ */
+ virtual void updateListData(QList<BaseVersionPtr> versions) = 0;
+};
diff --git a/logic/lists/ForgeVersionList.cpp b/logic/lists/ForgeVersionList.cpp
new file mode 100644
index 00000000..4902dc64
--- /dev/null
+++ b/logic/lists/ForgeVersionList.cpp
@@ -0,0 +1,439 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ForgeVersionList.h"
+#include <logic/net/NetJob.h>
+#include <logic/net/URLConstants.h>
+#include "MultiMC.h"
+
+#include <QtNetwork>
+#include <QtXml>
+#include <QRegExp>
+
+#include "logger/QsLog.h"
+
+ForgeVersionList::ForgeVersionList(QObject *parent) : BaseVersionList(parent)
+{
+}
+
+Task *ForgeVersionList::getLoadTask()
+{
+ return new ForgeListLoadTask(this);
+}
+
+bool ForgeVersionList::isLoaded()
+{
+ return m_loaded;
+}
+
+const BaseVersionPtr ForgeVersionList::at(int i) const
+{
+ return m_vlist.at(i);
+}
+
+int ForgeVersionList::count() const
+{
+ return m_vlist.count();
+}
+
+int ForgeVersionList::columnCount(const QModelIndex &parent) const
+{
+ return 3;
+}
+
+QVariant ForgeVersionList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ auto version = std::dynamic_pointer_cast<ForgeVersion>(m_vlist[index.row()]);
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (index.column())
+ {
+ case 0:
+ return version->name();
+
+ case 1:
+ return version->mcver;
+
+ case 2:
+ return version->typeString();
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ return version->descriptor();
+
+ case VersionPointerRole:
+ return qVariantFromValue(m_vlist[index.row()]);
+
+ default:
+ return QVariant();
+ }
+}
+
+QVariant ForgeVersionList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (section)
+ {
+ case 0:
+ return "Version";
+
+ case 1:
+ return "Minecraft";
+
+ case 2:
+ return "Type";
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ switch (section)
+ {
+ case 0:
+ return "The name of the version.";
+
+ case 1:
+ return "Minecraft version";
+
+ case 2:
+ return "The version's type.";
+
+ default:
+ return QVariant();
+ }
+
+ default:
+ return QVariant();
+ }
+}
+
+BaseVersionPtr ForgeVersionList::getLatestStable() const
+{
+ return BaseVersionPtr();
+}
+
+void ForgeVersionList::updateListData(QList<BaseVersionPtr> versions)
+{
+ beginResetModel();
+ m_vlist = versions;
+ m_loaded = true;
+ endResetModel();
+ // NOW SORT!!
+ // sort();
+}
+
+void ForgeVersionList::sort()
+{
+ // NO-OP for now
+}
+
+ForgeListLoadTask::ForgeListLoadTask(ForgeVersionList *vlist) : Task()
+{
+ m_list = vlist;
+}
+
+void ForgeListLoadTask::executeTask()
+{
+ setStatus(tr("Fetching Forge version lists..."));
+ auto job = new NetJob("Version index");
+ // we do not care if the version is stale or not.
+ auto forgeListEntry = MMC->metacache()->resolveEntry("minecraftforge", "list.json");
+ auto gradleForgeListEntry = MMC->metacache()->resolveEntry("minecraftforge", "json");
+
+ // verify by poking the server.
+ forgeListEntry->stale = true;
+ gradleForgeListEntry->stale = true;
+
+ job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::FORGE_LEGACY_URL),
+ forgeListEntry));
+ job->addNetAction(gradleListDownload = CacheDownload::make(
+ QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry));
+
+ connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
+ connect(gradleListDownload.get(), SIGNAL(failed(int)), SLOT(gradleListFailed()));
+
+ listJob.reset(job);
+ connect(listJob.get(), SIGNAL(succeeded()), SLOT(listDownloaded()));
+ connect(listJob.get(), SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));
+ listJob->start();
+}
+
+bool ForgeListLoadTask::parseForgeList(QList<BaseVersionPtr> &out)
+{
+ QByteArray data;
+ {
+ auto dlJob = listDownload;
+ auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
+ QFile listFile(filename);
+ if (!listFile.open(QIODevice::ReadOnly))
+ {
+ return false;
+ }
+ data = listFile.readAll();
+ dlJob.reset();
+ }
+
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ emitFailed("Error parsing version list JSON:" + jsonError.errorString());
+ return false;
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ emitFailed("Error parsing version list JSON: JSON root is not an object");
+ return false;
+ }
+
+ QJsonObject root = jsonDoc.object();
+
+ // Now, get the array of versions.
+ if (!root.value("builds").isArray())
+ {
+ emitFailed(
+ "Error parsing version list JSON: version list object is missing 'builds' array");
+ return false;
+ }
+ QJsonArray builds = root.value("builds").toArray();
+
+ for (int i = 0; i < builds.count(); i++)
+ {
+ // Load the version info.
+ if (!builds[i].isObject())
+ {
+ // FIXME: log this somewhere
+ continue;
+ }
+ QJsonObject obj = builds[i].toObject();
+ int build_nr = obj.value("build").toDouble(0);
+ if (!build_nr)
+ continue;
+ QJsonArray files = obj.value("files").toArray();
+ QString url, jobbuildver, mcver, buildtype, filename;
+ QString changelog_url, installer_url;
+ QString installer_filename;
+ bool valid = false;
+ for (int j = 0; j < files.count(); j++)
+ {
+ if (!files[j].isObject())
+ {
+ continue;
+ }
+ QJsonObject file = files[j].toObject();
+ buildtype = file.value("buildtype").toString();
+ if ((buildtype == "client" || buildtype == "universal") && !valid)
+ {
+ mcver = file.value("mcver").toString();
+ url = file.value("url").toString();
+ jobbuildver = file.value("jobbuildver").toString();
+ int lastSlash = url.lastIndexOf('/');
+ filename = url.mid(lastSlash + 1);
+ valid = true;
+ }
+ else if (buildtype == "changelog")
+ {
+ QString ext = file.value("ext").toString();
+ if (ext.isEmpty())
+ {
+ continue;
+ }
+ changelog_url = file.value("url").toString();
+ }
+ else if (buildtype == "installer")
+ {
+ installer_url = file.value("url").toString();
+ int lastSlash = installer_url.lastIndexOf('/');
+ installer_filename = installer_url.mid(lastSlash + 1);
+ }
+ }
+ if (valid)
+ {
+ // Now, we construct the version object and add it to the list.
+ std::shared_ptr<ForgeVersion> fVersion(new ForgeVersion());
+ fVersion->universal_url = url;
+ fVersion->changelog_url = changelog_url;
+ fVersion->installer_url = installer_url;
+ fVersion->jobbuildver = jobbuildver;
+ fVersion->mcver = mcver;
+ if (installer_filename.isEmpty())
+ {
+ fVersion->filename = filename;
+ }
+ else
+ {
+ fVersion->filename = installer_filename;
+ }
+ fVersion->m_buildnr = build_nr;
+ out.append(fVersion);
+ }
+ }
+
+ return true;
+}
+
+bool ForgeListLoadTask::parseForgeGradleList(QList<BaseVersionPtr> &out)
+{
+ QByteArray data;
+ {
+ auto dlJob = gradleListDownload;
+ auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
+ QFile listFile(filename);
+ if (!listFile.open(QIODevice::ReadOnly))
+ {
+ return false;
+ }
+ data = listFile.readAll();
+ dlJob.reset();
+ }
+
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ emitFailed("Error parsing gradle version list JSON:" + jsonError.errorString());
+ return false;
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ emitFailed("Error parsing gradle version list JSON: JSON root is not an object");
+ return false;
+ }
+
+ QJsonObject root = jsonDoc.object();
+
+ // we probably could hard code these, but it might still be worth doing it this way
+ const QString webpath = root.value("webpath").toString();
+ const QString artifact = root.value("artifact").toString();
+
+ QJsonObject numbers = root.value("number").toObject();
+ for (auto it = numbers.begin(); it != numbers.end(); ++it)
+ {
+ QJsonObject number = it.value().toObject();
+ std::shared_ptr<ForgeVersion> fVersion(new ForgeVersion());
+ fVersion->m_buildnr = number.value("build").toDouble();
+ fVersion->jobbuildver = number.value("version").toString();
+ fVersion->mcver = number.value("mcversion").toString();
+ fVersion->filename = "";
+ QString filename, installer_filename;
+ QJsonArray files = number.value("files").toArray();
+ for (auto fIt = files.begin(); fIt != files.end(); ++fIt)
+ {
+ // TODO with gradle we also get checksums, use them
+ QJsonArray file = (*fIt).toArray();
+ if (file.size() < 3)
+ {
+ continue;
+ }
+ if (file.at(1).toString() == "installer")
+ {
+ fVersion->installer_url = QString("%1/%2-%3/%4-%2-%3-installer.%5").arg(
+ webpath, fVersion->mcver, fVersion->jobbuildver, artifact,
+ file.at(0).toString());
+ installer_filename = QString("%1-%2-%3-installer.%4").arg(
+ artifact, fVersion->mcver, fVersion->jobbuildver, file.at(0).toString());
+ }
+ else if (file.at(1).toString() == "universal")
+ {
+ fVersion->universal_url = QString("%1/%2-%3/%4-%2-%3-universal.%5").arg(
+ webpath, fVersion->mcver, fVersion->jobbuildver, artifact,
+ file.at(0).toString());
+ filename = QString("%1-%2-%3-universal.%4").arg(
+ artifact, fVersion->mcver, fVersion->jobbuildver, file.at(0).toString());
+ }
+ else if (file.at(1).toString() == "changelog")
+ {
+ fVersion->changelog_url = QString("%1/%2-%3/%4-%2-%3-changelog.%5").arg(
+ webpath, fVersion->mcver, fVersion->jobbuildver, artifact,
+ file.at(0).toString());
+ }
+ }
+ if (fVersion->installer_url.isEmpty() && fVersion->universal_url.isEmpty())
+ {
+ continue;
+ }
+ fVersion->filename = fVersion->installer_url.isEmpty() ? filename : installer_filename;
+ out.append(fVersion);
+ }
+
+ return true;
+}
+
+void ForgeListLoadTask::listDownloaded()
+{
+ QList<BaseVersionPtr> list;
+ bool ret = true;
+ if (!parseForgeList(list))
+ {
+ ret = false;
+ }
+ if (!parseForgeGradleList(list))
+ {
+ ret = false;
+ }
+
+ if (!ret)
+ {
+ return;
+ }
+ std::sort(list.begin(), list.end(), [](const BaseVersionPtr & l, const BaseVersionPtr & r)
+ { return (*l > *r); });
+
+ m_list->updateListData(list);
+
+ emitSucceeded();
+ return;
+}
+
+void ForgeListLoadTask::listFailed()
+{
+ auto reply = listDownload->m_reply;
+ if (reply)
+ {
+ QLOG_ERROR() << "Getting forge version list failed: " << reply->errorString();
+ }
+ else
+ {
+ QLOG_ERROR() << "Getting forge version list failed for reasons unknown.";
+ }
+}
+void ForgeListLoadTask::gradleListFailed()
+{
+ auto reply = gradleListDownload->m_reply;
+ if (reply)
+ {
+ QLOG_ERROR() << "Getting forge version list failed: " << reply->errorString();
+ }
+ else
+ {
+ QLOG_ERROR() << "Getting forge version list failed for reasons unknown.";
+ }
+}
diff --git a/logic/lists/ForgeVersionList.h b/logic/lists/ForgeVersionList.h
new file mode 100644
index 00000000..b19d3f56
--- /dev/null
+++ b/logic/lists/ForgeVersionList.h
@@ -0,0 +1,128 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QAbstractListModel>
+#include <QUrl>
+
+#include <QNetworkReply>
+#include "BaseVersionList.h"
+#include "logic/tasks/Task.h"
+#include "logic/net/NetJob.h"
+
+class ForgeVersion;
+typedef std::shared_ptr<ForgeVersion> ForgeVersionPtr;
+
+struct ForgeVersion : public BaseVersion
+{
+ virtual QString descriptor() override
+ {
+ return filename;
+ }
+ ;
+ virtual QString name() override
+ {
+ return "Forge " + jobbuildver;
+ }
+ ;
+ virtual QString typeString() const override
+ {
+ if (installer_url.isEmpty())
+ return "Universal";
+ else
+ return "Installer";
+ }
+
+ virtual bool operator<(BaseVersion &a) override
+ {
+ ForgeVersion *pa = dynamic_cast<ForgeVersion *>(&a);
+ if(!pa)
+ return true;
+ return m_buildnr < pa->m_buildnr;
+ }
+ virtual bool operator>(BaseVersion &a) override
+ {
+ ForgeVersion *pa = dynamic_cast<ForgeVersion *>(&a);
+ if(!pa)
+ return false;
+ return m_buildnr > pa->m_buildnr;
+ }
+ int m_buildnr = 0;
+ QString universal_url;
+ QString changelog_url;
+ QString installer_url;
+ QString jobbuildver;
+ QString mcver;
+ QString filename;
+};
+
+class ForgeVersionList : public BaseVersionList
+{
+ Q_OBJECT
+public:
+ friend class ForgeListLoadTask;
+
+ explicit ForgeVersionList(QObject *parent = 0);
+
+ virtual Task *getLoadTask();
+ virtual bool isLoaded();
+ virtual const BaseVersionPtr at(int i) const;
+ virtual int count() const;
+ virtual void sort();
+
+ virtual BaseVersionPtr getLatestStable() const;
+
+ virtual QVariant data(const QModelIndex &index, int role) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+
+protected:
+ QList<BaseVersionPtr> m_vlist;
+
+ bool m_loaded = false;
+
+protected
+slots:
+ virtual void updateListData(QList<BaseVersionPtr> versions);
+};
+
+class ForgeListLoadTask : public Task
+{
+ Q_OBJECT
+
+public:
+ explicit ForgeListLoadTask(ForgeVersionList *vlist);
+
+ virtual void executeTask();
+
+protected
+slots:
+ void listDownloaded();
+ void listFailed();
+ void gradleListFailed();
+
+protected:
+ NetJobPtr listJob;
+ ForgeVersionList *m_list;
+
+ CacheDownloadPtr listDownload;
+ CacheDownloadPtr gradleListDownload;
+
+private:
+ bool parseForgeList(QList<BaseVersionPtr> &out);
+ bool parseForgeGradleList(QList<BaseVersionPtr> &out);
+};
diff --git a/logic/lists/InstanceList.cpp b/logic/lists/InstanceList.cpp
new file mode 100644
index 00000000..0d4eab95
--- /dev/null
+++ b/logic/lists/InstanceList.cpp
@@ -0,0 +1,608 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <QDir>
+#include <QSet>
+#include <QFile>
+#include <QDirIterator>
+#include <QThread>
+#include <QTextStream>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QXmlStreamReader>
+#include <QRegularExpression>
+#include <pathutils.h>
+
+#include "MultiMC.h"
+#include "logic/lists/InstanceList.h"
+#include "logic/icons/IconList.h"
+#include "logic/lists/MinecraftVersionList.h"
+#include "logic/BaseInstance.h"
+#include "logic/InstanceFactory.h"
+#include "logger/QsLog.h"
+
+const static int GROUP_FILE_FORMAT_VERSION = 1;
+
+InstanceList::InstanceList(const QString &instDir, QObject *parent)
+ : QAbstractListModel(parent), m_instDir(instDir)
+{
+ connect(MMC, &MultiMC::aboutToQuit, this, &InstanceList::saveGroupList);
+
+ if (!QDir::current().exists(m_instDir))
+ {
+ QDir::current().mkpath(m_instDir);
+ }
+
+ connect(MMC->minecraftlist().get(), &MinecraftVersionList::modelReset, this,
+ &InstanceList::loadList);
+}
+
+InstanceList::~InstanceList()
+{
+}
+
+int InstanceList::rowCount(const QModelIndex &parent) const
+{
+ Q_UNUSED(parent);
+ return m_instances.count();
+}
+
+QModelIndex InstanceList::index(int row, int column, const QModelIndex &parent) const
+{
+ Q_UNUSED(parent);
+ if (row < 0 || row >= m_instances.size())
+ return QModelIndex();
+ return createIndex(row, column, (void *)m_instances.at(row).get());
+}
+
+QVariant InstanceList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ {
+ return QVariant();
+ }
+ BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
+ switch (role)
+ {
+ case InstancePointerRole:
+ {
+ QVariant v = qVariantFromValue((void *)pdata);
+ return v;
+ }
+ case Qt::DisplayRole:
+ {
+ return pdata->name();
+ }
+ case Qt::ToolTipRole:
+ {
+ return pdata->instanceRoot();
+ }
+ case Qt::DecorationRole:
+ {
+ QString key = pdata->iconKey();
+ return MMC->icons()->getIcon(key);
+ }
+ // for now.
+ case KCategorizedSortFilterProxyModel::CategorySortRole:
+ case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
+ {
+ return pdata->group();
+ }
+ default:
+ break;
+ }
+ return QVariant();
+}
+
+Qt::ItemFlags InstanceList::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags f;
+ if (index.isValid())
+ {
+ f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
+ }
+ return f;
+}
+
+void InstanceList::groupChanged()
+{
+ // save the groups. save all of them.
+ saveGroupList();
+}
+
+QStringList InstanceList::getGroups()
+{
+ return m_groups.toList();
+}
+
+void InstanceList::saveGroupList()
+{
+ QString groupFileName = m_instDir + "/instgroups.json";
+ QFile groupFile(groupFileName);
+
+ // if you can't open the file, fail
+ if (!groupFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
+ {
+ // An error occurred. Ignore it.
+ QLOG_ERROR() << "Failed to save instance group file.";
+ return;
+ }
+ QTextStream out(&groupFile);
+ QMap<QString, QSet<QString>> groupMap;
+ for (auto instance : m_instances)
+ {
+ QString id = instance->id();
+ QString group = instance->group();
+ if (group.isEmpty())
+ continue;
+
+ // keep a list/set of groups for choosing
+ m_groups.insert(group);
+
+ if (!groupMap.count(group))
+ {
+ QSet<QString> set;
+ set.insert(id);
+ groupMap[group] = set;
+ }
+ else
+ {
+ QSet<QString> &set = groupMap[group];
+ set.insert(id);
+ }
+ }
+ QJsonObject toplevel;
+ toplevel.insert("formatVersion", QJsonValue(QString("1")));
+ QJsonObject groupsArr;
+ for (auto iter = groupMap.begin(); iter != groupMap.end(); iter++)
+ {
+ auto list = iter.value();
+ auto name = iter.key();
+ QJsonObject groupObj;
+ QJsonArray instanceArr;
+ groupObj.insert("hidden", QJsonValue(QString("false")));
+ for (auto item : list)
+ {
+ instanceArr.append(QJsonValue(item));
+ }
+ groupObj.insert("instances", instanceArr);
+ groupsArr.insert(name, groupObj);
+ }
+ toplevel.insert("groups", groupsArr);
+ QJsonDocument doc(toplevel);
+ groupFile.write(doc.toJson());
+ groupFile.close();
+}
+
+void InstanceList::loadGroupList(QMap<QString, QString> &groupMap)
+{
+ QString groupFileName = m_instDir + "/instgroups.json";
+
+ // if there's no group file, fail
+ if (!QFileInfo(groupFileName).exists())
+ return;
+
+ QFile groupFile(groupFileName);
+
+ // if you can't open the file, fail
+ if (!groupFile.open(QIODevice::ReadOnly))
+ {
+ // An error occurred. Ignore it.
+ QLOG_ERROR() << "Failed to read instance group file.";
+ return;
+ }
+
+ QTextStream in(&groupFile);
+ QString jsonStr = in.readAll();
+ groupFile.close();
+
+ QJsonParseError error;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
+
+ // if the json was bad, fail
+ if (error.error != QJsonParseError::NoError)
+ {
+ QLOG_ERROR() << QString("Failed to parse instance group file: %1 at offset %2")
+ .arg(error.errorString(), QString::number(error.offset))
+ .toUtf8();
+ return;
+ }
+
+ // if the root of the json wasn't an object, fail
+ if (!jsonDoc.isObject())
+ {
+ QLOG_WARN() << "Invalid group file. Root entry should be an object.";
+ return;
+ }
+
+ QJsonObject rootObj = jsonDoc.object();
+
+ // Make sure the format version matches, otherwise fail.
+ if (rootObj.value("formatVersion").toVariant().toInt() != GROUP_FILE_FORMAT_VERSION)
+ return;
+
+ // Get the groups. if it's not an object, fail
+ if (!rootObj.value("groups").isObject())
+ {
+ QLOG_WARN() << "Invalid group list JSON: 'groups' should be an object.";
+ return;
+ }
+
+ // Iterate through all the groups.
+ QJsonObject groupMapping = rootObj.value("groups").toObject();
+ for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++)
+ {
+ QString groupName = iter.key();
+
+ // If not an object, complain and skip to the next one.
+ if (!iter.value().isObject())
+ {
+ QLOG_WARN() << QString("Group '%1' in the group list should "
+ "be an object.")
+ .arg(groupName)
+ .toUtf8();
+ continue;
+ }
+
+ QJsonObject groupObj = iter.value().toObject();
+ if (!groupObj.value("instances").isArray())
+ {
+ QLOG_WARN() << QString("Group '%1' in the group list is invalid. "
+ "It should contain an array "
+ "called 'instances'.")
+ .arg(groupName)
+ .toUtf8();
+ continue;
+ }
+
+ // keep a list/set of groups for choosing
+ m_groups.insert(groupName);
+
+ // Iterate through the list of instances in the group.
+ QJsonArray instancesArray = groupObj.value("instances").toArray();
+
+ for (QJsonArray::iterator iter2 = instancesArray.begin(); iter2 != instancesArray.end();
+ iter2++)
+ {
+ groupMap[(*iter2).toString()] = groupName;
+ }
+ }
+}
+
+struct FTBRecord
+{
+ QString dir;
+ QString name;
+ QString logo;
+ QString mcVersion;
+ QString description;
+};
+
+void InstanceList::loadForgeInstances(QMap<QString, QString> groupMap)
+{
+ QList<FTBRecord> records;
+ QDir dir = QDir(MMC->settings()->get("FTBLauncherRoot").toString());
+ QDir dataDir = QDir(MMC->settings()->get("FTBRoot").toString());
+ if (!dir.exists())
+ {
+ QLOG_INFO() << "The FTB launcher directory specified does not exist. Please check your "
+ "settings.";
+ return;
+ }
+ else if (!dataDir.exists())
+ {
+ QLOG_INFO() << "The FTB directory specified does not exist. Please check your settings";
+ return;
+ }
+ dir.cd("ModPacks");
+ auto allFiles = dir.entryList(QDir::Readable | QDir::Files, QDir::Name);
+ for(auto filename: allFiles)
+ {
+ if(!filename.endsWith(".xml"))
+ continue;
+ auto fpath = dir.absoluteFilePath(filename);
+ QFile f(fpath);
+ QLOG_INFO() << "Discovering FTB instances -- " << fpath;
+ if (!f.open(QFile::ReadOnly))
+ continue;
+
+ // read the FTB packs XML.
+ QXmlStreamReader reader(&f);
+ while (!reader.atEnd())
+ {
+ switch (reader.readNext())
+ {
+ case QXmlStreamReader::StartElement:
+ {
+ if (reader.name() == "modpack")
+ {
+ QXmlStreamAttributes attrs = reader.attributes();
+ FTBRecord record;
+ record.dir = attrs.value("dir").toString();
+ QDir test(dataDir.absoluteFilePath(record.dir));
+ if(!test.exists())
+ continue;
+ record.name = attrs.value("name").toString();
+ record.logo = attrs.value("logo").toString();
+ record.mcVersion = attrs.value("mcVersion").toString();
+ record.description = attrs.value("description").toString();
+ records.append(record);
+ }
+ break;
+ }
+ case QXmlStreamReader::EndElement:
+ break;
+ case QXmlStreamReader::Characters:
+ break;
+ default:
+ break;
+ }
+ }
+ f.close();
+ }
+
+ if(!records.size())
+ {
+ QLOG_INFO() << "No FTB instances to load.";
+ return;
+ }
+ QLOG_INFO() << "Loading FTB instances! -- got " << records.size();
+ // process the records we acquired.
+ for (auto record : records)
+ {
+ auto instanceDir = dataDir.absoluteFilePath(record.dir);
+ QLOG_INFO() << "Loading FTB instance from " << instanceDir;
+ auto templateDir = dir.absoluteFilePath(record.dir);
+ if (!QFileInfo(instanceDir).exists())
+ {
+ continue;
+ }
+
+ QString iconKey = record.logo;
+ iconKey.remove(QRegularExpression("\\..*"));
+ MMC->icons()->addIcon(iconKey, iconKey, PathCombine(templateDir, record.logo),
+ MMCIcon::Transient);
+
+ if (!QFileInfo(PathCombine(instanceDir, "instance.cfg")).exists())
+ {
+ QLOG_INFO() << "Converting " << record.name << " as new.";
+ BaseInstance *instPtr = NULL;
+ auto &factory = InstanceFactory::get();
+ auto version = MMC->minecraftlist()->findVersion(record.mcVersion);
+ if (!version)
+ {
+ QLOG_ERROR() << "Can't load instance " << instanceDir
+ << " because minecraft version " << record.mcVersion
+ << " can't be resolved.";
+ continue;
+ }
+ auto error = factory.createInstance(instPtr, version, instanceDir,
+ InstanceFactory::FTBInstance);
+
+ if (!instPtr || error != InstanceFactory::NoCreateError)
+ continue;
+
+ instPtr->setGroupInitial("FTB");
+ instPtr->setName(record.name);
+ instPtr->setIconKey(iconKey);
+ instPtr->setIntendedVersionId(record.mcVersion);
+ instPtr->setNotes(record.description);
+ continueProcessInstance(instPtr, error, instanceDir, groupMap);
+ }
+ else
+ {
+ QLOG_INFO() << "Loading existing " << record.name;
+ BaseInstance *instPtr = NULL;
+ auto error = InstanceFactory::get().loadInstance(instPtr, instanceDir);
+ if (!instPtr || error != InstanceFactory::NoCreateError)
+ continue;
+ instPtr->setGroupInitial("FTB");
+ instPtr->setName(record.name);
+ instPtr->setIconKey(iconKey);
+ if (instPtr->intendedVersionId() != record.mcVersion)
+ instPtr->setIntendedVersionId(record.mcVersion);
+ instPtr->setNotes(record.description);
+ continueProcessInstance(instPtr, error, instanceDir, groupMap);
+ }
+ }
+}
+
+InstanceList::InstListError InstanceList::loadList()
+{
+ // load the instance groups
+ QMap<QString, QString> groupMap;
+ loadGroupList(groupMap);
+
+ beginResetModel();
+
+ m_instances.clear();
+
+ {
+ QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable,
+ QDirIterator::FollowSymlinks);
+ while (iter.hasNext())
+ {
+ QString subDir = iter.next();
+ if (!QFileInfo(PathCombine(subDir, "instance.cfg")).exists())
+ continue;
+ QLOG_INFO() << "Loading MultiMC instance from " << subDir;
+ BaseInstance *instPtr = NULL;
+ auto error = InstanceFactory::get().loadInstance(instPtr, subDir);
+ continueProcessInstance(instPtr, error, subDir, groupMap);
+ }
+ }
+
+ if (MMC->settings()->get("TrackFTBInstances").toBool())
+ {
+ loadForgeInstances(groupMap);
+ }
+
+ endResetModel();
+ emit dataIsInvalid();
+ return NoError;
+}
+
+/// Clear all instances. Triggers notifications.
+void InstanceList::clear()
+{
+ beginResetModel();
+ saveGroupList();
+ m_instances.clear();
+ endResetModel();
+ emit dataIsInvalid();
+}
+
+void InstanceList::on_InstFolderChanged(const Setting &setting, QVariant value)
+{
+ m_instDir = value.toString();
+ loadList();
+}
+
+/// Add an instance. Triggers notifications, returns the new index
+int InstanceList::add(InstancePtr t)
+{
+ beginInsertRows(QModelIndex(), m_instances.size(), m_instances.size());
+ m_instances.append(t);
+ t->setParent(this);
+ connect(t.get(), SIGNAL(propertiesChanged(BaseInstance *)), this,
+ SLOT(propertiesChanged(BaseInstance *)));
+ connect(t.get(), SIGNAL(groupChanged()), this, SLOT(groupChanged()));
+ connect(t.get(), SIGNAL(nuked(BaseInstance *)), this, SLOT(instanceNuked(BaseInstance *)));
+ endInsertRows();
+ return count() - 1;
+}
+
+InstancePtr InstanceList::getInstanceById(QString instId) const
+{
+ if (m_instances.isEmpty())
+ {
+ return InstancePtr();
+ }
+
+ QListIterator<InstancePtr> iter(m_instances);
+ InstancePtr inst;
+ while (iter.hasNext())
+ {
+ inst = iter.next();
+ if (inst->id() == instId)
+ break;
+ }
+ if (inst->id() != instId)
+ return InstancePtr();
+ else
+ return iter.peekPrevious();
+}
+
+QModelIndex InstanceList::getInstanceIndexById(const QString &id) const
+{
+ return index(getInstIndex(getInstanceById(id).get()));
+}
+
+int InstanceList::getInstIndex(BaseInstance *inst) const
+{
+ for (int i = 0; i < m_instances.count(); i++)
+ {
+ if (inst == m_instances[i].get())
+ {
+ return i;
+ }
+ }
+ return -1;
+}
+
+void InstanceList::continueProcessInstance(BaseInstance *instPtr, const int error,
+ const QDir &dir, QMap<QString, QString> &groupMap)
+{
+ if (error != InstanceFactory::NoLoadError && error != InstanceFactory::NotAnInstance)
+ {
+ QString errorMsg = QString("Failed to load instance %1: ")
+ .arg(QFileInfo(dir.absolutePath()).baseName())
+ .toUtf8();
+
+ switch (error)
+ {
+ default:
+ errorMsg += QString("Unknown instance loader error %1").arg(error);
+ break;
+ }
+ QLOG_ERROR() << errorMsg.toUtf8();
+ }
+ else if (!instPtr)
+ {
+ QLOG_ERROR() << QString("Error loading instance %1. Instance loader returned null.")
+ .arg(QFileInfo(dir.absolutePath()).baseName())
+ .toUtf8();
+ }
+ else
+ {
+ auto iter = groupMap.find(instPtr->id());
+ if (iter != groupMap.end())
+ {
+ instPtr->setGroupInitial((*iter));
+ }
+ QLOG_INFO() << "Loaded instance " << instPtr->name() << " from " << dir.absolutePath();
+ instPtr->setParent(this);
+ m_instances.append(std::shared_ptr<BaseInstance>(instPtr));
+ connect(instPtr, SIGNAL(propertiesChanged(BaseInstance *)), this,
+ SLOT(propertiesChanged(BaseInstance *)));
+ connect(instPtr, SIGNAL(groupChanged()), this, SLOT(groupChanged()));
+ connect(instPtr, SIGNAL(nuked(BaseInstance *)), this,
+ SLOT(instanceNuked(BaseInstance *)));
+ }
+}
+
+void InstanceList::instanceNuked(BaseInstance *inst)
+{
+ int i = getInstIndex(inst);
+ if (i != -1)
+ {
+ beginRemoveRows(QModelIndex(), i, i);
+ m_instances.removeAt(i);
+ endRemoveRows();
+ }
+}
+
+void InstanceList::propertiesChanged(BaseInstance *inst)
+{
+ int i = getInstIndex(inst);
+ if (i != -1)
+ {
+ emit dataChanged(index(i), index(i));
+ }
+}
+
+InstanceProxyModel::InstanceProxyModel(QObject *parent)
+ : KCategorizedSortFilterProxyModel(parent)
+{
+ // disable since by default we are globally sorting by date:
+ setCategorizedModel(true);
+}
+
+bool InstanceProxyModel::subSortLessThan(const QModelIndex &left,
+ const QModelIndex &right) const
+{
+ BaseInstance *pdataLeft = static_cast<BaseInstance *>(left.internalPointer());
+ BaseInstance *pdataRight = static_cast<BaseInstance *>(right.internalPointer());
+ QString sortMode = MMC->settings()->get("InstSortMode").toString();
+ if (sortMode == "LastLaunch")
+ {
+ return pdataLeft->lastLaunch() > pdataRight->lastLaunch();
+ }
+ else
+ {
+ return QString::localeAwareCompare(pdataLeft->name(), pdataRight->name()) < 0;
+ }
+}
diff --git a/logic/lists/InstanceList.h b/logic/lists/InstanceList.h
new file mode 100644
index 00000000..0ce808e5
--- /dev/null
+++ b/logic/lists/InstanceList.h
@@ -0,0 +1,139 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QAbstractListModel>
+#include <QSet>
+#include "categorizedsortfilterproxymodel.h"
+#include <QIcon>
+
+#include "logic/BaseInstance.h"
+
+class BaseInstance;
+
+class QDir;
+
+class InstanceList : public QAbstractListModel
+{
+ Q_OBJECT
+private:
+ void loadGroupList(QMap<QString, QString> &groupList);
+
+private
+slots:
+ void saveGroupList();
+
+public:
+ explicit InstanceList(const QString &instDir, QObject *parent = 0);
+ virtual ~InstanceList();
+
+public:
+ QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ QVariant data(const QModelIndex &index, int role) const;
+ Qt::ItemFlags flags(const QModelIndex &index) const;
+
+ enum AdditionalRoles
+ {
+ InstancePointerRole = 0x34B1CB48 ///< Return pointer to real instance
+ };
+ /*!
+ * \brief Error codes returned by functions in the InstanceList class.
+ * NoError Indicates that no error occurred.
+ * UnknownError indicates that an unspecified error occurred.
+ */
+ enum InstListError
+ {
+ NoError = 0,
+ UnknownError
+ };
+
+ QString instDir() const
+ {
+ return m_instDir;
+ }
+
+ /*!
+ * \brief Get the instance at index
+ */
+ InstancePtr at(int i) const
+ {
+ return m_instances.at(i);
+ }
+ ;
+
+ /*!
+ * \brief Get the count of loaded instances
+ */
+ int count() const
+ {
+ return m_instances.count();
+ }
+ ;
+
+ /// Clear all instances. Triggers notifications.
+ void clear();
+
+ /// Add an instance. Triggers notifications, returns the new index
+ int add(InstancePtr t);
+
+ /// Get an instance by ID
+ InstancePtr getInstanceById(QString id) const;
+
+ QModelIndex getInstanceIndexById(const QString &id) const;
+
+ // FIXME: instead of iterating through all instances and forming a set, keep the set around
+ QStringList getGroups();
+signals:
+ void dataIsInvalid();
+
+public
+slots:
+ void on_InstFolderChanged(const Setting &setting, QVariant value);
+
+ /*!
+ * \brief Loads the instance list. Triggers notifications.
+ */
+ InstListError loadList();
+ void loadForgeInstances(QMap<QString, QString> groupMap);
+
+private
+slots:
+ void propertiesChanged(BaseInstance *inst);
+ void instanceNuked(BaseInstance *inst);
+ void groupChanged();
+
+private:
+ int getInstIndex(BaseInstance *inst) const;
+
+ void continueProcessInstance(BaseInstance *instPtr, const int error, const QDir &dir,
+ QMap<QString, QString> &groupMap);
+
+protected:
+ QString m_instDir;
+ QList<InstancePtr> m_instances;
+ QSet<QString> m_groups;
+};
+
+class InstanceProxyModel : public KCategorizedSortFilterProxyModel
+{
+public:
+ explicit InstanceProxyModel(QObject *parent = 0);
+
+protected:
+ virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const;
+};
diff --git a/logic/lists/JavaVersionList.cpp b/logic/lists/JavaVersionList.cpp
new file mode 100644
index 00000000..eb1c5650
--- /dev/null
+++ b/logic/lists/JavaVersionList.cpp
@@ -0,0 +1,242 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JavaVersionList.h"
+#include "MultiMC.h"
+
+#include <QtNetwork>
+#include <QtXml>
+#include <QRegExp>
+
+#include "logger/QsLog.h"
+#include "logic/JavaCheckerJob.h"
+#include "logic/JavaUtils.h"
+
+JavaVersionList::JavaVersionList(QObject *parent) : BaseVersionList(parent)
+{
+}
+
+Task *JavaVersionList::getLoadTask()
+{
+ return new JavaListLoadTask(this);
+}
+
+const BaseVersionPtr JavaVersionList::at(int i) const
+{
+ return m_vlist.at(i);
+}
+
+bool JavaVersionList::isLoaded()
+{
+ return m_loaded;
+}
+
+int JavaVersionList::count() const
+{
+ return m_vlist.count();
+}
+
+int JavaVersionList::columnCount(const QModelIndex &parent) const
+{
+ return 3;
+}
+
+QVariant JavaVersionList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ auto version = std::dynamic_pointer_cast<JavaVersion>(m_vlist[index.row()]);
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (index.column())
+ {
+ case 0:
+ return version->id;
+
+ case 1:
+ return version->arch;
+
+ case 2:
+ return version->path;
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ return version->descriptor();
+
+ case VersionPointerRole:
+ return qVariantFromValue(m_vlist[index.row()]);
+
+ default:
+ return QVariant();
+ }
+}
+
+QVariant JavaVersionList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ switch (section)
+ {
+ case 0:
+ return "Version";
+
+ case 1:
+ return "Arch";
+
+ case 2:
+ return "Path";
+
+ default:
+ return QVariant();
+ }
+
+ case Qt::ToolTipRole:
+ switch (section)
+ {
+ case 0:
+ return "The name of the version.";
+
+ case 1:
+ return "The architecture this version is for.";
+
+ case 2:
+ return "Path to this Java version.";
+
+ default:
+ return QVariant();
+ }
+
+ default:
+ return QVariant();
+ }
+}
+
+BaseVersionPtr JavaVersionList::getTopRecommended() const
+{
+ auto first = m_vlist.first();
+ if(first != nullptr)
+ {
+ return first;
+ }
+ else
+ {
+ return BaseVersionPtr();
+ }
+}
+
+void JavaVersionList::updateListData(QList<BaseVersionPtr> versions)
+{
+ beginResetModel();
+ m_vlist = versions;
+ m_loaded = true;
+ endResetModel();
+ // NOW SORT!!
+ // sort();
+}
+
+void JavaVersionList::sort()
+{
+ // NO-OP for now
+}
+
+JavaListLoadTask::JavaListLoadTask(JavaVersionList *vlist)
+{
+ m_list = vlist;
+ m_currentRecommended = NULL;
+}
+
+JavaListLoadTask::~JavaListLoadTask()
+{
+}
+
+void JavaListLoadTask::executeTask()
+{
+ setStatus(tr("Detecting Java installations..."));
+
+ JavaUtils ju;
+ QList<QString> candidate_paths = ju.FindJavaPaths();
+
+ m_job = std::shared_ptr<JavaCheckerJob>(new JavaCheckerJob("Java detection"));
+ connect(m_job.get(), SIGNAL(finished(QList<JavaCheckResult>)), this, SLOT(javaCheckerFinished(QList<JavaCheckResult>)));
+ connect(m_job.get(), SIGNAL(progress(int, int)), this, SLOT(checkerProgress(int, int)));
+
+ QLOG_DEBUG() << "Probing the following Java paths: ";
+ int id = 0;
+ for(QString candidate : candidate_paths)
+ {
+ QLOG_DEBUG() << " " << candidate;
+
+ auto candidate_checker = new JavaChecker();
+ candidate_checker->path = candidate;
+ candidate_checker->id = id;
+ m_job->addJavaCheckerAction(JavaCheckerPtr(candidate_checker));
+
+ id++;
+ }
+
+ m_job->start();
+}
+
+void JavaListLoadTask::checkerProgress(int current, int total)
+{
+ float progress = (current * 100.0) / total;
+ this->setProgress((int) progress);
+}
+
+void JavaListLoadTask::javaCheckerFinished(QList<JavaCheckResult> results)
+{
+ QList<JavaVersionPtr> candidates;
+ m_job.reset();
+
+ QLOG_DEBUG() << "Found the following valid Java installations:";
+ for(JavaCheckResult result : results)
+ {
+ if(result.valid)
+ {
+ JavaVersionPtr javaVersion(new JavaVersion());
+
+ javaVersion->id = result.javaVersion;
+ javaVersion->arch = result.mojangPlatform;
+ javaVersion->path = result.path;
+ candidates.append(javaVersion);
+
+ QLOG_DEBUG() << " " << javaVersion->id << javaVersion->arch << javaVersion->path;
+ }
+ }
+
+ QList<BaseVersionPtr> javas_bvp;
+ for (auto &java : candidates)
+ {
+ //QLOG_INFO() << java->id << java->arch << " at " << java->path;
+ BaseVersionPtr bp_java = std::dynamic_pointer_cast<BaseVersion>(java);
+
+ if (bp_java)
+ {
+ javas_bvp.append(bp_java);
+ }
+ }
+
+ m_list->updateListData(javas_bvp);
+ emitSucceeded();
+}
diff --git a/logic/lists/JavaVersionList.h b/logic/lists/JavaVersionList.h
new file mode 100644
index 00000000..e6cc8e5f
--- /dev/null
+++ b/logic/lists/JavaVersionList.h
@@ -0,0 +1,96 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QAbstractListModel>
+
+#include "BaseVersionList.h"
+#include "logic/tasks/Task.h"
+#include "logic/JavaCheckerJob.h"
+
+class JavaListLoadTask;
+
+struct JavaVersion : public BaseVersion
+{
+ virtual QString descriptor()
+ {
+ return id;
+ }
+
+ virtual QString name()
+ {
+ return id;
+ }
+
+ virtual QString typeString() const
+ {
+ return arch;
+ }
+
+ QString id;
+ QString arch;
+ QString path;
+};
+
+typedef std::shared_ptr<JavaVersion> JavaVersionPtr;
+
+class JavaVersionList : public BaseVersionList
+{
+ Q_OBJECT
+public:
+ explicit JavaVersionList(QObject *parent = 0);
+
+ virtual Task *getLoadTask();
+ virtual bool isLoaded();
+ virtual const BaseVersionPtr at(int i) const;
+ virtual int count() const;
+ virtual void sort();
+
+ virtual BaseVersionPtr getTopRecommended() const;
+
+ virtual QVariant data(const QModelIndex &index, int role) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+ virtual int columnCount(const QModelIndex &parent) const;
+
+public
+slots:
+ virtual void updateListData(QList<BaseVersionPtr> versions);
+
+protected:
+ QList<BaseVersionPtr> m_vlist;
+
+ bool m_loaded = false;
+};
+
+class JavaListLoadTask : public Task
+{
+ Q_OBJECT
+
+public:
+ explicit JavaListLoadTask(JavaVersionList *vlist);
+ ~JavaListLoadTask();
+
+ virtual void executeTask();
+public slots:
+ void javaCheckerFinished(QList<JavaCheckResult> results);
+ void checkerProgress(int current, int total);
+
+protected:
+ std::shared_ptr<JavaCheckerJob> m_job;
+ JavaVersionList *m_list;
+ JavaVersion *m_currentRecommended;
+};
diff --git a/logic/lists/LwjglVersionList.cpp b/logic/lists/LwjglVersionList.cpp
new file mode 100644
index 00000000..df46d7be
--- /dev/null
+++ b/logic/lists/LwjglVersionList.cpp
@@ -0,0 +1,199 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LwjglVersionList.h"
+#include "MultiMC.h"
+
+#include <QtNetwork>
+#include <QtXml>
+#include <QRegExp>
+
+#include "logger/QsLog.h"
+
+#define RSS_URL "http://sourceforge.net/api/file/index/project-id/58488/mtime/desc/rss"
+
+LWJGLVersionList::LWJGLVersionList(QObject *parent) : QAbstractListModel(parent)
+{
+ setLoading(false);
+}
+
+QVariant LWJGLVersionList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ const PtrLWJGLVersion version = at(index.row());
+
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ return version->name();
+
+ case Qt::ToolTipRole:
+ return version->url();
+
+ default:
+ return QVariant();
+ }
+}
+
+QVariant LWJGLVersionList::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ switch (role)
+ {
+ case Qt::DisplayRole:
+ return "Version";
+
+ case Qt::ToolTipRole:
+ return "LWJGL version name.";
+
+ default:
+ return QVariant();
+ }
+}
+
+int LWJGLVersionList::columnCount(const QModelIndex &parent) const
+{
+ return 1;
+}
+
+bool LWJGLVersionList::isLoading() const
+{
+ return m_loading;
+}
+
+void LWJGLVersionList::loadList()
+{
+ Q_ASSERT_X(!m_loading, "loadList", "list is already loading (m_loading is true)");
+
+ setLoading(true);
+ auto worker = MMC->qnam();
+ QNetworkRequest req(QUrl(RSS_URL));
+ req.setRawHeader("Accept", "text/xml");
+ req.setRawHeader("User-Agent", "MultiMC/5.0 (Uncached)");
+ reply = worker->get(req);
+ connect(reply, SIGNAL(finished()), SLOT(netRequestComplete()));
+}
+
+inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
+{
+ QDomNodeList elementList = parent.elementsByTagName(tagname);
+ if (elementList.count())
+ return elementList.at(0).toElement();
+ else
+ return QDomElement();
+}
+
+void LWJGLVersionList::netRequestComplete()
+{
+ if (reply->error() == QNetworkReply::NoError)
+ {
+ QRegExp lwjglRegex("lwjgl-(([0-9]\\.?)+)\\.zip");
+ Q_ASSERT_X(lwjglRegex.isValid(), "load LWJGL list", "LWJGL regex is invalid");
+
+ QDomDocument doc;
+
+ QString xmlErrorMsg;
+ int errorLine;
+ if (!doc.setContent(reply->readAll(), false, &xmlErrorMsg, &errorLine))
+ {
+ failed("Failed to load LWJGL list. XML error: " + xmlErrorMsg + " at line " +
+ QString::number(errorLine));
+ setLoading(false);
+ return;
+ }
+
+ QDomNodeList items = doc.elementsByTagName("item");
+
+ QList<PtrLWJGLVersion> tempList;
+
+ for (int i = 0; i < items.length(); i++)
+ {
+ Q_ASSERT_X(items.at(i).isElement(), "load LWJGL list",
+ "XML element isn't an element... wat?");
+
+ QDomElement linkElement = getDomElementByTagName(items.at(i).toElement(), "link");
+ if (linkElement.isNull())
+ {
+ QLOG_INFO() << "Link element" << i << "in RSS feed doesn't exist! Skipping.";
+ continue;
+ }
+
+ QString link = linkElement.text();
+
+ // Make sure it's a download link.
+ if (link.endsWith("/download") && link.contains(lwjglRegex))
+ {
+ QString name = link.mid(lwjglRegex.indexIn(link) + 6);
+ // Subtract 4 here to remove the .zip file extension.
+ name = name.left(lwjglRegex.matchedLength() - 10);
+
+ QUrl url(link);
+ if (!url.isValid())
+ {
+ QLOG_INFO() << "LWJGL version URL isn't valid:" << link << "Skipping.";
+ continue;
+ }
+
+ tempList.append(LWJGLVersion::Create(name, link));
+ }
+ }
+
+ beginResetModel();
+ m_vlist.swap(tempList);
+ endResetModel();
+
+ QLOG_INFO() << "Loaded LWJGL list.";
+ finished();
+ }
+ else
+ {
+ failed("Failed to load LWJGL list. Network error: " + reply->errorString());
+ }
+
+ setLoading(false);
+ reply->deleteLater();
+}
+
+const PtrLWJGLVersion LWJGLVersionList::getVersion(const QString &versionName)
+{
+ for (int i = 0; i < count(); i++)
+ {
+ QString name = at(i)->name();
+ if (name == versionName)
+ return at(i);
+ }
+ return PtrLWJGLVersion();
+}
+
+void LWJGLVersionList::failed(QString msg)
+{
+ QLOG_INFO() << msg;
+ emit loadListFailed(msg);
+}
+
+void LWJGLVersionList::finished()
+{
+ emit loadListFinished();
+}
+
+void LWJGLVersionList::setLoading(bool loading)
+{
+ m_loading = loading;
+ emit loadingStateUpdated(m_loading);
+}
diff --git a/logic/lists/LwjglVersionList.h b/logic/lists/LwjglVersionList.h
new file mode 100644
index 00000000..fa57e8eb
--- /dev/null
+++ b/logic/lists/LwjglVersionList.h
@@ -0,0 +1,148 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QAbstractListModel>
+#include <QUrl>
+#include <QNetworkReply>
+
+#include <memory>
+
+class LWJGLVersion;
+typedef std::shared_ptr<LWJGLVersion> PtrLWJGLVersion;
+
+class LWJGLVersion : public QObject
+{
+ Q_OBJECT
+
+ LWJGLVersion(const QString &name, const QString &url, QObject *parent = 0)
+ : QObject(parent), m_name(name), m_url(url)
+ {
+ }
+
+public:
+
+ static PtrLWJGLVersion Create(const QString &name, const QString &url, QObject *parent = 0)
+ {
+ return PtrLWJGLVersion(new LWJGLVersion(name, url, parent));
+ }
+ ;
+
+ QString name() const
+ {
+ return m_name;
+ }
+
+ QString url() const
+ {
+ return m_url;
+ }
+
+protected:
+ QString m_name;
+ QString m_url;
+};
+
+class LWJGLVersionList : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ explicit LWJGLVersionList(QObject *parent = 0);
+
+ bool isLoaded()
+ {
+ return m_vlist.length() > 0;
+ }
+
+ const PtrLWJGLVersion getVersion(const QString &versionName);
+ PtrLWJGLVersion at(int index)
+ {
+ return m_vlist[index];
+ }
+ const PtrLWJGLVersion at(int index) const
+ {
+ return m_vlist[index];
+ }
+
+ int count() const
+ {
+ return m_vlist.length();
+ }
+
+ virtual QVariant data(const QModelIndex &index, int role) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+ virtual int rowCount(const QModelIndex &parent) const
+ {
+ return count();
+ }
+ virtual int columnCount(const QModelIndex &parent) const;
+
+ virtual bool isLoading() const;
+ virtual bool errored() const
+ {
+ return m_errored;
+ }
+
+ virtual QString lastErrorMsg() const
+ {
+ return m_lastErrorMsg;
+ }
+
+public
+slots:
+ /*!
+ * Loads the version list.
+ * This is done asynchronously. On success, the loadListFinished() signal will
+ * be emitted. The list model will be reset as well, resulting in the modelReset()
+ * signal being emitted. Note that the model will be reset before loadListFinished() is
+ * emitted.
+ * If loading the list failed, the loadListFailed(QString msg),
+ * signal will be emitted.
+ */
+ virtual void loadList();
+
+signals:
+ /*!
+ * Emitted when the list either starts or finishes loading.
+ * \param loading Whether or not the list is loading.
+ */
+ void loadingStateUpdated(bool loading);
+
+ void loadListFinished();
+
+ void loadListFailed(QString msg);
+
+private:
+ QList<PtrLWJGLVersion> m_vlist;
+
+ QNetworkReply *m_netReply;
+ QNetworkReply *reply;
+
+ bool m_loading;
+ bool m_errored;
+ QString m_lastErrorMsg;
+
+ void failed(QString msg);
+
+ void finished();
+
+ void setLoading(bool loading);
+
+private
+slots:
+ virtual void netRequestComplete();
+};
diff --git a/logic/lists/MinecraftVersionList.cpp b/logic/lists/MinecraftVersionList.cpp
new file mode 100644
index 00000000..91f86df0
--- /dev/null
+++ b/logic/lists/MinecraftVersionList.cpp
@@ -0,0 +1,286 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MinecraftVersionList.h"
+#include "MultiMC.h"
+#include "logic/net/URLConstants.h"
+
+#include <QtXml>
+
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonValue>
+#include <QJsonParseError>
+
+#include <QtAlgorithms>
+
+#include <QtNetwork>
+
+MinecraftVersionList::MinecraftVersionList(QObject *parent) : BaseVersionList(parent)
+{
+}
+
+Task *MinecraftVersionList::getLoadTask()
+{
+ return new MCVListLoadTask(this);
+}
+
+bool MinecraftVersionList::isLoaded()
+{
+ return m_loaded;
+}
+
+const BaseVersionPtr MinecraftVersionList::at(int i) const
+{
+ return m_vlist.at(i);
+}
+
+int MinecraftVersionList::count() const
+{
+ return m_vlist.count();
+}
+
+bool cmpVersions(BaseVersionPtr first, BaseVersionPtr second)
+{
+ auto left = std::dynamic_pointer_cast<MinecraftVersion>(first);
+ auto right = std::dynamic_pointer_cast<MinecraftVersion>(second);
+ return left->timestamp > right->timestamp;
+}
+
+void MinecraftVersionList::sort()
+{
+ beginResetModel();
+ qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
+ endResetModel();
+}
+
+BaseVersionPtr MinecraftVersionList::getLatestStable() const
+{
+ for (int i = 0; i < m_vlist.length(); i++)
+ {
+ auto ver = std::dynamic_pointer_cast<MinecraftVersion>(m_vlist.at(i));
+ if (ver->is_latest && !ver->is_snapshot)
+ {
+ return m_vlist.at(i);
+ }
+ }
+ return BaseVersionPtr();
+}
+
+void MinecraftVersionList::updateListData(QList<BaseVersionPtr> versions)
+{
+ beginResetModel();
+ m_vlist = versions;
+ m_loaded = true;
+ endResetModel();
+ // NOW SORT!!
+ sort();
+}
+
+inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
+{
+ QDomNodeList elementList = parent.elementsByTagName(tagname);
+ if (elementList.count())
+ return elementList.at(0).toElement();
+ else
+ return QDomElement();
+}
+
+inline QDateTime timeFromS3Time(QString str)
+{
+ return QDateTime::fromString(str, Qt::ISODate);
+}
+
+MCVListLoadTask::MCVListLoadTask(MinecraftVersionList *vlist)
+{
+ m_list = vlist;
+ m_currentStable = NULL;
+ vlistReply = nullptr;
+ legacyWhitelist.insert("1.5.2");
+ legacyWhitelist.insert("1.5.1");
+ legacyWhitelist.insert("1.5");
+ legacyWhitelist.insert("1.4.7");
+ legacyWhitelist.insert("1.4.6");
+ legacyWhitelist.insert("1.4.5");
+ legacyWhitelist.insert("1.4.4");
+ legacyWhitelist.insert("1.4.3");
+ legacyWhitelist.insert("1.4.2");
+ legacyWhitelist.insert("1.4.1");
+ legacyWhitelist.insert("1.4");
+ legacyWhitelist.insert("1.3.2");
+ legacyWhitelist.insert("1.3.1");
+ legacyWhitelist.insert("1.3");
+ legacyWhitelist.insert("1.2.5");
+ legacyWhitelist.insert("1.2.4");
+ legacyWhitelist.insert("1.2.3");
+ legacyWhitelist.insert("1.2.2");
+ legacyWhitelist.insert("1.2.1");
+ legacyWhitelist.insert("1.1");
+ legacyWhitelist.insert("1.0.1");
+ legacyWhitelist.insert("1.0");
+}
+
+MCVListLoadTask::~MCVListLoadTask()
+{
+}
+
+void MCVListLoadTask::executeTask()
+{
+ setStatus(tr("Loading instance version list..."));
+ auto worker = MMC->qnam();
+ vlistReply = worker->get(QNetworkRequest(QUrl("http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + "versions.json")));
+ connect(vlistReply, SIGNAL(finished()), this, SLOT(list_downloaded()));
+}
+
+void MCVListLoadTask::list_downloaded()
+{
+ if (vlistReply->error() != QNetworkReply::NoError)
+ {
+ vlistReply->deleteLater();
+ emitFailed("Failed to load Minecraft main version list" + vlistReply->errorString());
+ return;
+ }
+
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(vlistReply->readAll(), &jsonError);
+ vlistReply->deleteLater();
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ emitFailed("Error parsing version list JSON:" + jsonError.errorString());
+ return;
+ }
+
+ if (!jsonDoc.isObject())
+ {
+ emitFailed("Error parsing version list JSON: jsonDoc is not an object");
+ return;
+ }
+
+ QJsonObject root = jsonDoc.object();
+
+ // Get the ID of the latest release and the latest snapshot.
+ if (!root.value("latest").isObject())
+ {
+ emitFailed("Error parsing version list JSON: version list is missing 'latest' object");
+ return;
+ }
+
+ QJsonObject latest = root.value("latest").toObject();
+
+ QString latestReleaseID = latest.value("release").toString("");
+ QString latestSnapshotID = latest.value("snapshot").toString("");
+ if (latestReleaseID.isEmpty())
+ {
+ emitFailed("Error parsing version list JSON: latest release field is missing");
+ return;
+ }
+ if (latestSnapshotID.isEmpty())
+ {
+ emitFailed("Error parsing version list JSON: latest snapshot field is missing");
+ return;
+ }
+
+ // Now, get the array of versions.
+ if (!root.value("versions").isArray())
+ {
+ emitFailed(
+ "Error parsing version list JSON: version list object is missing 'versions' array");
+ return;
+ }
+ QJsonArray versions = root.value("versions").toArray();
+
+ QList<BaseVersionPtr> tempList;
+ for (int i = 0; i < versions.count(); i++)
+ {
+ bool is_snapshot = false;
+ bool is_latest = false;
+
+ // Load the version info.
+ if (!versions[i].isObject())
+ {
+ // FIXME: log this somewhere
+ continue;
+ }
+ QJsonObject version = versions[i].toObject();
+ QString versionID = version.value("id").toString("");
+ QString versionTimeStr = version.value("releaseTime").toString("");
+ QString versionTypeStr = version.value("type").toString("");
+ if (versionID.isEmpty() || versionTimeStr.isEmpty() || versionTypeStr.isEmpty())
+ {
+ // FIXME: log this somewhere
+ continue;
+ }
+
+ // Parse the timestamp.
+ QDateTime versionTime = timeFromS3Time(versionTimeStr);
+ if (!versionTime.isValid())
+ {
+ // FIXME: log this somewhere
+ continue;
+ }
+ // Parse the type.
+ MinecraftVersion::VersionType versionType;
+ // OneSix or Legacy. use filter to determine type
+ if (versionTypeStr == "release")
+ {
+ versionType = legacyWhitelist.contains(versionID) ? MinecraftVersion::Legacy
+ : MinecraftVersion::OneSix;
+ is_latest = (versionID == latestReleaseID);
+ is_snapshot = false;
+ }
+ else if (versionTypeStr == "snapshot") // It's a snapshot... yay
+ {
+ versionType = legacyWhitelist.contains(versionID) ? MinecraftVersion::Legacy
+ : MinecraftVersion::OneSix;
+ is_latest = (versionID == latestSnapshotID);
+ is_snapshot = true;
+ }
+ else if (versionTypeStr == "old_alpha")
+ {
+ versionType = MinecraftVersion::Nostalgia;
+ is_latest = false;
+ is_snapshot = false;
+ }
+ else if (versionTypeStr == "old_beta")
+ {
+ versionType = MinecraftVersion::Legacy;
+ is_latest = false;
+ is_snapshot = false;
+ }
+ else
+ {
+ // FIXME: log this somewhere
+ continue;
+ }
+ // Get the download URL.
+ QString dlUrl = "http://" + URLConstants::AWS_DOWNLOAD_VERSIONS + versionID + "/";
+
+ // Now, we construct the version object and add it to the list.
+ std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
+ mcVersion->m_name = mcVersion->m_descriptor = versionID;
+ mcVersion->timestamp = versionTime.toMSecsSinceEpoch();
+ mcVersion->download_url = dlUrl;
+ mcVersion->is_latest = is_latest;
+ mcVersion->is_snapshot = is_snapshot;
+ mcVersion->type = versionType;
+ tempList.append(mcVersion);
+ }
+ m_list->updateListData(tempList);
+
+ emitSucceeded();
+ return;
+}
diff --git a/logic/lists/MinecraftVersionList.h b/logic/lists/MinecraftVersionList.h
new file mode 100644
index 00000000..82af1009
--- /dev/null
+++ b/logic/lists/MinecraftVersionList.h
@@ -0,0 +1,74 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QList>
+#include <QSet>
+
+#include "BaseVersionList.h"
+#include "logic/tasks/Task.h"
+#include "logic/MinecraftVersion.h"
+
+class MCVListLoadTask;
+class QNetworkReply;
+
+class MinecraftVersionList : public BaseVersionList
+{
+ Q_OBJECT
+public:
+ friend class MCVListLoadTask;
+
+ explicit MinecraftVersionList(QObject *parent = 0);
+
+ virtual Task *getLoadTask();
+ virtual bool isLoaded();
+ virtual const BaseVersionPtr at(int i) const;
+ virtual int count() const;
+ virtual void sort();
+
+ virtual BaseVersionPtr getLatestStable() const;
+
+protected:
+ QList<BaseVersionPtr> m_vlist;
+
+ bool m_loaded = false;
+
+protected
+slots:
+ virtual void updateListData(QList<BaseVersionPtr> versions);
+};
+
+class MCVListLoadTask : public Task
+{
+ Q_OBJECT
+
+public:
+ explicit MCVListLoadTask(MinecraftVersionList *vlist);
+ ~MCVListLoadTask();
+
+ virtual void executeTask();
+
+protected
+slots:
+ void list_downloaded();
+
+protected:
+ QNetworkReply *vlistReply;
+ MinecraftVersionList *m_list;
+ MinecraftVersion *m_currentStable;
+ QSet<QString> legacyWhitelist;
+};
diff --git a/logic/net/ByteArrayDownload.cpp b/logic/net/ByteArrayDownload.cpp
new file mode 100644
index 00000000..27d2a250
--- /dev/null
+++ b/logic/net/ByteArrayDownload.cpp
@@ -0,0 +1,82 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ByteArrayDownload.h"
+#include "MultiMC.h"
+#include "logger/QsLog.h"
+
+ByteArrayDownload::ByteArrayDownload(QUrl url) : NetAction()
+{
+ m_url = url;
+ m_status = Job_NotStarted;
+}
+
+void ByteArrayDownload::start()
+{
+ QLOG_INFO() << "Downloading " << m_url.toString();
+ QNetworkRequest request(m_url);
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void ByteArrayDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ m_total_progress = bytesTotal;
+ m_progress = bytesReceived;
+ emit progress(m_index_within_job, bytesReceived, bytesTotal);
+}
+
+void ByteArrayDownload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ QLOG_ERROR() << "Error getting URL:" << m_url.toString().toLocal8Bit()
+ << "Network error: " << error;
+ m_status = Job_Failed;
+}
+
+void ByteArrayDownload::downloadFinished()
+{
+ // if the download succeeded
+ if (m_status != Job_Failed)
+ {
+ // nothing went wrong...
+ m_status = Job_Finished;
+ m_data = m_reply->readAll();
+ m_reply.reset();
+ emit succeeded(m_index_within_job);
+ return;
+ }
+ // else the download failed
+ else
+ {
+ m_reply.reset();
+ emit failed(m_index_within_job);
+ return;
+ }
+}
+
+void ByteArrayDownload::downloadReadyRead()
+{
+ // ~_~
+}
diff --git a/logic/net/ByteArrayDownload.h b/logic/net/ByteArrayDownload.h
new file mode 100644
index 00000000..0d90abc2
--- /dev/null
+++ b/logic/net/ByteArrayDownload.h
@@ -0,0 +1,44 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include "NetAction.h"
+
+typedef std::shared_ptr<class ByteArrayDownload> ByteArrayDownloadPtr;
+class ByteArrayDownload : public NetAction
+{
+ Q_OBJECT
+public:
+ ByteArrayDownload(QUrl url);
+ static ByteArrayDownloadPtr make(QUrl url)
+ {
+ return ByteArrayDownloadPtr(new ByteArrayDownload(url));
+ }
+
+public:
+ /// if not saving to file, downloaded data is placed here
+ QByteArray m_data;
+
+public
+slots:
+ virtual void start();
+
+protected
+slots:
+ void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ void downloadError(QNetworkReply::NetworkError error);
+ void downloadFinished();
+ void downloadReadyRead();
+};
diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp
new file mode 100644
index 00000000..d2a9bdee
--- /dev/null
+++ b/logic/net/CacheDownload.cpp
@@ -0,0 +1,169 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "CacheDownload.h"
+#include <pathutils.h>
+
+#include <QCryptographicHash>
+#include <QFileInfo>
+#include <QDateTime>
+#include "logger/QsLog.h"
+
+CacheDownload::CacheDownload(QUrl url, MetaEntryPtr entry)
+ : NetAction(), md5sum(QCryptographicHash::Md5)
+{
+ m_url = url;
+ m_entry = entry;
+ m_target_path = entry->getFullPath();
+ m_status = Job_NotStarted;
+}
+
+void CacheDownload::start()
+{
+ m_status = Job_InProgress;
+ if (!m_entry->stale)
+ {
+ m_status = Job_Finished;
+ emit succeeded(m_index_within_job);
+ return;
+ }
+ // create a new save file
+ m_output_file.reset(new QSaveFile(m_target_path));
+
+ // if there already is a file and md5 checking is in effect and it can be opened
+ if (!ensureFilePathExists(m_target_path))
+ {
+ QLOG_ERROR() << "Could not create folder for " + m_target_path;
+ m_status = Job_Failed;
+ emit failed(m_index_within_job);
+ return;
+ }
+ if (!m_output_file->open(QIODevice::WriteOnly))
+ {
+ QLOG_ERROR() << "Could not open " + m_target_path + " for writing";
+ m_status = Job_Failed;
+ emit failed(m_index_within_job);
+ return;
+ }
+ QLOG_INFO() << "Downloading " << m_url.toString();
+ QNetworkRequest request(m_url);
+
+ // check file consistency first.
+ QFile current(m_target_path);
+ if(current.exists() && current.size() != 0)
+ {
+ if (m_entry->remote_changed_timestamp.size())
+ request.setRawHeader(QString("If-Modified-Since").toLatin1(),
+ m_entry->remote_changed_timestamp.toLatin1());
+ if (m_entry->etag.size())
+ request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1());
+ }
+
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
+
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void CacheDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ m_total_progress = bytesTotal;
+ m_progress = bytesReceived;
+ emit progress(m_index_within_job, bytesReceived, bytesTotal);
+}
+
+void CacheDownload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ QLOG_ERROR() << "Failed " << m_url.toString() << " with reason " << error;
+ m_status = Job_Failed;
+}
+void CacheDownload::downloadFinished()
+{
+ // if the download succeeded
+ if (m_status == Job_Failed)
+ {
+ m_output_file->cancelWriting();
+ m_reply.reset();
+ emit failed(m_index_within_job);
+ return;
+ }
+
+ // if we wrote any data to the save file, we try to commit the data to the real file.
+ if (wroteAnyData)
+ {
+ // nothing went wrong...
+ if (m_output_file->commit())
+ {
+ m_status = Job_Finished;
+ m_entry->md5sum = md5sum.result().toHex().constData();
+ }
+ else
+ {
+ QLOG_ERROR() << "Failed to commit changes to " << m_target_path;
+ m_output_file->cancelWriting();
+ m_reply.reset();
+ m_status = Job_Failed;
+ emit failed(m_index_within_job);
+ return;
+ }
+ }
+ else
+ {
+ m_status = Job_Finished;
+ }
+
+ // then get rid of the save file
+ m_output_file.reset();
+
+ QFileInfo output_file_info(m_target_path);
+
+ m_entry->etag = m_reply->rawHeader("ETag").constData();
+ if (m_reply->hasRawHeader("Last-Modified"))
+ {
+ m_entry->remote_changed_timestamp = m_reply->rawHeader("Last-Modified").constData();
+ }
+ m_entry->local_changed_timestamp =
+ output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
+ m_entry->stale = false;
+ MMC->metacache()->updateEntry(m_entry);
+
+ m_reply.reset();
+ emit succeeded(m_index_within_job);
+ return;
+}
+
+void CacheDownload::downloadReadyRead()
+{
+ QByteArray ba = m_reply->readAll();
+ md5sum.addData(ba);
+ if (m_output_file->write(ba) != ba.size())
+ {
+ QLOG_ERROR() << "Failed writing into " + m_target_path;
+ m_status = Job_Failed;
+ m_reply->abort();
+ emit failed(m_index_within_job);
+ }
+ wroteAnyData = true;
+}
diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h
new file mode 100644
index 00000000..154f5988
--- /dev/null
+++ b/logic/net/CacheDownload.h
@@ -0,0 +1,58 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "NetAction.h"
+#include "HttpMetaCache.h"
+#include <QCryptographicHash>
+#include <QSaveFile>
+
+typedef std::shared_ptr<class CacheDownload> CacheDownloadPtr;
+class CacheDownload : public NetAction
+{
+ Q_OBJECT
+private:
+ MetaEntryPtr m_entry;
+ /// if saving to file, use the one specified in this string
+ QString m_target_path;
+ /// this is the output file, if any
+ std::shared_ptr<QSaveFile> m_output_file;
+ /// the hash-as-you-download
+ QCryptographicHash md5sum;
+
+ bool wroteAnyData = false;
+
+public:
+ explicit CacheDownload(QUrl url, MetaEntryPtr entry);
+ static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry)
+ {
+ return CacheDownloadPtr(new CacheDownload(url, entry));
+ }
+ QString getTargetFilepath()
+ {
+ return m_target_path;
+ }
+protected
+slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ virtual void downloadError(QNetworkReply::NetworkError error);
+ virtual void downloadFinished();
+ virtual void downloadReadyRead();
+
+public
+slots:
+ virtual void start();
+};
diff --git a/logic/net/ForgeMirror.h b/logic/net/ForgeMirror.h
new file mode 100644
index 00000000..2518dffe
--- /dev/null
+++ b/logic/net/ForgeMirror.h
@@ -0,0 +1,10 @@
+#pragma once
+#include <QString>
+
+struct ForgeMirror
+{
+ QString name;
+ QString logo_url;
+ QString website_url;
+ QString mirror_url;
+}; \ No newline at end of file
diff --git a/logic/net/ForgeMirrors.cpp b/logic/net/ForgeMirrors.cpp
new file mode 100644
index 00000000..b224306f
--- /dev/null
+++ b/logic/net/ForgeMirrors.cpp
@@ -0,0 +1,118 @@
+#include "MultiMC.h"
+#include "ForgeMirrors.h"
+#include "logger/QsLog.h"
+#include <algorithm>
+#include <random>
+
+ForgeMirrors::ForgeMirrors(QList<ForgeXzDownloadPtr> &libs, NetJobPtr parent_job,
+ QString mirrorlist)
+{
+ m_libs = libs;
+ m_parent_job = parent_job;
+ m_url = QUrl(mirrorlist);
+ m_status = Job_NotStarted;
+}
+
+void ForgeMirrors::start()
+{
+ QLOG_INFO() << "Downloading " << m_url.toString();
+ QNetworkRequest request(m_url);
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void ForgeMirrors::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ QLOG_ERROR() << "Error getting URL:" << m_url.toString().toLocal8Bit()
+ << "Network error: " << error;
+ m_status = Job_Failed;
+}
+
+void ForgeMirrors::downloadFinished()
+{
+ // if the download succeeded
+ if (m_status != Job_Failed)
+ {
+ // nothing went wrong... ?
+ parseMirrorList();
+ return;
+ }
+ // else the download failed, we use a fixed list
+ else
+ {
+ m_status = Job_Finished;
+ m_reply.reset();
+ deferToFixedList();
+ return;
+ }
+}
+
+void ForgeMirrors::deferToFixedList()
+{
+ m_mirrors.clear();
+ m_mirrors.append(
+ {"Minecraft Forge", "http://files.minecraftforge.net/forge_logo.png",
+ "http://files.minecraftforge.net/", "http://files.minecraftforge.net/maven/"});
+ m_mirrors.append({"Creeper Host",
+ "http://files.minecraftforge.net/forge_logo.png",
+ "https://www.creeperhost.net/link.php?id=1",
+ "http://new.creeperrepo.net/forge/maven/"});
+ injectDownloads();
+ emit succeeded(m_index_within_job);
+}
+
+void ForgeMirrors::parseMirrorList()
+{
+ m_status = Job_Finished;
+ auto data = m_reply->readAll();
+ m_reply.reset();
+ auto dataLines = data.split('\n');
+ for(auto line: dataLines)
+ {
+ auto elements = line.split('!');
+ if (elements.size() == 4)
+ {
+ m_mirrors.append({elements[0],elements[1],elements[2],elements[3]});
+ }
+ }
+ if(!m_mirrors.size())
+ deferToFixedList();
+ injectDownloads();
+ emit succeeded(m_index_within_job);
+}
+
+void ForgeMirrors::injectDownloads()
+{
+ // shuffle the mirrors randomly
+ std::random_device rd;
+ std::mt19937 rng(rd());
+ std::shuffle(m_mirrors.begin(), m_mirrors.end(), rng);
+
+ // tell parent to download the libs
+ for(auto lib: m_libs)
+ {
+ lib->setMirrors(m_mirrors);
+ m_parent_job->addNetAction(lib);
+ }
+}
+
+void ForgeMirrors::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ m_total_progress = bytesTotal;
+ m_progress = bytesReceived;
+ emit progress(m_index_within_job, bytesReceived, bytesTotal);
+}
+
+void ForgeMirrors::downloadReadyRead()
+{
+}
diff --git a/logic/net/ForgeMirrors.h b/logic/net/ForgeMirrors.h
new file mode 100644
index 00000000..990e49d6
--- /dev/null
+++ b/logic/net/ForgeMirrors.h
@@ -0,0 +1,58 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "NetAction.h"
+#include "HttpMetaCache.h"
+#include "ForgeXzDownload.h"
+#include "NetJob.h"
+#include <QFile>
+#include <QTemporaryFile>
+typedef std::shared_ptr<class ForgeMirrors> ForgeMirrorsPtr;
+
+class ForgeMirrors : public NetAction
+{
+ Q_OBJECT
+public:
+ QList<ForgeXzDownloadPtr> m_libs;
+ NetJobPtr m_parent_job;
+ QList<ForgeMirror> m_mirrors;
+
+public:
+ explicit ForgeMirrors(QList<ForgeXzDownloadPtr> &libs, NetJobPtr parent_job,
+ QString mirrorlist);
+ static ForgeMirrorsPtr make(QList<ForgeXzDownloadPtr> &libs, NetJobPtr parent_job,
+ QString mirrorlist)
+ {
+ return ForgeMirrorsPtr(new ForgeMirrors(libs, parent_job, mirrorlist));
+ }
+
+protected
+slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ virtual void downloadError(QNetworkReply::NetworkError error);
+ virtual void downloadFinished();
+ virtual void downloadReadyRead();
+
+private:
+ void parseMirrorList();
+ void deferToFixedList();
+ void injectDownloads();
+
+public
+slots:
+ virtual void start();
+};
diff --git a/logic/net/ForgeXzDownload.cpp b/logic/net/ForgeXzDownload.cpp
new file mode 100644
index 00000000..359ad858
--- /dev/null
+++ b/logic/net/ForgeXzDownload.cpp
@@ -0,0 +1,389 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "ForgeXzDownload.h"
+#include <pathutils.h>
+
+#include <QCryptographicHash>
+#include <QFileInfo>
+#include <QDateTime>
+#include <QDir>
+#include "logger/QsLog.h"
+
+ForgeXzDownload::ForgeXzDownload(QString relative_path, MetaEntryPtr entry) : NetAction()
+{
+ m_entry = entry;
+ m_target_path = entry->getFullPath();
+ m_pack200_xz_file.setFileTemplate("./dl_temp.XXXXXX");
+ m_status = Job_NotStarted;
+ m_url_path = relative_path;
+}
+
+void ForgeXzDownload::setMirrors(QList<ForgeMirror> &mirrors)
+{
+ m_mirror_index = 0;
+ m_mirrors = mirrors;
+ updateUrl();
+}
+
+void ForgeXzDownload::start()
+{
+ m_status = Job_InProgress;
+ if (!m_entry->stale)
+ {
+ m_status = Job_Finished;
+ emit succeeded(m_index_within_job);
+ return;
+ }
+ // can we actually create the real, final file?
+ if (!ensureFilePathExists(m_target_path))
+ {
+ m_status = Job_Failed;
+ emit failed(m_index_within_job);
+ return;
+ }
+ if (m_mirrors.empty())
+ {
+ m_status = Job_Failed;
+ emit failed(m_index_within_job);
+ return;
+ }
+
+ QLOG_INFO() << "Downloading " << m_url.toString();
+ QNetworkRequest request(m_url);
+ request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1());
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
+
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void ForgeXzDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ m_total_progress = bytesTotal;
+ m_progress = bytesReceived;
+ emit progress(m_index_within_job, bytesReceived, bytesTotal);
+}
+
+void ForgeXzDownload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ // TODO: log the reason why
+ m_status = Job_Failed;
+}
+
+void ForgeXzDownload::failAndTryNextMirror()
+{
+ m_status = Job_Failed;
+ int next = m_mirror_index + 1;
+ if(m_mirrors.size() == next)
+ m_mirror_index = 0;
+ else
+ m_mirror_index = next;
+
+ updateUrl();
+ emit failed(m_index_within_job);
+}
+
+void ForgeXzDownload::updateUrl()
+{
+ QLOG_INFO() << "Updating URL for " << m_url_path;
+ for (auto possible : m_mirrors)
+ {
+ QLOG_INFO() << "Possible: " << possible.name << " : " << possible.mirror_url;
+ }
+ QString aggregate = m_mirrors[m_mirror_index].mirror_url + m_url_path + ".pack.xz";
+ m_url = QUrl(aggregate);
+}
+
+void ForgeXzDownload::downloadFinished()
+{
+ //TEST: defer to other possible mirrors (autofail the first one)
+ /*
+ QLOG_INFO() <<"dl " << index_within_job << " mirror " << m_mirror_index;
+ if( m_mirror_index == 0)
+ {
+ QLOG_INFO() <<"dl " << index_within_job << " AUTOFAIL";
+ m_status = Job_Failed;
+ m_pack200_xz_file.close();
+ m_pack200_xz_file.remove();
+ m_reply.reset();
+ failAndTryNextMirror();
+ return;
+ }
+ */
+
+ // if the download succeeded
+ if (m_status != Job_Failed)
+ {
+ // nothing went wrong...
+ m_status = Job_Finished;
+ if (m_pack200_xz_file.isOpen())
+ {
+ // we actually downloaded something! process and isntall it
+ decompressAndInstall();
+ return;
+ }
+ else
+ {
+ // something bad happened -- on the local machine!
+ m_status = Job_Failed;
+ m_pack200_xz_file.remove();
+ m_reply.reset();
+ emit failed(m_index_within_job);
+ return;
+ }
+ }
+ // else the download failed
+ else
+ {
+ m_status = Job_Failed;
+ m_pack200_xz_file.close();
+ m_pack200_xz_file.remove();
+ m_reply.reset();
+ failAndTryNextMirror();
+ return;
+ }
+}
+
+void ForgeXzDownload::downloadReadyRead()
+{
+
+ if (!m_pack200_xz_file.isOpen())
+ {
+ if (!m_pack200_xz_file.open())
+ {
+ /*
+ * Can't open the file... the job failed
+ */
+ m_reply->abort();
+ emit failed(m_index_within_job);
+ return;
+ }
+ }
+ m_pack200_xz_file.write(m_reply->readAll());
+}
+
+#include "xz.h"
+#include "unpack200.h"
+#include <stdexcept>
+
+const size_t buffer_size = 8196;
+
+void ForgeXzDownload::decompressAndInstall()
+{
+ // rewind the downloaded temp file
+ m_pack200_xz_file.seek(0);
+ // de-xz'd file
+ QTemporaryFile pack200_file("./dl_temp.XXXXXX");
+ pack200_file.open();
+
+ bool xz_success = false;
+ // first, de-xz
+ {
+ uint8_t in[buffer_size];
+ uint8_t out[buffer_size];
+ struct xz_buf b;
+ struct xz_dec *s;
+ enum xz_ret ret;
+ xz_crc32_init();
+ xz_crc64_init();
+ s = xz_dec_init(XZ_DYNALLOC, 1 << 26);
+ if (s == nullptr)
+ {
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+ }
+ b.in = in;
+ b.in_pos = 0;
+ b.in_size = 0;
+ b.out = out;
+ b.out_pos = 0;
+ b.out_size = buffer_size;
+ while (!xz_success)
+ {
+ if (b.in_pos == b.in_size)
+ {
+ b.in_size = m_pack200_xz_file.read((char *)in, sizeof(in));
+ b.in_pos = 0;
+ }
+
+ ret = xz_dec_run(s, &b);
+
+ if (b.out_pos == sizeof(out))
+ {
+ if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
+ {
+ // msg = "Write error\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+ }
+
+ b.out_pos = 0;
+ }
+
+ if (ret == XZ_OK)
+ continue;
+
+ if (ret == XZ_UNSUPPORTED_CHECK)
+ {
+ // unsupported check. this is OK, but we should log this
+ continue;
+ }
+
+ if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
+ {
+ // write error
+ pack200_file.close();
+ xz_dec_end(s);
+ return;
+ }
+
+ switch (ret)
+ {
+ case XZ_STREAM_END:
+ xz_dec_end(s);
+ xz_success = true;
+ break;
+
+ case XZ_MEM_ERROR:
+ QLOG_ERROR() << "Memory allocation failed\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+
+ case XZ_MEMLIMIT_ERROR:
+ QLOG_ERROR() << "Memory usage limit reached\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+
+ case XZ_FORMAT_ERROR:
+ QLOG_ERROR() << "Not a .xz file\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+
+ case XZ_OPTIONS_ERROR:
+ QLOG_ERROR() << "Unsupported options in the .xz headers\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+
+ case XZ_DATA_ERROR:
+ case XZ_BUF_ERROR:
+ QLOG_ERROR() << "File is corrupt\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+
+ default:
+ QLOG_ERROR() << "Bug!\n";
+ xz_dec_end(s);
+ failAndTryNextMirror();
+ return;
+ }
+ }
+ }
+ m_pack200_xz_file.remove();
+
+ // revert pack200
+ pack200_file.seek(0);
+ int handle_in = pack200_file.handle();
+ // FIXME: dispose of file handles, pointers and the like. Ideally wrap in objects.
+ if(handle_in == -1)
+ {
+ QLOG_ERROR() << "Error reopening " << pack200_file.fileName();
+ failAndTryNextMirror();
+ return;
+ }
+ FILE * file_in = fdopen(handle_in,"r");
+ if(!file_in)
+ {
+ QLOG_ERROR() << "Error reopening " << pack200_file.fileName();
+ failAndTryNextMirror();
+ return;
+ }
+ QFile qfile_out(m_target_path);
+ if(!qfile_out.open(QIODevice::WriteOnly))
+ {
+ QLOG_ERROR() << "Error opening " << qfile_out.fileName();
+ failAndTryNextMirror();
+ return;
+ }
+ int handle_out = qfile_out.handle();
+ if(handle_out == -1)
+ {
+ QLOG_ERROR() << "Error opening " << qfile_out.fileName();
+ failAndTryNextMirror();
+ return;
+ }
+ FILE * file_out = fdopen(handle_out,"w");
+ if(!file_out)
+ {
+ QLOG_ERROR() << "Error opening " << qfile_out.fileName();
+ failAndTryNextMirror();
+ return;
+ }
+ try
+ {
+ unpack_200(file_in, file_out);
+ }
+ catch (std::runtime_error &err)
+ {
+ m_status = Job_Failed;
+ QLOG_ERROR() << "Error unpacking " << pack200_file.fileName() << " : " << err.what();
+ QFile f(m_target_path);
+ if (f.exists())
+ f.remove();
+ failAndTryNextMirror();
+ return;
+ }
+ pack200_file.remove();
+
+ QFile jar_file(m_target_path);
+
+ if (!jar_file.open(QIODevice::ReadOnly))
+ {
+ jar_file.remove();
+ failAndTryNextMirror();
+ return;
+ }
+ m_entry->md5sum = QCryptographicHash::hash(jar_file.readAll(), QCryptographicHash::Md5)
+ .toHex()
+ .constData();
+ jar_file.close();
+
+ QFileInfo output_file_info(m_target_path);
+ m_entry->etag = m_reply->rawHeader("ETag").constData();
+ m_entry->local_changed_timestamp =
+ output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
+ m_entry->stale = false;
+ MMC->metacache()->updateEntry(m_entry);
+
+ m_reply.reset();
+ emit succeeded(m_index_within_job);
+}
diff --git a/logic/net/ForgeXzDownload.h b/logic/net/ForgeXzDownload.h
new file mode 100644
index 00000000..990f91f0
--- /dev/null
+++ b/logic/net/ForgeXzDownload.h
@@ -0,0 +1,65 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "NetAction.h"
+#include "HttpMetaCache.h"
+#include <QFile>
+#include <QTemporaryFile>
+#include "ForgeMirror.h"
+
+typedef std::shared_ptr<class ForgeXzDownload> ForgeXzDownloadPtr;
+
+class ForgeXzDownload : public NetAction
+{
+ Q_OBJECT
+public:
+ MetaEntryPtr m_entry;
+ /// if saving to file, use the one specified in this string
+ QString m_target_path;
+ /// this is the output file, if any
+ QTemporaryFile m_pack200_xz_file;
+ /// mirror index (NOT OPTICS, I SWEAR)
+ int m_mirror_index = 0;
+ /// list of mirrors to use. Mirror has the url base
+ QList<ForgeMirror> m_mirrors;
+ /// path relative to the mirror base
+ QString m_url_path;
+
+public:
+ explicit ForgeXzDownload(QString relative_path, MetaEntryPtr entry);
+ static ForgeXzDownloadPtr make(QString relative_path, MetaEntryPtr entry)
+ {
+ return ForgeXzDownloadPtr(new ForgeXzDownload(relative_path, entry));
+ }
+ void setMirrors(QList<ForgeMirror> & mirrors);
+
+protected
+slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ virtual void downloadError(QNetworkReply::NetworkError error);
+ virtual void downloadFinished();
+ virtual void downloadReadyRead();
+
+public
+slots:
+ virtual void start();
+
+private:
+ void decompressAndInstall();
+ void failAndTryNextMirror();
+ void updateUrl();
+};
diff --git a/logic/net/HttpMetaCache.cpp b/logic/net/HttpMetaCache.cpp
new file mode 100644
index 00000000..29007951
--- /dev/null
+++ b/logic/net/HttpMetaCache.cpp
@@ -0,0 +1,253 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "HttpMetaCache.h"
+#include <pathutils.h>
+
+#include <QFileInfo>
+#include <QFile>
+#include <QTemporaryFile>
+#include <QSaveFile>
+#include <QDateTime>
+#include <QCryptographicHash>
+
+#include "logger/QsLog.h"
+
+#include <QJsonDocument>
+#include <QJsonArray>
+#include <QJsonObject>
+
+QString MetaEntry::getFullPath()
+{
+ return PathCombine(MMC->metacache()->getBasePath(base), path);
+}
+
+HttpMetaCache::HttpMetaCache(QString path) : QObject()
+{
+ m_index_file = path;
+ saveBatchingTimer.setSingleShot(true);
+ saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer);
+ connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow()));
+}
+
+HttpMetaCache::~HttpMetaCache()
+{
+ saveBatchingTimer.stop();
+ SaveNow();
+}
+
+MetaEntryPtr HttpMetaCache::getEntry(QString base, QString resource_path)
+{
+ // no base. no base path. can't store
+ if (!m_entries.contains(base))
+ {
+ // TODO: log problem
+ return MetaEntryPtr();
+ }
+ EntryMap &map = m_entries[base];
+ if (map.entry_list.contains(resource_path))
+ {
+ return map.entry_list[resource_path];
+ }
+ return MetaEntryPtr();
+}
+
+MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path,
+ QString expected_etag)
+{
+ auto entry = getEntry(base, resource_path);
+ // it's not present? generate a default stale entry
+ if (!entry)
+ {
+ return staleEntry(base, resource_path);
+ }
+
+ auto &selected_base = m_entries[base];
+ QString real_path = PathCombine(selected_base.base_path, resource_path);
+ QFileInfo finfo(real_path);
+
+ // is the file really there? if not -> stale
+ if (!finfo.isFile() || !finfo.isReadable())
+ {
+ // if the file doesn't exist, we disown the entry
+ selected_base.entry_list.remove(resource_path);
+ return staleEntry(base, resource_path);
+ }
+
+ if (!expected_etag.isEmpty() && expected_etag != entry->etag)
+ {
+ // if the etag doesn't match expected, we disown the entry
+ selected_base.entry_list.remove(resource_path);
+ return staleEntry(base, resource_path);
+ }
+
+ // if the file changed, check md5sum
+ qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch();
+ if (file_last_changed != entry->local_changed_timestamp)
+ {
+ QFile input(real_path);
+ input.open(QIODevice::ReadOnly);
+ QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5)
+ .toHex()
+ .constData();
+ if (entry->md5sum != md5sum)
+ {
+ selected_base.entry_list.remove(resource_path);
+ return staleEntry(base, resource_path);
+ }
+ // md5sums matched... keep entry and save the new state to file
+ entry->local_changed_timestamp = file_last_changed;
+ SaveEventually();
+ }
+
+ // entry passed all the checks we cared about.
+ return entry;
+}
+
+bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry)
+{
+ if (!m_entries.contains(stale_entry->base))
+ {
+ QLOG_ERROR() << "Cannot add entry with unknown base: "
+ << stale_entry->base.toLocal8Bit();
+ return false;
+ }
+ if (stale_entry->stale)
+ {
+ QLOG_ERROR() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit();
+ return false;
+ }
+ m_entries[stale_entry->base].entry_list[stale_entry->path] = stale_entry;
+ SaveEventually();
+ return true;
+}
+
+MetaEntryPtr HttpMetaCache::staleEntry(QString base, QString resource_path)
+{
+ auto foo = new MetaEntry;
+ foo->base = base;
+ foo->path = resource_path;
+ foo->stale = true;
+ return MetaEntryPtr(foo);
+}
+
+void HttpMetaCache::addBase(QString base, QString base_root)
+{
+ // TODO: report error
+ if (m_entries.contains(base))
+ return;
+ // TODO: check if the base path is valid
+ EntryMap foo;
+ foo.base_path = base_root;
+ m_entries[base] = foo;
+}
+
+QString HttpMetaCache::getBasePath(QString base)
+{
+ if (m_entries.contains(base))
+ {
+ return m_entries[base].base_path;
+ }
+ return QString();
+}
+
+void HttpMetaCache::Load()
+{
+ QFile index(m_index_file);
+ if (!index.open(QIODevice::ReadOnly))
+ return;
+
+ QJsonDocument json = QJsonDocument::fromJson(index.readAll());
+ if (!json.isObject())
+ return;
+ auto root = json.object();
+ // check file version first
+ auto version_val = root.value("version");
+ if (!version_val.isString())
+ return;
+ if (version_val.toString() != "1")
+ return;
+
+ // read the entry array
+ auto entries_val = root.value("entries");
+ if (!entries_val.isArray())
+ return;
+ QJsonArray array = entries_val.toArray();
+ for (auto element : array)
+ {
+ if (!element.isObject())
+ return;
+ auto element_obj = element.toObject();
+ QString base = element_obj.value("base").toString();
+ if (!m_entries.contains(base))
+ continue;
+ auto &entrymap = m_entries[base];
+ auto foo = new MetaEntry;
+ foo->base = base;
+ QString path = foo->path = element_obj.value("path").toString();
+ foo->md5sum = element_obj.value("md5sum").toString();
+ foo->etag = element_obj.value("etag").toString();
+ foo->local_changed_timestamp = element_obj.value("last_changed_timestamp").toDouble();
+ foo->remote_changed_timestamp =
+ element_obj.value("remote_changed_timestamp").toString();
+ // presumed innocent until closer examination
+ foo->stale = false;
+ entrymap.entry_list[path] = MetaEntryPtr(foo);
+ }
+}
+
+void HttpMetaCache::SaveEventually()
+{
+ // reset the save timer
+ saveBatchingTimer.stop();
+ saveBatchingTimer.start(30000);
+}
+
+void HttpMetaCache::SaveNow()
+{
+ QSaveFile tfile(m_index_file);
+ if (!tfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
+ return;
+ QJsonObject toplevel;
+ toplevel.insert("version", QJsonValue(QString("1")));
+ QJsonArray entriesArr;
+ for (auto group : m_entries)
+ {
+ for (auto entry : group.entry_list)
+ {
+ QJsonObject entryObj;
+ entryObj.insert("base", QJsonValue(entry->base));
+ entryObj.insert("path", QJsonValue(entry->path));
+ entryObj.insert("md5sum", QJsonValue(entry->md5sum));
+ entryObj.insert("etag", QJsonValue(entry->etag));
+ entryObj.insert("last_changed_timestamp",
+ QJsonValue(double(entry->local_changed_timestamp)));
+ if (!entry->remote_changed_timestamp.isEmpty())
+ entryObj.insert("remote_changed_timestamp",
+ QJsonValue(entry->remote_changed_timestamp));
+ entriesArr.append(entryObj);
+ }
+ }
+ toplevel.insert("entries", entriesArr);
+ QJsonDocument doc(toplevel);
+ QByteArray jsonData = doc.toJson();
+ qint64 result = tfile.write(jsonData);
+ if (result == -1)
+ return;
+ if (result != jsonData.size())
+ return;
+ tfile.commit();
+}
diff --git a/logic/net/HttpMetaCache.h b/logic/net/HttpMetaCache.h
new file mode 100644
index 00000000..08b39fe2
--- /dev/null
+++ b/logic/net/HttpMetaCache.h
@@ -0,0 +1,75 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QString>
+#include <QMap>
+#include <qtimer.h>
+
+struct MetaEntry
+{
+ QString base;
+ QString path;
+ QString md5sum;
+ QString etag;
+ qint64 local_changed_timestamp = 0;
+ QString remote_changed_timestamp; // QString for now, RFC 2822 encoded time
+ bool stale = true;
+ QString getFullPath();
+};
+
+typedef std::shared_ptr<MetaEntry> MetaEntryPtr;
+
+class HttpMetaCache : public QObject
+{
+ Q_OBJECT
+public:
+ // supply path to the cache index file
+ HttpMetaCache(QString path);
+ ~HttpMetaCache();
+
+ // get the entry solely from the cache
+ // you probably don't want this, unless you have some specific caching needs.
+ MetaEntryPtr getEntry(QString base, QString resource_path);
+
+ // get the entry from cache and verify that it isn't stale (within reason)
+ MetaEntryPtr resolveEntry(QString base, QString resource_path,
+ QString expected_etag = QString());
+
+ // add a previously resolved stale entry
+ bool updateEntry(MetaEntryPtr stale_entry);
+
+ void addBase(QString base, QString base_root);
+
+ // (re)start a timer that calls SaveNow later.
+ void SaveEventually();
+ void Load();
+ QString getBasePath(QString base);
+public
+slots:
+ void SaveNow();
+
+private:
+ // create a new stale entry, given the parameters
+ MetaEntryPtr staleEntry(QString base, QString resource_path);
+ struct EntryMap
+ {
+ QString base_path;
+ QMap<QString, MetaEntryPtr> entry_list;
+ };
+ QMap<QString, EntryMap> m_entries;
+ QString m_index_file;
+ QTimer saveBatchingTimer;
+}; \ No newline at end of file
diff --git a/logic/net/MD5EtagDownload.cpp b/logic/net/MD5EtagDownload.cpp
new file mode 100644
index 00000000..63583e8d
--- /dev/null
+++ b/logic/net/MD5EtagDownload.cpp
@@ -0,0 +1,156 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MultiMC.h"
+#include "MD5EtagDownload.h"
+#include <pathutils.h>
+#include <QCryptographicHash>
+#include "logger/QsLog.h"
+
+MD5EtagDownload::MD5EtagDownload(QUrl url, QString target_path) : NetAction()
+{
+ m_url = url;
+ m_target_path = target_path;
+ m_status = Job_NotStarted;
+}
+
+void MD5EtagDownload::start()
+{
+ QString filename = m_target_path;
+ m_output_file.setFileName(filename);
+ // if there already is a file and md5 checking is in effect and it can be opened
+ if (m_output_file.exists() && m_output_file.open(QIODevice::ReadOnly))
+ {
+ // get the md5 of the local file.
+ m_local_md5 =
+ QCryptographicHash::hash(m_output_file.readAll(), QCryptographicHash::Md5)
+ .toHex()
+ .constData();
+ m_output_file.close();
+ // if we are expecting some md5sum, compare it with the local one
+ if (!m_expected_md5.isEmpty())
+ {
+ // skip if they match
+ if(m_local_md5 == m_expected_md5)
+ {
+ QLOG_INFO() << "Skipping " << m_url.toString() << ": md5 match.";
+ emit succeeded(m_index_within_job);
+ return;
+ }
+ }
+ else
+ {
+ // no expected md5. we use the local md5sum as an ETag
+ }
+ }
+ if (!ensureFilePathExists(filename))
+ {
+ emit failed(m_index_within_job);
+ return;
+ }
+
+ QNetworkRequest request(m_url);
+
+ QLOG_INFO() << "Downloading " << m_url.toString() << " got " << m_local_md5;
+
+ if(!m_local_md5.isEmpty())
+ {
+ QLOG_INFO() << "Got " << m_local_md5;
+ request.setRawHeader(QString("If-None-Match").toLatin1(), m_local_md5.toLatin1());
+ }
+ if(!m_expected_md5.isEmpty())
+ QLOG_INFO() << "Expecting " << m_expected_md5;
+
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
+
+ // Go ahead and try to open the file.
+ // If we don't do this, empty files won't be created, which breaks the updater.
+ // Plus, this way, we don't end up starting a download for a file we can't open.
+ if (!m_output_file.open(QIODevice::WriteOnly))
+ {
+ emit failed(m_index_within_job);
+ return;
+ }
+
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->get(request);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
+ SLOT(downloadProgress(qint64, qint64)));
+ connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
+}
+
+void MD5EtagDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
+{
+ m_total_progress = bytesTotal;
+ m_progress = bytesReceived;
+ emit progress(m_index_within_job, bytesReceived, bytesTotal);
+}
+
+void MD5EtagDownload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ // TODO: log the reason why
+ m_status = Job_Failed;
+}
+
+void MD5EtagDownload::downloadFinished()
+{
+ // if the download succeeded
+ if (m_status != Job_Failed)
+ {
+ // nothing went wrong...
+ m_status = Job_Finished;
+ m_output_file.close();
+
+ // FIXME: compare with the real written data md5sum
+ // this is just an ETag
+ QLOG_INFO() << "Finished " << m_url.toString() << " got " << m_reply->rawHeader("ETag").constData();
+
+ m_reply.reset();
+ emit succeeded(m_index_within_job);
+ return;
+ }
+ // else the download failed
+ else
+ {
+ m_output_file.close();
+ m_output_file.remove();
+ m_reply.reset();
+ emit failed(m_index_within_job);
+ return;
+ }
+}
+
+void MD5EtagDownload::downloadReadyRead()
+{
+ if (!m_output_file.isOpen())
+ {
+ if (!m_output_file.open(QIODevice::WriteOnly))
+ {
+ /*
+ * Can't open the file... the job failed
+ */
+ m_reply->abort();
+ emit failed(m_index_within_job);
+ return;
+ }
+ }
+ m_output_file.write(m_reply->readAll());
+}
diff --git a/logic/net/MD5EtagDownload.h b/logic/net/MD5EtagDownload.h
new file mode 100644
index 00000000..d5aed0ca
--- /dev/null
+++ b/logic/net/MD5EtagDownload.h
@@ -0,0 +1,51 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "NetAction.h"
+#include <QFile>
+
+typedef std::shared_ptr<class MD5EtagDownload> Md5EtagDownloadPtr;
+class MD5EtagDownload : public NetAction
+{
+ Q_OBJECT
+public:
+ /// the expected md5 checksum. Only set from outside
+ QString m_expected_md5;
+ /// the md5 checksum of a file that already exists.
+ QString m_local_md5;
+ /// if saving to file, use the one specified in this string
+ QString m_target_path;
+ /// this is the output file, if any
+ QFile m_output_file;
+
+public:
+ explicit MD5EtagDownload(QUrl url, QString target_path);
+ static Md5EtagDownloadPtr make(QUrl url, QString target_path)
+ {
+ return Md5EtagDownloadPtr(new MD5EtagDownload(url, target_path));
+ }
+protected
+slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+ virtual void downloadError(QNetworkReply::NetworkError error);
+ virtual void downloadFinished();
+ virtual void downloadReadyRead();
+
+public
+slots:
+ virtual void start();
+};
diff --git a/logic/net/NetAction.h b/logic/net/NetAction.h
new file mode 100644
index 00000000..97c96e5d
--- /dev/null
+++ b/logic/net/NetAction.h
@@ -0,0 +1,89 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QUrl>
+#include <memory>
+#include <QNetworkReply>
+
+enum JobStatus
+{
+ Job_NotStarted,
+ Job_InProgress,
+ Job_Finished,
+ Job_Failed
+};
+
+typedef std::shared_ptr<class NetAction> NetActionPtr;
+class NetAction : public QObject
+{
+ Q_OBJECT
+protected:
+ explicit NetAction() : QObject(0) {};
+
+public:
+ virtual ~NetAction() {};
+
+public:
+ virtual qint64 totalProgress() const
+ {
+ return m_total_progress;
+ }
+ virtual qint64 currentProgress() const
+ {
+ return m_progress;
+ }
+ virtual qint64 numberOfFailures() const
+ {
+ return m_failures;
+ }
+public:
+ /// the network reply
+ std::shared_ptr<QNetworkReply> m_reply;
+
+ /// source URL
+ QUrl m_url;
+
+ /// The file's status
+ JobStatus m_status = Job_NotStarted;
+
+ /// index within the parent job
+ int m_index_within_job = 0;
+
+ qint64 m_progress = 0;
+ qint64 m_total_progress = 1;
+
+ /// number of failures up to this point
+ int m_failures = 0;
+
+signals:
+ void started(int index);
+ void progress(int index, qint64 current, qint64 total);
+ void succeeded(int index);
+ void failed(int index);
+
+protected
+slots:
+ virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
+ virtual void downloadError(QNetworkReply::NetworkError error) = 0;
+ virtual void downloadFinished() = 0;
+ virtual void downloadReadyRead() = 0;
+
+public
+slots:
+ virtual void start() = 0;
+};
diff --git a/logic/net/NetJob.cpp b/logic/net/NetJob.cpp
new file mode 100644
index 00000000..9e800d13
--- /dev/null
+++ b/logic/net/NetJob.cpp
@@ -0,0 +1,112 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NetJob.h"
+#include "pathutils.h"
+#include "MultiMC.h"
+#include "MD5EtagDownload.h"
+#include "ByteArrayDownload.h"
+#include "CacheDownload.h"
+
+#include "logger/QsLog.h"
+
+void NetJob::partSucceeded(int index)
+{
+ // do progress. all slots are 1 in size at least
+ auto &slot = parts_progress[index];
+ partProgress(index, slot.total_progress, slot.total_progress);
+
+ num_succeeded++;
+ QLOG_INFO() << m_job_name.toLocal8Bit() << "progress:" << num_succeeded << "/"
+ << downloads.size();
+
+ if (num_failed + num_succeeded == downloads.size())
+ {
+ if (num_failed)
+ {
+ QLOG_ERROR() << m_job_name.toLocal8Bit() << "failed.";
+ emit failed();
+ }
+ else
+ {
+ QLOG_INFO() << m_job_name.toLocal8Bit() << "succeeded.";
+ emit succeeded();
+ }
+ }
+}
+
+void NetJob::partFailed(int index)
+{
+ auto &slot = parts_progress[index];
+ if (slot.failures == 3)
+ {
+ QLOG_ERROR() << "Part" << index << "failed 3 times (" << downloads[index]->m_url << ")";
+ num_failed++;
+ if (num_failed + num_succeeded == downloads.size())
+ {
+ QLOG_ERROR() << m_job_name.toLocal8Bit() << "failed.";
+ emit failed();
+ }
+ }
+ else
+ {
+ QLOG_ERROR() << "Part" << index << "failed, restarting (" << downloads[index]->m_url
+ << ")";
+ // restart the job
+ slot.failures++;
+ downloads[index]->start();
+ }
+}
+
+void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal)
+{
+ auto &slot = parts_progress[index];
+
+ current_progress -= slot.current_progress;
+ slot.current_progress = bytesReceived;
+ current_progress += slot.current_progress;
+
+ total_progress -= slot.total_progress;
+ slot.total_progress = bytesTotal;
+ total_progress += slot.total_progress;
+ emit progress(current_progress, total_progress);
+}
+
+void NetJob::start()
+{
+ QLOG_INFO() << m_job_name.toLocal8Bit() << " started.";
+ m_running = true;
+ for (auto iter : downloads)
+ {
+ connect(iter.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
+ connect(iter.get(), SIGNAL(failed(int)), SLOT(partFailed(int)));
+ connect(iter.get(), SIGNAL(progress(int, qint64, qint64)),
+ SLOT(partProgress(int, qint64, qint64)));
+ iter->start();
+ }
+}
+
+QStringList NetJob::getFailedFiles()
+{
+ QStringList failed;
+ for (auto download : downloads)
+ {
+ if (download->m_status == Job_Failed)
+ {
+ failed.push_back(download->m_url.toString());
+ }
+ }
+ return failed;
+}
diff --git a/logic/net/NetJob.h b/logic/net/NetJob.h
new file mode 100644
index 00000000..03d6a36e
--- /dev/null
+++ b/logic/net/NetJob.h
@@ -0,0 +1,124 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <QtNetwork>
+#include <QLabel>
+#include "NetAction.h"
+#include "ByteArrayDownload.h"
+#include "MD5EtagDownload.h"
+#include "CacheDownload.h"
+#include "HttpMetaCache.h"
+#include "ForgeXzDownload.h"
+#include "logic/tasks/ProgressProvider.h"
+
+class NetJob;
+typedef std::shared_ptr<NetJob> NetJobPtr;
+
+class NetJob : public ProgressProvider
+{
+ Q_OBJECT
+public:
+ explicit NetJob(QString job_name) : ProgressProvider(), m_job_name(job_name) {};
+
+ template <typename T> bool addNetAction(T action)
+ {
+ NetActionPtr base = std::static_pointer_cast<NetAction>(action);
+ base->m_index_within_job = downloads.size();
+ downloads.append(action);
+ part_info pi;
+ {
+ pi.current_progress = base->currentProgress();
+ pi.total_progress = base->totalProgress();
+ pi.failures = base->numberOfFailures();
+ }
+ parts_progress.append(pi);
+ total_progress += pi.total_progress;
+ // if this is already running, the action needs to be started right away!
+ if (isRunning())
+ {
+ emit progress(current_progress, total_progress);
+ connect(base.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int)));
+ connect(base.get(), SIGNAL(failed(int)), SLOT(partFailed(int)));
+ connect(base.get(), SIGNAL(progress(int, qint64, qint64)),
+ SLOT(partProgress(int, qint64, qint64)));
+ base->start();
+ }
+ return true;
+ }
+
+ NetActionPtr operator[](int index)
+ {
+ return downloads[index];
+ }
+ ;
+ NetActionPtr first()
+ {
+ if (downloads.size())
+ return downloads[0];
+ return NetActionPtr();
+ }
+ int size() const
+ {
+ return downloads.size();
+ }
+ virtual void getProgress(qint64 &current, qint64 &total)
+ {
+ current = current_progress;
+ total = total_progress;
+ }
+ ;
+ virtual QString getStatus() const
+ {
+ return m_job_name;
+ }
+ virtual bool isRunning() const
+ {
+ return m_running;
+ }
+ ;
+ QStringList getFailedFiles();
+signals:
+ void started();
+ void progress(qint64 current, qint64 total);
+ void succeeded();
+ void failed();
+public
+slots:
+ virtual void start();
+ // FIXME: implement
+ virtual void abort() {};
+private
+slots:
+ void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal);
+ void partSucceeded(int index);
+ void partFailed(int index);
+
+private:
+ struct part_info
+ {
+ qint64 current_progress = 0;
+ qint64 total_progress = 1;
+ int failures = 0;
+ };
+ QString m_job_name;
+ QList<NetActionPtr> downloads;
+ QList<part_info> parts_progress;
+ qint64 current_progress = 0;
+ qint64 total_progress = 0;
+ int num_succeeded = 0;
+ int num_failed = 0;
+ bool m_running = false;
+};
diff --git a/logic/net/PasteUpload.cpp b/logic/net/PasteUpload.cpp
new file mode 100644
index 00000000..fa54d084
--- /dev/null
+++ b/logic/net/PasteUpload.cpp
@@ -0,0 +1,86 @@
+#include "PasteUpload.h"
+#include "MultiMC.h"
+#include "logger/QsLog.h"
+#include <QJsonObject>
+#include <QJsonDocument>
+#include "gui/dialogs/CustomMessageBox.h"
+#include <QDesktopServices>
+
+PasteUpload::PasteUpload(QWidget *window, QString text) : m_text(text), m_window(window)
+{
+}
+
+void PasteUpload::executeTask()
+{
+ QNetworkRequest request(QUrl("http://paste.ee/api"));
+ request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
+ QByteArray content(
+ "key=public&description=MultiMC5+Log+File&language=plain&format=json&paste=" +
+ m_text.toUtf8());
+ request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
+ request.setRawHeader("Content-Length", QByteArray::number(content.size()));
+
+ auto worker = MMC->qnam();
+ QNetworkReply *rep = worker->post(request, content);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ connect(rep, &QNetworkReply::downloadProgress, [&](qint64 value, qint64 max)
+ { setProgress(value / max * 100); });
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this,
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
+}
+
+void PasteUpload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ QLOG_ERROR() << "Network error: " << error;
+ emitFailed(m_reply->errorString());
+}
+
+void PasteUpload::downloadFinished()
+{
+ // if the download succeeded
+ if (m_reply->error() == QNetworkReply::NetworkError::NoError)
+ {
+ QByteArray data = m_reply->readAll();
+ m_reply.reset();
+ QJsonParseError jsonError;
+ QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ emitFailed(jsonError.errorString());
+ return;
+ }
+ QString error;
+ if (!parseResult(doc, &error))
+ {
+ emitFailed(error);
+ return;
+ }
+ }
+ // else the download failed
+ else
+ {
+ emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
+ m_reply.reset();
+ return;
+ }
+ emitSucceeded();
+}
+
+bool PasteUpload::parseResult(QJsonDocument doc, QString *parseError)
+{
+ auto object = doc.object();
+ auto status = object.value("status").toString("error");
+ if (status == "error")
+ {
+ parseError = new QString(object.value("error").toString());
+ return false;
+ }
+ // FIXME: not the place for GUI things.
+ QString pasteUrl = object.value("paste").toObject().value("link").toString();
+ QDesktopServices::openUrl(pasteUrl);
+ return true;
+}
+
diff --git a/logic/net/PasteUpload.h b/logic/net/PasteUpload.h
new file mode 100644
index 00000000..917a0016
--- /dev/null
+++ b/logic/net/PasteUpload.h
@@ -0,0 +1,26 @@
+#pragma once
+#include "logic/tasks/Task.h"
+#include <QMessageBox>
+#include <QNetworkReply>
+#include <memory>
+
+class PasteUpload : public Task
+{
+ Q_OBJECT
+public:
+ PasteUpload(QWidget *window, QString text);
+
+protected:
+ virtual void executeTask();
+
+private:
+ bool parseResult(QJsonDocument doc, QString *parseError);
+ QString m_text;
+ QString m_error;
+ QWidget *m_window;
+ std::shared_ptr<QNetworkReply> m_reply;
+public
+slots:
+ void downloadError(QNetworkReply::NetworkError);
+ void downloadFinished();
+};
diff --git a/logic/net/URLConstants.h b/logic/net/URLConstants.h
new file mode 100644
index 00000000..8cb1f3fd
--- /dev/null
+++ b/logic/net/URLConstants.h
@@ -0,0 +1,36 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QString>
+
+namespace URLConstants
+{
+const QString AWS_DOWNLOAD_BASE("s3.amazonaws.com/Minecraft.Download/");
+const QString AWS_DOWNLOAD_VERSIONS(AWS_DOWNLOAD_BASE + "versions/");
+const QString AWS_DOWNLOAD_LIBRARIES(AWS_DOWNLOAD_BASE + "libraries/");
+const QString AWS_DOWNLOAD_INDEXES(AWS_DOWNLOAD_BASE + "indexes/");
+const QString ASSETS_BASE("assets.minecraft.net/");
+//const QString MCN_BASE("sonicrules.org/mcnweb.py");
+const QString RESOURCE_BASE("resources.download.minecraft.net/");
+const QString LIBRARY_BASE("libraries.minecraft.net/");
+const QString SKINS_BASE("skins.minecraft.net/MinecraftSkins/");
+const QString AUTH_BASE("authserver.mojang.com/");
+const QString FORGE_LEGACY_URL("http://files.minecraftforge.net/minecraftforge/json");
+const QString FORGE_GRADLE_URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/json");
+const QString MOJANG_STATUS_URL("http://status.mojang.com/check");
+const QString MOJANG_STATUS_NEWS_URL("http://status.mojang.com/news");
+}
diff --git a/logic/news/NewsChecker.cpp b/logic/news/NewsChecker.cpp
new file mode 100644
index 00000000..8fc44fa9
--- /dev/null
+++ b/logic/news/NewsChecker.cpp
@@ -0,0 +1,135 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NewsChecker.h"
+
+#include <QByteArray>
+#include <QDomDocument>
+
+#include <logger/QsLog.h>
+
+NewsChecker::NewsChecker(const QString& feedUrl)
+{
+ m_feedUrl = feedUrl;
+}
+
+void NewsChecker::reloadNews()
+{
+ // Start a netjob to download the RSS feed and call rssDownloadFinished() when it's done.
+ if (isLoadingNews())
+ {
+ QLOG_INFO() << "Ignored request to reload news. Currently reloading already.";
+ return;
+ }
+
+ QLOG_INFO() << "Reloading news.";
+
+ NetJob* job = new NetJob("News RSS Feed");
+ job->addNetAction(ByteArrayDownload::make(m_feedUrl));
+ QObject::connect(job, &NetJob::succeeded, this, &NewsChecker::rssDownloadFinished);
+ QObject::connect(job, &NetJob::failed, this, &NewsChecker::rssDownloadFailed);
+ m_newsNetJob.reset(job);
+ job->start();
+}
+
+void NewsChecker::rssDownloadFinished()
+{
+ // Parse the XML file and process the RSS feed entries.
+ QLOG_DEBUG() << "Finished loading RSS feed.";
+
+ QByteArray data;
+ {
+ ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_newsNetJob->first());
+ data = dl->m_data;
+ m_newsNetJob.reset();
+ }
+
+ QDomDocument doc;
+ {
+ // Stuff to store error info in.
+ QString errorMsg = "Unknown error.";
+ int errorLine = -1;
+ int errorCol = -1;
+
+ // Parse the XML.
+ if (!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol))
+ {
+ QString fullErrorMsg = QString("Error parsing RSS feed XML. %s at %d:%d.").arg(errorMsg, errorLine, errorCol);
+ fail(fullErrorMsg);
+ return;
+ }
+ }
+
+ // If the parsing succeeded, read it.
+ QDomNodeList items = doc.elementsByTagName("item");
+ m_newsEntries.clear();
+ for (int i = 0; i < items.length(); i++)
+ {
+ QDomElement element = items.at(i).toElement();
+ NewsEntryPtr entry;
+ entry.reset(new NewsEntry());
+ QString errorMsg = "An unknown error occurred.";
+ if (NewsEntry::fromXmlElement(element, entry.get(), &errorMsg))
+ {
+ QLOG_DEBUG() << "Loaded news entry" << entry->title;
+ m_newsEntries.append(entry);
+ }
+ else
+ {
+ QLOG_WARN() << "Failed to load news entry at index" << i << ":" << errorMsg;
+ }
+ }
+
+ succeed();
+}
+
+void NewsChecker::rssDownloadFailed()
+{
+ // Set an error message and fail.
+ fail("Failed to load news RSS feed.");
+}
+
+
+QList<NewsEntryPtr> NewsChecker::getNewsEntries() const
+{
+ return m_newsEntries;
+}
+
+bool NewsChecker::isLoadingNews() const
+{
+ return m_newsNetJob.get() != nullptr;
+}
+
+QString NewsChecker::getLastLoadErrorMsg() const
+{
+ return m_lastLoadError;
+}
+
+void NewsChecker::succeed()
+{
+ m_lastLoadError = "";
+ QLOG_DEBUG() << "News loading succeeded.";
+ m_newsNetJob.reset();
+ emit newsLoaded();
+}
+
+void NewsChecker::fail(const QString& errorMsg)
+{
+ m_lastLoadError = errorMsg;
+ QLOG_DEBUG() << "Failed to load news:" << errorMsg;
+ m_newsNetJob.reset();
+ emit newsLoadingFailed(errorMsg);
+}
+
diff --git a/logic/news/NewsChecker.h b/logic/news/NewsChecker.h
new file mode 100644
index 00000000..820fe626
--- /dev/null
+++ b/logic/news/NewsChecker.h
@@ -0,0 +1,105 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QString>
+#include <QList>
+
+#include <logic/net/NetJob.h>
+
+#include "NewsEntry.h"
+
+class NewsChecker : public QObject
+{
+ Q_OBJECT
+public:
+ /*!
+ * Constructs a news reader to read from the given RSS feed URL.
+ */
+ NewsChecker(const QString& feedUrl);
+
+ /*!
+ * Returns the error message for the last time the news was loaded.
+ * Empty string if the last load was successful.
+ */
+ QString getLastLoadErrorMsg() const;
+
+ /*!
+ * Returns true if the news has been loaded successfully.
+ */
+ bool isNewsLoaded() const;
+
+ //! True if the news is currently loading. If true, reloadNews() will do nothing.
+ bool isLoadingNews() const;
+
+ /*!
+ * Returns a list of news entries.
+ */
+ QList<NewsEntryPtr> getNewsEntries() const;
+
+ /*!
+ * Reloads the news from the website's RSS feed.
+ * If the news is already loading, this does nothing.
+ */
+ void Q_SLOT reloadNews();
+
+signals:
+ /*!
+ * Signal fired after the news has finished loading.
+ */
+ void newsLoaded();
+
+ /*!
+ * Signal fired after the news fails to load.
+ */
+ void newsLoadingFailed(QString errorMsg);
+
+protected slots:
+ void rssDownloadFinished();
+ void rssDownloadFailed();
+
+protected:
+ //! The URL for the RSS feed to fetch.
+ QString m_feedUrl;
+
+ //! List of news entries.
+ QList<NewsEntryPtr> m_newsEntries;
+
+ //! The network job to use to load the news.
+ NetJobPtr m_newsNetJob;
+
+ //! True if news has been loaded.
+ bool m_loadedNews;
+
+ /*!
+ * Gets the error message that was given last time the news was loaded.
+ * If the last news load succeeded, this will be an empty string.
+ */
+ QString m_lastLoadError;
+
+
+ /*!
+ * Emits newsLoaded() and sets m_lastLoadError to empty string.
+ */
+ void Q_SLOT succeed();
+
+ /*!
+ * Emits newsLoadingFailed() and sets m_lastLoadError to the given message.
+ */
+ void Q_SLOT fail(const QString& errorMsg);
+};
+
diff --git a/logic/news/NewsEntry.cpp b/logic/news/NewsEntry.cpp
new file mode 100644
index 00000000..4c940f2e
--- /dev/null
+++ b/logic/news/NewsEntry.cpp
@@ -0,0 +1,77 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NewsEntry.h"
+
+#include <QDomNodeList>
+#include <QVariant>
+
+NewsEntry::NewsEntry(QObject* parent) :
+ QObject(parent)
+{
+ this->title = tr("Untitled");
+ this->content = tr("No content.");
+ this->link = "";
+ this->author = tr("Unknown Author");
+ this->pubDate = QDateTime::currentDateTime();
+}
+
+NewsEntry::NewsEntry(const QString& title, const QString& content, const QString& link, const QString& author, const QDateTime& pubDate, QObject* parent) :
+ QObject(parent)
+{
+ this->title = title;
+ this->content = content;
+ this->link = link;
+ this->author = author;
+ this->pubDate = pubDate;
+}
+
+/*!
+ * Gets the text content of the given child element as a QVariant.
+ */
+inline QString childValue(const QDomElement& element, const QString& childName, QString defaultVal="")
+{
+ QDomNodeList nodes = element.elementsByTagName(childName);
+ if (nodes.count() > 0)
+ {
+ QDomElement element = nodes.at(0).toElement();
+ return element.text();
+ }
+ else
+ {
+ return defaultVal;
+ }
+}
+
+bool NewsEntry::fromXmlElement(const QDomElement& element, NewsEntry* entry, QString* errorMsg)
+{
+ QString title = childValue(element, "title", tr("Untitled"));
+ QString content = childValue(element, "description", tr("No content."));
+ QString link = childValue(element, "link");
+ QString author = childValue(element, "dc:creator", tr("Unknown Author"));
+ QString pubDateStr = childValue(element, "pubDate");
+
+ // FIXME: For now, we're just ignoring timezones. We assume that all time zones in the RSS feed are the same.
+ QString dateFormat("ddd, dd MMM yyyy hh:mm:ss");
+ QDateTime pubDate = QDateTime::fromString(pubDateStr, dateFormat);
+
+ entry->title = title;
+ entry->content = content;
+ entry->link = link;
+ entry->author = author;
+ entry->pubDate = pubDate;
+ return true;
+}
+
diff --git a/logic/news/NewsEntry.h b/logic/news/NewsEntry.h
new file mode 100644
index 00000000..6bfa1adc
--- /dev/null
+++ b/logic/news/NewsEntry.h
@@ -0,0 +1,65 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QString>
+#include <QDomElement>
+#include <QDateTime>
+
+#include <memory>
+
+class NewsEntry : public QObject
+{
+ Q_OBJECT
+
+public:
+ /*!
+ * Constructs an empty news entry.
+ */
+ explicit NewsEntry(QObject* parent=0);
+
+ /*!
+ * Constructs a new news entry.
+ * Note that content may contain HTML.
+ */
+ NewsEntry(const QString& title, const QString& content, const QString& link, const QString& author, const QDateTime& pubDate, QObject* parent=0);
+
+ /*!
+ * Attempts to load information from the given XML element into the given news entry pointer.
+ * If this fails, the function will return false and store an error message in the errorMsg pointer.
+ */
+ static bool fromXmlElement(const QDomElement& element, NewsEntry* entry, QString* errorMsg=0);
+
+
+ //! The post title.
+ QString title;
+
+ //! The post's content. May contain HTML.
+ QString content;
+
+ //! URL to the post.
+ QString link;
+
+ //! The post's author.
+ QString author;
+
+ //! The date and time that this post was published.
+ QDateTime pubDate;
+};
+
+typedef std::shared_ptr<NewsEntry> NewsEntryPtr;
+
diff --git a/logic/status/StatusChecker.cpp b/logic/status/StatusChecker.cpp
new file mode 100644
index 00000000..66f800ae
--- /dev/null
+++ b/logic/status/StatusChecker.cpp
@@ -0,0 +1,137 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "StatusChecker.h"
+
+#include <logic/net/URLConstants.h>
+
+#include <QByteArray>
+#include <QDomDocument>
+
+#include <logger/QsLog.h>
+
+StatusChecker::StatusChecker()
+{
+
+}
+
+void StatusChecker::reloadStatus()
+{
+ if (isLoadingStatus())
+ {
+ // QLOG_INFO() << "Ignored request to reload status. Currently reloading already.";
+ return;
+ }
+
+ // QLOG_INFO() << "Reloading status.";
+
+ NetJob* job = new NetJob("Status JSON");
+ job->addNetAction(ByteArrayDownload::make(URLConstants::MOJANG_STATUS_URL));
+ QObject::connect(job, &NetJob::succeeded, this, &StatusChecker::statusDownloadFinished);
+ QObject::connect(job, &NetJob::failed, this, &StatusChecker::statusDownloadFailed);
+ m_statusNetJob.reset(job);
+ job->start();
+}
+
+void StatusChecker::statusDownloadFinished()
+{
+ QLOG_DEBUG() << "Finished loading status JSON.";
+
+ QByteArray data;
+ {
+ ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_statusNetJob->first());
+ data = dl->m_data;
+ m_statusNetJob.reset();
+ }
+
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ fail("Error parsing status JSON:" + jsonError.errorString());
+ return;
+ }
+
+ if (!jsonDoc.isArray())
+ {
+ fail("Error parsing status JSON: JSON root is not an array");
+ return;
+ }
+
+ QJsonArray root = jsonDoc.array();
+
+ for(auto status = root.begin(); status != root.end(); ++status)
+ {
+ QVariantMap map = (*status).toObject().toVariantMap();
+
+ for (QVariantMap::const_iterator iter = map.begin(); iter != map.end(); ++iter)
+ {
+ QString key = iter.key();
+ QVariant value = iter.value();
+
+ if(value.type() == QVariant::Type::String)
+ {
+ m_statusEntries.insert(key, value.toString());
+ //QLOG_DEBUG() << "Status JSON object: " << key << m_statusEntries[key];
+ }
+ else
+ {
+ fail("Malformed status JSON: expected status type to be a string.");
+ return;
+ }
+ }
+ }
+
+ succeed();
+}
+
+void StatusChecker::statusDownloadFailed()
+{
+ fail("Failed to load status JSON.");
+}
+
+
+QMap<QString, QString> StatusChecker::getStatusEntries() const
+{
+ return m_statusEntries;
+}
+
+bool StatusChecker::isLoadingStatus() const
+{
+ return m_statusNetJob.get() != nullptr;
+}
+
+QString StatusChecker::getLastLoadErrorMsg() const
+{
+ return m_lastLoadError;
+}
+
+void StatusChecker::succeed()
+{
+ m_lastLoadError = "";
+ QLOG_DEBUG() << "Status loading succeeded.";
+ m_statusNetJob.reset();
+ emit statusLoaded();
+}
+
+void StatusChecker::fail(const QString& errorMsg)
+{
+ m_lastLoadError = errorMsg;
+ QLOG_DEBUG() << "Failed to load status:" << errorMsg;
+ m_statusNetJob.reset();
+ emit statusLoadingFailed(errorMsg);
+}
+
diff --git a/logic/status/StatusChecker.h b/logic/status/StatusChecker.h
new file mode 100644
index 00000000..1cb01836
--- /dev/null
+++ b/logic/status/StatusChecker.h
@@ -0,0 +1,57 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QString>
+#include <QList>
+
+#include <logic/net/NetJob.h>
+
+class StatusChecker : public QObject
+{
+ Q_OBJECT
+public:
+ StatusChecker();
+
+ QString getLastLoadErrorMsg() const;
+
+ bool isStatusLoaded() const;
+
+ bool isLoadingStatus() const;
+
+ QMap<QString, QString> getStatusEntries() const;
+
+ void Q_SLOT reloadStatus();
+
+signals:
+ void statusLoaded();
+ void statusLoadingFailed(QString errorMsg);
+
+protected slots:
+ void statusDownloadFinished();
+ void statusDownloadFailed();
+
+protected:
+ QMap<QString, QString> m_statusEntries;
+ NetJobPtr m_statusNetJob;
+ bool m_loadedStatus;
+ QString m_lastLoadError;
+
+ void Q_SLOT succeed();
+ void Q_SLOT fail(const QString& errorMsg);
+};
+
diff --git a/logic/tasks/ProgressProvider.h b/logic/tasks/ProgressProvider.h
new file mode 100644
index 00000000..15e453a3
--- /dev/null
+++ b/logic/tasks/ProgressProvider.h
@@ -0,0 +1,42 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+
+class ProgressProvider : public QObject
+{
+ Q_OBJECT
+protected:
+ explicit ProgressProvider(QObject *parent = 0) : QObject(parent)
+ {
+ }
+signals:
+ void started();
+ void progress(qint64 current, qint64 total);
+ void succeeded();
+ void failed(QString reason);
+ void status(QString status);
+
+public:
+ virtual QString getStatus() const = 0;
+ virtual void getProgress(qint64 &current, qint64 &total) = 0;
+ virtual bool isRunning() const = 0;
+public
+slots:
+ virtual void start() = 0;
+ virtual void abort() = 0;
+};
diff --git a/logic/tasks/SequentialTask.cpp b/logic/tasks/SequentialTask.cpp
new file mode 100644
index 00000000..63025eee
--- /dev/null
+++ b/logic/tasks/SequentialTask.cpp
@@ -0,0 +1,77 @@
+#include "SequentialTask.h"
+
+SequentialTask::SequentialTask(QObject *parent) :
+ Task(parent), m_currentIndex(-1)
+{
+
+}
+
+QString SequentialTask::getStatus() const
+{
+ if (m_queue.isEmpty() || m_currentIndex >= m_queue.size())
+ {
+ return QString();
+ }
+ return m_queue.at(m_currentIndex)->getStatus();
+}
+
+void SequentialTask::getProgress(qint64 &current, qint64 &total)
+{
+ current = 0;
+ total = 0;
+ for (int i = 0; i < m_queue.size(); ++i)
+ {
+ qint64 subCurrent, subTotal;
+ m_queue.at(i)->getProgress(subCurrent, subTotal);
+ current += subCurrent;
+ total += subTotal;
+ }
+}
+
+void SequentialTask::addTask(std::shared_ptr<Task> task)
+{
+ m_queue.append(task);
+}
+
+void SequentialTask::executeTask()
+{
+ m_currentIndex = -1;
+ startNext();
+}
+
+void SequentialTask::startNext()
+{
+ if (m_currentIndex != -1)
+ {
+ std::shared_ptr<Task> previous = m_queue[m_currentIndex];
+ disconnect(previous.get(), 0, this, 0);
+ }
+ m_currentIndex++;
+ if (m_queue.isEmpty() || m_currentIndex >= m_queue.size())
+ {
+ emitSucceeded();
+ return;
+ }
+ std::shared_ptr<Task> next = m_queue[m_currentIndex];
+ connect(next.get(), SIGNAL(failed(QString)), this, SLOT(subTaskFailed(QString)));
+ connect(next.get(), SIGNAL(status(QString)), this, SLOT(subTaskStatus(QString)));
+ connect(next.get(), SIGNAL(progress(qint64,qint64)), this, SLOT(subTaskProgress()));
+ connect(next.get(), SIGNAL(succeeded()), this, SLOT(startNext()));
+ next->start();
+ emit status(getStatus());
+}
+
+void SequentialTask::subTaskFailed(const QString &msg)
+{
+ emitFailed(msg);
+}
+void SequentialTask::subTaskStatus(const QString &msg)
+{
+ setStatus(msg);
+}
+void SequentialTask::subTaskProgress()
+{
+ qint64 current, total;
+ getProgress(current, total);
+ setProgress(100 * current / total);
+}
diff --git a/logic/tasks/SequentialTask.h b/logic/tasks/SequentialTask.h
new file mode 100644
index 00000000..7f046928
--- /dev/null
+++ b/logic/tasks/SequentialTask.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "Task.h"
+
+#include <QQueue>
+#include <memory>
+
+class SequentialTask : public Task
+{
+ Q_OBJECT
+public:
+ explicit SequentialTask(QObject *parent = 0);
+
+ virtual QString getStatus() const;
+ virtual void getProgress(qint64 &current, qint64 &total);
+
+ void addTask(std::shared_ptr<Task> task);
+
+protected:
+ void executeTask();
+
+private
+slots:
+ void startNext();
+ void subTaskFailed(const QString &msg);
+ void subTaskStatus(const QString &msg);
+ void subTaskProgress();
+
+private:
+ QQueue<std::shared_ptr<Task> > m_queue;
+ int m_currentIndex;
+};
diff --git a/logic/tasks/Task.cpp b/logic/tasks/Task.cpp
new file mode 100644
index 00000000..cb7a5443
--- /dev/null
+++ b/logic/tasks/Task.cpp
@@ -0,0 +1,84 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Task.h"
+#include "logger/QsLog.h"
+
+Task::Task(QObject *parent) : ProgressProvider(parent)
+{
+}
+
+QString Task::getStatus() const
+{
+ return m_status;
+}
+
+void Task::setStatus(const QString &new_status)
+{
+ m_status = new_status;
+ emit status(new_status);
+}
+
+void Task::setProgress(int new_progress)
+{
+ m_progress = new_progress;
+ emit progress(new_progress, 100);
+}
+
+void Task::getProgress(qint64 &current, qint64 &total)
+{
+ current = m_progress;
+ total = 100;
+}
+
+void Task::start()
+{
+ m_running = true;
+ emit started();
+ executeTask();
+}
+
+void Task::emitFailed(QString reason)
+{
+ m_running = false;
+ m_succeeded = false;
+ m_failReason = reason;
+ QLOG_ERROR() << "Task failed: " << reason;
+ emit failed(reason);
+}
+
+void Task::emitSucceeded()
+{
+ m_running = false;
+ m_succeeded = true;
+ QLOG_INFO() << "Task succeeded";
+ emit succeeded();
+}
+
+bool Task::isRunning() const
+{
+ return m_running;
+}
+
+bool Task::successful() const
+{
+ return m_succeeded;
+}
+
+QString Task::failReason() const
+{
+ return m_failReason;
+}
+
diff --git a/logic/tasks/Task.h b/logic/tasks/Task.h
new file mode 100644
index 00000000..80d5e38b
--- /dev/null
+++ b/logic/tasks/Task.h
@@ -0,0 +1,66 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <QObject>
+#include <QString>
+#include "ProgressProvider.h"
+
+class Task : public ProgressProvider
+{
+ Q_OBJECT
+public:
+ explicit Task(QObject *parent = 0);
+
+ virtual QString getStatus() const;
+ virtual void getProgress(qint64 &current, qint64 &total);
+ virtual bool isRunning() const;
+
+ /*!
+ * True if this task was successful.
+ * If the task failed or is still running, returns false.
+ */
+ virtual bool successful() const;
+
+ /*!
+ * Returns the string that was passed to emitFailed as the error message when the task failed.
+ * If the task hasn't failed, returns an empty string.
+ */
+ virtual QString failReason() const;
+
+public
+slots:
+ virtual void start();
+ virtual void abort() {};
+
+protected:
+ virtual void executeTask() = 0;
+
+ virtual void emitSucceeded();
+ virtual void emitFailed(QString reason);
+
+protected
+slots:
+ void setStatus(const QString &status);
+ void setProgress(int progress);
+
+protected:
+ QString m_status;
+ int m_progress = 0;
+ bool m_running = false;
+ bool m_succeeded = false;
+ QString m_failReason = "";
+};
diff --git a/logic/tasks/ThreadTask.cpp b/logic/tasks/ThreadTask.cpp
new file mode 100644
index 00000000..ddd1dee5
--- /dev/null
+++ b/logic/tasks/ThreadTask.cpp
@@ -0,0 +1,41 @@
+#include "ThreadTask.h"
+#include <QtConcurrentRun>
+ThreadTask::ThreadTask(Task * internal, QObject *parent) : Task(parent), m_internal(internal)
+{
+}
+
+void ThreadTask::start()
+{
+ connect(m_internal, SIGNAL(failed(QString)), SLOT(iternal_failed(QString)));
+ connect(m_internal, SIGNAL(progress(qint64,qint64)), SLOT(iternal_progress(qint64,qint64)));
+ connect(m_internal, SIGNAL(started()), SLOT(iternal_started()));
+ connect(m_internal, SIGNAL(status(QString)), SLOT(iternal_status(QString)));
+ connect(m_internal, SIGNAL(succeeded()), SLOT(iternal_succeeded()));
+ m_running = true;
+ QtConcurrent::run(m_internal, &Task::start);
+}
+
+void ThreadTask::iternal_failed(QString reason)
+{
+ emitFailed(reason);
+}
+
+void ThreadTask::iternal_progress(qint64 current, qint64 total)
+{
+ progress(current, total);
+}
+
+void ThreadTask::iternal_started()
+{
+ emit started();
+}
+
+void ThreadTask::iternal_status(QString status)
+{
+ setStatus(status);
+}
+
+void ThreadTask::iternal_succeeded()
+{
+ emitSucceeded();
+}
diff --git a/logic/tasks/ThreadTask.h b/logic/tasks/ThreadTask.h
new file mode 100644
index 00000000..718dbc91
--- /dev/null
+++ b/logic/tasks/ThreadTask.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "Task.h"
+
+class ThreadTask : public Task
+{
+ Q_OBJECT
+public:
+ explicit ThreadTask(Task * internal, QObject * parent = nullptr);
+
+protected:
+ void executeTask() {};
+
+public slots:
+ virtual void start();
+
+private slots:
+ void iternal_started();
+ void iternal_progress(qint64 current, qint64 total);
+ void iternal_succeeded();
+ void iternal_failed(QString reason);
+ void iternal_status(QString status);
+private:
+ Task * m_internal;
+}; \ No newline at end of file
diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp
new file mode 100644
index 00000000..83679f19
--- /dev/null
+++ b/logic/updater/DownloadUpdateTask.cpp
@@ -0,0 +1,543 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DownloadUpdateTask.h"
+
+#include "MultiMC.h"
+#include "logic/updater/UpdateChecker.h"
+#include "logic/net/NetJob.h"
+#include "pathutils.h"
+
+#include <QFile>
+#include <QTemporaryDir>
+#include <QCryptographicHash>
+
+#include <QDomDocument>
+
+DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject *parent)
+ : Task(parent)
+{
+ m_cVersionId = MMC->version().build;
+
+ m_nRepoUrl = repoUrl;
+ m_nVersionId = versionId;
+
+ m_updateFilesDir.setAutoRemove(false);
+}
+
+void DownloadUpdateTask::executeTask()
+{
+ // GO!
+ // This will call the next step when it's done.
+ findCurrentVersionInfo();
+}
+
+void DownloadUpdateTask::processChannels()
+{
+ auto checker = MMC->updateChecker();
+
+ // Now, check the channel list again.
+ if (!checker->hasChannels())
+ {
+ // We still couldn't load the channel list. Give up. Call loadVersionInfo and return.
+ QLOG_INFO() << "Reloading the channel list didn't work. Giving up.";
+ loadVersionInfo();
+ return;
+ }
+
+ QList<UpdateChecker::ChannelListEntry> channels = checker->getChannelList();
+ QString channelId = MMC->version().channel;
+
+ m_cRepoUrl.clear();
+ // Search through the channel list for a channel with the correct ID.
+ for (auto channel : channels)
+ {
+ if (channel.id == channelId)
+ {
+ QLOG_INFO() << "Found matching channel.";
+ m_cRepoUrl = channel.url;
+ break;
+ }
+ }
+
+ // Now that we've done that, load version info.
+ loadVersionInfo();
+}
+
+void DownloadUpdateTask::findCurrentVersionInfo()
+{
+ setStatus(tr("Finding information about the current version..."));
+
+ auto checker = MMC->updateChecker();
+
+ if (!checker->hasChannels())
+ {
+ // Load the channel list and wait for it to finish loading.
+ QLOG_INFO() << "No channel list entries found. Will try reloading it.";
+
+ QObject::connect(checker.get(), &UpdateChecker::channelListLoaded, this,
+ &DownloadUpdateTask::processChannels);
+ checker->updateChanList();
+ }
+ else
+ {
+ processChannels();
+ }
+}
+
+void DownloadUpdateTask::loadVersionInfo()
+{
+ setStatus(tr("Loading version information..."));
+
+ // Create the net job for loading version info.
+ NetJob *netJob = new NetJob("Version Info");
+
+ // Find the index URL.
+ QUrl newIndexUrl = QUrl(m_nRepoUrl).resolved(QString::number(m_nVersionId) + ".json");
+ QLOG_DEBUG() << m_nRepoUrl << " turns into " << newIndexUrl;
+
+ // Add a net action to download the version info for the version we're updating to.
+ netJob->addNetAction(ByteArrayDownload::make(newIndexUrl));
+
+ // If we have a current version URL, get that one too.
+ if (!m_cRepoUrl.isEmpty())
+ {
+ QUrl cIndexUrl = QUrl(m_cRepoUrl).resolved(QString::number(m_cVersionId) + ".json");
+ netJob->addNetAction(ByteArrayDownload::make(cIndexUrl));
+ QLOG_DEBUG() << m_cRepoUrl << " turns into " << cIndexUrl;
+ }
+
+ // Connect slots so we know when it's done.
+ QObject::connect(netJob, &NetJob::succeeded, this,
+ &DownloadUpdateTask::vinfoDownloadFinished);
+ QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::vinfoDownloadFailed);
+
+ // Store the NetJob in a class member. We don't want to lose it!
+ m_vinfoNetJob.reset(netJob);
+
+ // Finally, we start the network job and the thread's event loop to wait for it to finish.
+ netJob->start();
+}
+
+void DownloadUpdateTask::vinfoDownloadFinished()
+{
+ // Both downloads succeeded. OK. Parse stuff.
+ parseDownloadedVersionInfo();
+}
+
+void DownloadUpdateTask::vinfoDownloadFailed()
+{
+ // Something failed. We really need the second download (current version info), so parse
+ // downloads anyways as long as the first one succeeded.
+ if (m_vinfoNetJob->first()->m_status != Job_Failed)
+ {
+ parseDownloadedVersionInfo();
+ return;
+ }
+
+ // TODO: Give a more detailed error message.
+ QLOG_ERROR() << "Failed to download version info files.";
+ emitFailed(tr("Failed to download version info files."));
+}
+
+void DownloadUpdateTask::parseDownloadedVersionInfo()
+{
+ setStatus(tr("Reading file list for new version..."));
+ QLOG_DEBUG() << "Reading file list for new version...";
+ QString error;
+ if (!parseVersionInfo(
+ std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->first())->m_data,
+ &m_nVersionFileList, &error))
+ {
+ emitFailed(error);
+ return;
+ }
+
+ // If there is a second entry in the network job's list, load it as the current version's
+ // info.
+ if (m_vinfoNetJob->size() >= 2 && m_vinfoNetJob->operator[](1)->m_status != Job_Failed)
+ {
+ setStatus(tr("Reading file list for current version..."));
+ QLOG_DEBUG() << "Reading file list for current version...";
+ QString error;
+ parseVersionInfo(
+ std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->operator[](1))->m_data,
+ &m_cVersionFileList, &error);
+ }
+
+ // We don't need this any more.
+ m_vinfoNetJob.reset();
+
+ // Now that we're done loading version info, we can move on to the next step. Process file
+ // lists and download files.
+ processFileLists();
+}
+
+bool DownloadUpdateTask::parseVersionInfo(const QByteArray &data, VersionFileList *list,
+ QString *error)
+{
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ *error = QString("Failed to parse version info JSON: %1 at %2")
+ .arg(jsonError.errorString())
+ .arg(jsonError.offset);
+ QLOG_ERROR() << error;
+ return false;
+ }
+
+ QJsonObject json = jsonDoc.object();
+
+ QLOG_DEBUG() << data;
+ QLOG_DEBUG() << "Loading version info from JSON.";
+ QJsonArray filesArray = json.value("Files").toArray();
+ for (QJsonValue fileValue : filesArray)
+ {
+ QJsonObject fileObj = fileValue.toObject();
+
+ QString file_path = fileObj.value("Path").toString();
+#ifdef Q_OS_MAC
+ // On OSX, the paths for the updater need to be fixed.
+ // basically, anything that isn't in the .app folder is ignored.
+ // everything else is changed so the code that processes the files actually finds
+ // them and puts the replacements in the right spots.
+ if (!fixPathForOSX(file_path))
+ continue;
+#endif
+ VersionFileEntry file{file_path, fileObj.value("Perms").toVariant().toInt(),
+ FileSourceList(), fileObj.value("MD5").toString(), };
+ QLOG_DEBUG() << "File" << file.path << "with perms" << file.mode;
+
+ QJsonArray sourceArray = fileObj.value("Sources").toArray();
+ for (QJsonValue val : sourceArray)
+ {
+ QJsonObject sourceObj = val.toObject();
+
+ QString type = sourceObj.value("SourceType").toString();
+ if (type == "http")
+ {
+ file.sources.append(
+ FileSource("http", sourceObj.value("Url").toString()));
+ }
+ else if (type == "httpc")
+ {
+ file.sources.append(
+ FileSource("httpc", sourceObj.value("Url").toString(),
+ sourceObj.value("CompressionType").toString()));
+ }
+ else
+ {
+ QLOG_WARN() << "Unknown source type" << type << "ignored.";
+ }
+ }
+
+ QLOG_DEBUG() << "Loaded info for" << file.path;
+
+ list->append(file);
+ }
+
+ return true;
+}
+
+void DownloadUpdateTask::processFileLists()
+{
+ // Create a network job for downloading files.
+ NetJob *netJob = new NetJob("Update Files");
+
+ if (!processFileLists(netJob, m_cVersionFileList, m_nVersionFileList, m_operationList))
+ {
+ emitFailed(tr("Failed to process update lists..."));
+ return;
+ }
+
+ // Add listeners to wait for the downloads to finish.
+ QObject::connect(netJob, &NetJob::succeeded, this,
+ &DownloadUpdateTask::fileDownloadFinished);
+ QObject::connect(netJob, &NetJob::progress, this,
+ &DownloadUpdateTask::fileDownloadProgressChanged);
+ QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::fileDownloadFailed);
+
+ // Now start the download.
+ setStatus(tr("Downloading %1 update files.").arg(QString::number(netJob->size())));
+ QLOG_DEBUG() << "Begin downloading update files to" << m_updateFilesDir.path();
+ m_filesNetJob.reset(netJob);
+ netJob->start();
+
+ writeInstallScript(m_operationList, PathCombine(m_updateFilesDir.path(), "file_list.xml"));
+}
+
+bool
+DownloadUpdateTask::processFileLists(NetJob *job,
+ const DownloadUpdateTask::VersionFileList &currentVersion,
+ const DownloadUpdateTask::VersionFileList &newVersion,
+ DownloadUpdateTask::UpdateOperationList &ops)
+{
+ setStatus(tr("Processing file lists - figuring out how to install the update..."));
+
+ // First, if we've loaded the current version's file list, we need to iterate through it and
+ // delete anything in the current one version's list that isn't in the new version's list.
+ for (VersionFileEntry entry : currentVersion)
+ {
+ QFileInfo toDelete(PathCombine(MMC->root(), entry.path));
+ if (!toDelete.exists())
+ {
+ QLOG_ERROR() << "Expected file " << toDelete.absoluteFilePath()
+ << " doesn't exist!";
+ }
+ bool keep = false;
+
+ //
+ for (VersionFileEntry newEntry : newVersion)
+ {
+ if (newEntry.path == entry.path)
+ {
+ QLOG_DEBUG() << "Not deleting" << entry.path
+ << "because it is still present in the new version.";
+ keep = true;
+ break;
+ }
+ }
+
+ // If the loop reaches the end and we didn't find a match, delete the file.
+ if (!keep)
+ {
+ if (toDelete.exists())
+ ops.append(UpdateOperation::DeleteOp(entry.path));
+ }
+ }
+
+ // Next, check each file in MultiMC's folder and see if we need to update them.
+ for (VersionFileEntry entry : newVersion)
+ {
+ // TODO: Let's not MD5sum a ton of files on the GUI thread. We should probably find a
+ // way to do this in the background.
+ QString fileMD5;
+ QString realEntryPath = PathCombine(MMC->root(), entry.path);
+ QFile entryFile(realEntryPath);
+ QFileInfo entryInfo(realEntryPath);
+
+ bool needs_upgrade = false;
+ if (!entryFile.exists())
+ {
+ needs_upgrade = true;
+ }
+ else
+ {
+ bool pass = true;
+ if (!entryInfo.isReadable())
+ {
+ QLOG_ERROR() << "File " << realEntryPath << " is not readable.";
+ pass = false;
+ }
+ if (!entryInfo.isWritable())
+ {
+ QLOG_ERROR() << "File " << realEntryPath << " is not writable.";
+ pass = false;
+ }
+ if (!entryFile.open(QFile::ReadOnly))
+ {
+ QLOG_ERROR() << "File " << realEntryPath << " cannot be opened for reading.";
+ pass = false;
+ }
+ if (!pass)
+ {
+ QLOG_ERROR() << "ROOT: " << MMC->root();
+ ops.clear();
+ return false;
+ }
+ }
+
+ if(!needs_upgrade)
+ {
+ QCryptographicHash hash(QCryptographicHash::Md5);
+ auto foo = entryFile.readAll();
+
+ hash.addData(foo);
+ fileMD5 = hash.result().toHex();
+ if ((fileMD5 != entry.md5))
+ {
+ QLOG_DEBUG() << "MD5Sum does not match!";
+ QLOG_DEBUG() << "Expected:'" << entry.md5 << "'";
+ QLOG_DEBUG() << "Got: '" << fileMD5 << "'";
+ needs_upgrade = true;
+ }
+ }
+
+ // skip file. it doesn't need an upgrade.
+ if (!needs_upgrade)
+ {
+ QLOG_DEBUG() << "File" << realEntryPath << " does not need updating.";
+ continue;
+ }
+
+ // yep. this file actually needs an upgrade. PROCEED.
+ QLOG_DEBUG() << "Found file" << realEntryPath << " that needs updating.";
+
+ // if it's the updater we want to treat it separately
+ bool isUpdater = entry.path.endsWith("updater") || entry.path.endsWith("updater.exe");
+
+ // Go through the sources list and find one to use.
+ // TODO: Make a NetAction that takes a source list and tries each of them until one
+ // works. For now, we'll just use the first http one.
+ for (FileSource source : entry.sources)
+ {
+ if (source.type == "http")
+ {
+ QLOG_DEBUG() << "Will download" << entry.path << "from" << source.url;
+
+ // Download it to updatedir/<filepath>-<md5> where filepath is the file's
+ // path with slashes replaced by underscores.
+ QString dlPath =
+ PathCombine(m_updateFilesDir.path(), QString(entry.path).replace("/", "_"));
+
+ if (isUpdater)
+ {
+#ifdef MultiMC_UPDATER_FORCE_LOCAL
+ QLOG_DEBUG() << "Skipping updater download and using local version.";
+#else
+ auto cache_entry = MMC->metacache()->resolveEntry("root", entry.path);
+ QLOG_DEBUG() << "Updater will be in " << cache_entry->getFullPath();
+ // force check.
+ cache_entry->stale = true;
+
+ auto download = CacheDownload::make(QUrl(source.url), cache_entry);
+ job->addNetAction(download);
+#endif
+ }
+ else
+ {
+ // We need to download the file to the updatefiles folder and add a task
+ // to copy it to its install path.
+ auto download = MD5EtagDownload::make(source.url, dlPath);
+ download->m_expected_md5 = entry.md5;
+ job->addNetAction(download);
+ ops.append(UpdateOperation::CopyOp(dlPath, entry.path, entry.mode));
+ }
+ }
+ }
+ }
+ return true;
+}
+
+bool DownloadUpdateTask::writeInstallScript(UpdateOperationList &opsList, QString scriptFile)
+{
+ // Build the base structure of the XML document.
+ QDomDocument doc;
+
+ QDomElement root = doc.createElement("update");
+ root.setAttribute("version", "3");
+ doc.appendChild(root);
+
+ QDomElement installFiles = doc.createElement("install");
+ root.appendChild(installFiles);
+
+ QDomElement removeFiles = doc.createElement("uninstall");
+ root.appendChild(removeFiles);
+
+ // Write the operation list to the XML document.
+ for (UpdateOperation op : opsList)
+ {
+ QDomElement file = doc.createElement("file");
+
+ switch (op.type)
+ {
+ case UpdateOperation::OP_COPY:
+ {
+ // Install the file.
+ QDomElement name = doc.createElement("source");
+ QDomElement path = doc.createElement("dest");
+ QDomElement mode = doc.createElement("mode");
+ name.appendChild(doc.createTextNode(op.file));
+ path.appendChild(doc.createTextNode(op.dest));
+ // We need to add a 0 at the beginning here, because Qt doesn't convert to octal
+ // correctly.
+ mode.appendChild(doc.createTextNode("0" + QString::number(op.mode, 8)));
+ file.appendChild(name);
+ file.appendChild(path);
+ file.appendChild(mode);
+ installFiles.appendChild(file);
+ QLOG_DEBUG() << "Will install file " << op.file << " to " << op.dest;
+ }
+ break;
+
+ case UpdateOperation::OP_DELETE:
+ {
+ // Delete the file.
+ file.appendChild(doc.createTextNode(op.file));
+ removeFiles.appendChild(file);
+ QLOG_DEBUG() << "Will remove file" << op.file;
+ }
+ break;
+
+ default:
+ QLOG_WARN() << "Can't write update operation of type" << op.type
+ << "to file. Not implemented.";
+ continue;
+ }
+ }
+
+ // Write the XML document to the file.
+ QFile outFile(scriptFile);
+
+ if (outFile.open(QIODevice::WriteOnly))
+ {
+ outFile.write(doc.toByteArray());
+ }
+ else
+ {
+ emitFailed(tr("Failed to write update script file."));
+ return false;
+ }
+
+ return true;
+}
+
+bool DownloadUpdateTask::fixPathForOSX(QString &path)
+{
+ if (path.startsWith("MultiMC.app/"))
+ {
+ // remove the prefix and add a new, more appropriate one.
+ path.remove(0, 12);
+ return true;
+ }
+ else
+ {
+ QLOG_ERROR() << "Update path not within .app: " << path;
+ return false;
+ }
+}
+
+void DownloadUpdateTask::fileDownloadFinished()
+{
+ emitSucceeded();
+}
+
+void DownloadUpdateTask::fileDownloadFailed()
+{
+ // TODO: Give more info about the failure.
+ QLOG_ERROR() << "Failed to download update files.";
+ emitFailed(tr("Failed to download update files."));
+}
+
+void DownloadUpdateTask::fileDownloadProgressChanged(qint64 current, qint64 total)
+{
+ setProgress((int)(((float)current / (float)total) * 100));
+}
+
+QString DownloadUpdateTask::updateFilesDir()
+{
+ return m_updateFilesDir.path();
+}
diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h
new file mode 100644
index 00000000..518bc235
--- /dev/null
+++ b/logic/updater/DownloadUpdateTask.h
@@ -0,0 +1,217 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "logic/tasks/Task.h"
+#include "logic/net/NetJob.h"
+
+/*!
+ * The DownloadUpdateTask is a task that takes a given version ID and repository URL,
+ * downloads that version's files from the repository, and prepares to install them.
+ */
+class DownloadUpdateTask : public Task
+{
+ Q_OBJECT
+
+public:
+ explicit DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent=0);
+
+ /*!
+ * Gets the directory that contains the update files.
+ */
+ QString updateFilesDir();
+
+public:
+
+ // TODO: We should probably put these data structures into a separate header...
+
+ /*!
+ * Struct that describes an entry in a VersionFileEntry's `Sources` list.
+ */
+ struct FileSource
+ {
+ FileSource(QString type, QString url, QString compression="")
+ {
+ this->type = type;
+ this->url = url;
+ this->compressionType = compression;
+ }
+
+ QString type;
+ QString url;
+ QString compressionType;
+ };
+ typedef QList<FileSource> FileSourceList;
+
+ /*!
+ * Structure that describes an entry in a GoUpdate version's `Files` list.
+ */
+ struct VersionFileEntry
+ {
+ QString path;
+ int mode;
+ FileSourceList sources;
+ QString md5;
+ };
+ typedef QList<VersionFileEntry> VersionFileList;
+
+ /*!
+ * Structure that describes an operation to perform when installing updates.
+ */
+ struct UpdateOperation
+ {
+ static UpdateOperation CopyOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_COPY, fsource, fdest, fmode}; }
+ static UpdateOperation MoveOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_MOVE, fsource, fdest, fmode}; }
+ static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 0644}; }
+ static UpdateOperation ChmodOp(QString file, int fmode) { return UpdateOperation{OP_CHMOD, file, "", fmode}; }
+
+ //! Specifies the type of operation that this is.
+ enum Type
+ {
+ OP_COPY,
+ OP_DELETE,
+ OP_MOVE,
+ OP_CHMOD,
+ } type;
+
+ //! The file to operate on. If this is a DELETE or CHMOD operation, this is the file that will be modified.
+ QString file;
+
+ //! The destination file. If this is a DELETE or CHMOD operation, this field will be ignored.
+ QString dest;
+
+ //! The mode to change the source file to. Ignored if this isn't a CHMOD operation.
+ int mode;
+
+ // Yeah yeah, polymorphism blah blah inheritance, blah blah object oriented. I'm lazy, OK?
+ };
+ typedef QList<UpdateOperation> UpdateOperationList;
+
+protected:
+ friend class DownloadUpdateTaskTest;
+
+
+ /*!
+ * Used for arguments to parseVersionInfo and friends to specify which version info file to parse.
+ */
+ enum VersionInfoFileEnum { NEW_VERSION, CURRENT_VERSION };
+
+
+ //! Entry point for tasks.
+ virtual void executeTask();
+
+ /*!
+ * Attempts to find the version ID and repository URL for the current version.
+ * The function will look up the repository URL in the UpdateChecker's channel list.
+ * If the repository URL can't be found, this function will return false.
+ */
+ virtual void findCurrentVersionInfo();
+
+ /*!
+ * This runs after we've tried loading the channel list.
+ * If the channel list doesn't need to be loaded, this will be called immediately.
+ * If the channel list does need to be loaded, this will be called when it's done.
+ */
+ void processChannels();
+
+ /*!
+ * Downloads the version info files from the repository.
+ * The files for both the current build, and the build that we're updating to need to be downloaded.
+ * If the current version's info file can't be found, MultiMC will not delete files that
+ * were removed between versions. It will still replace files that have changed, however.
+ * Note that although the repository URL for the current version is not given to the update task,
+ * the task will attempt to look it up in the UpdateChecker's channel list.
+ * If an error occurs here, the function will call emitFailed and return false.
+ */
+ virtual void loadVersionInfo();
+
+ /*!
+ * This function is called when version information is finished downloading.
+ * This handles parsing the JSON downloaded by the version info network job and then calls processFileLists.
+ * Note that this function will sometimes be called even if the version info download emits failed. If
+ * we couldn't download the current version's info file, we can still update. This will be called even if the
+ * current version's info file fails to download, as long as the new version's info file succeeded.
+ */
+ virtual void parseDownloadedVersionInfo();
+
+ /*!
+ * Loads the file list from the given version info JSON object into the given list.
+ */
+ virtual bool parseVersionInfo(const QByteArray &data, VersionFileList* list, QString *error);
+
+ /*!
+ * Takes a list of file entries for the current version's files and the new version's files
+ * and populates the downloadList and operationList with information about how to download and install the update.
+ */
+ virtual bool processFileLists(NetJob *job, const VersionFileList &currentVersion, const VersionFileList &newVersion, UpdateOperationList &ops);
+
+ /*!
+ * Calls \see processFileLists to populate the \see m_operationList and a NetJob, and then executes
+ * the NetJob to fetch all needed files
+ */
+ virtual void processFileLists();
+
+ /*!
+ * Takes the operations list and writes an install script for the updater to the update files directory.
+ */
+ virtual bool writeInstallScript(UpdateOperationList& opsList, QString scriptFile);
+
+ UpdateOperationList m_operationList;
+
+ VersionFileList m_nVersionFileList;
+ VersionFileList m_cVersionFileList;
+
+ //! Network job for downloading version info files.
+ NetJobPtr m_vinfoNetJob;
+
+ //! Network job for downloading update files.
+ NetJobPtr m_filesNetJob;
+
+ // Version ID and repo URL for the new version.
+ int m_nVersionId;
+ QString m_nRepoUrl;
+
+ // Version ID and repo URL for the currently installed version.
+ int m_cVersionId;
+ QString m_cRepoUrl;
+
+ /*!
+ * Temporary directory to store update files in.
+ * This will be set to not auto delete. Task will fail if this fails to be created.
+ */
+ QTemporaryDir m_updateFilesDir;
+
+ /*!
+ * Filters paths
+ * This fixes destination paths for OSX.
+ * The updater runs in MultiMC.app/Contents/MacOs by default
+ * The destination paths are such as this: MultiMC.app/blah/blah
+ *
+ * Therefore we chop off the 'MultiMC.app' prefix
+ *
+ * Returns false if the path couldn't be fixed (is invalid)
+ */
+ static bool fixPathForOSX(QString &path);
+
+protected slots:
+ void vinfoDownloadFinished();
+ void vinfoDownloadFailed();
+
+ void fileDownloadFinished();
+ void fileDownloadFailed();
+ void fileDownloadProgressChanged(qint64 current, qint64 total);
+};
+
diff --git a/logic/updater/NotificationChecker.cpp b/logic/updater/NotificationChecker.cpp
new file mode 100644
index 00000000..191e90a3
--- /dev/null
+++ b/logic/updater/NotificationChecker.cpp
@@ -0,0 +1,121 @@
+#include "NotificationChecker.h"
+
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+
+#include "MultiMC.h"
+#include "MultiMCVersion.h"
+#include "logic/net/CacheDownload.h"
+
+NotificationChecker::NotificationChecker(QObject *parent)
+ : QObject(parent), m_notificationsUrl(QUrl(NOTIFICATION_URL))
+{
+ // this will call checkForNotifications once the event loop is running
+ QMetaObject::invokeMethod(this, "checkForNotifications", Qt::QueuedConnection);
+}
+
+QUrl NotificationChecker::notificationsUrl() const
+{
+ return m_notificationsUrl;
+}
+void NotificationChecker::setNotificationsUrl(const QUrl &notificationsUrl)
+{
+ m_notificationsUrl = notificationsUrl;
+}
+
+QList<NotificationChecker::NotificationEntry> NotificationChecker::notificationEntries() const
+{
+ return m_entries;
+}
+
+void NotificationChecker::checkForNotifications()
+{
+ if (!m_notificationsUrl.isValid())
+ {
+ QLOG_ERROR() << "Failed to check for notifications. No notifications URL set."
+ << "If you'd like to use MultiMC's notification system, please pass the "
+ "URL to CMake at compile time.";
+ return;
+ }
+ if (m_checkJob)
+ {
+ return;
+ }
+ m_checkJob.reset(new NetJob("Checking for notifications"));
+ auto entry = MMC->metacache()->resolveEntry("root", "notifications.json");
+ entry->stale = true;
+ m_checkJob->addNetAction(m_download = CacheDownload::make(m_notificationsUrl, entry));
+ connect(m_download.get(), &CacheDownload::succeeded, this,
+ &NotificationChecker::downloadSucceeded);
+ m_checkJob->start();
+}
+
+void NotificationChecker::downloadSucceeded(int)
+{
+ m_entries.clear();
+
+ QFile file(m_download->getTargetFilepath());
+ if (file.open(QFile::ReadOnly))
+ {
+ QJsonArray root = QJsonDocument::fromJson(file.readAll()).array();
+ for (auto it = root.begin(); it != root.end(); ++it)
+ {
+ QJsonObject obj = (*it).toObject();
+ NotificationEntry entry;
+ entry.id = obj.value("id").toDouble();
+ entry.message = obj.value("message").toString();
+ entry.channel = obj.value("channel").toString();
+ entry.platform = obj.value("platform").toString();
+ entry.from = obj.value("from").toString();
+ entry.to = obj.value("to").toString();
+ const QString type = obj.value("type").toString("critical");
+ if (type == "critical")
+ {
+ entry.type = NotificationEntry::Critical;
+ }
+ else if (type == "warning")
+ {
+ entry.type = NotificationEntry::Warning;
+ }
+ else if (type == "information")
+ {
+ entry.type = NotificationEntry::Information;
+ }
+ m_entries.append(entry);
+ }
+ }
+
+ m_checkJob.reset();
+
+ emit notificationCheckFinished();
+}
+
+bool NotificationChecker::NotificationEntry::applies() const
+{
+ MultiMCVersion version = MMC->version();
+ bool channelApplies = channel.isEmpty() || channel == version.channel;
+ bool platformApplies = platform.isEmpty() || platform == version.platform;
+ bool fromApplies =
+ from.isEmpty() || from == FULL_VERSION_STR || !versionLessThan(FULL_VERSION_STR, from);
+ bool toApplies =
+ to.isEmpty() || to == FULL_VERSION_STR || !versionLessThan(to, FULL_VERSION_STR);
+ return channelApplies && platformApplies && fromApplies && toApplies;
+}
+
+bool NotificationChecker::NotificationEntry::versionLessThan(const QString &v1,
+ const QString &v2)
+{
+ QStringList l1 = v1.split('.');
+ QStringList l2 = v2.split('.');
+ while (!l1.isEmpty() && !l2.isEmpty())
+ {
+ int one = l1.isEmpty() ? 0 : l1.takeFirst().toInt();
+ int two = l2.isEmpty() ? 0 : l2.takeFirst().toInt();
+ if (one != two)
+ {
+ return one < two;
+ }
+ }
+ return false;
+}
diff --git a/logic/updater/NotificationChecker.h b/logic/updater/NotificationChecker.h
new file mode 100644
index 00000000..915ee54d
--- /dev/null
+++ b/logic/updater/NotificationChecker.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <QObject>
+
+#include "logic/net/NetJob.h"
+#include "logic/net/CacheDownload.h"
+
+class NotificationChecker : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit NotificationChecker(QObject *parent = 0);
+
+ QUrl notificationsUrl() const;
+ void setNotificationsUrl(const QUrl &notificationsUrl);
+
+ struct NotificationEntry
+ {
+ int id;
+ QString message;
+ enum
+ {
+ Critical,
+ Warning,
+ Information
+ } type;
+ QString channel;
+ QString platform;
+ QString from;
+ QString to;
+ bool applies() const;
+ static bool versionLessThan(const QString &v1, const QString &v2);
+ };
+
+ QList<NotificationEntry> notificationEntries() const;
+
+public
+slots:
+ void checkForNotifications();
+
+private
+slots:
+ void downloadSucceeded(int);
+
+signals:
+ void notificationCheckFinished();
+
+private:
+ QList<NotificationEntry> m_entries;
+ QUrl m_notificationsUrl;
+ NetJobPtr m_checkJob;
+ CacheDownloadPtr m_download;
+};
diff --git a/logic/updater/UpdateChecker.cpp b/logic/updater/UpdateChecker.cpp
new file mode 100644
index 00000000..8e2aa8b3
--- /dev/null
+++ b/logic/updater/UpdateChecker.cpp
@@ -0,0 +1,263 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "UpdateChecker.h"
+
+#include "MultiMC.h"
+
+#include "logger/QsLog.h"
+
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonValue>
+
+#include <settingsobject.h>
+
+#define API_VERSION 0
+#define CHANLIST_FORMAT 0
+
+UpdateChecker::UpdateChecker()
+{
+ m_channelListUrl = CHANLIST_URL;
+ m_updateChecking = false;
+ m_chanListLoading = false;
+ m_checkUpdateWaiting = false;
+ m_chanListLoaded = false;
+}
+
+QList<UpdateChecker::ChannelListEntry> UpdateChecker::getChannelList() const
+{
+ return m_channels;
+}
+
+bool UpdateChecker::hasChannels() const
+{
+ return !m_channels.isEmpty();
+}
+
+void UpdateChecker::checkForUpdate(bool notifyNoUpdate)
+{
+ QLOG_DEBUG() << "Checking for updates.";
+
+ // If the channel list hasn't loaded yet, load it and defer checking for updates until
+ // later.
+ if (!m_chanListLoaded)
+ {
+ QLOG_DEBUG() << "Channel list isn't loaded yet. Loading channel list and deferring "
+ "update check.";
+ m_checkUpdateWaiting = true;
+ updateChanList();
+ return;
+ }
+
+ if (m_updateChecking)
+ {
+ QLOG_DEBUG() << "Ignoring update check request. Already checking for updates.";
+ return;
+ }
+
+ m_updateChecking = true;
+
+ // Get the channel we're checking.
+ QString updateChannel = MMC->settings()->get("UpdateChannel").toString();
+
+ // Find the desired channel within the channel list and get its repo URL. If if cannot be
+ // found, error.
+ m_repoUrl = "";
+ for (ChannelListEntry entry : m_channels)
+ {
+ if (entry.id == updateChannel)
+ m_repoUrl = entry.url;
+ }
+
+ // If we didn't find our channel, error.
+ if (m_repoUrl.isEmpty())
+ {
+ emit updateCheckFailed();
+ return;
+ }
+
+ QUrl indexUrl = QUrl(m_repoUrl).resolved(QUrl("index.json"));
+
+ auto job = new NetJob("GoUpdate Repository Index");
+ job->addNetAction(ByteArrayDownload::make(indexUrl));
+ connect(job, &NetJob::succeeded, [this, notifyNoUpdate]()
+ { updateCheckFinished(notifyNoUpdate); });
+ connect(job, SIGNAL(failed()), SLOT(updateCheckFailed()));
+ indexJob.reset(job);
+ job->start();
+}
+
+void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
+{
+ QLOG_DEBUG() << "Finished downloading repo index. Checking for new versions.";
+
+ QJsonParseError jsonError;
+ QByteArray data;
+ {
+ ByteArrayDownloadPtr dl =
+ std::dynamic_pointer_cast<ByteArrayDownload>(indexJob->first());
+ data = dl->m_data;
+ indexJob.reset();
+ }
+
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+ if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject())
+ {
+ QLOG_ERROR() << "Failed to parse GoUpdate repository index. JSON error"
+ << jsonError.errorString() << "at offset" << jsonError.offset;
+ return;
+ }
+
+ QJsonObject object = jsonDoc.object();
+
+ bool success = false;
+ int apiVersion = object.value("ApiVersion").toVariant().toInt(&success);
+ if (apiVersion != API_VERSION || !success)
+ {
+ QLOG_ERROR() << "Failed to check for updates. API version mismatch. We're using"
+ << API_VERSION << "server has" << apiVersion;
+ return;
+ }
+
+ QLOG_DEBUG() << "Processing repository version list.";
+ QJsonObject newestVersion;
+ QJsonArray versions = object.value("Versions").toArray();
+ for (QJsonValue versionVal : versions)
+ {
+ QJsonObject version = versionVal.toObject();
+ if (newestVersion.value("Id").toVariant().toInt() <
+ version.value("Id").toVariant().toInt())
+ {
+ newestVersion = version;
+ }
+ }
+
+ // We've got the version with the greatest ID number. Now compare it to our current build
+ // number and update if they're different.
+ int newBuildNumber = newestVersion.value("Id").toVariant().toInt();
+ if (newBuildNumber != MMC->version().build)
+ {
+ QLOG_DEBUG() << "Found newer version with ID" << newBuildNumber;
+ // Update!
+ emit updateAvailable(m_repoUrl, newestVersion.value("Name").toVariant().toString(),
+ newBuildNumber);
+ }
+ else if (notifyNoUpdate)
+ {
+ emit noUpdateFound();
+ }
+
+ m_updateChecking = false;
+}
+
+void UpdateChecker::updateCheckFailed()
+{
+ // TODO: log errors better
+ QLOG_ERROR() << "Update check failed for reasons unknown.";
+}
+
+void UpdateChecker::updateChanList()
+{
+ QLOG_DEBUG() << "Loading the channel list.";
+
+ if (m_channelListUrl.isEmpty())
+ {
+ QLOG_ERROR() << "Failed to update channel list. No channel list URL set."
+ << "If you'd like to use MultiMC's update system, please pass the channel "
+ "list URL to CMake at compile time.";
+ return;
+ }
+
+ m_chanListLoading = true;
+ NetJob *job = new NetJob("Update System Channel List");
+ job->addNetAction(ByteArrayDownload::make(QUrl(m_channelListUrl)));
+ QObject::connect(job, &NetJob::succeeded, this, &UpdateChecker::chanListDownloadFinished);
+ QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed);
+ chanListJob.reset(job);
+ job->start();
+}
+
+void UpdateChecker::chanListDownloadFinished()
+{
+ QByteArray data;
+ {
+ ByteArrayDownloadPtr dl =
+ std::dynamic_pointer_cast<ByteArrayDownload>(chanListJob->first());
+ data = dl->m_data;
+ chanListJob.reset();
+ }
+
+ QJsonParseError jsonError;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
+ if (jsonError.error != QJsonParseError::NoError)
+ {
+ // TODO: Report errors to the user.
+ QLOG_ERROR() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at"
+ << jsonError.offset;
+ return;
+ }
+
+ QJsonObject object = jsonDoc.object();
+
+ bool success = false;
+ int formatVersion = object.value("format_version").toVariant().toInt(&success);
+ if (formatVersion != CHANLIST_FORMAT || !success)
+ {
+ QLOG_ERROR()
+ << "Failed to check for updates. Channel list format version mismatch. We're using"
+ << CHANLIST_FORMAT << "server has" << formatVersion;
+ return;
+ }
+
+ // Load channels into a temporary array.
+ QList<ChannelListEntry> loadedChannels;
+ QJsonArray channelArray = object.value("channels").toArray();
+ for (QJsonValue chanVal : channelArray)
+ {
+ QJsonObject channelObj = chanVal.toObject();
+ ChannelListEntry entry{channelObj.value("id").toVariant().toString(),
+ channelObj.value("name").toVariant().toString(),
+ channelObj.value("description").toVariant().toString(),
+ channelObj.value("url").toVariant().toString()};
+ if (entry.id.isEmpty() || entry.name.isEmpty() || entry.url.isEmpty())
+ {
+ QLOG_ERROR() << "Channel list entry with empty ID, name, or URL. Skipping.";
+ continue;
+ }
+ loadedChannels.append(entry);
+ }
+
+ // Swap the channel list we just loaded into the object's channel list.
+ m_channels.swap(loadedChannels);
+
+ m_chanListLoading = false;
+ m_chanListLoaded = true;
+ QLOG_INFO() << "Successfully loaded UpdateChecker channel list.";
+
+ // If we're waiting to check for updates, do that now.
+ if (m_checkUpdateWaiting)
+ checkForUpdate(false);
+
+ emit channelListLoaded();
+}
+
+void UpdateChecker::chanListDownloadFailed()
+{
+ m_chanListLoading = false;
+ QLOG_ERROR() << "Failed to download channel list.";
+ emit channelListLoaded();
+}
+
diff --git a/logic/updater/UpdateChecker.h b/logic/updater/UpdateChecker.h
new file mode 100644
index 00000000..3b0ee28d
--- /dev/null
+++ b/logic/updater/UpdateChecker.h
@@ -0,0 +1,111 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "logic/net/NetJob.h"
+
+#include <QUrl>
+
+class UpdateChecker : public QObject
+{
+ Q_OBJECT
+
+public:
+ UpdateChecker();
+ void checkForUpdate(bool notifyNoUpdate);
+
+ void setChannelListUrl(const QString &url) { m_channelListUrl = url; }
+
+ /*!
+ * Causes the update checker to download the channel list from the URL specified in config.h (generated by CMake).
+ * If this isn't called before checkForUpdate(), it will automatically be called.
+ */
+ void updateChanList();
+
+ /*!
+ * An entry in the channel list.
+ */
+ struct ChannelListEntry
+ {
+ QString id;
+ QString name;
+ QString description;
+ QString url;
+ };
+
+ /*!
+ * Returns a the current channel list.
+ * If the channel list hasn't been loaded, this list will be empty.
+ */
+ QList<ChannelListEntry> getChannelList() const;
+
+ /*!
+ * Returns false if the channel list is empty.
+ */
+ bool hasChannels() const;
+
+signals:
+ //! Signal emitted when an update is available. Passes the URL for the repo and the ID and name for the version.
+ void updateAvailable(QString repoUrl, QString versionName, int versionId);
+
+ //! Signal emitted when the channel list finishes loading or fails to load.
+ void channelListLoaded();
+
+ void noUpdateFound();
+
+private slots:
+ void updateCheckFinished(bool notifyNoUpdate);
+ void updateCheckFailed();
+
+ void chanListDownloadFinished();
+ void chanListDownloadFailed();
+
+private:
+ friend class UpdateCheckerTest;
+
+ NetJobPtr indexJob;
+ NetJobPtr chanListJob;
+
+ QString m_repoUrl;
+
+ QString m_channelListUrl;
+
+ QList<ChannelListEntry> m_channels;
+
+ /*!
+ * True while the system is checking for updates.
+ * If checkForUpdate is called while this is true, it will be ignored.
+ */
+ bool m_updateChecking;
+
+ /*!
+ * True if the channel list has loaded.
+ * If this is false, trying to check for updates will call updateChanList first.
+ */
+ bool m_chanListLoaded;
+
+ /*!
+ * Set to true while the channel list is currently loading.
+ */
+ bool m_chanListLoading;
+
+ /*!
+ * Set to true when checkForUpdate is called while the channel list isn't loaded.
+ * When the channel list finishes loading, if this is true, the update checker will check for updates.
+ */
+ bool m_checkUpdateWaiting;
+};
+