diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/logic/BaseInstance.cpp | 16 | ||||
-rw-r--r-- | api/logic/BaseInstance.h | 5 | ||||
-rw-r--r-- | api/logic/BaseInstanceProvider.h | 58 | ||||
-rw-r--r-- | api/logic/CMakeLists.txt | 1 | ||||
-rw-r--r-- | api/logic/ExponentialSeries.h | 43 | ||||
-rw-r--r-- | api/logic/FolderInstanceProvider.cpp | 81 | ||||
-rw-r--r-- | api/logic/FolderInstanceProvider.h | 45 | ||||
-rw-r--r-- | api/logic/InstanceCopyTask.cpp | 1 | ||||
-rw-r--r-- | api/logic/InstanceCopyTask.h | 2 | ||||
-rw-r--r-- | api/logic/InstanceCreationTask.cpp | 1 | ||||
-rw-r--r-- | api/logic/InstanceImportTask.cpp | 1 | ||||
-rw-r--r-- | api/logic/InstanceImportTask.h | 1 | ||||
-rw-r--r-- | api/logic/InstanceList.cpp | 65 | ||||
-rw-r--r-- | api/logic/InstanceList.h | 12 | ||||
-rw-r--r-- | api/logic/InstanceTask.h | 2 | ||||
-rw-r--r-- | api/logic/WatchLock.h | 20 | ||||
-rw-r--r-- | api/logic/minecraft/legacy/LegacyUpgradeTask.cpp | 1 | ||||
-rw-r--r-- | api/logic/minecraft/legacy/LegacyUpgradeTask.h | 2 | ||||
-rw-r--r-- | api/logic/modplatform/ftb/FtbPackInstallTask.h | 1 |
19 files changed, 137 insertions, 221 deletions
diff --git a/api/logic/BaseInstance.cpp b/api/logic/BaseInstance.cpp index f07b2bd0..3a4d84be 100644 --- a/api/logic/BaseInstance.cpp +++ b/api/logic/BaseInstance.cpp @@ -284,19 +284,3 @@ std::shared_ptr<LaunchTask> BaseInstance::getLaunchTask() { return m_launchProcess; } - -void BaseInstance::setProvider(BaseInstanceProvider* provider) -{ - // only once. - assert(!m_provider); - if(m_provider) - { - qWarning() << "Provider set more than once for instance" << id(); - } - m_provider = provider; -} - -BaseInstanceProvider* BaseInstance::provider() const -{ - return m_provider; -} diff --git a/api/logic/BaseInstance.h b/api/logic/BaseInstance.h index d9713a58..09d21b2e 100644 --- a/api/logic/BaseInstance.h +++ b/api/logic/BaseInstance.h @@ -38,7 +38,6 @@ class QDir; class Task; class LaunchTask; class BaseInstance; -class BaseInstanceProvider; // pointer for lazy people typedef std::shared_ptr<BaseInstance> InstancePtr; @@ -89,9 +88,6 @@ public: int64_t totalTimePlayed() const; void resetTimePlayed(); - void setProvider(BaseInstanceProvider * provider); - BaseInstanceProvider * provider() const; - /// get the type of this instance QString instanceType() const; @@ -271,7 +267,6 @@ protected: /* data */ bool m_isRunning = false; std::shared_ptr<LaunchTask> m_launchProcess; QDateTime m_timeStarted; - BaseInstanceProvider * m_provider = nullptr; private: /* data */ Status m_status = Status::Present; diff --git a/api/logic/BaseInstanceProvider.h b/api/logic/BaseInstanceProvider.h deleted file mode 100644 index 095d4dab..00000000 --- a/api/logic/BaseInstanceProvider.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include <QObject> -#include <QString> -#include "BaseInstance.h" -#include "settings/SettingsObject.h" - -#include "multimc_logic_export.h" - -using InstanceId = QString; -using GroupId = QString; -using InstanceLocator = std::pair<InstancePtr, int>; - -enum class InstCreateError -{ - NoCreateError = 0, - NoSuchVersion, - UnknownCreateError, - InstExists, - CantCreateDir -}; - -class MULTIMC_LOGIC_EXPORT BaseInstanceProvider : public QObject -{ - Q_OBJECT -public: - BaseInstanceProvider(SettingsObjectPtr settings) : m_globalSettings(settings) - { - // nil - } -public: - virtual QList<InstanceId> discoverInstances() = 0; - virtual InstancePtr loadInstance(const InstanceId &id) = 0; - virtual void loadGroupList() = 0; - virtual void saveGroupList() = 0; - - virtual QString getStagedInstancePath() - { - return QString(); - } - virtual bool commitStagedInstance(const QString & path, const QString& instanceName, const QString & groupName) - { - return false; - } - virtual bool destroyStagingPath(const QString & path) - { - return true; - } - -signals: - // Emit this when the list of provided instances changed - void instancesChanged(); - // Emit when the set of groups your provider supplies changes. - void groupsChanged(QSet<QString> groups); - -protected: - SettingsObjectPtr m_globalSettings; -}; diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index 4c2445f0..387b3428 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -16,7 +16,6 @@ set(CORE_SOURCES LoggedProcess.cpp MessageLevel.cpp MessageLevel.h - BaseInstanceProvider.h FolderInstanceProvider.h FolderInstanceProvider.cpp BaseVersion.h diff --git a/api/logic/ExponentialSeries.h b/api/logic/ExponentialSeries.h new file mode 100644 index 00000000..a9487f0a --- /dev/null +++ b/api/logic/ExponentialSeries.h @@ -0,0 +1,43 @@ + +#pragma once + +template <typename T> +inline void clamp(T& current, T min, T max) +{ + if (current < min) + { + current = min; + } + else if(current > max) + { + current = max; + } +} + +// List of numbers from min to max. Next is exponent times bigger than previous. + +class ExponentialSeries +{ +public: + ExponentialSeries(unsigned min, unsigned max, unsigned exponent = 2) + { + m_current = m_min = min; + m_max = max; + m_exponent = exponent; + } + void reset() + { + m_current = m_min; + } + unsigned operator()() + { + unsigned retval = m_current; + m_current *= m_exponent; + clamp(m_current, m_min, m_max); + return retval; + } + unsigned m_current; + unsigned m_min; + unsigned m_max; + unsigned m_exponent; +}; diff --git a/api/logic/FolderInstanceProvider.cpp b/api/logic/FolderInstanceProvider.cpp index 4a20127a..4c44836d 100644 --- a/api/logic/FolderInstanceProvider.cpp +++ b/api/logic/FolderInstanceProvider.cpp @@ -4,6 +4,9 @@ #include "minecraft/MinecraftInstance.h" #include "minecraft/legacy/LegacyInstance.h" #include "NullInstance.h" +#include "ExponentialSeries.h" +#include "WatchLock.h" + #include <QDir> #include <QDirIterator> @@ -16,23 +19,8 @@ const static int GROUP_FILE_FORMAT_VERSION = 1; -struct WatchLock -{ - WatchLock(QFileSystemWatcher * watcher, const QString& instDir) - : m_watcher(watcher), m_instDir(instDir) - { - m_watcher->removePath(m_instDir); - } - ~WatchLock() - { - m_watcher->addPath(m_instDir); - } - QFileSystemWatcher * m_watcher; - QString m_instDir; -}; - FolderInstanceProvider::FolderInstanceProvider(SettingsObjectPtr settings, const QString& instDir) - : BaseInstanceProvider(settings) + : m_globalSettings(settings) { // Create aand normalize path if (!QDir::current().exists(instDir)) @@ -105,7 +93,6 @@ InstancePtr FolderInstanceProvider::loadInstance(const InstanceId& id) inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot)); } inst->init(); - inst->setProvider(this); auto iter = groupMap.find(id); if (iter != groupMap.end()) { @@ -313,55 +300,13 @@ void FolderInstanceProvider::on_InstFolderChanged(const Setting &setting, QVaria } } -template <typename T> -static void clamp(T& current, T min, T max) -{ - if (current < min) - { - current = min; - } - else if(current > max) - { - current = max; - } -} - -namespace { -// List of numbers from min to max. Next is exponent times bigger than previous. -class ExponentialSeries -{ -public: - ExponentialSeries(unsigned min, unsigned max, unsigned exponent = 2) - { - m_current = m_min = min; - m_max = max; - m_exponent = exponent; - } - void reset() - { - m_current = m_min; - } - unsigned operator()() - { - unsigned retval = m_current; - m_current *= m_exponent; - clamp(m_current, m_min, m_max); - return retval; - } - unsigned m_current; - unsigned m_min; - unsigned m_max; - unsigned m_exponent; -}; -} - -class FolderInstanceStaging : public Task +class InstanceStaging : public Task { Q_OBJECT const unsigned minBackoff = 1; const unsigned maxBackoff = 16; public: - FolderInstanceStaging ( + InstanceStaging ( FolderInstanceProvider * parent, Task * child, const QString & stagingPath, @@ -371,18 +316,18 @@ public: { m_parent = parent; m_child.reset(child); - connect(child, &Task::succeeded, this, &FolderInstanceStaging::childSucceded); - connect(child, &Task::failed, this, &FolderInstanceStaging::childFailed); - connect(child, &Task::status, this, &FolderInstanceStaging::setStatus); - connect(child, &Task::progress, this, &FolderInstanceStaging::setProgress); + connect(child, &Task::succeeded, this, &InstanceStaging::childSucceded); + connect(child, &Task::failed, this, &InstanceStaging::childFailed); + connect(child, &Task::status, this, &InstanceStaging::setStatus); + connect(child, &Task::progress, this, &InstanceStaging::setProgress); m_instanceName = instanceName; m_groupName = groupName; m_stagingPath = stagingPath; m_backoffTimer.setSingleShot(true); - connect(&m_backoffTimer, &QTimer::timeout, this, &FolderInstanceStaging::childSucceded); + connect(&m_backoffTimer, &QTimer::timeout, this, &InstanceStaging::childSucceded); } - virtual ~FolderInstanceStaging() {}; + virtual ~InstanceStaging() {}; protected: virtual void executeTask() override @@ -439,7 +384,7 @@ Task * FolderInstanceProvider::wrapInstanceTask(InstanceTask * task) auto stagingPath = getStagedInstancePath(); task->setStagingPath(stagingPath); task->setParentSettings(m_globalSettings); - return new FolderInstanceStaging(this, task, stagingPath, task->name(), task->group()); + return new InstanceStaging(this, task, stagingPath, task->name(), task->group()); } QString FolderInstanceProvider::getStagedInstancePath() diff --git a/api/logic/FolderInstanceProvider.h b/api/logic/FolderInstanceProvider.h index e1f2ad64..ce5337c1 100644 --- a/api/logic/FolderInstanceProvider.h +++ b/api/logic/FolderInstanceProvider.h @@ -1,23 +1,42 @@ #pragma once -#include "BaseInstanceProvider.h" + +#include <QObject> +#include <QString> #include <QMap> +#include "BaseInstance.h" +#include "settings/SettingsObject.h" + +#include "multimc_logic_export.h" class QFileSystemWatcher; class InstanceTask; +using InstanceId = QString; +using GroupId = QString; +using InstanceLocator = std::pair<InstancePtr, int>; -class MULTIMC_LOGIC_EXPORT FolderInstanceProvider : public BaseInstanceProvider +enum class InstCreateError +{ + NoCreateError = 0, + NoSuchVersion, + UnknownCreateError, + InstExists, + CantCreateDir +}; + +class MULTIMC_LOGIC_EXPORT FolderInstanceProvider : public QObject { Q_OBJECT public: FolderInstanceProvider(SettingsObjectPtr settings, const QString & instDir); + virtual ~FolderInstanceProvider() = default; public: /// used by InstanceList to @return a list of plausible IDs to probe for - QList<InstanceId> discoverInstances() override; + QList<InstanceId> discoverInstances(); /// used by InstanceList to (re)load an instance with the given @id. - InstancePtr loadInstance(const InstanceId& id) override; + InstancePtr loadInstance(const InstanceId& id); // Wrap an instance creation task in some more task machinery and make it ready to be used Task * wrapInstanceTask(InstanceTask * task); @@ -26,17 +45,24 @@ public: * Create a new empty staging area for instance creation and @return a path/key top commit it later. * Used by instance manipulation tasks. */ - QString getStagedInstancePath() override; + QString getStagedInstancePath(); /** * Commit the staging area given by @keyPath to the provider - used when creation succeeds. * Used by instance manipulation tasks. */ - bool commitStagedInstance(const QString & keyPath, const QString& instanceName, const QString & groupName) override; + bool commitStagedInstance(const QString & keyPath, const QString& instanceName, const QString & groupName); /** * Destroy a previously created staging area given by @keyPath - used when creation fails. * Used by instance manipulation tasks. */ - bool destroyStagingPath(const QString & keyPath) override; + bool destroyStagingPath(const QString & keyPath); + +signals: + // Emit this when the list of provided instances changed + void instancesChanged(); + // Emit when the set of groups your provider supplies changes. + void groupsChanged(QSet<QString> groups); + public slots: void on_InstFolderChanged(const Setting &setting, QVariant value); @@ -46,10 +72,11 @@ private slots: void groupChanged(); private: /* methods */ - void loadGroupList() override; - void saveGroupList() override; + void loadGroupList(); + void saveGroupList(); private: /* data */ + SettingsObjectPtr m_globalSettings; QString m_instDir; QFileSystemWatcher * m_watcher; QMap<InstanceId, GroupId> groupMap; diff --git a/api/logic/InstanceCopyTask.cpp b/api/logic/InstanceCopyTask.cpp index cb2deb29..a576a0fd 100644 --- a/api/logic/InstanceCopyTask.cpp +++ b/api/logic/InstanceCopyTask.cpp @@ -1,5 +1,4 @@ #include "InstanceCopyTask.h" -#include "BaseInstanceProvider.h" #include "settings/INISettingsObject.h" #include "FileSystem.h" #include "NullInstance.h" diff --git a/api/logic/InstanceCopyTask.h b/api/logic/InstanceCopyTask.h index 0a338f2f..8dd55b40 100644 --- a/api/logic/InstanceCopyTask.h +++ b/api/logic/InstanceCopyTask.h @@ -11,8 +11,6 @@ #include "BaseInstance.h" #include "InstanceTask.h" -class BaseInstanceProvider; - class MULTIMC_LOGIC_EXPORT InstanceCopyTask : public InstanceTask { Q_OBJECT diff --git a/api/logic/InstanceCreationTask.cpp b/api/logic/InstanceCreationTask.cpp index 7ac474ec..15041845 100644 --- a/api/logic/InstanceCreationTask.cpp +++ b/api/logic/InstanceCreationTask.cpp @@ -1,5 +1,4 @@ #include "InstanceCreationTask.h" -#include "BaseInstanceProvider.h" #include "settings/INISettingsObject.h" #include "FileSystem.h" diff --git a/api/logic/InstanceImportTask.cpp b/api/logic/InstanceImportTask.cpp index f3e0858d..0edbc92e 100644 --- a/api/logic/InstanceImportTask.cpp +++ b/api/logic/InstanceImportTask.cpp @@ -1,6 +1,5 @@ #include "InstanceImportTask.h" #include "BaseInstance.h" -#include "BaseInstanceProvider.h" #include "FileSystem.h" #include "Env.h" #include "MMCZip.h" diff --git a/api/logic/InstanceImportTask.h b/api/logic/InstanceImportTask.h index 4156aa48..d326391b 100644 --- a/api/logic/InstanceImportTask.h +++ b/api/logic/InstanceImportTask.h @@ -10,7 +10,6 @@ #include "QObjectPtr.h" class QuaZip; -class BaseInstanceProvider; namespace Flame { class FileResolvingTask; diff --git a/api/logic/InstanceList.cpp b/api/logic/InstanceList.cpp index 9dd08b5b..7e2c03a9 100644 --- a/api/logic/InstanceList.cpp +++ b/api/logic/InstanceList.cpp @@ -155,48 +155,27 @@ static QMap<InstanceId, InstanceLocator> getIdMapping(const QList<InstancePtr> & return out; } -InstanceList::InstListError InstanceList::loadList(bool complete) +InstanceList::InstListError InstanceList::loadList() { auto existingIds = getIdMapping(m_instances); QList<InstancePtr> newList; - auto processIds = [&](BaseInstanceProvider * provider, QList<InstanceId> ids) + for(auto & id: m_provider->discoverInstances()) { - for(auto & id: ids) + if(existingIds.contains(id)) { - if(existingIds.contains(id)) - { - auto instPair = existingIds[id]; - /* - auto & instPtr = instPair.first; - auto & instIdx = instPair.second; - */ - existingIds.remove(id); - qDebug() << "Should keep and soft-reload" << id; - } - else - { - InstancePtr instPtr = provider->loadInstance(id); - if(instPtr) - { - newList.append(instPtr); - } - } - } - }; - if(complete) - { - for(auto & item: m_providers) - { - processIds(item.get(), item->discoverInstances()); + auto instPair = existingIds[id]; + existingIds.remove(id); + qDebug() << "Should keep and soft-reload" << id; } - } - else - { - for (auto & item: m_updatedProviders) + else { - processIds(item, item->discoverInstances()); + InstancePtr instPtr = m_provider->loadInstance(id); + if(instPtr) + { + newList.append(instPtr); + } } } @@ -225,10 +204,6 @@ InstanceList::InstListError InstanceList::loadList(bool complete) for(auto & removedItem: deadList) { auto instPtr = removedItem.first; - if(!complete && !m_updatedProviders.contains(instPtr->provider())) - { - continue; - } instPtr->invalidate(); currentItem = removedItem.second; if(back_bookmark == -1) @@ -256,7 +231,7 @@ InstanceList::InstListError InstanceList::loadList(bool complete) { add(newList); } - m_updatedProviders.clear(); + m_dirty = false; return NoError; } @@ -287,7 +262,7 @@ void InstanceList::resumeWatch() return; } m_watchLevel++; - if(m_watchLevel > 0 && !m_updatedProviders.isEmpty()) + if(m_watchLevel > 0 && m_dirty) { loadList(); } @@ -300,13 +275,13 @@ void InstanceList::suspendWatch() void InstanceList::providerUpdated() { - auto provider = dynamic_cast<BaseInstanceProvider *>(QObject::sender()); + auto provider = dynamic_cast<FolderInstanceProvider *>(QObject::sender()); if(!provider) { qWarning() << "InstanceList::providerUpdated triggered by a non-provider"; return; } - m_updatedProviders.insert(provider); + m_dirty = true; if(m_watchLevel == 1) { loadList(); @@ -318,11 +293,11 @@ void InstanceList::groupsPublished(QSet<QString> newGroups) m_groups.unite(newGroups); } -void InstanceList::addInstanceProvider(BaseInstanceProvider* provider) +void InstanceList::addInstanceProvider(FolderInstanceProvider* provider) { - connect(provider, &BaseInstanceProvider::instancesChanged, this, &InstanceList::providerUpdated); - connect(provider, &BaseInstanceProvider::groupsChanged, this, &InstanceList::groupsPublished); - m_providers.append(provider); + connect(provider, &FolderInstanceProvider::instancesChanged, this, &InstanceList::providerUpdated); + connect(provider, &FolderInstanceProvider::groupsChanged, this, &InstanceList::groupsPublished); + m_provider = provider; } InstancePtr InstanceList::getInstanceById(QString instId) const diff --git a/api/logic/InstanceList.h b/api/logic/InstanceList.h index 14d5280d..14b80a78 100644 --- a/api/logic/InstanceList.h +++ b/api/logic/InstanceList.h @@ -21,14 +21,12 @@ #include <QList> #include "BaseInstance.h" -#include "BaseInstanceProvider.h" +#include "FolderInstanceProvider.h" #include "multimc_logic_export.h" #include "QObjectPtr.h" -class BaseInstance; - class MULTIMC_LOGIC_EXPORT InstanceList : public QAbstractListModel { Q_OBJECT @@ -70,11 +68,11 @@ public: return m_instances.count(); } - InstListError loadList(bool complete = false); + InstListError loadList(); void saveNow(); /// Add an instance provider. Takes ownership of it. Should only be done before the first load. - void addInstanceProvider(BaseInstanceProvider * provider); + void addInstanceProvider(FolderInstanceProvider * provider); InstancePtr getInstanceById(QString id) const; QModelIndex getInstanceIndexById(const QString &id) const; @@ -99,8 +97,8 @@ private: protected: int m_watchLevel = 0; - QSet<BaseInstanceProvider *> m_updatedProviders; + bool m_dirty = false; QList<InstancePtr> m_instances; QSet<QString> m_groups; - QVector<shared_qobject_ptr<BaseInstanceProvider>> m_providers; + FolderInstanceProvider * m_provider; }; diff --git a/api/logic/InstanceTask.h b/api/logic/InstanceTask.h index 5face5fc..c5f6c7fd 100644 --- a/api/logic/InstanceTask.h +++ b/api/logic/InstanceTask.h @@ -4,8 +4,6 @@ #include "multimc_logic_export.h" #include "settings/SettingsObject.h" -class BaseInstanceProvider; - class MULTIMC_LOGIC_EXPORT InstanceTask : public Task { Q_OBJECT diff --git a/api/logic/WatchLock.h b/api/logic/WatchLock.h new file mode 100644 index 00000000..3e08b413 --- /dev/null +++ b/api/logic/WatchLock.h @@ -0,0 +1,20 @@ + +#pragma once + +#include <QString> +#include <QFileSystemWatcher> + +struct WatchLock +{ + WatchLock(QFileSystemWatcher * watcher, const QString& directory) + : m_watcher(watcher), m_directory(directory) + { + m_watcher->removePath(m_directory); + } + ~WatchLock() + { + m_watcher->addPath(m_directory); + } + QFileSystemWatcher * m_watcher; + QString m_directory; +}; diff --git a/api/logic/minecraft/legacy/LegacyUpgradeTask.cpp b/api/logic/minecraft/legacy/LegacyUpgradeTask.cpp index 0b682d2a..3f373a9c 100644 --- a/api/logic/minecraft/legacy/LegacyUpgradeTask.cpp +++ b/api/logic/minecraft/legacy/LegacyUpgradeTask.cpp @@ -1,5 +1,4 @@ #include "LegacyUpgradeTask.h" -#include "BaseInstanceProvider.h" #include "settings/INISettingsObject.h" #include "FileSystem.h" #include "NullInstance.h" diff --git a/api/logic/minecraft/legacy/LegacyUpgradeTask.h b/api/logic/minecraft/legacy/LegacyUpgradeTask.h index 5606eb87..e35e43b7 100644 --- a/api/logic/minecraft/legacy/LegacyUpgradeTask.h +++ b/api/logic/minecraft/legacy/LegacyUpgradeTask.h @@ -11,8 +11,6 @@ #include "BaseInstance.h" -class BaseInstanceProvider; - class MULTIMC_LOGIC_EXPORT LegacyUpgradeTask : public InstanceTask { Q_OBJECT diff --git a/api/logic/modplatform/ftb/FtbPackInstallTask.h b/api/logic/modplatform/ftb/FtbPackInstallTask.h index 8705ef89..64262688 100644 --- a/api/logic/modplatform/ftb/FtbPackInstallTask.h +++ b/api/logic/modplatform/ftb/FtbPackInstallTask.h @@ -1,6 +1,5 @@ #pragma once #include "InstanceTask.h" -#include "BaseInstanceProvider.h" #include "net/NetJob.h" #include "quazip.h" #include "quazipdir.h" |