From 9965decd81b8e8ca98d610f605263ccf4ce541e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Tue, 24 Jul 2018 01:24:06 +0200 Subject: [PATCH] NOISSUE squish. --- api/logic/CMakeLists.txt | 2 - api/logic/FolderInstanceProvider.cpp | 429 ----------------- api/logic/FolderInstanceProvider.h | 86 ---- api/logic/InstanceList.cpp | 444 +++++++++++++++++- api/logic/InstanceList.h | 64 ++- application/MainWindow.cpp | 11 +- application/MultiMC.cpp | 7 +- .../pages/instance/LegacyUpgradePage.cpp | 4 +- application/pages/modplatform/FTBPage.cpp | 1 - application/pages/modplatform/ImportPage.cpp | 3 - application/pages/modplatform/TechnicPage.cpp | 3 - application/pages/modplatform/TwitchPage.cpp | 3 - application/pages/modplatform/VanillaPage.cpp | 3 - 13 files changed, 489 insertions(+), 571 deletions(-) delete mode 100644 api/logic/FolderInstanceProvider.cpp delete mode 100644 api/logic/FolderInstanceProvider.h diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index 387b3428..f0b2d979 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -16,8 +16,6 @@ set(CORE_SOURCES LoggedProcess.cpp MessageLevel.cpp MessageLevel.h - FolderInstanceProvider.h - FolderInstanceProvider.cpp BaseVersion.h BaseInstance.h BaseInstance.cpp diff --git a/api/logic/FolderInstanceProvider.cpp b/api/logic/FolderInstanceProvider.cpp deleted file mode 100644 index 4c44836d..00000000 --- a/api/logic/FolderInstanceProvider.cpp +++ /dev/null @@ -1,429 +0,0 @@ -#include "FolderInstanceProvider.h" -#include "settings/INISettingsObject.h" -#include "FileSystem.h" -#include "minecraft/MinecraftInstance.h" -#include "minecraft/legacy/LegacyInstance.h" -#include "NullInstance.h" -#include "ExponentialSeries.h" -#include "WatchLock.h" - - -#include -#include -#include -#include -#include -#include -#include -#include - -const static int GROUP_FILE_FORMAT_VERSION = 1; - -FolderInstanceProvider::FolderInstanceProvider(SettingsObjectPtr settings, const QString& instDir) - : m_globalSettings(settings) -{ - // Create aand normalize path - if (!QDir::current().exists(instDir)) - { - QDir::current().mkpath(instDir); - } - // NOTE: canonicalPath requires the path to exist. Do not move this above the creation block! - m_instDir = QDir(instDir).canonicalPath(); - m_watcher = new QFileSystemWatcher(this); - connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &FolderInstanceProvider::instanceDirContentsChanged); - m_watcher->addPath(m_instDir); -} - -QList< InstanceId > FolderInstanceProvider::discoverInstances() -{ - qDebug() << "Discovering instances in" << m_instDir; - QList out; - QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable | QDir::Hidden, QDirIterator::FollowSymlinks); - while (iter.hasNext()) - { - QString subDir = iter.next(); - QFileInfo dirInfo(subDir); - if (!QFileInfo(FS::PathCombine(subDir, "instance.cfg")).exists()) - continue; - // if it is a symlink, ignore it if it goes to the instance folder - if(dirInfo.isSymLink()) - { - QFileInfo targetInfo(dirInfo.symLinkTarget()); - QFileInfo instDirInfo(m_instDir); - if(targetInfo.canonicalPath() == instDirInfo.canonicalFilePath()) - { - qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder"; - continue; - } - } - auto id = dirInfo.fileName(); - out.append(id); - qDebug() << "Found instance ID" << id; - } - instanceSet = out.toSet(); - m_instancesProbed = true; - return out; -} - -InstancePtr FolderInstanceProvider::loadInstance(const InstanceId& id) -{ - if(!m_groupsLoaded) - { - loadGroupList(); - } - - auto instanceRoot = FS::PathCombine(m_instDir, id); - auto instanceSettings = std::make_shared(FS::PathCombine(instanceRoot, "instance.cfg")); - InstancePtr inst; - - instanceSettings->registerSetting("InstanceType", "Legacy"); - - QString inst_type = instanceSettings->get("InstanceType").toString(); - - if (inst_type == "OneSix" || inst_type == "Nostalgia") - { - inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot)); - } - else if (inst_type == "Legacy") - { - inst.reset(new LegacyInstance(m_globalSettings, instanceSettings, instanceRoot)); - } - else - { - inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot)); - } - inst->init(); - auto iter = groupMap.find(id); - if (iter != groupMap.end()) - { - inst->setGroupInitial((*iter)); - } - connect(inst.get(), &BaseInstance::groupChanged, this, &FolderInstanceProvider::groupChanged); - qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot(); - return inst; -} - -void FolderInstanceProvider::saveGroupList() -{ - qDebug() << "Will save group list now."; - if(!m_instancesProbed) - { - qDebug() << "Group saving prevented because we don't know the full list of instances yet."; - return; - } - WatchLock foo(m_watcher, m_instDir); - QString groupFileName = m_instDir + "/instgroups.json"; - QMap> reverseGroupMap; - for (auto iter = groupMap.begin(); iter != groupMap.end(); iter++) - { - QString id = iter.key(); - QString group = iter.value(); - if (group.isEmpty()) - continue; - if(!instanceSet.contains(id)) - { - qDebug() << "Skipping saving missing instance" << id << "to groups list."; - continue; - } - - if (!reverseGroupMap.count(group)) - { - QSet set; - set.insert(id); - reverseGroupMap[group] = set; - } - else - { - QSet &set = reverseGroupMap[group]; - set.insert(id); - } - } - QJsonObject toplevel; - toplevel.insert("formatVersion", QJsonValue(QString("1"))); - QJsonObject groupsArr; - for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.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); - try - { - FS::write(groupFileName, doc.toJson()); - qDebug() << "Group list saved."; - } - catch (const FS::FileSystemException &e) - { - qCritical() << "Failed to write instance group file :" << e.cause(); - } -} - -void FolderInstanceProvider::loadGroupList() -{ - qDebug() << "Will load group list now."; - QSet groupSet; - - QString groupFileName = m_instDir + "/instgroups.json"; - - // if there's no group file, fail - if (!QFileInfo(groupFileName).exists()) - return; - - QByteArray jsonData; - try - { - jsonData = FS::read(groupFileName); - } - catch (const FS::FileSystemException &e) - { - qCritical() << "Failed to read instance group file :" << e.cause(); - return; - } - - QJsonParseError error; - QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); - - // if the json was bad, fail - if (error.error != QJsonParseError::NoError) - { - qCritical() << 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()) - { - qWarning() << "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()) - { - qWarning() << "Invalid group list JSON: 'groups' should be an object."; - return; - } - - groupMap.clear(); - - // 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()) - { - qWarning() << 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()) - { - qWarning() << 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 - groupSet.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; - } - } - m_groupsLoaded = true; - emit groupsChanged(groupSet); - qDebug() << "Group list loaded."; -} - -void FolderInstanceProvider::groupChanged() -{ - // save the groups. save all of them. - auto instance = (BaseInstance *) QObject::sender(); - auto id = instance->id(); - groupMap[id] = instance->group(); - emit groupsChanged({instance->group()}); - saveGroupList(); -} - - -void FolderInstanceProvider::instanceDirContentsChanged(const QString& path) -{ - Q_UNUSED(path); - emit instancesChanged(); -} - -void FolderInstanceProvider::on_InstFolderChanged(const Setting &setting, QVariant value) -{ - QString newInstDir = QDir(value.toString()).canonicalPath(); - if(newInstDir != m_instDir) - { - if(m_groupsLoaded) - { - saveGroupList(); - } - m_instDir = newInstDir; - m_groupsLoaded = false; - emit instancesChanged(); - } -} - -class InstanceStaging : public Task -{ -Q_OBJECT - const unsigned minBackoff = 1; - const unsigned maxBackoff = 16; -public: - InstanceStaging ( - FolderInstanceProvider * parent, - Task * child, - const QString & stagingPath, - const QString& instanceName, - const QString& groupName ) - : backoff(minBackoff, maxBackoff) - { - m_parent = parent; - m_child.reset(child); - 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, &InstanceStaging::childSucceded); - } - - virtual ~InstanceStaging() {}; - -protected: - virtual void executeTask() override - { - m_child->start(); - } - QStringList warnings() const override - { - return m_child->warnings(); - } - -private slots: - void childSucceded() - { - unsigned sleepTime = backoff(); - if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName)) - { - emitSucceeded(); - return; - } - // we actually failed, retry? - if(sleepTime == maxBackoff) - { - emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something.")); - return; - } - qDebug() << "Failed to commit instance" << m_instanceName << "Initiating backoff:" << sleepTime; - m_backoffTimer.start(sleepTime * 500); - } - void childFailed(const QString & reason) - { - m_parent->destroyStagingPath(m_stagingPath); - emitFailed(reason); - } - -private: - /* - * WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows. - * Basically, it starts messing things up while MultiMC is extracting/creating instances - * and causes that horrible failure that is NTFS to lock files in place because they are open. - */ - ExponentialSeries backoff; - QString m_stagingPath; - FolderInstanceProvider * m_parent; - unique_qobject_ptr m_child; - QString m_instanceName; - QString m_groupName; - QTimer m_backoffTimer; -}; - -#include "InstanceTask.h" -Task * FolderInstanceProvider::wrapInstanceTask(InstanceTask * task) -{ - auto stagingPath = getStagedInstancePath(); - task->setStagingPath(stagingPath); - task->setParentSettings(m_globalSettings); - return new InstanceStaging(this, task, stagingPath, task->name(), task->group()); -} - -QString FolderInstanceProvider::getStagedInstancePath() -{ - QString key = QUuid::createUuid().toString(); - QString relPath = FS::PathCombine("_MMC_TEMP/" , key); - QDir rootPath(m_instDir); - auto path = FS::PathCombine(m_instDir, relPath); - if(!rootPath.mkpath(relPath)) - { - return QString(); - } - return path; -} - -bool FolderInstanceProvider::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName) -{ - QDir dir; - QString instID = FS::DirNameFromString(instanceName, m_instDir); - { - WatchLock lock(m_watcher, m_instDir); - QString destination = FS::PathCombine(m_instDir, instID); - if(!dir.rename(path, destination)) - { - qWarning() << "Failed to move" << path << "to" << destination; - return false; - } - groupMap[instID] = groupName; - instanceSet.insert(instID); - emit groupsChanged({groupName}); - emit instancesChanged(); - } - saveGroupList(); - return true; -} - -bool FolderInstanceProvider::destroyStagingPath(const QString& keyPath) -{ - return FS::deletePath(keyPath); -} - -#include "FolderInstanceProvider.moc" diff --git a/api/logic/FolderInstanceProvider.h b/api/logic/FolderInstanceProvider.h deleted file mode 100644 index ce5337c1..00000000 --- a/api/logic/FolderInstanceProvider.h +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - - -#include -#include -#include -#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; - -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 discoverInstances(); - - /// used by InstanceList to (re)load an instance with the given @id. - 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); - - /** - * 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(); - /** - * 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); - /** - * Destroy a previously created staging area given by @keyPath - used when creation fails. - * Used by instance manipulation tasks. - */ - 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 groups); - - -public slots: - void on_InstFolderChanged(const Setting &setting, QVariant value); - -private slots: - void instanceDirContentsChanged(const QString &path); - void groupChanged(); - -private: /* methods */ - void loadGroupList(); - void saveGroupList(); - -private: /* data */ - SettingsObjectPtr m_globalSettings; - QString m_instDir; - QFileSystemWatcher * m_watcher; - QMap groupMap; - QSet instanceSet; - bool m_groupsLoaded = false; - bool m_instancesProbed = false; -}; diff --git a/api/logic/InstanceList.cpp b/api/logic/InstanceList.cpp index 7e2c03a9..616d2887 100644 --- a/api/logic/InstanceList.cpp +++ b/api/logic/InstanceList.cpp @@ -14,23 +14,50 @@ */ #include +#include #include #include #include #include #include +#include #include +#include +#include +#include +#include #include "InstanceList.h" #include "BaseInstance.h" - -#include "FolderInstanceProvider.h" +#include "InstanceTask.h" +#include "settings/INISettingsObject.h" +#include "minecraft/legacy/LegacyInstance.h" +#include "NullInstance.h" +#include "minecraft/MinecraftInstance.h" #include "FileSystem.h" +#include "ExponentialSeries.h" +#include "WatchLock.h" -InstanceList::InstanceList(QObject *parent) - : QAbstractListModel(parent) +const static int GROUP_FILE_FORMAT_VERSION = 1; + +InstanceList::InstanceList(SettingsObjectPtr settings, const QString & instDir, QObject *parent) + : QAbstractListModel(parent), m_globalSettings(settings) { resumeWatch(); + // Create aand normalize path + if (!QDir::current().exists(instDir)) + { + QDir::current().mkpath(instDir); + } + + connect(this, &InstanceList::instancesChanged, this, &InstanceList::providerUpdated); + connect(this, &InstanceList::groupsChanged, this, &InstanceList::groupsPublished); + + // NOTE: canonicalPath requires the path to exist. Do not move this above the creation block! + m_instDir = QDir(instDir).canonicalPath(); + m_watcher = new QFileSystemWatcher(this); + connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &InstanceList::instanceDirContentsChanged); + m_watcher->addPath(m_instDir); } InstanceList::~InstanceList() @@ -155,13 +182,44 @@ static QMap getIdMapping(const QList & return out; } +QList< InstanceId > InstanceList::discoverInstances() +{ + qDebug() << "Discovering instances in" << m_instDir; + QList out; + QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable | QDir::Hidden, QDirIterator::FollowSymlinks); + while (iter.hasNext()) + { + QString subDir = iter.next(); + QFileInfo dirInfo(subDir); + if (!QFileInfo(FS::PathCombine(subDir, "instance.cfg")).exists()) + continue; + // if it is a symlink, ignore it if it goes to the instance folder + if(dirInfo.isSymLink()) + { + QFileInfo targetInfo(dirInfo.symLinkTarget()); + QFileInfo instDirInfo(m_instDir); + if(targetInfo.canonicalPath() == instDirInfo.canonicalFilePath()) + { + qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder"; + continue; + } + } + auto id = dirInfo.fileName(); + out.append(id); + qDebug() << "Found instance ID" << id; + } + instanceSet = out.toSet(); + m_instancesProbed = true; + return out; +} + InstanceList::InstListError InstanceList::loadList() { auto existingIds = getIdMapping(m_instances); QList newList; - for(auto & id: m_provider->discoverInstances()) + for(auto & id: discoverInstances()) { if(existingIds.contains(id)) { @@ -171,7 +229,7 @@ InstanceList::InstListError InstanceList::loadList() } else { - InstancePtr instPtr = m_provider->loadInstance(id); + InstancePtr instPtr = loadInstance(id); if(instPtr) { newList.append(instPtr); @@ -275,12 +333,6 @@ void InstanceList::suspendWatch() void InstanceList::providerUpdated() { - auto provider = dynamic_cast(QObject::sender()); - if(!provider) - { - qWarning() << "InstanceList::providerUpdated triggered by a non-provider"; - return; - } m_dirty = true; if(m_watchLevel == 1) { @@ -293,13 +345,6 @@ void InstanceList::groupsPublished(QSet newGroups) m_groups.unite(newGroups); } -void InstanceList::addInstanceProvider(FolderInstanceProvider* provider) -{ - connect(provider, &FolderInstanceProvider::instancesChanged, this, &InstanceList::providerUpdated); - connect(provider, &FolderInstanceProvider::groupsChanged, this, &InstanceList::groupsPublished); - m_provider = provider; -} - InstancePtr InstanceList::getInstanceById(QString instId) const { if(instId.isEmpty()) @@ -340,3 +385,364 @@ void InstanceList::propertiesChanged(BaseInstance *inst) emit dataChanged(index(i), index(i)); } } + +InstancePtr InstanceList::loadInstance(const InstanceId& id) +{ + if(!m_groupsLoaded) + { + loadGroupList(); + } + + auto instanceRoot = FS::PathCombine(m_instDir, id); + auto instanceSettings = std::make_shared(FS::PathCombine(instanceRoot, "instance.cfg")); + InstancePtr inst; + + instanceSettings->registerSetting("InstanceType", "Legacy"); + + QString inst_type = instanceSettings->get("InstanceType").toString(); + + if (inst_type == "OneSix" || inst_type == "Nostalgia") + { + inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot)); + } + else if (inst_type == "Legacy") + { + inst.reset(new LegacyInstance(m_globalSettings, instanceSettings, instanceRoot)); + } + else + { + inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot)); + } + inst->init(); + auto iter = groupMap.find(id); + if (iter != groupMap.end()) + { + inst->setGroupInitial((*iter)); + } + connect(inst.get(), &BaseInstance::groupChanged, this, &InstanceList::groupChanged); + qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot(); + return inst; +} + +void InstanceList::saveGroupList() +{ + qDebug() << "Will save group list now."; + if(!m_instancesProbed) + { + qDebug() << "Group saving prevented because we don't know the full list of instances yet."; + return; + } + WatchLock foo(m_watcher, m_instDir); + QString groupFileName = m_instDir + "/instgroups.json"; + QMap> reverseGroupMap; + for (auto iter = groupMap.begin(); iter != groupMap.end(); iter++) + { + QString id = iter.key(); + QString group = iter.value(); + if (group.isEmpty()) + continue; + if(!instanceSet.contains(id)) + { + qDebug() << "Skipping saving missing instance" << id << "to groups list."; + continue; + } + + if (!reverseGroupMap.count(group)) + { + QSet set; + set.insert(id); + reverseGroupMap[group] = set; + } + else + { + QSet &set = reverseGroupMap[group]; + set.insert(id); + } + } + QJsonObject toplevel; + toplevel.insert("formatVersion", QJsonValue(QString("1"))); + QJsonObject groupsArr; + for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.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); + try + { + FS::write(groupFileName, doc.toJson()); + qDebug() << "Group list saved."; + } + catch (const FS::FileSystemException &e) + { + qCritical() << "Failed to write instance group file :" << e.cause(); + } +} + +void InstanceList::loadGroupList() +{ + qDebug() << "Will load group list now."; + QSet groupSet; + + QString groupFileName = m_instDir + "/instgroups.json"; + + // if there's no group file, fail + if (!QFileInfo(groupFileName).exists()) + return; + + QByteArray jsonData; + try + { + jsonData = FS::read(groupFileName); + } + catch (const FS::FileSystemException &e) + { + qCritical() << "Failed to read instance group file :" << e.cause(); + return; + } + + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); + + // if the json was bad, fail + if (error.error != QJsonParseError::NoError) + { + qCritical() << 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()) + { + qWarning() << "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()) + { + qWarning() << "Invalid group list JSON: 'groups' should be an object."; + return; + } + + groupMap.clear(); + + // 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()) + { + qWarning() << 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()) + { + qWarning() << 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 + groupSet.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; + } + } + m_groupsLoaded = true; + emit groupsChanged(groupSet); + qDebug() << "Group list loaded."; +} + +void InstanceList::groupChanged() +{ + // save the groups. save all of them. + auto instance = (BaseInstance *) QObject::sender(); + auto id = instance->id(); + groupMap[id] = instance->group(); + emit groupsChanged({instance->group()}); + saveGroupList(); +} + + +void InstanceList::instanceDirContentsChanged(const QString& path) +{ + Q_UNUSED(path); + emit instancesChanged(); +} + +void InstanceList::on_InstFolderChanged(const Setting &setting, QVariant value) +{ + QString newInstDir = QDir(value.toString()).canonicalPath(); + if(newInstDir != m_instDir) + { + if(m_groupsLoaded) + { + saveGroupList(); + } + m_instDir = newInstDir; + m_groupsLoaded = false; + emit instancesChanged(); + } +} + +class InstanceStaging : public Task +{ +Q_OBJECT + const unsigned minBackoff = 1; + const unsigned maxBackoff = 16; +public: + InstanceStaging ( + InstanceList * parent, + Task * child, + const QString & stagingPath, + const QString& instanceName, + const QString& groupName ) + : backoff(minBackoff, maxBackoff) + { + m_parent = parent; + m_child.reset(child); + 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, &InstanceStaging::childSucceded); + } + + virtual ~InstanceStaging() {}; + +protected: + virtual void executeTask() override + { + m_child->start(); + } + QStringList warnings() const override + { + return m_child->warnings(); + } + +private slots: + void childSucceded() + { + unsigned sleepTime = backoff(); + if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName)) + { + emitSucceeded(); + return; + } + // we actually failed, retry? + if(sleepTime == maxBackoff) + { + emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something.")); + return; + } + qDebug() << "Failed to commit instance" << m_instanceName << "Initiating backoff:" << sleepTime; + m_backoffTimer.start(sleepTime * 500); + } + void childFailed(const QString & reason) + { + m_parent->destroyStagingPath(m_stagingPath); + emitFailed(reason); + } + +private: + /* + * WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows. + * Basically, it starts messing things up while MultiMC is extracting/creating instances + * and causes that horrible failure that is NTFS to lock files in place because they are open. + */ + ExponentialSeries backoff; + QString m_stagingPath; + InstanceList * m_parent; + unique_qobject_ptr m_child; + QString m_instanceName; + QString m_groupName; + QTimer m_backoffTimer; +}; + +Task * InstanceList::wrapInstanceTask(InstanceTask * task) +{ + auto stagingPath = getStagedInstancePath(); + task->setStagingPath(stagingPath); + task->setParentSettings(m_globalSettings); + return new InstanceStaging(this, task, stagingPath, task->name(), task->group()); +} + +QString InstanceList::getStagedInstancePath() +{ + QString key = QUuid::createUuid().toString(); + QString relPath = FS::PathCombine("_MMC_TEMP/" , key); + QDir rootPath(m_instDir); + auto path = FS::PathCombine(m_instDir, relPath); + if(!rootPath.mkpath(relPath)) + { + return QString(); + } + return path; +} + +bool InstanceList::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName) +{ + QDir dir; + QString instID = FS::DirNameFromString(instanceName, m_instDir); + { + WatchLock lock(m_watcher, m_instDir); + QString destination = FS::PathCombine(m_instDir, instID); + if(!dir.rename(path, destination)) + { + qWarning() << "Failed to move" << path << "to" << destination; + return false; + } + groupMap[instID] = groupName; + instanceSet.insert(instID); + emit groupsChanged({groupName}); + emit instancesChanged(); + } + saveGroupList(); + return true; +} + +bool InstanceList::destroyStagingPath(const QString& keyPath) +{ + return FS::deletePath(keyPath); +} + +#include "InstanceList.moc" \ No newline at end of file diff --git a/api/logic/InstanceList.h b/api/logic/InstanceList.h index 14b80a78..a487a7ca 100644 --- a/api/logic/InstanceList.h +++ b/api/logic/InstanceList.h @@ -21,18 +21,33 @@ #include #include "BaseInstance.h" -#include "FolderInstanceProvider.h" #include "multimc_logic_export.h" #include "QObjectPtr.h" +class QFileSystemWatcher; +class InstanceTask; +using InstanceId = QString; +using GroupId = QString; +using InstanceLocator = std::pair; + +enum class InstCreateError +{ + NoCreateError = 0, + NoSuchVersion, + UnknownCreateError, + InstExists, + CantCreateDir +}; + + class MULTIMC_LOGIC_EXPORT InstanceList : public QAbstractListModel { Q_OBJECT public: - explicit InstanceList(QObject *parent = 0); + explicit InstanceList(SettingsObjectPtr settings, const QString & instDir, QObject *parent = 0); virtual ~InstanceList(); public: @@ -71,8 +86,6 @@ public: InstListError loadList(); void saveNow(); - /// Add an instance provider. Takes ownership of it. Should only be done before the first load. - void addInstanceProvider(FolderInstanceProvider * provider); InstancePtr getInstanceById(QString id) const; QModelIndex getInstanceIndexById(const QString &id) const; @@ -81,24 +94,63 @@ public: void deleteGroup(const GroupId & name); void deleteInstance(const InstanceId & id); + // Wrap an instance creation task in some more task machinery and make it ready to be used + Task * wrapInstanceTask(InstanceTask * task); + + /** + * 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(); + + /** + * 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); + + /** + * Destroy a previously created staging area given by @keyPath - used when creation fails. + * Used by instance manipulation tasks. + */ + bool destroyStagingPath(const QString & keyPath); + signals: void dataIsInvalid(); + void instancesChanged(); + void groupsChanged(QSet groups); + +public slots: + void on_InstFolderChanged(const Setting &setting, QVariant value); private slots: void propertiesChanged(BaseInstance *inst); void groupsPublished(QSet); void providerUpdated(); + void instanceDirContentsChanged(const QString &path); + void groupChanged(); private: int getInstIndex(BaseInstance *inst) const; void suspendWatch(); void resumeWatch(); void add(const QList &list); + void loadGroupList(); + void saveGroupList(); + QList discoverInstances(); + InstancePtr loadInstance(const InstanceId& id); -protected: +private: int m_watchLevel = 0; bool m_dirty = false; QList m_instances; QSet m_groups; - FolderInstanceProvider * m_provider; + + SettingsObjectPtr m_globalSettings; + QString m_instDir; + QFileSystemWatcher * m_watcher; + QMap groupMap; + QSet instanceSet; + bool m_groupsLoaded = false; + bool m_instancesProbed = false; }; diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index a84af880..9a1efb2b 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -86,7 +86,6 @@ #include "dialogs/EditAccountDialog.h" #include "dialogs/NotificationDialog.h" #include "dialogs/ExportInstanceDialog.h" -#include #include #include "UpdateController.h" #include "KonamiCode.h" @@ -1279,11 +1278,8 @@ void MainWindow::runModalTask(Task *task) void MainWindow::instanceFromInstanceTask(InstanceTask *rawTask) { - std::unique_ptr task(MMC->folderProvider()->wrapInstanceTask(rawTask)); + std::unique_ptr task(MMC->instances()->wrapInstanceTask(rawTask)); runModalTask(task.get()); - - // FIXME: handle instance selection after creation - // finalizeInstance(newInstance); } void MainWindow::on_actionCopyInstance_triggered() @@ -1299,11 +1295,8 @@ void MainWindow::on_actionCopyInstance_triggered() copyTask->setName(copyInstDlg.instName()); copyTask->setGroup(copyInstDlg.instGroup()); copyTask->setIcon(copyInstDlg.iconKey()); - std::unique_ptr task(MMC->folderProvider()->wrapInstanceTask(copyTask)); + std::unique_ptr task(MMC->instances()->wrapInstanceTask(copyTask)); runModalTask(task.get()); - - // FIXME: handle instance selection after creation - // finalizeInstance(newInstance); } void MainWindow::finalizeInstance(InstancePtr inst) diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 191f52fe..3a9c281e 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -36,7 +36,6 @@ #include "dialogs/CustomMessageBox.h" #include "InstanceList.h" -#include "FolderInstanceProvider.h" #include #include "icons/IconList.h" @@ -597,10 +596,8 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) { qWarning() << "Your instance path contains \'!\' and this is known to cause java problems"; } - m_instances.reset(new InstanceList(this)); - m_instanceFolder = new FolderInstanceProvider(m_settings, instDir); - connect(InstDirSetting.get(), &Setting::SettingChanged, m_instanceFolder, &FolderInstanceProvider::on_InstFolderChanged); - m_instances->addInstanceProvider(m_instanceFolder); + m_instances.reset(new InstanceList(m_settings, instDir, this)); + connect(InstDirSetting.get(), &Setting::SettingChanged, m_instances.get(), &InstanceList::on_InstFolderChanged); qDebug() << "Loading Instances..."; m_instances->loadList(); qDebug() << "<> Instances loaded."; diff --git a/application/pages/instance/LegacyUpgradePage.cpp b/application/pages/instance/LegacyUpgradePage.cpp index 7cd29a62..0d2e5996 100644 --- a/application/pages/instance/LegacyUpgradePage.cpp +++ b/application/pages/instance/LegacyUpgradePage.cpp @@ -1,10 +1,10 @@ #include "LegacyUpgradePage.h" #include "ui_LegacyUpgradePage.h" +#include "InstanceList.h" #include "minecraft/legacy/LegacyInstance.h" #include "minecraft/legacy/LegacyUpgradeTask.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" #include "dialogs/CustomMessageBox.h" #include "dialogs/ProgressDialog.h" @@ -40,7 +40,7 @@ void LegacyUpgradePage::on_upgradeButton_clicked() upgradeTask->setName(newName); upgradeTask->setGroup(m_inst->group()); upgradeTask->setIcon(m_inst->iconKey()); - std::unique_ptr task(MMC->folderProvider()->wrapInstanceTask(upgradeTask)); + std::unique_ptr task(MMC->instances()->wrapInstanceTask(upgradeTask)); runModalTask(task.get()); } diff --git a/application/pages/modplatform/FTBPage.cpp b/application/pages/modplatform/FTBPage.cpp index ae4dd92e..1884bbfb 100644 --- a/application/pages/modplatform/FTBPage.cpp +++ b/application/pages/modplatform/FTBPage.cpp @@ -2,7 +2,6 @@ #include "ui_FTBPage.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" #include "dialogs/CustomMessageBox.h" #include "dialogs/NewInstanceDialog.h" #include "modplatform/ftb/FtbPackFetchTask.h" diff --git a/application/pages/modplatform/ImportPage.cpp b/application/pages/modplatform/ImportPage.cpp index 5e476195..3cd7c2cf 100644 --- a/application/pages/modplatform/ImportPage.cpp +++ b/application/pages/modplatform/ImportPage.cpp @@ -2,9 +2,6 @@ #include "ui_ImportPage.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" -#include "dialogs/CustomMessageBox.h" -#include "dialogs/ProgressDialog.h" #include "dialogs/NewInstanceDialog.h" #include #include diff --git a/application/pages/modplatform/TechnicPage.cpp b/application/pages/modplatform/TechnicPage.cpp index 1ee3ca9e..2f95bec8 100644 --- a/application/pages/modplatform/TechnicPage.cpp +++ b/application/pages/modplatform/TechnicPage.cpp @@ -2,9 +2,6 @@ #include "ui_TechnicPage.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" -#include "dialogs/CustomMessageBox.h" -#include "dialogs/ProgressDialog.h" #include "dialogs/NewInstanceDialog.h" TechnicPage::TechnicPage(NewInstanceDialog* dialog, QWidget *parent) diff --git a/application/pages/modplatform/TwitchPage.cpp b/application/pages/modplatform/TwitchPage.cpp index f9b326ae..a984c01c 100644 --- a/application/pages/modplatform/TwitchPage.cpp +++ b/application/pages/modplatform/TwitchPage.cpp @@ -2,9 +2,6 @@ #include "ui_TwitchPage.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" -#include "dialogs/CustomMessageBox.h" -#include "dialogs/ProgressDialog.h" #include "dialogs/NewInstanceDialog.h" TwitchPage::TwitchPage(NewInstanceDialog* dialog, QWidget *parent) diff --git a/application/pages/modplatform/VanillaPage.cpp b/application/pages/modplatform/VanillaPage.cpp index f0f2a95e..77362fcc 100644 --- a/application/pages/modplatform/VanillaPage.cpp +++ b/application/pages/modplatform/VanillaPage.cpp @@ -2,9 +2,6 @@ #include "ui_VanillaPage.h" #include "MultiMC.h" -#include "FolderInstanceProvider.h" -#include "dialogs/CustomMessageBox.h" -#include "dialogs/ProgressDialog.h" #include #include